5 * Copyright (C) 2007-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
40 static const char *hooknames[] = {
41 [NF_IP_PRE_ROUTING] = "PREROUTING",
42 [NF_IP_LOCAL_IN] = "INPUT",
43 [NF_IP_FORWARD] = "FORWARD",
44 [NF_IP_LOCAL_OUT] = "OUTPUT",
45 [NF_IP_POST_ROUTING] = "POSTROUTING",
48 #define LABEL_ACCEPT "ACCEPT"
49 #define LABEL_DROP "DROP"
50 #define LABEL_QUEUE "QUEUE"
51 #define LABEL_RETURN "RETURN"
53 #define XT_OPTION_OFFSET_SCALE 256
55 /* fn returns 0 to continue iteration */
56 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
63 for (__i = 0, __n = 0; __i < (size); \
64 __i += __entry->next_offset, __n++) { \
65 __entry = (void *)(entries) + __i; \
69 __ret = fn(__entry, ## args); \
76 /* fn returns 0 to continue iteration */
77 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
78 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
80 #define ENTRY_ITERATE(entries, size, fn, args...) \
81 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
83 #define MIN_ALIGN (__alignof__(struct ipt_entry))
85 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
88 struct xt_entry_target t;
89 char error[IPT_TABLE_MAXNAMELEN];
92 struct connman_iptables_entry {
96 struct ipt_entry *entry;
99 struct connman_iptables {
102 struct ipt_getinfo *info;
103 struct ipt_get_entries *blob_entries;
105 unsigned int num_entries;
106 unsigned int old_entries;
109 unsigned int underflow[NF_INET_NUMHOOKS];
110 unsigned int hook_entry[NF_INET_NUMHOOKS];
115 static GHashTable *table_hash = NULL;
117 static struct ipt_entry *get_entry(struct connman_iptables *table,
120 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
124 static int is_hook_entry(struct connman_iptables *table,
125 struct ipt_entry *entry)
129 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
130 if ((table->info->valid_hooks & (1 << i))
131 && get_entry(table, table->info->hook_entry[i]) == entry)
138 static unsigned long entry_to_offset(struct connman_iptables *table,
139 struct ipt_entry *entry)
141 return (void *)entry - (void *)table->blob_entries->entrytable;
144 static int target_to_verdict(char *target_name)
146 if (!strcmp(target_name, LABEL_ACCEPT))
147 return -NF_ACCEPT - 1;
149 if (!strcmp(target_name, LABEL_DROP))
152 if (!strcmp(target_name, LABEL_QUEUE))
153 return -NF_QUEUE - 1;
155 if (!strcmp(target_name, LABEL_RETURN))
161 static gboolean is_builtin_target(char *target_name)
163 if (!strcmp(target_name, LABEL_ACCEPT) ||
164 !strcmp(target_name, LABEL_DROP) ||
165 !strcmp(target_name, LABEL_QUEUE) ||
166 !strcmp(target_name, LABEL_RETURN))
172 static gboolean is_jump(struct connman_iptables_entry *e)
174 struct xt_entry_target *target;
176 target = ipt_get_target(e->entry);
178 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
179 struct xt_standard_target *t;
181 t = (struct xt_standard_target *)target;
183 switch (t->verdict) {
199 static gboolean is_chain(struct connman_iptables *table,
200 struct connman_iptables_entry *e)
202 struct ipt_entry *entry;
203 struct xt_entry_target *target;
209 target = ipt_get_target(entry);
210 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
216 static GList *find_chain_head(struct connman_iptables *table,
220 struct connman_iptables_entry *head;
221 struct ipt_entry *entry;
222 struct xt_entry_target *target;
225 for (list = table->entries; list; list = list->next) {
230 builtin = head->builtin;
231 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
234 /* User defined chain */
235 target = ipt_get_target(entry);
236 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
237 !strcmp((char *)target->data, chain_name))
244 static GList *find_chain_tail(struct connman_iptables *table,
247 struct connman_iptables_entry *tail;
248 GList *chain_head, *list;
250 chain_head = find_chain_head(table, chain_name);
251 if (chain_head == NULL)
254 /* Then we look for the next chain */
255 for (list = chain_head->next; list; list = list->next) {
258 if (is_chain(table, tail))
262 /* Nothing found, we return the table end */
263 return g_list_last(table->entries);
267 static void update_offsets(struct connman_iptables *table)
270 struct connman_iptables_entry *entry, *prev_entry;
272 for (list = table->entries; list; list = list->next) {
275 if (list == table->entries) {
282 prev_entry = prev->data;
284 entry->offset = prev_entry->offset +
285 prev_entry->entry->next_offset;
289 static void update_targets_reference(struct connman_iptables *table,
290 struct connman_iptables_entry *entry_before,
291 struct connman_iptables_entry *modified_entry,
292 gboolean is_removing)
294 struct connman_iptables_entry *tmp;
295 struct xt_standard_target *t;
299 offset = modified_entry->entry->next_offset;
301 for (list = table->entries; list; list = list->next) {
307 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
309 if (is_removing == TRUE) {
310 if (t->verdict >= entry_before->offset)
311 t->verdict -= offset;
313 if (t->verdict > entry_before->offset)
314 t->verdict += offset;
319 static int iptables_add_entry(struct connman_iptables *table,
320 struct ipt_entry *entry, GList *before,
323 struct connman_iptables_entry *e, *entry_before;
328 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
333 e->builtin = builtin;
335 table->entries = g_list_insert_before(table->entries, before, e);
336 table->num_entries++;
337 table->size += entry->next_offset;
339 if (before == NULL) {
340 e->offset = table->size - entry->next_offset;
345 entry_before = before->data;
348 * We've just appended/insterted a new entry. All references
349 * should be bumped accordingly.
351 update_targets_reference(table, entry_before, e, FALSE);
353 update_offsets(table);
358 static int remove_table_entry(struct connman_iptables *table,
359 struct connman_iptables_entry *entry)
363 table->num_entries--;
364 table->size -= entry->entry->next_offset;
365 removed = entry->entry->next_offset;
367 g_free(entry->entry);
369 table->entries = g_list_remove(table->entries, entry);
374 static int iptables_flush_chain(struct connman_iptables *table,
377 GList *chain_head, *chain_tail, *list, *next;
378 struct connman_iptables_entry *entry;
379 int builtin, removed = 0;
381 chain_head = find_chain_head(table, name);
382 if (chain_head == NULL)
385 chain_tail = find_chain_tail(table, name);
386 if (chain_tail == NULL)
389 entry = chain_head->data;
390 builtin = entry->builtin;
395 list = chain_head->next;
397 if (list == chain_tail->prev)
400 while (list != chain_tail->prev) {
402 next = g_list_next(list);
404 removed += remove_table_entry(table, entry);
410 struct connman_iptables_entry *e;
414 entry->builtin = builtin;
416 table->underflow[builtin] -= removed;
418 for (list = chain_tail; list; list = list->next) {
421 builtin = e->builtin;
425 table->hook_entry[builtin] -= removed;
426 table->underflow[builtin] -= removed;
430 update_offsets(table);
435 static int iptables_add_chain(struct connman_iptables *table,
439 struct ipt_entry *entry_head;
440 struct ipt_entry *entry_return;
441 struct error_target *error;
442 struct ipt_standard_target *standard;
443 u_int16_t entry_head_size, entry_return_size;
445 last = g_list_last(table->entries);
448 * An empty chain is composed of:
449 * - A head entry, with no match and an error target.
450 * The error target data is the chain name.
451 * - A tail entry, with no match and a standard target.
452 * The standard target verdict is XT_RETURN (return to the
457 entry_head_size = sizeof(struct ipt_entry) +
458 sizeof(struct error_target);
459 entry_head = g_try_malloc0(entry_head_size);
460 if (entry_head == NULL)
463 memset(entry_head, 0, entry_head_size);
465 entry_head->target_offset = sizeof(struct ipt_entry);
466 entry_head->next_offset = entry_head_size;
468 error = (struct error_target *) entry_head->elems;
469 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
470 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
471 strcpy(error->error, name);
473 if (iptables_add_entry(table, entry_head, last, -1) < 0)
477 entry_return_size = sizeof(struct ipt_entry) +
478 sizeof(struct ipt_standard_target);
479 entry_return = g_try_malloc0(entry_return_size);
480 if (entry_return == NULL)
483 memset(entry_return, 0, entry_return_size);
485 entry_return->target_offset = sizeof(struct ipt_entry);
486 entry_return->next_offset = entry_return_size;
488 standard = (struct ipt_standard_target *) entry_return->elems;
489 standard->target.u.user.target_size =
490 ALIGN(sizeof(struct ipt_standard_target));
491 standard->verdict = XT_RETURN;
493 if (iptables_add_entry(table, entry_return, last, -1) < 0)
499 g_free(entry_return);
506 static int iptables_delete_chain(struct connman_iptables *table, char *name)
508 struct connman_iptables_entry *entry;
509 GList *chain_head, *chain_tail;
511 chain_head = find_chain_head(table, name);
512 if (chain_head == NULL)
515 entry = chain_head->data;
517 /* We cannot remove builtin chain */
518 if (entry->builtin >= 0)
521 chain_tail = find_chain_tail(table, name);
522 if (chain_tail == NULL)
525 /* Chain must be flushed */
526 if (chain_head->next != chain_tail->prev)
529 remove_table_entry(table, entry);
531 entry = chain_tail->prev->data;
532 remove_table_entry(table, entry);
534 update_offsets(table);
539 static struct ipt_entry *new_rule(struct ipt_ip *ip,
540 char *target_name, struct xtables_target *xt_t,
541 struct xtables_rule_match *xt_rm)
543 struct xtables_rule_match *tmp_xt_rm;
544 struct ipt_entry *new_entry;
545 size_t match_size, target_size;
548 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
549 match_size += tmp_xt_rm->match->m->u.match_size;
552 target_size = ALIGN(xt_t->t->u.target_size);
554 target_size = ALIGN(sizeof(struct xt_standard_target));
556 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
558 if (new_entry == NULL)
561 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
563 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
564 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
568 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
569 tmp_xt_rm = tmp_xt_rm->next) {
570 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
571 tmp_xt_rm->match->m->u.match_size);
572 match_size += tmp_xt_rm->match->m->u.match_size;
576 struct xt_entry_target *entry_target;
578 entry_target = ipt_get_target(new_entry);
579 memcpy(entry_target, xt_t->t, target_size);
585 static void update_hooks(struct connman_iptables *table, GList *chain_head,
586 struct ipt_entry *entry)
589 struct connman_iptables_entry *head, *e;
592 if (chain_head == NULL)
595 head = chain_head->data;
597 builtin = head->builtin;
601 table->underflow[builtin] += entry->next_offset;
603 for (list = chain_head->next; list; list = list->next) {
606 builtin = e->builtin;
610 table->hook_entry[builtin] += entry->next_offset;
611 table->underflow[builtin] += entry->next_offset;
615 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
616 struct ipt_ip *ip, char *chain_name,
617 char *target_name, struct xtables_target *xt_t,
618 int *builtin, struct xtables_rule_match *xt_rm)
620 GList *chain_tail, *chain_head;
621 struct ipt_entry *new_entry;
622 struct connman_iptables_entry *head;
624 chain_head = find_chain_head(table, chain_name);
625 if (chain_head == NULL)
628 chain_tail = find_chain_tail(table, chain_name);
629 if (chain_tail == NULL)
632 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
633 if (new_entry == NULL)
636 update_hooks(table, chain_head, new_entry);
639 * If the chain is builtin, and does not have any rule,
640 * then the one that we're inserting is becoming the head
641 * and thus needs the builtin flag.
643 head = chain_head->data;
644 if (head->builtin < 0)
646 else if (chain_head == chain_tail->prev) {
647 *builtin = head->builtin;
654 static int iptables_append_rule(struct connman_iptables *table,
655 struct ipt_ip *ip, char *chain_name,
656 char *target_name, struct xtables_target *xt_t,
657 struct xtables_rule_match *xt_rm)
660 struct ipt_entry *new_entry;
661 int builtin = -1, ret;
665 chain_tail = find_chain_tail(table, chain_name);
666 if (chain_tail == NULL)
669 new_entry = prepare_rule_inclusion(table, ip, chain_name,
670 target_name, xt_t, &builtin, xt_rm);
671 if (new_entry == NULL)
674 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
681 static int iptables_insert_rule(struct connman_iptables *table,
682 struct ipt_ip *ip, char *chain_name,
683 char *target_name, struct xtables_target *xt_t,
684 struct xtables_rule_match *xt_rm)
686 struct ipt_entry *new_entry;
687 int builtin = -1, ret;
690 chain_head = find_chain_head(table, chain_name);
691 if (chain_head == NULL)
694 new_entry = prepare_rule_inclusion(table, ip, chain_name,
695 target_name, xt_t, &builtin, xt_rm);
696 if (new_entry == NULL)
699 ret = iptables_add_entry(table, new_entry, chain_head->next, builtin);
706 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
707 struct ipt_entry *i_e2)
709 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
712 if (i_e1->target_offset != i_e2->target_offset)
715 if (i_e1->next_offset != i_e2->next_offset)
721 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
722 struct xt_entry_target *xt_e_t2)
724 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
727 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
728 struct xt_standard_target *xt_s_t1;
729 struct xt_standard_target *xt_s_t2;
731 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
732 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
734 if (xt_s_t1->verdict != xt_s_t2->verdict)
737 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
740 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
747 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
748 struct xt_entry_match *xt_e_m2)
750 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
753 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
756 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
759 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
765 static int iptables_delete_rule(struct connman_iptables *table,
766 struct ipt_ip *ip, char *chain_name,
767 char *target_name, struct xtables_target *xt_t,
768 struct xtables_match *xt_m,
769 struct xtables_rule_match *xt_rm)
771 GList *chain_tail, *chain_head, *list;
772 struct xt_entry_target *xt_e_t = NULL;
773 struct xt_entry_match *xt_e_m = NULL;
774 struct connman_iptables_entry *entry;
775 struct ipt_entry *entry_test;
776 int builtin, removed;
780 chain_head = find_chain_head(table, chain_name);
781 if (chain_head == NULL)
784 chain_tail = find_chain_tail(table, chain_name);
785 if (chain_tail == NULL)
791 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
792 if (entry_test == NULL)
796 xt_e_t = ipt_get_target(entry_test);
798 xt_e_m = (struct xt_entry_match *)entry_test->elems;
800 entry = chain_head->data;
801 builtin = entry->builtin;
806 list = chain_head->next;
808 for (entry = NULL; list != chain_tail->prev; list = list->next) {
809 struct connman_iptables_entry *tmp;
810 struct ipt_entry *tmp_e;
815 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
819 struct xt_entry_target *tmp_xt_e_t;
821 tmp_xt_e_t = ipt_get_target(tmp_e);
823 if (!is_same_target(tmp_xt_e_t, xt_e_t))
828 struct xt_entry_match *tmp_xt_e_m;
830 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
832 if (!is_same_match(tmp_xt_e_m, xt_e_m))
845 /* We have deleted a rule,
846 * all references should be bumped accordingly */
847 if (list->next != NULL)
848 update_targets_reference(table, list->next->data,
851 removed += remove_table_entry(table, entry);
857 entry->builtin = builtin;
860 table->underflow[builtin] -= removed;
861 for (list = chain_tail; list; list = list->next) {
864 builtin = entry->builtin;
868 table->hook_entry[builtin] -= removed;
869 table->underflow[builtin] -= removed;
873 update_offsets(table);
878 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
880 struct ipt_replace *r;
882 struct connman_iptables_entry *e;
883 unsigned char *entry_index;
885 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
889 memset(r, 0, sizeof(*r) + table->size);
891 r->counters = g_try_malloc0(sizeof(struct xt_counters)
892 * table->old_entries);
893 if (r->counters == NULL) {
898 strcpy(r->name, table->info->name);
899 r->num_entries = table->num_entries;
900 r->size = table->size;
902 r->num_counters = table->old_entries;
903 r->valid_hooks = table->info->valid_hooks;
905 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
906 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
908 entry_index = (unsigned char *)r->entries;
909 for (list = table->entries; list; list = list->next) {
912 memcpy(entry_index, e->entry, e->entry->next_offset);
913 entry_index += e->entry->next_offset;
919 static void dump_ip(struct ipt_entry *entry)
921 struct ipt_ip *ip = &entry->ip;
922 char ip_string[INET6_ADDRSTRLEN];
923 char ip_mask[INET6_ADDRSTRLEN];
925 if (strlen(ip->iniface))
926 connman_info("\tin %s", ip->iniface);
928 if (strlen(ip->outiface))
929 connman_info("\tout %s", ip->outiface);
931 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
932 inet_ntop(AF_INET, &ip->smsk,
933 ip_mask, INET6_ADDRSTRLEN) != NULL)
934 connman_info("\tsrc %s/%s", ip_string, ip_mask);
936 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
937 inet_ntop(AF_INET, &ip->dmsk,
938 ip_mask, INET6_ADDRSTRLEN) != NULL)
939 connman_info("\tdst %s/%s", ip_string, ip_mask);
942 static void dump_target(struct connman_iptables *table,
943 struct ipt_entry *entry)
946 struct xtables_target *xt_t;
947 struct xt_entry_target *target;
949 target = ipt_get_target(entry);
951 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
952 struct xt_standard_target *t;
954 t = (struct xt_standard_target *)target;
956 switch (t->verdict) {
958 connman_info("\ttarget RETURN");
962 connman_info("\ttarget ACCEPT");
966 connman_info("\ttarget DROP");
970 connman_info("\ttarget QUEUE");
974 connman_info("\ttarget STOP");
978 connman_info("\tJUMP @%p (0x%x)",
979 (char*)table->blob_entries->entrytable +
980 t->verdict, t->verdict);
984 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
985 XTF_LOAD_MUST_SUCCEED);
987 if(xt_t->print != NULL)
988 xt_t->print(NULL, target, 1);
990 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
992 connman_info("\ttarget %s", target->u.user.name);
996 if(xt_t->print != NULL) {
997 connman_info("\ttarget ");
998 xt_t->print(NULL, target, 1);
1003 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1005 struct xtables_match *xt_m;
1006 struct xt_entry_match *match;
1008 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1011 match = (struct xt_entry_match *) entry->elems;
1013 if (!strlen(match->u.user.name))
1016 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1020 if(xt_m->print != NULL) {
1021 connman_info("\tmatch ");
1022 xt_m->print(NULL, match, 1);
1028 connman_info("\tmatch %s", match->u.user.name);
1032 static int dump_entry(struct ipt_entry *entry,
1033 struct connman_iptables *table)
1035 struct xt_entry_target *target;
1036 unsigned int offset;
1039 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1040 target = ipt_get_target(entry);
1041 builtin = is_hook_entry(table, entry);
1043 if (entry_to_offset(table, entry) + entry->next_offset ==
1044 table->blob_entries->size) {
1045 connman_info("End of CHAIN 0x%x", offset);
1049 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1050 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
1051 target->data, entry, entry->elems,
1052 (char *)entry + entry->target_offset,
1053 entry->next_offset);
1056 } else if (builtin >= 0) {
1057 connman_info("CHAIN (%s) %p match %p target %p size %d",
1058 hooknames[builtin], entry, entry->elems,
1059 (char *)entry + entry->target_offset,
1060 entry->next_offset);
1062 connman_info("RULE %p match %p target %p size %d", entry,
1064 (char *)entry + entry->target_offset,
1065 entry->next_offset);
1068 dump_match(table, entry);
1069 dump_target(table, entry);
1075 static void iptables_dump(struct connman_iptables *table)
1077 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1079 table->info->valid_hooks, table->info->num_entries,
1082 ENTRY_ITERATE(table->blob_entries->entrytable,
1083 table->blob_entries->size,
1088 static int iptables_get_entries(struct connman_iptables *table)
1090 socklen_t entry_size;
1092 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1094 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1095 table->blob_entries, &entry_size);
1098 static int iptables_replace(struct connman_iptables *table,
1099 struct ipt_replace *r)
1101 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1102 sizeof(*r) + r->size);
1105 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1107 struct ipt_entry *new_entry;
1110 new_entry = g_try_malloc0(entry->next_offset);
1111 if (new_entry == NULL)
1114 memcpy(new_entry, entry, entry->next_offset);
1116 builtin = is_hook_entry(table, entry);
1118 return iptables_add_entry(table, new_entry, NULL, builtin);
1121 static void table_cleanup(struct connman_iptables *table)
1124 struct connman_iptables_entry *entry;
1126 close(table->ipt_sock);
1128 for (list = table->entries; list; list = list->next) {
1131 g_free(entry->entry);
1135 g_list_free(table->entries);
1136 g_free(table->info);
1137 g_free(table->blob_entries);
1141 static struct connman_iptables *iptables_init(char *table_name)
1143 struct connman_iptables *table = NULL;
1144 char *module = NULL;
1147 DBG("%s", table_name);
1149 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1152 module = g_strconcat("iptable_", table_name, NULL);
1156 if (xtables_insmod(module, NULL, TRUE) != 0)
1162 table = g_hash_table_lookup(table_hash, table_name);
1166 table = g_try_new0(struct connman_iptables, 1);
1170 table->info = g_try_new0(struct ipt_getinfo, 1);
1171 if (table->info == NULL)
1174 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1175 if (table->ipt_sock < 0)
1178 s = sizeof(*table->info);
1179 strcpy(table->info->name, table_name);
1180 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1181 table->info, &s) < 0)
1184 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1186 if (table->blob_entries == NULL)
1189 strcpy(table->blob_entries->name, table_name);
1190 table->blob_entries->size = table->info->size;
1192 if (iptables_get_entries(table) < 0)
1195 table->num_entries = 0;
1196 table->old_entries = table->info->num_entries;
1199 memcpy(table->underflow, table->info->underflow,
1200 sizeof(table->info->underflow));
1201 memcpy(table->hook_entry, table->info->hook_entry,
1202 sizeof(table->info->hook_entry));
1204 ENTRY_ITERATE(table->blob_entries->entrytable,
1205 table->blob_entries->size,
1208 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1215 table_cleanup(table);
1220 static struct option iptables_opts[] = {
1221 {.name = "append", .has_arg = 1, .val = 'A'},
1222 {.name = "delete", .has_arg = 1, .val = 'D'},
1223 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1224 {.name = "insert", .has_arg = 1, .val = 'I'},
1225 {.name = "list", .has_arg = 2, .val = 'L'},
1226 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1227 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1228 {.name = "destination", .has_arg = 1, .val = 'd'},
1229 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1230 {.name = "jump", .has_arg = 1, .val = 'j'},
1231 {.name = "match", .has_arg = 1, .val = 'm'},
1232 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1233 {.name = "source", .has_arg = 1, .val = 's'},
1234 {.name = "table", .has_arg = 1, .val = 't'},
1238 struct xtables_globals iptables_globals = {
1240 .opts = iptables_opts,
1241 .orig_opts = iptables_opts,
1244 static struct xtables_target *prepare_target(struct connman_iptables *table,
1247 struct xtables_target *xt_t = NULL;
1248 gboolean is_builtin, is_user_defined;
1249 GList *chain_head = NULL;
1253 is_user_defined = FALSE;
1255 if (is_builtin_target(target_name))
1258 chain_head = find_chain_head(table, target_name);
1259 if (chain_head != NULL && chain_head->next != NULL)
1260 is_user_defined = TRUE;
1263 if (is_builtin || is_user_defined)
1264 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1265 XTF_LOAD_MUST_SUCCEED);
1267 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1272 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1274 xt_t->t = g_try_malloc0(target_size);
1275 if (xt_t->t == NULL)
1278 xt_t->t->u.target_size = target_size;
1280 if (is_builtin || is_user_defined) {
1281 struct xt_standard_target *target;
1283 target = (struct xt_standard_target *)(xt_t->t);
1284 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1286 if (is_builtin == TRUE)
1287 target->verdict = target_to_verdict(target_name);
1288 else if (is_user_defined == TRUE) {
1289 struct connman_iptables_entry *target_rule;
1291 if (chain_head == NULL) {
1296 target_rule = chain_head->next->data;
1297 target->verdict = target_rule->offset;
1300 strcpy(xt_t->t->u.user.name, target_name);
1301 xt_t->t->u.user.revision = xt_t->revision;
1302 if (xt_t->init != NULL)
1303 xt_t->init(xt_t->t);
1306 if (xt_t->x6_options != NULL)
1307 iptables_globals.opts =
1308 xtables_options_xfrm(
1309 #if XTABLES_VERSION_CODE > 5
1310 iptables_globals.orig_opts,
1312 iptables_globals.opts,
1314 &xt_t->option_offset);
1316 iptables_globals.opts =
1317 xtables_merge_options(
1318 #if XTABLES_VERSION_CODE > 5
1319 iptables_globals.orig_opts,
1321 iptables_globals.opts,
1323 &xt_t->option_offset);
1325 if (iptables_globals.opts == NULL) {
1333 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1334 struct xtables_rule_match **xt_rm, char *match_name)
1336 struct xtables_match *xt_m;
1339 if (match_name == NULL)
1342 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1343 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1345 xt_m->m = g_try_malloc0(match_size);
1346 if (xt_m->m == NULL)
1349 xt_m->m->u.match_size = match_size;
1350 strcpy(xt_m->m->u.user.name, xt_m->name);
1351 xt_m->m->u.user.revision = xt_m->revision;
1353 if (xt_m->init != NULL)
1354 xt_m->init(xt_m->m);
1356 if (xt_m == xt_m->next)
1359 if (xt_m->x6_options != NULL)
1360 iptables_globals.opts =
1361 xtables_options_xfrm(
1362 #if XTABLES_VERSION_CODE > 5
1363 iptables_globals.orig_opts,
1365 iptables_globals.opts,
1367 &xt_m->option_offset);
1369 iptables_globals.opts =
1370 xtables_merge_options(
1371 #if XTABLES_VERSION_CODE > 5
1372 iptables_globals.orig_opts,
1374 iptables_globals.opts,
1376 &xt_m->option_offset);
1378 if (iptables_globals.opts == NULL) {
1387 static int iptables_command(int argc, char *argv[])
1389 struct connman_iptables *table;
1390 struct xtables_rule_match *xt_rm, *tmp_xt_rm;
1391 struct xtables_match *xt_m, *xt_m_t;
1392 struct xtables_target *xt_t;
1394 char *table_name, *chain, *new_chain, *match_name, *target_name;
1395 char *flush_chain, *delete_chain;
1396 int c, ret, in_len, out_len;
1397 gboolean dump, invert, insert, delete;
1398 struct in_addr src, dst;
1407 table_name = chain = new_chain = match_name = target_name = NULL;
1408 flush_chain = delete_chain = NULL;
1409 memset(&ip, 0, sizeof(struct ipt_ip));
1416 /* extension's options will generate false-positives errors */
1421 while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:j:i:m:o:s:t:",
1422 iptables_globals.opts, NULL)) != -1) {
1425 /* It is either -A, -D or -I at once */
1433 /* It is either -A, -D or -I at once */
1442 flush_chain = optarg;
1446 /* It is either -A, -D or -I at once */
1463 delete_chain = optarg;
1467 if (!inet_pton(AF_INET, optarg, &dst))
1471 inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1474 ip.invflags |= IPT_INV_DSTIP;
1479 in_len = strlen(optarg);
1481 if (in_len + 1 > IFNAMSIZ)
1484 strcpy(ip.iniface, optarg);
1485 memset(ip.iniface_mask, 0xff, in_len + 1);
1488 ip.invflags |= IPT_INV_VIA_IN;
1493 target_name = optarg;
1494 xt_t = prepare_target(table, target_name);
1501 match_name = optarg;
1502 xt_m = prepare_matches(table, &xt_rm, match_name);
1509 out_len = strlen(optarg);
1511 if (out_len + 1 > IFNAMSIZ)
1514 strcpy(ip.outiface, optarg);
1515 memset(ip.outiface_mask, 0xff, out_len + 1);
1518 ip.invflags |= IPT_INV_VIA_OUT;
1523 if (!inet_pton(AF_INET, optarg, &src))
1527 inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1530 ip.invflags |= IPT_INV_SRCIP;
1535 table_name = optarg;
1539 if (optarg[0] == '!' && optarg[1] == '\0') {
1545 connman_error("Invalid option");
1551 if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1552 xt_t->parse != NULL) &&
1553 (c >= (int) xt_t->option_offset &&
1554 c < (int) xt_t->option_offset +
1555 XT_OPTION_OFFSET_SCALE)) {
1556 xtables_option_tpcall(c, argv,
1557 invert, xt_t, NULL);
1562 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1563 tmp_xt_rm = tmp_xt_rm->next) {
1564 xt_m_t = tmp_xt_rm->match;
1566 if (tmp_xt_rm->completed ||
1567 (xt_m_t->x6_parse == NULL &&
1568 xt_m_t->parse == NULL))
1571 if (c < (int) xt_m_t->option_offset ||
1572 c >= (int) xt_m_t->option_offset
1573 + XT_OPTION_OFFSET_SCALE)
1576 xtables_option_mpcall(c, argv,
1577 invert, xt_m_t, NULL);
1588 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1589 tmp_xt_rm = tmp_xt_rm->next)
1590 xtables_option_mfcall(tmp_xt_rm->match);
1593 xtables_option_tfcall(xt_t);
1595 if (table_name == NULL)
1596 table_name = "filter";
1598 table = iptables_init(table_name);
1599 if (table == NULL) {
1604 if (delete_chain != NULL) {
1605 printf("Delete chain %s\n", delete_chain);
1607 iptables_delete_chain(table, delete_chain);
1613 iptables_dump(table);
1620 DBG("Flush chain %s", flush_chain);
1622 iptables_flush_chain(table, flush_chain);
1627 if (chain && new_chain) {
1633 DBG("New chain %s", new_chain);
1635 ret = iptables_add_chain(table, new_chain);
1643 if (delete == TRUE) {
1644 DBG("Deleting %s to %s (match %s)\n",
1645 target_name, chain, match_name);
1647 ret = iptables_delete_rule(table, &ip, chain,
1648 target_name, xt_t, xt_m, xt_rm);
1653 if (insert == TRUE) {
1654 DBG("Inserting %s to %s (match %s)",
1655 target_name, chain, match_name);
1657 ret = iptables_insert_rule(table, &ip, chain,
1658 target_name, xt_t, xt_rm);
1662 DBG("Adding %s to %s (match %s)",
1663 target_name, chain, match_name);
1665 ret = iptables_append_rule(table, &ip, chain,
1666 target_name, xt_t, xt_rm);
1682 int __connman_iptables_command(const char *format, ...)
1684 char **argv, **arguments, *command;
1691 va_start(args, format);
1693 command = g_strdup_vprintf(format, args);
1697 if (command == NULL)
1700 arguments = g_strsplit_set(command, " ", -1);
1702 for (argc = 0; arguments[argc]; argc++);
1705 DBG("command %s argc %d", command, argc);
1707 argv = g_try_malloc0(argc * sizeof(char *));
1710 g_strfreev(arguments);
1714 argv[0] = "iptables";
1715 for (i = 1; i < argc; i++)
1716 argv[i] = arguments[i - 1];
1718 ret = iptables_command(argc, argv);
1721 g_strfreev(arguments);
1728 int __connman_iptables_commit(const char *table_name)
1730 struct connman_iptables *table;
1731 struct ipt_replace *repl;
1734 DBG("%s", table_name);
1736 table = g_hash_table_lookup(table_hash, table_name);
1740 repl = iptables_blob(table);
1742 err = iptables_replace(table, repl);
1744 g_free(repl->counters);
1750 g_hash_table_remove(table_hash, table_name);
1755 static void remove_table(gpointer user_data)
1757 struct connman_iptables *table = user_data;
1759 table_cleanup(table);
1762 int __connman_iptables_init(void)
1766 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1767 g_free, remove_table);
1769 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
1775 void __connman_iptables_cleanup(void)
1779 g_hash_table_destroy(table_hash);
1781 xtables_free_opts(1);