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 {
165 struct ipt_getinfo *info;
166 struct ipt_get_entries *blob_entries;
168 unsigned int num_entries;
169 unsigned int old_entries;
172 unsigned int underflow[NF_INET_NUMHOOKS];
173 unsigned int hook_entry[NF_INET_NUMHOOKS];
178 static GHashTable *table_hash = NULL;
179 static gboolean debug_enabled = FALSE;
181 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
182 unsigned int hook,size_t size,
183 unsigned int offset, void *user_data);
185 static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
189 if (*valid_hooks == 0)
190 return NF_INET_NUMHOOKS;
192 h = __builtin_ffs(*valid_hooks) - 1;
193 *valid_hooks ^= (1 << h);
198 static int iterate_entries(struct ipt_entry *entries,
199 unsigned int valid_hooks,
200 unsigned int *hook_entry,
201 unsigned int *underflow,
202 size_t size, iterate_entries_cb_t cb,
205 unsigned int offset, h, hook;
207 struct ipt_entry *entry;
209 h = next_hook_entry_index(&valid_hooks);
212 for (offset = 0, entry = entries; offset < size;
213 offset += entry->next_offset) {
215 entry = (void *)entries + offset;
218 * Updating builtin, hook and h is very tricky.
220 * - builtin is only set to the current hook number
221 * if the current entry is the hook entry (aka chain
222 * head). And only for builtin chains, never for
224 * - hook is the current hook number. If we
225 * look at user chains it needs to be NF_INET_NETNUMHOOKS.
226 * - h is the next hook entry. Thous we need to be carefully
227 * not to access the table when h is NF_INET_NETNUMHOOKS.
229 if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
234 if (h == NF_INET_NUMHOOKS)
237 if (h < NF_INET_NUMHOOKS && underflow[h] <= offset) {
238 h = next_hook_entry_index(&valid_hooks);
241 err = cb(entry, builtin, hook, size, offset, user_data);
249 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
250 size_t size, unsigned int offset,
253 iterate_entries_cb_t cb = user_data;
255 DBG("entry %p hook %d offset %d size %d", entry, hook,
256 offset, entry->next_offset);
258 return cb(entry, builtin, hook, size, offset, NULL);
261 static int target_to_verdict(const char *target_name)
263 if (!strcmp(target_name, LABEL_ACCEPT))
264 return -NF_ACCEPT - 1;
266 if (!strcmp(target_name, LABEL_DROP))
269 if (!strcmp(target_name, LABEL_QUEUE))
270 return -NF_QUEUE - 1;
272 if (!strcmp(target_name, LABEL_RETURN))
278 static gboolean is_builtin_target(const char *target_name)
280 if (!strcmp(target_name, LABEL_ACCEPT) ||
281 !strcmp(target_name, LABEL_DROP) ||
282 !strcmp(target_name, LABEL_QUEUE) ||
283 !strcmp(target_name, LABEL_RETURN))
289 static gboolean is_jump(struct connman_iptables_entry *e)
291 struct xt_entry_target *target;
293 target = ipt_get_target(e->entry);
295 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
296 struct xt_standard_target *t;
298 t = (struct xt_standard_target *)target;
300 switch (t->verdict) {
316 static gboolean is_fallthrough(struct connman_iptables_entry *e)
318 struct xt_entry_target *target;
320 target = ipt_get_target(e->entry);
321 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
322 struct xt_standard_target *t;
324 t = (struct xt_standard_target *)target;
331 static gboolean is_chain(struct connman_iptables *table,
332 struct connman_iptables_entry *e)
334 struct ipt_entry *entry;
335 struct xt_entry_target *target;
341 target = ipt_get_target(entry);
342 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
348 static GList *find_chain_head(struct connman_iptables *table,
349 const char *chain_name)
352 struct connman_iptables_entry *head;
353 struct ipt_entry *entry;
354 struct xt_entry_target *target;
357 for (list = table->entries; list; list = list->next) {
362 builtin = head->builtin;
363 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
366 /* User defined chain */
367 target = ipt_get_target(entry);
368 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
369 !strcmp((char *)target->data, chain_name))
376 static GList *find_chain_tail(struct connman_iptables *table,
377 const char *chain_name)
379 struct connman_iptables_entry *tail;
380 GList *chain_head, *list;
382 chain_head = find_chain_head(table, chain_name);
383 if (chain_head == NULL)
386 /* Then we look for the next chain */
387 for (list = chain_head->next; list; list = list->next) {
390 if (is_chain(table, tail))
394 /* Nothing found, we return the table end */
395 return g_list_last(table->entries);
399 static void update_offsets(struct connman_iptables *table)
402 struct connman_iptables_entry *entry, *prev_entry;
404 for (list = table->entries; list; list = list->next) {
407 if (list == table->entries) {
414 prev_entry = prev->data;
416 entry->offset = prev_entry->offset +
417 prev_entry->entry->next_offset;
421 static void update_targets_reference(struct connman_iptables *table,
422 struct connman_iptables_entry *entry_before,
423 struct connman_iptables_entry *modified_entry,
424 gboolean is_removing)
426 struct connman_iptables_entry *tmp;
427 struct xt_standard_target *t;
431 offset = modified_entry->entry->next_offset;
433 for (list = table->entries; list; list = list->next) {
439 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
441 if (is_removing == TRUE) {
442 if (t->verdict >= entry_before->offset)
443 t->verdict -= offset;
445 if (t->verdict > entry_before->offset)
446 t->verdict += offset;
450 if (is_fallthrough(modified_entry)) {
451 t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
453 t->verdict = entry_before->offset +
454 modified_entry->entry->target_offset +
455 ALIGN(sizeof(struct xt_standard_target));
456 t->target.u.target_size =
457 ALIGN(sizeof(struct xt_standard_target));
461 static int iptables_add_entry(struct connman_iptables *table,
462 struct ipt_entry *entry, GList *before,
465 struct connman_iptables_entry *e, *entry_before;
470 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
475 e->builtin = builtin;
477 table->entries = g_list_insert_before(table->entries, before, e);
478 table->num_entries++;
479 table->size += entry->next_offset;
481 if (before == NULL) {
482 e->offset = table->size - entry->next_offset;
487 entry_before = before->data;
490 * We've just appended/insterted a new entry. All references
491 * should be bumped accordingly.
493 update_targets_reference(table, entry_before, e, FALSE);
495 update_offsets(table);
500 static int remove_table_entry(struct connman_iptables *table,
501 struct connman_iptables_entry *entry)
505 table->num_entries--;
506 table->size -= entry->entry->next_offset;
507 removed = entry->entry->next_offset;
509 table->entries = g_list_remove(table->entries, entry);
511 g_free(entry->entry);
517 static int iptables_flush_chain(struct connman_iptables *table,
520 GList *chain_head, *chain_tail, *list, *next;
521 struct connman_iptables_entry *entry;
522 int builtin, removed = 0;
524 chain_head = find_chain_head(table, name);
525 if (chain_head == NULL)
528 chain_tail = find_chain_tail(table, name);
529 if (chain_tail == NULL)
532 entry = chain_head->data;
533 builtin = entry->builtin;
538 list = chain_head->next;
540 if (list == chain_tail->prev)
543 while (list != chain_tail->prev) {
545 next = g_list_next(list);
547 removed += remove_table_entry(table, entry);
553 struct connman_iptables_entry *e;
557 entry->builtin = builtin;
559 table->underflow[builtin] -= removed;
561 for (list = chain_tail; list; list = list->next) {
564 builtin = e->builtin;
568 table->hook_entry[builtin] -= removed;
569 table->underflow[builtin] -= removed;
573 update_offsets(table);
578 static int iptables_add_chain(struct connman_iptables *table,
582 struct ipt_entry *entry_head;
583 struct ipt_entry *entry_return;
584 struct error_target *error;
585 struct ipt_standard_target *standard;
586 u_int16_t entry_head_size, entry_return_size;
588 last = g_list_last(table->entries);
591 * An empty chain is composed of:
592 * - A head entry, with no match and an error target.
593 * The error target data is the chain name.
594 * - A tail entry, with no match and a standard target.
595 * The standard target verdict is XT_RETURN (return to the
600 entry_head_size = sizeof(struct ipt_entry) +
601 sizeof(struct error_target);
602 entry_head = g_try_malloc0(entry_head_size);
603 if (entry_head == NULL)
606 memset(entry_head, 0, entry_head_size);
608 entry_head->target_offset = sizeof(struct ipt_entry);
609 entry_head->next_offset = entry_head_size;
611 error = (struct error_target *) entry_head->elems;
612 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
613 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
614 strcpy(error->error, name);
616 if (iptables_add_entry(table, entry_head, last, -1) < 0)
620 entry_return_size = sizeof(struct ipt_entry) +
621 sizeof(struct ipt_standard_target);
622 entry_return = g_try_malloc0(entry_return_size);
623 if (entry_return == NULL)
626 memset(entry_return, 0, entry_return_size);
628 entry_return->target_offset = sizeof(struct ipt_entry);
629 entry_return->next_offset = entry_return_size;
631 standard = (struct ipt_standard_target *) entry_return->elems;
632 standard->target.u.user.target_size =
633 ALIGN(sizeof(struct ipt_standard_target));
634 standard->verdict = XT_RETURN;
636 if (iptables_add_entry(table, entry_return, last, -1) < 0)
642 g_free(entry_return);
649 static int iptables_delete_chain(struct connman_iptables *table,
652 struct connman_iptables_entry *entry;
653 GList *chain_head, *chain_tail;
655 chain_head = find_chain_head(table, name);
656 if (chain_head == NULL)
659 entry = chain_head->data;
661 /* We cannot remove builtin chain */
662 if (entry->builtin >= 0)
665 chain_tail = find_chain_tail(table, name);
666 if (chain_tail == NULL)
669 /* Chain must be flushed */
670 if (chain_head->next != chain_tail->prev)
673 remove_table_entry(table, entry);
675 entry = chain_tail->prev->data;
676 remove_table_entry(table, entry);
678 update_offsets(table);
683 static struct ipt_entry *new_rule(struct ipt_ip *ip,
684 const char *target_name, struct xtables_target *xt_t,
685 struct xtables_rule_match *xt_rm)
687 struct xtables_rule_match *tmp_xt_rm;
688 struct ipt_entry *new_entry;
689 size_t match_size, target_size;
692 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
693 match_size += tmp_xt_rm->match->m->u.match_size;
696 target_size = ALIGN(xt_t->t->u.target_size);
698 target_size = ALIGN(sizeof(struct xt_standard_target));
700 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
702 if (new_entry == NULL)
705 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
707 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
708 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
712 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
713 tmp_xt_rm = tmp_xt_rm->next) {
714 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
715 tmp_xt_rm->match->m->u.match_size);
716 match_size += tmp_xt_rm->match->m->u.match_size;
720 struct xt_entry_target *entry_target;
722 entry_target = ipt_get_target(new_entry);
723 memcpy(entry_target, xt_t->t, target_size);
729 static void update_hooks(struct connman_iptables *table, GList *chain_head,
730 struct ipt_entry *entry)
733 struct connman_iptables_entry *head, *e;
736 if (chain_head == NULL)
739 head = chain_head->data;
741 builtin = head->builtin;
745 table->underflow[builtin] += entry->next_offset;
747 for (list = chain_head->next; list; list = list->next) {
750 builtin = e->builtin;
754 table->hook_entry[builtin] += entry->next_offset;
755 table->underflow[builtin] += entry->next_offset;
759 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
760 struct ipt_ip *ip, const char *chain_name,
761 const char *target_name,
762 struct xtables_target *xt_t,
763 int *builtin, struct xtables_rule_match *xt_rm)
765 GList *chain_tail, *chain_head;
766 struct ipt_entry *new_entry;
767 struct connman_iptables_entry *head;
769 chain_head = find_chain_head(table, chain_name);
770 if (chain_head == NULL)
773 chain_tail = find_chain_tail(table, chain_name);
774 if (chain_tail == NULL)
777 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
778 if (new_entry == NULL)
781 update_hooks(table, chain_head, new_entry);
784 * If the chain is builtin, and does not have any rule,
785 * then the one that we're inserting is becoming the head
786 * and thus needs the builtin flag.
788 head = chain_head->data;
789 if (head->builtin < 0)
791 else if (chain_head == chain_tail->prev) {
792 *builtin = head->builtin;
799 static int iptables_append_rule(struct connman_iptables *table,
800 struct ipt_ip *ip, const char *chain_name,
801 const char *target_name,
802 struct xtables_target *xt_t,
803 struct xtables_rule_match *xt_rm)
805 struct ipt_entry *new_entry;
806 int builtin = -1, ret;
809 chain_tail = find_chain_tail(table, chain_name);
810 if (chain_tail == NULL)
813 new_entry = prepare_rule_inclusion(table, ip, chain_name,
814 target_name, xt_t, &builtin, xt_rm);
815 if (new_entry == NULL)
818 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
825 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
826 struct ipt_entry *i_e2)
828 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
831 if (i_e1->target_offset != i_e2->target_offset)
834 if (i_e1->next_offset != i_e2->next_offset)
840 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
841 struct xt_entry_target *xt_e_t2)
845 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
848 if (strcmp(xt_e_t1->u.user.name, "") == 0 &&
849 strcmp(xt_e_t2->u.user.name, "") == 0) {
852 } else if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
853 struct xt_standard_target *xt_s_t1;
854 struct xt_standard_target *xt_s_t2;
856 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
857 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
859 if (xt_s_t1->verdict != xt_s_t2->verdict)
862 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
865 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
868 for (i = 0; i < xt_e_t1->u.target_size -
869 sizeof(struct xt_standard_target); i++) {
870 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
878 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
879 struct xt_entry_match *xt_e_m2)
883 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
886 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
889 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
892 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
895 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
897 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
904 static GList *find_existing_rule(struct connman_iptables *table,
905 struct ipt_ip *ip, const char *chain_name,
906 const char *target_name,
907 struct xtables_target *xt_t,
908 struct xtables_match *xt_m,
909 struct xtables_rule_match *xt_rm)
911 GList *chain_tail, *chain_head, *list;
912 struct xt_entry_target *xt_e_t = NULL;
913 struct xt_entry_match *xt_e_m = NULL;
914 struct connman_iptables_entry *entry;
915 struct ipt_entry *entry_test;
918 chain_head = find_chain_head(table, chain_name);
919 if (chain_head == NULL)
922 chain_tail = find_chain_tail(table, chain_name);
923 if (chain_tail == NULL)
929 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
930 if (entry_test == NULL)
934 xt_e_t = ipt_get_target(entry_test);
936 xt_e_m = (struct xt_entry_match *)entry_test->elems;
938 entry = chain_head->data;
939 builtin = entry->builtin;
944 list = chain_head->next;
946 for (; list != chain_tail->prev; list = list->next) {
947 struct connman_iptables_entry *tmp;
948 struct ipt_entry *tmp_e;
953 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
957 struct xt_entry_target *tmp_xt_e_t;
959 tmp_xt_e_t = ipt_get_target(tmp_e);
961 if (!is_same_target(tmp_xt_e_t, xt_e_t))
966 struct xt_entry_match *tmp_xt_e_m;
968 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
970 if (!is_same_match(tmp_xt_e_m, xt_e_m))
979 if (list != chain_tail->prev)
985 static int iptables_delete_rule(struct connman_iptables *table,
986 struct ipt_ip *ip, const char *chain_name,
987 const char *target_name,
988 struct xtables_target *xt_t,
989 struct xtables_match *xt_m,
990 struct xtables_rule_match *xt_rm)
992 struct connman_iptables_entry *entry;
993 GList *chain_head, *chain_tail, *list;
994 int builtin, removed;
998 chain_head = find_chain_head(table, chain_name);
999 if (chain_head == NULL)
1002 chain_tail = find_chain_tail(table, chain_name);
1003 if (chain_tail == NULL)
1006 list = find_existing_rule(table, ip, chain_name, target_name,
1011 entry = chain_head->data;
1012 builtin = entry->builtin;
1018 /* We have deleted a rule,
1019 * all references should be bumped accordingly */
1020 if (list->next != NULL)
1021 update_targets_reference(table, list->next->data,
1024 removed += remove_table_entry(table, entry);
1030 entry->builtin = builtin;
1033 table->underflow[builtin] -= removed;
1034 for (list = chain_tail; list; list = list->next) {
1037 builtin = entry->builtin;
1041 table->hook_entry[builtin] -= removed;
1042 table->underflow[builtin] -= removed;
1046 update_offsets(table);
1051 static int iptables_change_policy(struct connman_iptables *table,
1052 const char *chain_name, const char *policy)
1054 GList *chain_head, *chain_tail;
1055 struct connman_iptables_entry *entry;
1056 struct xt_entry_target *target;
1057 struct xt_standard_target *t;
1060 verdict = target_to_verdict(policy);
1062 case -NF_ACCEPT - 1:
1069 chain_head = find_chain_head(table, chain_name);
1070 if (chain_head == NULL)
1073 entry = chain_head->data;
1074 if (entry->builtin < 0)
1077 chain_tail = find_chain_tail(table, chain_name);
1078 if (chain_tail == NULL)
1081 entry = chain_tail->prev->data;
1082 target = ipt_get_target(entry->entry);
1084 t = (struct xt_standard_target *)target;
1085 t->verdict = verdict;
1090 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1092 struct ipt_replace *r;
1094 struct connman_iptables_entry *e;
1095 unsigned char *entry_index;
1097 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1101 memset(r, 0, sizeof(*r) + table->size);
1103 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1104 * table->old_entries);
1105 if (r->counters == NULL) {
1110 strcpy(r->name, table->info->name);
1111 r->num_entries = table->num_entries;
1112 r->size = table->size;
1114 r->num_counters = table->old_entries;
1115 r->valid_hooks = table->info->valid_hooks;
1117 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1118 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1120 entry_index = (unsigned char *)r->entries;
1121 for (list = table->entries; list; list = list->next) {
1124 memcpy(entry_index, e->entry, e->entry->next_offset);
1125 entry_index += e->entry->next_offset;
1131 static void dump_ip(struct ipt_entry *entry)
1133 struct ipt_ip *ip = &entry->ip;
1134 char ip_string[INET6_ADDRSTRLEN];
1135 char ip_mask[INET6_ADDRSTRLEN];
1137 if (strlen(ip->iniface))
1138 DBG("\tin %s", ip->iniface);
1140 if (strlen(ip->outiface))
1141 DBG("\tout %s", ip->outiface);
1143 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1144 inet_ntop(AF_INET, &ip->smsk,
1145 ip_mask, INET6_ADDRSTRLEN) != NULL)
1146 DBG("\tsrc %s/%s", ip_string, ip_mask);
1148 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1149 inet_ntop(AF_INET, &ip->dmsk,
1150 ip_mask, INET6_ADDRSTRLEN) != NULL)
1151 DBG("\tdst %s/%s", ip_string, ip_mask);
1154 static void dump_target(struct ipt_entry *entry)
1157 struct xtables_target *xt_t;
1158 struct xt_entry_target *target;
1160 target = ipt_get_target(entry);
1162 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1163 struct xt_standard_target *t;
1165 t = (struct xt_standard_target *)target;
1167 switch (t->verdict) {
1169 DBG("\ttarget RETURN");
1172 case -NF_ACCEPT - 1:
1173 DBG("\ttarget ACCEPT");
1177 DBG("\ttarget DROP");
1181 DBG("\ttarget QUEUE");
1185 DBG("\ttarget STOP");
1189 DBG("\tJUMP %u", t->verdict);
1193 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1194 XTF_LOAD_MUST_SUCCEED);
1196 if(xt_t->print != NULL)
1197 xt_t->print(NULL, target, 1);
1199 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1201 DBG("\ttarget %s", target->u.user.name);
1205 if(xt_t->print != NULL) {
1207 xt_t->print(NULL, target, 1);
1212 static void dump_match(struct ipt_entry *entry)
1214 struct xtables_match *xt_m;
1215 struct xt_entry_match *match;
1217 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1220 match = (struct xt_entry_match *) entry->elems;
1222 if (!strlen(match->u.user.name))
1225 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1229 if(xt_m->print != NULL) {
1231 xt_m->print(NULL, match, 1);
1237 DBG("\tmatch %s", match->u.user.name);
1241 static int dump_entry(struct ipt_entry *entry, int builtin,
1242 unsigned int hook, size_t size, unsigned int offset,
1245 struct xt_entry_target *target;
1247 target = ipt_get_target(entry);
1249 if (offset + entry->next_offset == size) {
1250 DBG("\tEnd of CHAIN");
1254 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1255 DBG("\tUSER CHAIN (%s) match %p target %p",
1256 target->data, entry->elems,
1257 (char *)entry + entry->target_offset);
1260 } else if (builtin >= 0) {
1261 DBG("\tCHAIN (%s) match %p target %p",
1262 hooknames[builtin], entry->elems,
1263 (char *)entry + entry->target_offset);
1265 DBG("\tRULE match %p target %p",
1267 (char *)entry + entry->target_offset);
1277 static void dump_table(struct connman_iptables *table)
1279 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1281 table->info->valid_hooks, table->info->num_entries,
1284 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1285 table->info->hook_entry[NF_IP_PRE_ROUTING],
1286 table->info->hook_entry[NF_IP_LOCAL_IN],
1287 table->info->hook_entry[NF_IP_FORWARD],
1288 table->info->hook_entry[NF_IP_LOCAL_OUT],
1289 table->info->hook_entry[NF_IP_POST_ROUTING]);
1290 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1291 table->info->underflow[NF_IP_PRE_ROUTING],
1292 table->info->underflow[NF_IP_LOCAL_IN],
1293 table->info->underflow[NF_IP_FORWARD],
1294 table->info->underflow[NF_IP_LOCAL_OUT],
1295 table->info->underflow[NF_IP_POST_ROUTING]);
1297 iterate_entries(table->blob_entries->entrytable,
1298 table->info->valid_hooks,
1299 table->info->hook_entry,
1300 table->info->underflow,
1301 table->blob_entries->size,
1302 print_entry, dump_entry);
1305 static void dump_ipt_replace(struct ipt_replace *repl)
1307 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1308 repl->name, repl->valid_hooks, repl->num_entries,
1311 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1312 repl->hook_entry[NF_IP_PRE_ROUTING],
1313 repl->hook_entry[NF_IP_LOCAL_IN],
1314 repl->hook_entry[NF_IP_FORWARD],
1315 repl->hook_entry[NF_IP_LOCAL_OUT],
1316 repl->hook_entry[NF_IP_POST_ROUTING]);
1317 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1318 repl->underflow[NF_IP_PRE_ROUTING],
1319 repl->underflow[NF_IP_LOCAL_IN],
1320 repl->underflow[NF_IP_FORWARD],
1321 repl->underflow[NF_IP_LOCAL_OUT],
1322 repl->underflow[NF_IP_POST_ROUTING]);
1324 iterate_entries(repl->entries, repl->valid_hooks,
1325 repl->hook_entry, repl->underflow,
1326 repl->size, print_entry, dump_entry);
1329 static int iptables_get_entries(struct connman_iptables *table)
1331 socklen_t entry_size;
1333 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1335 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1336 table->blob_entries, &entry_size);
1339 static int iptables_replace(struct connman_iptables *table,
1340 struct ipt_replace *r)
1342 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1343 sizeof(*r) + r->size);
1346 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1347 size_t size, unsigned offset, void *user_data)
1349 struct connman_iptables *table = user_data;
1350 struct ipt_entry *new_entry;
1352 new_entry = g_try_malloc0(entry->next_offset);
1353 if (new_entry == NULL)
1356 memcpy(new_entry, entry, entry->next_offset);
1358 return iptables_add_entry(table, new_entry, NULL, builtin);
1361 static void table_cleanup(struct connman_iptables *table)
1364 struct connman_iptables_entry *entry;
1369 if (table->ipt_sock >= 0)
1370 close(table->ipt_sock);
1372 for (list = table->entries; list; list = list->next) {
1375 g_free(entry->entry);
1379 g_list_free(table->entries);
1380 g_free(table->info);
1381 g_free(table->blob_entries);
1385 static struct connman_iptables *iptables_init(const char *table_name)
1387 struct connman_iptables *table = NULL;
1388 char *module = NULL;
1391 if (table_name == NULL)
1392 table_name = "filter";
1394 DBG("%s", table_name);
1396 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1397 DBG("ip_tables module loading gives error but trying anyway");
1399 module = g_strconcat("iptable_", table_name, NULL);
1403 if (xtables_insmod(module, NULL, TRUE) != 0)
1404 DBG("%s module loading gives error but trying anyway", module);
1408 table = g_hash_table_lookup(table_hash, table_name);
1412 table = g_try_new0(struct connman_iptables, 1);
1416 table->info = g_try_new0(struct ipt_getinfo, 1);
1417 if (table->info == NULL)
1420 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1421 if (table->ipt_sock < 0)
1424 s = sizeof(*table->info);
1425 strcpy(table->info->name, table_name);
1426 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1427 table->info, &s) < 0) {
1428 connman_error("iptables support missing error %d (%s)", errno,
1433 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1435 if (table->blob_entries == NULL)
1438 strcpy(table->blob_entries->name, table_name);
1439 table->blob_entries->size = table->info->size;
1441 if (iptables_get_entries(table) < 0)
1444 table->num_entries = 0;
1445 table->old_entries = table->info->num_entries;
1448 memcpy(table->underflow, table->info->underflow,
1449 sizeof(table->info->underflow));
1450 memcpy(table->hook_entry, table->info->hook_entry,
1451 sizeof(table->info->hook_entry));
1453 iterate_entries(table->blob_entries->entrytable,
1454 table->info->valid_hooks, table->info->hook_entry,
1455 table->info->underflow, table->blob_entries->size,
1458 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1460 if (debug_enabled == TRUE)
1466 table_cleanup(table);
1471 static struct option iptables_opts[] = {
1472 {.name = "append", .has_arg = 1, .val = 'A'},
1473 {.name = "compare", .has_arg = 1, .val = 'C'},
1474 {.name = "delete", .has_arg = 1, .val = 'D'},
1475 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1476 {.name = "insert", .has_arg = 1, .val = 'I'},
1477 {.name = "list", .has_arg = 2, .val = 'L'},
1478 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1479 {.name = "policy", .has_arg = 1, .val = 'P'},
1480 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1481 {.name = "destination", .has_arg = 1, .val = 'd'},
1482 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1483 {.name = "jump", .has_arg = 1, .val = 'j'},
1484 {.name = "match", .has_arg = 1, .val = 'm'},
1485 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1486 {.name = "source", .has_arg = 1, .val = 's'},
1487 {.name = "table", .has_arg = 1, .val = 't'},
1491 struct xtables_globals iptables_globals = {
1493 .opts = iptables_opts,
1494 .orig_opts = iptables_opts,
1497 static struct xtables_target *prepare_target(struct connman_iptables *table,
1498 const char *target_name)
1500 struct xtables_target *xt_t = NULL;
1501 gboolean is_builtin, is_user_defined;
1502 GList *chain_head = NULL;
1506 is_user_defined = FALSE;
1508 if (is_builtin_target(target_name))
1511 chain_head = find_chain_head(table, target_name);
1512 if (chain_head != NULL && chain_head->next != NULL)
1513 is_user_defined = TRUE;
1516 if (is_builtin || is_user_defined)
1517 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1518 XTF_LOAD_MUST_SUCCEED);
1520 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1525 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1527 xt_t->t = g_try_malloc0(target_size);
1528 if (xt_t->t == NULL)
1531 xt_t->t->u.target_size = target_size;
1533 if (is_builtin || is_user_defined) {
1534 struct xt_standard_target *target;
1536 target = (struct xt_standard_target *)(xt_t->t);
1537 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1539 if (is_builtin == TRUE)
1540 target->verdict = target_to_verdict(target_name);
1541 else if (is_user_defined == TRUE) {
1542 struct connman_iptables_entry *target_rule;
1544 if (chain_head == NULL) {
1549 target_rule = chain_head->next->data;
1550 target->verdict = target_rule->offset;
1553 strcpy(xt_t->t->u.user.name, target_name);
1554 xt_t->t->u.user.revision = xt_t->revision;
1555 if (xt_t->init != NULL)
1556 xt_t->init(xt_t->t);
1559 #if XTABLES_VERSION_CODE > 5
1560 if (xt_t->x6_options != NULL)
1561 iptables_globals.opts =
1562 xtables_options_xfrm(
1563 iptables_globals.orig_opts,
1564 iptables_globals.opts,
1566 &xt_t->option_offset);
1569 iptables_globals.opts =
1570 xtables_merge_options(
1571 #if XTABLES_VERSION_CODE > 5
1572 iptables_globals.orig_opts,
1574 iptables_globals.opts,
1576 &xt_t->option_offset);
1578 if (iptables_globals.opts == NULL) {
1586 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1587 struct xtables_rule_match **xt_rm,
1588 const char *match_name)
1590 struct xtables_match *xt_m;
1593 if (match_name == NULL)
1596 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1597 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1599 xt_m->m = g_try_malloc0(match_size);
1600 if (xt_m->m == NULL)
1603 xt_m->m->u.match_size = match_size;
1604 strcpy(xt_m->m->u.user.name, xt_m->name);
1605 xt_m->m->u.user.revision = xt_m->revision;
1607 if (xt_m->init != NULL)
1608 xt_m->init(xt_m->m);
1610 #if XTABLES_VERSION_CODE > 5
1611 if (xt_m->x6_options != NULL)
1612 iptables_globals.opts =
1613 xtables_options_xfrm(
1614 iptables_globals.orig_opts,
1615 iptables_globals.opts,
1617 &xt_m->option_offset);
1620 iptables_globals.opts =
1621 xtables_merge_options(
1622 #if XTABLES_VERSION_CODE > 5
1623 iptables_globals.orig_opts,
1625 iptables_globals.opts,
1627 &xt_m->option_offset);
1629 if (iptables_globals.opts == NULL) {
1637 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1640 uint32_t prefixlength;
1644 tokens = g_strsplit(str, "/", 2);
1648 if (!inet_pton(AF_INET, tokens[0], ip)) {
1653 if (tokens[1] != NULL) {
1654 prefixlength = strtol(tokens[1], NULL, 10);
1655 if (prefixlength > 31) {
1660 tmp = ~(0xffffffff >> prefixlength);
1665 mask->s_addr = htonl(tmp);
1666 ip->s_addr = ip->s_addr & mask->s_addr;
1674 static struct connman_iptables *pre_load_table(const char *table_name,
1675 struct connman_iptables *table)
1680 return iptables_init(table_name);
1683 struct parse_context {
1687 struct xtables_target *xt_t;
1688 struct xtables_match *xt_m;
1689 struct xtables_rule_match *xt_rm;
1692 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1697 tokens = g_strsplit_set(str, " ", -1);
1699 i = g_strv_length(tokens);
1701 /* Add space for the argv[0] value */
1704 /* Don't forget the last NULL entry */
1705 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1706 if (ctx->argv == NULL) {
1712 * getopt_long() jumps over the first token; we need to add some
1713 * random argv[0] entry.
1715 ctx->argv[0] = g_strdup("argh");
1716 for (i = 1; i < ctx->argc; i++)
1717 ctx->argv[i] = tokens[i - 1];
1724 #if XTABLES_VERSION_CODE > 5
1726 static int parse_xt_modules(int c, connman_bool_t invert,
1727 struct parse_context *ctx)
1729 struct xtables_match *m;
1730 struct xtables_rule_match *rm;
1732 DBG("xtables version code > 5");
1734 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1735 if (rm->completed != 0)
1740 if (m->x6_parse == NULL && m->parse == NULL)
1743 if (c < (int) m->option_offset ||
1744 c >= (int) m->option_offset
1745 + XT_OPTION_OFFSET_SCALE)
1748 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1751 if (ctx->xt_t == NULL)
1754 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1757 if (c < (int) ctx->xt_t->option_offset ||
1758 c >= (int) ctx->xt_t->option_offset
1759 + XT_OPTION_OFFSET_SCALE)
1762 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1767 static int final_check_xt_modules(struct parse_context *ctx)
1769 struct xtables_rule_match *rm;
1771 DBG("xtables version code > 5");
1773 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1774 xtables_option_mfcall(rm->match);
1776 if (ctx->xt_t != NULL)
1777 xtables_option_tfcall(ctx->xt_t);
1784 static int parse_xt_modules(int c, connman_bool_t invert,
1785 struct parse_context *ctx)
1787 struct xtables_match *m;
1788 struct xtables_rule_match *rm;
1791 DBG("xtables version code <= 5");
1793 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1794 if (rm->completed == 1)
1799 if (m->parse == NULL)
1802 err = m->parse(c - m->option_offset,
1803 argv, invert, &m->mflags,
1809 if (ctx->xt_t == NULL)
1812 if (ctx->xt_t->parse == NULL)
1815 err = ctx->xt_m->parse(c - ctx->xt_m->option_offset,
1816 ctx->argv, invert, &ctx->xt_m->mflags,
1817 NULL, &ctx->xt_m->m);
1821 static int final_check_xt_modules(struct parse_context *ctx)
1823 struct xtables_rule_match *rm;
1825 DBG("xtables version code <= 5");
1827 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1828 if (rm->match->final_check != NULL)
1829 rm->match->final_check(rm->match->mflags);
1831 if (ctx->xt_t != NULL && ctx->xt_t->final_check != NULL)
1832 ctx->xt_t->final_check(ctx->xt_t->tflags);
1839 static int parse_rule_spec(struct connman_iptables *table,
1840 struct parse_context *ctx)
1843 * How the parser works:
1845 * - If getopt finds 's', 'd', 'i', 'o'.
1846 * just extract the information.
1847 * - if '!' is found, set the invert flag to true and
1848 * removes the '!' from the optarg string and jumps
1849 * back to getopt to reparse the current optarg string.
1850 * After reparsing the invert flag is reseted to false.
1851 * - If 'm' or 'j' is found then call either
1852 * prepare_matches() or prepare_target(). Those function
1853 * will modify (extend) the longopts for getopt_long.
1854 * That means getopt will change its matching context according
1855 * the loaded target.
1857 * Here an example with iptables-test
1859 * argv[0] = ./tools/iptables-test
1871 * getopt found 'm' then the optarg is "mark" and optind 7
1872 * The longopts array containts before hitting the `case 'm'`
1874 * val A has_arg 1 name append
1875 * val C has_arg 1 name compare
1876 * val D has_arg 1 name delete
1877 * val F has_arg 1 name flush-chain
1878 * val I has_arg 1 name insert
1879 * val L has_arg 2 name list
1880 * val N has_arg 1 name new-chain
1881 * val P has_arg 1 name policy
1882 * val X has_arg 1 name delete-chain
1883 * val d has_arg 1 name destination
1884 * val i has_arg 1 name in-interface
1885 * val j has_arg 1 name jump
1886 * val m has_arg 1 name match
1887 * val o has_arg 1 name out-interface
1888 * val s has_arg 1 name source
1889 * val t has_arg 1 name table
1891 * After executing the `case 'm'` block longopts is
1893 * val A has_arg 1 name append
1894 * val C has_arg 1 name compare
1895 * val D has_arg 1 name delete
1896 * val F has_arg 1 name flush-chain
1897 * val I has_arg 1 name insert
1898 * val L has_arg 2 name list
1899 * val N has_arg 1 name new-chain
1900 * val P has_arg 1 name policy
1901 * val X has_arg 1 name delete-chain
1902 * val d has_arg 1 name destination
1903 * val i has_arg 1 name in-interface
1904 * val j has_arg 1 name jump
1905 * val m has_arg 1 name match
1906 * val o has_arg 1 name out-interface
1907 * val s has_arg 1 name source
1908 * val t has_arg 1 name table
1909 * val has_arg 1 name mark
1911 * So the 'mark' matcher has added the 'mark' options
1912 * and getopt will then return c '256' optarg "999" optind 9
1913 * And we will hit the 'default' statement which then
1914 * will call the matchers parser (xt_m->parser() or
1915 * xtables_option_mpcall() depending on which version
1916 * of libxtables is found.
1918 connman_bool_t invert = FALSE;
1923 ctx->ip = g_try_new0(struct ipt_ip, 1);
1924 if (ctx->ip == NULL)
1928 * Tell getopt_long not to generate error messages for unknown
1929 * options and also reset optind back to 0.
1934 while ((c = getopt_long(ctx->argc, ctx->argv,
1936 iptables_globals.opts, NULL)) != -1) {
1939 /* Source specification */
1940 if (!parse_ip_and_mask(optarg,
1946 ctx->ip->invflags |= IPT_INV_SRCIP;
1950 /* Destination specification */
1951 if (!parse_ip_and_mask(optarg,
1957 ctx->ip->invflags |= IPT_INV_DSTIP;
1960 /* In interface specification */
1961 len = strlen(optarg);
1963 if (len + 1 > IFNAMSIZ)
1966 strcpy(ctx->ip->iniface, optarg);
1967 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1970 ctx->ip->invflags |= IPT_INV_VIA_IN;
1974 /* Out interface specification */
1975 len = strlen(optarg);
1977 if (len + 1 > IFNAMSIZ)
1980 strcpy(ctx->ip->outiface, optarg);
1981 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1984 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1989 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1990 if (ctx->xt_m == NULL) {
1998 ctx->xt_t = prepare_target(table, optarg);
1999 if (ctx->xt_t == NULL) {
2006 if (optarg[0] == '!' && optarg[1] == '\0') {
2009 /* Remove the '!' from the optarg */
2013 * And recall getopt_long without reseting
2021 err = parse_xt_modules(c, invert, ctx);
2031 err = final_check_xt_modules(ctx);
2037 static void reset_xtables(void)
2039 struct xtables_match *xt_m;
2040 struct xtables_target *xt_t;
2043 * As side effect parsing a rule sets some global flags
2044 * which will be evaluated/verified. Let's reset them
2045 * to ensure we can parse more than one rule.
2047 * Clear all flags because the flags are only valid
2050 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
2053 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
2059 * We need also to free the memory implicitly allocated
2060 * during parsing (see xtables_options_xfrm()).
2061 * Note xt_params is actually iptables_globals.
2063 if (xt_params->opts != xt_params->orig_opts) {
2064 g_free(xt_params->opts);
2065 xt_params->opts = xt_params->orig_opts;
2067 xt_params->option_offset = 0;
2070 static void cleanup_parse_context(struct parse_context *ctx)
2072 struct xtables_rule_match *rm, *tmp;
2074 g_strfreev(ctx->argv);
2076 if (ctx->xt_t != NULL) {
2077 g_free(ctx->xt_t->t);
2078 ctx->xt_t->t = NULL;
2080 if (ctx->xt_m != NULL) {
2081 g_free(ctx->xt_m->m);
2082 ctx->xt_m->m = NULL;
2084 for (tmp = NULL, rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
2094 int __connman_iptables_dump(const char *table_name)
2096 struct connman_iptables *table;
2098 DBG("-t %s -L", table_name);
2100 table = pre_load_table(table_name, NULL);
2109 int __connman_iptables_new_chain(const char *table_name,
2112 struct connman_iptables *table;
2114 DBG("-t %s -N %s", table_name, chain);
2116 table = pre_load_table(table_name, NULL);
2120 return iptables_add_chain(table, chain);
2123 int __connman_iptables_delete_chain(const char *table_name,
2126 struct connman_iptables *table;
2128 DBG("-t %s -X %s", table_name, chain);
2130 table = pre_load_table(table_name, NULL);
2134 return iptables_delete_chain(table, chain);
2137 int __connman_iptables_flush_chain(const char *table_name,
2140 struct connman_iptables *table;
2142 DBG("-t %s -F %s", table_name, chain);
2144 table = pre_load_table(table_name, NULL);
2148 return iptables_flush_chain(table, chain);
2151 int __connman_iptables_change_policy(const char *table_name,
2155 struct connman_iptables *table;
2157 DBG("-t %s -F %s", table_name, chain);
2159 table = pre_load_table(table_name, NULL);
2163 return iptables_change_policy(table, chain, policy);
2166 int __connman_iptables_append(const char *table_name,
2168 const char *rule_spec)
2170 struct connman_iptables *table;
2171 struct parse_context *ctx;
2172 const char *target_name;
2175 ctx = g_try_new0(struct parse_context, 1);
2179 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2181 err = prepare_getopt_args(rule_spec, ctx);
2185 table = pre_load_table(table_name, NULL);
2186 if (table == NULL) {
2191 err = parse_rule_spec(table, ctx);
2195 if (ctx->xt_t == NULL)
2198 target_name = ctx->xt_t->name;
2200 err = iptables_append_rule(table, ctx->ip, chain,
2201 target_name, ctx->xt_t, ctx->xt_rm);
2203 cleanup_parse_context(ctx);
2209 int __connman_iptables_delete(const char *table_name,
2211 const char *rule_spec)
2213 struct connman_iptables *table;
2214 struct parse_context *ctx;
2215 const char *target_name;
2218 ctx = g_try_new0(struct parse_context, 1);
2222 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2224 err = prepare_getopt_args(rule_spec, ctx);
2228 table = pre_load_table(table_name, NULL);
2229 if (table == NULL) {
2234 err = parse_rule_spec(table, ctx);
2238 if (ctx->xt_t == NULL)
2241 target_name = ctx->xt_t->name;
2243 err = iptables_delete_rule(table, ctx->ip, chain,
2244 target_name, ctx->xt_t, ctx->xt_m,
2247 cleanup_parse_context(ctx);
2253 int __connman_iptables_commit(const char *table_name)
2255 struct connman_iptables *table;
2256 struct ipt_replace *repl;
2259 DBG("%s", table_name);
2261 table = g_hash_table_lookup(table_hash, table_name);
2265 repl = iptables_blob(table);
2267 if (debug_enabled == TRUE)
2268 dump_ipt_replace(repl);
2270 err = iptables_replace(table, repl);
2272 g_free(repl->counters);
2278 g_hash_table_remove(table_hash, table_name);
2283 static void remove_table(gpointer user_data)
2285 struct connman_iptables *table = user_data;
2287 table_cleanup(table);
2290 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2291 unsigned int hook, size_t size,
2292 unsigned int offset, void *user_data)
2294 GSList **chains = user_data;
2295 struct xt_entry_target *target;
2298 if (offset + entry->next_offset == size)
2301 target = ipt_get_target(entry);
2303 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
2304 name = g_strdup((const char*)target->data);
2305 else if (builtin >= 0)
2306 name = g_strdup(hooknames[builtin]);
2310 *chains = g_slist_prepend(*chains, name);
2315 void flush_table(const char *name)
2317 GSList *chains = NULL, *list;
2318 struct connman_iptables *table;
2320 table = pre_load_table(name, NULL);
2324 iterate_entries(table->blob_entries->entrytable,
2325 table->info->valid_hooks,
2326 table->info->hook_entry,
2327 table->info->underflow,
2328 table->blob_entries->size,
2329 flush_table_cb, &chains);
2333 * The offset update code is fragile and it works
2334 * only safe if we remove elements and move forwards
2337 chains = g_slist_reverse(chains);
2339 for (list = chains; list != NULL; list = list->next) {
2340 char *chain = list->data;
2342 DBG("chain %s", chain);
2343 iptables_flush_chain(table, chain);
2346 __connman_iptables_commit(name);
2347 g_slist_free_full(chains, g_free);
2350 int __connman_iptables_init(void)
2354 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2355 debug_enabled = TRUE;
2357 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2358 g_free, remove_table);
2360 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2365 void __connman_iptables_cleanup(void)
2369 g_hash_table_destroy(table_hash);