5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
41 * Some comments on how the iptables API works (some of them from the
42 * source code from iptables and the kernel):
44 * - valid_hooks: bit indicates valid IDs for hook_entry
45 * - hook_entry[ID] offset to the chain start
46 * - overflows should be end of entry chains, and uncodintional policy nodes.
47 * - policy entry: last entry in a chain
48 * - user chain: end of last builtin + policy entry
49 * - final entry must be error node
50 * - Underflows must be unconditional and use the STANDARD target with
52 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
53 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
54 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
55 * table header/meta info. The table is appended after the header. The entries
56 * are of the type struct ipt_entry.
57 * - After the ipt_entry the matches are appended. After the matches
58 * the target is appended.
59 * - ipt_entry->target_offset = Size of ipt_entry + matches
60 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
61 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
62 * - hook_entry and overflow mark the begining and the end of a chain, e.g
63 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
64 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
65 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
66 * the last element). FORWARD has one entry starting/ending at 352. The entry
67 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
68 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
70 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
71 * writing that table looks like this:
73 * filter valid_hooks 0x0000000e num_entries 5 size 856
74 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
75 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
76 * entry 0x699d30 offset 0 size 224
77 * RULE match 0x699da0 target 0x699dd0
78 * match mark match 0x3e7
79 * target LOG flags 0 level 4
82 * entry 0x699e10 offset 224 size 152
83 * RULE match 0x699e80 target 0x699e80
87 * entry 0x699ea8 offset 376 size 152
88 * RULE match 0x699f18 target 0x699f18
92 * entry 0x699f40 offset 528 size 152
93 * RULE match 0x699fb0 target 0x699fb0
97 * entry 0x699fd8 offset 680 size 176
98 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
100 * Reading the filter table looks like this:
102 * filter valid_hooks 0x0000000e num_entries 5 size 856
103 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
104 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
105 * entry 0x25fec28 offset 0 size 224
106 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
107 * match mark match 0x3e7
108 * target LOG flags 0 level 4
109 * src 0.0.0.0/0.0.0.0
110 * dst 0.0.0.0/0.0.0.0
111 * entry 0x25fed08 offset 224 size 152
112 * RULE match 0x25fed78 target 0x25fed78
114 * src 0.0.0.0/0.0.0.0
115 * dst 0.0.0.0/0.0.0.0
116 * entry 0x25feda0 offset 376 size 152
117 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
119 * src 0.0.0.0/0.0.0.0
120 * dst 0.0.0.0/0.0.0.0
121 * entry 0x25fee38 offset 528 size 152
122 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
124 * src 0.0.0.0/0.0.0.0
125 * dst 0.0.0.0/0.0.0.0
126 * entry 0x25feed0 offset 680 size 176
130 static const char *hooknames[] = {
131 [NF_IP_PRE_ROUTING] = "PREROUTING",
132 [NF_IP_LOCAL_IN] = "INPUT",
133 [NF_IP_FORWARD] = "FORWARD",
134 [NF_IP_LOCAL_OUT] = "OUTPUT",
135 [NF_IP_POST_ROUTING] = "POSTROUTING",
138 #define LABEL_ACCEPT "ACCEPT"
139 #define LABEL_DROP "DROP"
140 #define LABEL_QUEUE "QUEUE"
141 #define LABEL_RETURN "RETURN"
143 #define XT_OPTION_OFFSET_SCALE 256
145 #define MIN_ALIGN (__alignof__(struct ipt_entry))
147 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
149 struct error_target {
150 struct xt_entry_target t;
151 char error[IPT_TABLE_MAXNAMELEN];
154 struct connman_iptables_entry {
158 struct ipt_entry *entry;
161 struct connman_iptables {
164 struct ipt_getinfo *info;
165 struct ipt_get_entries *blob_entries;
167 unsigned int num_entries;
168 unsigned int old_entries;
171 unsigned int underflow[NF_INET_NUMHOOKS];
172 unsigned int hook_entry[NF_INET_NUMHOOKS];
177 static GHashTable *table_hash = NULL;
178 static gboolean debug_enabled = FALSE;
180 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
181 unsigned int hook,size_t size,
182 unsigned int offset, void *user_data);
184 static int iterate_entries(struct ipt_entry *entries,
185 unsigned int valid_hooks,
186 unsigned int *hook_entry,
187 size_t size, iterate_entries_cb_t cb,
192 struct ipt_entry *entry;
194 if (valid_hooks != 0)
195 h = __builtin_ffs(valid_hooks) - 1;
197 h = NF_INET_NUMHOOKS;
199 for (i = 0, entry = entries; i < size;
200 i += entry->next_offset) {
202 entry = (void *)entries + i;
205 * Find next valid hook which offset is higher
206 * or equal with the current offset.
208 if (h < NF_INET_NUMHOOKS) {
209 if (hook_entry[h] < i) {
210 valid_hooks ^= (1 << h);
212 if (valid_hooks != 0)
213 h = __builtin_ffs(valid_hooks) - 1;
215 h = NF_INET_NUMHOOKS;
218 if (hook_entry[h] == i)
222 err = cb(entry, builtin, h, size, i, user_data);
231 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
232 size_t size, unsigned int offset,
235 iterate_entries_cb_t cb = user_data;
237 DBG("entry %p hook %d offset %d size %d", entry, hook,
238 offset, entry->next_offset);
240 return cb(entry, builtin, hook, size, offset, NULL);
243 static int target_to_verdict(const char *target_name)
245 if (!strcmp(target_name, LABEL_ACCEPT))
246 return -NF_ACCEPT - 1;
248 if (!strcmp(target_name, LABEL_DROP))
251 if (!strcmp(target_name, LABEL_QUEUE))
252 return -NF_QUEUE - 1;
254 if (!strcmp(target_name, LABEL_RETURN))
260 static gboolean is_builtin_target(const char *target_name)
262 if (!strcmp(target_name, LABEL_ACCEPT) ||
263 !strcmp(target_name, LABEL_DROP) ||
264 !strcmp(target_name, LABEL_QUEUE) ||
265 !strcmp(target_name, LABEL_RETURN))
271 static gboolean is_jump(struct connman_iptables_entry *e)
273 struct xt_entry_target *target;
275 target = ipt_get_target(e->entry);
277 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
278 struct xt_standard_target *t;
280 t = (struct xt_standard_target *)target;
282 switch (t->verdict) {
298 static gboolean is_fallthrough(struct connman_iptables_entry *e)
300 struct xt_entry_target *target;
302 target = ipt_get_target(e->entry);
303 if (!strcmp(target->u.user.name, ""))
309 static gboolean is_chain(struct connman_iptables *table,
310 struct connman_iptables_entry *e)
312 struct ipt_entry *entry;
313 struct xt_entry_target *target;
319 target = ipt_get_target(entry);
320 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
326 static GList *find_chain_head(struct connman_iptables *table,
327 const char *chain_name)
330 struct connman_iptables_entry *head;
331 struct ipt_entry *entry;
332 struct xt_entry_target *target;
335 for (list = table->entries; list; list = list->next) {
340 builtin = head->builtin;
341 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
344 /* User defined chain */
345 target = ipt_get_target(entry);
346 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
347 !strcmp((char *)target->data, chain_name))
354 static GList *find_chain_tail(struct connman_iptables *table,
355 const char *chain_name)
357 struct connman_iptables_entry *tail;
358 GList *chain_head, *list;
360 chain_head = find_chain_head(table, chain_name);
361 if (chain_head == NULL)
364 /* Then we look for the next chain */
365 for (list = chain_head->next; list; list = list->next) {
368 if (is_chain(table, tail))
372 /* Nothing found, we return the table end */
373 return g_list_last(table->entries);
377 static void update_offsets(struct connman_iptables *table)
380 struct connman_iptables_entry *entry, *prev_entry;
382 for (list = table->entries; list; list = list->next) {
385 if (list == table->entries) {
392 prev_entry = prev->data;
394 entry->offset = prev_entry->offset +
395 prev_entry->entry->next_offset;
399 static void update_targets_reference(struct connman_iptables *table,
400 struct connman_iptables_entry *entry_before,
401 struct connman_iptables_entry *modified_entry,
402 gboolean is_removing)
404 struct connman_iptables_entry *tmp;
405 struct xt_standard_target *t;
409 offset = modified_entry->entry->next_offset;
411 for (list = table->entries; list; list = list->next) {
417 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
419 if (is_removing == TRUE) {
420 if (t->verdict >= entry_before->offset)
421 t->verdict -= offset;
423 if (t->verdict > entry_before->offset)
424 t->verdict += offset;
428 if (is_fallthrough(modified_entry)) {
429 t = (struct xt_standard_target *) ipt_get_target(modified_entry->entry);
431 t->verdict = entry_before->offset +
432 modified_entry->entry->target_offset +
433 ALIGN(sizeof(struct xt_standard_target));
434 t->target.u.target_size =
435 ALIGN(sizeof(struct xt_standard_target));
439 static int iptables_add_entry(struct connman_iptables *table,
440 struct ipt_entry *entry, GList *before,
443 struct connman_iptables_entry *e, *entry_before;
448 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
453 e->builtin = builtin;
455 table->entries = g_list_insert_before(table->entries, before, e);
456 table->num_entries++;
457 table->size += entry->next_offset;
459 if (before == NULL) {
460 e->offset = table->size - entry->next_offset;
465 entry_before = before->data;
468 * We've just appended/insterted a new entry. All references
469 * should be bumped accordingly.
471 update_targets_reference(table, entry_before, e, FALSE);
473 update_offsets(table);
478 static int remove_table_entry(struct connman_iptables *table,
479 struct connman_iptables_entry *entry)
483 table->num_entries--;
484 table->size -= entry->entry->next_offset;
485 removed = entry->entry->next_offset;
487 g_free(entry->entry);
489 table->entries = g_list_remove(table->entries, entry);
494 static int iptables_flush_chain(struct connman_iptables *table,
497 GList *chain_head, *chain_tail, *list, *next;
498 struct connman_iptables_entry *entry;
499 int builtin, removed = 0;
501 chain_head = find_chain_head(table, name);
502 if (chain_head == NULL)
505 chain_tail = find_chain_tail(table, name);
506 if (chain_tail == NULL)
509 entry = chain_head->data;
510 builtin = entry->builtin;
515 list = chain_head->next;
517 if (list == chain_tail->prev)
520 while (list != chain_tail->prev) {
522 next = g_list_next(list);
524 removed += remove_table_entry(table, entry);
530 struct connman_iptables_entry *e;
534 entry->builtin = builtin;
536 table->underflow[builtin] -= removed;
538 for (list = chain_tail; list; list = list->next) {
541 builtin = e->builtin;
545 table->hook_entry[builtin] -= removed;
546 table->underflow[builtin] -= removed;
550 update_offsets(table);
555 static int iptables_add_chain(struct connman_iptables *table,
559 struct ipt_entry *entry_head;
560 struct ipt_entry *entry_return;
561 struct error_target *error;
562 struct ipt_standard_target *standard;
563 u_int16_t entry_head_size, entry_return_size;
565 last = g_list_last(table->entries);
568 * An empty chain is composed of:
569 * - A head entry, with no match and an error target.
570 * The error target data is the chain name.
571 * - A tail entry, with no match and a standard target.
572 * The standard target verdict is XT_RETURN (return to the
577 entry_head_size = sizeof(struct ipt_entry) +
578 sizeof(struct error_target);
579 entry_head = g_try_malloc0(entry_head_size);
580 if (entry_head == NULL)
583 memset(entry_head, 0, entry_head_size);
585 entry_head->target_offset = sizeof(struct ipt_entry);
586 entry_head->next_offset = entry_head_size;
588 error = (struct error_target *) entry_head->elems;
589 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
590 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
591 strcpy(error->error, name);
593 if (iptables_add_entry(table, entry_head, last, -1) < 0)
597 entry_return_size = sizeof(struct ipt_entry) +
598 sizeof(struct ipt_standard_target);
599 entry_return = g_try_malloc0(entry_return_size);
600 if (entry_return == NULL)
603 memset(entry_return, 0, entry_return_size);
605 entry_return->target_offset = sizeof(struct ipt_entry);
606 entry_return->next_offset = entry_return_size;
608 standard = (struct ipt_standard_target *) entry_return->elems;
609 standard->target.u.user.target_size =
610 ALIGN(sizeof(struct ipt_standard_target));
611 standard->verdict = XT_RETURN;
613 if (iptables_add_entry(table, entry_return, last, -1) < 0)
619 g_free(entry_return);
626 static int iptables_delete_chain(struct connman_iptables *table,
629 struct connman_iptables_entry *entry;
630 GList *chain_head, *chain_tail;
632 chain_head = find_chain_head(table, name);
633 if (chain_head == NULL)
636 entry = chain_head->data;
638 /* We cannot remove builtin chain */
639 if (entry->builtin >= 0)
642 chain_tail = find_chain_tail(table, name);
643 if (chain_tail == NULL)
646 /* Chain must be flushed */
647 if (chain_head->next != chain_tail->prev)
650 remove_table_entry(table, entry);
652 entry = chain_tail->prev->data;
653 remove_table_entry(table, entry);
655 update_offsets(table);
660 static struct ipt_entry *new_rule(struct ipt_ip *ip,
661 const char *target_name, struct xtables_target *xt_t,
662 struct xtables_rule_match *xt_rm)
664 struct xtables_rule_match *tmp_xt_rm;
665 struct ipt_entry *new_entry;
666 size_t match_size, target_size;
669 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
670 match_size += tmp_xt_rm->match->m->u.match_size;
673 target_size = ALIGN(xt_t->t->u.target_size);
675 target_size = ALIGN(sizeof(struct xt_standard_target));
677 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
679 if (new_entry == NULL)
682 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
684 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
685 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
689 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
690 tmp_xt_rm = tmp_xt_rm->next) {
691 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
692 tmp_xt_rm->match->m->u.match_size);
693 match_size += tmp_xt_rm->match->m->u.match_size;
697 struct xt_entry_target *entry_target;
699 entry_target = ipt_get_target(new_entry);
700 memcpy(entry_target, xt_t->t, target_size);
706 static void update_hooks(struct connman_iptables *table, GList *chain_head,
707 struct ipt_entry *entry)
710 struct connman_iptables_entry *head, *e;
713 if (chain_head == NULL)
716 head = chain_head->data;
718 builtin = head->builtin;
722 table->underflow[builtin] += entry->next_offset;
724 for (list = chain_head->next; list; list = list->next) {
727 builtin = e->builtin;
731 table->hook_entry[builtin] += entry->next_offset;
732 table->underflow[builtin] += entry->next_offset;
736 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
737 struct ipt_ip *ip, const char *chain_name,
738 const char *target_name,
739 struct xtables_target *xt_t,
740 int *builtin, struct xtables_rule_match *xt_rm)
742 GList *chain_tail, *chain_head;
743 struct ipt_entry *new_entry;
744 struct connman_iptables_entry *head;
746 chain_head = find_chain_head(table, chain_name);
747 if (chain_head == NULL)
750 chain_tail = find_chain_tail(table, chain_name);
751 if (chain_tail == NULL)
754 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
755 if (new_entry == NULL)
758 update_hooks(table, chain_head, new_entry);
761 * If the chain is builtin, and does not have any rule,
762 * then the one that we're inserting is becoming the head
763 * and thus needs the builtin flag.
765 head = chain_head->data;
766 if (head->builtin < 0)
768 else if (chain_head == chain_tail->prev) {
769 *builtin = head->builtin;
776 static int iptables_insert_rule(struct connman_iptables *table,
777 struct ipt_ip *ip, const char *chain_name,
778 const char *target_name,
779 struct xtables_target *xt_t,
780 struct xtables_rule_match *xt_rm)
782 struct ipt_entry *new_entry;
783 int builtin = -1, ret;
786 chain_head = find_chain_head(table, chain_name);
787 if (chain_head == NULL)
790 new_entry = prepare_rule_inclusion(table, ip, chain_name,
791 target_name, xt_t, &builtin, xt_rm);
792 if (new_entry == NULL)
796 chain_head = chain_head->next;
798 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
805 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
806 struct ipt_entry *i_e2)
808 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
811 if (i_e1->target_offset != i_e2->target_offset)
814 if (i_e1->next_offset != i_e2->next_offset)
820 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
821 struct xt_entry_target *xt_e_t2)
825 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
828 if (strcmp(xt_e_t1->u.user.name, "") == 0 &&
829 strcmp(xt_e_t2->u.user.name, "") == 0) {
832 } else if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
833 struct xt_standard_target *xt_s_t1;
834 struct xt_standard_target *xt_s_t2;
836 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
837 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
839 if (xt_s_t1->verdict != xt_s_t2->verdict)
842 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
845 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
848 for (i = 0; i < xt_e_t1->u.target_size -
849 sizeof(struct xt_standard_target); i++) {
850 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
858 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
859 struct xt_entry_match *xt_e_m2)
863 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
866 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
869 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
872 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
875 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
877 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
884 static GList *find_existing_rule(struct connman_iptables *table,
885 struct ipt_ip *ip, const char *chain_name,
886 const char *target_name,
887 struct xtables_target *xt_t,
888 struct xtables_match *xt_m,
889 struct xtables_rule_match *xt_rm)
891 GList *chain_tail, *chain_head, *list;
892 struct xt_entry_target *xt_e_t = NULL;
893 struct xt_entry_match *xt_e_m = NULL;
894 struct connman_iptables_entry *entry;
895 struct ipt_entry *entry_test;
898 chain_head = find_chain_head(table, chain_name);
899 if (chain_head == NULL)
902 chain_tail = find_chain_tail(table, chain_name);
903 if (chain_tail == NULL)
909 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
910 if (entry_test == NULL)
914 xt_e_t = ipt_get_target(entry_test);
916 xt_e_m = (struct xt_entry_match *)entry_test->elems;
918 entry = chain_head->data;
919 builtin = entry->builtin;
924 list = chain_head->next;
926 for (; list != chain_tail->prev; list = list->next) {
927 struct connman_iptables_entry *tmp;
928 struct ipt_entry *tmp_e;
933 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
937 struct xt_entry_target *tmp_xt_e_t;
939 tmp_xt_e_t = ipt_get_target(tmp_e);
941 if (!is_same_target(tmp_xt_e_t, xt_e_t))
946 struct xt_entry_match *tmp_xt_e_m;
948 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
950 if (!is_same_match(tmp_xt_e_m, xt_e_m))
959 if (list != chain_tail->prev)
965 static int iptables_delete_rule(struct connman_iptables *table,
966 struct ipt_ip *ip, const char *chain_name,
967 const char *target_name,
968 struct xtables_target *xt_t,
969 struct xtables_match *xt_m,
970 struct xtables_rule_match *xt_rm)
972 struct connman_iptables_entry *entry;
973 GList *chain_head, *chain_tail, *list;
974 int builtin, removed;
978 chain_head = find_chain_head(table, chain_name);
979 if (chain_head == NULL)
982 chain_tail = find_chain_tail(table, chain_name);
983 if (chain_tail == NULL)
986 list = find_existing_rule(table, ip, chain_name, target_name,
991 entry = chain_head->data;
992 builtin = entry->builtin;
998 /* We have deleted a rule,
999 * all references should be bumped accordingly */
1000 if (list->next != NULL)
1001 update_targets_reference(table, list->next->data,
1004 removed += remove_table_entry(table, entry);
1010 entry->builtin = builtin;
1013 table->underflow[builtin] -= removed;
1014 for (list = chain_tail; list; list = list->next) {
1017 builtin = entry->builtin;
1021 table->hook_entry[builtin] -= removed;
1022 table->underflow[builtin] -= removed;
1026 update_offsets(table);
1031 static int iptables_change_policy(struct connman_iptables *table,
1032 const char *chain_name, const char *policy)
1035 struct connman_iptables_entry *entry;
1036 struct xt_entry_target *target;
1037 struct xt_standard_target *t;
1040 verdict = target_to_verdict(policy);
1044 chain_head = find_chain_head(table, chain_name);
1045 if (chain_head == NULL)
1048 entry = chain_head->data;
1049 if (entry->builtin < 0)
1052 target = ipt_get_target(entry->entry);
1054 t = (struct xt_standard_target *)target;
1055 t->verdict = verdict;
1060 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1062 struct ipt_replace *r;
1064 struct connman_iptables_entry *e;
1065 unsigned char *entry_index;
1067 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1071 memset(r, 0, sizeof(*r) + table->size);
1073 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1074 * table->old_entries);
1075 if (r->counters == NULL) {
1080 strcpy(r->name, table->info->name);
1081 r->num_entries = table->num_entries;
1082 r->size = table->size;
1084 r->num_counters = table->old_entries;
1085 r->valid_hooks = table->info->valid_hooks;
1087 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1088 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1090 entry_index = (unsigned char *)r->entries;
1091 for (list = table->entries; list; list = list->next) {
1094 memcpy(entry_index, e->entry, e->entry->next_offset);
1095 entry_index += e->entry->next_offset;
1101 static void dump_ip(struct ipt_entry *entry)
1103 struct ipt_ip *ip = &entry->ip;
1104 char ip_string[INET6_ADDRSTRLEN];
1105 char ip_mask[INET6_ADDRSTRLEN];
1107 if (strlen(ip->iniface))
1108 DBG("\tin %s", ip->iniface);
1110 if (strlen(ip->outiface))
1111 DBG("\tout %s", ip->outiface);
1113 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1114 inet_ntop(AF_INET, &ip->smsk,
1115 ip_mask, INET6_ADDRSTRLEN) != NULL)
1116 DBG("\tsrc %s/%s", ip_string, ip_mask);
1118 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1119 inet_ntop(AF_INET, &ip->dmsk,
1120 ip_mask, INET6_ADDRSTRLEN) != NULL)
1121 DBG("\tdst %s/%s", ip_string, ip_mask);
1124 static void dump_target(struct ipt_entry *entry)
1127 struct xtables_target *xt_t;
1128 struct xt_entry_target *target;
1130 target = ipt_get_target(entry);
1132 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1133 struct xt_standard_target *t;
1135 t = (struct xt_standard_target *)target;
1137 switch (t->verdict) {
1139 DBG("\ttarget RETURN");
1142 case -NF_ACCEPT - 1:
1143 DBG("\ttarget ACCEPT");
1147 DBG("\ttarget DROP");
1151 DBG("\ttarget QUEUE");
1155 DBG("\ttarget STOP");
1159 DBG("\tJUMP %u", t->verdict);
1163 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1164 XTF_LOAD_MUST_SUCCEED);
1166 if(xt_t->print != NULL)
1167 xt_t->print(NULL, target, 1);
1169 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1171 DBG("\ttarget %s", target->u.user.name);
1175 if(xt_t->print != NULL) {
1177 xt_t->print(NULL, target, 1);
1182 static void dump_match(struct ipt_entry *entry)
1184 struct xtables_match *xt_m;
1185 struct xt_entry_match *match;
1187 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1190 match = (struct xt_entry_match *) entry->elems;
1192 if (!strlen(match->u.user.name))
1195 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1199 if(xt_m->print != NULL) {
1201 xt_m->print(NULL, match, 1);
1207 DBG("\tmatch %s", match->u.user.name);
1211 static int dump_entry(struct ipt_entry *entry, int builtin,
1212 unsigned int hook, size_t size, unsigned int offset,
1215 struct xt_entry_target *target;
1217 target = ipt_get_target(entry);
1219 if (offset + entry->next_offset == size) {
1220 DBG("\tEnd of CHAIN");
1224 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1225 DBG("\tUSER CHAIN (%s) match %p target %p",
1226 target->data, entry->elems,
1227 (char *)entry + entry->target_offset);
1230 } else if (builtin >= 0) {
1231 DBG("\tCHAIN (%s) match %p target %p",
1232 hooknames[builtin], entry->elems,
1233 (char *)entry + entry->target_offset);
1235 DBG("\tRULE match %p target %p",
1237 (char *)entry + entry->target_offset);
1247 static void dump_table(struct connman_iptables *table)
1249 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1251 table->info->valid_hooks, table->info->num_entries,
1254 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1255 table->info->hook_entry[NF_IP_PRE_ROUTING],
1256 table->info->hook_entry[NF_IP_LOCAL_IN],
1257 table->info->hook_entry[NF_IP_FORWARD],
1258 table->info->hook_entry[NF_IP_LOCAL_OUT],
1259 table->info->hook_entry[NF_IP_POST_ROUTING]);
1260 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1261 table->info->underflow[NF_IP_PRE_ROUTING],
1262 table->info->underflow[NF_IP_LOCAL_IN],
1263 table->info->underflow[NF_IP_FORWARD],
1264 table->info->underflow[NF_IP_LOCAL_OUT],
1265 table->info->underflow[NF_IP_POST_ROUTING]);
1267 iterate_entries(table->blob_entries->entrytable,
1268 table->info->valid_hooks,
1269 table->info->hook_entry,
1270 table->blob_entries->size,
1271 print_entry, dump_entry);
1274 static void dump_ipt_replace(struct ipt_replace *repl)
1276 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1277 repl->name, repl->valid_hooks, repl->num_entries,
1280 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1281 repl->hook_entry[NF_IP_PRE_ROUTING],
1282 repl->hook_entry[NF_IP_LOCAL_IN],
1283 repl->hook_entry[NF_IP_FORWARD],
1284 repl->hook_entry[NF_IP_LOCAL_OUT],
1285 repl->hook_entry[NF_IP_POST_ROUTING]);
1286 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1287 repl->underflow[NF_IP_PRE_ROUTING],
1288 repl->underflow[NF_IP_LOCAL_IN],
1289 repl->underflow[NF_IP_FORWARD],
1290 repl->underflow[NF_IP_LOCAL_OUT],
1291 repl->underflow[NF_IP_POST_ROUTING]);
1293 iterate_entries(repl->entries, repl->valid_hooks,
1294 repl->hook_entry, repl->size, print_entry, dump_entry);
1297 static int iptables_get_entries(struct connman_iptables *table)
1299 socklen_t entry_size;
1301 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1303 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1304 table->blob_entries, &entry_size);
1307 static int iptables_replace(struct connman_iptables *table,
1308 struct ipt_replace *r)
1310 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1311 sizeof(*r) + r->size);
1314 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1315 size_t size, unsigned offset, void *user_data)
1317 struct connman_iptables *table = user_data;
1318 struct ipt_entry *new_entry;
1320 new_entry = g_try_malloc0(entry->next_offset);
1321 if (new_entry == NULL)
1324 memcpy(new_entry, entry, entry->next_offset);
1326 return iptables_add_entry(table, new_entry, NULL, builtin);
1329 static void table_cleanup(struct connman_iptables *table)
1332 struct connman_iptables_entry *entry;
1337 if (table->ipt_sock >= 0)
1338 close(table->ipt_sock);
1340 for (list = table->entries; list; list = list->next) {
1343 g_free(entry->entry);
1347 g_list_free(table->entries);
1348 g_free(table->info);
1349 g_free(table->blob_entries);
1353 static struct connman_iptables *iptables_init(const char *table_name)
1355 struct connman_iptables *table = NULL;
1356 char *module = NULL;
1359 if (table_name == NULL)
1360 table_name = "filter";
1362 DBG("%s", table_name);
1364 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1365 DBG("ip_tables module loading gives error but trying anyway");
1367 module = g_strconcat("iptable_", table_name, NULL);
1371 if (xtables_insmod(module, NULL, TRUE) != 0)
1372 DBG("%s module loading gives error but trying anyway", module);
1376 table = g_hash_table_lookup(table_hash, table_name);
1380 table = g_try_new0(struct connman_iptables, 1);
1384 table->info = g_try_new0(struct ipt_getinfo, 1);
1385 if (table->info == NULL)
1388 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1389 if (table->ipt_sock < 0)
1392 s = sizeof(*table->info);
1393 strcpy(table->info->name, table_name);
1394 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1395 table->info, &s) < 0) {
1396 connman_error("iptables support missing error %d (%s)", errno,
1401 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1403 if (table->blob_entries == NULL)
1406 strcpy(table->blob_entries->name, table_name);
1407 table->blob_entries->size = table->info->size;
1409 if (iptables_get_entries(table) < 0)
1412 table->num_entries = 0;
1413 table->old_entries = table->info->num_entries;
1416 memcpy(table->underflow, table->info->underflow,
1417 sizeof(table->info->underflow));
1418 memcpy(table->hook_entry, table->info->hook_entry,
1419 sizeof(table->info->hook_entry));
1421 iterate_entries(table->blob_entries->entrytable,
1422 table->info->valid_hooks, table->info->hook_entry,
1423 table->blob_entries->size, add_entry, table);
1425 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1427 if (debug_enabled == TRUE)
1433 table_cleanup(table);
1438 static struct option iptables_opts[] = {
1439 {.name = "append", .has_arg = 1, .val = 'A'},
1440 {.name = "compare", .has_arg = 1, .val = 'C'},
1441 {.name = "delete", .has_arg = 1, .val = 'D'},
1442 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1443 {.name = "insert", .has_arg = 1, .val = 'I'},
1444 {.name = "list", .has_arg = 2, .val = 'L'},
1445 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1446 {.name = "policy", .has_arg = 1, .val = 'P'},
1447 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1448 {.name = "destination", .has_arg = 1, .val = 'd'},
1449 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1450 {.name = "jump", .has_arg = 1, .val = 'j'},
1451 {.name = "match", .has_arg = 1, .val = 'm'},
1452 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1453 {.name = "source", .has_arg = 1, .val = 's'},
1454 {.name = "table", .has_arg = 1, .val = 't'},
1458 struct xtables_globals iptables_globals = {
1460 .opts = iptables_opts,
1461 .orig_opts = iptables_opts,
1464 static struct xtables_target *prepare_target(struct connman_iptables *table,
1465 const char *target_name)
1467 struct xtables_target *xt_t = NULL;
1468 gboolean is_builtin, is_user_defined;
1469 GList *chain_head = NULL;
1473 is_user_defined = FALSE;
1475 if (is_builtin_target(target_name))
1478 chain_head = find_chain_head(table, target_name);
1479 if (chain_head != NULL && chain_head->next != NULL)
1480 is_user_defined = TRUE;
1483 if (is_builtin || is_user_defined)
1484 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1485 XTF_LOAD_MUST_SUCCEED);
1487 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1492 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1494 xt_t->t = g_try_malloc0(target_size);
1495 if (xt_t->t == NULL)
1498 xt_t->t->u.target_size = target_size;
1500 if (is_builtin || is_user_defined) {
1501 struct xt_standard_target *target;
1503 target = (struct xt_standard_target *)(xt_t->t);
1504 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1506 if (is_builtin == TRUE)
1507 target->verdict = target_to_verdict(target_name);
1508 else if (is_user_defined == TRUE) {
1509 struct connman_iptables_entry *target_rule;
1511 if (chain_head == NULL) {
1516 target_rule = chain_head->next->data;
1517 target->verdict = target_rule->offset;
1520 strcpy(xt_t->t->u.user.name, target_name);
1521 xt_t->t->u.user.revision = xt_t->revision;
1522 if (xt_t->init != NULL)
1523 xt_t->init(xt_t->t);
1526 #if XTABLES_VERSION_CODE > 5
1527 if (xt_t->x6_options != NULL)
1528 iptables_globals.opts =
1529 xtables_options_xfrm(
1530 iptables_globals.orig_opts,
1531 iptables_globals.opts,
1533 &xt_t->option_offset);
1536 iptables_globals.opts =
1537 xtables_merge_options(
1538 #if XTABLES_VERSION_CODE > 5
1539 iptables_globals.orig_opts,
1541 iptables_globals.opts,
1543 &xt_t->option_offset);
1545 if (iptables_globals.opts == NULL) {
1553 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1554 struct xtables_rule_match **xt_rm,
1555 const char *match_name)
1557 struct xtables_match *xt_m;
1560 if (match_name == NULL)
1563 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1564 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1566 xt_m->m = g_try_malloc0(match_size);
1567 if (xt_m->m == NULL)
1570 xt_m->m->u.match_size = match_size;
1571 strcpy(xt_m->m->u.user.name, xt_m->name);
1572 xt_m->m->u.user.revision = xt_m->revision;
1574 if (xt_m->init != NULL)
1575 xt_m->init(xt_m->m);
1577 if (xt_m == xt_m->next)
1580 #if XTABLES_VERSION_CODE > 5
1581 if (xt_m->x6_options != NULL)
1582 iptables_globals.opts =
1583 xtables_options_xfrm(
1584 iptables_globals.orig_opts,
1585 iptables_globals.opts,
1587 &xt_m->option_offset);
1590 iptables_globals.opts =
1591 xtables_merge_options(
1592 #if XTABLES_VERSION_CODE > 5
1593 iptables_globals.orig_opts,
1595 iptables_globals.opts,
1597 &xt_m->option_offset);
1599 if (iptables_globals.opts == NULL) {
1608 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1611 uint32_t prefixlength;
1615 tokens = g_strsplit(str, "/", 2);
1619 if (!inet_pton(AF_INET, tokens[0], ip)) {
1624 if (tokens[1] != NULL) {
1625 prefixlength = strtol(tokens[1], NULL, 10);
1626 if (prefixlength > 31) {
1631 tmp = ~(0xffffffff >> prefixlength);
1636 mask->s_addr = htonl(tmp);
1637 ip->s_addr = ip->s_addr & mask->s_addr;
1645 static struct connman_iptables *pre_load_table(const char *table_name,
1646 struct connman_iptables *table)
1651 return iptables_init(table_name);
1654 static void clear_tables_flags(void)
1656 struct xtables_match *xt_m;
1657 struct xtables_target *xt_t;
1660 * Clear all flags because the flags are only valid
1663 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1666 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
1672 struct parse_context {
1676 struct xtables_target *xt_t;
1677 struct xtables_match *xt_m;
1678 struct xtables_rule_match *xt_rm;
1681 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1686 tokens = g_strsplit_set(str, " ", -1);
1688 for (i = 0; tokens[i]; i++);
1690 /* Add space for the argv[0] value */
1693 /* Don't forget the last NULL entry */
1694 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1695 if (ctx->argv == NULL) {
1701 * getopt_long() jumps over the first token; we need to add some
1702 * random argv[0] entry.
1704 ctx->argv[0] = g_strdup("argh");
1705 for (i = 1; i < ctx->argc; i++)
1706 ctx->argv[i] = tokens[i - 1];
1711 #if XTABLES_VERSION_CODE > 5
1713 static int parse_xt_modules(int c, connman_bool_t invert,
1714 struct parse_context *ctx)
1716 struct xtables_match *m;
1717 struct xtables_rule_match *rm;
1719 DBG("xtables version code > 5");
1721 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1722 if (rm->completed != 0)
1727 if (m->x6_parse == NULL && m->parse == NULL)
1730 if (c < (int) m->option_offset ||
1731 c >= (int) m->option_offset
1732 + XT_OPTION_OFFSET_SCALE)
1735 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1738 if (ctx->xt_t == NULL)
1741 if (ctx->xt_t->x6_parse == NULL && ctx->xt_t->parse == NULL)
1744 if (c < (int) ctx->xt_t->option_offset ||
1745 c >= (int) ctx->xt_t->option_offset
1746 + XT_OPTION_OFFSET_SCALE)
1749 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1754 static int final_check_xt_modules(struct parse_context *ctx)
1756 struct xtables_rule_match *rm;
1758 DBG("xtables version code > 5");
1760 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1761 xtables_option_mfcall(rm->match);
1763 if (ctx->xt_t != NULL)
1764 xtables_option_tfcall(ctx->xt_t);
1771 static int parse_xt_modules(int c, connman_bool_t invert,
1772 struct parse_context *ctx)
1774 struct xtables_match *m;
1775 struct xtables_rule_match *rm;
1778 DBG("xtables version code <= 5");
1780 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next) {
1781 if (rm->completed == 1)
1786 if (m->parse == NULL)
1789 err = m->parse(c - m->option_offset,
1790 argv, invert, &m->mflags,
1796 if (ctx->xt_t == NULL)
1799 if (ctx->xt_t->parse == NULL)
1802 err = ctx->xt_m->parse(c - ctx->xt_m->option_offset,
1803 ctx->argv, invert, &ctx->xt_m->mflags,
1804 NULL, &ctx->xt_m->m);
1808 static int final_check_xt_modules(struct parse_context *ctx)
1810 struct xtables_rule_match *rm;
1812 DBG("xtables version code <= 5");
1814 for (rm = ctx->xt_rm; rm != NULL; rm = rm->next)
1815 if (rm->match->final_check != NULL)
1816 rm->match->final_check(rm->match->mflags);
1818 if (ctx->xt_t != NULL && ctx->xt_t->final_check != NULL)
1819 ctx->xt_t->final_check(ctx->xt_t->tflags);
1826 static int parse_rule_spec(struct connman_iptables *table,
1827 struct parse_context *ctx)
1830 * How the parser works:
1832 * - If getopt finds 's', 'd', 'i', 'o'.
1833 * just extract the information.
1834 * - if '!' is found, set the invert flag to true and
1835 * removes the '!' from the optarg string and jumps
1836 * back to getopt to reparse the current optarg string.
1837 * After reparsing the invert flag is reseted to false.
1838 * - If 'm' or 'j' is found then call either
1839 * prepare_matches() or prepare_target(). Those function
1840 * will modify (extend) the longopts for getopt_long.
1841 * That means getopt will change its matching context according
1842 * the loaded target.
1844 * Here an example with iptables-test
1846 * argv[0] = ./tools/iptables-test
1858 * getopt found 'm' then the optarg is "mark" and optind 7
1859 * The longopts array containts before hitting the `case 'm'`
1861 * val A has_arg 1 name append
1862 * val C has_arg 1 name compare
1863 * val D has_arg 1 name delete
1864 * val F has_arg 1 name flush-chain
1865 * val I has_arg 1 name insert
1866 * val L has_arg 2 name list
1867 * val N has_arg 1 name new-chain
1868 * val P has_arg 1 name policy
1869 * val X has_arg 1 name delete-chain
1870 * val d has_arg 1 name destination
1871 * val i has_arg 1 name in-interface
1872 * val j has_arg 1 name jump
1873 * val m has_arg 1 name match
1874 * val o has_arg 1 name out-interface
1875 * val s has_arg 1 name source
1876 * val t has_arg 1 name table
1878 * After executing the `case 'm'` block longopts is
1880 * val A has_arg 1 name append
1881 * val C has_arg 1 name compare
1882 * val D has_arg 1 name delete
1883 * val F has_arg 1 name flush-chain
1884 * val I has_arg 1 name insert
1885 * val L has_arg 2 name list
1886 * val N has_arg 1 name new-chain
1887 * val P has_arg 1 name policy
1888 * val X has_arg 1 name delete-chain
1889 * val d has_arg 1 name destination
1890 * val i has_arg 1 name in-interface
1891 * val j has_arg 1 name jump
1892 * val m has_arg 1 name match
1893 * val o has_arg 1 name out-interface
1894 * val s has_arg 1 name source
1895 * val t has_arg 1 name table
1896 * val has_arg 1 name mark
1898 * So the 'mark' matcher has added the 'mark' options
1899 * and getopt will then return c '256' optarg "999" optind 9
1900 * And we will hit the 'default' statement which then
1901 * will call the matchers parser (xt_m->parser() or
1902 * xtables_option_mpcall() depending on which version
1903 * of libxtables is found.
1905 connman_bool_t invert = FALSE;
1910 ctx->ip = g_try_new0(struct ipt_ip, 1);
1911 if (ctx->ip == NULL)
1915 * As side effect parsing a rule sets some global flags
1916 * which will be evaluated/verified. Let's reset them
1917 * to ensure we can parse more than one rule.
1919 clear_tables_flags();
1922 * Tell getopt_long not to generate error messages for unknown
1923 * options and also reset optind back to 0.
1928 while ((c = getopt_long(ctx->argc, ctx->argv,
1930 iptables_globals.opts, NULL)) != -1) {
1933 /* Source specification */
1934 if (!parse_ip_and_mask(optarg,
1940 ctx->ip->invflags |= IPT_INV_SRCIP;
1944 /* Destination specification */
1945 if (!parse_ip_and_mask(optarg,
1951 ctx->ip->invflags |= IPT_INV_DSTIP;
1954 /* In interface specification */
1955 len = strlen(optarg);
1957 if (len + 1 > IFNAMSIZ)
1960 strcpy(ctx->ip->iniface, optarg);
1961 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1964 ctx->ip->invflags |= IPT_INV_VIA_IN;
1968 /* Out interface specification */
1969 len = strlen(optarg);
1971 if (len + 1 > IFNAMSIZ)
1974 strcpy(ctx->ip->outiface, optarg);
1975 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1978 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1983 ctx->xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1984 if (ctx->xt_m == NULL) {
1992 ctx->xt_t = prepare_target(table, optarg);
1993 if (ctx->xt_t == NULL) {
2000 if (optarg[0] == '!' && optarg[1] == '\0') {
2003 /* Remove the '!' from the optarg */
2007 * And recall getopt_long without reseting
2015 err = parse_xt_modules(c, invert, ctx);
2025 err = final_check_xt_modules(ctx);
2031 static void cleanup_parse_context(struct parse_context *ctx)
2033 g_strfreev(ctx->argv);
2035 if (ctx->xt_t != NULL)
2036 g_free(ctx->xt_t->t);
2037 if (ctx->xt_m != NULL)
2038 g_free(ctx->xt_m->m);
2042 int __connman_iptables_new_chain(const char *table_name,
2045 struct connman_iptables *table;
2047 DBG("-t %s -N %s", table_name, chain);
2049 table = pre_load_table(table_name, NULL);
2053 return iptables_add_chain(table, chain);
2056 int __connman_iptables_delete_chain(const char *table_name,
2059 struct connman_iptables *table;
2061 DBG("-t %s -X %s", table_name, chain);
2063 table = pre_load_table(table_name, NULL);
2067 return iptables_delete_chain(table, chain);
2070 int __connman_iptables_flush_chain(const char *table_name,
2073 struct connman_iptables *table;
2075 DBG("-t %s -F %s", table_name, chain);
2077 table = pre_load_table(table_name, NULL);
2081 return iptables_flush_chain(table, chain);
2084 int __connman_iptables_change_policy(const char *table_name,
2088 struct connman_iptables *table;
2090 DBG("-t %s -F %s", table_name, chain);
2092 table = pre_load_table(table_name, NULL);
2096 return iptables_change_policy(table, chain, policy);
2099 int __connman_iptables_append(const char *table_name,
2101 const char *rule_spec)
2103 struct connman_iptables *table;
2104 struct parse_context *ctx;
2105 const char *target_name;
2108 ctx = g_try_new0(struct parse_context, 1);
2112 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2114 err = prepare_getopt_args(rule_spec, ctx);
2118 table = pre_load_table(table_name, NULL);
2119 if (table == NULL) {
2124 err = parse_rule_spec(table, ctx);
2128 if (ctx->xt_t == NULL)
2131 target_name = ctx->xt_t->name;
2133 err = iptables_insert_rule(table, ctx->ip, chain,
2134 target_name, ctx->xt_t, ctx->xt_rm);
2136 cleanup_parse_context(ctx);
2141 int __connman_iptables_delete(const char *table_name,
2143 const char *rule_spec)
2145 struct connman_iptables *table;
2146 struct parse_context *ctx;
2147 const char *target_name;
2150 ctx = g_try_new0(struct parse_context, 1);
2154 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2156 err = prepare_getopt_args(rule_spec, ctx);
2160 table = pre_load_table(table_name, NULL);
2161 if (table == NULL) {
2166 err = parse_rule_spec(table, ctx);
2170 if (ctx->xt_t == NULL)
2173 target_name = ctx->xt_t->name;
2175 err = iptables_delete_rule(table, ctx->ip, chain,
2176 target_name, ctx->xt_t, ctx->xt_m,
2179 cleanup_parse_context(ctx);
2184 int __connman_iptables_commit(const char *table_name)
2186 struct connman_iptables *table;
2187 struct ipt_replace *repl;
2190 DBG("%s", table_name);
2192 table = g_hash_table_lookup(table_hash, table_name);
2196 repl = iptables_blob(table);
2198 if (debug_enabled == TRUE)
2199 dump_ipt_replace(repl);
2201 err = iptables_replace(table, repl);
2203 g_free(repl->counters);
2209 g_hash_table_remove(table_hash, table_name);
2214 static void remove_table(gpointer user_data)
2216 struct connman_iptables *table = user_data;
2218 table_cleanup(table);
2221 static int flush_table_cb(struct ipt_entry *entry, int builtin,
2222 unsigned int hook, size_t size,
2223 unsigned int offset, void *user_data)
2225 GSList **chains = user_data;
2226 struct xt_entry_target *target;
2229 if (offset + entry->next_offset == size)
2232 target = ipt_get_target(entry);
2234 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
2235 name = g_strdup((const char*)target->data);
2236 else if (builtin >= 0)
2237 name = g_strdup(hooknames[builtin]);
2241 *chains = g_slist_prepend(*chains, name);
2246 static void flush_table(const char *name)
2248 GSList *chains = NULL, *list;
2249 struct connman_iptables *table;
2251 table = pre_load_table(name, NULL);
2255 iterate_entries(table->blob_entries->entrytable,
2256 table->info->valid_hooks,
2257 table->info->hook_entry,
2258 table->blob_entries->size,
2259 flush_table_cb, &chains);
2261 for (list = chains; list != NULL; list = list->next) {
2262 char *chain = list->data;
2264 DBG("chain %s", chain);
2265 iptables_flush_chain(table, chain);
2268 __connman_iptables_commit(name);
2269 g_slist_free_full(chains, g_free);
2272 static void flush_all_chains(void)
2274 flush_table("filter");
2275 flush_table("mangle");
2279 int __connman_iptables_init(void)
2283 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2284 debug_enabled = TRUE;
2286 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2287 g_free, remove_table);
2289 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2296 void __connman_iptables_cleanup(void)
2300 g_hash_table_destroy(table_hash);
2302 xtables_free_opts(1);