5 * Copyright (C) 2007-2012 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>
41 * Some comments on how the iptables API works (some of them from the
42 * source code from iptables and the kernel):
44 * - valid_hooks: bit indicates valid IDs for hook_entry
45 * - hook_entry[ID] offset to the chain start
46 * - overflows should be end of entry chains, and uncodintional policy nodes.
47 * - policy entry: last entry in a chain
48 * - user chain: end of last builtin + policy entry
49 * - final entry must be error node
50 * - Underflows must be unconditional and use the STANDARD target with
52 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
53 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
54 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
55 * table header/meta info. The table is appended after the header. The entries
56 * are of the type struct ipt_entry.
57 * - After the ipt_entry the matches are appended. After the matches
58 * the target is appended.
59 * - ipt_entry->target_offset = Size of ipt_entry + matches
60 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
61 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
62 * - hook_entry and overflow mark the begining and the end of a chain, e.g
63 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
64 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
65 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
66 * the last element). FORWARD has one entry starting/ending at 352. The entry
67 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
68 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
70 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
71 * writing that table looks like this:
73 * filter valid_hooks 0x0000000e num_entries 5 size 856
74 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
75 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
76 * entry 0x699d30 offset 0 size 224
77 * RULE match 0x699da0 target 0x699dd0
78 * match mark match 0x3e7
79 * target LOG flags 0 level 4
82 * entry 0x699e10 offset 224 size 152
83 * RULE match 0x699e80 target 0x699e80
87 * entry 0x699ea8 offset 376 size 152
88 * RULE match 0x699f18 target 0x699f18
92 * entry 0x699f40 offset 528 size 152
93 * RULE match 0x699fb0 target 0x699fb0
97 * entry 0x699fd8 offset 680 size 176
98 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
100 * Reading the filter table looks like this:
102 * filter valid_hooks 0x0000000e num_entries 5 size 856
103 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
104 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
105 * entry 0x25fec28 offset 0 size 224
106 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
107 * match mark match 0x3e7
108 * target LOG flags 0 level 4
109 * src 0.0.0.0/0.0.0.0
110 * dst 0.0.0.0/0.0.0.0
111 * entry 0x25fed08 offset 224 size 152
112 * RULE match 0x25fed78 target 0x25fed78
114 * src 0.0.0.0/0.0.0.0
115 * dst 0.0.0.0/0.0.0.0
116 * entry 0x25feda0 offset 376 size 152
117 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
119 * src 0.0.0.0/0.0.0.0
120 * dst 0.0.0.0/0.0.0.0
121 * entry 0x25fee38 offset 528 size 152
122 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
124 * src 0.0.0.0/0.0.0.0
125 * dst 0.0.0.0/0.0.0.0
126 * entry 0x25feed0 offset 680 size 176
130 static const char *hooknames[] = {
131 [NF_IP_PRE_ROUTING] = "PREROUTING",
132 [NF_IP_LOCAL_IN] = "INPUT",
133 [NF_IP_FORWARD] = "FORWARD",
134 [NF_IP_LOCAL_OUT] = "OUTPUT",
135 [NF_IP_POST_ROUTING] = "POSTROUTING",
138 #define LABEL_ACCEPT "ACCEPT"
139 #define LABEL_DROP "DROP"
140 #define LABEL_QUEUE "QUEUE"
141 #define LABEL_RETURN "RETURN"
143 #define XT_OPTION_OFFSET_SCALE 256
145 /* fn returns 0 to continue iteration */
146 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
153 for (__i = 0, __n = 0; __i < (size); \
154 __i += __entry->next_offset, __n++) { \
155 __entry = (void *)(entries) + __i; \
159 __ret = fn(__entry, ## args); \
166 /* fn returns 0 to continue iteration */
167 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
168 _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
170 #define ENTRY_ITERATE(entries, size, fn, args...) \
171 _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
173 #define MIN_ALIGN (__alignof__(struct ipt_entry))
175 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
177 struct error_target {
178 struct xt_entry_target t;
179 char error[IPT_TABLE_MAXNAMELEN];
182 struct connman_iptables_entry {
186 struct ipt_entry *entry;
189 struct connman_iptables {
192 struct ipt_getinfo *info;
193 struct ipt_get_entries *blob_entries;
195 unsigned int num_entries;
196 unsigned int old_entries;
199 unsigned int underflow[NF_INET_NUMHOOKS];
200 unsigned int hook_entry[NF_INET_NUMHOOKS];
205 static GHashTable *table_hash = NULL;
207 static struct ipt_entry *get_entry(struct connman_iptables *table,
210 return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
214 static int is_hook_entry(struct connman_iptables *table,
215 struct ipt_entry *entry)
219 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
220 if ((table->info->valid_hooks & (1 << i))
221 && get_entry(table, table->info->hook_entry[i]) == entry)
228 static unsigned long entry_to_offset(struct connman_iptables *table,
229 struct ipt_entry *entry)
231 return (void *)entry - (void *)table->blob_entries->entrytable;
234 static int target_to_verdict(const char *target_name)
236 if (!strcmp(target_name, LABEL_ACCEPT))
237 return -NF_ACCEPT - 1;
239 if (!strcmp(target_name, LABEL_DROP))
242 if (!strcmp(target_name, LABEL_QUEUE))
243 return -NF_QUEUE - 1;
245 if (!strcmp(target_name, LABEL_RETURN))
251 static gboolean is_builtin_target(const char *target_name)
253 if (!strcmp(target_name, LABEL_ACCEPT) ||
254 !strcmp(target_name, LABEL_DROP) ||
255 !strcmp(target_name, LABEL_QUEUE) ||
256 !strcmp(target_name, LABEL_RETURN))
262 static gboolean is_jump(struct connman_iptables_entry *e)
264 struct xt_entry_target *target;
266 target = ipt_get_target(e->entry);
268 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
269 struct xt_standard_target *t;
271 t = (struct xt_standard_target *)target;
273 switch (t->verdict) {
289 static gboolean is_chain(struct connman_iptables *table,
290 struct connman_iptables_entry *e)
292 struct ipt_entry *entry;
293 struct xt_entry_target *target;
299 target = ipt_get_target(entry);
300 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
306 static GList *find_chain_head(struct connman_iptables *table,
307 const char *chain_name)
310 struct connman_iptables_entry *head;
311 struct ipt_entry *entry;
312 struct xt_entry_target *target;
315 for (list = table->entries; list; list = list->next) {
320 builtin = head->builtin;
321 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
324 /* User defined chain */
325 target = ipt_get_target(entry);
326 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
327 !strcmp((char *)target->data, chain_name))
334 static GList *find_chain_tail(struct connman_iptables *table,
335 const char *chain_name)
337 struct connman_iptables_entry *tail;
338 GList *chain_head, *list;
340 chain_head = find_chain_head(table, chain_name);
341 if (chain_head == NULL)
344 /* Then we look for the next chain */
345 for (list = chain_head->next; list; list = list->next) {
348 if (is_chain(table, tail))
352 /* Nothing found, we return the table end */
353 return g_list_last(table->entries);
357 static void update_offsets(struct connman_iptables *table)
360 struct connman_iptables_entry *entry, *prev_entry;
362 for (list = table->entries; list; list = list->next) {
365 if (list == table->entries) {
372 prev_entry = prev->data;
374 entry->offset = prev_entry->offset +
375 prev_entry->entry->next_offset;
379 static void update_targets_reference(struct connman_iptables *table,
380 struct connman_iptables_entry *entry_before,
381 struct connman_iptables_entry *modified_entry,
382 gboolean is_removing)
384 struct connman_iptables_entry *tmp;
385 struct xt_standard_target *t;
389 offset = modified_entry->entry->next_offset;
391 for (list = table->entries; list; list = list->next) {
397 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
399 if (is_removing == TRUE) {
400 if (t->verdict >= entry_before->offset)
401 t->verdict -= offset;
403 if (t->verdict > entry_before->offset)
404 t->verdict += offset;
409 static int iptables_add_entry(struct connman_iptables *table,
410 struct ipt_entry *entry, GList *before,
413 struct connman_iptables_entry *e, *entry_before;
418 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
423 e->builtin = builtin;
425 table->entries = g_list_insert_before(table->entries, before, e);
426 table->num_entries++;
427 table->size += entry->next_offset;
429 if (before == NULL) {
430 e->offset = table->size - entry->next_offset;
435 entry_before = before->data;
438 * We've just appended/insterted a new entry. All references
439 * should be bumped accordingly.
441 update_targets_reference(table, entry_before, e, FALSE);
443 update_offsets(table);
448 static int remove_table_entry(struct connman_iptables *table,
449 struct connman_iptables_entry *entry)
453 table->num_entries--;
454 table->size -= entry->entry->next_offset;
455 removed = entry->entry->next_offset;
457 g_free(entry->entry);
459 table->entries = g_list_remove(table->entries, entry);
464 static int iptables_flush_chain(struct connman_iptables *table,
467 GList *chain_head, *chain_tail, *list, *next;
468 struct connman_iptables_entry *entry;
469 int builtin, removed = 0;
471 chain_head = find_chain_head(table, name);
472 if (chain_head == NULL)
475 chain_tail = find_chain_tail(table, name);
476 if (chain_tail == NULL)
479 entry = chain_head->data;
480 builtin = entry->builtin;
485 list = chain_head->next;
487 if (list == chain_tail->prev)
490 while (list != chain_tail->prev) {
492 next = g_list_next(list);
494 removed += remove_table_entry(table, entry);
500 struct connman_iptables_entry *e;
504 entry->builtin = builtin;
506 table->underflow[builtin] -= removed;
508 for (list = chain_tail; list; list = list->next) {
511 builtin = e->builtin;
515 table->hook_entry[builtin] -= removed;
516 table->underflow[builtin] -= removed;
520 update_offsets(table);
525 static int iptables_add_chain(struct connman_iptables *table,
529 struct ipt_entry *entry_head;
530 struct ipt_entry *entry_return;
531 struct error_target *error;
532 struct ipt_standard_target *standard;
533 u_int16_t entry_head_size, entry_return_size;
535 last = g_list_last(table->entries);
538 * An empty chain is composed of:
539 * - A head entry, with no match and an error target.
540 * The error target data is the chain name.
541 * - A tail entry, with no match and a standard target.
542 * The standard target verdict is XT_RETURN (return to the
547 entry_head_size = sizeof(struct ipt_entry) +
548 sizeof(struct error_target);
549 entry_head = g_try_malloc0(entry_head_size);
550 if (entry_head == NULL)
553 memset(entry_head, 0, entry_head_size);
555 entry_head->target_offset = sizeof(struct ipt_entry);
556 entry_head->next_offset = entry_head_size;
558 error = (struct error_target *) entry_head->elems;
559 strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
560 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
561 strcpy(error->error, name);
563 if (iptables_add_entry(table, entry_head, last, -1) < 0)
567 entry_return_size = sizeof(struct ipt_entry) +
568 sizeof(struct ipt_standard_target);
569 entry_return = g_try_malloc0(entry_return_size);
570 if (entry_return == NULL)
573 memset(entry_return, 0, entry_return_size);
575 entry_return->target_offset = sizeof(struct ipt_entry);
576 entry_return->next_offset = entry_return_size;
578 standard = (struct ipt_standard_target *) entry_return->elems;
579 standard->target.u.user.target_size =
580 ALIGN(sizeof(struct ipt_standard_target));
581 standard->verdict = XT_RETURN;
583 if (iptables_add_entry(table, entry_return, last, -1) < 0)
589 g_free(entry_return);
596 static int iptables_delete_chain(struct connman_iptables *table,
599 struct connman_iptables_entry *entry;
600 GList *chain_head, *chain_tail;
602 chain_head = find_chain_head(table, name);
603 if (chain_head == NULL)
606 entry = chain_head->data;
608 /* We cannot remove builtin chain */
609 if (entry->builtin >= 0)
612 chain_tail = find_chain_tail(table, name);
613 if (chain_tail == NULL)
616 /* Chain must be flushed */
617 if (chain_head->next != chain_tail->prev)
620 remove_table_entry(table, entry);
622 entry = chain_tail->prev->data;
623 remove_table_entry(table, entry);
625 update_offsets(table);
630 static struct ipt_entry *new_rule(struct ipt_ip *ip,
631 const char *target_name, struct xtables_target *xt_t,
632 struct xtables_rule_match *xt_rm)
634 struct xtables_rule_match *tmp_xt_rm;
635 struct ipt_entry *new_entry;
636 size_t match_size, target_size;
639 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
640 match_size += tmp_xt_rm->match->m->u.match_size;
643 target_size = ALIGN(xt_t->t->u.target_size);
645 target_size = ALIGN(sizeof(struct xt_standard_target));
647 new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
649 if (new_entry == NULL)
652 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
654 new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
655 new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
659 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
660 tmp_xt_rm = tmp_xt_rm->next) {
661 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
662 tmp_xt_rm->match->m->u.match_size);
663 match_size += tmp_xt_rm->match->m->u.match_size;
667 struct xt_entry_target *entry_target;
669 entry_target = ipt_get_target(new_entry);
670 memcpy(entry_target, xt_t->t, target_size);
676 static void update_hooks(struct connman_iptables *table, GList *chain_head,
677 struct ipt_entry *entry)
680 struct connman_iptables_entry *head, *e;
683 if (chain_head == NULL)
686 head = chain_head->data;
688 builtin = head->builtin;
692 table->underflow[builtin] += entry->next_offset;
694 for (list = chain_head->next; list; list = list->next) {
697 builtin = e->builtin;
701 table->hook_entry[builtin] += entry->next_offset;
702 table->underflow[builtin] += entry->next_offset;
706 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
707 struct ipt_ip *ip, const char *chain_name,
708 const char *target_name,
709 struct xtables_target *xt_t,
710 int *builtin, struct xtables_rule_match *xt_rm)
712 GList *chain_tail, *chain_head;
713 struct ipt_entry *new_entry;
714 struct connman_iptables_entry *head;
716 chain_head = find_chain_head(table, chain_name);
717 if (chain_head == NULL)
720 chain_tail = find_chain_tail(table, chain_name);
721 if (chain_tail == NULL)
724 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
725 if (new_entry == NULL)
728 update_hooks(table, chain_head, new_entry);
731 * If the chain is builtin, and does not have any rule,
732 * then the one that we're inserting is becoming the head
733 * and thus needs the builtin flag.
735 head = chain_head->data;
736 if (head->builtin < 0)
738 else if (chain_head == chain_tail->prev) {
739 *builtin = head->builtin;
746 static int iptables_append_rule(struct connman_iptables *table,
747 struct ipt_ip *ip, char *chain_name,
748 char *target_name, struct xtables_target *xt_t,
749 struct xtables_rule_match *xt_rm)
752 struct ipt_entry *new_entry;
753 int builtin = -1, ret;
757 chain_tail = find_chain_tail(table, chain_name);
758 if (chain_tail == NULL)
761 new_entry = prepare_rule_inclusion(table, ip, chain_name,
762 target_name, xt_t, &builtin, xt_rm);
763 if (new_entry == NULL)
766 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
773 static int iptables_insert_rule(struct connman_iptables *table,
774 struct ipt_ip *ip, const char *chain_name,
775 const char *target_name,
776 struct xtables_target *xt_t,
777 struct xtables_rule_match *xt_rm)
779 struct ipt_entry *new_entry;
780 int builtin = -1, ret;
783 chain_head = find_chain_head(table, chain_name);
784 if (chain_head == NULL)
787 new_entry = prepare_rule_inclusion(table, ip, chain_name,
788 target_name, xt_t, &builtin, xt_rm);
789 if (new_entry == NULL)
793 chain_head = chain_head->next;
795 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
802 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
803 struct ipt_entry *i_e2)
805 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
808 if (i_e1->target_offset != i_e2->target_offset)
811 if (i_e1->next_offset != i_e2->next_offset)
817 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
818 struct xt_entry_target *xt_e_t2)
820 if (xt_e_t1 == NULL || xt_e_t2 == NULL)
823 if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
824 struct xt_standard_target *xt_s_t1;
825 struct xt_standard_target *xt_s_t2;
827 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
828 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
830 if (xt_s_t1->verdict != xt_s_t2->verdict)
833 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
836 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
843 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
844 struct xt_entry_match *xt_e_m2)
846 if (xt_e_m1 == NULL || xt_e_m2 == NULL)
849 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
852 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
855 if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
861 static GList *find_existing_rule(struct connman_iptables *table,
862 struct ipt_ip *ip, const char *chain_name,
863 const char *target_name,
864 struct xtables_target *xt_t,
865 struct xtables_match *xt_m,
866 struct xtables_rule_match *xt_rm)
868 GList *chain_tail, *chain_head, *list;
869 struct xt_entry_target *xt_e_t = NULL;
870 struct xt_entry_match *xt_e_m = NULL;
871 struct connman_iptables_entry *entry;
872 struct ipt_entry *entry_test;
875 chain_head = find_chain_head(table, chain_name);
876 if (chain_head == NULL)
879 chain_tail = find_chain_tail(table, chain_name);
880 if (chain_tail == NULL)
886 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
887 if (entry_test == NULL)
891 xt_e_t = ipt_get_target(entry_test);
893 xt_e_m = (struct xt_entry_match *)entry_test->elems;
895 entry = chain_head->data;
896 builtin = entry->builtin;
901 list = chain_head->next;
903 for (; list != chain_tail->prev; list = list->next) {
904 struct connman_iptables_entry *tmp;
905 struct ipt_entry *tmp_e;
910 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
914 struct xt_entry_target *tmp_xt_e_t;
916 tmp_xt_e_t = ipt_get_target(tmp_e);
918 if (!is_same_target(tmp_xt_e_t, xt_e_t))
923 struct xt_entry_match *tmp_xt_e_m;
925 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
927 if (!is_same_match(tmp_xt_e_m, xt_e_m))
936 if (list != chain_tail->prev)
942 static int iptables_delete_rule(struct connman_iptables *table,
943 struct ipt_ip *ip, const char *chain_name,
944 const char *target_name,
945 struct xtables_target *xt_t,
946 struct xtables_match *xt_m,
947 struct xtables_rule_match *xt_rm)
949 struct connman_iptables_entry *entry;
950 GList *chain_tail, *list;
951 int builtin, removed;
955 chain_tail = find_chain_tail(table, chain_name);
956 if (chain_tail == NULL)
959 list = find_existing_rule(table, ip, chain_name, target_name,
968 builtin = entry->builtin;
970 /* We have deleted a rule,
971 * all references should be bumped accordingly */
972 if (list->next != NULL)
973 update_targets_reference(table, list->next->data,
976 removed += remove_table_entry(table, entry);
982 entry->builtin = builtin;
985 table->underflow[builtin] -= removed;
986 for (list = chain_tail; list; list = list->next) {
989 builtin = entry->builtin;
993 table->hook_entry[builtin] -= removed;
994 table->underflow[builtin] -= removed;
998 update_offsets(table);
1003 static int iptables_compare_rule(struct connman_iptables *table,
1004 struct ipt_ip *ip, const char *chain_name,
1005 const char *target_name,
1006 struct xtables_target *xt_t,
1007 struct xtables_match *xt_m,
1008 struct xtables_rule_match *xt_rm)
1010 struct connman_iptables_entry *entry;
1013 found = find_existing_rule(table, ip, chain_name, target_name,
1018 entry = found->data;
1026 static int iptables_change_policy(struct connman_iptables *table,
1027 const char *chain_name, const char *policy)
1030 struct connman_iptables_entry *entry;
1031 struct xt_entry_target *target;
1032 struct xt_standard_target *t;
1035 verdict = target_to_verdict(policy);
1039 chain_head = find_chain_head(table, chain_name);
1040 if (chain_head == NULL)
1043 entry = chain_head->data;
1044 if (entry->builtin < 0)
1047 target = ipt_get_target(entry->entry);
1049 t = (struct xt_standard_target *)target;
1050 t->verdict = verdict;
1055 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1057 struct ipt_replace *r;
1059 struct connman_iptables_entry *e;
1060 unsigned char *entry_index;
1062 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1066 memset(r, 0, sizeof(*r) + table->size);
1068 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1069 * table->old_entries);
1070 if (r->counters == NULL) {
1075 strcpy(r->name, table->info->name);
1076 r->num_entries = table->num_entries;
1077 r->size = table->size;
1079 r->num_counters = table->old_entries;
1080 r->valid_hooks = table->info->valid_hooks;
1082 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1083 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1085 entry_index = (unsigned char *)r->entries;
1086 for (list = table->entries; list; list = list->next) {
1089 memcpy(entry_index, e->entry, e->entry->next_offset);
1090 entry_index += e->entry->next_offset;
1096 static void dump_ip(struct ipt_entry *entry)
1098 struct ipt_ip *ip = &entry->ip;
1099 char ip_string[INET6_ADDRSTRLEN];
1100 char ip_mask[INET6_ADDRSTRLEN];
1102 if (strlen(ip->iniface))
1103 connman_info("\tin %s", ip->iniface);
1105 if (strlen(ip->outiface))
1106 connman_info("\tout %s", ip->outiface);
1108 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) != NULL &&
1109 inet_ntop(AF_INET, &ip->smsk,
1110 ip_mask, INET6_ADDRSTRLEN) != NULL)
1111 connman_info("\tsrc %s/%s", ip_string, ip_mask);
1113 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) != NULL &&
1114 inet_ntop(AF_INET, &ip->dmsk,
1115 ip_mask, INET6_ADDRSTRLEN) != NULL)
1116 connman_info("\tdst %s/%s", ip_string, ip_mask);
1119 static void dump_target(struct connman_iptables *table,
1120 struct ipt_entry *entry)
1123 struct xtables_target *xt_t;
1124 struct xt_entry_target *target;
1126 target = ipt_get_target(entry);
1128 if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1129 struct xt_standard_target *t;
1131 t = (struct xt_standard_target *)target;
1133 switch (t->verdict) {
1135 connman_info("\ttarget RETURN");
1138 case -NF_ACCEPT - 1:
1139 connman_info("\ttarget ACCEPT");
1143 connman_info("\ttarget DROP");
1147 connman_info("\ttarget QUEUE");
1151 connman_info("\ttarget STOP");
1155 connman_info("\tJUMP @%p (0x%x)",
1156 (char*)table->blob_entries->entrytable +
1157 t->verdict, t->verdict);
1161 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1162 XTF_LOAD_MUST_SUCCEED);
1164 if(xt_t->print != NULL)
1165 xt_t->print(NULL, target, 1);
1167 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1169 connman_info("\ttarget %s", target->u.user.name);
1173 if(xt_t->print != NULL) {
1174 connman_info("\ttarget ");
1175 xt_t->print(NULL, target, 1);
1180 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1182 struct xtables_match *xt_m;
1183 struct xt_entry_match *match;
1185 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1188 match = (struct xt_entry_match *) entry->elems;
1190 if (!strlen(match->u.user.name))
1193 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1197 if(xt_m->print != NULL) {
1198 connman_info("\tmatch ");
1199 xt_m->print(NULL, match, 1);
1205 connman_info("\tmatch %s", match->u.user.name);
1209 static int dump_entry(struct ipt_entry *entry,
1210 struct connman_iptables *table)
1212 struct xt_entry_target *target;
1213 unsigned int offset;
1216 offset = (char *)entry - (char *)table->blob_entries->entrytable;
1217 target = ipt_get_target(entry);
1218 builtin = is_hook_entry(table, entry);
1220 if (entry_to_offset(table, entry) + entry->next_offset ==
1221 table->blob_entries->size) {
1222 connman_info("End of CHAIN 0x%x", offset);
1226 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1227 connman_info("USER CHAIN (%s) %p match %p target %p size %d",
1228 target->data, entry, entry->elems,
1229 (char *)entry + entry->target_offset,
1230 entry->next_offset);
1233 } else if (builtin >= 0) {
1234 connman_info("CHAIN (%s) %p match %p target %p size %d",
1235 hooknames[builtin], entry, entry->elems,
1236 (char *)entry + entry->target_offset,
1237 entry->next_offset);
1239 connman_info("RULE %p match %p target %p size %d", entry,
1241 (char *)entry + entry->target_offset,
1242 entry->next_offset);
1245 dump_match(table, entry);
1246 dump_target(table, entry);
1252 static void iptables_dump(struct connman_iptables *table)
1254 connman_info("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1256 table->info->valid_hooks, table->info->num_entries,
1259 ENTRY_ITERATE(table->blob_entries->entrytable,
1260 table->blob_entries->size,
1265 static int iptables_get_entries(struct connman_iptables *table)
1267 socklen_t entry_size;
1269 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1271 return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1272 table->blob_entries, &entry_size);
1275 static int iptables_replace(struct connman_iptables *table,
1276 struct ipt_replace *r)
1278 return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1279 sizeof(*r) + r->size);
1282 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1284 struct ipt_entry *new_entry;
1287 new_entry = g_try_malloc0(entry->next_offset);
1288 if (new_entry == NULL)
1291 memcpy(new_entry, entry, entry->next_offset);
1293 builtin = is_hook_entry(table, entry);
1295 return iptables_add_entry(table, new_entry, NULL, builtin);
1298 static void table_cleanup(struct connman_iptables *table)
1301 struct connman_iptables_entry *entry;
1306 if (table->ipt_sock >= 0)
1307 close(table->ipt_sock);
1309 for (list = table->entries; list; list = list->next) {
1312 g_free(entry->entry);
1316 g_list_free(table->entries);
1317 g_free(table->info);
1318 g_free(table->blob_entries);
1322 static struct connman_iptables *iptables_init(const char *table_name)
1324 struct connman_iptables *table = NULL;
1325 char *module = NULL;
1328 if (table_name == NULL)
1329 table_name = "filter";
1331 DBG("%s", table_name);
1333 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1334 DBG("ip_tables module loading gives error but trying anyway");
1336 module = g_strconcat("iptable_", table_name, NULL);
1340 if (xtables_insmod(module, NULL, TRUE) != 0)
1341 DBG("%s module loading gives error but trying anyway", module);
1345 table = g_hash_table_lookup(table_hash, table_name);
1349 table = g_try_new0(struct connman_iptables, 1);
1353 table->info = g_try_new0(struct ipt_getinfo, 1);
1354 if (table->info == NULL)
1357 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1358 if (table->ipt_sock < 0)
1361 s = sizeof(*table->info);
1362 strcpy(table->info->name, table_name);
1363 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1364 table->info, &s) < 0) {
1365 connman_error("iptables support missing error %d (%s)", errno,
1370 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1372 if (table->blob_entries == NULL)
1375 strcpy(table->blob_entries->name, table_name);
1376 table->blob_entries->size = table->info->size;
1378 if (iptables_get_entries(table) < 0)
1381 table->num_entries = 0;
1382 table->old_entries = table->info->num_entries;
1385 memcpy(table->underflow, table->info->underflow,
1386 sizeof(table->info->underflow));
1387 memcpy(table->hook_entry, table->info->hook_entry,
1388 sizeof(table->info->hook_entry));
1390 ENTRY_ITERATE(table->blob_entries->entrytable,
1391 table->blob_entries->size,
1394 g_hash_table_insert(table_hash, g_strdup(table_name), table);
1399 table_cleanup(table);
1404 static struct option iptables_opts[] = {
1405 {.name = "append", .has_arg = 1, .val = 'A'},
1406 {.name = "compare", .has_arg = 1, .val = 'C'},
1407 {.name = "delete", .has_arg = 1, .val = 'D'},
1408 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1409 {.name = "insert", .has_arg = 1, .val = 'I'},
1410 {.name = "list", .has_arg = 2, .val = 'L'},
1411 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1412 {.name = "policy", .has_arg = 1, .val = 'P'},
1413 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1414 {.name = "destination", .has_arg = 1, .val = 'd'},
1415 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1416 {.name = "jump", .has_arg = 1, .val = 'j'},
1417 {.name = "match", .has_arg = 1, .val = 'm'},
1418 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1419 {.name = "source", .has_arg = 1, .val = 's'},
1420 {.name = "table", .has_arg = 1, .val = 't'},
1424 struct xtables_globals iptables_globals = {
1426 .opts = iptables_opts,
1427 .orig_opts = iptables_opts,
1430 static struct xtables_target *prepare_target(struct connman_iptables *table,
1431 const char *target_name)
1433 struct xtables_target *xt_t = NULL;
1434 gboolean is_builtin, is_user_defined;
1435 GList *chain_head = NULL;
1439 is_user_defined = FALSE;
1441 if (is_builtin_target(target_name))
1444 chain_head = find_chain_head(table, target_name);
1445 if (chain_head != NULL && chain_head->next != NULL)
1446 is_user_defined = TRUE;
1449 if (is_builtin || is_user_defined)
1450 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1451 XTF_LOAD_MUST_SUCCEED);
1453 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1458 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1460 xt_t->t = g_try_malloc0(target_size);
1461 if (xt_t->t == NULL)
1464 xt_t->t->u.target_size = target_size;
1466 if (is_builtin || is_user_defined) {
1467 struct xt_standard_target *target;
1469 target = (struct xt_standard_target *)(xt_t->t);
1470 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1472 if (is_builtin == TRUE)
1473 target->verdict = target_to_verdict(target_name);
1474 else if (is_user_defined == TRUE) {
1475 struct connman_iptables_entry *target_rule;
1477 if (chain_head == NULL) {
1482 target_rule = chain_head->next->data;
1483 target->verdict = target_rule->offset;
1486 strcpy(xt_t->t->u.user.name, target_name);
1487 xt_t->t->u.user.revision = xt_t->revision;
1488 if (xt_t->init != NULL)
1489 xt_t->init(xt_t->t);
1492 #if XTABLES_VERSION_CODE > 5
1493 if (xt_t->x6_options != NULL)
1494 iptables_globals.opts =
1495 xtables_options_xfrm(
1496 iptables_globals.orig_opts,
1497 iptables_globals.opts,
1499 &xt_t->option_offset);
1502 iptables_globals.opts =
1503 xtables_merge_options(
1504 #if XTABLES_VERSION_CODE > 5
1505 iptables_globals.orig_opts,
1507 iptables_globals.opts,
1509 &xt_t->option_offset);
1511 if (iptables_globals.opts == NULL) {
1519 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1520 struct xtables_rule_match **xt_rm,
1521 const char *match_name)
1523 struct xtables_match *xt_m;
1526 if (match_name == NULL)
1529 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1530 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1532 xt_m->m = g_try_malloc0(match_size);
1533 if (xt_m->m == NULL)
1536 xt_m->m->u.match_size = match_size;
1537 strcpy(xt_m->m->u.user.name, xt_m->name);
1538 xt_m->m->u.user.revision = xt_m->revision;
1540 if (xt_m->init != NULL)
1541 xt_m->init(xt_m->m);
1543 if (xt_m == xt_m->next)
1546 #if XTABLES_VERSION_CODE > 5
1547 if (xt_m->x6_options != NULL)
1548 iptables_globals.opts =
1549 xtables_options_xfrm(
1550 iptables_globals.orig_opts,
1551 iptables_globals.opts,
1553 &xt_m->option_offset);
1556 iptables_globals.opts =
1557 xtables_merge_options(
1558 #if XTABLES_VERSION_CODE > 5
1559 iptables_globals.orig_opts,
1561 iptables_globals.opts,
1563 &xt_m->option_offset);
1565 if (iptables_globals.opts == NULL) {
1574 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1577 uint32_t prefixlength;
1581 tokens = g_strsplit(str, "/", 2);
1585 if (!inet_pton(AF_INET, tokens[0], ip)) {
1590 if (tokens[1] != NULL) {
1591 prefixlength = strtol(tokens[1], NULL, 10);
1592 if (prefixlength > 31) {
1597 tmp = ~(0xffffffff >> prefixlength);
1602 mask->s_addr = htonl(tmp);
1603 ip->s_addr = ip->s_addr & mask->s_addr;
1611 static struct connman_iptables *pre_load_table(const char *table_name,
1612 struct connman_iptables *table)
1617 return iptables_init(table_name);
1620 static void clear_tables_flags(void)
1622 struct xtables_match *xt_m;
1623 struct xtables_target *xt_t;
1626 * Clear all flags because the flags are only valid
1629 for (xt_m = xtables_matches; xt_m != NULL; xt_m = xt_m->next)
1632 for (xt_t = xtables_targets; xt_t != NULL; xt_t = xt_t->next) {
1638 static int iptables_command(int argc, char *argv[])
1640 struct connman_iptables *table;
1641 struct xtables_rule_match *xt_rm, *tmp_xt_rm;
1642 struct xtables_match *xt_m, *xt_m_t;
1643 struct xtables_target *xt_t;
1645 char *table_name, *chain, *new_chain, *match_name, *target_name;
1646 char *flush_chain, *delete_chain, *policy;
1647 int c, ret, in_len, out_len;
1648 gboolean dump, invert, insert, delete, compare;
1658 chain = new_chain = match_name = target_name = NULL;
1659 flush_chain = delete_chain = policy = table_name = NULL;
1660 memset(&ip, 0, sizeof(struct ipt_ip));
1665 /* Default code for options parsing */
1668 clear_tables_flags();
1670 /* extension's options will generate false-positives errors */
1675 while ((c = getopt_long(argc, argv,
1676 "-A:C:D:F:I:L::N:P:X:d:j:i:m:o:s:t:",
1677 iptables_globals.opts, NULL)) != -1) {
1680 /* It is either -A, -C, -D or -I at once */
1688 /* It is either -A, -C, -D or -I at once */
1697 /* It is either -A, -C, -D or -I at once */
1706 flush_chain = optarg;
1710 /* It is either -A, -C, -D or -I at once */
1729 policy = argv[optind++];
1736 delete_chain = optarg;
1740 if (!parse_ip_and_mask(optarg, &ip.dst, &ip.dmsk))
1744 ip.invflags |= IPT_INV_DSTIP;
1749 in_len = strlen(optarg);
1751 if (in_len + 1 > IFNAMSIZ)
1754 strcpy(ip.iniface, optarg);
1755 memset(ip.iniface_mask, 0xff, in_len + 1);
1758 ip.invflags |= IPT_INV_VIA_IN;
1763 target_name = optarg;
1765 table = pre_load_table(table_name, table);
1769 xt_t = prepare_target(table, target_name);
1776 match_name = optarg;
1778 table = pre_load_table(table_name, table);
1782 xt_m = prepare_matches(table, &xt_rm, match_name);
1789 out_len = strlen(optarg);
1791 if (out_len + 1 > IFNAMSIZ)
1794 strcpy(ip.outiface, optarg);
1795 memset(ip.outiface_mask, 0xff, out_len + 1);
1798 ip.invflags |= IPT_INV_VIA_OUT;
1803 if (!parse_ip_and_mask(optarg, &ip.src, &ip.smsk))
1807 ip.invflags |= IPT_INV_SRCIP;
1812 table_name = optarg;
1814 table = pre_load_table(table_name, table);
1821 if (optarg[0] == '!' && optarg[1] == '\0') {
1827 connman_error("Invalid option");
1832 #if XTABLES_VERSION_CODE > 5
1833 if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1834 xt_t->parse != NULL) &&
1835 (c >= (int) xt_t->option_offset &&
1836 c < (int) xt_t->option_offset +
1837 XT_OPTION_OFFSET_SCALE)) {
1838 xtables_option_tpcall(c, argv,
1839 invert, xt_t, NULL);
1844 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1845 tmp_xt_rm = tmp_xt_rm->next) {
1846 xt_m_t = tmp_xt_rm->match;
1848 if (tmp_xt_rm->completed ||
1849 (xt_m_t->x6_parse == NULL &&
1850 xt_m_t->parse == NULL))
1853 if (c < (int) xt_m_t->option_offset ||
1854 c >= (int) xt_m_t->option_offset
1855 + XT_OPTION_OFFSET_SCALE)
1858 xtables_option_mpcall(c, argv,
1859 invert, xt_m_t, NULL);
1864 if (xt_t == NULL || xt_t->parse == NULL ||
1865 !xt_t->parse(c - xt_t->option_offset,
1866 argv, invert, &xt_t->tflags, NULL, &xt_t->t)) {
1868 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1869 tmp_xt_rm = tmp_xt_rm->next) {
1870 xt_m_t = tmp_xt_rm->match;
1872 if (tmp_xt_rm->completed ||
1873 xt_m_t->parse == NULL)
1876 if (xt_m->parse(c - xt_m->option_offset,
1877 argv, invert, &xt_m->mflags,
1889 #if XTABLES_VERSION_CODE > 5
1890 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1891 tmp_xt_rm = tmp_xt_rm->next)
1892 xtables_option_mfcall(tmp_xt_rm->match);
1895 xtables_option_tfcall(xt_t);
1897 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1898 tmp_xt_rm = tmp_xt_rm->next)
1899 if (tmp_xt_rm->match->final_check != NULL)
1900 tmp_xt_rm->match->final_check(
1901 tmp_xt_rm->match->mflags);
1903 if (xt_t != NULL && xt_t->final_check != NULL)
1904 xt_t->final_check(xt_t->tflags);
1907 table = pre_load_table(table_name, table);
1911 /* Option parsing went fine, falling back to succes code */
1914 if (delete_chain != NULL) {
1915 printf("Delete chain %s\n", delete_chain);
1917 iptables_delete_chain(table, delete_chain);
1923 iptables_dump(table);
1929 DBG("Flush chain %s", flush_chain);
1931 iptables_flush_chain(table, flush_chain);
1936 if (chain && new_chain) {
1942 DBG("New chain %s", new_chain);
1944 ret = iptables_add_chain(table, new_chain);
1949 if (policy != NULL) {
1950 printf("Changing policy of %s to %s\n", chain, policy);
1952 iptables_change_policy(table, chain, policy);
1960 if (compare == TRUE) {
1961 ret = iptables_compare_rule(table, &ip, chain,
1962 target_name, xt_t, xt_m, xt_rm);
1966 if (delete == TRUE) {
1967 DBG("Deleting %s to %s (match %s)\n",
1968 target_name, chain, match_name);
1970 ret = iptables_delete_rule(table, &ip, chain,
1971 target_name, xt_t, xt_m, xt_rm);
1976 if (insert == TRUE) {
1977 DBG("Inserting %s to %s (match %s)",
1978 target_name, chain, match_name);
1980 ret = iptables_insert_rule(table, &ip, chain,
1981 target_name, xt_t, xt_rm);
1985 DBG("Adding %s to %s (match %s)",
1986 target_name, chain, match_name);
1988 ret = iptables_append_rule(table, &ip, chain,
1989 target_name, xt_t, xt_rm);
2005 int __connman_iptables_command(const char *format, ...)
2007 char **argv, **arguments, *command;
2014 va_start(args, format);
2016 command = g_strdup_vprintf(format, args);
2020 if (command == NULL)
2023 arguments = g_strsplit_set(command, " ", -1);
2025 for (argc = 0; arguments[argc]; argc++);
2028 DBG("command %s argc %d", command, argc);
2030 argv = g_try_malloc0(argc * sizeof(char *));
2033 g_strfreev(arguments);
2037 argv[0] = "iptables";
2038 for (i = 1; i < argc; i++)
2039 argv[i] = arguments[i - 1];
2041 ret = iptables_command(argc, argv);
2044 g_strfreev(arguments);
2051 int __connman_iptables_commit(const char *table_name)
2053 struct connman_iptables *table;
2054 struct ipt_replace *repl;
2057 DBG("%s", table_name);
2059 table = g_hash_table_lookup(table_hash, table_name);
2063 repl = iptables_blob(table);
2065 err = iptables_replace(table, repl);
2067 g_free(repl->counters);
2073 g_hash_table_remove(table_hash, table_name);
2078 static void remove_table(gpointer user_data)
2080 struct connman_iptables *table = user_data;
2082 table_cleanup(table);
2085 int __connman_iptables_init(void)
2089 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2090 g_free, remove_table);
2092 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2098 void __connman_iptables_cleanup(void)
2102 g_hash_table_destroy(table_hash);
2104 xtables_free_opts(1);