tools: iptables-test delete a chain if only it is already flushed.
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
5  *
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.
9  *
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.
14  *
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
18  *
19  */
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <xtables.h>
29
30 #include <linux/netfilter_ipv4/ip_tables.h>
31
32 #include <glib.h>
33
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",
40 };
41
42 #define LABEL_ACCEPT  "ACCEPT"
43 #define LABEL_DROP    "DROP"
44 #define LABEL_QUEUE   "QUEUE"
45 #define LABEL_RETURN  "RETURN"
46
47 /* fn returns 0 to continue iteration */
48 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
49 ({                                                              \
50         unsigned int __i;                                       \
51         int __n;                                                \
52         int __ret = 0;                                          \
53         type *__entry;                                          \
54                                                                 \
55         for (__i = 0, __n = 0; __i < (size);                    \
56              __i += __entry->next_offset, __n++) {              \
57                 __entry = (void *)(entries) + __i;              \
58                 if (__n < n)                                    \
59                         continue;                               \
60                                                                 \
61                 __ret = fn(__entry,  ## args);                  \
62                 if (__ret != 0)                                 \
63                         break;                                  \
64         }                                                       \
65         __ret;                                                  \
66 })
67
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)
71
72 #define ENTRY_ITERATE(entries, size, fn, args...) \
73         _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
74
75 #define MIN_ALIGN (__alignof__(struct ipt_entry))
76
77 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
78
79 struct error_target {
80         struct xt_entry_target t;
81         char error[IPT_TABLE_MAXNAMELEN];
82 };
83
84 struct connman_iptables_entry {
85         int offset;
86         int builtin;
87
88         struct ipt_entry *entry;
89 };
90
91 struct connman_iptables {
92         int ipt_sock;
93
94         struct ipt_getinfo *info;
95         struct ipt_get_entries *blob_entries;
96
97         unsigned int num_entries;
98         unsigned int old_entries;
99         unsigned int size;
100
101         unsigned int underflow[NF_INET_NUMHOOKS];
102         unsigned int hook_entry[NF_INET_NUMHOOKS];
103
104         GList *entries;
105 };
106
107
108 static struct ipt_entry *get_entry(struct connman_iptables *table,
109                                         unsigned int offset)
110 {
111         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
112                                                                         offset);
113 }
114
115 static int is_hook_entry(struct connman_iptables *table,
116                                 struct ipt_entry *entry)
117 {
118         unsigned int i;
119
120         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
121                 if ((table->info->valid_hooks & (1 << i))
122                 && get_entry(table, table->info->hook_entry[i]) == entry)
123                         return i;
124         }
125
126         return -1;
127 }
128
129 static unsigned long entry_to_offset(struct connman_iptables *table,
130                                         struct ipt_entry *entry)
131 {
132         return (void *)entry - (void *)table->blob_entries->entrytable;
133 }
134
135 static int target_to_verdict(char *target_name)
136 {
137         if (!strcmp(target_name, LABEL_ACCEPT))
138                 return -NF_ACCEPT - 1;
139
140         if (!strcmp(target_name, LABEL_DROP))
141                 return -NF_DROP - 1;
142
143         if (!strcmp(target_name, LABEL_QUEUE))
144                 return -NF_QUEUE - 1;
145
146         if (!strcmp(target_name, LABEL_RETURN))
147                 return XT_RETURN;
148
149         return 0;
150 }
151
152 static gboolean is_builtin_target(char *target_name)
153 {
154         if (!strcmp(target_name, LABEL_ACCEPT) ||
155                 !strcmp(target_name, LABEL_DROP) ||
156                 !strcmp(target_name, LABEL_QUEUE) ||
157                 !strcmp(target_name, LABEL_RETURN))
158                 return TRUE;
159
160         return FALSE;
161 }
162
163 static gboolean is_jump(struct connman_iptables_entry *e)
164 {
165         struct xt_entry_target *target;
166
167         target = ipt_get_target(e->entry);
168
169         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
170                 struct xt_standard_target *t;
171
172                 t = (struct xt_standard_target *)target;
173
174                 switch (t->verdict) {
175                 case XT_RETURN:
176                 case -NF_ACCEPT - 1:
177                 case -NF_DROP - 1:
178                 case -NF_QUEUE - 1:
179                 case -NF_STOP - 1:
180                         return false;
181
182                 default:
183                         return true;
184                 }
185         }
186
187         return false;
188 }
189
190 static gboolean is_chain(struct connman_iptables *table,
191                                 struct connman_iptables_entry *e)
192 {
193         struct ipt_entry *entry;
194         struct xt_entry_target *target;
195
196         entry = e->entry;
197         if (e->builtin >= 0)
198                 return TRUE;
199
200         target = ipt_get_target(entry);
201         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
202                 return TRUE;
203
204         return FALSE;
205 }
206
207 static GList *find_chain_head(struct connman_iptables *table,
208                                 char *chain_name)
209 {
210         GList *list;
211         struct connman_iptables_entry *head;
212         struct ipt_entry *entry;
213         struct xt_entry_target *target;
214         int builtin;
215
216         for (list = table->entries; list; list = list->next) {
217                 head = list->data;
218                 entry = head->entry;
219
220                 /* Buit-in chain */
221                 builtin = head->builtin;
222                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
223                         break;
224
225                 /* User defined chain */
226                 target = ipt_get_target(entry);
227                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
228                     !strcmp((char *)target->data, chain_name))
229                         break;
230         }
231
232         return list;
233 }
234
235 static GList *find_chain_tail(struct connman_iptables *table,
236                                 char *chain_name)
237 {
238         GList *chain_head, *list;
239         struct connman_iptables_entry *tail;
240
241         chain_head = find_chain_head(table, chain_name);
242         if (chain_head == NULL)
243                 return NULL;
244
245         /* Then we look for the next chain */
246         for (list = chain_head->next; list; list = list->next) {
247                 tail = list->data;
248
249                 if (is_chain(table, tail))
250                         return list;
251         }
252
253         /* Nothing found, we return the table end */
254         return g_list_last(table->entries);
255 }
256
257 static void update_offsets(struct connman_iptables *table)
258 {
259         GList *list, *prev;
260         struct connman_iptables_entry *entry, *prev_entry;
261
262         for (list = table->entries; list; list = list->next) {
263                 entry = list->data;
264
265                 if (list == table->entries) {
266                         entry->offset = 0;
267
268                         continue;
269                 }
270
271                 prev = list->prev;
272                 prev_entry = prev->data;
273
274                 entry->offset = prev_entry->offset +
275                                         prev_entry->entry->next_offset;
276         }
277 }
278
279 static int connman_add_entry(struct connman_iptables *table,
280                                 struct ipt_entry *entry, GList *before,
281                                         int builtin)
282 {
283         GList *list;
284         struct connman_iptables_entry *e, *tmp, *entry_before;
285         struct xt_standard_target *t;
286
287         if (table == NULL)
288                 return -1;
289
290         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
291         if (e == NULL)
292                 return -1;
293
294         e->entry = entry;
295         e->builtin = builtin;
296
297         table->entries = g_list_insert_before(table->entries, before, e);
298         table->num_entries++;
299         table->size += entry->next_offset;
300
301         if (before == NULL) {
302                 e->offset = table->size - entry->next_offset;
303
304                 return 0;
305         }
306
307         entry_before = before->data;
308
309         /*
310          * We've just insterted a new entry. All references before it
311          * should be bumped accordingly.
312          */
313         for (list = table->entries; list != before; list = list->next) {
314                 tmp = list->data;
315
316                 if (!is_jump(tmp))
317                         continue;
318
319                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
320
321                 if (t->verdict >= entry_before->offset)
322                         t->verdict += entry->next_offset;
323         }
324
325         update_offsets(table);
326
327         return 0;
328 }
329
330 static int remove_table_entry(struct connman_iptables *table,
331                                         struct connman_iptables_entry *entry)
332 {
333         int removed = 0;
334
335         table->num_entries--;
336         table->size -= entry->entry->next_offset;
337         removed = entry->entry->next_offset;
338
339         g_free(entry->entry);
340
341         table->entries = g_list_remove(table->entries, entry);
342
343         return removed;
344 }
345
346 static int connman_iptables_flush_chain(struct connman_iptables *table,
347                                                 char *name)
348 {
349         GList *chain_head, *chain_tail, *list, *next;
350         struct connman_iptables_entry *entry;
351         int builtin, removed = 0;
352
353         chain_head = find_chain_head(table, name);
354         if (chain_head == NULL)
355                 return -EINVAL;
356
357         chain_tail = find_chain_tail(table, name);
358         if (chain_tail == NULL)
359                 return -EINVAL;
360
361         entry = chain_head->data;
362         builtin = entry->builtin;
363
364         if (builtin >= 0)
365                 list = chain_head;
366         else
367                 list = chain_head->next;
368
369         if (list == chain_tail->prev)
370                 return 0;
371
372         while (list != chain_tail->prev) {
373                 entry = list->data;
374                 next = g_list_next(list);
375
376                 removed += remove_table_entry(table, entry);
377
378                 list = next;
379         }
380
381         if (builtin >= 0) {
382                 struct connman_iptables_entry *e;
383
384                 entry = list->data;
385
386                 entry->builtin = builtin;
387
388                 table->underflow[builtin] -= removed;
389
390                 for (list = chain_tail; list; list = list->next) {
391                         e = list->data;
392
393                         builtin = e->builtin;
394                         if (builtin < 0)
395                                 continue;
396
397                         table->hook_entry[builtin] -= removed;
398                         table->underflow[builtin] -= removed;
399                 }
400         }
401
402         update_offsets(table);
403
404         return 0;
405 }
406
407 static int connman_iptables_delete_chain(struct connman_iptables *table,
408                                                 char *name)
409 {
410         GList *chain_head, *chain_tail;
411         struct connman_iptables_entry *entry;
412
413         chain_head = find_chain_head(table, name);
414         if (chain_head == NULL)
415                 return -EINVAL;
416
417         entry = chain_head->data;
418
419         /* We cannot remove builtin chain */
420         if (entry->builtin >= 0)
421                 return -EINVAL;
422
423         chain_tail = find_chain_tail(table, name);
424         if (chain_tail == NULL)
425                 return -EINVAL;
426
427         /* Chain must be flushed */
428         if (chain_head->next != chain_tail->prev)
429                 return -EINVAL;
430
431         remove_table_entry(table, entry);
432
433         entry = chain_tail->prev->data;
434         remove_table_entry(table, entry);
435
436         update_offsets(table);
437
438         return 0;
439 }
440
441 static int connman_iptables_add_chain(struct connman_iptables *table,
442                                         char *name)
443 {
444         GList *last;
445         struct ipt_entry *entry_head;
446         struct ipt_entry *entry_return;
447         struct error_target *error;
448         struct ipt_standard_target *standard;
449         u_int16_t entry_head_size, entry_return_size;
450
451         last = g_list_last(table->entries);
452
453         /*
454          * An empty chain is composed of:
455          * - A head entry, with no match and an error target.
456          *   The error target data is the chain name.
457          * - A tail entry, with no match and a standard target.
458          *   The standard target verdict is XT_RETURN (return to the
459          *   caller).
460          */
461
462         /* head entry */
463         entry_head_size = sizeof(struct ipt_entry) +
464                                 sizeof(struct error_target);
465         entry_head = g_try_malloc0(entry_head_size);
466         if (entry_head == NULL)
467                 goto err_head;
468
469         memset(entry_head, 0, entry_head_size);
470
471         entry_head->target_offset = sizeof(struct ipt_entry);
472         entry_head->next_offset = entry_head_size;
473
474         error = (struct error_target *) entry_head->elems;
475         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
476         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
477         strcpy(error->error, name);
478
479         if (connman_add_entry(table, entry_head, last, -1) < 0)
480                 goto err_head;
481
482         /* tail entry */
483         entry_return_size = sizeof(struct ipt_entry) +
484                                 sizeof(struct ipt_standard_target);
485         entry_return = g_try_malloc0(entry_return_size);
486         if (entry_return == NULL)
487                 goto err;
488
489         memset(entry_return, 0, entry_return_size);
490
491         entry_return->target_offset = sizeof(struct ipt_entry);
492         entry_return->next_offset = entry_return_size;
493
494         standard = (struct ipt_standard_target *) entry_return->elems;
495         standard->target.u.user.target_size =
496                                 ALIGN(sizeof(struct ipt_standard_target));
497         standard->verdict = XT_RETURN;
498
499         if (connman_add_entry(table, entry_return, last, -1) < 0)
500                 goto err;
501
502         return 0;
503
504 err:
505         g_free(entry_return);
506 err_head:
507         g_free(entry_head);
508
509         return -ENOMEM;
510 }
511
512 static struct ipt_entry *
513 new_rule(struct connman_iptables *table,
514                 char *target_name, struct xtables_target *xt_t,
515                 char *match_name, struct xtables_match *xt_m)
516 {
517         struct ipt_entry *new_entry;
518         size_t match_size, target_size;
519         int is_builtin = is_builtin_target(target_name);
520
521         if (xt_m)
522                 match_size = xt_m->m->u.match_size;
523         else
524                 match_size = 0;
525
526         if (xt_t)
527                 target_size = ALIGN(xt_t->t->u.target_size);
528         else
529                 target_size = ALIGN(sizeof(struct xt_standard_target));
530
531         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
532                                                                 match_size);
533         if (new_entry == NULL)
534                 return NULL;
535
536         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
537         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
538                                                                 match_size;
539         if (xt_m) {
540                 struct xt_entry_match *entry_match;
541
542                 entry_match = (struct xt_entry_match *)new_entry->elems;
543                 memcpy(entry_match, xt_m->m, match_size);
544         }
545
546         if (xt_t) {
547                 struct xt_entry_target *entry_target;
548
549                 if (is_builtin) {
550                         struct xt_standard_target *target;
551
552                         target = (struct xt_standard_target *)(xt_t->t);
553                         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
554                         target->verdict = target_to_verdict(target_name);
555                 }
556
557                 entry_target = ipt_get_target(new_entry);
558                 memcpy(entry_target, xt_t->t, target_size);
559         } else {
560                 struct connman_iptables_entry *target_rule;
561                 struct xt_standard_target *target;
562                 GList *chain_head;
563
564                 /*
565                  * This is a user defined target, i.e. a chain jump.
566                  * We search for the chain head, and the target verdict
567                  * is the first rule's offset on this chain.
568                  * The offset is from the beginning of the table.
569                  */
570
571                 chain_head = find_chain_head(table, target_name);
572                 if (chain_head == NULL || chain_head->next == NULL) {
573                         g_free(new_entry);
574                         return NULL;
575                 }
576
577                 target_rule = chain_head->next->data;
578
579                 target = (struct xt_standard_target *)ipt_get_target(new_entry);
580                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
581                 target->target.u.user.target_size = target_size;
582                 target->verdict = target_rule->offset;
583         }
584
585         return new_entry;
586 }
587
588 static void update_hooks(struct connman_iptables *table, GList *chain_head, struct ipt_entry *entry)
589 {
590         GList *list;
591         struct connman_iptables_entry *head, *e;
592         int builtin;
593
594         if (chain_head == NULL)
595                 return;
596
597         head = chain_head->data;
598
599         builtin = head->builtin;
600         if (builtin < 0)
601                 return;
602
603         table->underflow[builtin] += entry->next_offset;
604
605         for (list = chain_head->next; list; list = list->next) {
606                 e = list->data;
607
608                 builtin = e->builtin;
609                 if (builtin < 0)
610                         continue;
611
612                 table->hook_entry[builtin] += entry->next_offset;
613                 table->underflow[builtin] += entry->next_offset;
614         }
615 }
616
617 static int
618 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
619                                 char *target_name, struct xtables_target *xt_t,
620                                 char *match_name, struct xtables_match *xt_m)
621 {
622         GList *chain_tail, *chain_head;
623         struct ipt_entry *new_entry;
624         struct connman_iptables_entry *head;
625         int builtin = -1;
626
627         chain_head = find_chain_head(table, chain_name);
628         if (chain_head == NULL)
629                 return -EINVAL;
630
631         chain_tail = find_chain_tail(table, chain_name);
632         if (chain_tail == NULL)
633                 return -EINVAL;
634
635         new_entry = new_rule(table,
636                                 target_name, xt_t,
637                                 match_name, xt_m);
638         if (new_entry == NULL)
639                 return -EINVAL;
640
641         update_hooks(table, chain_head, new_entry);
642
643         /*
644          * If the chain is builtin, and does not have any rule,
645          * then the one that we're inserting is becoming the head
646          * and thus needs the builtin flag.
647          */
648         head = chain_head->data;
649         if (head->builtin < 0)
650                 builtin = -1;
651         else if (chain_head == chain_tail->prev) {
652                 builtin = head->builtin;
653                 head->builtin = -1;
654         }
655
656         return connman_add_entry(table, new_entry, chain_tail->prev, builtin);
657 }
658
659 static struct ipt_replace *
660 connman_iptables_blob(struct connman_iptables *table)
661 {
662         struct ipt_replace *r;
663         GList *list;
664         struct connman_iptables_entry *e;
665         unsigned char *entry_index;
666
667         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
668         if (r == NULL)
669                 return NULL;
670
671         memset(r, 0, sizeof(*r) + table->size);
672
673         r->counters = g_try_malloc0(sizeof(struct xt_counters)
674                                 * table->old_entries);
675         if (r->counters == NULL) {
676                 g_free(r);
677                 return NULL;
678         }
679
680         strcpy(r->name, table->info->name);
681         r->num_entries = table->num_entries;
682         r->size = table->size;
683
684         r->num_counters = table->old_entries;
685         r->valid_hooks  = table->info->valid_hooks;
686
687         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
688         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
689
690         entry_index = (unsigned char *)r->entries;
691         for (list = table->entries; list; list = list->next) {
692                 e = list->data;
693
694                 memcpy(entry_index, e->entry, e->entry->next_offset);
695                 entry_index += e->entry->next_offset;
696         }
697
698         return r;
699 }
700
701 static void dump_target(struct connman_iptables *table,
702                                 struct ipt_entry *entry)
703
704 {
705         struct xtables_target *xt_t;
706         struct xt_entry_target *target;
707
708         target = ipt_get_target(entry);
709
710         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
711                 struct xt_standard_target *t;
712
713                 t = (struct xt_standard_target *)target;
714
715                 switch (t->verdict) {
716                 case XT_RETURN:
717                         printf("\ttarget RETURN\n");
718                         break;
719
720                 case -NF_ACCEPT - 1:
721                         printf("\ttarget ACCEPT\n");
722                         break;
723
724                 case -NF_DROP - 1:
725                         printf("\ttarget DROP\n");
726                         break;
727
728                 case -NF_QUEUE - 1:
729                         printf("\ttarget QUEUE\n");
730                         break;
731
732                 case -NF_STOP - 1:
733                         printf("\ttarget STOP\n");
734                         break;
735
736                 default:
737                         printf("\tJUMP @%p (0x%x)\n",
738                                 (char*)table->blob_entries->entrytable +
739                                 t->verdict, t->verdict);
740                         break;
741                 }
742
743                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
744                                                 XTF_LOAD_MUST_SUCCEED);
745
746                 if(xt_t->print != NULL)
747                         xt_t->print(NULL, target, 1);
748         } else {
749                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
750                 if (xt_t == NULL) {
751                         printf("\ttarget %s\n", target->u.user.name);
752                         return;
753                 }
754
755                 if(xt_t->print != NULL) {
756                         printf("\ttarget ");
757                         xt_t->print(NULL, target, 1);
758                         printf("\n");
759                 }
760         }
761 }
762
763 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
764 {
765         struct xtables_match *xt_m;
766         struct xt_entry_match *match;
767
768         if (entry->elems == (unsigned char *)entry + entry->target_offset)
769                 return;
770
771         match = (struct xt_entry_match *) entry->elems;
772
773         if (!strlen(match->u.user.name))
774                 return;
775
776         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
777         if (xt_m == NULL)
778                 goto out;
779
780         if(xt_m->print != NULL) {
781                 printf("\tmatch ");
782                 xt_m->print(NULL, match, 1);
783                 printf("\n");
784
785                 return;
786         }
787
788 out:
789         printf("\tmatch %s\n", match->u.user.name);
790
791 }
792
793 static int connman_iptables_dump_entry(struct ipt_entry *entry,
794                                         struct connman_iptables *table)
795 {
796         struct xt_entry_target *target;
797         unsigned int offset;
798         int builtin;
799
800         offset = (char *)entry - (char *)table->blob_entries->entrytable;
801         target = ipt_get_target(entry);
802         builtin = is_hook_entry(table, entry);
803
804         if (entry_to_offset(table, entry) + entry->next_offset ==
805                                         table->blob_entries->size) {
806                 printf("End of CHAIN 0x%x\n", offset);
807                 return 0;
808         }
809
810         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
811                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
812                         target->data, entry, entry->elems,
813                         (char *)entry + entry->target_offset,
814                                 entry->next_offset);
815
816                 return 0;
817         } else if (builtin >= 0) {
818                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
819                         hooknames[builtin], entry, entry->elems,
820                         (char *)entry + entry->target_offset,
821                                 entry->next_offset);
822         } else {
823                 printf("RULE %p  match %p  target %p  size %d\n", entry,
824                         entry->elems,
825                         (char *)entry + entry->target_offset,
826                                 entry->next_offset);
827         }
828
829         dump_match(table, entry);
830         dump_target(table, entry);
831
832         return 0;
833 }
834
835 static void connman_iptables_dump_hook(struct connman_iptables *table)
836 {
837         int i;
838         printf("hooks: \n");
839         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
840                 if ((table->info->valid_hooks & (1 << i)))
841                         printf("%s entry %p underflow %p (%#x)\n",
842                                 hooknames[i],
843                                 table->blob_entries->entrytable +
844                                                 table->info->hook_entry[i],
845                                 table->blob_entries->entrytable +
846                                                 table->info->underflow[i],
847                                         table->info->underflow[i]);
848         }
849 }
850
851 static void connman_iptables_dump(struct connman_iptables *table)
852 {
853         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
854                 table->info->name,
855                 table->info->valid_hooks, table->info->num_entries,
856                 table->info->size);
857
858         connman_iptables_dump_hook(table);
859
860         ENTRY_ITERATE(table->blob_entries->entrytable,
861                         table->blob_entries->size,
862                         connman_iptables_dump_entry, table);
863
864 }
865
866 static int connman_iptables_get_entries(struct connman_iptables *table)
867 {
868         socklen_t entry_size;
869
870         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
871
872         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
873                                 table->blob_entries, &entry_size);
874 }
875
876 static int connman_iptables_replace(struct connman_iptables *table,
877                                         struct ipt_replace *r)
878 {
879         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
880                          sizeof(*r) + r->size);
881 }
882
883 static void connman_iptables_cleanup(struct connman_iptables *table)
884 {
885         GList *list;
886         struct connman_iptables_entry *entry;
887
888         close(table->ipt_sock);
889
890         for (list = table->entries; list; list = list->next) {
891                 entry = list->data;
892
893                 g_free(entry->entry);
894         }
895
896         g_free(table->info);
897         g_free(table->blob_entries);
898         g_free(table);
899
900         xtables_free_opts(1);
901 }
902
903 static int connman_iptables_commit(struct connman_iptables *table)
904 {
905         struct ipt_replace *repl;
906
907         repl = connman_iptables_blob(table);
908
909         return connman_iptables_replace(table, repl);
910 }
911
912 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
913 {
914         struct ipt_entry *new_entry;
915         int builtin;
916
917         new_entry = g_try_malloc0(entry->next_offset);
918         if (new_entry == NULL)
919                 return -ENOMEM;
920
921         memcpy(new_entry, entry, entry->next_offset);
922
923         builtin = is_hook_entry(table, entry);
924
925         return connman_add_entry(table, new_entry, NULL, builtin);
926 }
927
928 static struct connman_iptables *connman_iptables_init(const char *table_name)
929 {
930         struct connman_iptables *table;
931         socklen_t s;
932
933         table =  g_try_new0(struct connman_iptables, 1);
934         if (table == NULL)
935                 return NULL;
936
937         table->info =  g_try_new0(struct ipt_getinfo, 1);
938         if (table->info == NULL)
939                 goto err;
940
941         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
942         if (table->ipt_sock < 0)
943                 goto err;
944
945         s = sizeof(*table->info);
946         strcpy(table->info->name, table_name);
947         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
948                                                 table->info, &s) < 0)
949                 goto err;
950
951         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
952                                                 table->info->size);
953         if (table->blob_entries == NULL)
954                 goto err;
955
956         strcpy(table->blob_entries->name, table_name);
957         table->blob_entries->size = table->info->size;
958
959         if (connman_iptables_get_entries(table) < 0)
960                 goto err;
961
962         table->num_entries = 0;
963         table->old_entries = table->info->num_entries;
964         table->size = 0;
965
966         memcpy(table->underflow, table->info->underflow,
967                                 sizeof(table->info->underflow));
968         memcpy(table->hook_entry, table->info->hook_entry,
969                                 sizeof(table->info->hook_entry));
970
971         ENTRY_ITERATE(table->blob_entries->entrytable,
972                         table->blob_entries->size,
973                                 add_entry, table);
974
975
976         return table;
977
978 err:
979
980         connman_iptables_cleanup(table);
981
982         return NULL;
983 }
984
985
986 static struct option connman_iptables_opts[] = {
987         {.name = "append",        .has_arg = 1, .val = 'A'},
988         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
989         {.name = "list",          .has_arg = 2, .val = 'L'},
990         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
991         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
992         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
993         {.name = "jump",          .has_arg = 1, .val = 'j'},
994         {.name = "match",         .has_arg = 1, .val = 'm'},
995         {.name = "out-interface", .has_arg = 1, .val = 'o'},
996         {.name = "table",         .has_arg = 1, .val = 't'},
997         {NULL},
998 };
999
1000 struct xtables_globals connman_iptables_globals = {
1001         .option_offset = 0,
1002         .opts = connman_iptables_opts,
1003         .orig_opts = connman_iptables_opts,
1004 };
1005
1006 int main(int argc, char *argv[])
1007 {
1008         struct connman_iptables *table;
1009         struct xtables_match *xt_m;
1010         struct xtables_target *xt_t;
1011         char *table_name, *chain, *new_chain, *match_name, *target_name;
1012         char *delete_chain, *flush_chain;
1013         int c;
1014         size_t size;
1015         gboolean dump, invert, delete;
1016
1017         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1018
1019         dump = FALSE;
1020         invert = FALSE;
1021         delete = FALSE;
1022         table_name = chain = new_chain = match_name = target_name = NULL;
1023         delete_chain = flush_chain = NULL;
1024         table = NULL;
1025         xt_m = NULL;
1026         xt_t = NULL;
1027
1028         while ((c = getopt_long(argc, argv,
1029            "-A:F:L::N:X:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
1030                 switch (c) {
1031                 case 'A':
1032                         chain = optarg;
1033                         break;
1034
1035                 case 'F':
1036                         flush_chain = optarg;
1037                         break;
1038
1039                 case 'L':
1040                         dump = true;
1041                         break;
1042
1043                 case 'N':
1044                         new_chain = optarg;
1045                         break;
1046
1047                 case 'X':
1048                         delete = true;
1049                         delete_chain = optarg;
1050                         break;
1051
1052                 case 'j':
1053                         target_name = optarg;
1054                         xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1055
1056                         if (xt_t == NULL)
1057                                 break;
1058
1059                         size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1060
1061                         xt_t->t = g_try_malloc0(size);
1062                         if (xt_t->t == NULL)
1063                                 goto out;
1064                         xt_t->t->u.target_size = size;
1065                         strcpy(xt_t->t->u.user.name, target_name);
1066                         xt_t->t->u.user.revision = xt_t->revision;
1067                         if (xt_t->init != NULL)
1068                                 xt_t->init(xt_t->t);
1069                         connman_iptables_globals.opts =
1070                                 xtables_merge_options(
1071 #if XTABLES_VERSION_CODE > 5
1072                                                      connman_iptables_globals.orig_opts,
1073 #endif
1074                                                      connman_iptables_globals.opts,
1075                                                      xt_t->extra_opts,
1076                                                      &xt_t->option_offset);
1077                         if (connman_iptables_globals.opts == NULL)
1078                                 goto out;
1079
1080                         break;
1081
1082                 case 'i':
1083                         break;
1084
1085                 case 'm':
1086                         match_name = optarg;
1087
1088                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1089                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1090                         xt_m->m = g_try_malloc0(size);
1091                         if (xt_m == NULL)
1092                                 goto out;
1093                         xt_m->m->u.match_size = size;
1094                         strcpy(xt_m->m->u.user.name, xt_m->name);
1095                         xt_m->m->u.user.revision = xt_m->revision;
1096                         if (xt_m->init != NULL)
1097                                 xt_m->init(xt_m->m);
1098                         if (xt_m != xt_m->next) {
1099                                 connman_iptables_globals.opts =
1100                                         xtables_merge_options(
1101 #if XTABLES_VERSION_CODE > 5
1102                                                 connman_iptables_globals.orig_opts,
1103 #endif
1104                                                 connman_iptables_globals.opts,
1105                                                 xt_m->extra_opts,
1106                                                 &xt_m->option_offset);
1107                                 if (connman_iptables_globals.opts == NULL)
1108                                         goto out;
1109                         }
1110
1111                         break;
1112
1113                 case 'o':
1114                         break;
1115
1116                 case 't':
1117                         table_name = optarg;
1118                         break;
1119
1120                 case 1:
1121                         if (optarg[0] == '!' && optarg[1] == '\0') {
1122                                 if (invert)
1123                                         printf("Consecutive ! not allowed\n");
1124
1125                                 invert = TRUE;
1126                                 optarg[0] = '\0';
1127                                 continue;
1128                         }
1129
1130                         printf("Invalid option\n");
1131
1132                         return -1;
1133
1134                 default:
1135                         if (xt_t == NULL || xt_t->parse == NULL ||
1136                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
1137                                         &xt_t->tflags, NULL, &xt_t->t)) {
1138                                 if (xt_m == NULL || xt_m->parse == NULL)
1139                                         break;
1140
1141                                 xt_m->parse(c - xt_m->option_offset, argv,
1142                                         invert, &xt_m->mflags, NULL, &xt_m->m);
1143                         }
1144
1145                         break;
1146                 }
1147         }
1148
1149         if (table_name == NULL)
1150                 table_name = "filter";
1151
1152         table = connman_iptables_init(table_name);
1153         if (table == NULL)
1154                 return -1;
1155
1156         if (delete) {
1157                 if (delete_chain == NULL)
1158                         goto out;
1159
1160                 printf("Delete chain %s\n", delete_chain);
1161
1162                 connman_iptables_delete_chain(table, delete_chain);
1163
1164                 goto commit;
1165         }
1166
1167         if (flush_chain) {
1168                 printf("Flush chain %s\n", flush_chain);
1169
1170                 connman_iptables_flush_chain(table, flush_chain);
1171
1172                 goto commit;
1173         }
1174
1175         if (dump) {
1176                 connman_iptables_dump(table);
1177
1178                 return 0;
1179         }
1180
1181         if (chain && new_chain)
1182                 return -1;
1183
1184         if (new_chain) {
1185                 printf("New chain %s\n", new_chain);
1186
1187                 connman_iptables_add_chain(table, new_chain);
1188
1189                 goto commit;
1190         }
1191
1192         if (chain) {
1193                 if (target_name == NULL)
1194                         return -1;
1195
1196                 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
1197
1198                 connman_iptables_add_rule(table, chain, target_name, xt_t,
1199                                         match_name, xt_m);
1200
1201                 goto commit;
1202         }
1203
1204 commit:
1205
1206         connman_iptables_commit(table);
1207
1208 out:
1209         connman_iptables_cleanup(table);
1210
1211         if (xt_t)
1212                 g_free(xt_t->t);
1213
1214         if (xt_m)
1215                 g_free(xt_m->m);
1216
1217         return 0;
1218 }