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>
30 #include <linux/netfilter_ipv4/ip_tables.h>
34 static const char *hooknames[] = {
35 [NF_IP_PRE_ROUTING] = "PREROUTING",
36 [NF_IP_LOCAL_IN] = "INPUT",
37 [NF_IP_FORWARD] = "FORWARD",
38 [NF_IP_LOCAL_OUT] = "OUTPUT",
39 [NF_IP_POST_ROUTING] = "POSTROUTING",
42 #define LABEL_ACCEPT "ACCEPT"
43 #define LABEL_DROP "DROP"
44 #define LABEL_QUEUE "QUEUE"
45 #define LABEL_RETURN "RETURN"
47 /* fn returns 0 to continue iteration */
48 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
55 for (__i = 0, __n = 0; __i < (size); \
56 __i += __entry->next_offset, __n++) { \
57 __entry = (void *)(entries) + __i; \
61 __ret = fn(__entry, ## args); \
68 /* fn returns 0 to continue iteration */
69 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
70 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
72 #define ENTRY_ITERATE(entries, size, fn, args...) \
73 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
75 #define MIN_ALIGN (__alignof__(struct ipt_entry))
77 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
79 struct ipt_error_target {
80 struct xt_entry_target t;
81 char error[IPT_TABLE_MAXNAMELEN];
84 struct connman_iptables_entry {
89 struct ipt_entry *entry;
92 struct connman_iptables {
95 struct ipt_getinfo *info;
96 struct ipt_get_entries *blob_entries;
98 unsigned int num_entries;
99 unsigned int old_entries;
106 static struct ipt_entry *get_entry(struct connman_iptables *table,
109 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
113 static int is_hook_entry(struct connman_iptables *table,
114 struct ipt_entry *entry)
118 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
119 if ((table->info->valid_hooks & (1 << i))
120 && get_entry(table, table->info->hook_entry[i]) == entry)
127 static unsigned long entry_to_offset(struct connman_iptables *table,
128 struct ipt_entry *entry)
130 return (void *)entry - (void *)table->blob_entries->entrytable;
133 static int target_to_verdict(char *target_name)
135 if (!strcmp(target_name, LABEL_ACCEPT))
136 return -NF_ACCEPT - 1;
138 if (!strcmp(target_name, LABEL_DROP))
141 if (!strcmp(target_name, LABEL_QUEUE))
142 return -NF_QUEUE - 1;
144 if (!strcmp(target_name, LABEL_RETURN))
150 static gboolean is_builtin_target(char *target_name)
152 if (!strcmp(target_name, LABEL_ACCEPT) ||
153 !strcmp(target_name, LABEL_DROP) ||
154 !strcmp(target_name, LABEL_QUEUE) ||
155 !strcmp(target_name, LABEL_RETURN))
161 static gboolean is_chain(struct connman_iptables *table,
162 struct connman_iptables_entry *e)
165 struct ipt_entry *entry;
166 struct xt_entry_target *target;
169 builtin = is_hook_entry(table, entry);
173 target = ipt_get_target(entry);
174 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
180 static GList *find_chain_tail(struct connman_iptables *table,
183 GList *chain_head, *list;
184 struct connman_iptables_entry *head, *tail;
185 struct ipt_entry *entry;
186 struct xt_entry_target *target;
189 /* First we look for the head */
190 for (list = table->entries; list; list = list->next) {
195 builtin = is_hook_entry(table, entry);
196 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
199 /* User defined chain */
200 target = ipt_get_target(entry);
201 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
202 !strcmp((char *)target->data, chain_name))
211 /* Then we look for the next chain */
212 for (list = chain_head->next; list; list = list->next) {
216 if (is_chain(table, tail))
220 /* Nothing found, we return the table end */
221 return g_list_last(table->entries);
224 static int connman_add_entry(struct connman_iptables *table,
225 struct ipt_entry *entry, GList *before)
227 struct connman_iptables_entry *e;
232 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
238 table->entries = g_list_insert_before(table->entries, before, e);
239 table->num_entries++;
240 table->size += entry->next_offset;
245 static int connman_iptables_add_chain(struct connman_iptables *table,
249 struct ipt_entry *entry_head;
250 struct ipt_entry *entry_return;
251 struct ipt_error_target *error;
252 struct ipt_standard_target *standard;
253 u_int16_t entry_head_size, entry_return_size;
255 last = g_list_last(table->entries);
258 * An empty chain is composed of:
259 * - A head entry, with no match and an error target.
260 * The error target data is the chain name.
261 * - A tail entry, with no match and a standard target.
262 * The standard target verdict is XT_RETURN (return to the
267 entry_head_size = sizeof(struct ipt_entry) +
268 sizeof(struct ipt_error_target);
269 entry_head = g_try_malloc0(entry_head_size);
270 if (entry_head == NULL)
273 memset(entry_head, 0, entry_head_size);
275 entry_head->target_offset = sizeof(struct ipt_entry);
276 entry_head->next_offset = entry_head_size;
278 error = (struct ipt_error_target *) entry_head->elems;
279 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
280 error->t.u.user.target_size = ALIGN(sizeof(struct ipt_error_target));
281 strcpy(error->error, name);
283 if (connman_add_entry(table, entry_head, last) < 0)
287 entry_return_size = sizeof(struct ipt_entry) +
288 sizeof(struct ipt_standard_target);
289 entry_return = g_try_malloc0(entry_return_size);
290 if (entry_return == NULL)
293 memset(entry_return, 0, entry_return_size);
295 entry_return->target_offset = sizeof(struct ipt_entry);
296 entry_return->next_offset = entry_return_size;
298 standard = (struct ipt_standard_target *) entry_return->elems;
299 standard->target.u.user.target_size =
300 ALIGN(sizeof(struct ipt_standard_target));
301 standard->verdict = XT_RETURN;
303 if (connman_add_entry(table, entry_return, last) < 0)
310 g_free(entry_return);
315 static struct ipt_entry *
316 new_builtin_rule(char *target_name,
317 char *match_name, int match_argc, char **match_argv)
319 struct ipt_entry *new_entry;
320 size_t match_size, target_size;
321 struct xtables_match *xt_m;
322 struct xt_standard_target *target;
328 xt_m = xtables_find_match(match_name, XTF_TRY_LOAD, NULL);
332 match_size = ALIGN(sizeof(struct xt_entry_match)) + xt_m->size;
335 target_size = ALIGN(sizeof(struct xt_standard_target));
337 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
339 if (new_entry == NULL)
342 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
343 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
347 struct xt_entry_match *entry_match;
349 entry_match = (struct xt_entry_match *)new_entry->elems;
350 entry_match->u.match_size = match_size;
351 strcpy(entry_match->u.user.name, xt_m->name);
352 entry_match->u.user.revision = xt_m->revision;
353 if (xt_m->init != NULL)
354 xt_m->init(entry_match);
357 target = (struct xt_standard_target *)(new_entry->elems + match_size);
358 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
359 target->target.u.user.target_size =
360 ALIGN(sizeof(struct ipt_standard_target));
361 target->verdict = target_to_verdict(target_name);
366 static struct ipt_entry *
367 new_custom_rule(char *target_name, int target_argc, char **target_argv,
368 char *match_name, int match_argc, char **match_argv)
373 static struct ipt_entry *
374 new_rule(char *target_name, int target_argc, char **target_argv,
375 char *match_name, int match_argc, char **match_argv)
377 struct ipt_entry *new_entry;
379 if (is_builtin_target(target_name))
380 new_entry = new_builtin_rule(target_name,
381 match_name, match_argc, match_argv);
383 new_entry = new_custom_rule(target_name,
384 target_argc, target_argv,
385 match_name, match_argc, match_argv);
391 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
392 char *target_name, int target_argc, char **target_argv,
393 char *match_name, int match_argc, char **match_argv)
396 struct ipt_entry *new_entry;
398 chain_tail = find_chain_tail(table, chain_name);
399 if (chain_tail == NULL)
402 new_entry = new_rule(target_name, target_argc, target_argv,
403 match_name, match_argc, match_argv);
404 if (new_entry == NULL)
407 return connman_add_entry(table, new_entry, chain_tail->prev);
410 static struct ipt_replace *
411 connman_iptables_blob(struct connman_iptables *table)
413 struct ipt_replace *r;
415 struct connman_iptables_entry *e;
416 unsigned char *entry_index;
418 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
422 memset(r, 0, sizeof(*r) + table->size);
424 r->counters = g_try_malloc0(sizeof(struct xt_counters)
425 * table->num_entries);
426 if (r->counters == NULL) {
431 strcpy(r->name, table->info->name);
432 r->num_entries = table->num_entries;
433 r->size = table->size;
435 r->num_counters = table->old_entries;
436 r->valid_hooks = table->info->valid_hooks;
438 memcpy(r->hook_entry, table->info->hook_entry,
439 sizeof(table->info->hook_entry));
440 memcpy(r->underflow, table->info->underflow,
441 sizeof(table->info->underflow));
443 entry_index = (unsigned char *)r->entries;
444 for (list = table->entries; list; list = list->next) {
447 memcpy(entry_index, e->entry, e->entry->next_offset);
448 entry_index += e->entry->next_offset;
454 static void dump_target(struct connman_iptables *table,
455 struct ipt_entry *entry)
458 struct xtables_target *xt_t;
459 struct xt_entry_target *target;
461 target = ipt_get_target(entry);
463 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
464 struct xt_standard_target *t;
466 t = (struct xt_standard_target *)target;
468 switch (t->verdict) {
470 printf("\ttarget RETURN\n");
474 printf("\ttarget ACCEPT\n");
478 printf("\ttarget DROP\n");
482 printf("\ttarget QUEUE\n");
486 printf("\ttarget STOP\n");
490 printf("\tJUMP @%p (0x%x)\n",
491 (char*)table->blob_entries->entrytable +
492 t->verdict, t->verdict);
496 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
497 XTF_LOAD_MUST_SUCCEED);
499 if(xt_t->print != NULL)
500 xt_t->print(NULL, target, 1);
502 printf("\ttarget %s\n", target->u.user.name);
504 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
508 if(xt_t->print != NULL)
509 xt_t->print(NULL, target, 1);
513 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
515 struct xtables_match *xt_m;
516 struct xt_entry_match *match;
518 match = (struct xt_entry_match *) entry->elems;
520 if (!strlen(match->u.user.name))
523 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
527 if(xt_m->print != NULL) {
529 xt_m->print(NULL, match, 1);
536 printf("\tmatch %s\n", match->u.user.name);
540 static int connman_iptables_dump_entry(struct ipt_entry *entry,
541 struct connman_iptables *table)
543 struct xt_entry_target *target;
547 offset = (char *)entry - (char *)table->blob_entries->entrytable;
548 target = ipt_get_target(entry);
549 builtin = is_hook_entry(table, entry);
551 if (entry_to_offset(table, entry) + entry->next_offset ==
552 table->blob_entries->size) {
553 printf("End of CHAIN 0x%x\n", offset);
557 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
558 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
559 target->data, entry, entry->elems,
560 (char *)entry + entry->target_offset,
564 } else if (builtin >= 0) {
565 printf("CHAIN (%s) %p match %p target %p size %d\n",
566 hooknames[builtin], entry, entry->elems,
567 (char *)entry + entry->target_offset,
570 printf("RULE %p match %p target %p size %d\n", entry,
572 (char *)entry + entry->target_offset,
576 dump_match(table, entry);
577 dump_target(table, entry);
582 static void connman_iptables_dump(struct connman_iptables *table)
584 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
586 table->info->valid_hooks, table->info->num_entries,
589 ENTRY_ITERATE(table->blob_entries->entrytable,
590 table->blob_entries->size,
591 connman_iptables_dump_entry, table);
595 static int connman_iptables_get_entries(struct connman_iptables *table)
597 socklen_t entry_size;
599 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
601 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
602 table->blob_entries, &entry_size);
605 static int connman_iptables_replace(struct connman_iptables *table,
606 struct ipt_replace *r)
608 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
609 sizeof(*r) + r->size);
612 static void connman_iptables_cleanup(struct connman_iptables *table)
614 close(table->ipt_sock);
616 g_free(table->blob_entries);
620 static int connman_iptables_commit(struct connman_iptables *table)
622 struct ipt_replace *repl;
624 repl = connman_iptables_blob(table);
626 return connman_iptables_replace(table, repl);
629 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
631 return connman_add_entry(table, entry, NULL);
634 static struct connman_iptables *connman_iptables_init(const char *table_name)
636 struct connman_iptables *table;
639 table = g_try_new0(struct connman_iptables, 1);
643 table->info = g_try_new0(struct ipt_getinfo, 1);
644 if (table->info == NULL)
647 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
648 if (table->ipt_sock < 0)
651 s = sizeof(*table->info);
652 strcpy(table->info->name, table_name);
653 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
654 table->info, &s) < 0)
657 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
659 if (table->blob_entries == NULL)
662 strcpy(table->blob_entries->name, table_name);
663 table->blob_entries->size = table->info->size;
665 if (connman_iptables_get_entries(table) < 0)
668 table->num_entries = 0;
669 table->old_entries = table->info->num_entries;
672 ENTRY_ITERATE(table->blob_entries->entrytable,
673 table->blob_entries->size,
681 connman_iptables_cleanup(table);
687 static struct option connman_iptables_opts[] = {
688 {.name = "append", .has_arg = 1, .val = 'A'},
689 {.name = "list", .has_arg = 2, .val = 'L'},
690 {.name = "new-chain", .has_arg = 1, .val = 'N'},
691 {.name = "in-interface", .has_arg = 1, .val = 'i'},
692 {.name = "jump", .has_arg = 1, .val = 'j'},
693 {.name = "match", .has_arg = 1, .val = 'm'},
694 {.name = "out-interface", .has_arg = 1, .val = 'o'},
695 {.name = "table", .has_arg = 1, .val = 't'},
699 int main(int argc, char *argv[])
701 struct connman_iptables *table;
702 char *table_name, *chain, *new_chain, *match_name, *target_name;
707 xtables_set_nfproto(NFPROTO_IPV4);
710 table_name = chain = new_chain = match_name = target_name = NULL;
712 while ((c = getopt_long(argc, argv,
713 "-A:L::N:j:i:m:o:t:", connman_iptables_opts, NULL)) != -1) {
728 target_name = optarg;
746 printf("default %s\n", optarg);
751 if (table_name == NULL)
752 table_name = "filter";
754 table = connman_iptables_init(table_name);
759 connman_iptables_dump(table);
764 if (chain && new_chain)
768 printf("New chain %s\n", new_chain);
770 connman_iptables_add_chain(table, new_chain);
776 if (target_name == NULL)
779 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
781 connman_iptables_add_rule(table, chain,
782 target_name, 0, NULL,
783 match_name, 0, NULL);
790 connman_iptables_commit(table);
792 connman_iptables_cleanup(table);