5 * Copyright (C) 2007-2012 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>
39 void flush_table(const char *name);
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 {
159 struct ipt_entry *entry;
162 struct connman_iptables {
166 struct ipt_getinfo *info;
167 struct ipt_get_entries *blob_entries;
169 unsigned int num_entries;
170 unsigned int old_entries;
173 unsigned int underflow[NF_INET_NUMHOOKS];
174 unsigned int hook_entry[NF_INET_NUMHOOKS];
179 static GHashTable *table_hash = NULL;
180 static gboolean debug_enabled = FALSE;
182 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
183 unsigned int hook,size_t size,
184 unsigned int offset, void *user_data);
186 static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
190 if (*valid_hooks == 0)
191 return NF_INET_NUMHOOKS;
193 h = __builtin_ffs(*valid_hooks) - 1;
194 *valid_hooks ^= (1 << h);
199 static int iterate_entries(struct ipt_entry *entries,
200 unsigned int valid_hooks,
201 unsigned int *hook_entry,
202 unsigned int *underflow,
203 size_t size, iterate_entries_cb_t cb,
206 unsigned int offset, h, hook;
208 struct ipt_entry *entry;
210 h = next_hook_entry_index(&valid_hooks);
213 for (offset = 0, entry = entries; offset < size;
214 offset += entry->next_offset) {
216 entry = (void *)entries + offset;
219 * Updating builtin, hook and h is very tricky.
221 * - builtin is only set to the current hook number
222 * if the current entry is the hook entry (aka chain
223 * head). And only for builtin chains, never for
225 * - hook is the current hook number. If we
226 * look at user chains it needs to be NF_INET_NETNUMHOOKS.
227 * - h is the next hook entry. Thous we need to be carefully
228 * not to access the table when h is NF_INET_NETNUMHOOKS.
230 if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
235 if (h == NF_INET_NUMHOOKS)
238 if (h < NF_INET_NUMHOOKS && underflow[h] <= offset) {
239 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 %d offset %d size %d", entry, hook,
257 offset, entry->next_offset);
259 return cb(entry, builtin, hook, size, offset, NULL);
262 static int target_to_verdict(const char *target_name)
264 if (!g_strcmp0(target_name, LABEL_ACCEPT))
265 return -NF_ACCEPT - 1;
267 if (!g_strcmp0(target_name, LABEL_DROP))
270 if (!g_strcmp0(target_name, LABEL_QUEUE))
271 return -NF_QUEUE - 1;
273 if (!g_strcmp0(target_name, LABEL_RETURN))
279 static gboolean is_builtin_target(const char *target_name)
281 if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
282 !g_strcmp0(target_name, LABEL_DROP) ||
283 !g_strcmp0(target_name, LABEL_QUEUE) ||
284 !g_strcmp0(target_name, LABEL_RETURN))
290 static gboolean is_jump(struct connman_iptables_entry *e)
292 struct xt_entry_target *target;
294 target = ipt_get_target(e->entry);
296 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
297 struct xt_standard_target *t;
299 t = (struct xt_standard_target *)target;
301 switch (t->verdict) {
317 static gboolean is_fallthrough(struct connman_iptables_entry *e)
319 struct xt_entry_target *target;
321 target = ipt_get_target(e->entry);
322 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
323 struct xt_standard_target *t;
325 t = (struct xt_standard_target *)target;
332 static gboolean is_chain(struct connman_iptables *table,
333 struct connman_iptables_entry *e)
335 struct ipt_entry *entry;
336 struct xt_entry_target *target;
342 target = ipt_get_target(entry);
343 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
349 static GList *find_chain_head(struct connman_iptables *table,
350 const char *chain_name)
353 struct connman_iptables_entry *head;
354 struct ipt_entry *entry;
355 struct xt_entry_target *target;
358 for (list = table->entries; list; list = list->next) {
363 builtin = head->builtin;
364 if (builtin >= 0 && !g_strcmp0(hooknames[builtin], chain_name))
367 /* User defined chain */
368 target = ipt_get_target(entry);
369 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET) &&
370 !g_strcmp0((char *)target->data, chain_name))
377 static GList *find_chain_tail(struct connman_iptables *table,
378 const char *chain_name)
380 struct connman_iptables_entry *tail;
381 GList *chain_head, *list;
383 chain_head = find_chain_head(table, chain_name);
384 if (chain_head == NULL)
387 /* Then we look for the next chain */
388 for (list = chain_head->next; list; list = list->next) {
391 if (is_chain(table, tail))
395 /* Nothing found, we return the table end */
396 return g_list_last(table->entries);
400 static void update_offsets(struct connman_iptables *table)
403 struct connman_iptables_entry *entry, *prev_entry;
405 for (list = table->entries; list; list = list->next) {
408 if (list == table->entries) {
415 prev_entry = prev->data;
417 entry->offset = prev_entry->offset +
418 prev_entry->entry->next_offset;
422 static void update_targets_reference(struct connman_iptables *table,
423 struct connman_iptables_entry *entry_before,
424 struct connman_iptables_entry *modified_entry,
425 gboolean is_removing)
427 struct connman_iptables_entry *tmp;
428 struct xt_standard_target *t;
432 offset = modified_entry->entry->next_offset;
434 for (list = table->entries; list; list = list->next) {
440 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
442 if (is_removing == TRUE) {
443 if (t->verdict >= entry_before->offset)
444 t->verdict -= offset;
446 if (t->verdict > entry_before->offset)
447 t->verdict += offset;
451 if (is_fallthrough(modified_entry)) {
452 t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
454 t->verdict = entry_before->offset +
455 modified_entry->entry->target_offset +
456 ALIGN(sizeof(struct xt_standard_target));
457 t->target.u.target_size =
458 ALIGN(sizeof(struct xt_standard_target));
462 static int iptables_add_entry(struct connman_iptables *table,
463 struct ipt_entry *entry, GList *before,
466 struct connman_iptables_entry *e, *entry_before;
471 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
476 e->builtin = builtin;
478 table->entries = g_list_insert_before(table->entries, before, e);
479 table->num_entries++;
480 table->size += entry->next_offset;
482 if (before == NULL) {
483 e->offset = table->size - entry->next_offset;
488 entry_before = before->data;
491 * We've just appended/insterted a new entry. All references
492 * should be bumped accordingly.
494 update_targets_reference(table, entry_before, e, FALSE);
496 update_offsets(table);
501 static int remove_table_entry(struct connman_iptables *table,
502 struct connman_iptables_entry *entry)
506 table->num_entries--;
507 table->size -= entry->entry->next_offset;
508 removed = entry->entry->next_offset;
510 table->entries = g_list_remove(table->entries, entry);
512 g_free(entry->entry);
518 static void delete_update_hooks(struct connman_iptables *table,
519 int builtin, GList *chain_head,
522 struct connman_iptables_entry *e;
525 e = chain_head->data;
526 e->builtin = builtin;
528 table->underflow[builtin] -= removed;
530 for (list = chain_head->next; list; list = list->next) {
536 table->hook_entry[e->builtin] -= removed;
537 table->underflow[e->builtin] -= removed;
541 static int iptables_flush_chain(struct connman_iptables *table,
544 GList *chain_head, *chain_tail, *list, *next;
545 struct connman_iptables_entry *entry;
546 int builtin, removed = 0;
548 DBG("table %s chain %s", table->name, name);
550 chain_head = find_chain_head(table, name);
551 if (chain_head == NULL)
554 chain_tail = find_chain_tail(table, name);
555 if (chain_tail == NULL)
558 entry = chain_head->data;
559 builtin = entry->builtin;
564 list = chain_head->next;
566 if (list == chain_tail->prev)
569 while (list != chain_tail->prev) {
571 next = g_list_next(list);
573 removed += remove_table_entry(table, entry);
579 delete_update_hooks(table, builtin, chain_tail->prev, removed);
581 update_offsets(table);
586 static int iptables_add_chain(struct connman_iptables *table,
590 struct ipt_entry *entry_head;
591 struct ipt_entry *entry_return;
592 struct error_target *error;
593 struct ipt_standard_target *standard;
594 u_int16_t entry_head_size, entry_return_size;
596 DBG("table %s chain %s", table->name, name);
598 last = g_list_last(table->entries);
601 * An empty chain is composed of:
602 * - A head entry, with no match and an error target.
603 * The error target data is the chain name.
604 * - A tail entry, with no match and a standard target.
605 * The standard target verdict is XT_RETURN (return to the
610 entry_head_size = sizeof(struct ipt_entry) +
611 sizeof(struct error_target);
612 entry_head = g_try_malloc0(entry_head_size);
613 if (entry_head == NULL)
616 entry_head->target_offset = sizeof(struct ipt_entry);
617 entry_head->next_offset = entry_head_size;
619 error = (struct error_target *) entry_head->elems;
620 g_stpcpy(error->t.u.user.name, IPT_ERROR_TARGET);
621 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
622 g_stpcpy(error->error, name);
624 if (iptables_add_entry(table, entry_head, last, -1) < 0)
628 entry_return_size = sizeof(struct ipt_entry) +
629 sizeof(struct ipt_standard_target);
630 entry_return = g_try_malloc0(entry_return_size);
631 if (entry_return == NULL)
634 entry_return->target_offset = sizeof(struct ipt_entry);
635 entry_return->next_offset = entry_return_size;
637 standard = (struct ipt_standard_target *) entry_return->elems;
638 standard->target.u.user.target_size =
639 ALIGN(sizeof(struct ipt_standard_target));
640 standard->verdict = XT_RETURN;
642 if (iptables_add_entry(table, entry_return, last, -1) < 0)
648 g_free(entry_return);
655 static int iptables_delete_chain(struct connman_iptables *table,
658 struct connman_iptables_entry *entry;
659 GList *chain_head, *chain_tail;
661 DBG("table %s chain %s", table->name, name);
663 chain_head = find_chain_head(table, name);
664 if (chain_head == NULL)
667 entry = chain_head->data;
669 /* We cannot remove builtin chain */
670 if (entry->builtin >= 0)
673 chain_tail = find_chain_tail(table, name);
674 if (chain_tail == NULL)
677 /* Chain must be flushed */
678 if (chain_head->next != chain_tail->prev)
681 remove_table_entry(table, entry);
683 entry = chain_tail->prev->data;
684 remove_table_entry(table, entry);
686 update_offsets(table);
691 static struct ipt_entry *new_rule(struct ipt_ip *ip,
692 const char *target_name, struct xtables_target *xt_t,
693 struct xtables_rule_match *xt_rm)
695 struct xtables_rule_match *tmp_xt_rm;
696 struct ipt_entry *new_entry;
697 size_t match_size, target_size;
700 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
701 match_size += tmp_xt_rm->match->m->u.match_size;
704 target_size = ALIGN(xt_t->t->u.target_size);
706 target_size = ALIGN(sizeof(struct xt_standard_target));
708 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
710 if (new_entry == NULL)
713 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
715 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
716 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
720 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
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;
744 if (chain_head == NULL)
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,
772 connman_bool_t insert)
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);
779 if (chain_head == NULL)
782 chain_tail = find_chain_tail(table, chain_name);
783 if (chain_tail == NULL)
786 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
787 if (new_entry == NULL)
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 == TRUE || 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);
821 if (chain_tail == NULL)
824 new_entry = prepare_rule_inclusion(table, ip, chain_name,
825 target_name, xt_t, &builtin, xt_rm, FALSE);
826 if (new_entry == NULL)
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);
849 if (chain_head == NULL)
852 new_entry = prepare_rule_inclusion(table, ip, chain_name,
853 target_name, xt_t, &builtin, xt_rm, TRUE);
854 if (new_entry == NULL)
858 chain_head = chain_head->next;
860 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
867 static gboolean 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 gboolean is_same_target(struct xt_entry_target *xt_e_t1,
883 struct xt_entry_target *xt_e_t2)
887 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
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 gboolean is_same_match(struct xt_entry_match *xt_e_m1,
921 struct xt_entry_match *xt_e_m2)
925 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
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,
950 struct xtables_match *xt_m,
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);
961 if (chain_head == NULL)
964 chain_tail = find_chain_tail(table, chain_name);
965 if (chain_tail == NULL)
971 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
972 if (entry_test == NULL)
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) == FALSE)
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,
1031 struct xtables_match *xt_m,
1032 struct xtables_rule_match *xt_rm)
1034 struct connman_iptables_entry *entry;
1035 GList *chain_head, *chain_tail, *list;
1036 int builtin, removed;
1038 DBG("table %s chain %s", table->name, chain_name);
1042 chain_head = find_chain_head(table, chain_name);
1043 if (chain_head == NULL)
1046 chain_tail = find_chain_tail(table, chain_name);
1047 if (chain_tail == NULL)
1050 list = find_existing_rule(table, ip, chain_name, target_name,
1055 entry = chain_head->data;
1056 builtin = entry->builtin;
1058 if (builtin >= 0 && list == chain_head) {
1060 * We are about to remove the first rule in the
1061 * chain. In this case we need to store the builtin
1062 * value to the new chain_head.
1064 * Note, for builtin chains, chain_head->next is
1065 * always valid. A builtin chain has always a policy
1068 chain_head = chain_head->next;
1070 entry = chain_head->data;
1071 entry->builtin = builtin;
1078 /* We have deleted a rule,
1079 * all references should be bumped accordingly */
1080 if (list->next != NULL)
1081 update_targets_reference(table, list->next->data,
1084 removed += remove_table_entry(table, entry);
1087 delete_update_hooks(table, builtin, chain_head, removed);
1089 update_offsets(table);
1094 static int iptables_change_policy(struct connman_iptables *table,
1095 const char *chain_name, const char *policy)
1097 GList *chain_head, *chain_tail;
1098 struct connman_iptables_entry *entry;
1099 struct xt_entry_target *target;
1100 struct xt_standard_target *t;
1103 DBG("table %s chain %s policy %s", table->name, chain_name, policy);
1105 verdict = target_to_verdict(policy);
1107 case -NF_ACCEPT - 1:
1114 chain_head = find_chain_head(table, chain_name);
1115 if (chain_head == NULL)
1118 entry = chain_head->data;
1119 if (entry->builtin < 0)
1122 chain_tail = find_chain_tail(table, chain_name);
1123 if (chain_tail == NULL)
1126 entry = chain_tail->prev->data;
1127 target = ipt_get_target(entry->entry);
1129 t = (struct xt_standard_target *)target;
1130 t->verdict = verdict;
1135 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1137 struct ipt_replace *r;
1139 struct connman_iptables_entry *e;
1140 unsigned char *entry_index;
1142 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1146 memset(r, 0, sizeof(*r) + table->size);
1148 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1149 * table->old_entries);
1150 if (r->counters == NULL) {
1155 g_stpcpy(r->name, table->info->name);
1156 r->num_entries = table->num_entries;
1157 r->size = table->size;
1159 r->num_counters = table->old_entries;
1160 r->valid_hooks = table->info->valid_hooks;
1162 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1163 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1165 entry_index = (unsigned char *)r->entries;
1166 for (list = table->entries; list; list = list->next) {
1169 memcpy(entry_index, e->entry, e->entry->next_offset);
1170 entry_index += e->entry->next_offset;
1176 static void dump_ip(struct ipt_entry *entry)
1178 struct ipt_ip *ip = &entry->ip;
1179 char ip_string[INET6_ADDRSTRLEN];
1180 char ip_mask[INET6_ADDRSTRLEN];
1182 if (strlen(ip->iniface))
1183 DBG("\tin %s", ip->iniface);
1185 if (strlen(ip->outiface))
1186 DBG("\tout %s", ip->outiface);
1188 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1189 inet_ntop(AF_INET, &ip->smsk,
1190 ip_mask, INET6_ADDRSTRLEN) != NULL)
1191 DBG("\tsrc %s/%s", ip_string, ip_mask);
1193 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1194 inet_ntop(AF_INET, &ip->dmsk,
1195 ip_mask, INET6_ADDRSTRLEN) != NULL)
1196 DBG("\tdst %s/%s", ip_string, ip_mask);
1199 static void dump_target(struct ipt_entry *entry)
1202 struct xtables_target *xt_t;
1203 struct xt_entry_target *target;
1205 target = ipt_get_target(entry);
1207 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
1208 struct xt_standard_target *t;
1210 t = (struct xt_standard_target *)target;
1212 switch (t->verdict) {
1214 DBG("\ttarget RETURN");
1217 case -NF_ACCEPT - 1:
1218 DBG("\ttarget ACCEPT");
1222 DBG("\ttarget DROP");
1226 DBG("\ttarget QUEUE");
1230 DBG("\ttarget STOP");
1234 DBG("\tJUMP %u", t->verdict);
1238 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1239 XTF_LOAD_MUST_SUCCEED);
1241 if(xt_t->print != NULL)
1242 xt_t->print(NULL, target, 1);
1244 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1246 DBG("\ttarget %s", target->u.user.name);
1250 if(xt_t->print != NULL) {
1252 xt_t->print(NULL, target, 1);
1257 static void dump_match(struct ipt_entry *entry)
1259 struct xtables_match *xt_m;
1260 struct xt_entry_match *match;
1262 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1265 match = (struct xt_entry_match *) entry->elems;
1267 if (!strlen(match->u.user.name))
1270 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1274 if(xt_m->print != NULL) {
1276 xt_m->print(NULL, match, 1);
1282 DBG("\tmatch %s", match->u.user.name);
1286 static int dump_entry(struct ipt_entry *entry, int builtin,
1287 unsigned int hook, size_t size, unsigned int offset,
1290 struct xt_entry_target *target;
1292 target = ipt_get_target(entry);
1294 if (offset + entry->next_offset == size) {
1295 DBG("\tEnd of CHAIN");
1299 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET)) {
1300 DBG("\tUSER CHAIN (%s) match %p target %p",
1301 target->data, entry->elems,
1302 (char *)entry + entry->target_offset);
1305 } else if (builtin >= 0) {
1306 DBG("\tCHAIN (%s) match %p target %p",
1307 hooknames[builtin], entry->elems,
1308 (char *)entry + entry->target_offset);
1310 DBG("\tRULE match %p target %p",
1312 (char *)entry + entry->target_offset);
1322 static void dump_table(struct connman_iptables *table)
1324 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1326 table->info->valid_hooks, table->info->num_entries,
1329 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1330 table->info->hook_entry[NF_IP_PRE_ROUTING],
1331 table->info->hook_entry[NF_IP_LOCAL_IN],
1332 table->info->hook_entry[NF_IP_FORWARD],
1333 table->info->hook_entry[NF_IP_LOCAL_OUT],
1334 table->info->hook_entry[NF_IP_POST_ROUTING]);
1335 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1336 table->info->underflow[NF_IP_PRE_ROUTING],
1337 table->info->underflow[NF_IP_LOCAL_IN],
1338 table->info->underflow[NF_IP_FORWARD],
1339 table->info->underflow[NF_IP_LOCAL_OUT],
1340 table->info->underflow[NF_IP_POST_ROUTING]);
1342 iterate_entries(table->blob_entries->entrytable,
1343 table->info->valid_hooks,
1344 table->info->hook_entry,
1345 table->info->underflow,
1346 table->blob_entries->size,
1347 print_entry, dump_entry);
1350 static void dump_ipt_replace(struct ipt_replace *repl)
1352 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1353 repl->name, repl->valid_hooks, repl->num_entries,
1356 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1357 repl->hook_entry[NF_IP_PRE_ROUTING],
1358 repl->hook_entry[NF_IP_LOCAL_IN],
1359 repl->hook_entry[NF_IP_FORWARD],
1360 repl->hook_entry[NF_IP_LOCAL_OUT],
1361 repl->hook_entry[NF_IP_POST_ROUTING]);
1362 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1363 repl->underflow[NF_IP_PRE_ROUTING],
1364 repl->underflow[NF_IP_LOCAL_IN],
1365 repl->underflow[NF_IP_FORWARD],
1366 repl->underflow[NF_IP_LOCAL_OUT],
1367 repl->underflow[NF_IP_POST_ROUTING]);
1369 iterate_entries(repl->entries, repl->valid_hooks,
1370 repl->hook_entry, repl->underflow,
1371 repl->size, print_entry, dump_entry);
1374 static int iptables_get_entries(struct connman_iptables *table)
1376 socklen_t entry_size;
1378 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1380 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1381 table->blob_entries, &entry_size);
1384 static int iptables_replace(struct connman_iptables *table,
1385 struct ipt_replace *r)
1387 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1388 sizeof(*r) + r->size);
1391 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1392 size_t size, unsigned offset, void *user_data)
1394 struct connman_iptables *table = user_data;
1395 struct ipt_entry *new_entry;
1397 new_entry = g_try_malloc0(entry->next_offset);
1398 if (new_entry == NULL)
1401 memcpy(new_entry, entry, entry->next_offset);
1403 return iptables_add_entry(table, new_entry, NULL, builtin);
1406 static void table_cleanup(struct connman_iptables *table)
1409 struct connman_iptables_entry *entry;
1414 if (table->ipt_sock >= 0)
1415 close(table->ipt_sock);
1417 for (list = table->entries; list; list = list->next) {
1420 g_free(entry->entry);
1424 g_list_free(table->entries);
1425 g_free(table->name);
1426 g_free(table->info);
1427 g_free(table->blob_entries);
1431 static struct connman_iptables *iptables_init(const char *table_name)
1433 struct connman_iptables *table = NULL;
1434 char *module = NULL;
1437 DBG("%s", table_name);
1439 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1440 DBG("ip_tables module loading gives error but trying anyway");
1442 module = g_strconcat("iptable_", table_name, NULL);
1446 if (xtables_insmod(module, NULL, TRUE) != 0)
1447 DBG("%s module loading gives error but trying anyway", module);
1451 table = g_try_new0(struct connman_iptables, 1);
1455 table->info = g_try_new0(struct ipt_getinfo, 1);
1456 if (table->info == NULL)
1459 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1460 if (table->ipt_sock < 0)
1463 s = sizeof(*table->info);
1464 g_stpcpy(table->info->name, table_name);
1465 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1466 table->info, &s) < 0) {
1467 connman_error("iptables support missing error %d (%s)", errno,
1472 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1474 if (table->blob_entries == NULL)
1477 g_stpcpy(table->blob_entries->name, table_name);
1478 table->blob_entries->size = table->info->size;
1480 if (iptables_get_entries(table) < 0)
1483 table->num_entries = 0;
1484 table->old_entries = table->info->num_entries;
1487 memcpy(table->underflow, table->info->underflow,
1488 sizeof(table->info->underflow));
1489 memcpy(table->hook_entry, table->info->hook_entry,
1490 sizeof(table->info->hook_entry));
1492 iterate_entries(table->blob_entries->entrytable,
1493 table->info->valid_hooks, table->info->hook_entry,
1494 table->info->underflow, table->blob_entries->size,
1497 if (debug_enabled == TRUE)
1503 table_cleanup(table);
1508 static struct option iptables_opts[] = {
1509 {.name = "append", .has_arg = 1, .val = 'A'},
1510 {.name = "compare", .has_arg = 1, .val = 'C'},
1511 {.name = "delete", .has_arg = 1, .val = 'D'},
1512 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1513 {.name = "insert", .has_arg = 1, .val = 'I'},
1514 {.name = "list", .has_arg = 2, .val = 'L'},
1515 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1516 {.name = "policy", .has_arg = 1, .val = 'P'},
1517 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1518 {.name = "destination", .has_arg = 1, .val = 'd'},
1519 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1520 {.name = "jump", .has_arg = 1, .val = 'j'},
1521 {.name = "match", .has_arg = 1, .val = 'm'},
1522 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1523 {.name = "source", .has_arg = 1, .val = 's'},
1524 {.name = "table", .has_arg = 1, .val = 't'},
1528 struct xtables_globals iptables_globals = {
1530 .opts = iptables_opts,
1531 .orig_opts = iptables_opts,
1534 static struct xtables_target *prepare_target(struct connman_iptables *table,
1535 const char *target_name)
1537 struct xtables_target *xt_t = NULL;
1538 gboolean is_builtin, is_user_defined;
1539 GList *chain_head = NULL;
1543 is_user_defined = FALSE;
1545 if (is_builtin_target(target_name))
1548 chain_head = find_chain_head(table, target_name);
1549 if (chain_head != NULL && chain_head->next != NULL)
1550 is_user_defined = TRUE;
1553 if (is_builtin || is_user_defined)
1554 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1555 XTF_LOAD_MUST_SUCCEED);
1557 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1562 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1564 xt_t->t = g_try_malloc0(target_size);
1565 if (xt_t->t == NULL)
1568 xt_t->t->u.target_size = target_size;
1570 if (is_builtin || is_user_defined) {
1571 struct xt_standard_target *target;
1573 target = (struct xt_standard_target *)(xt_t->t);
1574 g_stpcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1576 if (is_builtin == TRUE)
1577 target->verdict = target_to_verdict(target_name);
1578 else if (is_user_defined == TRUE) {
1579 struct connman_iptables_entry *target_rule;
1581 if (chain_head == NULL) {
1586 target_rule = chain_head->next->data;
1587 target->verdict = target_rule->offset;
1590 g_stpcpy(xt_t->t->u.user.name, target_name);
1591 xt_t->t->u.user.revision = xt_t->revision;
1592 if (xt_t->init != NULL)
1593 xt_t->init(xt_t->t);
1596 if (xt_t->x6_options != NULL)
1597 iptables_globals.opts =
1598 xtables_options_xfrm(
1599 iptables_globals.orig_opts,
1600 iptables_globals.opts,
1602 &xt_t->option_offset);
1604 iptables_globals.opts =
1605 xtables_merge_options(
1606 iptables_globals.orig_opts,
1607 iptables_globals.opts,
1609 &xt_t->option_offset);
1611 if (iptables_globals.opts == NULL) {
1619 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1620 struct xtables_rule_match **xt_rm,
1621 const char *match_name)
1623 struct xtables_match *xt_m;
1626 if (match_name == NULL)
1629 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1630 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1632 xt_m->m = g_try_malloc0(match_size);
1633 if (xt_m->m == NULL)
1636 xt_m->m->u.match_size = match_size;
1637 g_stpcpy(xt_m->m->u.user.name, xt_m->name);
1638 xt_m->m->u.user.revision = xt_m->revision;
1640 if (xt_m->init != NULL)
1641 xt_m->init(xt_m->m);
1643 if (xt_m->x6_options != NULL)
1644 iptables_globals.opts =
1645 xtables_options_xfrm(
1646 iptables_globals.orig_opts,
1647 iptables_globals.opts,
1649 &xt_m->option_offset);
1651 iptables_globals.opts =
1652 xtables_merge_options(
1653 iptables_globals.orig_opts,
1654 iptables_globals.opts,
1656 &xt_m->option_offset);
1658 if (iptables_globals.opts == NULL) {
1666 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1669 uint32_t prefixlength;
1673 tokens = g_strsplit(str, "/", 2);
1677 if (!inet_pton(AF_INET, tokens[0], ip)) {
1682 if (tokens[1] != NULL) {
1683 prefixlength = strtol(tokens[1], NULL, 10);
1684 if (prefixlength > 31) {
1689 tmp = ~(0xffffffff >> prefixlength);
1694 mask->s_addr = htonl(tmp);
1695 ip->s_addr = ip->s_addr & mask->s_addr;
1703 static struct connman_iptables *get_table(const char *table_name)
1705 struct connman_iptables *table;
1707 if (table_name == NULL)
1708 table_name = "filter";
1710 table = g_hash_table_lookup(table_hash, table_name);
1714 table = iptables_init(table_name);
1718 table->name = g_strdup(table_name);
1719 g_hash_table_replace(table_hash, table->name, table);
1724 struct parse_context {
1728 struct xtables_target *xt_t;
1729 struct xtables_match *xt_m;
1730 struct xtables_rule_match *xt_rm;
1733 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1738 tokens = g_strsplit_set(str, " ", -1);
1740 i = g_strv_length(tokens);
1742 /* Add space for the argv[0] value */
1745 /* Don't forget the last NULL entry */
1746 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1747 if (ctx->argv == NULL) {
1753 * getopt_long() jumps over the first token; we need to add some
1754 * random argv[0] entry.
1756 ctx->argv[0] = g_strdup("argh");
1757 for (i = 1; i < ctx->argc; i++)
1758 ctx->argv[i] = tokens[i - 1];
1765 static int parse_xt_modules(int c, connman_bool_t invert,
1766 struct parse_context *ctx)
1768 struct xtables_match *m;
1769 struct xtables_rule_match *rm;
1771 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1772 if (rm->completed != 0)
1777 if (m->x6_parse == NULL && m->parse == NULL)
1780 if (c < (int) m->option_offset ||
1781 c >= (int) m->option_offset
1782 + XT_OPTION_OFFSET_SCALE)
1785 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1788 if (ctx->xt_t == NULL)
1791 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1794 if (c < (int) ctx->xt_t->option_offset ||
1795 c >= (int) ctx->xt_t->option_offset
1796 + XT_OPTION_OFFSET_SCALE)
1799 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1804 static int final_check_xt_modules(struct parse_context *ctx)
1806 struct xtables_rule_match *rm;
1808 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1809 xtables_option_mfcall(rm->match);
1811 if (ctx->xt_t != NULL)
1812 xtables_option_tfcall(ctx->xt_t);
1817 static int parse_rule_spec(struct connman_iptables *table,
1818 struct parse_context *ctx)
1821 * How the parser works:
1823 * - If getopt finds 's', 'd', 'i', 'o'.
1824 * just extract the information.
1825 * - if '!' is found, set the invert flag to true and
1826 * removes the '!' from the optarg string and jumps
1827 * back to getopt to reparse the current optarg string.
1828 * After reparsing the invert flag is reseted to false.
1829 * - If 'm' or 'j' is found then call either
1830 * prepare_matches() or prepare_target(). Those function
1831 * will modify (extend) the longopts for getopt_long.
1832 * That means getopt will change its matching context according
1833 * the loaded target.
1835 * Here an example with iptables-test
1837 * argv[0] = ./tools/iptables-test
1849 * getopt found 'm' then the optarg is "mark" and optind 7
1850 * The longopts array containts before hitting the `case 'm'`
1852 * val A has_arg 1 name append
1853 * val C has_arg 1 name compare
1854 * val D has_arg 1 name delete
1855 * val F has_arg 1 name flush-chain
1856 * val I has_arg 1 name insert
1857 * val L has_arg 2 name list
1858 * val N has_arg 1 name new-chain
1859 * val P has_arg 1 name policy
1860 * val X has_arg 1 name delete-chain
1861 * val d has_arg 1 name destination
1862 * val i has_arg 1 name in-interface
1863 * val j has_arg 1 name jump
1864 * val m has_arg 1 name match
1865 * val o has_arg 1 name out-interface
1866 * val s has_arg 1 name source
1867 * val t has_arg 1 name table
1869 * After executing the `case 'm'` block longopts is
1871 * val A has_arg 1 name append
1872 * val C has_arg 1 name compare
1873 * val D has_arg 1 name delete
1874 * val F has_arg 1 name flush-chain
1875 * val I has_arg 1 name insert
1876 * val L has_arg 2 name list
1877 * val N has_arg 1 name new-chain
1878 * val P has_arg 1 name policy
1879 * val X has_arg 1 name delete-chain
1880 * val d has_arg 1 name destination
1881 * val i has_arg 1 name in-interface
1882 * val j has_arg 1 name jump
1883 * val m has_arg 1 name match
1884 * val o has_arg 1 name out-interface
1885 * val s has_arg 1 name source
1886 * val t has_arg 1 name table
1887 * val has_arg 1 name mark
1889 * So the 'mark' matcher has added the 'mark' options
1890 * and getopt will then return c '256' optarg "999" optind 9
1891 * And we will hit the 'default' statement which then
1892 * will call the matchers parser (xt_m->parser() or
1893 * xtables_option_mpcall() depending on which version
1894 * of libxtables is found.
1896 connman_bool_t invert = FALSE;
1899 ctx->ip = g_try_new0(struct ipt_ip, 1);
1900 if (ctx->ip == NULL)
1904 * Tell getopt_long not to generate error messages for unknown
1905 * options and also reset optind back to 0.
1910 while ((c = getopt_long(ctx->argc, ctx->argv,
1912 iptables_globals.opts, NULL)) != -1) {
1915 /* Source specification */
1916 if (!parse_ip_and_mask(optarg,
1922 ctx->ip->invflags |= IPT_INV_SRCIP;
1926 /* Destination specification */
1927 if (!parse_ip_and_mask(optarg,
1933 ctx->ip->invflags |= IPT_INV_DSTIP;
1936 /* In interface specification */
1937 len = strlen(optarg);
1939 if (len + 1 > IFNAMSIZ)
1942 g_stpcpy(ctx->ip->iniface, optarg);
1943 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1946 ctx->ip->invflags |= IPT_INV_VIA_IN;
1950 /* Out interface specification */
1951 len = strlen(optarg);
1953 if (len + 1 > IFNAMSIZ)
1956 g_stpcpy(ctx->ip->outiface, optarg);
1957 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1960 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1965 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1966 if (ctx->xt_m == NULL) {
1974 ctx->xt_t = prepare_target(table, optarg);
1975 if (ctx->xt_t == NULL) {
1982 if (optarg[0] == '!' && optarg[1] == '\0') {
1985 /* Remove the '!' from the optarg */
1989 * And recall getopt_long without reseting
1997 err = parse_xt_modules(c, invert, ctx);
2007 err = final_check_xt_modules(ctx);
2013 static void reset_xtables(void)
2015 struct xtables_match *xt_m;
2016 struct xtables_target *xt_t;
2019 * As side effect parsing a rule sets some global flags
2020 * which will be evaluated/verified. Let's reset them
2021 * to ensure we can parse more than one rule.
2023 * Clear all flags because the flags are only valid
2026 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
2029 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
2035 * We need also to free the memory implicitly allocated
2036 * during parsing (see xtables_options_xfrm()).
2037 * Note xt_params is actually iptables_globals.
2039 if (xt_params->opts != xt_params->orig_opts) {
2040 g_free(xt_params->opts);
2041 xt_params->opts = xt_params->orig_opts;
2043 xt_params->option_offset = 0;
2046 static void cleanup_parse_context(struct parse_context *ctx)
2048 struct xtables_rule_match *rm, *tmp;
2050 g_strfreev(ctx->argv);
2052 if (ctx->xt_t != NULL) {
2053 g_free(ctx->xt_t->t);
2054 ctx->xt_t->t = NULL;
2056 if (ctx->xt_m != NULL) {
2057 g_free(ctx->xt_m->m);
2058 ctx->xt_m->m = NULL;
2060 for (tmp = NULL, rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
2070 int __connman_iptables_dump(const char *table_name)
2072 struct connman_iptables *table;
2074 DBG("-t %s -L", table_name);
2076 table = get_table(table_name);
2085 int __connman_iptables_new_chain(const char *table_name,
2088 struct connman_iptables *table;
2090 DBG("-t %s -N %s", table_name, chain);
2092 table = get_table(table_name);
2096 return iptables_add_chain(table, chain);
2099 int __connman_iptables_delete_chain(const char *table_name,
2102 struct connman_iptables *table;
2104 DBG("-t %s -X %s", table_name, chain);
2106 table = get_table(table_name);
2110 return iptables_delete_chain(table, chain);
2113 int __connman_iptables_flush_chain(const char *table_name,
2116 struct connman_iptables *table;
2118 DBG("-t %s -F %s", table_name, chain);
2120 table = get_table(table_name);
2124 return iptables_flush_chain(table, chain);
2127 int __connman_iptables_change_policy(const char *table_name,
2131 struct connman_iptables *table;
2133 DBG("-t %s -F %s", table_name, chain);
2135 table = get_table(table_name);
2139 return iptables_change_policy(table, chain, policy);
2142 int __connman_iptables_append(const char *table_name,
2144 const char *rule_spec)
2146 struct connman_iptables *table;
2147 struct parse_context *ctx;
2148 const char *target_name;
2151 ctx = g_try_new0(struct parse_context, 1);
2155 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2157 err = prepare_getopt_args(rule_spec, ctx);
2161 table = get_table(table_name);
2162 if (table == NULL) {
2167 err = parse_rule_spec(table, ctx);
2171 if (ctx->xt_t == NULL)
2174 target_name = ctx->xt_t->name;
2176 err = iptables_append_rule(table, ctx->ip, chain,
2177 target_name, ctx->xt_t, ctx->xt_rm);
2179 cleanup_parse_context(ctx);
2185 int __connman_iptables_insert(const char *table_name,
2187 const char *rule_spec)
2189 struct connman_iptables *table;
2190 struct parse_context *ctx;
2191 const char *target_name;
2194 ctx = g_try_new0(struct parse_context, 1);
2198 DBG("-t %s -I %s %s", table_name, chain, rule_spec);
2200 err = prepare_getopt_args(rule_spec, ctx);
2204 table = get_table(table_name);
2205 if (table == NULL) {
2210 err = parse_rule_spec(table, ctx);
2214 if (ctx->xt_t == NULL)
2217 target_name = ctx->xt_t->name;
2219 err = iptables_insert_rule(table, ctx->ip, chain,
2220 target_name, ctx->xt_t, ctx->xt_rm);
2222 cleanup_parse_context(ctx);
2228 int __connman_iptables_delete(const char *table_name,
2230 const char *rule_spec)
2232 struct connman_iptables *table;
2233 struct parse_context *ctx;
2234 const char *target_name;
2237 ctx = g_try_new0(struct parse_context, 1);
2241 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2243 err = prepare_getopt_args(rule_spec, ctx);
2247 table = get_table(table_name);
2248 if (table == NULL) {
2253 err = parse_rule_spec(table, ctx);
2257 if (ctx->xt_t == NULL)
2260 target_name = ctx->xt_t->name;
2262 err = iptables_delete_rule(table, ctx->ip, chain,
2263 target_name, ctx->xt_t, ctx->xt_m,
2266 cleanup_parse_context(ctx);
2272 int __connman_iptables_commit(const char *table_name)
2274 struct connman_iptables *table;
2275 struct ipt_replace *repl;
2278 DBG("%s", table_name);
2280 table = g_hash_table_lookup(table_hash, table_name);
2284 repl = iptables_blob(table);
2286 if (debug_enabled == TRUE)
2287 dump_ipt_replace(repl);
2289 err = iptables_replace(table, repl);
2291 g_free(repl->counters);
2297 g_hash_table_remove(table_hash, table_name);
2302 static void remove_table(gpointer user_data)
2304 struct connman_iptables *table = user_data;
2306 table_cleanup(table);
2309 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2310 unsigned int hook, size_t size,
2311 unsigned int offset, void *user_data)
2313 GSList **chains = user_data;
2314 struct xt_entry_target *target;
2317 if (offset + entry->next_offset == size)
2320 target = ipt_get_target(entry);
2322 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
2323 name = g_strdup((const char*)target->data);
2324 else if (builtin >= 0)
2325 name = g_strdup(hooknames[builtin]);
2329 *chains = g_slist_prepend(*chains, name);
2334 void flush_table(const char *name)
2336 GSList *chains = NULL, *list;
2337 struct connman_iptables *table;
2339 table = get_table(name);
2343 iterate_entries(table->blob_entries->entrytable,
2344 table->info->valid_hooks,
2345 table->info->hook_entry,
2346 table->info->underflow,
2347 table->blob_entries->size,
2348 flush_table_cb, &chains);
2352 * The offset update code is fragile and it works
2353 * only safe if we remove elements and move forwards
2356 chains = g_slist_reverse(chains);
2358 for (list = chains; list != NULL; list = list->next) {
2359 char *chain = list->data;
2361 DBG("chain %s", chain);
2362 iptables_flush_chain(table, chain);
2365 __connman_iptables_commit(name);
2366 g_slist_free_full(chains, g_free);
2369 int __connman_iptables_init(void)
2373 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2374 debug_enabled = TRUE;
2376 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2377 NULL, remove_table);
2379 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2384 void __connman_iptables_cleanup(void)
2388 g_hash_table_destroy(table_hash);