5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
39 void flush_table(const char *name);
42 * Some comments on how the iptables API works (some of them from the
43 * source code from iptables and the kernel):
45 * - valid_hooks: bit indicates valid IDs for hook_entry
46 * - hook_entry[ID] offset to the chain start
47 * - overflows should be end of entry chains, and uncodintional policy nodes.
48 * - policy entry: last entry in a chain
49 * - user chain: end of last builtin + policy entry
50 * - final entry must be error node
51 * - Underflows must be unconditional and use the STANDARD target with
53 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
54 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
55 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
56 * table header/meta info. The table is appended after the header. The entries
57 * are of the type struct ipt_entry.
58 * - After the ipt_entry the matches are appended. After the matches
59 * the target is appended.
60 * - ipt_entry->target_offset = Size of ipt_entry + matches
61 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
62 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
63 * - hook_entry and overflow mark the begining and the end of a chain, e.g
64 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
65 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
66 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
67 * the last element). FORWARD has one entry starting/ending at 352. The entry
68 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
69 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
71 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
72 * writing that table looks like this:
74 * filter valid_hooks 0x0000000e num_entries 5 size 856
75 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
76 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
77 * entry 0x699d30 offset 0 size 224
78 * RULE match 0x699da0 target 0x699dd0
79 * match mark match 0x3e7
80 * target LOG flags 0 level 4
83 * entry 0x699e10 offset 224 size 152
84 * RULE match 0x699e80 target 0x699e80
88 * entry 0x699ea8 offset 376 size 152
89 * RULE match 0x699f18 target 0x699f18
93 * entry 0x699f40 offset 528 size 152
94 * RULE match 0x699fb0 target 0x699fb0
98 * entry 0x699fd8 offset 680 size 176
99 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
101 * Reading the filter table looks like this:
103 * filter valid_hooks 0x0000000e num_entries 5 size 856
104 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
105 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
106 * entry 0x25fec28 offset 0 size 224
107 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
108 * match mark match 0x3e7
109 * target LOG flags 0 level 4
110 * src 0.0.0.0/0.0.0.0
111 * dst 0.0.0.0/0.0.0.0
112 * entry 0x25fed08 offset 224 size 152
113 * RULE match 0x25fed78 target 0x25fed78
115 * src 0.0.0.0/0.0.0.0
116 * dst 0.0.0.0/0.0.0.0
117 * entry 0x25feda0 offset 376 size 152
118 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
120 * src 0.0.0.0/0.0.0.0
121 * dst 0.0.0.0/0.0.0.0
122 * entry 0x25fee38 offset 528 size 152
123 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
125 * src 0.0.0.0/0.0.0.0
126 * dst 0.0.0.0/0.0.0.0
127 * entry 0x25feed0 offset 680 size 176
131 static const char *hooknames[] = {
132 [NF_IP_PRE_ROUTING] = "PREROUTING",
133 [NF_IP_LOCAL_IN] = "INPUT",
134 [NF_IP_FORWARD] = "FORWARD",
135 [NF_IP_LOCAL_OUT] = "OUTPUT",
136 [NF_IP_POST_ROUTING] = "POSTROUTING",
139 #define LABEL_ACCEPT "ACCEPT"
140 #define LABEL_DROP "DROP"
141 #define LABEL_QUEUE "QUEUE"
142 #define LABEL_RETURN "RETURN"
144 #define XT_OPTION_OFFSET_SCALE 256
146 #define MIN_ALIGN (__alignof__(struct ipt_entry))
148 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
150 struct error_target {
151 struct xt_entry_target t;
152 char error[IPT_TABLE_MAXNAMELEN];
155 struct connman_iptables_entry {
159 struct ipt_entry *entry;
162 struct connman_iptables {
165 struct ipt_getinfo *info;
166 struct ipt_get_entries *blob_entries;
168 unsigned int num_entries;
169 unsigned int old_entries;
172 unsigned int underflow[NF_INET_NUMHOOKS];
173 unsigned int hook_entry[NF_INET_NUMHOOKS];
178 static GHashTable *table_hash = NULL;
179 static gboolean debug_enabled = FALSE;
181 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
182 unsigned int hook,size_t size,
183 unsigned int offset, void *user_data);
185 static int iterate_entries(struct ipt_entry *entries,
186 unsigned int valid_hooks,
187 unsigned int *hook_entry,
188 size_t size, iterate_entries_cb_t cb,
193 struct ipt_entry *entry;
195 if (valid_hooks != 0)
196 h = __builtin_ffs(valid_hooks) - 1;
198 h = NF_INET_NUMHOOKS;
200 for (i = 0, entry = entries; i < size;
201 i += entry->next_offset) {
203 entry = (void *)entries + i;
206 * Find next valid hook which offset is higher
207 * or equal with the current offset.
209 if (h < NF_INET_NUMHOOKS) {
210 if (hook_entry[h] < i) {
211 valid_hooks ^= (1 << h);
213 if (valid_hooks != 0)
214 h = __builtin_ffs(valid_hooks) - 1;
216 h = NF_INET_NUMHOOKS;
219 if (hook_entry[h] == i)
223 err = cb(entry, builtin, h, size, i, user_data);
232 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
233 size_t size, unsigned int offset,
236 iterate_entries_cb_t cb = user_data;
238 DBG("entry %p hook %d offset %d size %d", entry, hook,
239 offset, entry->next_offset);
241 return cb(entry, builtin, hook, size, offset, NULL);
244 static int target_to_verdict(const char *target_name)
246 if (!strcmp(target_name, LABEL_ACCEPT))
247 return -NF_ACCEPT - 1;
249 if (!strcmp(target_name, LABEL_DROP))
252 if (!strcmp(target_name, LABEL_QUEUE))
253 return -NF_QUEUE - 1;
255 if (!strcmp(target_name, LABEL_RETURN))
261 static gboolean is_builtin_target(const char *target_name)
263 if (!strcmp(target_name, LABEL_ACCEPT) ||
264 !strcmp(target_name, LABEL_DROP) ||
265 !strcmp(target_name, LABEL_QUEUE) ||
266 !strcmp(target_name, LABEL_RETURN))
272 static gboolean is_jump(struct connman_iptables_entry *e)
274 struct xt_entry_target *target;
276 target = ipt_get_target(e->entry);
278 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
279 struct xt_standard_target *t;
281 t = (struct xt_standard_target *)target;
283 switch (t->verdict) {
299 static gboolean is_fallthrough(struct connman_iptables_entry *e)
301 struct xt_entry_target *target;
303 target = ipt_get_target(e->entry);
304 if (!strcmp(target->u.user.name, ""))
310 static gboolean is_chain(struct connman_iptables *table,
311 struct connman_iptables_entry *e)
313 struct ipt_entry *entry;
314 struct xt_entry_target *target;
320 target = ipt_get_target(entry);
321 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
327 static GList *find_chain_head(struct connman_iptables *table,
328 const char *chain_name)
331 struct connman_iptables_entry *head;
332 struct ipt_entry *entry;
333 struct xt_entry_target *target;
336 for (list = table->entries; list; list = list->next) {
341 builtin = head->builtin;
342 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
345 /* User defined chain */
346 target = ipt_get_target(entry);
347 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
348 !strcmp((char *)target->data, chain_name))
355 static GList *find_chain_tail(struct connman_iptables *table,
356 const char *chain_name)
358 struct connman_iptables_entry *tail;
359 GList *chain_head, *list;
361 chain_head = find_chain_head(table, chain_name);
362 if (chain_head == NULL)
365 /* Then we look for the next chain */
366 for (list = chain_head->next; list; list = list->next) {
369 if (is_chain(table, tail))
373 /* Nothing found, we return the table end */
374 return g_list_last(table->entries);
378 static void update_offsets(struct connman_iptables *table)
381 struct connman_iptables_entry *entry, *prev_entry;
383 for (list = table->entries; list; list = list->next) {
386 if (list == table->entries) {
393 prev_entry = prev->data;
395 entry->offset = prev_entry->offset +
396 prev_entry->entry->next_offset;
400 static void update_targets_reference(struct connman_iptables *table,
401 struct connman_iptables_entry *entry_before,
402 struct connman_iptables_entry *modified_entry,
403 gboolean is_removing)
405 struct connman_iptables_entry *tmp;
406 struct xt_standard_target *t;
410 offset = modified_entry->entry->next_offset;
412 for (list = table->entries; list; list = list->next) {
418 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
420 if (is_removing == TRUE) {
421 if (t->verdict >= entry_before->offset)
422 t->verdict -= offset;
424 if (t->verdict > entry_before->offset)
425 t->verdict += offset;
429 if (is_fallthrough(modified_entry)) {
430 t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
432 t->verdict = entry_before->offset +
433 modified_entry->entry->target_offset +
434 ALIGN(sizeof(struct xt_standard_target));
435 t->target.u.target_size =
436 ALIGN(sizeof(struct xt_standard_target));
440 static int iptables_add_entry(struct connman_iptables *table,
441 struct ipt_entry *entry, GList *before,
444 struct connman_iptables_entry *e, *entry_before;
449 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
454 e->builtin = builtin;
456 table->entries = g_list_insert_before(table->entries, before, e);
457 table->num_entries++;
458 table->size += entry->next_offset;
460 if (before == NULL) {
461 e->offset = table->size - entry->next_offset;
466 entry_before = before->data;
469 * We've just appended/insterted a new entry. All references
470 * should be bumped accordingly.
472 update_targets_reference(table, entry_before, e, FALSE);
474 update_offsets(table);
479 static int remove_table_entry(struct connman_iptables *table,
480 struct connman_iptables_entry *entry)
484 table->num_entries--;
485 table->size -= entry->entry->next_offset;
486 removed = entry->entry->next_offset;
488 table->entries = g_list_remove(table->entries, entry);
490 g_free(entry->entry);
496 static int iptables_flush_chain(struct connman_iptables *table,
499 GList *chain_head, *chain_tail, *list, *next;
500 struct connman_iptables_entry *entry;
501 int builtin, removed = 0;
503 chain_head = find_chain_head(table, name);
504 if (chain_head == NULL)
507 chain_tail = find_chain_tail(table, name);
508 if (chain_tail == NULL)
511 entry = chain_head->data;
512 builtin = entry->builtin;
517 list = chain_head->next;
519 if (list == chain_tail->prev)
522 while (list != chain_tail->prev) {
524 next = g_list_next(list);
526 removed += remove_table_entry(table, entry);
532 struct connman_iptables_entry *e;
536 entry->builtin = builtin;
538 table->underflow[builtin] -= removed;
540 for (list = chain_tail; list; list = list->next) {
543 builtin = e->builtin;
547 table->hook_entry[builtin] -= removed;
548 table->underflow[builtin] -= removed;
552 update_offsets(table);
557 static int iptables_add_chain(struct connman_iptables *table,
561 struct ipt_entry *entry_head;
562 struct ipt_entry *entry_return;
563 struct error_target *error;
564 struct ipt_standard_target *standard;
565 u_int16_t entry_head_size, entry_return_size;
567 last = g_list_last(table->entries);
570 * An empty chain is composed of:
571 * - A head entry, with no match and an error target.
572 * The error target data is the chain name.
573 * - A tail entry, with no match and a standard target.
574 * The standard target verdict is XT_RETURN (return to the
579 entry_head_size = sizeof(struct ipt_entry) +
580 sizeof(struct error_target);
581 entry_head = g_try_malloc0(entry_head_size);
582 if (entry_head == NULL)
585 memset(entry_head, 0, entry_head_size);
587 entry_head->target_offset = sizeof(struct ipt_entry);
588 entry_head->next_offset = entry_head_size;
590 error = (struct error_target *) entry_head->elems;
591 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
592 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
593 strcpy(error->error, name);
595 if (iptables_add_entry(table, entry_head, last, -1) < 0)
599 entry_return_size = sizeof(struct ipt_entry) +
600 sizeof(struct ipt_standard_target);
601 entry_return = g_try_malloc0(entry_return_size);
602 if (entry_return == NULL)
605 memset(entry_return, 0, entry_return_size);
607 entry_return->target_offset = sizeof(struct ipt_entry);
608 entry_return->next_offset = entry_return_size;
610 standard = (struct ipt_standard_target *) entry_return->elems;
611 standard->target.u.user.target_size =
612 ALIGN(sizeof(struct ipt_standard_target));
613 standard->verdict = XT_RETURN;
615 if (iptables_add_entry(table, entry_return, last, -1) < 0)
621 g_free(entry_return);
628 static int iptables_delete_chain(struct connman_iptables *table,
631 struct connman_iptables_entry *entry;
632 GList *chain_head, *chain_tail;
634 chain_head = find_chain_head(table, name);
635 if (chain_head == NULL)
638 entry = chain_head->data;
640 /* We cannot remove builtin chain */
641 if (entry->builtin >= 0)
644 chain_tail = find_chain_tail(table, name);
645 if (chain_tail == NULL)
648 /* Chain must be flushed */
649 if (chain_head->next != chain_tail->prev)
652 remove_table_entry(table, entry);
654 entry = chain_tail->prev->data;
655 remove_table_entry(table, entry);
657 update_offsets(table);
662 static struct ipt_entry *new_rule(struct ipt_ip *ip,
663 const char *target_name, struct xtables_target *xt_t,
664 struct xtables_rule_match *xt_rm)
666 struct xtables_rule_match *tmp_xt_rm;
667 struct ipt_entry *new_entry;
668 size_t match_size, target_size;
671 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
672 match_size += tmp_xt_rm->match->m->u.match_size;
675 target_size = ALIGN(xt_t->t->u.target_size);
677 target_size = ALIGN(sizeof(struct xt_standard_target));
679 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
681 if (new_entry == NULL)
684 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
686 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
687 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
691 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
692 tmp_xt_rm = tmp_xt_rm->next) {
693 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
694 tmp_xt_rm->match->m->u.match_size);
695 match_size += tmp_xt_rm->match->m->u.match_size;
699 struct xt_entry_target *entry_target;
701 entry_target = ipt_get_target(new_entry);
702 memcpy(entry_target, xt_t->t, target_size);
708 static void update_hooks(struct connman_iptables *table, GList *chain_head,
709 struct ipt_entry *entry)
712 struct connman_iptables_entry *head, *e;
715 if (chain_head == NULL)
718 head = chain_head->data;
720 builtin = head->builtin;
724 table->underflow[builtin] += entry->next_offset;
726 for (list = chain_head->next; list; list = list->next) {
729 builtin = e->builtin;
733 table->hook_entry[builtin] += entry->next_offset;
734 table->underflow[builtin] += entry->next_offset;
738 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
739 struct ipt_ip *ip, const char *chain_name,
740 const char *target_name,
741 struct xtables_target *xt_t,
742 int *builtin, struct xtables_rule_match *xt_rm)
744 GList *chain_tail, *chain_head;
745 struct ipt_entry *new_entry;
746 struct connman_iptables_entry *head;
748 chain_head = find_chain_head(table, chain_name);
749 if (chain_head == NULL)
752 chain_tail = find_chain_tail(table, chain_name);
753 if (chain_tail == NULL)
756 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
757 if (new_entry == NULL)
760 update_hooks(table, chain_head, new_entry);
763 * If the chain is builtin, and does not have any rule,
764 * then the one that we're inserting is becoming the head
765 * and thus needs the builtin flag.
767 head = chain_head->data;
768 if (head->builtin < 0)
770 else if (chain_head == chain_tail->prev) {
771 *builtin = head->builtin;
778 static int iptables_insert_rule(struct connman_iptables *table,
779 struct ipt_ip *ip, const char *chain_name,
780 const char *target_name,
781 struct xtables_target *xt_t,
782 struct xtables_rule_match *xt_rm)
784 struct ipt_entry *new_entry;
785 int builtin = -1, ret;
788 chain_head = find_chain_head(table, chain_name);
789 if (chain_head == NULL)
792 new_entry = prepare_rule_inclusion(table, ip, chain_name,
793 target_name, xt_t, &builtin, xt_rm);
794 if (new_entry == NULL)
798 chain_head = chain_head->next;
800 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
807 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
808 struct ipt_entry *i_e2)
810 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
813 if (i_e1->target_offset != i_e2->target_offset)
816 if (i_e1->next_offset != i_e2->next_offset)
822 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
823 struct xt_entry_target *xt_e_t2)
827 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
830 if (strcmp(xt_e_t1->u.user.name, "") == 0 &&
831 strcmp(xt_e_t2->u.user.name, "") == 0) {
834 } else if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
835 struct xt_standard_target *xt_s_t1;
836 struct xt_standard_target *xt_s_t2;
838 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
839 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
841 if (xt_s_t1->verdict != xt_s_t2->verdict)
844 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
847 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
850 for (i = 0; i < xt_e_t1->u.target_size -
851 sizeof(struct xt_standard_target); i++) {
852 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
860 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
861 struct xt_entry_match *xt_e_m2)
865 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
868 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
871 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
874 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
877 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
879 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
886 static GList *find_existing_rule(struct connman_iptables *table,
887 struct ipt_ip *ip, const char *chain_name,
888 const char *target_name,
889 struct xtables_target *xt_t,
890 struct xtables_match *xt_m,
891 struct xtables_rule_match *xt_rm)
893 GList *chain_tail, *chain_head, *list;
894 struct xt_entry_target *xt_e_t = NULL;
895 struct xt_entry_match *xt_e_m = NULL;
896 struct connman_iptables_entry *entry;
897 struct ipt_entry *entry_test;
900 chain_head = find_chain_head(table, chain_name);
901 if (chain_head == NULL)
904 chain_tail = find_chain_tail(table, chain_name);
905 if (chain_tail == NULL)
911 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
912 if (entry_test == NULL)
916 xt_e_t = ipt_get_target(entry_test);
918 xt_e_m = (struct xt_entry_match *)entry_test->elems;
920 entry = chain_head->data;
921 builtin = entry->builtin;
926 list = chain_head->next;
928 for (; list != chain_tail->prev; list = list->next) {
929 struct connman_iptables_entry *tmp;
930 struct ipt_entry *tmp_e;
935 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
939 struct xt_entry_target *tmp_xt_e_t;
941 tmp_xt_e_t = ipt_get_target(tmp_e);
943 if (!is_same_target(tmp_xt_e_t, xt_e_t))
948 struct xt_entry_match *tmp_xt_e_m;
950 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
952 if (!is_same_match(tmp_xt_e_m, xt_e_m))
961 if (list != chain_tail->prev)
967 static int iptables_delete_rule(struct connman_iptables *table,
968 struct ipt_ip *ip, const char *chain_name,
969 const char *target_name,
970 struct xtables_target *xt_t,
971 struct xtables_match *xt_m,
972 struct xtables_rule_match *xt_rm)
974 struct connman_iptables_entry *entry;
975 GList *chain_head, *chain_tail, *list;
976 int builtin, removed;
980 chain_head = find_chain_head(table, chain_name);
981 if (chain_head == NULL)
984 chain_tail = find_chain_tail(table, chain_name);
985 if (chain_tail == NULL)
988 list = find_existing_rule(table, ip, chain_name, target_name,
993 entry = chain_head->data;
994 builtin = entry->builtin;
1000 /* We have deleted a rule,
1001 * all references should be bumped accordingly */
1002 if (list->next != NULL)
1003 update_targets_reference(table, list->next->data,
1006 removed += remove_table_entry(table, entry);
1012 entry->builtin = builtin;
1015 table->underflow[builtin] -= removed;
1016 for (list = chain_tail; list; list = list->next) {
1019 builtin = entry->builtin;
1023 table->hook_entry[builtin] -= removed;
1024 table->underflow[builtin] -= removed;
1028 update_offsets(table);
1033 static int iptables_change_policy(struct connman_iptables *table,
1034 const char *chain_name, const char *policy)
1037 struct connman_iptables_entry *entry;
1038 struct xt_entry_target *target;
1039 struct xt_standard_target *t;
1042 verdict = target_to_verdict(policy);
1046 chain_head = find_chain_head(table, chain_name);
1047 if (chain_head == NULL)
1050 entry = chain_head->data;
1051 if (entry->builtin < 0)
1054 target = ipt_get_target(entry->entry);
1056 t = (struct xt_standard_target *)target;
1057 t->verdict = verdict;
1062 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1064 struct ipt_replace *r;
1066 struct connman_iptables_entry *e;
1067 unsigned char *entry_index;
1069 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1073 memset(r, 0, sizeof(*r) + table->size);
1075 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1076 * table->old_entries);
1077 if (r->counters == NULL) {
1082 strcpy(r->name, table->info->name);
1083 r->num_entries = table->num_entries;
1084 r->size = table->size;
1086 r->num_counters = table->old_entries;
1087 r->valid_hooks = table->info->valid_hooks;
1089 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1090 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1092 entry_index = (unsigned char *)r->entries;
1093 for (list = table->entries; list; list = list->next) {
1096 memcpy(entry_index, e->entry, e->entry->next_offset);
1097 entry_index += e->entry->next_offset;
1103 static void dump_ip(struct ipt_entry *entry)
1105 struct ipt_ip *ip = &entry->ip;
1106 char ip_string[INET6_ADDRSTRLEN];
1107 char ip_mask[INET6_ADDRSTRLEN];
1109 if (strlen(ip->iniface))
1110 DBG("\tin %s", ip->iniface);
1112 if (strlen(ip->outiface))
1113 DBG("\tout %s", ip->outiface);
1115 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1116 inet_ntop(AF_INET, &ip->smsk,
1117 ip_mask, INET6_ADDRSTRLEN) != NULL)
1118 DBG("\tsrc %s/%s", ip_string, ip_mask);
1120 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1121 inet_ntop(AF_INET, &ip->dmsk,
1122 ip_mask, INET6_ADDRSTRLEN) != NULL)
1123 DBG("\tdst %s/%s", ip_string, ip_mask);
1126 static void dump_target(struct ipt_entry *entry)
1129 struct xtables_target *xt_t;
1130 struct xt_entry_target *target;
1132 target = ipt_get_target(entry);
1134 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1135 struct xt_standard_target *t;
1137 t = (struct xt_standard_target *)target;
1139 switch (t->verdict) {
1141 DBG("\ttarget RETURN");
1144 case -NF_ACCEPT - 1:
1145 DBG("\ttarget ACCEPT");
1149 DBG("\ttarget DROP");
1153 DBG("\ttarget QUEUE");
1157 DBG("\ttarget STOP");
1161 DBG("\tJUMP %u", t->verdict);
1165 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1166 XTF_LOAD_MUST_SUCCEED);
1168 if(xt_t->print != NULL)
1169 xt_t->print(NULL, target, 1);
1171 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1173 DBG("\ttarget %s", target->u.user.name);
1177 if(xt_t->print != NULL) {
1179 xt_t->print(NULL, target, 1);
1184 static void dump_match(struct ipt_entry *entry)
1186 struct xtables_match *xt_m;
1187 struct xt_entry_match *match;
1189 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1192 match = (struct xt_entry_match *) entry->elems;
1194 if (!strlen(match->u.user.name))
1197 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1201 if(xt_m->print != NULL) {
1203 xt_m->print(NULL, match, 1);
1209 DBG("\tmatch %s", match->u.user.name);
1213 static int dump_entry(struct ipt_entry *entry, int builtin,
1214 unsigned int hook, size_t size, unsigned int offset,
1217 struct xt_entry_target *target;
1219 target = ipt_get_target(entry);
1221 if (offset + entry->next_offset == size) {
1222 DBG("\tEnd of CHAIN");
1226 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1227 DBG("\tUSER CHAIN (%s) match %p target %p",
1228 target->data, entry->elems,
1229 (char *)entry + entry->target_offset);
1232 } else if (builtin >= 0) {
1233 DBG("\tCHAIN (%s) match %p target %p",
1234 hooknames[builtin], entry->elems,
1235 (char *)entry + entry->target_offset);
1237 DBG("\tRULE match %p target %p",
1239 (char *)entry + entry->target_offset);
1249 static void dump_table(struct connman_iptables *table)
1251 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1253 table->info->valid_hooks, table->info->num_entries,
1256 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1257 table->info->hook_entry[NF_IP_PRE_ROUTING],
1258 table->info->hook_entry[NF_IP_LOCAL_IN],
1259 table->info->hook_entry[NF_IP_FORWARD],
1260 table->info->hook_entry[NF_IP_LOCAL_OUT],
1261 table->info->hook_entry[NF_IP_POST_ROUTING]);
1262 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1263 table->info->underflow[NF_IP_PRE_ROUTING],
1264 table->info->underflow[NF_IP_LOCAL_IN],
1265 table->info->underflow[NF_IP_FORWARD],
1266 table->info->underflow[NF_IP_LOCAL_OUT],
1267 table->info->underflow[NF_IP_POST_ROUTING]);
1269 iterate_entries(table->blob_entries->entrytable,
1270 table->info->valid_hooks,
1271 table->info->hook_entry,
1272 table->blob_entries->size,
1273 print_entry, dump_entry);
1276 static void dump_ipt_replace(struct ipt_replace *repl)
1278 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1279 repl->name, repl->valid_hooks, repl->num_entries,
1282 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1283 repl->hook_entry[NF_IP_PRE_ROUTING],
1284 repl->hook_entry[NF_IP_LOCAL_IN],
1285 repl->hook_entry[NF_IP_FORWARD],
1286 repl->hook_entry[NF_IP_LOCAL_OUT],
1287 repl->hook_entry[NF_IP_POST_ROUTING]);
1288 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1289 repl->underflow[NF_IP_PRE_ROUTING],
1290 repl->underflow[NF_IP_LOCAL_IN],
1291 repl->underflow[NF_IP_FORWARD],
1292 repl->underflow[NF_IP_LOCAL_OUT],
1293 repl->underflow[NF_IP_POST_ROUTING]);
1295 iterate_entries(repl->entries, repl->valid_hooks,
1296 repl->hook_entry, repl->size, print_entry, dump_entry);
1299 static int iptables_get_entries(struct connman_iptables *table)
1301 socklen_t entry_size;
1303 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1305 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1306 table->blob_entries, &entry_size);
1309 static int iptables_replace(struct connman_iptables *table,
1310 struct ipt_replace *r)
1312 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1313 sizeof(*r) + r->size);
1316 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1317 size_t size, unsigned offset, void *user_data)
1319 struct connman_iptables *table = user_data;
1320 struct ipt_entry *new_entry;
1322 new_entry = g_try_malloc0(entry->next_offset);
1323 if (new_entry == NULL)
1326 memcpy(new_entry, entry, entry->next_offset);
1328 return iptables_add_entry(table, new_entry, NULL, builtin);
1331 static void table_cleanup(struct connman_iptables *table)
1334 struct connman_iptables_entry *entry;
1339 if (table->ipt_sock >= 0)
1340 close(table->ipt_sock);
1342 for (list = table->entries; list; list = list->next) {
1345 g_free(entry->entry);
1349 g_list_free(table->entries);
1350 g_free(table->info);
1351 g_free(table->blob_entries);
1355 static struct connman_iptables *iptables_init(const char *table_name)
1357 struct connman_iptables *table = NULL;
1358 char *module = NULL;
1361 if (table_name == NULL)
1362 table_name = "filter";
1364 DBG("%s", table_name);
1366 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1367 DBG("ip_tables module loading gives error but trying anyway");
1369 module = g_strconcat("iptable_", table_name, NULL);
1373 if (xtables_insmod(module, NULL, TRUE) != 0)
1374 DBG("%s module loading gives error but trying anyway", module);
1378 table = g_hash_table_lookup(table_hash, table_name);
1382 table = g_try_new0(struct connman_iptables, 1);
1386 table->info = g_try_new0(struct ipt_getinfo, 1);
1387 if (table->info == NULL)
1390 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1391 if (table->ipt_sock < 0)
1394 s = sizeof(*table->info);
1395 strcpy(table->info->name, table_name);
1396 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1397 table->info, &s) < 0) {
1398 connman_error("iptables support missing error %d (%s)", errno,
1403 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1405 if (table->blob_entries == NULL)
1408 strcpy(table->blob_entries->name, table_name);
1409 table->blob_entries->size = table->info->size;
1411 if (iptables_get_entries(table) < 0)
1414 table->num_entries = 0;
1415 table->old_entries = table->info->num_entries;
1418 memcpy(table->underflow, table->info->underflow,
1419 sizeof(table->info->underflow));
1420 memcpy(table->hook_entry, table->info->hook_entry,
1421 sizeof(table->info->hook_entry));
1423 iterate_entries(table->blob_entries->entrytable,
1424 table->info->valid_hooks, table->info->hook_entry,
1425 table->blob_entries->size, add_entry, table);
1427 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1429 if (debug_enabled == TRUE)
1435 table_cleanup(table);
1440 static struct option iptables_opts[] = {
1441 {.name = "append", .has_arg = 1, .val = 'A'},
1442 {.name = "compare", .has_arg = 1, .val = 'C'},
1443 {.name = "delete", .has_arg = 1, .val = 'D'},
1444 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1445 {.name = "insert", .has_arg = 1, .val = 'I'},
1446 {.name = "list", .has_arg = 2, .val = 'L'},
1447 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1448 {.name = "policy", .has_arg = 1, .val = 'P'},
1449 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1450 {.name = "destination", .has_arg = 1, .val = 'd'},
1451 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1452 {.name = "jump", .has_arg = 1, .val = 'j'},
1453 {.name = "match", .has_arg = 1, .val = 'm'},
1454 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1455 {.name = "source", .has_arg = 1, .val = 's'},
1456 {.name = "table", .has_arg = 1, .val = 't'},
1460 struct xtables_globals iptables_globals = {
1462 .opts = iptables_opts,
1463 .orig_opts = iptables_opts,
1466 static struct xtables_target *prepare_target(struct connman_iptables *table,
1467 const char *target_name)
1469 struct xtables_target *xt_t = NULL;
1470 gboolean is_builtin, is_user_defined;
1471 GList *chain_head = NULL;
1475 is_user_defined = FALSE;
1477 if (is_builtin_target(target_name))
1480 chain_head = find_chain_head(table, target_name);
1481 if (chain_head != NULL && chain_head->next != NULL)
1482 is_user_defined = TRUE;
1485 if (is_builtin || is_user_defined)
1486 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1487 XTF_LOAD_MUST_SUCCEED);
1489 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1494 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1496 xt_t->t = g_try_malloc0(target_size);
1497 if (xt_t->t == NULL)
1500 xt_t->t->u.target_size = target_size;
1502 if (is_builtin || is_user_defined) {
1503 struct xt_standard_target *target;
1505 target = (struct xt_standard_target *)(xt_t->t);
1506 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1508 if (is_builtin == TRUE)
1509 target->verdict = target_to_verdict(target_name);
1510 else if (is_user_defined == TRUE) {
1511 struct connman_iptables_entry *target_rule;
1513 if (chain_head == NULL) {
1518 target_rule = chain_head->next->data;
1519 target->verdict = target_rule->offset;
1522 strcpy(xt_t->t->u.user.name, target_name);
1523 xt_t->t->u.user.revision = xt_t->revision;
1524 if (xt_t->init != NULL)
1525 xt_t->init(xt_t->t);
1528 #if XTABLES_VERSION_CODE > 5
1529 if (xt_t->x6_options != NULL)
1530 iptables_globals.opts =
1531 xtables_options_xfrm(
1532 iptables_globals.orig_opts,
1533 iptables_globals.opts,
1535 &xt_t->option_offset);
1538 iptables_globals.opts =
1539 xtables_merge_options(
1540 #if XTABLES_VERSION_CODE > 5
1541 iptables_globals.orig_opts,
1543 iptables_globals.opts,
1545 &xt_t->option_offset);
1547 if (iptables_globals.opts == NULL) {
1555 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1556 struct xtables_rule_match **xt_rm,
1557 const char *match_name)
1559 struct xtables_match *xt_m;
1562 if (match_name == NULL)
1565 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1566 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1568 xt_m->m = g_try_malloc0(match_size);
1569 if (xt_m->m == NULL)
1572 xt_m->m->u.match_size = match_size;
1573 strcpy(xt_m->m->u.user.name, xt_m->name);
1574 xt_m->m->u.user.revision = xt_m->revision;
1576 if (xt_m->init != NULL)
1577 xt_m->init(xt_m->m);
1579 if (xt_m == xt_m->next)
1582 #if XTABLES_VERSION_CODE > 5
1583 if (xt_m->x6_options != NULL)
1584 iptables_globals.opts =
1585 xtables_options_xfrm(
1586 iptables_globals.orig_opts,
1587 iptables_globals.opts,
1589 &xt_m->option_offset);
1592 iptables_globals.opts =
1593 xtables_merge_options(
1594 #if XTABLES_VERSION_CODE > 5
1595 iptables_globals.orig_opts,
1597 iptables_globals.opts,
1599 &xt_m->option_offset);
1601 if (iptables_globals.opts == NULL) {
1610 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1613 uint32_t prefixlength;
1617 tokens = g_strsplit(str, "/", 2);
1621 if (!inet_pton(AF_INET, tokens[0], ip)) {
1626 if (tokens[1] != NULL) {
1627 prefixlength = strtol(tokens[1], NULL, 10);
1628 if (prefixlength > 31) {
1633 tmp = ~(0xffffffff >> prefixlength);
1638 mask->s_addr = htonl(tmp);
1639 ip->s_addr = ip->s_addr & mask->s_addr;
1647 static struct connman_iptables *pre_load_table(const char *table_name,
1648 struct connman_iptables *table)
1653 return iptables_init(table_name);
1656 static void clear_tables_flags(void)
1658 struct xtables_match *xt_m;
1659 struct xtables_target *xt_t;
1662 * Clear all flags because the flags are only valid
1665 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1668 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
1674 struct parse_context {
1678 struct xtables_target *xt_t;
1679 struct xtables_match *xt_m;
1680 struct xtables_rule_match *xt_rm;
1683 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1688 tokens = g_strsplit_set(str, " ", -1);
1690 for (i = 0; tokens[i]; i++);
1692 /* Add space for the argv[0] value */
1695 /* Don't forget the last NULL entry */
1696 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1697 if (ctx->argv == NULL) {
1703 * getopt_long() jumps over the first token; we need to add some
1704 * random argv[0] entry.
1706 ctx->argv[0] = g_strdup("argh");
1707 for (i = 1; i < ctx->argc; i++)
1708 ctx->argv[i] = tokens[i - 1];
1713 #if XTABLES_VERSION_CODE > 5
1715 static int parse_xt_modules(int c, connman_bool_t invert,
1716 struct parse_context *ctx)
1718 struct xtables_match *m;
1719 struct xtables_rule_match *rm;
1721 DBG("xtables version code > 5");
1723 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1724 if (rm->completed != 0)
1729 if (m->x6_parse == NULL && m->parse == NULL)
1732 if (c < (int) m->option_offset ||
1733 c >= (int) m->option_offset
1734 + XT_OPTION_OFFSET_SCALE)
1737 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1740 if (ctx->xt_t == NULL)
1743 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1746 if (c < (int) ctx->xt_t->option_offset ||
1747 c >= (int) ctx->xt_t->option_offset
1748 + XT_OPTION_OFFSET_SCALE)
1751 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1756 static int final_check_xt_modules(struct parse_context *ctx)
1758 struct xtables_rule_match *rm;
1760 DBG("xtables version code > 5");
1762 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1763 xtables_option_mfcall(rm->match);
1765 if (ctx->xt_t != NULL)
1766 xtables_option_tfcall(ctx->xt_t);
1773 static int parse_xt_modules(int c, connman_bool_t invert,
1774 struct parse_context *ctx)
1776 struct xtables_match *m;
1777 struct xtables_rule_match *rm;
1780 DBG("xtables version code <= 5");
1782 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1783 if (rm->completed == 1)
1788 if (m->parse == NULL)
1791 err = m->parse(c - m->option_offset,
1792 argv, invert, &m->mflags,
1798 if (ctx->xt_t == NULL)
1801 if (ctx->xt_t->parse == NULL)
1804 err = ctx->xt_m->parse(c - ctx->xt_m->option_offset,
1805 ctx->argv, invert, &ctx->xt_m->mflags,
1806 NULL, &ctx->xt_m->m);
1810 static int final_check_xt_modules(struct parse_context *ctx)
1812 struct xtables_rule_match *rm;
1814 DBG("xtables version code <= 5");
1816 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1817 if (rm->match->final_check != NULL)
1818 rm->match->final_check(rm->match->mflags);
1820 if (ctx->xt_t != NULL && ctx->xt_t->final_check != NULL)
1821 ctx->xt_t->final_check(ctx->xt_t->tflags);
1828 static int parse_rule_spec(struct connman_iptables *table,
1829 struct parse_context *ctx)
1832 * How the parser works:
1834 * - If getopt finds 's', 'd', 'i', 'o'.
1835 * just extract the information.
1836 * - if '!' is found, set the invert flag to true and
1837 * removes the '!' from the optarg string and jumps
1838 * back to getopt to reparse the current optarg string.
1839 * After reparsing the invert flag is reseted to false.
1840 * - If 'm' or 'j' is found then call either
1841 * prepare_matches() or prepare_target(). Those function
1842 * will modify (extend) the longopts for getopt_long.
1843 * That means getopt will change its matching context according
1844 * the loaded target.
1846 * Here an example with iptables-test
1848 * argv[0] = ./tools/iptables-test
1860 * getopt found 'm' then the optarg is "mark" and optind 7
1861 * The longopts array containts before hitting the `case 'm'`
1863 * val A has_arg 1 name append
1864 * val C has_arg 1 name compare
1865 * val D has_arg 1 name delete
1866 * val F has_arg 1 name flush-chain
1867 * val I has_arg 1 name insert
1868 * val L has_arg 2 name list
1869 * val N has_arg 1 name new-chain
1870 * val P has_arg 1 name policy
1871 * val X has_arg 1 name delete-chain
1872 * val d has_arg 1 name destination
1873 * val i has_arg 1 name in-interface
1874 * val j has_arg 1 name jump
1875 * val m has_arg 1 name match
1876 * val o has_arg 1 name out-interface
1877 * val s has_arg 1 name source
1878 * val t has_arg 1 name table
1880 * After executing the `case 'm'` block longopts is
1882 * val A has_arg 1 name append
1883 * val C has_arg 1 name compare
1884 * val D has_arg 1 name delete
1885 * val F has_arg 1 name flush-chain
1886 * val I has_arg 1 name insert
1887 * val L has_arg 2 name list
1888 * val N has_arg 1 name new-chain
1889 * val P has_arg 1 name policy
1890 * val X has_arg 1 name delete-chain
1891 * val d has_arg 1 name destination
1892 * val i has_arg 1 name in-interface
1893 * val j has_arg 1 name jump
1894 * val m has_arg 1 name match
1895 * val o has_arg 1 name out-interface
1896 * val s has_arg 1 name source
1897 * val t has_arg 1 name table
1898 * val has_arg 1 name mark
1900 * So the 'mark' matcher has added the 'mark' options
1901 * and getopt will then return c '256' optarg "999" optind 9
1902 * And we will hit the 'default' statement which then
1903 * will call the matchers parser (xt_m->parser() or
1904 * xtables_option_mpcall() depending on which version
1905 * of libxtables is found.
1907 connman_bool_t invert = FALSE;
1912 ctx->ip = g_try_new0(struct ipt_ip, 1);
1913 if (ctx->ip == NULL)
1917 * As side effect parsing a rule sets some global flags
1918 * which will be evaluated/verified. Let's reset them
1919 * to ensure we can parse more than one rule.
1921 clear_tables_flags();
1924 * Tell getopt_long not to generate error messages for unknown
1925 * options and also reset optind back to 0.
1930 while ((c = getopt_long(ctx->argc, ctx->argv,
1932 iptables_globals.opts, NULL)) != -1) {
1935 /* Source specification */
1936 if (!parse_ip_and_mask(optarg,
1942 ctx->ip->invflags |= IPT_INV_SRCIP;
1946 /* Destination specification */
1947 if (!parse_ip_and_mask(optarg,
1953 ctx->ip->invflags |= IPT_INV_DSTIP;
1956 /* In interface specification */
1957 len = strlen(optarg);
1959 if (len + 1 > IFNAMSIZ)
1962 strcpy(ctx->ip->iniface, optarg);
1963 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1966 ctx->ip->invflags |= IPT_INV_VIA_IN;
1970 /* Out interface specification */
1971 len = strlen(optarg);
1973 if (len + 1 > IFNAMSIZ)
1976 strcpy(ctx->ip->outiface, optarg);
1977 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1980 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1985 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1986 if (ctx->xt_m == NULL) {
1994 ctx->xt_t = prepare_target(table, optarg);
1995 if (ctx->xt_t == NULL) {
2002 if (optarg[0] == '!' && optarg[1] == '\0') {
2005 /* Remove the '!' from the optarg */
2009 * And recall getopt_long without reseting
2017 err = parse_xt_modules(c, invert, ctx);
2027 err = final_check_xt_modules(ctx);
2033 static void reset_xtables(void)
2036 * We need also to free the memory implicitly allocated
2037 * during parsing (see xtables_options_xfrm()).
2038 * Note xt_params is actually iptables_globals.
2040 if (xt_params->opts != xt_params->orig_opts) {
2041 g_free(xt_params->opts);
2042 xt_params->opts = xt_params->orig_opts;
2044 xt_params->option_offset = 0;
2047 static void cleanup_parse_context(struct parse_context *ctx)
2049 struct xtables_rule_match *rm, *tmp;
2051 g_strfreev(ctx->argv);
2053 if (ctx->xt_t != NULL) {
2054 g_free(ctx->xt_t->t);
2055 ctx->xt_t->t = NULL;
2057 if (ctx->xt_m != NULL) {
2058 g_free(ctx->xt_m->m);
2059 ctx->xt_m->m = NULL;
2061 for (tmp = NULL, rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
2071 int __connman_iptables_new_chain(const char *table_name,
2074 struct connman_iptables *table;
2076 DBG("-t %s -N %s", table_name, chain);
2078 table = pre_load_table(table_name, NULL);
2082 return iptables_add_chain(table, chain);
2085 int __connman_iptables_delete_chain(const char *table_name,
2088 struct connman_iptables *table;
2090 DBG("-t %s -X %s", table_name, chain);
2092 table = pre_load_table(table_name, NULL);
2096 return iptables_delete_chain(table, chain);
2099 int __connman_iptables_flush_chain(const char *table_name,
2102 struct connman_iptables *table;
2104 DBG("-t %s -F %s", table_name, chain);
2106 table = pre_load_table(table_name, NULL);
2110 return iptables_flush_chain(table, chain);
2113 int __connman_iptables_change_policy(const char *table_name,
2117 struct connman_iptables *table;
2119 DBG("-t %s -F %s", table_name, chain);
2121 table = pre_load_table(table_name, NULL);
2125 return iptables_change_policy(table, chain, policy);
2128 int __connman_iptables_append(const char *table_name,
2130 const char *rule_spec)
2132 struct connman_iptables *table;
2133 struct parse_context *ctx;
2134 const char *target_name;
2137 ctx = g_try_new0(struct parse_context, 1);
2141 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2143 err = prepare_getopt_args(rule_spec, ctx);
2147 table = pre_load_table(table_name, NULL);
2148 if (table == NULL) {
2153 err = parse_rule_spec(table, ctx);
2157 if (ctx->xt_t == NULL)
2160 target_name = ctx->xt_t->name;
2162 err = iptables_insert_rule(table, ctx->ip, chain,
2163 target_name, ctx->xt_t, ctx->xt_rm);
2165 cleanup_parse_context(ctx);
2171 int __connman_iptables_delete(const char *table_name,
2173 const char *rule_spec)
2175 struct connman_iptables *table;
2176 struct parse_context *ctx;
2177 const char *target_name;
2180 ctx = g_try_new0(struct parse_context, 1);
2184 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2186 err = prepare_getopt_args(rule_spec, ctx);
2190 table = pre_load_table(table_name, NULL);
2191 if (table == NULL) {
2196 err = parse_rule_spec(table, ctx);
2200 if (ctx->xt_t == NULL)
2203 target_name = ctx->xt_t->name;
2205 err = iptables_delete_rule(table, ctx->ip, chain,
2206 target_name, ctx->xt_t, ctx->xt_m,
2209 cleanup_parse_context(ctx);
2215 int __connman_iptables_commit(const char *table_name)
2217 struct connman_iptables *table;
2218 struct ipt_replace *repl;
2221 DBG("%s", table_name);
2223 table = g_hash_table_lookup(table_hash, table_name);
2227 repl = iptables_blob(table);
2229 if (debug_enabled == TRUE)
2230 dump_ipt_replace(repl);
2232 err = iptables_replace(table, repl);
2234 g_free(repl->counters);
2240 g_hash_table_remove(table_hash, table_name);
2245 static void remove_table(gpointer user_data)
2247 struct connman_iptables *table = user_data;
2249 table_cleanup(table);
2252 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2253 unsigned int hook, size_t size,
2254 unsigned int offset, void *user_data)
2256 GSList **chains = user_data;
2257 struct xt_entry_target *target;
2260 if (offset + entry->next_offset == size)
2263 target = ipt_get_target(entry);
2265 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
2266 name = g_strdup((const char*)target->data);
2267 else if (builtin >= 0)
2268 name = g_strdup(hooknames[builtin]);
2272 *chains = g_slist_prepend(*chains, name);
2277 void flush_table(const char *name)
2279 GSList *chains = NULL, *list;
2280 struct connman_iptables *table;
2282 table = pre_load_table(name, NULL);
2286 iterate_entries(table->blob_entries->entrytable,
2287 table->info->valid_hooks,
2288 table->info->hook_entry,
2289 table->blob_entries->size,
2290 flush_table_cb, &chains);
2292 for (list = chains; list != NULL; list = list->next) {
2293 char *chain = list->data;
2295 DBG("chain %s", chain);
2296 iptables_flush_chain(table, chain);
2299 __connman_iptables_commit(name);
2300 g_slist_free_full(chains, g_free);
2303 int __connman_iptables_init(void)
2307 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2308 debug_enabled = TRUE;
2310 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2311 g_free, remove_table);
2313 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2318 void __connman_iptables_cleanup(void)
2322 g_hash_table_destroy(table_hash);