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>
41 * Some comments on how the iptables API works (some of them from the
42 * source code from iptables and the kernel):
44 * - valid_hooks: bit indicates valid IDs for hook_entry
45 * - hook_entry[ID] offset to the chain start
46 * - overflows should be end of entry chains, and uncodintional policy nodes.
47 * - policy entry: last entry in a chain
48 * - user chain: end of last builtin + policy entry
49 * - final entry must be error node
50 * - Underflows must be unconditional and use the STANDARD target with
52 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
53 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
54 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
55 * table header/meta info. The table is appended after the header. The entries
56 * are of the type struct ipt_entry.
57 * - After the ipt_entry the matches are appended. After the matches
58 * the target is appended.
59 * - ipt_entry->target_offset = Size of ipt_entry + matches
60 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
61 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
62 * - hook_entry and overflow mark the begining and the end of a chain, e.g
63 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
64 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
65 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
66 * the last element). FORWARD has one entry starting/ending at 352. The entry
67 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
68 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
70 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
71 * writing that table looks like this:
73 * filter valid_hooks 0x0000000e num_entries 5 size 856
74 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
75 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
76 * entry 0x699d30 offset 0 size 224
77 * RULE match 0x699da0 target 0x699dd0
78 * match mark match 0x3e7
79 * target LOG flags 0 level 4
82 * entry 0x699e10 offset 224 size 152
83 * RULE match 0x699e80 target 0x699e80
87 * entry 0x699ea8 offset 376 size 152
88 * RULE match 0x699f18 target 0x699f18
92 * entry 0x699f40 offset 528 size 152
93 * RULE match 0x699fb0 target 0x699fb0
97 * entry 0x699fd8 offset 680 size 176
98 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
100 * Reading the filter table looks like this:
102 * filter valid_hooks 0x0000000e num_entries 5 size 856
103 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
104 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
105 * entry 0x25fec28 offset 0 size 224
106 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
107 * match mark match 0x3e7
108 * target LOG flags 0 level 4
109 * src 0.0.0.0/0.0.0.0
110 * dst 0.0.0.0/0.0.0.0
111 * entry 0x25fed08 offset 224 size 152
112 * RULE match 0x25fed78 target 0x25fed78
114 * src 0.0.0.0/0.0.0.0
115 * dst 0.0.0.0/0.0.0.0
116 * entry 0x25feda0 offset 376 size 152
117 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
119 * src 0.0.0.0/0.0.0.0
120 * dst 0.0.0.0/0.0.0.0
121 * entry 0x25fee38 offset 528 size 152
122 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
124 * src 0.0.0.0/0.0.0.0
125 * dst 0.0.0.0/0.0.0.0
126 * entry 0x25feed0 offset 680 size 176
130 static const char *hooknames[] = {
131 [NF_IP_PRE_ROUTING] = "PREROUTING",
132 [NF_IP_LOCAL_IN] = "INPUT",
133 [NF_IP_FORWARD] = "FORWARD",
134 [NF_IP_LOCAL_OUT] = "OUTPUT",
135 [NF_IP_POST_ROUTING] = "POSTROUTING",
138 #define LABEL_ACCEPT "ACCEPT"
139 #define LABEL_DROP "DROP"
140 #define LABEL_QUEUE "QUEUE"
141 #define LABEL_RETURN "RETURN"
143 #define XT_OPTION_OFFSET_SCALE 256
145 #define MIN_ALIGN (__alignof__(struct ipt_entry))
147 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
149 struct error_target {
150 struct xt_entry_target t;
151 char error[IPT_TABLE_MAXNAMELEN];
154 struct connman_iptables_entry {
158 struct ipt_entry *entry;
161 struct connman_iptables {
164 struct ipt_getinfo *info;
165 struct ipt_get_entries *blob_entries;
167 unsigned int num_entries;
168 unsigned int old_entries;
171 unsigned int underflow[NF_INET_NUMHOOKS];
172 unsigned int hook_entry[NF_INET_NUMHOOKS];
177 static GHashTable *table_hash = NULL;
178 static gboolean debug_enabled = FALSE;
180 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
181 unsigned int hook,size_t size,
182 unsigned int offset, void *user_data);
184 static int iterate_entries(struct ipt_entry *entries,
185 unsigned int valid_hooks,
186 unsigned int *hook_entry,
187 size_t size, iterate_entries_cb_t cb,
192 struct ipt_entry *entry;
194 if (valid_hooks != 0)
195 h = __builtin_ffs(valid_hooks) - 1;
197 h = NF_INET_NUMHOOKS;
199 for (i = 0, entry = entries; i < size;
200 i += entry->next_offset) {
202 entry = (void *)entries + i;
205 * Find next valid hook which offset is higher
206 * or equal with the current offset.
208 if (h < NF_INET_NUMHOOKS) {
209 if (hook_entry[h] < i) {
210 valid_hooks ^= (1 << h);
212 if (valid_hooks != 0)
213 h = __builtin_ffs(valid_hooks) - 1;
215 h = NF_INET_NUMHOOKS;
218 if (hook_entry[h] == i)
222 err = cb(entry, builtin, h, size, i, user_data);
231 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
232 size_t size, unsigned int offset,
235 iterate_entries_cb_t cb = user_data;
237 DBG("entry %p hook %d offset %d size %d", entry, hook,
238 offset, entry->next_offset);
240 return cb(entry, builtin, hook, size, offset, NULL);
243 static int target_to_verdict(const char *target_name)
245 if (!strcmp(target_name, LABEL_ACCEPT))
246 return -NF_ACCEPT - 1;
248 if (!strcmp(target_name, LABEL_DROP))
251 if (!strcmp(target_name, LABEL_QUEUE))
252 return -NF_QUEUE - 1;
254 if (!strcmp(target_name, LABEL_RETURN))
260 static gboolean is_builtin_target(const char *target_name)
262 if (!strcmp(target_name, LABEL_ACCEPT) ||
263 !strcmp(target_name, LABEL_DROP) ||
264 !strcmp(target_name, LABEL_QUEUE) ||
265 !strcmp(target_name, LABEL_RETURN))
271 static gboolean is_jump(struct connman_iptables_entry *e)
273 struct xt_entry_target *target;
275 target = ipt_get_target(e->entry);
277 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
278 struct xt_standard_target *t;
280 t = (struct xt_standard_target *)target;
282 switch (t->verdict) {
298 static gboolean is_chain(struct connman_iptables *table,
299 struct connman_iptables_entry *e)
301 struct ipt_entry *entry;
302 struct xt_entry_target *target;
308 target = ipt_get_target(entry);
309 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
315 static GList *find_chain_head(struct connman_iptables *table,
316 const char *chain_name)
319 struct connman_iptables_entry *head;
320 struct ipt_entry *entry;
321 struct xt_entry_target *target;
324 for (list = table->entries; list; list = list->next) {
329 builtin = head->builtin;
330 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
333 /* User defined chain */
334 target = ipt_get_target(entry);
335 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
336 !strcmp((char *)target->data, chain_name))
343 static GList *find_chain_tail(struct connman_iptables *table,
344 const char *chain_name)
346 struct connman_iptables_entry *tail;
347 GList *chain_head, *list;
349 chain_head = find_chain_head(table, chain_name);
350 if (chain_head == NULL)
353 /* Then we look for the next chain */
354 for (list = chain_head->next; list; list = list->next) {
357 if (is_chain(table, tail))
361 /* Nothing found, we return the table end */
362 return g_list_last(table->entries);
366 static void update_offsets(struct connman_iptables *table)
369 struct connman_iptables_entry *entry, *prev_entry;
371 for (list = table->entries; list; list = list->next) {
374 if (list == table->entries) {
381 prev_entry = prev->data;
383 entry->offset = prev_entry->offset +
384 prev_entry->entry->next_offset;
388 static void update_targets_reference(struct connman_iptables *table,
389 struct connman_iptables_entry *entry_before,
390 struct connman_iptables_entry *modified_entry,
391 gboolean is_removing)
393 struct connman_iptables_entry *tmp;
394 struct xt_standard_target *t;
398 offset = modified_entry->entry->next_offset;
400 for (list = table->entries; list; list = list->next) {
406 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
408 if (is_removing == TRUE) {
409 if (t->verdict >= entry_before->offset)
410 t->verdict -= offset;
412 if (t->verdict > entry_before->offset)
413 t->verdict += offset;
418 static int iptables_add_entry(struct connman_iptables *table,
419 struct ipt_entry *entry, GList *before,
422 struct connman_iptables_entry *e, *entry_before;
427 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
432 e->builtin = builtin;
434 table->entries = g_list_insert_before(table->entries, before, e);
435 table->num_entries++;
436 table->size += entry->next_offset;
438 if (before == NULL) {
439 e->offset = table->size - entry->next_offset;
444 entry_before = before->data;
447 * We've just appended/insterted a new entry. All references
448 * should be bumped accordingly.
450 update_targets_reference(table, entry_before, e, FALSE);
452 update_offsets(table);
457 static int remove_table_entry(struct connman_iptables *table,
458 struct connman_iptables_entry *entry)
462 table->num_entries--;
463 table->size -= entry->entry->next_offset;
464 removed = entry->entry->next_offset;
466 g_free(entry->entry);
468 table->entries = g_list_remove(table->entries, entry);
473 static int iptables_flush_chain(struct connman_iptables *table,
476 GList *chain_head, *chain_tail, *list, *next;
477 struct connman_iptables_entry *entry;
478 int builtin, removed = 0;
480 chain_head = find_chain_head(table, name);
481 if (chain_head == NULL)
484 chain_tail = find_chain_tail(table, name);
485 if (chain_tail == NULL)
488 entry = chain_head->data;
489 builtin = entry->builtin;
494 list = chain_head->next;
496 if (list == chain_tail->prev)
499 while (list != chain_tail->prev) {
501 next = g_list_next(list);
503 removed += remove_table_entry(table, entry);
509 struct connman_iptables_entry *e;
513 entry->builtin = builtin;
515 table->underflow[builtin] -= removed;
517 for (list = chain_tail; list; list = list->next) {
520 builtin = e->builtin;
524 table->hook_entry[builtin] -= removed;
525 table->underflow[builtin] -= removed;
529 update_offsets(table);
534 static int iptables_add_chain(struct connman_iptables *table,
538 struct ipt_entry *entry_head;
539 struct ipt_entry *entry_return;
540 struct error_target *error;
541 struct ipt_standard_target *standard;
542 u_int16_t entry_head_size, entry_return_size;
544 last = g_list_last(table->entries);
547 * An empty chain is composed of:
548 * - A head entry, with no match and an error target.
549 * The error target data is the chain name.
550 * - A tail entry, with no match and a standard target.
551 * The standard target verdict is XT_RETURN (return to the
556 entry_head_size = sizeof(struct ipt_entry) +
557 sizeof(struct error_target);
558 entry_head = g_try_malloc0(entry_head_size);
559 if (entry_head == NULL)
562 memset(entry_head, 0, entry_head_size);
564 entry_head->target_offset = sizeof(struct ipt_entry);
565 entry_head->next_offset = entry_head_size;
567 error = (struct error_target *) entry_head->elems;
568 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
569 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
570 strcpy(error->error, name);
572 if (iptables_add_entry(table, entry_head, last, -1) < 0)
576 entry_return_size = sizeof(struct ipt_entry) +
577 sizeof(struct ipt_standard_target);
578 entry_return = g_try_malloc0(entry_return_size);
579 if (entry_return == NULL)
582 memset(entry_return, 0, entry_return_size);
584 entry_return->target_offset = sizeof(struct ipt_entry);
585 entry_return->next_offset = entry_return_size;
587 standard = (struct ipt_standard_target *) entry_return->elems;
588 standard->target.u.user.target_size =
589 ALIGN(sizeof(struct ipt_standard_target));
590 standard->verdict = XT_RETURN;
592 if (iptables_add_entry(table, entry_return, last, -1) < 0)
598 g_free(entry_return);
605 static int iptables_delete_chain(struct connman_iptables *table,
608 struct connman_iptables_entry *entry;
609 GList *chain_head, *chain_tail;
611 chain_head = find_chain_head(table, name);
612 if (chain_head == NULL)
615 entry = chain_head->data;
617 /* We cannot remove builtin chain */
618 if (entry->builtin >= 0)
621 chain_tail = find_chain_tail(table, name);
622 if (chain_tail == NULL)
625 /* Chain must be flushed */
626 if (chain_head->next != chain_tail->prev)
629 remove_table_entry(table, entry);
631 entry = chain_tail->prev->data;
632 remove_table_entry(table, entry);
634 update_offsets(table);
639 static struct ipt_entry *new_rule(struct ipt_ip *ip,
640 const char *target_name, struct xtables_target *xt_t,
641 struct xtables_rule_match *xt_rm)
643 struct xtables_rule_match *tmp_xt_rm;
644 struct ipt_entry *new_entry;
645 size_t match_size, target_size;
648 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
649 match_size += tmp_xt_rm->match->m->u.match_size;
652 target_size = ALIGN(xt_t->t->u.target_size);
654 target_size = ALIGN(sizeof(struct xt_standard_target));
656 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
658 if (new_entry == NULL)
661 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
663 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
664 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
668 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
669 tmp_xt_rm = tmp_xt_rm->next) {
670 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
671 tmp_xt_rm->match->m->u.match_size);
672 match_size += tmp_xt_rm->match->m->u.match_size;
676 struct xt_entry_target *entry_target;
678 entry_target = ipt_get_target(new_entry);
679 memcpy(entry_target, xt_t->t, target_size);
685 static void update_hooks(struct connman_iptables *table, GList *chain_head,
686 struct ipt_entry *entry)
689 struct connman_iptables_entry *head, *e;
692 if (chain_head == NULL)
695 head = chain_head->data;
697 builtin = head->builtin;
701 table->underflow[builtin] += entry->next_offset;
703 for (list = chain_head->next; list; list = list->next) {
706 builtin = e->builtin;
710 table->hook_entry[builtin] += entry->next_offset;
711 table->underflow[builtin] += entry->next_offset;
715 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
716 struct ipt_ip *ip, const char *chain_name,
717 const char *target_name,
718 struct xtables_target *xt_t,
719 int *builtin, struct xtables_rule_match *xt_rm)
721 GList *chain_tail, *chain_head;
722 struct ipt_entry *new_entry;
723 struct connman_iptables_entry *head;
725 chain_head = find_chain_head(table, chain_name);
726 if (chain_head == NULL)
729 chain_tail = find_chain_tail(table, chain_name);
730 if (chain_tail == NULL)
733 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
734 if (new_entry == NULL)
737 update_hooks(table, chain_head, new_entry);
740 * If the chain is builtin, and does not have any rule,
741 * then the one that we're inserting is becoming the head
742 * and thus needs the builtin flag.
744 head = chain_head->data;
745 if (head->builtin < 0)
747 else if (chain_head == chain_tail->prev) {
748 *builtin = head->builtin;
755 static int iptables_insert_rule(struct connman_iptables *table,
756 struct ipt_ip *ip, const char *chain_name,
757 const char *target_name,
758 struct xtables_target *xt_t,
759 struct xtables_rule_match *xt_rm)
761 struct ipt_entry *new_entry;
762 int builtin = -1, ret;
765 chain_head = find_chain_head(table, chain_name);
766 if (chain_head == NULL)
769 new_entry = prepare_rule_inclusion(table, ip, chain_name,
770 target_name, xt_t, &builtin, xt_rm);
771 if (new_entry == NULL)
775 chain_head = chain_head->next;
777 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
784 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
785 struct ipt_entry *i_e2)
787 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
790 if (i_e1->target_offset != i_e2->target_offset)
793 if (i_e1->next_offset != i_e2->next_offset)
799 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
800 struct xt_entry_target *xt_e_t2)
802 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
805 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
806 struct xt_standard_target *xt_s_t1;
807 struct xt_standard_target *xt_s_t2;
809 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
810 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
812 if (xt_s_t1->verdict != xt_s_t2->verdict)
815 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
818 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
825 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
826 struct xt_entry_match *xt_e_m2)
828 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
831 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
834 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
837 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
843 static GList *find_existing_rule(struct connman_iptables *table,
844 struct ipt_ip *ip, const char *chain_name,
845 const char *target_name,
846 struct xtables_target *xt_t,
847 struct xtables_match *xt_m,
848 struct xtables_rule_match *xt_rm)
850 GList *chain_tail, *chain_head, *list;
851 struct xt_entry_target *xt_e_t = NULL;
852 struct xt_entry_match *xt_e_m = NULL;
853 struct connman_iptables_entry *entry;
854 struct ipt_entry *entry_test;
857 chain_head = find_chain_head(table, chain_name);
858 if (chain_head == NULL)
861 chain_tail = find_chain_tail(table, chain_name);
862 if (chain_tail == NULL)
868 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
869 if (entry_test == NULL)
873 xt_e_t = ipt_get_target(entry_test);
875 xt_e_m = (struct xt_entry_match *)entry_test->elems;
877 entry = chain_head->data;
878 builtin = entry->builtin;
883 list = chain_head->next;
885 for (; list != chain_tail->prev; list = list->next) {
886 struct connman_iptables_entry *tmp;
887 struct ipt_entry *tmp_e;
892 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
896 struct xt_entry_target *tmp_xt_e_t;
898 tmp_xt_e_t = ipt_get_target(tmp_e);
900 if (!is_same_target(tmp_xt_e_t, xt_e_t))
905 struct xt_entry_match *tmp_xt_e_m;
907 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
909 if (!is_same_match(tmp_xt_e_m, xt_e_m))
918 if (list != chain_tail->prev)
924 static int iptables_delete_rule(struct connman_iptables *table,
925 struct ipt_ip *ip, const char *chain_name,
926 const char *target_name,
927 struct xtables_target *xt_t,
928 struct xtables_match *xt_m,
929 struct xtables_rule_match *xt_rm)
931 struct connman_iptables_entry *entry;
932 GList *chain_tail, *list;
933 int builtin, removed;
937 chain_tail = find_chain_tail(table, chain_name);
938 if (chain_tail == NULL)
941 list = find_existing_rule(table, ip, chain_name, target_name,
950 builtin = entry->builtin;
952 /* We have deleted a rule,
953 * all references should be bumped accordingly */
954 if (list->next != NULL)
955 update_targets_reference(table, list->next->data,
958 removed += remove_table_entry(table, entry);
964 entry->builtin = builtin;
967 table->underflow[builtin] -= removed;
968 for (list = chain_tail; list; list = list->next) {
971 builtin = entry->builtin;
975 table->hook_entry[builtin] -= removed;
976 table->underflow[builtin] -= removed;
980 update_offsets(table);
985 static int iptables_change_policy(struct connman_iptables *table,
986 const char *chain_name, const char *policy)
989 struct connman_iptables_entry *entry;
990 struct xt_entry_target *target;
991 struct xt_standard_target *t;
994 verdict = target_to_verdict(policy);
998 chain_head = find_chain_head(table, chain_name);
999 if (chain_head == NULL)
1002 entry = chain_head->data;
1003 if (entry->builtin < 0)
1006 target = ipt_get_target(entry->entry);
1008 t = (struct xt_standard_target *)target;
1009 t->verdict = verdict;
1014 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1016 struct ipt_replace *r;
1018 struct connman_iptables_entry *e;
1019 unsigned char *entry_index;
1021 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1025 memset(r, 0, sizeof(*r) + table->size);
1027 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1028 * table->old_entries);
1029 if (r->counters == NULL) {
1034 strcpy(r->name, table->info->name);
1035 r->num_entries = table->num_entries;
1036 r->size = table->size;
1038 r->num_counters = table->old_entries;
1039 r->valid_hooks = table->info->valid_hooks;
1041 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1042 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1044 entry_index = (unsigned char *)r->entries;
1045 for (list = table->entries; list; list = list->next) {
1048 memcpy(entry_index, e->entry, e->entry->next_offset);
1049 entry_index += e->entry->next_offset;
1055 static void dump_ip(struct ipt_entry *entry)
1057 struct ipt_ip *ip = &entry->ip;
1058 char ip_string[INET6_ADDRSTRLEN];
1059 char ip_mask[INET6_ADDRSTRLEN];
1061 if (strlen(ip->iniface))
1062 DBG("\tin %s", ip->iniface);
1064 if (strlen(ip->outiface))
1065 DBG("\tout %s", ip->outiface);
1067 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1068 inet_ntop(AF_INET, &ip->smsk,
1069 ip_mask, INET6_ADDRSTRLEN) != NULL)
1070 DBG("\tsrc %s/%s", ip_string, ip_mask);
1072 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1073 inet_ntop(AF_INET, &ip->dmsk,
1074 ip_mask, INET6_ADDRSTRLEN) != NULL)
1075 DBG("\tdst %s/%s", ip_string, ip_mask);
1078 static void dump_target(struct ipt_entry *entry)
1081 struct xtables_target *xt_t;
1082 struct xt_entry_target *target;
1084 target = ipt_get_target(entry);
1086 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1087 struct xt_standard_target *t;
1089 t = (struct xt_standard_target *)target;
1091 switch (t->verdict) {
1093 DBG("\ttarget RETURN");
1096 case -NF_ACCEPT - 1:
1097 DBG("\ttarget ACCEPT");
1101 DBG("\ttarget DROP");
1105 DBG("\ttarget QUEUE");
1109 DBG("\ttarget STOP");
1113 DBG("\tJUMP %u", t->verdict);
1117 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1118 XTF_LOAD_MUST_SUCCEED);
1120 if(xt_t->print != NULL)
1121 xt_t->print(NULL, target, 1);
1123 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1125 DBG("\ttarget %s", target->u.user.name);
1129 if(xt_t->print != NULL) {
1131 xt_t->print(NULL, target, 1);
1136 static void dump_match(struct ipt_entry *entry)
1138 struct xtables_match *xt_m;
1139 struct xt_entry_match *match;
1141 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1144 match = (struct xt_entry_match *) entry->elems;
1146 if (!strlen(match->u.user.name))
1149 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1153 if(xt_m->print != NULL) {
1155 xt_m->print(NULL, match, 1);
1161 DBG("\tmatch %s", match->u.user.name);
1165 static int dump_entry(struct ipt_entry *entry, int builtin,
1166 unsigned int hook, size_t size, unsigned int offset,
1169 struct xt_entry_target *target;
1171 target = ipt_get_target(entry);
1173 if (offset + entry->next_offset == size) {
1174 DBG("\tEnd of CHAIN");
1178 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1179 DBG("\tUSER CHAIN (%s) match %p target %p",
1180 target->data, entry->elems,
1181 (char *)entry + entry->target_offset);
1184 } else if (builtin >= 0) {
1185 DBG("\tCHAIN (%s) match %p target %p",
1186 hooknames[builtin], entry->elems,
1187 (char *)entry + entry->target_offset);
1189 DBG("\tRULE match %p target %p",
1191 (char *)entry + entry->target_offset);
1201 static void dump_table(struct connman_iptables *table)
1203 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1205 table->info->valid_hooks, table->info->num_entries,
1208 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1209 table->info->hook_entry[NF_IP_PRE_ROUTING],
1210 table->info->hook_entry[NF_IP_LOCAL_IN],
1211 table->info->hook_entry[NF_IP_FORWARD],
1212 table->info->hook_entry[NF_IP_LOCAL_OUT],
1213 table->info->hook_entry[NF_IP_POST_ROUTING]);
1214 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1215 table->info->underflow[NF_IP_PRE_ROUTING],
1216 table->info->underflow[NF_IP_LOCAL_IN],
1217 table->info->underflow[NF_IP_FORWARD],
1218 table->info->underflow[NF_IP_LOCAL_OUT],
1219 table->info->underflow[NF_IP_POST_ROUTING]);
1221 iterate_entries(table->blob_entries->entrytable,
1222 table->info->valid_hooks,
1223 table->info->hook_entry,
1224 table->blob_entries->size,
1225 print_entry, dump_entry);
1228 static void dump_ipt_replace(struct ipt_replace *repl)
1230 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1231 repl->name, repl->valid_hooks, repl->num_entries,
1234 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1235 repl->hook_entry[NF_IP_PRE_ROUTING],
1236 repl->hook_entry[NF_IP_LOCAL_IN],
1237 repl->hook_entry[NF_IP_FORWARD],
1238 repl->hook_entry[NF_IP_LOCAL_OUT],
1239 repl->hook_entry[NF_IP_POST_ROUTING]);
1240 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1241 repl->underflow[NF_IP_PRE_ROUTING],
1242 repl->underflow[NF_IP_LOCAL_IN],
1243 repl->underflow[NF_IP_FORWARD],
1244 repl->underflow[NF_IP_LOCAL_OUT],
1245 repl->underflow[NF_IP_POST_ROUTING]);
1247 iterate_entries(repl->entries, repl->valid_hooks,
1248 repl->hook_entry, repl->size, print_entry, dump_entry);
1251 static int iptables_get_entries(struct connman_iptables *table)
1253 socklen_t entry_size;
1255 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1257 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1258 table->blob_entries, &entry_size);
1261 static int iptables_replace(struct connman_iptables *table,
1262 struct ipt_replace *r)
1264 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1265 sizeof(*r) + r->size);
1268 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1269 size_t size, unsigned offset, void *user_data)
1271 struct connman_iptables *table = user_data;
1272 struct ipt_entry *new_entry;
1274 new_entry = g_try_malloc0(entry->next_offset);
1275 if (new_entry == NULL)
1278 memcpy(new_entry, entry, entry->next_offset);
1280 return iptables_add_entry(table, new_entry, NULL, builtin);
1283 static void table_cleanup(struct connman_iptables *table)
1286 struct connman_iptables_entry *entry;
1291 if (table->ipt_sock >= 0)
1292 close(table->ipt_sock);
1294 for (list = table->entries; list; list = list->next) {
1297 g_free(entry->entry);
1301 g_list_free(table->entries);
1302 g_free(table->info);
1303 g_free(table->blob_entries);
1307 static struct connman_iptables *iptables_init(const char *table_name)
1309 struct connman_iptables *table = NULL;
1310 char *module = NULL;
1313 if (table_name == NULL)
1314 table_name = "filter";
1316 DBG("%s", table_name);
1318 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1319 DBG("ip_tables module loading gives error but trying anyway");
1321 module = g_strconcat("iptable_", table_name, NULL);
1325 if (xtables_insmod(module, NULL, TRUE) != 0)
1326 DBG("%s module loading gives error but trying anyway", module);
1330 table = g_hash_table_lookup(table_hash, table_name);
1334 table = g_try_new0(struct connman_iptables, 1);
1338 table->info = g_try_new0(struct ipt_getinfo, 1);
1339 if (table->info == NULL)
1342 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1343 if (table->ipt_sock < 0)
1346 s = sizeof(*table->info);
1347 strcpy(table->info->name, table_name);
1348 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1349 table->info, &s) < 0) {
1350 connman_error("iptables support missing error %d (%s)", errno,
1355 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1357 if (table->blob_entries == NULL)
1360 strcpy(table->blob_entries->name, table_name);
1361 table->blob_entries->size = table->info->size;
1363 if (iptables_get_entries(table) < 0)
1366 table->num_entries = 0;
1367 table->old_entries = table->info->num_entries;
1370 memcpy(table->underflow, table->info->underflow,
1371 sizeof(table->info->underflow));
1372 memcpy(table->hook_entry, table->info->hook_entry,
1373 sizeof(table->info->hook_entry));
1375 iterate_entries(table->blob_entries->entrytable,
1376 table->info->valid_hooks, table->info->hook_entry,
1377 table->blob_entries->size, add_entry, table);
1379 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1381 if (debug_enabled == TRUE)
1387 table_cleanup(table);
1392 static struct option iptables_opts[] = {
1393 {.name = "append", .has_arg = 1, .val = 'A'},
1394 {.name = "compare", .has_arg = 1, .val = 'C'},
1395 {.name = "delete", .has_arg = 1, .val = 'D'},
1396 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1397 {.name = "insert", .has_arg = 1, .val = 'I'},
1398 {.name = "list", .has_arg = 2, .val = 'L'},
1399 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1400 {.name = "policy", .has_arg = 1, .val = 'P'},
1401 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1402 {.name = "destination", .has_arg = 1, .val = 'd'},
1403 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1404 {.name = "jump", .has_arg = 1, .val = 'j'},
1405 {.name = "match", .has_arg = 1, .val = 'm'},
1406 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1407 {.name = "source", .has_arg = 1, .val = 's'},
1408 {.name = "table", .has_arg = 1, .val = 't'},
1412 struct xtables_globals iptables_globals = {
1414 .opts = iptables_opts,
1415 .orig_opts = iptables_opts,
1418 static struct xtables_target *prepare_target(struct connman_iptables *table,
1419 const char *target_name)
1421 struct xtables_target *xt_t = NULL;
1422 gboolean is_builtin, is_user_defined;
1423 GList *chain_head = NULL;
1427 is_user_defined = FALSE;
1429 if (is_builtin_target(target_name))
1432 chain_head = find_chain_head(table, target_name);
1433 if (chain_head != NULL && chain_head->next != NULL)
1434 is_user_defined = TRUE;
1437 if (is_builtin || is_user_defined)
1438 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1439 XTF_LOAD_MUST_SUCCEED);
1441 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1446 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1448 xt_t->t = g_try_malloc0(target_size);
1449 if (xt_t->t == NULL)
1452 xt_t->t->u.target_size = target_size;
1454 if (is_builtin || is_user_defined) {
1455 struct xt_standard_target *target;
1457 target = (struct xt_standard_target *)(xt_t->t);
1458 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1460 if (is_builtin == TRUE)
1461 target->verdict = target_to_verdict(target_name);
1462 else if (is_user_defined == TRUE) {
1463 struct connman_iptables_entry *target_rule;
1465 if (chain_head == NULL) {
1470 target_rule = chain_head->next->data;
1471 target->verdict = target_rule->offset;
1474 strcpy(xt_t->t->u.user.name, target_name);
1475 xt_t->t->u.user.revision = xt_t->revision;
1476 if (xt_t->init != NULL)
1477 xt_t->init(xt_t->t);
1480 #if XTABLES_VERSION_CODE > 5
1481 if (xt_t->x6_options != NULL)
1482 iptables_globals.opts =
1483 xtables_options_xfrm(
1484 iptables_globals.orig_opts,
1485 iptables_globals.opts,
1487 &xt_t->option_offset);
1490 iptables_globals.opts =
1491 xtables_merge_options(
1492 #if XTABLES_VERSION_CODE > 5
1493 iptables_globals.orig_opts,
1495 iptables_globals.opts,
1497 &xt_t->option_offset);
1499 if (iptables_globals.opts == NULL) {
1507 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1508 struct xtables_rule_match **xt_rm,
1509 const char *match_name)
1511 struct xtables_match *xt_m;
1514 if (match_name == NULL)
1517 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1518 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1520 xt_m->m = g_try_malloc0(match_size);
1521 if (xt_m->m == NULL)
1524 xt_m->m->u.match_size = match_size;
1525 strcpy(xt_m->m->u.user.name, xt_m->name);
1526 xt_m->m->u.user.revision = xt_m->revision;
1528 if (xt_m->init != NULL)
1529 xt_m->init(xt_m->m);
1531 if (xt_m == xt_m->next)
1534 #if XTABLES_VERSION_CODE > 5
1535 if (xt_m->x6_options != NULL)
1536 iptables_globals.opts =
1537 xtables_options_xfrm(
1538 iptables_globals.orig_opts,
1539 iptables_globals.opts,
1541 &xt_m->option_offset);
1544 iptables_globals.opts =
1545 xtables_merge_options(
1546 #if XTABLES_VERSION_CODE > 5
1547 iptables_globals.orig_opts,
1549 iptables_globals.opts,
1551 &xt_m->option_offset);
1553 if (iptables_globals.opts == NULL) {
1562 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1565 uint32_t prefixlength;
1569 tokens = g_strsplit(str, "/", 2);
1573 if (!inet_pton(AF_INET, tokens[0], ip)) {
1578 if (tokens[1] != NULL) {
1579 prefixlength = strtol(tokens[1], NULL, 10);
1580 if (prefixlength > 31) {
1585 tmp = ~(0xffffffff >> prefixlength);
1590 mask->s_addr = htonl(tmp);
1591 ip->s_addr = ip->s_addr & mask->s_addr;
1599 static struct connman_iptables *pre_load_table(const char *table_name,
1600 struct connman_iptables *table)
1605 return iptables_init(table_name);
1608 static void clear_tables_flags(void)
1610 struct xtables_match *xt_m;
1611 struct xtables_target *xt_t;
1614 * Clear all flags because the flags are only valid
1617 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1620 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
1626 struct parse_context {
1630 struct xtables_target *xt_t;
1631 struct xtables_match *xt_m;
1632 struct xtables_rule_match *xt_rm;
1635 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1640 tokens = g_strsplit_set(str, " ", -1);
1642 for (i = 0; tokens[i]; i++);
1644 /* Add space for the argv[0] value */
1647 /* Don't forget the last NULL entry */
1648 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1649 if (ctx->argv == NULL) {
1655 * getopt_long() jumps over the first token; we need to add some
1656 * random argv[0] entry.
1658 ctx->argv[0] = g_strdup("argh");
1659 for (i = 1; i < ctx->argc; i++)
1660 ctx->argv[i] = tokens[i - 1];
1665 #if XTABLES_VERSION_CODE > 5
1667 static int parse_xt_modules(int c, connman_bool_t invert,
1668 struct parse_context *ctx)
1670 struct xtables_match *m;
1671 struct xtables_rule_match *rm;
1673 DBG("xtables version code > 5");
1675 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1676 if (rm->completed != 0)
1681 if (m->x6_parse == NULL && m->parse == NULL)
1684 if (c < (int) m->option_offset ||
1685 c >= (int) m->option_offset
1686 + XT_OPTION_OFFSET_SCALE)
1689 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1692 if (ctx->xt_t == NULL)
1695 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1698 if (c < (int) ctx->xt_t->option_offset ||
1699 c >= (int) ctx->xt_t->option_offset
1700 + XT_OPTION_OFFSET_SCALE)
1703 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1708 static int final_check_xt_modules(struct parse_context *ctx)
1710 struct xtables_rule_match *rm;
1712 DBG("xtables version code > 5");
1714 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1715 xtables_option_mfcall(rm->match);
1717 if (ctx->xt_t != NULL)
1718 xtables_option_tfcall(ctx->xt_t);
1725 static int parse_xt_modules(int c, connman_bool_t invert,
1726 struct parse_context *ctx)
1728 struct xtables_match *m;
1729 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 == 1)
1740 if (m->parse == NULL)
1743 err = m->parse(c - m->option_offset,
1744 argv, invert, &m->mflags,
1750 if (ctx->xt_t == NULL)
1753 if (ctx->xt_t->parse == NULL)
1756 err = ctx->xt_m->parse(c - ctx->xt_m->option_offset,
1757 ctx->argv, invert, &ctx->xt_m->mflags,
1758 NULL, &ctx->xt_m->m);
1762 static int final_check_xt_modules(struct parse_context *ctx)
1764 struct xtables_rule_match *rm;
1766 DBG("xtables version code <= 5");
1768 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1769 if (rm->match->final_check != NULL)
1770 rm->match->final_check(rm->match->mflags);
1772 if (ctx->xt_t != NULL && ctx->xt_t->final_check != NULL)
1773 ctx->xt_t->final_check(ctx->xt_t->tflags);
1780 static int parse_rule_spec(struct connman_iptables *table,
1781 struct parse_context *ctx)
1784 * How the parser works:
1786 * - If getopt finds 's', 'd', 'i', 'o'.
1787 * just extract the information.
1788 * - if '!' is found, set the invert flag to true and
1789 * removes the '!' from the optarg string and jumps
1790 * back to getopt to reparse the current optarg string.
1791 * After reparsing the invert flag is reseted to false.
1792 * - If 'm' or 'j' is found then call either
1793 * prepare_matches() or prepare_target(). Those function
1794 * will modify (extend) the longopts for getopt_long.
1795 * That means getopt will change its matching context according
1796 * the loaded target.
1798 * Here an example with iptables-test
1800 * argv[0] = ./tools/iptables-test
1812 * getopt found 'm' then the optarg is "mark" and optind 7
1813 * The longopts array containts before hitting the `case 'm'`
1815 * val A has_arg 1 name append
1816 * val C has_arg 1 name compare
1817 * val D has_arg 1 name delete
1818 * val F has_arg 1 name flush-chain
1819 * val I has_arg 1 name insert
1820 * val L has_arg 2 name list
1821 * val N has_arg 1 name new-chain
1822 * val P has_arg 1 name policy
1823 * val X has_arg 1 name delete-chain
1824 * val d has_arg 1 name destination
1825 * val i has_arg 1 name in-interface
1826 * val j has_arg 1 name jump
1827 * val m has_arg 1 name match
1828 * val o has_arg 1 name out-interface
1829 * val s has_arg 1 name source
1830 * val t has_arg 1 name table
1832 * After executing the `case 'm'` block longopts is
1834 * val A has_arg 1 name append
1835 * val C has_arg 1 name compare
1836 * val D has_arg 1 name delete
1837 * val F has_arg 1 name flush-chain
1838 * val I has_arg 1 name insert
1839 * val L has_arg 2 name list
1840 * val N has_arg 1 name new-chain
1841 * val P has_arg 1 name policy
1842 * val X has_arg 1 name delete-chain
1843 * val d has_arg 1 name destination
1844 * val i has_arg 1 name in-interface
1845 * val j has_arg 1 name jump
1846 * val m has_arg 1 name match
1847 * val o has_arg 1 name out-interface
1848 * val s has_arg 1 name source
1849 * val t has_arg 1 name table
1850 * val has_arg 1 name mark
1852 * So the 'mark' matcher has added the 'mark' options
1853 * and getopt will then return c '256' optarg "999" optind 9
1854 * And we will hit the 'default' statement which then
1855 * will call the matchers parser (xt_m->parser() or
1856 * xtables_option_mpcall() depending on which version
1857 * of libxtables is found.
1859 connman_bool_t invert = FALSE;
1864 ctx->ip = g_try_new0(struct ipt_ip, 1);
1865 if (ctx->ip == NULL)
1869 * As side effect parsing a rule sets some global flags
1870 * which will be evaluated/verified. Let's reset them
1871 * to ensure we can parse more than one rule.
1873 clear_tables_flags();
1876 * Tell getopt_long not to generate error messages for unknown
1877 * options and also reset optind back to 0.
1882 while ((c = getopt_long(ctx->argc, ctx->argv,
1884 iptables_globals.opts, NULL)) != -1) {
1887 /* Source specification */
1888 if (!parse_ip_and_mask(optarg,
1894 ctx->ip->invflags |= IPT_INV_SRCIP;
1898 /* Destination specification */
1899 if (!parse_ip_and_mask(optarg,
1905 ctx->ip->invflags |= IPT_INV_DSTIP;
1908 /* In interface specification */
1909 len = strlen(optarg);
1911 if (len + 1 > IFNAMSIZ)
1914 strcpy(ctx->ip->iniface, optarg);
1915 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1918 ctx->ip->invflags |= IPT_INV_VIA_IN;
1922 /* Out interface specification */
1923 len = strlen(optarg);
1925 if (len + 1 > IFNAMSIZ)
1928 strcpy(ctx->ip->outiface, optarg);
1929 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1932 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1937 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1938 if (ctx->xt_m == NULL) {
1946 ctx->xt_t = prepare_target(table, optarg);
1947 if (ctx->xt_t == NULL) {
1954 if (optarg[0] == '!' && optarg[1] == '\0') {
1957 /* Remove the '!' from the optarg */
1961 * And recall getopt_long without reseting
1969 err = parse_xt_modules(c, invert, ctx);
1979 err = final_check_xt_modules(ctx);
1985 static void cleanup_parse_context(struct parse_context *ctx)
1987 g_strfreev(ctx->argv);
1989 if (ctx->xt_t != NULL)
1990 g_free(ctx->xt_t->t);
1991 if (ctx->xt_m != NULL)
1992 g_free(ctx->xt_m->m);
1996 int __connman_iptables_new_chain(const char *table_name,
1999 struct connman_iptables *table;
2001 DBG("-t %s -N %s", table_name, chain);
2003 table = pre_load_table(table_name, NULL);
2007 return iptables_add_chain(table, chain);
2010 int __connman_iptables_delete_chain(const char *table_name,
2013 struct connman_iptables *table;
2015 DBG("-t %s -X %s", table_name, chain);
2017 table = pre_load_table(table_name, NULL);
2021 return iptables_delete_chain(table, chain);
2024 int __connman_iptables_flush_chain(const char *table_name,
2027 struct connman_iptables *table;
2029 DBG("-t %s -F %s", table_name, chain);
2031 table = pre_load_table(table_name, NULL);
2035 return iptables_flush_chain(table, chain);
2038 int __connman_iptables_change_policy(const char *table_name,
2042 struct connman_iptables *table;
2044 DBG("-t %s -F %s", table_name, chain);
2046 table = pre_load_table(table_name, NULL);
2050 return iptables_change_policy(table, chain, policy);
2053 int __connman_iptables_append(const char *table_name,
2055 const char *rule_spec)
2057 struct connman_iptables *table;
2058 struct parse_context *ctx;
2059 const char *target_name;
2062 ctx = g_try_new0(struct parse_context, 1);
2066 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2068 err = prepare_getopt_args(rule_spec, ctx);
2072 table = pre_load_table(table_name, NULL);
2073 if (table == NULL) {
2078 err = parse_rule_spec(table, ctx);
2082 if (ctx->xt_t == NULL)
2085 target_name = ctx->xt_t->name;
2087 err = iptables_insert_rule(table, ctx->ip, chain,
2088 target_name, ctx->xt_t, ctx->xt_rm);
2090 cleanup_parse_context(ctx);
2095 int __connman_iptables_delete(const char *table_name,
2097 const char *rule_spec)
2099 struct connman_iptables *table;
2100 struct parse_context *ctx;
2101 const char *target_name;
2104 ctx = g_try_new0(struct parse_context, 1);
2108 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2110 err = prepare_getopt_args(rule_spec, ctx);
2114 table = pre_load_table(table_name, NULL);
2115 if (table == NULL) {
2120 err = parse_rule_spec(table, ctx);
2124 if (ctx->xt_t == NULL)
2127 target_name = ctx->xt_t->name;
2129 err = iptables_delete_rule(table, ctx->ip, chain,
2130 target_name, ctx->xt_t, ctx->xt_m,
2133 cleanup_parse_context(ctx);
2138 int __connman_iptables_commit(const char *table_name)
2140 struct connman_iptables *table;
2141 struct ipt_replace *repl;
2144 DBG("%s", table_name);
2146 table = g_hash_table_lookup(table_hash, table_name);
2150 repl = iptables_blob(table);
2152 if (debug_enabled == TRUE)
2153 dump_ipt_replace(repl);
2155 err = iptables_replace(table, repl);
2157 g_free(repl->counters);
2163 g_hash_table_remove(table_hash, table_name);
2168 static void remove_table(gpointer user_data)
2170 struct connman_iptables *table = user_data;
2172 table_cleanup(table);
2175 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2176 unsigned int hook, size_t size,
2177 unsigned int offset, void *user_data)
2179 GSList **chains = user_data;
2180 struct xt_entry_target *target;
2183 if (offset + entry->next_offset == size)
2186 target = ipt_get_target(entry);
2188 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
2189 name = g_strdup((const char*)target->data);
2190 else if (builtin >= 0)
2191 name = g_strdup(hooknames[builtin]);
2195 *chains = g_slist_prepend(*chains, name);
2200 static void flush_table(const char *name)
2202 GSList *chains = NULL, *list;
2203 struct connman_iptables *table;
2205 table = pre_load_table(name, NULL);
2209 iterate_entries(table->blob_entries->entrytable,
2210 table->info->valid_hooks,
2211 table->info->hook_entry,
2212 table->blob_entries->size,
2213 flush_table_cb, &chains);
2215 for (list = chains; list != NULL; list = list->next) {
2216 char *chain = list->data;
2218 DBG("chain %s", chain);
2219 iptables_flush_chain(table, chain);
2222 __connman_iptables_commit(name);
2223 g_slist_free_full(chains, g_free);
2226 static void flush_all_chains(void)
2228 flush_table("filter");
2229 flush_table("mangle");
2233 int __connman_iptables_init(void)
2237 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2238 debug_enabled = TRUE;
2240 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2241 g_free, remove_table);
2243 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2250 void __connman_iptables_cleanup(void)
2254 g_hash_table_destroy(table_hash);
2256 xtables_free_opts(1);