5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
39 void flush_table(const char *name);
42 * Some comments on how the iptables API works (some of them from the
43 * source code from iptables and the kernel):
45 * - valid_hooks: bit indicates valid IDs for hook_entry
46 * - hook_entry[ID] offset to the chain start
47 * - overflows should be end of entry chains, and uncodintional policy nodes.
48 * - policy entry: last entry in a chain
49 * - user chain: end of last builtin + policy entry
50 * - final entry must be error node
51 * - Underflows must be unconditional and use the STANDARD target with
53 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
54 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
55 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
56 * table header/meta info. The table is appended after the header. The entries
57 * are of the type struct ipt_entry.
58 * - After the ipt_entry the matches are appended. After the matches
59 * the target is appended.
60 * - ipt_entry->target_offset = Size of ipt_entry + matches
61 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
62 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
63 * - hook_entry and overflow mark the begining and the end of a chain, e.g
64 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
65 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
66 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
67 * the last element). FORWARD has one entry starting/ending at 352. The entry
68 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
69 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
71 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
72 * writing that table looks like this:
74 * filter valid_hooks 0x0000000e num_entries 5 size 856
75 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
76 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
77 * entry 0x699d30 offset 0 size 224
78 * RULE match 0x699da0 target 0x699dd0
79 * match mark match 0x3e7
80 * target LOG flags 0 level 4
83 * entry 0x699e10 offset 224 size 152
84 * RULE match 0x699e80 target 0x699e80
88 * entry 0x699ea8 offset 376 size 152
89 * RULE match 0x699f18 target 0x699f18
93 * entry 0x699f40 offset 528 size 152
94 * RULE match 0x699fb0 target 0x699fb0
98 * entry 0x699fd8 offset 680 size 176
99 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
101 * Reading the filter table looks like this:
103 * filter valid_hooks 0x0000000e num_entries 5 size 856
104 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
105 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
106 * entry 0x25fec28 offset 0 size 224
107 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
108 * match mark match 0x3e7
109 * target LOG flags 0 level 4
110 * src 0.0.0.0/0.0.0.0
111 * dst 0.0.0.0/0.0.0.0
112 * entry 0x25fed08 offset 224 size 152
113 * RULE match 0x25fed78 target 0x25fed78
115 * src 0.0.0.0/0.0.0.0
116 * dst 0.0.0.0/0.0.0.0
117 * entry 0x25feda0 offset 376 size 152
118 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
120 * src 0.0.0.0/0.0.0.0
121 * dst 0.0.0.0/0.0.0.0
122 * entry 0x25fee38 offset 528 size 152
123 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
125 * src 0.0.0.0/0.0.0.0
126 * dst 0.0.0.0/0.0.0.0
127 * entry 0x25feed0 offset 680 size 176
131 static const char *hooknames[] = {
132 [NF_IP_PRE_ROUTING] = "PREROUTING",
133 [NF_IP_LOCAL_IN] = "INPUT",
134 [NF_IP_FORWARD] = "FORWARD",
135 [NF_IP_LOCAL_OUT] = "OUTPUT",
136 [NF_IP_POST_ROUTING] = "POSTROUTING",
139 #define LABEL_ACCEPT "ACCEPT"
140 #define LABEL_DROP "DROP"
141 #define LABEL_QUEUE "QUEUE"
142 #define LABEL_RETURN "RETURN"
144 #define XT_OPTION_OFFSET_SCALE 256
146 #define MIN_ALIGN (__alignof__(struct ipt_entry))
148 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
150 struct error_target {
151 struct xt_entry_target t;
152 char error[IPT_TABLE_MAXNAMELEN];
155 struct connman_iptables_entry {
159 struct ipt_entry *entry;
162 struct connman_iptables {
166 struct ipt_getinfo *info;
167 struct ipt_get_entries *blob_entries;
169 unsigned int num_entries;
170 unsigned int old_entries;
173 unsigned int underflow[NF_INET_NUMHOOKS];
174 unsigned int hook_entry[NF_INET_NUMHOOKS];
179 static GHashTable *table_hash = NULL;
180 static gboolean debug_enabled = FALSE;
182 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
183 unsigned int hook,size_t size,
184 unsigned int offset, void *user_data);
186 static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
190 if (*valid_hooks == 0)
191 return NF_INET_NUMHOOKS;
193 h = __builtin_ffs(*valid_hooks) - 1;
194 *valid_hooks ^= (1 << h);
199 static int iterate_entries(struct ipt_entry *entries,
200 unsigned int valid_hooks,
201 unsigned int *hook_entry,
202 unsigned int *underflow,
203 size_t size, iterate_entries_cb_t cb,
206 unsigned int offset, h, hook;
208 struct ipt_entry *entry;
210 h = next_hook_entry_index(&valid_hooks);
213 for (offset = 0, entry = entries; offset < size;
214 offset += entry->next_offset) {
216 entry = (void *)entries + offset;
219 * Updating builtin, hook and h is very tricky.
221 * - builtin is only set to the current hook number
222 * if the current entry is the hook entry (aka chain
223 * head). And only for builtin chains, never for
225 * - hook is the current hook number. If we
226 * look at user chains it needs to be NF_INET_NETNUMHOOKS.
227 * - h is the next hook entry. Thous we need to be carefully
228 * not to access the table when h is NF_INET_NETNUMHOOKS.
230 if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
235 if (h == NF_INET_NUMHOOKS)
238 if (h < NF_INET_NUMHOOKS && underflow[h] <= offset) {
239 h = next_hook_entry_index(&valid_hooks);
242 err = cb(entry, builtin, hook, size, offset, user_data);
250 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
251 size_t size, unsigned int offset,
254 iterate_entries_cb_t cb = user_data;
256 DBG("entry %p hook %d offset %d size %d", entry, hook,
257 offset, entry->next_offset);
259 return cb(entry, builtin, hook, size, offset, NULL);
262 static int target_to_verdict(const char *target_name)
264 if (!g_strcmp0(target_name, LABEL_ACCEPT))
265 return -NF_ACCEPT - 1;
267 if (!g_strcmp0(target_name, LABEL_DROP))
270 if (!g_strcmp0(target_name, LABEL_QUEUE))
271 return -NF_QUEUE - 1;
273 if (!g_strcmp0(target_name, LABEL_RETURN))
279 static gboolean is_builtin_target(const char *target_name)
281 if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
282 !g_strcmp0(target_name, LABEL_DROP) ||
283 !g_strcmp0(target_name, LABEL_QUEUE) ||
284 !g_strcmp0(target_name, LABEL_RETURN))
290 static gboolean is_jump(struct connman_iptables_entry *e)
292 struct xt_entry_target *target;
294 target = ipt_get_target(e->entry);
296 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
297 struct xt_standard_target *t;
299 t = (struct xt_standard_target *)target;
301 switch (t->verdict) {
317 static gboolean is_fallthrough(struct connman_iptables_entry *e)
319 struct xt_entry_target *target;
321 target = ipt_get_target(e->entry);
322 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
323 struct xt_standard_target *t;
325 t = (struct xt_standard_target *)target;
332 static gboolean is_chain(struct connman_iptables *table,
333 struct connman_iptables_entry *e)
335 struct ipt_entry *entry;
336 struct xt_entry_target *target;
342 target = ipt_get_target(entry);
343 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
349 static GList *find_chain_head(struct connman_iptables *table,
350 const char *chain_name)
353 struct connman_iptables_entry *head;
354 struct ipt_entry *entry;
355 struct xt_entry_target *target;
358 for (list = table->entries; list; list = list->next) {
363 builtin = head->builtin;
364 if (builtin >= 0 && !g_strcmp0(hooknames[builtin], chain_name))
367 /* User defined chain */
368 target = ipt_get_target(entry);
369 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET) &&
370 !g_strcmp0((char *)target->data, chain_name))
377 static GList *find_chain_tail(struct connman_iptables *table,
378 const char *chain_name)
380 struct connman_iptables_entry *tail;
381 GList *chain_head, *list;
383 chain_head = find_chain_head(table, chain_name);
384 if (chain_head == NULL)
387 /* Then we look for the next chain */
388 for (list = chain_head->next; list; list = list->next) {
391 if (is_chain(table, tail))
395 /* Nothing found, we return the table end */
396 return g_list_last(table->entries);
400 static void update_offsets(struct connman_iptables *table)
403 struct connman_iptables_entry *entry, *prev_entry;
405 for (list = table->entries; list; list = list->next) {
408 if (list == table->entries) {
415 prev_entry = prev->data;
417 entry->offset = prev_entry->offset +
418 prev_entry->entry->next_offset;
422 static void update_targets_reference(struct connman_iptables *table,
423 struct connman_iptables_entry *entry_before,
424 struct connman_iptables_entry *modified_entry,
425 gboolean is_removing)
427 struct connman_iptables_entry *tmp;
428 struct xt_standard_target *t;
432 offset = modified_entry->entry->next_offset;
434 for (list = table->entries; list; list = list->next) {
440 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
442 if (is_removing == TRUE) {
443 if (t->verdict >= entry_before->offset)
444 t->verdict -= offset;
446 if (t->verdict > entry_before->offset)
447 t->verdict += offset;
451 if (is_fallthrough(modified_entry)) {
452 t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
454 t->verdict = entry_before->offset +
455 modified_entry->entry->target_offset +
456 ALIGN(sizeof(struct xt_standard_target));
457 t->target.u.target_size =
458 ALIGN(sizeof(struct xt_standard_target));
462 static int iptables_add_entry(struct connman_iptables *table,
463 struct ipt_entry *entry, GList *before,
466 struct connman_iptables_entry *e, *entry_before;
471 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
476 e->builtin = builtin;
478 table->entries = g_list_insert_before(table->entries, before, e);
479 table->num_entries++;
480 table->size += entry->next_offset;
482 if (before == NULL) {
483 e->offset = table->size - entry->next_offset;
488 entry_before = before->data;
491 * We've just appended/insterted a new entry. All references
492 * should be bumped accordingly.
494 update_targets_reference(table, entry_before, e, FALSE);
496 update_offsets(table);
501 static int remove_table_entry(struct connman_iptables *table,
502 struct connman_iptables_entry *entry)
506 table->num_entries--;
507 table->size -= entry->entry->next_offset;
508 removed = entry->entry->next_offset;
510 table->entries = g_list_remove(table->entries, entry);
512 g_free(entry->entry);
518 static void delete_update_hooks(struct connman_iptables *table,
519 int builtin, GList *chain_head,
522 struct connman_iptables_entry *e;
525 e = chain_head->data;
526 e->builtin = builtin;
528 table->underflow[builtin] -= removed;
530 for (list = chain_head->next; list; list = list->next) {
536 table->hook_entry[e->builtin] -= removed;
537 table->underflow[e->builtin] -= removed;
541 static int iptables_flush_chain(struct connman_iptables *table,
544 GList *chain_head, *chain_tail, *list, *next;
545 struct connman_iptables_entry *entry;
546 int builtin, removed = 0;
548 DBG("table %s chain %s", table->name, name);
550 chain_head = find_chain_head(table, name);
551 if (chain_head == NULL)
554 chain_tail = find_chain_tail(table, name);
555 if (chain_tail == NULL)
558 entry = chain_head->data;
559 builtin = entry->builtin;
564 list = chain_head->next;
566 if (list == chain_tail->prev)
569 while (list != chain_tail->prev) {
571 next = g_list_next(list);
573 removed += remove_table_entry(table, entry);
579 delete_update_hooks(table, builtin, chain_tail->prev, removed);
581 update_offsets(table);
586 static int iptables_add_chain(struct connman_iptables *table,
590 struct ipt_entry *entry_head;
591 struct ipt_entry *entry_return;
592 struct error_target *error;
593 struct ipt_standard_target *standard;
594 u_int16_t entry_head_size, entry_return_size;
596 DBG("table %s chain %s", table->name, name);
598 last = g_list_last(table->entries);
601 * An empty chain is composed of:
602 * - A head entry, with no match and an error target.
603 * The error target data is the chain name.
604 * - A tail entry, with no match and a standard target.
605 * The standard target verdict is XT_RETURN (return to the
610 entry_head_size = sizeof(struct ipt_entry) +
611 sizeof(struct error_target);
612 entry_head = g_try_malloc0(entry_head_size);
613 if (entry_head == NULL)
616 entry_head->target_offset = sizeof(struct ipt_entry);
617 entry_head->next_offset = entry_head_size;
619 error = (struct error_target *) entry_head->elems;
620 g_stpcpy(error->t.u.user.name, IPT_ERROR_TARGET);
621 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
622 g_stpcpy(error->error, name);
624 if (iptables_add_entry(table, entry_head, last, -1) < 0)
628 entry_return_size = sizeof(struct ipt_entry) +
629 sizeof(struct ipt_standard_target);
630 entry_return = g_try_malloc0(entry_return_size);
631 if (entry_return == NULL)
634 entry_return->target_offset = sizeof(struct ipt_entry);
635 entry_return->next_offset = entry_return_size;
637 standard = (struct ipt_standard_target *) entry_return->elems;
638 standard->target.u.user.target_size =
639 ALIGN(sizeof(struct ipt_standard_target));
640 standard->verdict = XT_RETURN;
642 if (iptables_add_entry(table, entry_return, last, -1) < 0)
648 g_free(entry_return);
655 static int iptables_delete_chain(struct connman_iptables *table,
658 struct connman_iptables_entry *entry;
659 GList *chain_head, *chain_tail;
661 DBG("table %s chain %s", table->name, name);
663 chain_head = find_chain_head(table, name);
664 if (chain_head == NULL)
667 entry = chain_head->data;
669 /* We cannot remove builtin chain */
670 if (entry->builtin >= 0)
673 chain_tail = find_chain_tail(table, name);
674 if (chain_tail == NULL)
677 /* Chain must be flushed */
678 if (chain_head->next != chain_tail->prev)
681 remove_table_entry(table, entry);
683 entry = chain_tail->prev->data;
684 remove_table_entry(table, entry);
686 update_offsets(table);
691 static struct ipt_entry *new_rule(struct ipt_ip *ip,
692 const char *target_name, struct xtables_target *xt_t,
693 struct xtables_rule_match *xt_rm)
695 struct xtables_rule_match *tmp_xt_rm;
696 struct ipt_entry *new_entry;
697 size_t match_size, target_size;
700 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
701 match_size += tmp_xt_rm->match->m->u.match_size;
704 target_size = ALIGN(xt_t->t->u.target_size);
706 target_size = ALIGN(sizeof(struct xt_standard_target));
708 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
710 if (new_entry == NULL)
713 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
715 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
716 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
720 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
721 tmp_xt_rm = tmp_xt_rm->next) {
722 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
723 tmp_xt_rm->match->m->u.match_size);
724 match_size += tmp_xt_rm->match->m->u.match_size;
728 struct xt_entry_target *entry_target;
730 entry_target = ipt_get_target(new_entry);
731 memcpy(entry_target, xt_t->t, target_size);
737 static void update_hooks(struct connman_iptables *table, GList *chain_head,
738 struct ipt_entry *entry)
741 struct connman_iptables_entry *head, *e;
744 if (chain_head == NULL)
747 head = chain_head->data;
749 builtin = head->builtin;
753 table->underflow[builtin] += entry->next_offset;
755 for (list = chain_head->next; list; list = list->next) {
758 builtin = e->builtin;
762 table->hook_entry[builtin] += entry->next_offset;
763 table->underflow[builtin] += entry->next_offset;
767 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
768 struct ipt_ip *ip, const char *chain_name,
769 const char *target_name,
770 struct xtables_target *xt_t,
771 int *builtin, struct xtables_rule_match *xt_rm,
772 connman_bool_t insert)
774 GList *chain_tail, *chain_head;
775 struct ipt_entry *new_entry;
776 struct connman_iptables_entry *head;
778 chain_head = find_chain_head(table, chain_name);
779 if (chain_head == NULL)
782 chain_tail = find_chain_tail(table, chain_name);
783 if (chain_tail == NULL)
786 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
787 if (new_entry == NULL)
790 update_hooks(table, chain_head, new_entry);
793 * If the chain is builtin, and does not have any rule,
794 * then the one that we're inserting is becoming the head
795 * and thus needs the builtin flag.
797 head = chain_head->data;
798 if (head->builtin < 0)
800 else if (insert == TRUE || chain_head == chain_tail->prev) {
801 *builtin = head->builtin;
808 static int iptables_append_rule(struct connman_iptables *table,
809 struct ipt_ip *ip, const char *chain_name,
810 const char *target_name,
811 struct xtables_target *xt_t,
812 struct xtables_rule_match *xt_rm)
814 struct ipt_entry *new_entry;
815 int builtin = -1, ret;
818 DBG("table %s chain %s", table->name, chain_name);
820 chain_tail = find_chain_tail(table, chain_name);
821 if (chain_tail == NULL)
824 new_entry = prepare_rule_inclusion(table, ip, chain_name,
825 target_name, xt_t, &builtin, xt_rm, FALSE);
826 if (new_entry == NULL)
829 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
836 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
837 struct ipt_entry *i_e2)
839 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
842 if (i_e1->target_offset != i_e2->target_offset)
845 if (i_e1->next_offset != i_e2->next_offset)
851 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
852 struct xt_entry_target *xt_e_t2)
856 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
859 if (g_strcmp0(xt_e_t1->u.user.name, "") == 0 &&
860 g_strcmp0(xt_e_t2->u.user.name, "") == 0) {
863 } else if (g_strcmp0(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
864 struct xt_standard_target *xt_s_t1;
865 struct xt_standard_target *xt_s_t2;
867 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
868 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
870 if (xt_s_t1->verdict != xt_s_t2->verdict)
873 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
876 if (g_strcmp0(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
879 for (i = 0; i < xt_e_t1->u.target_size -
880 sizeof(struct xt_standard_target); i++) {
881 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
889 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
890 struct xt_entry_match *xt_e_m2)
894 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
897 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
900 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
903 if (g_strcmp0(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
906 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
908 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
915 static GList *find_existing_rule(struct connman_iptables *table,
916 struct ipt_ip *ip, const char *chain_name,
917 const char *target_name,
918 struct xtables_target *xt_t,
919 struct xtables_match *xt_m,
920 struct xtables_rule_match *xt_rm)
922 GList *chain_tail, *chain_head, *list;
923 struct xt_entry_target *xt_e_t = NULL;
924 struct xt_entry_match *xt_e_m = NULL;
925 struct connman_iptables_entry *entry;
926 struct ipt_entry *entry_test;
929 chain_head = find_chain_head(table, chain_name);
930 if (chain_head == NULL)
933 chain_tail = find_chain_tail(table, chain_name);
934 if (chain_tail == NULL)
940 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
941 if (entry_test == NULL)
945 xt_e_t = ipt_get_target(entry_test);
947 xt_e_m = (struct xt_entry_match *)entry_test->elems;
949 entry = chain_head->data;
950 builtin = entry->builtin;
955 list = chain_head->next;
957 for (; list != chain_tail->prev; list = list->next) {
958 struct connman_iptables_entry *tmp;
959 struct ipt_entry *tmp_e;
964 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
968 struct xt_entry_target *tmp_xt_e_t;
970 tmp_xt_e_t = ipt_get_target(tmp_e);
972 if (!is_same_target(tmp_xt_e_t, xt_e_t))
977 struct xt_entry_match *tmp_xt_e_m;
979 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
981 if (!is_same_match(tmp_xt_e_m, xt_e_m))
990 if (list != chain_tail->prev)
996 static int iptables_delete_rule(struct connman_iptables *table,
997 struct ipt_ip *ip, const char *chain_name,
998 const char *target_name,
999 struct xtables_target *xt_t,
1000 struct xtables_match *xt_m,
1001 struct xtables_rule_match *xt_rm)
1003 struct connman_iptables_entry *entry;
1004 GList *chain_head, *chain_tail, *list;
1005 int builtin, removed;
1007 DBG("table %s chain %s", table->name, chain_name);
1011 chain_head = find_chain_head(table, chain_name);
1012 if (chain_head == NULL)
1015 chain_tail = find_chain_tail(table, chain_name);
1016 if (chain_tail == NULL)
1019 list = find_existing_rule(table, ip, chain_name, target_name,
1024 entry = chain_head->data;
1025 builtin = entry->builtin;
1027 if (builtin >= 0 && list == chain_head) {
1029 * We are about to remove the first rule in the
1030 * chain. In this case we need to store the builtin
1031 * value to the new chain_head.
1033 * Note, for builtin chains, chain_head->next is
1034 * always valid. A builtin chain has always a policy
1037 chain_head = chain_head->next;
1039 entry = chain_head->data;
1040 entry->builtin = builtin;
1047 /* We have deleted a rule,
1048 * all references should be bumped accordingly */
1049 if (list->next != NULL)
1050 update_targets_reference(table, list->next->data,
1053 removed += remove_table_entry(table, entry);
1056 delete_update_hooks(table, builtin, chain_head, removed);
1058 update_offsets(table);
1063 static int iptables_change_policy(struct connman_iptables *table,
1064 const char *chain_name, const char *policy)
1066 GList *chain_head, *chain_tail;
1067 struct connman_iptables_entry *entry;
1068 struct xt_entry_target *target;
1069 struct xt_standard_target *t;
1072 DBG("table %s chain %s policy %s", table->name, chain_name, policy);
1074 verdict = target_to_verdict(policy);
1076 case -NF_ACCEPT - 1:
1083 chain_head = find_chain_head(table, chain_name);
1084 if (chain_head == NULL)
1087 entry = chain_head->data;
1088 if (entry->builtin < 0)
1091 chain_tail = find_chain_tail(table, chain_name);
1092 if (chain_tail == NULL)
1095 entry = chain_tail->prev->data;
1096 target = ipt_get_target(entry->entry);
1098 t = (struct xt_standard_target *)target;
1099 t->verdict = verdict;
1104 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1106 struct ipt_replace *r;
1108 struct connman_iptables_entry *e;
1109 unsigned char *entry_index;
1111 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1115 memset(r, 0, sizeof(*r) + table->size);
1117 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1118 * table->old_entries);
1119 if (r->counters == NULL) {
1124 g_stpcpy(r->name, table->info->name);
1125 r->num_entries = table->num_entries;
1126 r->size = table->size;
1128 r->num_counters = table->old_entries;
1129 r->valid_hooks = table->info->valid_hooks;
1131 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1132 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1134 entry_index = (unsigned char *)r->entries;
1135 for (list = table->entries; list; list = list->next) {
1138 memcpy(entry_index, e->entry, e->entry->next_offset);
1139 entry_index += e->entry->next_offset;
1145 static void dump_ip(struct ipt_entry *entry)
1147 struct ipt_ip *ip = &entry->ip;
1148 char ip_string[INET6_ADDRSTRLEN];
1149 char ip_mask[INET6_ADDRSTRLEN];
1151 if (strlen(ip->iniface))
1152 DBG("\tin %s", ip->iniface);
1154 if (strlen(ip->outiface))
1155 DBG("\tout %s", ip->outiface);
1157 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1158 inet_ntop(AF_INET, &ip->smsk,
1159 ip_mask, INET6_ADDRSTRLEN) != NULL)
1160 DBG("\tsrc %s/%s", ip_string, ip_mask);
1162 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1163 inet_ntop(AF_INET, &ip->dmsk,
1164 ip_mask, INET6_ADDRSTRLEN) != NULL)
1165 DBG("\tdst %s/%s", ip_string, ip_mask);
1168 static void dump_target(struct ipt_entry *entry)
1171 struct xtables_target *xt_t;
1172 struct xt_entry_target *target;
1174 target = ipt_get_target(entry);
1176 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
1177 struct xt_standard_target *t;
1179 t = (struct xt_standard_target *)target;
1181 switch (t->verdict) {
1183 DBG("\ttarget RETURN");
1186 case -NF_ACCEPT - 1:
1187 DBG("\ttarget ACCEPT");
1191 DBG("\ttarget DROP");
1195 DBG("\ttarget QUEUE");
1199 DBG("\ttarget STOP");
1203 DBG("\tJUMP %u", t->verdict);
1207 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1208 XTF_LOAD_MUST_SUCCEED);
1210 if(xt_t->print != NULL)
1211 xt_t->print(NULL, target, 1);
1213 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1215 DBG("\ttarget %s", target->u.user.name);
1219 if(xt_t->print != NULL) {
1221 xt_t->print(NULL, target, 1);
1226 static void dump_match(struct ipt_entry *entry)
1228 struct xtables_match *xt_m;
1229 struct xt_entry_match *match;
1231 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1234 match = (struct xt_entry_match *) entry->elems;
1236 if (!strlen(match->u.user.name))
1239 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1243 if(xt_m->print != NULL) {
1245 xt_m->print(NULL, match, 1);
1251 DBG("\tmatch %s", match->u.user.name);
1255 static int dump_entry(struct ipt_entry *entry, int builtin,
1256 unsigned int hook, size_t size, unsigned int offset,
1259 struct xt_entry_target *target;
1261 target = ipt_get_target(entry);
1263 if (offset + entry->next_offset == size) {
1264 DBG("\tEnd of CHAIN");
1268 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET)) {
1269 DBG("\tUSER CHAIN (%s) match %p target %p",
1270 target->data, entry->elems,
1271 (char *)entry + entry->target_offset);
1274 } else if (builtin >= 0) {
1275 DBG("\tCHAIN (%s) match %p target %p",
1276 hooknames[builtin], entry->elems,
1277 (char *)entry + entry->target_offset);
1279 DBG("\tRULE match %p target %p",
1281 (char *)entry + entry->target_offset);
1291 static void dump_table(struct connman_iptables *table)
1293 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1295 table->info->valid_hooks, table->info->num_entries,
1298 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1299 table->info->hook_entry[NF_IP_PRE_ROUTING],
1300 table->info->hook_entry[NF_IP_LOCAL_IN],
1301 table->info->hook_entry[NF_IP_FORWARD],
1302 table->info->hook_entry[NF_IP_LOCAL_OUT],
1303 table->info->hook_entry[NF_IP_POST_ROUTING]);
1304 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1305 table->info->underflow[NF_IP_PRE_ROUTING],
1306 table->info->underflow[NF_IP_LOCAL_IN],
1307 table->info->underflow[NF_IP_FORWARD],
1308 table->info->underflow[NF_IP_LOCAL_OUT],
1309 table->info->underflow[NF_IP_POST_ROUTING]);
1311 iterate_entries(table->blob_entries->entrytable,
1312 table->info->valid_hooks,
1313 table->info->hook_entry,
1314 table->info->underflow,
1315 table->blob_entries->size,
1316 print_entry, dump_entry);
1319 static void dump_ipt_replace(struct ipt_replace *repl)
1321 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1322 repl->name, repl->valid_hooks, repl->num_entries,
1325 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1326 repl->hook_entry[NF_IP_PRE_ROUTING],
1327 repl->hook_entry[NF_IP_LOCAL_IN],
1328 repl->hook_entry[NF_IP_FORWARD],
1329 repl->hook_entry[NF_IP_LOCAL_OUT],
1330 repl->hook_entry[NF_IP_POST_ROUTING]);
1331 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1332 repl->underflow[NF_IP_PRE_ROUTING],
1333 repl->underflow[NF_IP_LOCAL_IN],
1334 repl->underflow[NF_IP_FORWARD],
1335 repl->underflow[NF_IP_LOCAL_OUT],
1336 repl->underflow[NF_IP_POST_ROUTING]);
1338 iterate_entries(repl->entries, repl->valid_hooks,
1339 repl->hook_entry, repl->underflow,
1340 repl->size, print_entry, dump_entry);
1343 static int iptables_get_entries(struct connman_iptables *table)
1345 socklen_t entry_size;
1347 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1349 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1350 table->blob_entries, &entry_size);
1353 static int iptables_replace(struct connman_iptables *table,
1354 struct ipt_replace *r)
1356 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1357 sizeof(*r) + r->size);
1360 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1361 size_t size, unsigned offset, void *user_data)
1363 struct connman_iptables *table = user_data;
1364 struct ipt_entry *new_entry;
1366 new_entry = g_try_malloc0(entry->next_offset);
1367 if (new_entry == NULL)
1370 memcpy(new_entry, entry, entry->next_offset);
1372 return iptables_add_entry(table, new_entry, NULL, builtin);
1375 static void table_cleanup(struct connman_iptables *table)
1378 struct connman_iptables_entry *entry;
1383 if (table->ipt_sock >= 0)
1384 close(table->ipt_sock);
1386 for (list = table->entries; list; list = list->next) {
1389 g_free(entry->entry);
1393 g_list_free(table->entries);
1394 g_free(table->name);
1395 g_free(table->info);
1396 g_free(table->blob_entries);
1400 static struct connman_iptables *iptables_init(const char *table_name)
1402 struct connman_iptables *table = NULL;
1403 char *module = NULL;
1406 DBG("%s", table_name);
1408 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1409 DBG("ip_tables module loading gives error but trying anyway");
1411 module = g_strconcat("iptable_", table_name, NULL);
1415 if (xtables_insmod(module, NULL, TRUE) != 0)
1416 DBG("%s module loading gives error but trying anyway", module);
1420 table = g_try_new0(struct connman_iptables, 1);
1424 table->info = g_try_new0(struct ipt_getinfo, 1);
1425 if (table->info == NULL)
1428 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1429 if (table->ipt_sock < 0)
1432 s = sizeof(*table->info);
1433 g_stpcpy(table->info->name, table_name);
1434 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1435 table->info, &s) < 0) {
1436 connman_error("iptables support missing error %d (%s)", errno,
1441 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1443 if (table->blob_entries == NULL)
1446 g_stpcpy(table->blob_entries->name, table_name);
1447 table->blob_entries->size = table->info->size;
1449 if (iptables_get_entries(table) < 0)
1452 table->num_entries = 0;
1453 table->old_entries = table->info->num_entries;
1456 memcpy(table->underflow, table->info->underflow,
1457 sizeof(table->info->underflow));
1458 memcpy(table->hook_entry, table->info->hook_entry,
1459 sizeof(table->info->hook_entry));
1461 iterate_entries(table->blob_entries->entrytable,
1462 table->info->valid_hooks, table->info->hook_entry,
1463 table->info->underflow, table->blob_entries->size,
1466 if (debug_enabled == TRUE)
1472 table_cleanup(table);
1477 static struct option iptables_opts[] = {
1478 {.name = "append", .has_arg = 1, .val = 'A'},
1479 {.name = "compare", .has_arg = 1, .val = 'C'},
1480 {.name = "delete", .has_arg = 1, .val = 'D'},
1481 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1482 {.name = "insert", .has_arg = 1, .val = 'I'},
1483 {.name = "list", .has_arg = 2, .val = 'L'},
1484 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1485 {.name = "policy", .has_arg = 1, .val = 'P'},
1486 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1487 {.name = "destination", .has_arg = 1, .val = 'd'},
1488 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1489 {.name = "jump", .has_arg = 1, .val = 'j'},
1490 {.name = "match", .has_arg = 1, .val = 'm'},
1491 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1492 {.name = "source", .has_arg = 1, .val = 's'},
1493 {.name = "table", .has_arg = 1, .val = 't'},
1497 struct xtables_globals iptables_globals = {
1499 .opts = iptables_opts,
1500 .orig_opts = iptables_opts,
1503 static struct xtables_target *prepare_target(struct connman_iptables *table,
1504 const char *target_name)
1506 struct xtables_target *xt_t = NULL;
1507 gboolean is_builtin, is_user_defined;
1508 GList *chain_head = NULL;
1512 is_user_defined = FALSE;
1514 if (is_builtin_target(target_name))
1517 chain_head = find_chain_head(table, target_name);
1518 if (chain_head != NULL && chain_head->next != NULL)
1519 is_user_defined = TRUE;
1522 if (is_builtin || is_user_defined)
1523 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1524 XTF_LOAD_MUST_SUCCEED);
1526 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1531 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1533 xt_t->t = g_try_malloc0(target_size);
1534 if (xt_t->t == NULL)
1537 xt_t->t->u.target_size = target_size;
1539 if (is_builtin || is_user_defined) {
1540 struct xt_standard_target *target;
1542 target = (struct xt_standard_target *)(xt_t->t);
1543 g_stpcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1545 if (is_builtin == TRUE)
1546 target->verdict = target_to_verdict(target_name);
1547 else if (is_user_defined == TRUE) {
1548 struct connman_iptables_entry *target_rule;
1550 if (chain_head == NULL) {
1555 target_rule = chain_head->next->data;
1556 target->verdict = target_rule->offset;
1559 g_stpcpy(xt_t->t->u.user.name, target_name);
1560 xt_t->t->u.user.revision = xt_t->revision;
1561 if (xt_t->init != NULL)
1562 xt_t->init(xt_t->t);
1565 if (xt_t->x6_options != NULL)
1566 iptables_globals.opts =
1567 xtables_options_xfrm(
1568 iptables_globals.orig_opts,
1569 iptables_globals.opts,
1571 &xt_t->option_offset);
1573 iptables_globals.opts =
1574 xtables_merge_options(
1575 iptables_globals.orig_opts,
1576 iptables_globals.opts,
1578 &xt_t->option_offset);
1580 if (iptables_globals.opts == NULL) {
1588 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1589 struct xtables_rule_match **xt_rm,
1590 const char *match_name)
1592 struct xtables_match *xt_m;
1595 if (match_name == NULL)
1598 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1599 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1601 xt_m->m = g_try_malloc0(match_size);
1602 if (xt_m->m == NULL)
1605 xt_m->m->u.match_size = match_size;
1606 g_stpcpy(xt_m->m->u.user.name, xt_m->name);
1607 xt_m->m->u.user.revision = xt_m->revision;
1609 if (xt_m->init != NULL)
1610 xt_m->init(xt_m->m);
1612 if (xt_m->x6_options != NULL)
1613 iptables_globals.opts =
1614 xtables_options_xfrm(
1615 iptables_globals.orig_opts,
1616 iptables_globals.opts,
1618 &xt_m->option_offset);
1620 iptables_globals.opts =
1621 xtables_merge_options(
1622 iptables_globals.orig_opts,
1623 iptables_globals.opts,
1625 &xt_m->option_offset);
1627 if (iptables_globals.opts == NULL) {
1635 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1638 uint32_t prefixlength;
1642 tokens = g_strsplit(str, "/", 2);
1646 if (!inet_pton(AF_INET, tokens[0], ip)) {
1651 if (tokens[1] != NULL) {
1652 prefixlength = strtol(tokens[1], NULL, 10);
1653 if (prefixlength > 31) {
1658 tmp = ~(0xffffffff >> prefixlength);
1663 mask->s_addr = htonl(tmp);
1664 ip->s_addr = ip->s_addr & mask->s_addr;
1672 static struct connman_iptables *get_table(const char *table_name)
1674 struct connman_iptables *table;
1676 if (table_name == NULL)
1677 table_name = "filter";
1679 table = g_hash_table_lookup(table_hash, table_name);
1683 table = iptables_init(table_name);
1687 table->name = g_strdup(table_name);
1688 g_hash_table_replace(table_hash, table->name, table);
1693 struct parse_context {
1697 struct xtables_target *xt_t;
1698 struct xtables_match *xt_m;
1699 struct xtables_rule_match *xt_rm;
1702 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1707 tokens = g_strsplit_set(str, " ", -1);
1709 i = g_strv_length(tokens);
1711 /* Add space for the argv[0] value */
1714 /* Don't forget the last NULL entry */
1715 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1716 if (ctx->argv == NULL) {
1722 * getopt_long() jumps over the first token; we need to add some
1723 * random argv[0] entry.
1725 ctx->argv[0] = g_strdup("argh");
1726 for (i = 1; i < ctx->argc; i++)
1727 ctx->argv[i] = tokens[i - 1];
1734 static int parse_xt_modules(int c, connman_bool_t invert,
1735 struct parse_context *ctx)
1737 struct xtables_match *m;
1738 struct xtables_rule_match *rm;
1740 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1741 if (rm->completed != 0)
1746 if (m->x6_parse == NULL && m->parse == NULL)
1749 if (c < (int) m->option_offset ||
1750 c >= (int) m->option_offset
1751 + XT_OPTION_OFFSET_SCALE)
1754 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1757 if (ctx->xt_t == NULL)
1760 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1763 if (c < (int) ctx->xt_t->option_offset ||
1764 c >= (int) ctx->xt_t->option_offset
1765 + XT_OPTION_OFFSET_SCALE)
1768 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1773 static int final_check_xt_modules(struct parse_context *ctx)
1775 struct xtables_rule_match *rm;
1777 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1778 xtables_option_mfcall(rm->match);
1780 if (ctx->xt_t != NULL)
1781 xtables_option_tfcall(ctx->xt_t);
1786 static int parse_rule_spec(struct connman_iptables *table,
1787 struct parse_context *ctx)
1790 * How the parser works:
1792 * - If getopt finds 's', 'd', 'i', 'o'.
1793 * just extract the information.
1794 * - if '!' is found, set the invert flag to true and
1795 * removes the '!' from the optarg string and jumps
1796 * back to getopt to reparse the current optarg string.
1797 * After reparsing the invert flag is reseted to false.
1798 * - If 'm' or 'j' is found then call either
1799 * prepare_matches() or prepare_target(). Those function
1800 * will modify (extend) the longopts for getopt_long.
1801 * That means getopt will change its matching context according
1802 * the loaded target.
1804 * Here an example with iptables-test
1806 * argv[0] = ./tools/iptables-test
1818 * getopt found 'm' then the optarg is "mark" and optind 7
1819 * The longopts array containts before hitting the `case 'm'`
1821 * val A has_arg 1 name append
1822 * val C has_arg 1 name compare
1823 * val D has_arg 1 name delete
1824 * val F has_arg 1 name flush-chain
1825 * val I has_arg 1 name insert
1826 * val L has_arg 2 name list
1827 * val N has_arg 1 name new-chain
1828 * val P has_arg 1 name policy
1829 * val X has_arg 1 name delete-chain
1830 * val d has_arg 1 name destination
1831 * val i has_arg 1 name in-interface
1832 * val j has_arg 1 name jump
1833 * val m has_arg 1 name match
1834 * val o has_arg 1 name out-interface
1835 * val s has_arg 1 name source
1836 * val t has_arg 1 name table
1838 * After executing the `case 'm'` block longopts is
1840 * val A has_arg 1 name append
1841 * val C has_arg 1 name compare
1842 * val D has_arg 1 name delete
1843 * val F has_arg 1 name flush-chain
1844 * val I has_arg 1 name insert
1845 * val L has_arg 2 name list
1846 * val N has_arg 1 name new-chain
1847 * val P has_arg 1 name policy
1848 * val X has_arg 1 name delete-chain
1849 * val d has_arg 1 name destination
1850 * val i has_arg 1 name in-interface
1851 * val j has_arg 1 name jump
1852 * val m has_arg 1 name match
1853 * val o has_arg 1 name out-interface
1854 * val s has_arg 1 name source
1855 * val t has_arg 1 name table
1856 * val has_arg 1 name mark
1858 * So the 'mark' matcher has added the 'mark' options
1859 * and getopt will then return c '256' optarg "999" optind 9
1860 * And we will hit the 'default' statement which then
1861 * will call the matchers parser (xt_m->parser() or
1862 * xtables_option_mpcall() depending on which version
1863 * of libxtables is found.
1865 connman_bool_t invert = FALSE;
1868 ctx->ip = g_try_new0(struct ipt_ip, 1);
1869 if (ctx->ip == NULL)
1873 * Tell getopt_long not to generate error messages for unknown
1874 * options and also reset optind back to 0.
1879 while ((c = getopt_long(ctx->argc, ctx->argv,
1881 iptables_globals.opts, NULL)) != -1) {
1884 /* Source specification */
1885 if (!parse_ip_and_mask(optarg,
1891 ctx->ip->invflags |= IPT_INV_SRCIP;
1895 /* Destination specification */
1896 if (!parse_ip_and_mask(optarg,
1902 ctx->ip->invflags |= IPT_INV_DSTIP;
1905 /* In interface specification */
1906 len = strlen(optarg);
1908 if (len + 1 > IFNAMSIZ)
1911 g_stpcpy(ctx->ip->iniface, optarg);
1912 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1915 ctx->ip->invflags |= IPT_INV_VIA_IN;
1919 /* Out interface specification */
1920 len = strlen(optarg);
1922 if (len + 1 > IFNAMSIZ)
1925 g_stpcpy(ctx->ip->outiface, optarg);
1926 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1929 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1934 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1935 if (ctx->xt_m == NULL) {
1943 ctx->xt_t = prepare_target(table, optarg);
1944 if (ctx->xt_t == NULL) {
1951 if (optarg[0] == '!' && optarg[1] == '\0') {
1954 /* Remove the '!' from the optarg */
1958 * And recall getopt_long without reseting
1966 err = parse_xt_modules(c, invert, ctx);
1976 err = final_check_xt_modules(ctx);
1982 static void reset_xtables(void)
1984 struct xtables_match *xt_m;
1985 struct xtables_target *xt_t;
1988 * As side effect parsing a rule sets some global flags
1989 * which will be evaluated/verified. Let's reset them
1990 * to ensure we can parse more than one rule.
1992 * Clear all flags because the flags are only valid
1995 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1998 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
2004 * We need also to free the memory implicitly allocated
2005 * during parsing (see xtables_options_xfrm()).
2006 * Note xt_params is actually iptables_globals.
2008 if (xt_params->opts != xt_params->orig_opts) {
2009 g_free(xt_params->opts);
2010 xt_params->opts = xt_params->orig_opts;
2012 xt_params->option_offset = 0;
2015 static void cleanup_parse_context(struct parse_context *ctx)
2017 struct xtables_rule_match *rm, *tmp;
2019 g_strfreev(ctx->argv);
2021 if (ctx->xt_t != NULL) {
2022 g_free(ctx->xt_t->t);
2023 ctx->xt_t->t = NULL;
2025 if (ctx->xt_m != NULL) {
2026 g_free(ctx->xt_m->m);
2027 ctx->xt_m->m = NULL;
2029 for (tmp = NULL, rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
2039 int __connman_iptables_dump(const char *table_name)
2041 struct connman_iptables *table;
2043 DBG("-t %s -L", table_name);
2045 table = get_table(table_name);
2054 int __connman_iptables_new_chain(const char *table_name,
2057 struct connman_iptables *table;
2059 DBG("-t %s -N %s", table_name, chain);
2061 table = get_table(table_name);
2065 return iptables_add_chain(table, chain);
2068 int __connman_iptables_delete_chain(const char *table_name,
2071 struct connman_iptables *table;
2073 DBG("-t %s -X %s", table_name, chain);
2075 table = get_table(table_name);
2079 return iptables_delete_chain(table, chain);
2082 int __connman_iptables_flush_chain(const char *table_name,
2085 struct connman_iptables *table;
2087 DBG("-t %s -F %s", table_name, chain);
2089 table = get_table(table_name);
2093 return iptables_flush_chain(table, chain);
2096 int __connman_iptables_change_policy(const char *table_name,
2100 struct connman_iptables *table;
2102 DBG("-t %s -F %s", table_name, chain);
2104 table = get_table(table_name);
2108 return iptables_change_policy(table, chain, policy);
2111 int __connman_iptables_append(const char *table_name,
2113 const char *rule_spec)
2115 struct connman_iptables *table;
2116 struct parse_context *ctx;
2117 const char *target_name;
2120 ctx = g_try_new0(struct parse_context, 1);
2124 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2126 err = prepare_getopt_args(rule_spec, ctx);
2130 table = get_table(table_name);
2131 if (table == NULL) {
2136 err = parse_rule_spec(table, ctx);
2140 if (ctx->xt_t == NULL)
2143 target_name = ctx->xt_t->name;
2145 err = iptables_append_rule(table, ctx->ip, chain,
2146 target_name, ctx->xt_t, ctx->xt_rm);
2148 cleanup_parse_context(ctx);
2154 int __connman_iptables_delete(const char *table_name,
2156 const char *rule_spec)
2158 struct connman_iptables *table;
2159 struct parse_context *ctx;
2160 const char *target_name;
2163 ctx = g_try_new0(struct parse_context, 1);
2167 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2169 err = prepare_getopt_args(rule_spec, ctx);
2173 table = get_table(table_name);
2174 if (table == NULL) {
2179 err = parse_rule_spec(table, ctx);
2183 if (ctx->xt_t == NULL)
2186 target_name = ctx->xt_t->name;
2188 err = iptables_delete_rule(table, ctx->ip, chain,
2189 target_name, ctx->xt_t, ctx->xt_m,
2192 cleanup_parse_context(ctx);
2198 int __connman_iptables_commit(const char *table_name)
2200 struct connman_iptables *table;
2201 struct ipt_replace *repl;
2204 DBG("%s", table_name);
2206 table = g_hash_table_lookup(table_hash, table_name);
2210 repl = iptables_blob(table);
2212 if (debug_enabled == TRUE)
2213 dump_ipt_replace(repl);
2215 err = iptables_replace(table, repl);
2217 g_free(repl->counters);
2223 g_hash_table_remove(table_hash, table_name);
2228 static void remove_table(gpointer user_data)
2230 struct connman_iptables *table = user_data;
2232 table_cleanup(table);
2235 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2236 unsigned int hook, size_t size,
2237 unsigned int offset, void *user_data)
2239 GSList **chains = user_data;
2240 struct xt_entry_target *target;
2243 if (offset + entry->next_offset == size)
2246 target = ipt_get_target(entry);
2248 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
2249 name = g_strdup((const char*)target->data);
2250 else if (builtin >= 0)
2251 name = g_strdup(hooknames[builtin]);
2255 *chains = g_slist_prepend(*chains, name);
2260 void flush_table(const char *name)
2262 GSList *chains = NULL, *list;
2263 struct connman_iptables *table;
2265 table = get_table(name);
2269 iterate_entries(table->blob_entries->entrytable,
2270 table->info->valid_hooks,
2271 table->info->hook_entry,
2272 table->info->underflow,
2273 table->blob_entries->size,
2274 flush_table_cb, &chains);
2278 * The offset update code is fragile and it works
2279 * only safe if we remove elements and move forwards
2282 chains = g_slist_reverse(chains);
2284 for (list = chains; list != NULL; list = list->next) {
2285 char *chain = list->data;
2287 DBG("chain %s", chain);
2288 iptables_flush_chain(table, chain);
2291 __connman_iptables_commit(name);
2292 g_slist_free_full(chains, g_free);
2295 int __connman_iptables_init(void)
2299 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2300 debug_enabled = TRUE;
2302 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2303 NULL, remove_table);
2305 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2310 void __connman_iptables_cleanup(void)
2314 g_hash_table_destroy(table_hash);