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 void update_targets_reference(struct connman_iptables *table,
282 struct connman_iptables_entry *entry_before,
283 struct connman_iptables_entry *modified_entry)
285 struct connman_iptables_entry *tmp;
286 struct xt_standard_target *t;
289 for (list = table->entries; list; list = list->next) {
295 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
297 if (t->verdict > entry_before->offset)
298 t->verdict += modified_entry->entry->next_offset;
302 static int connman_add_entry(struct connman_iptables *table,
303 struct ipt_entry *entry, GList *before,
306 struct connman_iptables_entry *e, *entry_before;
311 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
316 e->builtin = builtin;
318 table->entries = g_list_insert_before(table->entries, before, e);
319 table->num_entries++;
320 table->size += entry->next_offset;
322 if (before == NULL) {
323 e->offset = table->size - entry->next_offset;
328 entry_before = before->data;
331 * We've just appended/insterted a new entry. All references
332 * should be bumped accordingly.
334 update_targets_reference(table, entry_before, e);
336 update_offsets(table);
341 static int remove_table_entry(struct connman_iptables *table,
342 struct connman_iptables_entry *entry)
346 table->num_entries--;
347 table->size -= entry->entry->next_offset;
348 removed = entry->entry->next_offset;
350 g_free(entry->entry);
352 table->entries = g_list_remove(table->entries, entry);
357 static int connman_iptables_flush_chain(struct connman_iptables *table,
360 GList *chain_head, *chain_tail, *list, *next;
361 struct connman_iptables_entry *entry;
362 int builtin, removed = 0;
364 chain_head = find_chain_head(table, name);
365 if (chain_head == NULL)
368 chain_tail = find_chain_tail(table, name);
369 if (chain_tail == NULL)
372 entry = chain_head->data;
373 builtin = entry->builtin;
378 list = chain_head->next;
380 if (list == chain_tail->prev)
383 while (list != chain_tail->prev) {
385 next = g_list_next(list);
387 removed += remove_table_entry(table, entry);
393 struct connman_iptables_entry *e;
397 entry->builtin = builtin;
399 table->underflow[builtin] -= removed;
401 for (list = chain_tail; list; list = list->next) {
404 builtin = e->builtin;
408 table->hook_entry[builtin] -= removed;
409 table->underflow[builtin] -= removed;
413 update_offsets(table);
418 static int connman_iptables_delete_chain(struct connman_iptables *table,
421 GList *chain_head, *chain_tail;
422 struct connman_iptables_entry *entry;
424 chain_head = find_chain_head(table, name);
425 if (chain_head == NULL)
428 entry = chain_head->data;
430 /* We cannot remove builtin chain */
431 if (entry->builtin >= 0)
434 chain_tail = find_chain_tail(table, name);
435 if (chain_tail == NULL)
438 /* Chain must be flushed */
439 if (chain_head->next != chain_tail->prev)
442 remove_table_entry(table, entry);
444 entry = chain_tail->prev->data;
445 remove_table_entry(table, entry);
447 update_offsets(table);
452 static int connman_iptables_add_chain(struct connman_iptables *table,
456 struct ipt_entry *entry_head;
457 struct ipt_entry *entry_return;
458 struct error_target *error;
459 struct ipt_standard_target *standard;
460 u_int16_t entry_head_size, entry_return_size;
462 last = g_list_last(table->entries);
465 * An empty chain is composed of:
466 * - A head entry, with no match and an error target.
467 * The error target data is the chain name.
468 * - A tail entry, with no match and a standard target.
469 * The standard target verdict is XT_RETURN (return to the
474 entry_head_size = sizeof(struct ipt_entry) +
475 sizeof(struct error_target);
476 entry_head = g_try_malloc0(entry_head_size);
477 if (entry_head == NULL)
480 memset(entry_head, 0, entry_head_size);
482 entry_head->target_offset = sizeof(struct ipt_entry);
483 entry_head->next_offset = entry_head_size;
485 error = (struct error_target *) entry_head->elems;
486 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
487 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
488 strcpy(error->error, name);
490 if (connman_add_entry(table, entry_head, last, -1) < 0)
494 entry_return_size = sizeof(struct ipt_entry) +
495 sizeof(struct ipt_standard_target);
496 entry_return = g_try_malloc0(entry_return_size);
497 if (entry_return == NULL)
500 memset(entry_return, 0, entry_return_size);
502 entry_return->target_offset = sizeof(struct ipt_entry);
503 entry_return->next_offset = entry_return_size;
505 standard = (struct ipt_standard_target *) entry_return->elems;
506 standard->target.u.user.target_size =
507 ALIGN(sizeof(struct ipt_standard_target));
508 standard->verdict = XT_RETURN;
510 if (connman_add_entry(table, entry_return, last, -1) < 0)
516 g_free(entry_return);
523 static struct ipt_entry *
524 new_rule(struct ipt_ip *ip, char *target_name,
525 struct xtables_target *xt_t,
526 char *match_name, struct xtables_match *xt_m)
528 struct ipt_entry *new_entry;
529 size_t match_size, target_size;
532 match_size = xt_m->m->u.match_size;
537 target_size = ALIGN(xt_t->t->u.target_size);
539 target_size = ALIGN(sizeof(struct xt_standard_target));
541 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
543 if (new_entry == NULL)
546 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
548 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
549 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
552 struct xt_entry_match *entry_match;
554 entry_match = (struct xt_entry_match *)new_entry->elems;
555 memcpy(entry_match, xt_m->m, match_size);
559 struct xt_entry_target *entry_target;
561 entry_target = ipt_get_target(new_entry);
562 memcpy(entry_target, xt_t->t, target_size);
568 static void update_hooks(struct connman_iptables *table, GList *chain_head,
569 struct ipt_entry *entry)
572 struct connman_iptables_entry *head, *e;
575 if (chain_head == NULL)
578 head = chain_head->data;
580 builtin = head->builtin;
584 table->underflow[builtin] += entry->next_offset;
586 for (list = chain_head->next; list; list = list->next) {
589 builtin = e->builtin;
593 table->hook_entry[builtin] += entry->next_offset;
594 table->underflow[builtin] += entry->next_offset;
598 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
599 struct ipt_ip *ip, char *chain_name,
600 char *target_name, struct xtables_target *xt_t,
601 char *match_name, struct xtables_match *xt_m,
604 GList *chain_tail, *chain_head;
605 struct ipt_entry *new_entry;
606 struct connman_iptables_entry *head;
608 chain_head = find_chain_head(table, chain_name);
609 if (chain_head == NULL)
612 chain_tail = find_chain_tail(table, chain_name);
613 if (chain_tail == NULL)
616 new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
617 if (new_entry == NULL)
620 update_hooks(table, chain_head, new_entry);
623 * If the chain is builtin, and does not have any rule,
624 * then the one that we're inserting is becoming the head
625 * and thus needs the builtin flag.
627 head = chain_head->data;
628 if (head->builtin < 0)
630 else if (chain_head == chain_tail->prev) {
631 *builtin = head->builtin;
639 connman_iptables_append_rule(struct connman_iptables *table,
640 struct ipt_ip *ip, char *chain_name,
641 char *target_name, struct xtables_target *xt_t,
642 char *match_name, struct xtables_match *xt_m)
645 struct ipt_entry *new_entry;
646 int builtin = -1, ret;
648 chain_tail = find_chain_tail(table, chain_name);
649 if (chain_tail == NULL)
652 new_entry = prepare_rule_inclusion(table, ip, chain_name,
653 target_name, xt_t, match_name, xt_m, &builtin);
654 if (new_entry == NULL)
657 ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
664 static int connman_iptables_insert_rule(struct connman_iptables *table,
665 struct ipt_ip *ip, char *chain_name,
666 char *target_name, struct xtables_target *xt_t,
667 char *match_name, struct xtables_match *xt_m)
670 struct ipt_entry *new_entry;
671 int builtin = -1, ret;
673 chain_head = find_chain_head(table, chain_name);
674 if (chain_head == NULL)
677 new_entry = prepare_rule_inclusion(table, ip, chain_name,
678 target_name, xt_t, match_name, xt_m, &builtin);
679 if (new_entry == NULL)
682 ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
689 static struct ipt_replace *
690 connman_iptables_blob(struct connman_iptables *table)
692 struct ipt_replace *r;
694 struct connman_iptables_entry *e;
695 unsigned char *entry_index;
697 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
701 memset(r, 0, sizeof(*r) + table->size);
703 r->counters = g_try_malloc0(sizeof(struct xt_counters)
704 * table->old_entries);
705 if (r->counters == NULL) {
710 strcpy(r->name, table->info->name);
711 r->num_entries = table->num_entries;
712 r->size = table->size;
714 r->num_counters = table->old_entries;
715 r->valid_hooks = table->info->valid_hooks;
717 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
718 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
720 entry_index = (unsigned char *)r->entries;
721 for (list = table->entries; list; list = list->next) {
724 memcpy(entry_index, e->entry, e->entry->next_offset);
725 entry_index += e->entry->next_offset;
731 static void dump_target(struct connman_iptables *table,
732 struct ipt_entry *entry)
735 struct xtables_target *xt_t;
736 struct xt_entry_target *target;
738 target = ipt_get_target(entry);
740 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
741 struct xt_standard_target *t;
743 t = (struct xt_standard_target *)target;
745 switch (t->verdict) {
747 printf("\ttarget RETURN\n");
751 printf("\ttarget ACCEPT\n");
755 printf("\ttarget DROP\n");
759 printf("\ttarget QUEUE\n");
763 printf("\ttarget STOP\n");
767 printf("\tJUMP @%p (0x%x)\n",
768 (char*)table->blob_entries->entrytable +
769 t->verdict, t->verdict);
773 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
774 XTF_LOAD_MUST_SUCCEED);
776 if(xt_t->print != NULL)
777 xt_t->print(NULL, target, 1);
779 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
781 printf("\ttarget %s\n", target->u.user.name);
785 if(xt_t->print != NULL) {
787 xt_t->print(NULL, target, 1);
793 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
795 struct xtables_match *xt_m;
796 struct xt_entry_match *match;
798 if (entry->elems == (unsigned char *)entry + entry->target_offset)
801 match = (struct xt_entry_match *) entry->elems;
803 if (!strlen(match->u.user.name))
806 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
810 if(xt_m->print != NULL) {
812 xt_m->print(NULL, match, 1);
819 printf("\tmatch %s\n", match->u.user.name);
823 static int connman_iptables_dump_entry(struct ipt_entry *entry,
824 struct connman_iptables *table)
826 struct xt_entry_target *target;
830 offset = (char *)entry - (char *)table->blob_entries->entrytable;
831 target = ipt_get_target(entry);
832 builtin = is_hook_entry(table, entry);
834 if (entry_to_offset(table, entry) + entry->next_offset ==
835 table->blob_entries->size) {
836 printf("End of CHAIN 0x%x\n", offset);
840 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
841 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
842 target->data, entry, entry->elems,
843 (char *)entry + entry->target_offset,
847 } else if (builtin >= 0) {
848 printf("CHAIN (%s) %p match %p target %p size %d\n",
849 hooknames[builtin], entry, entry->elems,
850 (char *)entry + entry->target_offset,
853 printf("RULE %p match %p target %p size %d\n", entry,
855 (char *)entry + entry->target_offset,
859 dump_match(table, entry);
860 dump_target(table, entry);
865 static void connman_iptables_dump_hook(struct connman_iptables *table)
869 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
870 if ((table->info->valid_hooks & (1 << i)))
871 printf("%s entry %p underflow %p (%#x)\n",
873 table->blob_entries->entrytable +
874 table->info->hook_entry[i],
875 table->blob_entries->entrytable +
876 table->info->underflow[i],
877 table->info->underflow[i]);
881 static void connman_iptables_dump(struct connman_iptables *table)
883 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
885 table->info->valid_hooks, table->info->num_entries,
888 connman_iptables_dump_hook(table);
890 ENTRY_ITERATE(table->blob_entries->entrytable,
891 table->blob_entries->size,
892 connman_iptables_dump_entry, table);
896 static int connman_iptables_get_entries(struct connman_iptables *table)
898 socklen_t entry_size;
900 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
902 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
903 table->blob_entries, &entry_size);
906 static int connman_iptables_replace(struct connman_iptables *table,
907 struct ipt_replace *r)
909 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
910 sizeof(*r) + r->size);
913 static void connman_iptables_cleanup(struct connman_iptables *table)
916 struct connman_iptables_entry *entry;
918 close(table->ipt_sock);
920 for (list = table->entries; list; list = list->next) {
923 g_free(entry->entry);
927 g_free(table->blob_entries);
930 xtables_free_opts(1);
933 static int connman_iptables_commit(struct connman_iptables *table)
935 struct ipt_replace *repl;
937 repl = connman_iptables_blob(table);
939 return connman_iptables_replace(table, repl);
942 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
944 struct ipt_entry *new_entry;
947 new_entry = g_try_malloc0(entry->next_offset);
948 if (new_entry == NULL)
951 memcpy(new_entry, entry, entry->next_offset);
953 builtin = is_hook_entry(table, entry);
955 return connman_add_entry(table, new_entry, NULL, builtin);
958 static struct connman_iptables *connman_iptables_init(const char *table_name)
960 struct connman_iptables *table;
963 table = g_try_new0(struct connman_iptables, 1);
967 table->info = g_try_new0(struct ipt_getinfo, 1);
968 if (table->info == NULL)
971 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
972 if (table->ipt_sock < 0)
975 s = sizeof(*table->info);
976 strcpy(table->info->name, table_name);
977 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
978 table->info, &s) < 0)
981 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
983 if (table->blob_entries == NULL)
986 strcpy(table->blob_entries->name, table_name);
987 table->blob_entries->size = table->info->size;
989 if (connman_iptables_get_entries(table) < 0)
992 table->num_entries = 0;
993 table->old_entries = table->info->num_entries;
996 memcpy(table->underflow, table->info->underflow,
997 sizeof(table->info->underflow));
998 memcpy(table->hook_entry, table->info->hook_entry,
999 sizeof(table->info->hook_entry));
1001 ENTRY_ITERATE(table->blob_entries->entrytable,
1002 table->blob_entries->size,
1010 connman_iptables_cleanup(table);
1015 static struct xtables_target *prepare_target(struct connman_iptables *table,
1018 struct xtables_target *xt_t = NULL;
1019 gboolean is_builtin, is_user_defined;
1020 GList *chain_head = NULL;
1024 is_user_defined = FALSE;
1026 if (is_builtin_target(target_name))
1029 chain_head = find_chain_head(table, target_name);
1030 if (chain_head != NULL && chain_head->next != NULL)
1031 is_user_defined = TRUE;
1034 if (is_builtin || is_user_defined)
1035 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1036 XTF_LOAD_MUST_SUCCEED);
1038 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1043 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1045 xt_t->t = g_try_malloc0(target_size);
1046 if (xt_t->t == NULL)
1049 xt_t->t->u.target_size = target_size;
1051 if (is_builtin || is_user_defined) {
1052 struct xt_standard_target *target;
1054 target = (struct xt_standard_target *)(xt_t->t);
1055 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1057 if (is_builtin == TRUE)
1058 target->verdict = target_to_verdict(target_name);
1059 else if (is_user_defined == TRUE) {
1060 struct connman_iptables_entry *target_rule;
1062 if (chain_head == NULL) {
1067 target_rule = chain_head->next->data;
1068 target->verdict = target_rule->offset;
1071 strcpy(xt_t->t->u.user.name, target_name);
1072 xt_t->t->u.user.revision = xt_t->revision;
1073 if (xt_t->init != NULL)
1074 xt_t->init(xt_t->t);
1080 static struct option connman_iptables_opts[] = {
1081 {.name = "append", .has_arg = 1, .val = 'A'},
1082 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1083 {.name = "insert", .has_arg = 1, .val = 'I'},
1084 {.name = "list", .has_arg = 2, .val = 'L'},
1085 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1086 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1087 {.name = "destination", .has_arg = 1, .val = 'd'},
1088 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1089 {.name = "jump", .has_arg = 1, .val = 'j'},
1090 {.name = "match", .has_arg = 1, .val = 'm'},
1091 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1092 {.name = "source", .has_arg = 1, .val = 's'},
1093 {.name = "table", .has_arg = 1, .val = 't'},
1097 struct xtables_globals connman_iptables_globals = {
1099 .opts = connman_iptables_opts,
1100 .orig_opts = connman_iptables_opts,
1103 int main(int argc, char *argv[])
1105 struct connman_iptables *table;
1106 struct xtables_match *xt_m;
1107 struct xtables_target *xt_t;
1109 char *table_name, *chain, *new_chain, *match_name, *target_name;
1110 char *delete_chain, *flush_chain;
1111 int c, in_len, out_len;
1113 gboolean dump, invert, delete, insert;
1114 struct in_addr src, dst;
1116 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1122 table_name = chain = new_chain = match_name = target_name = NULL;
1123 delete_chain = flush_chain = NULL;
1124 memset(&ip, 0, sizeof(struct ipt_ip));
1129 while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:i:j:m:o:s:t:",
1130 connman_iptables_globals.opts, NULL)) != -1) {
1133 /* It is either -A, -D or -I at once */
1141 flush_chain = optarg;
1145 /* It is either -A, -D or -I at once */
1163 delete_chain = optarg;
1167 if (!inet_pton(AF_INET, optarg, &dst))
1171 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1174 ip.invflags |= IPT_INV_DSTIP;
1179 in_len = strlen(optarg);
1181 if (in_len + 1 > IFNAMSIZ)
1184 strcpy(ip.iniface, optarg);
1185 memset(ip.iniface_mask, 0xff, in_len + 1);
1188 ip.invflags |= IPT_INV_VIA_IN;
1193 target_name = optarg;
1197 match_name = optarg;
1199 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1200 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1201 xt_m->m = g_try_malloc0(size);
1204 xt_m->m->u.match_size = size;
1205 strcpy(xt_m->m->u.user.name, xt_m->name);
1206 xt_m->m->u.user.revision = xt_m->revision;
1207 if (xt_m->init != NULL)
1208 xt_m->init(xt_m->m);
1209 if (xt_m != xt_m->next) {
1210 connman_iptables_globals.opts =
1211 xtables_merge_options(
1212 #if XTABLES_VERSION_CODE > 5
1213 connman_iptables_globals.orig_opts,
1215 connman_iptables_globals.opts,
1217 &xt_m->option_offset);
1218 if (connman_iptables_globals.opts == NULL)
1225 out_len = strlen(optarg);
1227 if (out_len + 1 > IFNAMSIZ)
1230 strcpy(ip.outiface, optarg);
1231 memset(ip.outiface_mask, 0xff, out_len + 1);
1234 ip.invflags |= IPT_INV_VIA_OUT;
1239 if (!inet_pton(AF_INET, optarg, &src))
1243 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1246 ip.invflags |= IPT_INV_SRCIP;
1251 table_name = optarg;
1255 if (optarg[0] == '!' && optarg[1] == '\0') {
1257 printf("Consecutive ! not allowed\n");
1264 printf("Invalid option\n");
1269 if (xt_t == NULL || xt_t->parse == NULL ||
1270 !xt_t->parse(c - xt_t->option_offset, argv, invert,
1271 &xt_t->tflags, NULL, &xt_t->t)) {
1272 if (xt_m == NULL || xt_m->parse == NULL)
1275 xt_m->parse(c - xt_m->option_offset, argv,
1276 invert, &xt_m->mflags, NULL, &xt_m->m);
1283 if (table_name == NULL)
1284 table_name = "filter";
1286 table = connman_iptables_init(table_name);
1291 if (delete_chain == NULL)
1294 printf("Delete chain %s\n", delete_chain);
1296 connman_iptables_delete_chain(table, delete_chain);
1302 printf("Flush chain %s\n", flush_chain);
1304 connman_iptables_flush_chain(table, flush_chain);
1310 connman_iptables_dump(table);
1315 if (chain && new_chain)
1319 printf("New chain %s\n", new_chain);
1321 connman_iptables_add_chain(table, new_chain);
1327 xt_t = prepare_target(table, target_name);
1331 connman_iptables_globals.opts =
1332 xtables_merge_options(
1333 #if XTABLES_VERSION_CODE > 5
1334 connman_iptables_globals.orig_opts,
1336 connman_iptables_globals.opts,
1338 &xt_t->option_offset);
1339 if (connman_iptables_globals.opts == NULL)
1342 if (insert == TRUE) {
1343 printf("Inserting %s to %s (match %s)\n", target_name,
1346 connman_iptables_insert_rule(table, &ip, chain,
1347 target_name, xt_t, match_name, xt_m);
1349 printf("Appending %s to %s (match %s)\n", target_name,
1352 connman_iptables_append_rule(table, &ip, chain,
1353 target_name, xt_t, match_name, xt_m);
1359 connman_iptables_commit(table);
1362 connman_iptables_cleanup(table);