4 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <arpa/inet.h>
32 #include <linux/netfilter_ipv4/ip_tables.h>
36 static const char *hooknames[] = {
37 [NF_IP_PRE_ROUTING] = "PREROUTING",
38 [NF_IP_LOCAL_IN] = "INPUT",
39 [NF_IP_FORWARD] = "FORWARD",
40 [NF_IP_LOCAL_OUT] = "OUTPUT",
41 [NF_IP_POST_ROUTING] = "POSTROUTING",
44 #define LABEL_ACCEPT "ACCEPT"
45 #define LABEL_DROP "DROP"
46 #define LABEL_QUEUE "QUEUE"
47 #define LABEL_RETURN "RETURN"
49 /* fn returns 0 to continue iteration */
50 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
57 for (__i = 0, __n = 0; __i < (size); \
58 __i += __entry->next_offset, __n++) { \
59 __entry = (void *)(entries) + __i; \
63 __ret = fn(__entry, ## args); \
70 /* fn returns 0 to continue iteration */
71 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
72 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
74 #define ENTRY_ITERATE(entries, size, fn, args...) \
75 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
77 #define MIN_ALIGN (__alignof__(struct ipt_entry))
79 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
82 struct xt_entry_target t;
83 char error[IPT_TABLE_MAXNAMELEN];
86 struct connman_iptables_entry {
90 struct ipt_entry *entry;
93 struct connman_iptables {
96 struct ipt_getinfo *info;
97 struct ipt_get_entries *blob_entries;
99 unsigned int num_entries;
100 unsigned int old_entries;
103 unsigned int underflow[NF_INET_NUMHOOKS];
104 unsigned int hook_entry[NF_INET_NUMHOOKS];
110 static struct ipt_entry *get_entry(struct connman_iptables *table,
113 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
117 static int is_hook_entry(struct connman_iptables *table,
118 struct ipt_entry *entry)
122 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
123 if ((table->info->valid_hooks & (1 << i))
124 && get_entry(table, table->info->hook_entry[i]) == entry)
131 static unsigned long entry_to_offset(struct connman_iptables *table,
132 struct ipt_entry *entry)
134 return (void *)entry - (void *)table->blob_entries->entrytable;
137 static int target_to_verdict(char *target_name)
139 if (!strcmp(target_name, LABEL_ACCEPT))
140 return -NF_ACCEPT - 1;
142 if (!strcmp(target_name, LABEL_DROP))
145 if (!strcmp(target_name, LABEL_QUEUE))
146 return -NF_QUEUE - 1;
148 if (!strcmp(target_name, LABEL_RETURN))
154 static gboolean is_builtin_target(char *target_name)
156 if (!strcmp(target_name, LABEL_ACCEPT) ||
157 !strcmp(target_name, LABEL_DROP) ||
158 !strcmp(target_name, LABEL_QUEUE) ||
159 !strcmp(target_name, LABEL_RETURN))
165 static gboolean is_jump(struct connman_iptables_entry *e)
167 struct xt_entry_target *target;
169 target = ipt_get_target(e->entry);
171 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
172 struct xt_standard_target *t;
174 t = (struct xt_standard_target *)target;
176 switch (t->verdict) {
192 static gboolean is_chain(struct connman_iptables *table,
193 struct connman_iptables_entry *e)
195 struct ipt_entry *entry;
196 struct xt_entry_target *target;
202 target = ipt_get_target(entry);
203 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
209 static GList *find_chain_head(struct connman_iptables *table,
213 struct connman_iptables_entry *head;
214 struct ipt_entry *entry;
215 struct xt_entry_target *target;
218 for (list = table->entries; list; list = list->next) {
223 builtin = head->builtin;
224 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
227 /* User defined chain */
228 target = ipt_get_target(entry);
229 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
230 !strcmp((char *)target->data, chain_name))
237 static GList *find_chain_tail(struct connman_iptables *table,
240 GList *chain_head, *list;
241 struct connman_iptables_entry *tail;
243 chain_head = find_chain_head(table, chain_name);
244 if (chain_head == NULL)
247 /* Then we look for the next chain */
248 for (list = chain_head->next; list; list = list->next) {
251 if (is_chain(table, tail))
255 /* Nothing found, we return the table end */
256 return g_list_last(table->entries);
259 static void update_offsets(struct connman_iptables *table)
262 struct connman_iptables_entry *entry, *prev_entry;
264 for (list = table->entries; list; list = list->next) {
267 if (list == table->entries) {
274 prev_entry = prev->data;
276 entry->offset = prev_entry->offset +
277 prev_entry->entry->next_offset;
281 static int connman_add_entry(struct connman_iptables *table,
282 struct ipt_entry *entry, GList *before,
286 struct connman_iptables_entry *e, *tmp, *entry_before;
287 struct xt_standard_target *t;
292 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
297 e->builtin = builtin;
299 table->entries = g_list_insert_before(table->entries, before, e);
300 table->num_entries++;
301 table->size += entry->next_offset;
303 if (before == NULL) {
304 e->offset = table->size - entry->next_offset;
309 entry_before = before->data;
312 * We've just appended/insterted a new entry. All references
313 * should be bumped accordingly.
315 for (list = table->entries; list; list = list->next) {
321 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
323 if (t->verdict > entry_before->offset)
324 t->verdict += entry->next_offset;
327 update_offsets(table);
332 static int remove_table_entry(struct connman_iptables *table,
333 struct connman_iptables_entry *entry)
337 table->num_entries--;
338 table->size -= entry->entry->next_offset;
339 removed = entry->entry->next_offset;
341 g_free(entry->entry);
343 table->entries = g_list_remove(table->entries, entry);
348 static int connman_iptables_flush_chain(struct connman_iptables *table,
351 GList *chain_head, *chain_tail, *list, *next;
352 struct connman_iptables_entry *entry;
353 int builtin, removed = 0;
355 chain_head = find_chain_head(table, name);
356 if (chain_head == NULL)
359 chain_tail = find_chain_tail(table, name);
360 if (chain_tail == NULL)
363 entry = chain_head->data;
364 builtin = entry->builtin;
369 list = chain_head->next;
371 if (list == chain_tail->prev)
374 while (list != chain_tail->prev) {
376 next = g_list_next(list);
378 removed += remove_table_entry(table, entry);
384 struct connman_iptables_entry *e;
388 entry->builtin = builtin;
390 table->underflow[builtin] -= removed;
392 for (list = chain_tail; list; list = list->next) {
395 builtin = e->builtin;
399 table->hook_entry[builtin] -= removed;
400 table->underflow[builtin] -= removed;
404 update_offsets(table);
409 static int connman_iptables_delete_chain(struct connman_iptables *table,
412 GList *chain_head, *chain_tail;
413 struct connman_iptables_entry *entry;
415 chain_head = find_chain_head(table, name);
416 if (chain_head == NULL)
419 entry = chain_head->data;
421 /* We cannot remove builtin chain */
422 if (entry->builtin >= 0)
425 chain_tail = find_chain_tail(table, name);
426 if (chain_tail == NULL)
429 /* Chain must be flushed */
430 if (chain_head->next != chain_tail->prev)
433 remove_table_entry(table, entry);
435 entry = chain_tail->prev->data;
436 remove_table_entry(table, entry);
438 update_offsets(table);
443 static int connman_iptables_add_chain(struct connman_iptables *table,
447 struct ipt_entry *entry_head;
448 struct ipt_entry *entry_return;
449 struct error_target *error;
450 struct ipt_standard_target *standard;
451 u_int16_t entry_head_size, entry_return_size;
453 last = g_list_last(table->entries);
456 * An empty chain is composed of:
457 * - A head entry, with no match and an error target.
458 * The error target data is the chain name.
459 * - A tail entry, with no match and a standard target.
460 * The standard target verdict is XT_RETURN (return to the
465 entry_head_size = sizeof(struct ipt_entry) +
466 sizeof(struct error_target);
467 entry_head = g_try_malloc0(entry_head_size);
468 if (entry_head == NULL)
471 memset(entry_head, 0, entry_head_size);
473 entry_head->target_offset = sizeof(struct ipt_entry);
474 entry_head->next_offset = entry_head_size;
476 error = (struct error_target *) entry_head->elems;
477 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
478 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
479 strcpy(error->error, name);
481 if (connman_add_entry(table, entry_head, last, -1) < 0)
485 entry_return_size = sizeof(struct ipt_entry) +
486 sizeof(struct ipt_standard_target);
487 entry_return = g_try_malloc0(entry_return_size);
488 if (entry_return == NULL)
491 memset(entry_return, 0, entry_return_size);
493 entry_return->target_offset = sizeof(struct ipt_entry);
494 entry_return->next_offset = entry_return_size;
496 standard = (struct ipt_standard_target *) entry_return->elems;
497 standard->target.u.user.target_size =
498 ALIGN(sizeof(struct ipt_standard_target));
499 standard->verdict = XT_RETURN;
501 if (connman_add_entry(table, entry_return, last, -1) < 0)
507 g_free(entry_return);
514 static struct ipt_entry *
515 new_rule(struct connman_iptables *table, struct ipt_ip *ip,
516 char *target_name, struct xtables_target *xt_t,
517 char *match_name, struct xtables_match *xt_m)
519 struct ipt_entry *new_entry;
520 size_t match_size, target_size;
521 int is_builtin = is_builtin_target(target_name);
524 match_size = xt_m->m->u.match_size;
529 target_size = ALIGN(xt_t->t->u.target_size);
531 target_size = ALIGN(sizeof(struct xt_standard_target));
533 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
535 if (new_entry == NULL)
538 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
540 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
541 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
544 struct xt_entry_match *entry_match;
546 entry_match = (struct xt_entry_match *)new_entry->elems;
547 memcpy(entry_match, xt_m->m, match_size);
551 struct xt_entry_target *entry_target;
554 struct xt_standard_target *target;
556 target = (struct xt_standard_target *)(xt_t->t);
557 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
558 target->verdict = target_to_verdict(target_name);
561 entry_target = ipt_get_target(new_entry);
562 memcpy(entry_target, xt_t->t, target_size);
564 struct connman_iptables_entry *target_rule;
565 struct xt_standard_target *target;
569 * This is a user defined target, i.e. a chain jump.
570 * We search for the chain head, and the target verdict
571 * is the first rule's offset on this chain.
572 * The offset is from the beginning of the table.
575 chain_head = find_chain_head(table, target_name);
576 if (chain_head == NULL || chain_head->next == NULL) {
581 target_rule = chain_head->next->data;
583 target = (struct xt_standard_target *)ipt_get_target(new_entry);
584 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
585 target->target.u.user.target_size = target_size;
586 target->verdict = target_rule->offset;
592 static void update_hooks(struct connman_iptables *table, GList *chain_head,
593 struct ipt_entry *entry)
596 struct connman_iptables_entry *head, *e;
599 if (chain_head == NULL)
602 head = chain_head->data;
604 builtin = head->builtin;
608 table->underflow[builtin] += entry->next_offset;
610 for (list = chain_head->next; list; list = list->next) {
613 builtin = e->builtin;
617 table->hook_entry[builtin] += entry->next_offset;
618 table->underflow[builtin] += entry->next_offset;
622 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
623 struct ipt_ip *ip, char *chain_name,
624 char *target_name, struct xtables_target *xt_t,
625 char *match_name, struct xtables_match *xt_m,
628 GList *chain_tail, *chain_head;
629 struct ipt_entry *new_entry;
630 struct connman_iptables_entry *head;
632 chain_head = find_chain_head(table, chain_name);
633 if (chain_head == NULL)
636 chain_tail = find_chain_tail(table, chain_name);
637 if (chain_tail == NULL)
640 new_entry = new_rule(table, ip, target_name, xt_t, match_name, xt_m);
641 if (new_entry == NULL)
644 update_hooks(table, chain_head, new_entry);
647 * If the chain is builtin, and does not have any rule,
648 * then the one that we're inserting is becoming the head
649 * and thus needs the builtin flag.
651 head = chain_head->data;
652 if (head->builtin < 0)
654 else if (chain_head == chain_tail->prev) {
655 *builtin = head->builtin;
663 connman_iptables_append_rule(struct connman_iptables *table,
664 struct ipt_ip *ip, char *chain_name,
665 char *target_name, struct xtables_target *xt_t,
666 char *match_name, struct xtables_match *xt_m)
669 struct ipt_entry *new_entry;
670 int builtin = -1, ret;
672 chain_tail = find_chain_tail(table, chain_name);
673 if (chain_tail == NULL)
676 new_entry = prepare_rule_inclusion(table, ip, chain_name,
677 target_name, xt_t, match_name, xt_m, &builtin);
678 if (new_entry == NULL)
681 ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
688 static int connman_iptables_insert_rule(struct connman_iptables *table,
689 struct ipt_ip *ip, char *chain_name,
690 char *target_name, struct xtables_target *xt_t,
691 char *match_name, struct xtables_match *xt_m)
694 struct ipt_entry *new_entry;
695 int builtin = -1, ret;
697 chain_head = find_chain_head(table, chain_name);
698 if (chain_head == NULL)
701 new_entry = prepare_rule_inclusion(table, ip, chain_name,
702 target_name, xt_t, match_name, xt_m, &builtin);
703 if (new_entry == NULL)
706 ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
713 static struct ipt_replace *
714 connman_iptables_blob(struct connman_iptables *table)
716 struct ipt_replace *r;
718 struct connman_iptables_entry *e;
719 unsigned char *entry_index;
721 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
725 memset(r, 0, sizeof(*r) + table->size);
727 r->counters = g_try_malloc0(sizeof(struct xt_counters)
728 * table->old_entries);
729 if (r->counters == NULL) {
734 strcpy(r->name, table->info->name);
735 r->num_entries = table->num_entries;
736 r->size = table->size;
738 r->num_counters = table->old_entries;
739 r->valid_hooks = table->info->valid_hooks;
741 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
742 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
744 entry_index = (unsigned char *)r->entries;
745 for (list = table->entries; list; list = list->next) {
748 memcpy(entry_index, e->entry, e->entry->next_offset);
749 entry_index += e->entry->next_offset;
755 static void dump_target(struct connman_iptables *table,
756 struct ipt_entry *entry)
759 struct xtables_target *xt_t;
760 struct xt_entry_target *target;
762 target = ipt_get_target(entry);
764 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
765 struct xt_standard_target *t;
767 t = (struct xt_standard_target *)target;
769 switch (t->verdict) {
771 printf("\ttarget RETURN\n");
775 printf("\ttarget ACCEPT\n");
779 printf("\ttarget DROP\n");
783 printf("\ttarget QUEUE\n");
787 printf("\ttarget STOP\n");
791 printf("\tJUMP @%p (0x%x)\n",
792 (char*)table->blob_entries->entrytable +
793 t->verdict, t->verdict);
797 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
798 XTF_LOAD_MUST_SUCCEED);
800 if(xt_t->print != NULL)
801 xt_t->print(NULL, target, 1);
803 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
805 printf("\ttarget %s\n", target->u.user.name);
809 if(xt_t->print != NULL) {
811 xt_t->print(NULL, target, 1);
817 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
819 struct xtables_match *xt_m;
820 struct xt_entry_match *match;
822 if (entry->elems == (unsigned char *)entry + entry->target_offset)
825 match = (struct xt_entry_match *) entry->elems;
827 if (!strlen(match->u.user.name))
830 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
834 if(xt_m->print != NULL) {
836 xt_m->print(NULL, match, 1);
843 printf("\tmatch %s\n", match->u.user.name);
847 static int connman_iptables_dump_entry(struct ipt_entry *entry,
848 struct connman_iptables *table)
850 struct xt_entry_target *target;
854 offset = (char *)entry - (char *)table->blob_entries->entrytable;
855 target = ipt_get_target(entry);
856 builtin = is_hook_entry(table, entry);
858 if (entry_to_offset(table, entry) + entry->next_offset ==
859 table->blob_entries->size) {
860 printf("End of CHAIN 0x%x\n", offset);
864 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
865 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
866 target->data, entry, entry->elems,
867 (char *)entry + entry->target_offset,
871 } else if (builtin >= 0) {
872 printf("CHAIN (%s) %p match %p target %p size %d\n",
873 hooknames[builtin], entry, entry->elems,
874 (char *)entry + entry->target_offset,
877 printf("RULE %p match %p target %p size %d\n", entry,
879 (char *)entry + entry->target_offset,
883 dump_match(table, entry);
884 dump_target(table, entry);
889 static void connman_iptables_dump_hook(struct connman_iptables *table)
893 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
894 if ((table->info->valid_hooks & (1 << i)))
895 printf("%s entry %p underflow %p (%#x)\n",
897 table->blob_entries->entrytable +
898 table->info->hook_entry[i],
899 table->blob_entries->entrytable +
900 table->info->underflow[i],
901 table->info->underflow[i]);
905 static void connman_iptables_dump(struct connman_iptables *table)
907 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
909 table->info->valid_hooks, table->info->num_entries,
912 connman_iptables_dump_hook(table);
914 ENTRY_ITERATE(table->blob_entries->entrytable,
915 table->blob_entries->size,
916 connman_iptables_dump_entry, table);
920 static int connman_iptables_get_entries(struct connman_iptables *table)
922 socklen_t entry_size;
924 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
926 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
927 table->blob_entries, &entry_size);
930 static int connman_iptables_replace(struct connman_iptables *table,
931 struct ipt_replace *r)
933 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
934 sizeof(*r) + r->size);
937 static void connman_iptables_cleanup(struct connman_iptables *table)
940 struct connman_iptables_entry *entry;
942 close(table->ipt_sock);
944 for (list = table->entries; list; list = list->next) {
947 g_free(entry->entry);
951 g_free(table->blob_entries);
954 xtables_free_opts(1);
957 static int connman_iptables_commit(struct connman_iptables *table)
959 struct ipt_replace *repl;
961 repl = connman_iptables_blob(table);
963 return connman_iptables_replace(table, repl);
966 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
968 struct ipt_entry *new_entry;
971 new_entry = g_try_malloc0(entry->next_offset);
972 if (new_entry == NULL)
975 memcpy(new_entry, entry, entry->next_offset);
977 builtin = is_hook_entry(table, entry);
979 return connman_add_entry(table, new_entry, NULL, builtin);
982 static struct connman_iptables *connman_iptables_init(const char *table_name)
984 struct connman_iptables *table;
987 table = g_try_new0(struct connman_iptables, 1);
991 table->info = g_try_new0(struct ipt_getinfo, 1);
992 if (table->info == NULL)
995 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
996 if (table->ipt_sock < 0)
999 s = sizeof(*table->info);
1000 strcpy(table->info->name, table_name);
1001 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1002 table->info, &s) < 0)
1005 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1007 if (table->blob_entries == NULL)
1010 strcpy(table->blob_entries->name, table_name);
1011 table->blob_entries->size = table->info->size;
1013 if (connman_iptables_get_entries(table) < 0)
1016 table->num_entries = 0;
1017 table->old_entries = table->info->num_entries;
1020 memcpy(table->underflow, table->info->underflow,
1021 sizeof(table->info->underflow));
1022 memcpy(table->hook_entry, table->info->hook_entry,
1023 sizeof(table->info->hook_entry));
1025 ENTRY_ITERATE(table->blob_entries->entrytable,
1026 table->blob_entries->size,
1034 connman_iptables_cleanup(table);
1040 static struct option connman_iptables_opts[] = {
1041 {.name = "append", .has_arg = 1, .val = 'A'},
1042 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1043 {.name = "insert", .has_arg = 1, .val = 'I'},
1044 {.name = "list", .has_arg = 2, .val = 'L'},
1045 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1046 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1047 {.name = "destination", .has_arg = 1, .val = 'd'},
1048 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1049 {.name = "jump", .has_arg = 1, .val = 'j'},
1050 {.name = "match", .has_arg = 1, .val = 'm'},
1051 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1052 {.name = "source", .has_arg = 1, .val = 's'},
1053 {.name = "table", .has_arg = 1, .val = 't'},
1057 struct xtables_globals connman_iptables_globals = {
1059 .opts = connman_iptables_opts,
1060 .orig_opts = connman_iptables_opts,
1063 int main(int argc, char *argv[])
1065 struct connman_iptables *table;
1066 struct xtables_match *xt_m;
1067 struct xtables_target *xt_t;
1069 char *table_name, *chain, *new_chain, *match_name, *target_name;
1070 char *delete_chain, *flush_chain;
1071 int c, in_len, out_len;
1073 gboolean dump, invert, delete, insert;
1074 struct in_addr src, dst;
1076 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1082 table_name = chain = new_chain = match_name = target_name = NULL;
1083 delete_chain = flush_chain = NULL;
1084 memset(&ip, 0, sizeof(struct ipt_ip));
1089 while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:i:j:m:o:s:t:",
1090 connman_iptables_globals.opts, NULL)) != -1) {
1093 /* It is either -A, -D or -I at once */
1101 flush_chain = optarg;
1105 /* It is either -A, -D or -I at once */
1123 delete_chain = optarg;
1127 if (!inet_pton(AF_INET, optarg, &dst))
1131 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1134 ip.invflags |= IPT_INV_DSTIP;
1139 in_len = strlen(optarg);
1141 if (in_len + 1 > IFNAMSIZ)
1144 strcpy(ip.iniface, optarg);
1145 memset(ip.iniface_mask, 0xff, in_len + 1);
1148 ip.invflags |= IPT_INV_VIA_IN;
1153 target_name = optarg;
1154 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1159 size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1161 xt_t->t = g_try_malloc0(size);
1162 if (xt_t->t == NULL)
1164 xt_t->t->u.target_size = size;
1165 strcpy(xt_t->t->u.user.name, target_name);
1166 xt_t->t->u.user.revision = xt_t->revision;
1167 if (xt_t->init != NULL)
1168 xt_t->init(xt_t->t);
1169 connman_iptables_globals.opts =
1170 xtables_merge_options(
1171 #if XTABLES_VERSION_CODE > 5
1172 connman_iptables_globals.orig_opts,
1174 connman_iptables_globals.opts,
1176 &xt_t->option_offset);
1177 if (connman_iptables_globals.opts == NULL)
1183 match_name = optarg;
1185 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1186 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1187 xt_m->m = g_try_malloc0(size);
1190 xt_m->m->u.match_size = size;
1191 strcpy(xt_m->m->u.user.name, xt_m->name);
1192 xt_m->m->u.user.revision = xt_m->revision;
1193 if (xt_m->init != NULL)
1194 xt_m->init(xt_m->m);
1195 if (xt_m != xt_m->next) {
1196 connman_iptables_globals.opts =
1197 xtables_merge_options(
1198 #if XTABLES_VERSION_CODE > 5
1199 connman_iptables_globals.orig_opts,
1201 connman_iptables_globals.opts,
1203 &xt_m->option_offset);
1204 if (connman_iptables_globals.opts == NULL)
1211 out_len = strlen(optarg);
1213 if (out_len + 1 > IFNAMSIZ)
1216 strcpy(ip.outiface, optarg);
1217 memset(ip.outiface_mask, 0xff, out_len + 1);
1220 ip.invflags |= IPT_INV_VIA_OUT;
1225 if (!inet_pton(AF_INET, optarg, &src))
1229 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1232 ip.invflags |= IPT_INV_SRCIP;
1237 table_name = optarg;
1241 if (optarg[0] == '!' && optarg[1] == '\0') {
1243 printf("Consecutive ! not allowed\n");
1250 printf("Invalid option\n");
1255 if (xt_t == NULL || xt_t->parse == NULL ||
1256 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1257 &xt_t->tflags, NULL, &xt_t->t)) {
1258 if (xt_m == NULL || xt_m->parse == NULL)
1261 xt_m->parse(c - xt_m->option_offset, argv,
1262 invert, &xt_m->mflags, NULL, &xt_m->m);
1269 if (table_name == NULL)
1270 table_name = "filter";
1272 table = connman_iptables_init(table_name);
1277 if (delete_chain == NULL)
1280 printf("Delete chain %s\n", delete_chain);
1282 connman_iptables_delete_chain(table, delete_chain);
1288 printf("Flush chain %s\n", flush_chain);
1290 connman_iptables_flush_chain(table, flush_chain);
1296 connman_iptables_dump(table);
1301 if (chain && new_chain)
1305 printf("New chain %s\n", new_chain);
1307 connman_iptables_add_chain(table, new_chain);
1313 if (target_name == NULL)
1316 if (insert == TRUE) {
1317 printf("Inserting %s to %s (match %s)\n", target_name,
1320 connman_iptables_insert_rule(table, &ip, chain,
1321 target_name, xt_t, match_name, xt_m);
1323 printf("Appending %s to %s (match %s)\n", target_name,
1326 connman_iptables_append_rule(table, &ip, chain,
1327 target_name, xt_t, match_name, xt_m);
1333 connman_iptables_commit(table);
1336 connman_iptables_cleanup(table);