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_rule(char *target_name, struct xtables_target *xt_t,
317 char *match_name, struct xtables_match *xt_m)
319 struct ipt_entry *new_entry;
320 size_t match_size, target_size;
321 int is_builtin = is_builtin_target(target_name);
324 match_size = xt_m->m->u.match_size;
329 target_size = ALIGN(xt_t->t->u.target_size);
333 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
335 if (new_entry == NULL)
338 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
339 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
342 struct xt_entry_match *entry_match;
344 entry_match = (struct xt_entry_match *)new_entry->elems;
345 memcpy(entry_match, xt_m->m, match_size);
349 struct xt_entry_target *entry_target;
352 struct xt_standard_target *target;
354 target = (struct xt_standard_target *)(xt_t->t);
355 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
356 target->verdict = target_to_verdict(target_name);
359 entry_target = ipt_get_target(new_entry);
360 memcpy(entry_target, xt_t->t, target_size);
367 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
368 char *target_name, struct xtables_target *xt_t,
369 char *match_name, struct xtables_match *xt_m)
372 struct ipt_entry *new_entry;
374 chain_tail = find_chain_tail(table, chain_name);
375 if (chain_tail == NULL)
378 new_entry = new_rule(target_name, xt_t,
380 if (new_entry == NULL)
383 return connman_add_entry(table, new_entry, chain_tail->prev);
386 static struct ipt_replace *
387 connman_iptables_blob(struct connman_iptables *table)
389 struct ipt_replace *r;
391 struct connman_iptables_entry *e;
392 unsigned char *entry_index;
394 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
398 memset(r, 0, sizeof(*r) + table->size);
400 r->counters = g_try_malloc0(sizeof(struct xt_counters)
401 * table->num_entries);
402 if (r->counters == NULL) {
407 strcpy(r->name, table->info->name);
408 r->num_entries = table->num_entries;
409 r->size = table->size;
411 r->num_counters = table->old_entries;
412 r->valid_hooks = table->info->valid_hooks;
414 memcpy(r->hook_entry, table->info->hook_entry,
415 sizeof(table->info->hook_entry));
416 memcpy(r->underflow, table->info->underflow,
417 sizeof(table->info->underflow));
419 entry_index = (unsigned char *)r->entries;
420 for (list = table->entries; list; list = list->next) {
423 memcpy(entry_index, e->entry, e->entry->next_offset);
424 entry_index += e->entry->next_offset;
430 static void dump_target(struct connman_iptables *table,
431 struct ipt_entry *entry)
434 struct xtables_target *xt_t;
435 struct xt_entry_target *target;
437 target = ipt_get_target(entry);
439 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
440 struct xt_standard_target *t;
442 t = (struct xt_standard_target *)target;
444 switch (t->verdict) {
446 printf("\ttarget RETURN\n");
450 printf("\ttarget ACCEPT\n");
454 printf("\ttarget DROP\n");
458 printf("\ttarget QUEUE\n");
462 printf("\ttarget STOP\n");
466 printf("\tJUMP @%p (0x%x)\n",
467 (char*)table->blob_entries->entrytable +
468 t->verdict, t->verdict);
472 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
473 XTF_LOAD_MUST_SUCCEED);
475 if(xt_t->print != NULL)
476 xt_t->print(NULL, target, 1);
478 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
480 printf("\ttarget %s\n", target->u.user.name);
484 if(xt_t->print != NULL) {
486 xt_t->print(NULL, target, 1);
492 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
494 struct xtables_match *xt_m;
495 struct xt_entry_match *match;
497 if (entry->elems == (unsigned char *)entry + entry->target_offset)
500 match = (struct xt_entry_match *) entry->elems;
502 if (!strlen(match->u.user.name))
505 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
509 if(xt_m->print != NULL) {
511 xt_m->print(NULL, match, 1);
518 printf("\tmatch %s\n", match->u.user.name);
522 static int connman_iptables_dump_entry(struct ipt_entry *entry,
523 struct connman_iptables *table)
525 struct xt_entry_target *target;
529 offset = (char *)entry - (char *)table->blob_entries->entrytable;
530 target = ipt_get_target(entry);
531 builtin = is_hook_entry(table, entry);
533 if (entry_to_offset(table, entry) + entry->next_offset ==
534 table->blob_entries->size) {
535 printf("End of CHAIN 0x%x\n", offset);
539 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
540 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
541 target->data, entry, entry->elems,
542 (char *)entry + entry->target_offset,
546 } else if (builtin >= 0) {
547 printf("CHAIN (%s) %p match %p target %p size %d\n",
548 hooknames[builtin], entry, entry->elems,
549 (char *)entry + entry->target_offset,
552 printf("RULE %p match %p target %p size %d\n", entry,
554 (char *)entry + entry->target_offset,
558 dump_match(table, entry);
559 dump_target(table, entry);
564 static void connman_iptables_dump(struct connman_iptables *table)
566 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
568 table->info->valid_hooks, table->info->num_entries,
571 ENTRY_ITERATE(table->blob_entries->entrytable,
572 table->blob_entries->size,
573 connman_iptables_dump_entry, table);
577 static int connman_iptables_get_entries(struct connman_iptables *table)
579 socklen_t entry_size;
581 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
583 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
584 table->blob_entries, &entry_size);
587 static int connman_iptables_replace(struct connman_iptables *table,
588 struct ipt_replace *r)
590 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
591 sizeof(*r) + r->size);
594 static void connman_iptables_cleanup(struct connman_iptables *table)
596 close(table->ipt_sock);
598 g_free(table->blob_entries);
601 xtables_free_opts(1);
604 static int connman_iptables_commit(struct connman_iptables *table)
606 struct ipt_replace *repl;
608 repl = connman_iptables_blob(table);
610 return connman_iptables_replace(table, repl);
613 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
615 return connman_add_entry(table, entry, NULL);
618 static struct connman_iptables *connman_iptables_init(const char *table_name)
620 struct connman_iptables *table;
623 table = g_try_new0(struct connman_iptables, 1);
627 table->info = g_try_new0(struct ipt_getinfo, 1);
628 if (table->info == NULL)
631 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
632 if (table->ipt_sock < 0)
635 s = sizeof(*table->info);
636 strcpy(table->info->name, table_name);
637 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
638 table->info, &s) < 0)
641 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
643 if (table->blob_entries == NULL)
646 strcpy(table->blob_entries->name, table_name);
647 table->blob_entries->size = table->info->size;
649 if (connman_iptables_get_entries(table) < 0)
652 table->num_entries = 0;
653 table->old_entries = table->info->num_entries;
656 ENTRY_ITERATE(table->blob_entries->entrytable,
657 table->blob_entries->size,
665 connman_iptables_cleanup(table);
671 static struct option connman_iptables_opts[] = {
672 {.name = "append", .has_arg = 1, .val = 'A'},
673 {.name = "list", .has_arg = 2, .val = 'L'},
674 {.name = "new-chain", .has_arg = 1, .val = 'N'},
675 {.name = "in-interface", .has_arg = 1, .val = 'i'},
676 {.name = "jump", .has_arg = 1, .val = 'j'},
677 {.name = "match", .has_arg = 1, .val = 'm'},
678 {.name = "out-interface", .has_arg = 1, .val = 'o'},
679 {.name = "table", .has_arg = 1, .val = 't'},
683 struct xtables_globals connman_iptables_globals = {
685 .opts = connman_iptables_opts,
686 .orig_opts = connman_iptables_opts,
689 int main(int argc, char *argv[])
691 struct connman_iptables *table;
692 struct xtables_match *xt_m;
693 struct xtables_target *xt_t;
694 char *table_name, *chain, *new_chain, *match_name, *target_name;
697 gboolean dump, invert;
699 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
703 table_name = chain = new_chain = match_name = target_name = NULL;
708 while ((c = getopt_long(argc, argv,
709 "-A:L::N:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
724 target_name = optarg;
725 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
730 size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
732 xt_t->t = g_try_malloc0(size);
735 xt_t->t->u.target_size = size;
736 strcpy(xt_t->t->u.user.name, target_name);
737 xt_t->t->u.user.revision = xt_t->revision;
738 if (xt_t->init != NULL)
740 connman_iptables_globals.opts =
741 xtables_merge_options(connman_iptables_globals.opts,
743 &xt_t->option_offset);
744 if (connman_iptables_globals.opts == NULL)
755 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
756 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
757 xt_m->m = g_try_malloc0(size);
760 xt_m->m->u.match_size = size;
761 strcpy(xt_m->m->u.user.name, xt_m->name);
762 xt_m->m->u.user.revision = xt_m->revision;
763 if (xt_m->init != NULL)
765 if (xt_m != xt_m->next) {
766 connman_iptables_globals.opts =
767 xtables_merge_options(connman_iptables_globals.opts,
769 &xt_m->option_offset);
770 if (connman_iptables_globals.opts == NULL)
784 if (optarg[0] == '!' && optarg[1] == '\0') {
786 printf("Consecutive ! not allowed\n");
793 printf("Invalid option\n");
798 if (xt_t == NULL || xt_t->parse == NULL ||
799 !xt_t->parse(c - xt_t->option_offset, argv, invert,
800 &xt_t->tflags, NULL, &xt_t->t)) {
801 if (xt_m == NULL || xt_m->parse == NULL)
804 xt_m->parse(c - xt_m->option_offset, argv,
805 invert, &xt_m->mflags, NULL, &xt_m->m);
812 if (table_name == NULL)
813 table_name = "filter";
815 table = connman_iptables_init(table_name);
820 connman_iptables_dump(table);
825 if (chain && new_chain)
829 printf("New chain %s\n", new_chain);
831 connman_iptables_add_chain(table, new_chain);
837 if (target_name == NULL)
840 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
842 connman_iptables_add_rule(table, chain, target_name, xt_t,
850 connman_iptables_commit(table);
853 connman_iptables_cleanup(table);