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>
36 #include <linux/netfilter_ipv4/ip_tables.h>
39 #include "src/shared/util.h"
42 * Some comments on how the iptables API works (some of them from the
43 * source code from iptables and the kernel):
45 * - valid_hooks: bit indicates valid IDs for hook_entry
46 * - hook_entry[ID] offset to the chain start
47 * - overflows should be end of entry chains, and uncodintional policy nodes.
48 * - policy entry: last entry in a chain
49 * - user chain: end of last builtin + policy entry
50 * - final entry must be error node
51 * - Underflows must be unconditional and use the STANDARD target with
53 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
54 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
55 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
56 * table header/meta info. The table is appended after the header. The entries
57 * are of the type struct ipt_entry.
58 * - After the ipt_entry the matches are appended. After the matches
59 * the target is appended.
60 * - ipt_entry->target_offset = Size of ipt_entry + matches
61 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
62 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
63 * - hook_entry and overflow mark the begining and the end of a chain, e.g
64 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
65 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
66 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
67 * the last element). FORWARD has one entry starting/ending at 352. The entry
68 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
69 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
71 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
72 * writing that table looks like this:
74 * filter valid_hooks 0x0000000e num_entries 5 size 856
75 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
76 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
77 * entry 0x699d30 offset 0 size 224
78 * RULE match 0x699da0 target 0x699dd0
79 * match mark match 0x3e7
80 * target LOG flags 0 level 4
83 * entry 0x699e10 offset 224 size 152
84 * RULE match 0x699e80 target 0x699e80
88 * entry 0x699ea8 offset 376 size 152
89 * RULE match 0x699f18 target 0x699f18
93 * entry 0x699f40 offset 528 size 152
94 * RULE match 0x699fb0 target 0x699fb0
98 * entry 0x699fd8 offset 680 size 176
99 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
101 * Reading the filter table looks like this:
103 * filter valid_hooks 0x0000000e num_entries 5 size 856
104 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
105 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
106 * entry 0x25fec28 offset 0 size 224
107 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
108 * match mark match 0x3e7
109 * target LOG flags 0 level 4
110 * src 0.0.0.0/0.0.0.0
111 * dst 0.0.0.0/0.0.0.0
112 * entry 0x25fed08 offset 224 size 152
113 * RULE match 0x25fed78 target 0x25fed78
115 * src 0.0.0.0/0.0.0.0
116 * dst 0.0.0.0/0.0.0.0
117 * entry 0x25feda0 offset 376 size 152
118 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
120 * src 0.0.0.0/0.0.0.0
121 * dst 0.0.0.0/0.0.0.0
122 * entry 0x25fee38 offset 528 size 152
123 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
125 * src 0.0.0.0/0.0.0.0
126 * dst 0.0.0.0/0.0.0.0
127 * entry 0x25feed0 offset 680 size 176
131 static const char *hooknames[] = {
132 [NF_IP_PRE_ROUTING] = "PREROUTING",
133 [NF_IP_LOCAL_IN] = "INPUT",
134 [NF_IP_FORWARD] = "FORWARD",
135 [NF_IP_LOCAL_OUT] = "OUTPUT",
136 [NF_IP_POST_ROUTING] = "POSTROUTING",
139 #define LABEL_ACCEPT "ACCEPT"
140 #define LABEL_DROP "DROP"
141 #define LABEL_QUEUE "QUEUE"
142 #define LABEL_RETURN "RETURN"
144 #define XT_OPTION_OFFSET_SCALE 256
146 #define MIN_ALIGN (__alignof__(struct ipt_entry))
148 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
150 struct error_target {
151 struct xt_entry_target t;
152 char error[IPT_TABLE_MAXNAMELEN];
155 struct connman_iptables_entry {
160 struct ipt_entry *entry;
163 struct connman_iptables {
167 struct ipt_getinfo *info;
168 struct ipt_get_entries *blob_entries;
170 unsigned int num_entries;
171 unsigned int old_entries;
174 unsigned int underflow[NF_INET_NUMHOOKS];
175 unsigned int hook_entry[NF_INET_NUMHOOKS];
180 static GHashTable *table_hash = NULL;
181 static bool debug_enabled = false;
183 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
184 unsigned int hook, size_t size,
185 unsigned int offset, void *user_data);
187 static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
191 if (*valid_hooks == 0)
192 return NF_INET_NUMHOOKS;
194 h = __builtin_ffs(*valid_hooks) - 1;
195 *valid_hooks ^= (1 << h);
200 static int iterate_entries(struct ipt_entry *entries,
201 unsigned int valid_hooks,
202 unsigned int *hook_entry,
203 unsigned int *underflow,
204 size_t size, iterate_entries_cb_t cb,
207 unsigned int offset, h, hook;
209 struct ipt_entry *entry;
211 h = next_hook_entry_index(&valid_hooks);
214 for (offset = 0, entry = entries; offset < size;
215 offset += entry->next_offset) {
217 entry = (void *)entries + offset;
220 * Updating builtin, hook and h is very tricky.
222 * - builtin is only set to the current hook number
223 * if the current entry is the hook entry (aka chain
224 * head). And only for builtin chains, never for
226 * - hook is the current hook number. If we
227 * look at user chains it needs to be NF_INET_NETNUMHOOKS.
228 * - h is the next hook entry. Thous we need to be carefully
229 * not to access the table when h is NF_INET_NETNUMHOOKS.
231 if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
236 if (h == NF_INET_NUMHOOKS)
239 if (h < NF_INET_NUMHOOKS && underflow[h] <= offset)
240 h = next_hook_entry_index(&valid_hooks);
242 err = cb(entry, builtin, hook, size, offset, user_data);
250 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
251 size_t size, unsigned int offset,
254 iterate_entries_cb_t cb = user_data;
256 DBG("entry %p hook %u offset %u size %u packets %"PRIu64" bytes %"PRIu64,
257 entry, hook, offset, (unsigned int) entry->next_offset,
258 (uint64_t) entry->counters.pcnt, (uint64_t) entry->counters.bcnt);
260 return cb(entry, builtin, hook, size, offset, NULL);
263 static int target_to_verdict(const char *target_name)
265 if (!g_strcmp0(target_name, LABEL_ACCEPT))
266 return -NF_ACCEPT - 1;
268 if (!g_strcmp0(target_name, LABEL_DROP))
271 if (!g_strcmp0(target_name, LABEL_QUEUE))
272 return -NF_QUEUE - 1;
274 if (!g_strcmp0(target_name, LABEL_RETURN))
280 static bool is_builtin_target(const char *target_name)
282 if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
283 !g_strcmp0(target_name, LABEL_DROP) ||
284 !g_strcmp0(target_name, LABEL_QUEUE) ||
285 !g_strcmp0(target_name, LABEL_RETURN))
291 static bool is_jump(struct connman_iptables_entry *e)
293 struct xt_entry_target *target;
295 target = ipt_get_target(e->entry);
297 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
298 struct xt_standard_target *t;
300 t = (struct xt_standard_target *)target;
302 switch (t->verdict) {
318 static bool is_fallthrough(struct connman_iptables_entry *e)
320 struct xt_entry_target *target;
322 target = ipt_get_target(e->entry);
323 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
324 struct xt_standard_target *t;
326 t = (struct xt_standard_target *)target;
333 static bool is_chain(struct connman_iptables *table,
334 struct connman_iptables_entry *e)
336 struct ipt_entry *entry;
337 struct xt_entry_target *target;
343 target = ipt_get_target(entry);
344 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
350 static GList *find_chain_head(struct connman_iptables *table,
351 const char *chain_name)
354 struct connman_iptables_entry *head;
355 struct ipt_entry *entry;
356 struct xt_entry_target *target;
359 for (list = table->entries; list; list = list->next) {
364 builtin = head->builtin;
365 if (builtin >= 0 && !g_strcmp0(hooknames[builtin], chain_name))
368 /* User defined chain */
369 target = ipt_get_target(entry);
370 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET) &&
371 !g_strcmp0((char *)target->data, chain_name))
378 static GList *find_chain_tail(struct connman_iptables *table,
379 const char *chain_name)
381 struct connman_iptables_entry *tail;
382 GList *chain_head, *list;
384 chain_head = find_chain_head(table, chain_name);
388 /* Then we look for the next chain */
389 for (list = chain_head->next; list; list = list->next) {
392 if (is_chain(table, tail))
396 /* Nothing found, we return the table end */
397 return g_list_last(table->entries);
401 static void update_offsets(struct connman_iptables *table)
404 struct connman_iptables_entry *entry, *prev_entry;
406 for (list = table->entries; list; list = list->next) {
409 if (list == table->entries) {
416 prev_entry = prev->data;
418 entry->offset = prev_entry->offset +
419 prev_entry->entry->next_offset;
423 static void update_targets_reference(struct connman_iptables *table,
424 struct connman_iptables_entry *entry_before,
425 struct connman_iptables_entry *modified_entry,
428 struct connman_iptables_entry *tmp;
429 struct xt_standard_target *t;
433 offset = modified_entry->entry->next_offset;
435 for (list = table->entries; list; list = list->next) {
441 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
444 if (t->verdict >= entry_before->offset)
445 t->verdict -= offset;
447 if (t->verdict > entry_before->offset)
448 t->verdict += offset;
452 if (is_fallthrough(modified_entry)) {
453 t = (struct xt_standard_target *)
454 ipt_get_target(modified_entry->entry);
456 t->verdict = entry_before->offset +
457 modified_entry->entry->target_offset +
458 ALIGN(sizeof(struct xt_standard_target));
459 t->target.u.target_size =
460 ALIGN(sizeof(struct xt_standard_target));
464 static int iptables_add_entry(struct connman_iptables *table,
465 struct ipt_entry *entry, GList *before,
466 int builtin, int counter_idx)
468 struct connman_iptables_entry *e, *entry_before;
473 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
478 e->builtin = builtin;
479 e->counter_idx = counter_idx;
481 table->entries = g_list_insert_before(table->entries, before, e);
482 table->num_entries++;
483 table->size += entry->next_offset;
486 e->offset = table->size - entry->next_offset;
491 entry_before = before->data;
494 * We've just appended/insterted a new entry. All references
495 * should be bumped accordingly.
497 update_targets_reference(table, entry_before, e, false);
499 update_offsets(table);
504 static int remove_table_entry(struct connman_iptables *table,
505 struct connman_iptables_entry *entry)
509 table->num_entries--;
510 table->size -= entry->entry->next_offset;
511 removed = entry->entry->next_offset;
513 table->entries = g_list_remove(table->entries, entry);
515 g_free(entry->entry);
521 static void delete_update_hooks(struct connman_iptables *table,
522 int builtin, GList *chain_head,
525 struct connman_iptables_entry *e;
528 e = chain_head->data;
529 e->builtin = builtin;
531 table->underflow[builtin] -= removed;
533 for (list = chain_head->next; list; list = list->next) {
539 table->hook_entry[e->builtin] -= removed;
540 table->underflow[e->builtin] -= removed;
544 static int iptables_flush_chain(struct connman_iptables *table,
547 GList *chain_head, *chain_tail, *list, *next;
548 struct connman_iptables_entry *entry;
549 int builtin, removed = 0;
551 DBG("table %s chain %s", table->name, name);
553 chain_head = find_chain_head(table, name);
557 chain_tail = find_chain_tail(table, name);
561 entry = chain_head->data;
562 builtin = entry->builtin;
567 list = chain_head->next;
569 if (list == chain_tail->prev)
572 while (list != chain_tail->prev) {
574 next = g_list_next(list);
576 removed += remove_table_entry(table, entry);
582 delete_update_hooks(table, builtin, chain_tail->prev, removed);
584 update_offsets(table);
589 static int iptables_add_chain(struct connman_iptables *table,
593 struct ipt_entry *entry_head;
594 struct ipt_entry *entry_return;
595 struct error_target *error;
596 struct ipt_standard_target *standard;
597 u_int16_t entry_head_size, entry_return_size;
599 DBG("table %s chain %s", table->name, name);
601 last = g_list_last(table->entries);
604 * An empty chain is composed of:
605 * - A head entry, with no match and an error target.
606 * The error target data is the chain name.
607 * - A tail entry, with no match and a standard target.
608 * The standard target verdict is XT_RETURN (return to the
613 entry_head_size = ALIGN(sizeof(struct ipt_entry)) +
614 ALIGN(sizeof(struct error_target));
615 entry_head = g_try_malloc0(entry_head_size);
619 entry_head->target_offset = ALIGN(sizeof(struct ipt_entry));
620 entry_head->next_offset = entry_head_size;
622 error = (struct error_target *) entry_head->elems;
623 g_stpcpy(error->t.u.user.name, IPT_ERROR_TARGET);
624 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
625 g_stpcpy(error->error, name);
627 if (iptables_add_entry(table, entry_head, last, -1, -1) < 0)
631 entry_return_size = ALIGN(sizeof(struct ipt_entry))+
632 ALIGN(sizeof(struct ipt_standard_target));
633 entry_return = g_try_malloc0(entry_return_size);
637 entry_return->target_offset = ALIGN(sizeof(struct ipt_entry));
638 entry_return->next_offset = entry_return_size;
640 standard = (struct ipt_standard_target *) entry_return->elems;
641 standard->target.u.user.target_size =
642 ALIGN(sizeof(struct ipt_standard_target));
643 standard->verdict = XT_RETURN;
645 if (iptables_add_entry(table, entry_return, last, -1, -1) < 0)
651 g_free(entry_return);
658 static int iptables_delete_chain(struct connman_iptables *table,
661 struct connman_iptables_entry *entry;
662 GList *chain_head, *chain_tail;
664 DBG("table %s chain %s", table->name, name);
666 chain_head = find_chain_head(table, name);
670 entry = chain_head->data;
672 /* We cannot remove builtin chain */
673 if (entry->builtin >= 0)
676 chain_tail = find_chain_tail(table, name);
680 /* Chain must be flushed */
681 if (chain_head->next != chain_tail->prev)
684 remove_table_entry(table, entry);
686 entry = chain_tail->prev->data;
687 remove_table_entry(table, entry);
689 update_offsets(table);
694 static struct ipt_entry *new_rule(struct ipt_ip *ip,
695 const char *target_name, struct xtables_target *xt_t,
696 struct xtables_rule_match *xt_rm)
698 struct xtables_rule_match *tmp_xt_rm;
699 struct ipt_entry *new_entry;
700 size_t match_size, target_size;
703 for (tmp_xt_rm = xt_rm; tmp_xt_rm; tmp_xt_rm = tmp_xt_rm->next)
704 match_size += tmp_xt_rm->match->m->u.match_size;
707 target_size = xt_t->t->u.target_size;
709 target_size = ALIGN(sizeof(struct xt_standard_target));
711 new_entry = g_try_malloc0(ALIGN(sizeof(struct ipt_entry)) +
712 target_size + match_size);
716 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
718 new_entry->target_offset = ALIGN(sizeof(struct ipt_entry)) +
720 new_entry->next_offset = ALIGN(sizeof(struct ipt_entry)) +
721 target_size + match_size;
724 for (tmp_xt_rm = xt_rm; tmp_xt_rm;
725 tmp_xt_rm = tmp_xt_rm->next) {
726 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
727 tmp_xt_rm->match->m->u.match_size);
728 match_size += tmp_xt_rm->match->m->u.match_size;
732 struct xt_entry_target *entry_target;
734 entry_target = ipt_get_target(new_entry);
735 memcpy(entry_target, xt_t->t, target_size);
741 static void update_hooks(struct connman_iptables *table, GList *chain_head,
742 struct ipt_entry *entry)
745 struct connman_iptables_entry *head, *e;
751 head = chain_head->data;
753 builtin = head->builtin;
757 table->underflow[builtin] += entry->next_offset;
759 for (list = chain_head->next; list; list = list->next) {
762 builtin = e->builtin;
766 table->hook_entry[builtin] += entry->next_offset;
767 table->underflow[builtin] += entry->next_offset;
771 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
772 struct ipt_ip *ip, const char *chain_name,
773 const char *target_name,
774 struct xtables_target *xt_t,
775 int *builtin, struct xtables_rule_match *xt_rm,
778 GList *chain_tail, *chain_head;
779 struct ipt_entry *new_entry;
780 struct connman_iptables_entry *head;
782 chain_head = find_chain_head(table, chain_name);
786 chain_tail = find_chain_tail(table, chain_name);
790 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
794 update_hooks(table, chain_head, new_entry);
797 * If the chain is builtin, and does not have any rule,
798 * then the one that we're inserting is becoming the head
799 * and thus needs the builtin flag.
801 head = chain_head->data;
802 if (head->builtin < 0)
804 else if (insert || chain_head == chain_tail->prev) {
805 *builtin = head->builtin;
812 static int iptables_append_rule(struct connman_iptables *table,
813 struct ipt_ip *ip, const char *chain_name,
814 const char *target_name,
815 struct xtables_target *xt_t,
816 struct xtables_rule_match *xt_rm)
818 struct ipt_entry *new_entry;
819 int builtin = -1, ret;
822 DBG("table %s chain %s", table->name, chain_name);
824 chain_tail = find_chain_tail(table, chain_name);
828 new_entry = prepare_rule_inclusion(table, ip, chain_name,
829 target_name, xt_t, &builtin, xt_rm, false);
833 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin, -1);
840 static int iptables_insert_rule(struct connman_iptables *table,
841 struct ipt_ip *ip, const char *chain_name,
842 const char *target_name,
843 struct xtables_target *xt_t,
844 struct xtables_rule_match *xt_rm)
846 struct ipt_entry *new_entry;
847 int builtin = -1, ret;
850 DBG("table %s chain %s", table->name, chain_name);
852 chain_head = find_chain_head(table, chain_name);
856 new_entry = prepare_rule_inclusion(table, ip, chain_name,
857 target_name, xt_t, &builtin, xt_rm, true);
862 chain_head = chain_head->next;
864 ret = iptables_add_entry(table, new_entry, chain_head, builtin, -1);
871 static bool is_same_ipt_entry(struct ipt_entry *i_e1,
872 struct ipt_entry *i_e2)
874 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
877 if (i_e1->target_offset != i_e2->target_offset)
880 if (i_e1->next_offset != i_e2->next_offset)
886 static bool is_same_target(struct xt_entry_target *xt_e_t1,
887 struct xt_entry_target *xt_e_t2)
891 if (!xt_e_t1 || !xt_e_t2)
894 if (g_strcmp0(xt_e_t1->u.user.name, "") == 0 &&
895 g_strcmp0(xt_e_t2->u.user.name, "") == 0) {
898 } else if (g_strcmp0(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
899 struct xt_standard_target *xt_s_t1;
900 struct xt_standard_target *xt_s_t2;
902 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
903 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
905 if (xt_s_t1->verdict != xt_s_t2->verdict)
908 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
911 if (g_strcmp0(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
914 for (i = 0; i < xt_e_t1->u.target_size -
915 sizeof(struct xt_standard_target); i++) {
916 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
924 static bool is_same_match(struct xt_entry_match *xt_e_m1,
925 struct xt_entry_match *xt_e_m2)
929 if (!xt_e_m1 || !xt_e_m2)
932 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
935 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
938 if (g_strcmp0(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
941 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
943 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
950 static GList *find_existing_rule(struct connman_iptables *table,
951 struct ipt_ip *ip, const char *chain_name,
952 const char *target_name,
953 struct xtables_target *xt_t,
955 struct xtables_rule_match *xt_rm)
957 GList *chain_tail, *chain_head, *list;
958 struct xt_entry_target *xt_e_t = NULL;
959 struct xt_entry_match *xt_e_m = NULL;
960 struct connman_iptables_entry *entry;
961 struct ipt_entry *entry_test;
964 chain_head = find_chain_head(table, chain_name);
968 chain_tail = find_chain_tail(table, chain_name);
972 if (!xt_t && !matches)
975 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
980 xt_e_t = ipt_get_target(entry_test);
982 xt_e_m = (struct xt_entry_match *)entry_test->elems;
984 entry = chain_head->data;
985 builtin = entry->builtin;
990 list = chain_head->next;
992 for (; list != chain_tail->prev; list = list->next) {
993 struct connman_iptables_entry *tmp;
994 struct ipt_entry *tmp_e;
999 if (!is_same_ipt_entry(entry_test, tmp_e))
1003 struct xt_entry_target *tmp_xt_e_t;
1005 tmp_xt_e_t = ipt_get_target(tmp_e);
1007 if (!is_same_target(tmp_xt_e_t, xt_e_t))
1012 struct xt_entry_match *tmp_xt_e_m;
1014 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
1016 if (!is_same_match(tmp_xt_e_m, xt_e_m))
1025 if (list != chain_tail->prev)
1031 static int iptables_delete_rule(struct connman_iptables *table,
1032 struct ipt_ip *ip, const char *chain_name,
1033 const char *target_name,
1034 struct xtables_target *xt_t,
1036 struct xtables_rule_match *xt_rm)
1038 struct connman_iptables_entry *entry;
1039 GList *chain_head, *chain_tail, *list;
1040 int builtin, removed;
1043 DBG("table %s chain %s", table->name, chain_name);
1047 chain_head = find_chain_head(table, chain_name);
1051 chain_tail = find_chain_tail(table, chain_name);
1055 list = find_existing_rule(table, ip, chain_name, target_name,
1056 xt_t, matches, xt_rm);
1060 entry = chain_head->data;
1061 builtin = entry->builtin;
1063 if (builtin >= 0 && list == chain_head) {
1065 * We are about to remove the first rule in the
1066 * chain. In this case we need to store the builtin
1067 * value to the new chain_head.
1069 * Note, for builtin chains, chain_head->next is
1070 * always valid. A builtin chain has always a policy
1073 chain_head = chain_head->next;
1075 entry = chain_head->data;
1076 entry->builtin = builtin;
1083 /* We have deleted a rule,
1084 * all references should be bumped accordingly */
1086 update_targets_reference(table, list->next->data,
1089 removed += remove_table_entry(table, entry);
1092 delete_update_hooks(table, builtin, chain_head, removed);
1094 update_offsets(table);
1099 static int iptables_change_policy(struct connman_iptables *table,
1100 const char *chain_name, const char *policy)
1102 GList *chain_head, *chain_tail;
1103 struct connman_iptables_entry *entry;
1104 struct xt_entry_target *target;
1105 struct xt_standard_target *t;
1108 DBG("table %s chain %s policy %s", table->name, chain_name, policy);
1110 verdict = target_to_verdict(policy);
1112 case -NF_ACCEPT - 1:
1119 chain_head = find_chain_head(table, chain_name);
1123 entry = chain_head->data;
1124 if (entry->builtin < 0)
1127 chain_tail = find_chain_tail(table, chain_name);
1131 entry = chain_tail->prev->data;
1132 target = ipt_get_target(entry->entry);
1134 t = (struct xt_standard_target *)target;
1135 if (t->verdict != verdict)
1136 entry->counter_idx = -1;
1137 t->verdict = verdict;
1142 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1144 struct ipt_replace *r;
1146 struct connman_iptables_entry *e;
1147 unsigned char *entry_index;
1149 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1153 memset(r, 0, sizeof(*r) + table->size);
1155 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1156 * table->old_entries);
1162 g_stpcpy(r->name, table->info->name);
1163 r->num_entries = table->num_entries;
1164 r->size = table->size;
1166 r->num_counters = table->old_entries;
1167 r->valid_hooks = table->info->valid_hooks;
1169 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1170 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1172 entry_index = (unsigned char *)r->entries;
1173 for (list = table->entries; list; list = list->next) {
1176 memcpy(entry_index, e->entry, e->entry->next_offset);
1177 entry_index += e->entry->next_offset;
1183 static void dump_ip(struct ipt_entry *entry)
1185 struct ipt_ip *ip = &entry->ip;
1186 char ip_string[INET6_ADDRSTRLEN];
1187 char ip_mask[INET6_ADDRSTRLEN];
1189 if (strlen(ip->iniface))
1190 DBG("\tin %s", ip->iniface);
1192 if (strlen(ip->outiface))
1193 DBG("\tout %s", ip->outiface);
1195 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) &&
1196 inet_ntop(AF_INET, &ip->smsk, ip_mask,
1198 DBG("\tsrc %s/%s", ip_string, ip_mask);
1200 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) &&
1201 inet_ntop(AF_INET, &ip->dmsk, ip_mask,
1203 DBG("\tdst %s/%s", ip_string, ip_mask);
1206 static void dump_target(struct ipt_entry *entry)
1209 struct xtables_target *xt_t;
1210 struct xt_entry_target *target;
1212 target = ipt_get_target(entry);
1214 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
1215 struct xt_standard_target *t;
1217 t = (struct xt_standard_target *)target;
1219 switch (t->verdict) {
1221 DBG("\ttarget RETURN");
1224 case -NF_ACCEPT - 1:
1225 DBG("\ttarget ACCEPT");
1229 DBG("\ttarget DROP");
1233 DBG("\ttarget QUEUE");
1237 DBG("\ttarget STOP");
1241 DBG("\tJUMP %u", t->verdict);
1245 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1246 XTF_LOAD_MUST_SUCCEED);
1249 xt_t->print(NULL, target, 1);
1251 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1253 DBG("\ttarget %s", target->u.user.name);
1259 xt_t->print(NULL, target, 1);
1263 if (xt_t == xt_t->next)
1267 static void dump_match(struct ipt_entry *entry)
1269 struct xtables_match *xt_m;
1270 struct xt_entry_match *match;
1272 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1275 match = (struct xt_entry_match *) entry->elems;
1277 if (!strlen(match->u.user.name))
1280 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1286 xt_m->print(NULL, match, 1);
1290 if (xt_m == xt_m->next)
1294 DBG("\tmatch %s", match->u.user.name);
1298 static int dump_entry(struct ipt_entry *entry, int builtin,
1299 unsigned int hook, size_t size, unsigned int offset,
1302 struct xt_entry_target *target;
1304 target = ipt_get_target(entry);
1306 if (offset + entry->next_offset == size) {
1307 DBG("\tEnd of CHAIN");
1311 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET)) {
1312 DBG("\tUSER CHAIN (%s) match %p target %p",
1313 target->data, entry->elems,
1314 (char *)entry + entry->target_offset);
1317 } else if (builtin >= 0) {
1318 DBG("\tCHAIN (%s) match %p target %p",
1319 hooknames[builtin], entry->elems,
1320 (char *)entry + entry->target_offset);
1322 DBG("\tRULE match %p target %p",
1324 (char *)entry + entry->target_offset);
1334 static void dump_table(struct connman_iptables *table)
1336 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1338 table->info->valid_hooks, table->info->num_entries,
1341 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1342 table->info->hook_entry[NF_IP_PRE_ROUTING],
1343 table->info->hook_entry[NF_IP_LOCAL_IN],
1344 table->info->hook_entry[NF_IP_FORWARD],
1345 table->info->hook_entry[NF_IP_LOCAL_OUT],
1346 table->info->hook_entry[NF_IP_POST_ROUTING]);
1347 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1348 table->info->underflow[NF_IP_PRE_ROUTING],
1349 table->info->underflow[NF_IP_LOCAL_IN],
1350 table->info->underflow[NF_IP_FORWARD],
1351 table->info->underflow[NF_IP_LOCAL_OUT],
1352 table->info->underflow[NF_IP_POST_ROUTING]);
1354 iterate_entries(table->blob_entries->entrytable,
1355 table->info->valid_hooks,
1356 table->info->hook_entry,
1357 table->info->underflow,
1358 table->blob_entries->size,
1359 print_entry, dump_entry);
1362 static void dump_ipt_replace(struct ipt_replace *repl)
1364 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1365 repl->name, repl->valid_hooks, repl->num_entries,
1368 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1369 repl->hook_entry[NF_IP_PRE_ROUTING],
1370 repl->hook_entry[NF_IP_LOCAL_IN],
1371 repl->hook_entry[NF_IP_FORWARD],
1372 repl->hook_entry[NF_IP_LOCAL_OUT],
1373 repl->hook_entry[NF_IP_POST_ROUTING]);
1374 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1375 repl->underflow[NF_IP_PRE_ROUTING],
1376 repl->underflow[NF_IP_LOCAL_IN],
1377 repl->underflow[NF_IP_FORWARD],
1378 repl->underflow[NF_IP_LOCAL_OUT],
1379 repl->underflow[NF_IP_POST_ROUTING]);
1381 iterate_entries(repl->entries, repl->valid_hooks,
1382 repl->hook_entry, repl->underflow,
1383 repl->size, print_entry, dump_entry);
1386 static int iptables_get_entries(struct connman_iptables *table)
1388 socklen_t entry_size;
1391 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1393 err = getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1394 table->blob_entries, &entry_size);
1401 static int iptables_replace(struct connman_iptables *table,
1402 struct ipt_replace *r)
1406 err = setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1407 sizeof(*r) + r->size);
1414 static int iptables_add_counters(struct connman_iptables *table,
1415 struct xt_counters_info *c)
1419 err = setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_ADD_COUNTERS, c,
1420 sizeof(*c) + sizeof(struct xt_counters) * c->num_counters);
1427 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1428 size_t size, unsigned offset, void *user_data)
1430 struct connman_iptables *table = user_data;
1431 struct ipt_entry *new_entry;
1433 new_entry = g_try_malloc0(entry->next_offset);
1437 memcpy(new_entry, entry, entry->next_offset);
1439 return iptables_add_entry(table, new_entry, NULL, builtin,
1440 table->num_entries);
1443 static void table_cleanup(struct connman_iptables *table)
1446 struct connman_iptables_entry *entry;
1451 if (table->ipt_sock >= 0)
1452 close(table->ipt_sock);
1454 for (list = table->entries; list; list = list->next) {
1457 g_free(entry->entry);
1461 g_list_free(table->entries);
1462 g_free(table->name);
1463 g_free(table->info);
1464 g_free(table->blob_entries);
1468 static struct connman_iptables *iptables_init(const char *table_name)
1470 struct connman_iptables *table = NULL;
1471 char *module = NULL;
1474 DBG("%s", table_name);
1476 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1477 DBG("ip_tables module loading gives error but trying anyway");
1479 module = g_strconcat("iptable_", table_name, NULL);
1483 if (xtables_insmod(module, NULL, TRUE) != 0)
1484 DBG("%s module loading gives error but trying anyway", module);
1488 table = g_try_new0(struct connman_iptables, 1);
1492 table->info = g_try_new0(struct ipt_getinfo, 1);
1496 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1497 if (table->ipt_sock < 0)
1500 s = sizeof(*table->info);
1501 g_stpcpy(table->info->name, table_name);
1502 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1503 table->info, &s) < 0) {
1504 connman_error("iptables support missing error %d (%s)", errno,
1509 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1511 if (!table->blob_entries)
1514 g_stpcpy(table->blob_entries->name, table_name);
1515 table->blob_entries->size = table->info->size;
1517 if (iptables_get_entries(table) < 0)
1520 table->num_entries = 0;
1521 table->old_entries = table->info->num_entries;
1524 memcpy(table->underflow, table->info->underflow,
1525 sizeof(table->info->underflow));
1526 memcpy(table->hook_entry, table->info->hook_entry,
1527 sizeof(table->info->hook_entry));
1529 iterate_entries(table->blob_entries->entrytable,
1530 table->info->valid_hooks, table->info->hook_entry,
1531 table->info->underflow, table->blob_entries->size,
1540 table_cleanup(table);
1545 static struct option iptables_opts[] = {
1546 {.name = "append", .has_arg = 1, .val = 'A'},
1547 {.name = "compare", .has_arg = 1, .val = 'C'},
1548 {.name = "delete", .has_arg = 1, .val = 'D'},
1549 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1550 {.name = "insert", .has_arg = 1, .val = 'I'},
1551 {.name = "list", .has_arg = 2, .val = 'L'},
1552 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1553 {.name = "policy", .has_arg = 1, .val = 'P'},
1554 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1555 {.name = "destination", .has_arg = 1, .val = 'd'},
1556 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1557 {.name = "jump", .has_arg = 1, .val = 'j'},
1558 {.name = "match", .has_arg = 1, .val = 'm'},
1559 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1560 {.name = "source", .has_arg = 1, .val = 's'},
1561 {.name = "table", .has_arg = 1, .val = 't'},
1565 struct xtables_globals iptables_globals = {
1567 .opts = iptables_opts,
1568 .orig_opts = iptables_opts,
1569 #if XTABLES_VERSION_CODE > 10
1570 .compat_rev = xtables_compatible_revision,
1574 static struct xtables_target *prepare_target(struct connman_iptables *table,
1575 const char *target_name)
1577 struct xtables_target *xt_t = NULL;
1578 bool is_builtin, is_user_defined;
1579 GList *chain_head = NULL;
1583 is_user_defined = false;
1585 if (is_builtin_target(target_name))
1588 chain_head = find_chain_head(table, target_name);
1589 if (chain_head && chain_head->next)
1590 is_user_defined = true;
1593 if (is_builtin || is_user_defined)
1594 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1595 XTF_LOAD_MUST_SUCCEED);
1597 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1602 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1604 xt_t->t = g_try_malloc0(target_size);
1608 xt_t->t->u.target_size = target_size;
1610 if (is_builtin || is_user_defined) {
1611 struct xt_standard_target *target;
1613 target = (struct xt_standard_target *)(xt_t->t);
1614 g_stpcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1617 target->verdict = target_to_verdict(target_name);
1618 else if (is_user_defined) {
1619 struct connman_iptables_entry *target_rule;
1621 target_rule = chain_head->next->data;
1622 target->verdict = target_rule->offset;
1625 g_stpcpy(xt_t->t->u.user.name, target_name);
1626 xt_t->t->u.user.revision = xt_t->revision;
1628 xt_t->init(xt_t->t);
1631 if (xt_t->x6_options)
1632 iptables_globals.opts =
1633 xtables_options_xfrm(
1634 iptables_globals.orig_opts,
1635 iptables_globals.opts,
1637 &xt_t->option_offset);
1639 iptables_globals.opts =
1640 xtables_merge_options(
1641 iptables_globals.orig_opts,
1642 iptables_globals.opts,
1644 &xt_t->option_offset);
1646 if (!iptables_globals.opts) {
1654 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1655 struct xtables_rule_match **xt_rm,
1656 const char *match_name)
1658 struct xtables_match *xt_m;
1664 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1665 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1667 xt_m->m = g_try_malloc0(match_size);
1671 xt_m->m->u.match_size = match_size;
1672 g_stpcpy(xt_m->m->u.user.name, xt_m->name);
1673 xt_m->m->u.user.revision = xt_m->revision;
1676 xt_m->init(xt_m->m);
1678 if (xt_m->x6_options)
1679 iptables_globals.opts =
1680 xtables_options_xfrm(
1681 iptables_globals.orig_opts,
1682 iptables_globals.opts,
1684 &xt_m->option_offset);
1686 iptables_globals.opts =
1687 xtables_merge_options(
1688 iptables_globals.orig_opts,
1689 iptables_globals.opts,
1691 &xt_m->option_offset);
1693 if (!iptables_globals.opts) {
1696 if (xt_m == xt_m->next)
1705 static int parse_ip_and_mask(const char *str, struct in_addr *ip,
1706 struct in_addr *mask)
1709 uint32_t prefixlength;
1713 tokens = g_strsplit(str, "/", 2);
1717 if (!inet_pton(AF_INET, tokens[0], ip)) {
1723 prefixlength = strtol(tokens[1], NULL, 10);
1724 if (prefixlength > 31) {
1729 tmp = ~(0xffffffff >> prefixlength);
1734 mask->s_addr = htonl(tmp);
1735 ip->s_addr = ip->s_addr & mask->s_addr;
1743 static struct connman_iptables *get_table(const char *table_name)
1745 struct connman_iptables *table;
1748 table_name = "filter";
1750 table = g_hash_table_lookup(table_hash, table_name);
1754 table = iptables_init(table_name);
1758 table->name = g_strdup(table_name);
1759 g_hash_table_replace(table_hash, table->name, table);
1764 struct parse_context {
1768 struct xtables_target *xt_t;
1770 struct xtables_rule_match *xt_rm;
1774 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1779 tokens = g_strsplit_set(str, " ", -1);
1781 i = g_strv_length(tokens);
1783 /* Add space for the argv[0] value */
1786 /* Don't forget the last NULL entry */
1787 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1794 * getopt_long() jumps over the first token; we need to add some
1795 * random argv[0] entry.
1797 ctx->argv[0] = g_strdup("argh");
1798 for (i = 1; i < ctx->argc; i++)
1799 ctx->argv[i] = tokens[i - 1];
1806 static int parse_xt_modules(int c, bool invert,
1807 struct parse_context *ctx)
1809 struct xtables_match *m;
1810 struct xtables_rule_match *rm;
1811 struct ipt_entry fw;
1813 memset(&fw, 0, sizeof(fw));
1815 /* The SNAT parser wants to know the protocol. */
1816 if (ctx->proto == 0)
1817 ctx->proto = IPPROTO_IP;
1818 fw.ip.proto = ctx->proto;
1820 for (rm = ctx->xt_rm; rm; rm = rm->next) {
1821 if (rm->completed != 0)
1826 if (!m->x6_parse && !m->parse)
1829 if (c < (int) m->option_offset ||
1830 c >= (int) m->option_offset
1831 + XT_OPTION_OFFSET_SCALE)
1834 xtables_option_mpcall(c, ctx->argv, invert, m, &fw);
1840 if (!ctx->xt_t->x6_parse && !ctx->xt_t->parse)
1843 if (c < (int) ctx->xt_t->option_offset ||
1844 c >= (int) ctx->xt_t->option_offset
1845 + XT_OPTION_OFFSET_SCALE)
1848 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, &fw);
1853 static int final_check_xt_modules(struct parse_context *ctx)
1855 struct xtables_rule_match *rm;
1857 for (rm = ctx->xt_rm; rm; rm = rm->next)
1858 xtables_option_mfcall(rm->match);
1861 xtables_option_tfcall(ctx->xt_t);
1866 static int parse_rule_spec(struct connman_iptables *table,
1867 struct parse_context *ctx)
1870 * How the parser works:
1872 * - If getopt finds 's', 'd', 'i', 'o'.
1873 * just extract the information.
1874 * - if '!' is found, set the invert flag to true and
1875 * removes the '!' from the optarg string and jumps
1876 * back to getopt to reparse the current optarg string.
1877 * After reparsing the invert flag is reseted to false.
1878 * - If 'm' or 'j' is found then call either
1879 * prepare_matches() or prepare_target(). Those function
1880 * will modify (extend) the longopts for getopt_long.
1881 * That means getopt will change its matching context according
1882 * the loaded target.
1884 * Here an example with iptables-test
1886 * argv[0] = ./tools/iptables-test
1898 * getopt found 'm' then the optarg is "mark" and optind 7
1899 * The longopts array containts before hitting the `case 'm'`
1901 * val A has_arg 1 name append
1902 * val C has_arg 1 name compare
1903 * val D has_arg 1 name delete
1904 * val F has_arg 1 name flush-chain
1905 * val I has_arg 1 name insert
1906 * val L has_arg 2 name list
1907 * val N has_arg 1 name new-chain
1908 * val P has_arg 1 name policy
1909 * val X has_arg 1 name delete-chain
1910 * val d has_arg 1 name destination
1911 * val i has_arg 1 name in-interface
1912 * val j has_arg 1 name jump
1913 * val m has_arg 1 name match
1914 * val o has_arg 1 name out-interface
1915 * val s has_arg 1 name source
1916 * val t has_arg 1 name table
1918 * After executing the `case 'm'` block longopts is
1920 * val A has_arg 1 name append
1921 * val C has_arg 1 name compare
1922 * val D has_arg 1 name delete
1923 * val F has_arg 1 name flush-chain
1924 * val I has_arg 1 name insert
1925 * val L has_arg 2 name list
1926 * val N has_arg 1 name new-chain
1927 * val P has_arg 1 name policy
1928 * val X has_arg 1 name delete-chain
1929 * val d has_arg 1 name destination
1930 * val i has_arg 1 name in-interface
1931 * val j has_arg 1 name jump
1932 * val m has_arg 1 name match
1933 * val o has_arg 1 name out-interface
1934 * val s has_arg 1 name source
1935 * val t has_arg 1 name table
1936 * val has_arg 1 name mark
1938 * So the 'mark' matcher has added the 'mark' options
1939 * and getopt will then return c '256' optarg "999" optind 9
1940 * And we will hit the 'default' statement which then
1941 * will call the matchers parser (xt_m->parser() or
1942 * xtables_option_mpcall() depending on which version
1943 * of libxtables is found.
1945 struct xtables_match *xt_m;
1946 bool invert = false;
1949 ctx->ip = g_try_new0(struct ipt_ip, 1);
1954 * Tell getopt_long not to generate error messages for unknown
1955 * options and also reset optind back to 0.
1960 while ((c = getopt_long(ctx->argc, ctx->argv,
1962 iptables_globals.opts, NULL)) != -1) {
1965 /* Source specification */
1966 if (!parse_ip_and_mask(optarg,
1972 ctx->ip->invflags |= IPT_INV_SRCIP;
1976 /* Destination specification */
1977 if (!parse_ip_and_mask(optarg,
1983 ctx->ip->invflags |= IPT_INV_DSTIP;
1986 /* In interface specification */
1987 len = strlen(optarg);
1989 if (len + 1 > IFNAMSIZ)
1992 g_stpcpy(ctx->ip->iniface, optarg);
1993 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1996 ctx->ip->invflags |= IPT_INV_VIA_IN;
2000 /* Out interface specification */
2001 len = strlen(optarg);
2003 if (len + 1 > IFNAMSIZ)
2006 g_stpcpy(ctx->ip->outiface, optarg);
2007 memset(ctx->ip->outiface_mask, 0xff, len + 1);
2010 ctx->ip->invflags |= IPT_INV_VIA_OUT;
2015 xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
2020 ctx->xt_m = g_list_append(ctx->xt_m, xt_m);
2024 ctx->proto = xtables_parse_protocol(optarg);
2028 ctx->xt_t = prepare_target(table, optarg);
2036 if (optarg[0] == '!' && optarg[1] == '\0') {
2039 /* Remove the '!' from the optarg */
2043 * And recall getopt_long without reseting
2051 err = parse_xt_modules(c, invert, ctx);
2061 err = final_check_xt_modules(ctx);
2067 static void reset_xtables(void)
2069 struct xtables_match *xt_m;
2070 struct xtables_target *xt_t;
2073 * As side effect parsing a rule sets some global flags
2074 * which will be evaluated/verified. Let's reset them
2075 * to ensure we can parse more than one rule.
2077 * Clear all flags because the flags are only valid
2080 for (xt_m = xtables_matches; xt_m; xt_m = xt_m->next)
2083 for (xt_t = xtables_targets; xt_t; xt_t = xt_t->next) {
2089 * We need also to free the memory implicitly allocated
2090 * during parsing (see xtables_options_xfrm()).
2091 * Note xt_params is actually iptables_globals.
2093 if (xt_params->opts != xt_params->orig_opts) {
2094 g_free(xt_params->opts);
2095 xt_params->opts = xt_params->orig_opts;
2097 xt_params->option_offset = 0;
2100 static void cleanup_parse_context(struct parse_context *ctx)
2102 struct xtables_rule_match *rm, *tmp;
2105 g_strfreev(ctx->argv);
2108 g_free(ctx->xt_t->t);
2109 ctx->xt_t->t = NULL;
2112 for (list = ctx->xt_m; list; list = list->next) {
2113 struct xtables_match *xt_m = list->data;
2117 if (xt_m != xt_m->next)
2122 g_list_free(ctx->xt_m);
2124 for (tmp = NULL, rm = ctx->xt_rm; rm; rm = rm->next) {
2134 int __connman_iptables_dump(const char *table_name)
2136 struct connman_iptables *table;
2138 DBG("-t %s -L", table_name);
2140 table = get_table(table_name);
2149 int __connman_iptables_new_chain(const char *table_name,
2152 struct connman_iptables *table;
2154 DBG("-t %s -N %s", table_name, chain);
2156 table = get_table(table_name);
2160 return iptables_add_chain(table, chain);
2163 int __connman_iptables_delete_chain(const char *table_name,
2166 struct connman_iptables *table;
2168 DBG("-t %s -X %s", table_name, chain);
2170 table = get_table(table_name);
2174 return iptables_delete_chain(table, chain);
2177 int __connman_iptables_flush_chain(const char *table_name,
2180 struct connman_iptables *table;
2182 DBG("-t %s -F %s", table_name, chain);
2184 table = get_table(table_name);
2188 return iptables_flush_chain(table, chain);
2191 int __connman_iptables_change_policy(const char *table_name,
2195 struct connman_iptables *table;
2197 DBG("-t %s -F %s", table_name, chain);
2199 table = get_table(table_name);
2203 return iptables_change_policy(table, chain, policy);
2206 int __connman_iptables_append(const char *table_name,
2208 const char *rule_spec)
2210 struct connman_iptables *table;
2211 struct parse_context *ctx;
2212 const char *target_name;
2215 ctx = g_try_new0(struct parse_context, 1);
2219 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2221 err = prepare_getopt_args(rule_spec, ctx);
2225 table = get_table(table_name);
2231 err = parse_rule_spec(table, ctx);
2238 target_name = ctx->xt_t->name;
2240 err = iptables_append_rule(table, ctx->ip, chain,
2241 target_name, ctx->xt_t, ctx->xt_rm);
2243 cleanup_parse_context(ctx);
2249 int __connman_iptables_insert(const char *table_name,
2251 const char *rule_spec)
2253 struct connman_iptables *table;
2254 struct parse_context *ctx;
2255 const char *target_name;
2258 ctx = g_try_new0(struct parse_context, 1);
2262 DBG("-t %s -I %s %s", table_name, chain, rule_spec);
2264 err = prepare_getopt_args(rule_spec, ctx);
2268 table = get_table(table_name);
2274 err = parse_rule_spec(table, ctx);
2281 target_name = ctx->xt_t->name;
2283 err = iptables_insert_rule(table, ctx->ip, chain,
2284 target_name, ctx->xt_t, ctx->xt_rm);
2286 cleanup_parse_context(ctx);
2292 int __connman_iptables_delete(const char *table_name,
2294 const char *rule_spec)
2296 struct connman_iptables *table;
2297 struct parse_context *ctx;
2298 const char *target_name;
2301 ctx = g_try_new0(struct parse_context, 1);
2305 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2307 err = prepare_getopt_args(rule_spec, ctx);
2311 table = get_table(table_name);
2317 err = parse_rule_spec(table, ctx);
2324 target_name = ctx->xt_t->name;
2326 err = iptables_delete_rule(table, ctx->ip, chain,
2327 target_name, ctx->xt_t, ctx->xt_m,
2330 cleanup_parse_context(ctx);
2336 int __connman_iptables_commit(const char *table_name)
2338 struct connman_iptables *table;
2339 struct ipt_replace *repl;
2341 struct xt_counters_info *counters;
2342 struct connman_iptables_entry *e;
2346 DBG("%s", table_name);
2348 table = g_hash_table_lookup(table_hash, table_name);
2352 repl = iptables_blob(table);
2357 dump_ipt_replace(repl);
2359 err = iptables_replace(table, repl);
2364 counters = g_try_malloc0(sizeof(*counters) +
2365 sizeof(struct xt_counters) * table->num_entries);
2368 goto out_hash_remove;
2370 g_stpcpy(counters->name, table->info->name);
2371 counters->num_counters = table->num_entries;
2372 for (list = table->entries, cnt = 0; list; list = list->next, cnt++) {
2374 if (e->counter_idx >= 0)
2375 counters->counters[cnt] = repl->counters[e->counter_idx];
2377 err = iptables_add_counters(table, counters);
2381 goto out_hash_remove;
2386 g_hash_table_remove(table_hash, table_name);
2388 g_free(repl->counters);
2393 static void remove_table(gpointer user_data)
2395 struct connman_iptables *table = user_data;
2397 table_cleanup(table);
2400 static int iterate_chains_cb(struct ipt_entry *entry, int builtin,
2401 unsigned int hook, size_t size,
2402 unsigned int offset, void *user_data)
2404 struct cb_data *cbd = user_data;
2405 connman_iptables_iterate_chains_cb_t cb = cbd->cb;
2406 struct xt_entry_target *target;
2408 if (offset + entry->next_offset == size)
2411 target = ipt_get_target(entry);
2413 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
2414 (*cb)((const char *)target->data, cbd->user_data);
2415 else if (builtin >= 0)
2416 (*cb)(hooknames[builtin], cbd->user_data);
2421 int __connman_iptables_iterate_chains(const char *table_name,
2422 connman_iptables_iterate_chains_cb_t cb,
2425 struct cb_data *cbd = cb_data_new(cb, user_data);
2426 struct connman_iptables *table;
2428 table = get_table(table_name);
2434 iterate_entries(table->blob_entries->entrytable,
2435 table->info->valid_hooks,
2436 table->info->hook_entry,
2437 table->info->underflow,
2438 table->blob_entries->size,
2439 iterate_chains_cb, cbd);
2446 int __connman_iptables_init(void)
2450 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2451 debug_enabled = true;
2453 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2454 NULL, remove_table);
2456 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2461 void __connman_iptables_cleanup(void)
2465 g_hash_table_destroy(table_hash);