4 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
5 * Copyright (C) 2013 BMW Car IT GmbH.
6 * Copyright (C) 2018 Jolla Ltd. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "../src/connman.h"
32 enum iptables_command {
33 IPTABLES_COMMAND_APPEND,
34 IPTABLES_COMMAND_INSERT,
35 IPTABLES_COMMAND_DELETE,
36 IPTABLES_COMMAND_POLICY,
37 IPTABLES_COMMAND_CHAIN_INSERT,
38 IPTABLES_COMMAND_CHAIN_DELETE,
39 IPTABLES_COMMAND_CHAIN_FLUSH,
40 IPTABLES_COMMAND_DUMP,
41 IPTABLES_COMMAND_UNKNOWN,
44 int main(int argc, char *argv[])
46 enum iptables_command cmd = IPTABLES_COMMAND_UNKNOWN;
47 char *table = NULL, *chain = NULL, *rule = NULL, *tmp;
52 while ((c = getopt_long(argc, argv,
53 "-A:I:D:P:N:X:F:Lt:", NULL, NULL)) != -1) {
57 cmd = IPTABLES_COMMAND_APPEND;
61 cmd = IPTABLES_COMMAND_INSERT;
65 cmd = IPTABLES_COMMAND_DELETE;
69 /* The policy will be stored in rule. */
70 cmd = IPTABLES_COMMAND_POLICY;
74 cmd = IPTABLES_COMMAND_CHAIN_INSERT;
78 cmd = IPTABLES_COMMAND_CHAIN_DELETE;
82 cmd = IPTABLES_COMMAND_CHAIN_FLUSH;
85 cmd = IPTABLES_COMMAND_DUMP;
99 for (i = optind - 1; i < argc; i++) {
102 rule = g_strdup_printf("%s %s", rule, argv[i]);
105 rule = g_strdup(argv[i]);
108 __connman_iptables_init();
111 case IPTABLES_COMMAND_APPEND:
112 err = __connman_iptables_append(AF_INET6, table, chain, rule);
114 case IPTABLES_COMMAND_INSERT:
115 err = __connman_iptables_insert(AF_INET6, table, chain, rule);
117 case IPTABLES_COMMAND_DELETE:
118 err = __connman_iptables_delete(AF_INET6, table, chain, rule);
120 case IPTABLES_COMMAND_POLICY:
121 err = __connman_iptables_change_policy(AF_INET6, table, chain,
124 case IPTABLES_COMMAND_CHAIN_INSERT:
125 err = __connman_iptables_new_chain(AF_INET6, table, chain);
127 case IPTABLES_COMMAND_CHAIN_DELETE:
128 err = __connman_iptables_delete_chain(AF_INET6, table, chain);
130 case IPTABLES_COMMAND_CHAIN_FLUSH:
131 err = __connman_iptables_flush_chain(AF_INET6, table, chain);
133 case IPTABLES_COMMAND_DUMP:
134 __connman_log_init(argv[0], "*", false, false,
135 "ip6tables-test", "1");
136 err = __connman_iptables_dump(AF_INET6, table);
138 case IPTABLES_COMMAND_UNKNOWN:
139 printf("Missing command\n");
140 printf("usage: ip6tables-test [-t table] {-A|-I|-D} chain rule\n");
141 printf(" ip6tables-test [-t table] {-N|-X|-F} chain\n");
142 printf(" ip6tables-test [-t table] -L\n");
143 printf(" ip6tables-test [-t table] -P chain target\n");
148 printf("Error: %s\n", strerror(-err));
152 err = __connman_iptables_commit(AF_INET6, table);
154 printf("Failed to commit changes: %s\n", strerror(-err));
160 __connman_iptables_cleanup();