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, struct xtables_match *xt_m)
318 struct ipt_entry *new_entry;
319 size_t match_size, target_size;
320 struct xt_entry_match *entry_match;
321 struct xt_standard_target *target;
325 match_size = xt_m->m->u.match_size;
329 target_size = ALIGN(sizeof(struct xt_standard_target));
331 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
333 if (new_entry == NULL)
336 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
337 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
341 entry_match = (struct xt_entry_match *)new_entry->elems;
342 memcpy(entry_match, xt_m->m, match_size);
345 target = (struct xt_standard_target *)(new_entry->elems + match_size);
346 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
347 target->target.u.user.target_size =
348 ALIGN(sizeof(struct ipt_standard_target));
349 target->verdict = target_to_verdict(target_name);
354 static struct ipt_entry *
355 new_custom_rule(struct xtables_target *xt_t, struct xtables_match *xt_m)
360 static struct ipt_entry *
361 new_rule(char *target_name, struct xtables_target *xt_t,
362 char *match_name, struct xtables_match *xt_m)
364 struct ipt_entry *new_entry;
366 if (is_builtin_target(target_name))
367 new_entry = new_builtin_rule(target_name, xt_m);
369 new_entry = new_custom_rule(xt_t, xt_m);
375 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
376 char *target_name, struct xtables_target *xt_t,
377 char *match_name, struct xtables_match *xt_m)
380 struct ipt_entry *new_entry;
382 chain_tail = find_chain_tail(table, chain_name);
383 if (chain_tail == NULL)
386 new_entry = new_rule(target_name, xt_t,
388 if (new_entry == NULL)
391 return connman_add_entry(table, new_entry, chain_tail->prev);
394 static struct ipt_replace *
395 connman_iptables_blob(struct connman_iptables *table)
397 struct ipt_replace *r;
399 struct connman_iptables_entry *e;
400 unsigned char *entry_index;
402 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
406 memset(r, 0, sizeof(*r) + table->size);
408 r->counters = g_try_malloc0(sizeof(struct xt_counters)
409 * table->num_entries);
410 if (r->counters == NULL) {
415 strcpy(r->name, table->info->name);
416 r->num_entries = table->num_entries;
417 r->size = table->size;
419 r->num_counters = table->old_entries;
420 r->valid_hooks = table->info->valid_hooks;
422 memcpy(r->hook_entry, table->info->hook_entry,
423 sizeof(table->info->hook_entry));
424 memcpy(r->underflow, table->info->underflow,
425 sizeof(table->info->underflow));
427 entry_index = (unsigned char *)r->entries;
428 for (list = table->entries; list; list = list->next) {
431 memcpy(entry_index, e->entry, e->entry->next_offset);
432 entry_index += e->entry->next_offset;
438 static void dump_target(struct connman_iptables *table,
439 struct ipt_entry *entry)
442 struct xtables_target *xt_t;
443 struct xt_entry_target *target;
445 target = ipt_get_target(entry);
447 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
448 struct xt_standard_target *t;
450 t = (struct xt_standard_target *)target;
452 switch (t->verdict) {
454 printf("\ttarget RETURN\n");
458 printf("\ttarget ACCEPT\n");
462 printf("\ttarget DROP\n");
466 printf("\ttarget QUEUE\n");
470 printf("\ttarget STOP\n");
474 printf("\tJUMP @%p (0x%x)\n",
475 (char*)table->blob_entries->entrytable +
476 t->verdict, t->verdict);
480 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
481 XTF_LOAD_MUST_SUCCEED);
483 if(xt_t->print != NULL)
484 xt_t->print(NULL, target, 1);
486 printf("\ttarget %s\n", target->u.user.name);
488 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
492 if(xt_t->print != NULL)
493 xt_t->print(NULL, target, 1);
497 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
499 struct xtables_match *xt_m;
500 struct xt_entry_match *match;
502 match = (struct xt_entry_match *) entry->elems;
504 if (!strlen(match->u.user.name))
507 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
511 if(xt_m->print != NULL) {
513 xt_m->print(NULL, match, 1);
520 printf("\tmatch %s\n", match->u.user.name);
524 static int connman_iptables_dump_entry(struct ipt_entry *entry,
525 struct connman_iptables *table)
527 struct xt_entry_target *target;
531 offset = (char *)entry - (char *)table->blob_entries->entrytable;
532 target = ipt_get_target(entry);
533 builtin = is_hook_entry(table, entry);
535 if (entry_to_offset(table, entry) + entry->next_offset ==
536 table->blob_entries->size) {
537 printf("End of CHAIN 0x%x\n", offset);
541 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
542 printf("USER CHAIN (%s) %p match %p target %p size %d\n",
543 target->data, entry, entry->elems,
544 (char *)entry + entry->target_offset,
548 } else if (builtin >= 0) {
549 printf("CHAIN (%s) %p match %p target %p size %d\n",
550 hooknames[builtin], entry, entry->elems,
551 (char *)entry + entry->target_offset,
554 printf("RULE %p match %p target %p size %d\n", entry,
556 (char *)entry + entry->target_offset,
560 dump_match(table, entry);
561 dump_target(table, entry);
566 static void connman_iptables_dump(struct connman_iptables *table)
568 printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
570 table->info->valid_hooks, table->info->num_entries,
573 ENTRY_ITERATE(table->blob_entries->entrytable,
574 table->blob_entries->size,
575 connman_iptables_dump_entry, table);
579 static int connman_iptables_get_entries(struct connman_iptables *table)
581 socklen_t entry_size;
583 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
585 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
586 table->blob_entries, &entry_size);
589 static int connman_iptables_replace(struct connman_iptables *table,
590 struct ipt_replace *r)
592 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
593 sizeof(*r) + r->size);
596 static void connman_iptables_cleanup(struct connman_iptables *table)
598 close(table->ipt_sock);
600 g_free(table->blob_entries);
603 xtables_free_opts(1);
606 static int connman_iptables_commit(struct connman_iptables *table)
608 struct ipt_replace *repl;
610 repl = connman_iptables_blob(table);
612 return connman_iptables_replace(table, repl);
615 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
617 return connman_add_entry(table, entry, NULL);
620 static struct connman_iptables *connman_iptables_init(const char *table_name)
622 struct connman_iptables *table;
625 table = g_try_new0(struct connman_iptables, 1);
629 table->info = g_try_new0(struct ipt_getinfo, 1);
630 if (table->info == NULL)
633 table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
634 if (table->ipt_sock < 0)
637 s = sizeof(*table->info);
638 strcpy(table->info->name, table_name);
639 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
640 table->info, &s) < 0)
643 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
645 if (table->blob_entries == NULL)
648 strcpy(table->blob_entries->name, table_name);
649 table->blob_entries->size = table->info->size;
651 if (connman_iptables_get_entries(table) < 0)
654 table->num_entries = 0;
655 table->old_entries = table->info->num_entries;
658 ENTRY_ITERATE(table->blob_entries->entrytable,
659 table->blob_entries->size,
667 connman_iptables_cleanup(table);
673 static struct option connman_iptables_opts[] = {
674 {.name = "append", .has_arg = 1, .val = 'A'},
675 {.name = "list", .has_arg = 2, .val = 'L'},
676 {.name = "new-chain", .has_arg = 1, .val = 'N'},
677 {.name = "in-interface", .has_arg = 1, .val = 'i'},
678 {.name = "jump", .has_arg = 1, .val = 'j'},
679 {.name = "match", .has_arg = 1, .val = 'm'},
680 {.name = "out-interface", .has_arg = 1, .val = 'o'},
681 {.name = "table", .has_arg = 1, .val = 't'},
685 struct xtables_globals connman_iptables_globals = {
687 .opts = connman_iptables_opts,
688 .orig_opts = connman_iptables_opts,
691 int main(int argc, char *argv[])
693 struct connman_iptables *table;
694 struct xtables_match *xt_m;
695 struct xtables_target *xt_t;
696 char *table_name, *chain, *new_chain, *match_name, *target_name;
699 gboolean dump, invert;
701 xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
705 table_name = chain = new_chain = match_name = target_name = NULL;
710 while ((c = getopt_long(argc, argv,
711 "-A:L::N:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
726 target_name = optarg;
727 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
732 size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
734 xt_t->t = g_try_malloc0(size);
737 xt_t->t->u.target_size = size;
738 strcpy(xt_t->t->u.user.name, target_name);
739 xt_t->t->u.user.revision = xt_t->revision;
740 if (xt_t->init != NULL)
742 connman_iptables_globals.opts =
743 xtables_merge_options(connman_iptables_globals.opts,
745 &xt_t->option_offset);
746 if (connman_iptables_globals.opts == NULL)
757 xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
758 size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
759 xt_m->m = g_try_malloc0(size);
762 xt_m->m->u.match_size = size;
763 strcpy(xt_m->m->u.user.name, xt_m->name);
764 xt_m->m->u.user.revision = xt_m->revision;
765 if (xt_m->init != NULL)
767 if (xt_m != xt_m->next) {
768 connman_iptables_globals.opts =
769 xtables_merge_options(connman_iptables_globals.opts,
771 &xt_m->option_offset);
772 if (connman_iptables_globals.opts == NULL)
786 if (optarg[0] == '!' && optarg[1] == '\0') {
788 printf("Consecutive ! not allowed\n");
795 printf("Invalid option\n");
800 if (xt_t == NULL || xt_t->parse == NULL ||
801 !xt_t->parse(c - xt_t->option_offset, argv, invert,
802 &xt_t->tflags, NULL, &xt_t->t)) {
803 if (xt_m == NULL || xt_m->parse == NULL)
806 xt_m->parse(c - xt_m->option_offset, argv,
807 invert, &xt_m->mflags, NULL, &xt_m->m);
814 if (table_name == NULL)
815 table_name = "filter";
817 table = connman_iptables_init(table_name);
822 connman_iptables_dump(table);
827 if (chain && new_chain)
831 printf("New chain %s\n", new_chain);
833 connman_iptables_add_chain(table, new_chain);
839 if (target_name == NULL)
842 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
844 connman_iptables_add_rule(table, chain, target_name, xt_t,
852 connman_iptables_commit(table);
855 connman_iptables_cleanup(table);