tools: find_chain_head/find_chain_tail code factorization in iptables-test
[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 connman_iptables_flush_chain(struct connman_iptables *table,
331                                                 char *name)
332 {
333         GList *chain_head, *chain_tail, *list, *next;
334         struct connman_iptables_entry *entry;
335         int builtin, removed = 0;
336
337         chain_head = find_chain_head(table, name);
338         if (chain_head == NULL)
339                 return -EINVAL;
340
341         chain_tail = find_chain_tail(table, name);
342         if (chain_tail == NULL)
343                 return -EINVAL;
344
345         entry = chain_head->data;
346         builtin = entry->builtin;
347
348         if (builtin >= 0)
349                 list = chain_head;
350         else
351                 list = chain_head->next;
352
353         if (list == chain_tail->prev)
354                 return 0;
355
356         while (list != chain_tail->prev) {
357                 entry = list->data;
358                 next = g_list_next(list);
359
360                 table->num_entries--;
361                 table->size -= entry->entry->next_offset;
362                 removed += entry->entry->next_offset;
363
364                 table->entries = g_list_remove(table->entries, list->data);
365
366                 list = next;
367         }
368
369         if (builtin >= 0) {
370                 struct connman_iptables_entry *e;
371
372                 entry = list->data;
373
374                 entry->builtin = builtin;
375
376                 table->underflow[builtin] -= removed;
377
378                 for (list = chain_tail; list; list = list->next) {
379                         e = list->data;
380
381                         builtin = e->builtin;
382                         if (builtin < 0)
383                                 continue;
384
385                         table->hook_entry[builtin] -= removed;
386                         table->underflow[builtin] -= removed;
387                 }
388         }
389
390         update_offsets(table);
391
392         return 0;
393 }
394
395 static int connman_iptables_delete_chain(struct connman_iptables *table,
396                                                 char *name)
397 {
398         GList *chain_head, *chain_tail, *list, *next;
399         struct connman_iptables_entry *entry;
400
401         chain_head = find_chain_head(table, name);
402         if (chain_head == NULL)
403                 return -EINVAL;
404
405         chain_tail = find_chain_tail(table, name);
406         if (chain_tail == NULL)
407                 return -EINVAL;
408
409         list = chain_head;
410
411         while (list != chain_tail) {
412                 entry = list->data;
413                 next = g_list_next(list);
414
415                 table->num_entries--;
416                 table->size -= entry->entry->next_offset;
417
418                 table->entries = g_list_remove(table->entries, list->data);
419
420                 list = next;
421         }
422
423         update_offsets(table);
424
425         return 0;
426 }
427
428 static int connman_iptables_add_chain(struct connman_iptables *table,
429                                         char *name)
430 {
431         GList *last;
432         struct ipt_entry *entry_head;
433         struct ipt_entry *entry_return;
434         struct error_target *error;
435         struct ipt_standard_target *standard;
436         u_int16_t entry_head_size, entry_return_size;
437
438         last = g_list_last(table->entries);
439
440         /*
441          * An empty chain is composed of:
442          * - A head entry, with no match and an error target.
443          *   The error target data is the chain name.
444          * - A tail entry, with no match and a standard target.
445          *   The standard target verdict is XT_RETURN (return to the
446          *   caller).
447          */
448
449         /* head entry */
450         entry_head_size = sizeof(struct ipt_entry) +
451                                 sizeof(struct error_target);
452         entry_head = g_try_malloc0(entry_head_size);
453         if (entry_head == NULL)
454                 goto err_head;
455
456         memset(entry_head, 0, entry_head_size);
457
458         entry_head->target_offset = sizeof(struct ipt_entry);
459         entry_head->next_offset = entry_head_size;
460
461         error = (struct error_target *) entry_head->elems;
462         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
463         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
464         strcpy(error->error, name);
465
466         if (connman_add_entry(table, entry_head, last, -1) < 0)
467                 goto err_head;
468
469         /* tail entry */
470         entry_return_size = sizeof(struct ipt_entry) +
471                                 sizeof(struct ipt_standard_target);
472         entry_return = g_try_malloc0(entry_return_size);
473         if (entry_return == NULL)
474                 goto err;
475
476         memset(entry_return, 0, entry_return_size);
477
478         entry_return->target_offset = sizeof(struct ipt_entry);
479         entry_return->next_offset = entry_return_size;
480
481         standard = (struct ipt_standard_target *) entry_return->elems;
482         standard->target.u.user.target_size =
483                                 ALIGN(sizeof(struct ipt_standard_target));
484         standard->verdict = XT_RETURN;
485
486         if (connman_add_entry(table, entry_return, last, -1) < 0)
487                 goto err;
488
489         return 0;
490
491 err:
492         g_free(entry_return);
493 err_head:
494         g_free(entry_head);
495
496         return -ENOMEM;
497 }
498
499 static struct ipt_entry *
500 new_rule(struct connman_iptables *table,
501                 char *target_name, struct xtables_target *xt_t,
502                 char *match_name, struct xtables_match *xt_m)
503 {
504         struct ipt_entry *new_entry;
505         size_t match_size, target_size;
506         int is_builtin = is_builtin_target(target_name);
507
508         if (xt_m)
509                 match_size = xt_m->m->u.match_size;
510         else
511                 match_size = 0;
512
513         if (xt_t)
514                 target_size = ALIGN(xt_t->t->u.target_size);
515         else
516                 target_size = ALIGN(sizeof(struct xt_standard_target));
517
518         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
519                                                                 match_size);
520         if (new_entry == NULL)
521                 return NULL;
522
523         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
524         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
525                                                                 match_size;
526         if (xt_m) {
527                 struct xt_entry_match *entry_match;
528
529                 entry_match = (struct xt_entry_match *)new_entry->elems;
530                 memcpy(entry_match, xt_m->m, match_size);
531         }
532
533         if (xt_t) {
534                 struct xt_entry_target *entry_target;
535
536                 if (is_builtin) {
537                         struct xt_standard_target *target;
538
539                         target = (struct xt_standard_target *)(xt_t->t);
540                         strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
541                         target->verdict = target_to_verdict(target_name);
542                 }
543
544                 entry_target = ipt_get_target(new_entry);
545                 memcpy(entry_target, xt_t->t, target_size);
546         } else {
547                 struct connman_iptables_entry *target_rule;
548                 struct xt_standard_target *target;
549                 GList *chain_head;
550
551                 /*
552                  * This is a user defined target, i.e. a chain jump.
553                  * We search for the chain head, and the target verdict
554                  * is the first rule's offset on this chain.
555                  * The offset is from the beginning of the table.
556                  */
557
558                 chain_head = find_chain_head(table, target_name);
559                 if (chain_head == NULL || chain_head->next == NULL) {
560                         g_free(new_entry);
561                         return NULL;
562                 }
563
564                 target_rule = chain_head->next->data;
565
566                 target = (struct xt_standard_target *)ipt_get_target(new_entry);
567                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
568                 target->target.u.user.target_size = target_size;
569                 target->verdict = target_rule->offset;
570         }
571
572         return new_entry;
573 }
574
575 static void update_hooks(struct connman_iptables *table, GList *chain_head, struct ipt_entry *entry)
576 {
577         GList *list;
578         struct connman_iptables_entry *head, *e;
579         int builtin;
580
581         if (chain_head == NULL)
582                 return;
583
584         head = chain_head->data;
585
586         builtin = head->builtin;
587         if (builtin < 0)
588                 return;
589
590         table->underflow[builtin] += entry->next_offset;
591
592         for (list = chain_head->next; list; list = list->next) {
593                 e = list->data;
594
595                 builtin = e->builtin;
596                 if (builtin < 0)
597                         continue;
598
599                 table->hook_entry[builtin] += entry->next_offset;
600                 table->underflow[builtin] += entry->next_offset;
601         }
602 }
603
604 static int
605 connman_iptables_add_rule(struct connman_iptables *table, char *chain_name,
606                                 char *target_name, struct xtables_target *xt_t,
607                                 char *match_name, struct xtables_match *xt_m)
608 {
609         GList *chain_tail, *chain_head;
610         struct ipt_entry *new_entry;
611         struct connman_iptables_entry *head;
612         int builtin = -1;
613
614         chain_head = find_chain_head(table, chain_name);
615         if (chain_head == NULL)
616                 return -EINVAL;
617
618         chain_tail = find_chain_tail(table, chain_name);
619         if (chain_tail == NULL)
620                 return -EINVAL;
621
622         new_entry = new_rule(table,
623                                 target_name, xt_t,
624                                 match_name, xt_m);
625         if (new_entry == NULL)
626                 return -EINVAL;
627
628         update_hooks(table, chain_head, new_entry);
629
630         /*
631          * If the chain is builtin, and does not have any rule,
632          * then the one that we're inserting is becoming the head
633          * and thus needs the builtin flag.
634          */
635         head = chain_head->data;
636         if (head->builtin < 0)
637                 builtin = -1;
638         else if (chain_head == chain_tail->prev) {
639                 builtin = head->builtin;
640                 head->builtin = -1;
641         }
642
643         return connman_add_entry(table, new_entry, chain_tail->prev, builtin);
644 }
645
646 static struct ipt_replace *
647 connman_iptables_blob(struct connman_iptables *table)
648 {
649         struct ipt_replace *r;
650         GList *list;
651         struct connman_iptables_entry *e;
652         unsigned char *entry_index;
653
654         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
655         if (r == NULL)
656                 return NULL;
657
658         memset(r, 0, sizeof(*r) + table->size);
659
660         r->counters = g_try_malloc0(sizeof(struct xt_counters)
661                                 * table->old_entries);
662         if (r->counters == NULL) {
663                 g_free(r);
664                 return NULL;
665         }
666
667         strcpy(r->name, table->info->name);
668         r->num_entries = table->num_entries;
669         r->size = table->size;
670
671         r->num_counters = table->old_entries;
672         r->valid_hooks  = table->info->valid_hooks;
673
674         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
675         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
676
677         entry_index = (unsigned char *)r->entries;
678         for (list = table->entries; list; list = list->next) {
679                 e = list->data;
680
681                 memcpy(entry_index, e->entry, e->entry->next_offset);
682                 entry_index += e->entry->next_offset;
683         }
684
685         return r;
686 }
687
688 static void dump_target(struct connman_iptables *table,
689                                 struct ipt_entry *entry)
690
691 {
692         struct xtables_target *xt_t;
693         struct xt_entry_target *target;
694
695         target = ipt_get_target(entry);
696
697         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
698                 struct xt_standard_target *t;
699
700                 t = (struct xt_standard_target *)target;
701
702                 switch (t->verdict) {
703                 case XT_RETURN:
704                         printf("\ttarget RETURN\n");
705                         break;
706
707                 case -NF_ACCEPT - 1:
708                         printf("\ttarget ACCEPT\n");
709                         break;
710
711                 case -NF_DROP - 1:
712                         printf("\ttarget DROP\n");
713                         break;
714
715                 case -NF_QUEUE - 1:
716                         printf("\ttarget QUEUE\n");
717                         break;
718
719                 case -NF_STOP - 1:
720                         printf("\ttarget STOP\n");
721                         break;
722
723                 default:
724                         printf("\tJUMP @%p (0x%x)\n",
725                                 (char*)table->blob_entries->entrytable +
726                                 t->verdict, t->verdict);
727                         break;
728                 }
729
730                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
731                                                 XTF_LOAD_MUST_SUCCEED);
732
733                 if(xt_t->print != NULL)
734                         xt_t->print(NULL, target, 1);
735         } else {
736                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
737                 if (xt_t == NULL) {
738                         printf("\ttarget %s\n", target->u.user.name);
739                         return;
740                 }
741
742                 if(xt_t->print != NULL) {
743                         printf("\ttarget ");
744                         xt_t->print(NULL, target, 1);
745                         printf("\n");
746                 }
747         }
748 }
749
750 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
751 {
752         struct xtables_match *xt_m;
753         struct xt_entry_match *match;
754
755         if (entry->elems == (unsigned char *)entry + entry->target_offset)
756                 return;
757
758         match = (struct xt_entry_match *) entry->elems;
759
760         if (!strlen(match->u.user.name))
761                 return;
762
763         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
764         if (xt_m == NULL)
765                 goto out;
766
767         if(xt_m->print != NULL) {
768                 printf("\tmatch ");
769                 xt_m->print(NULL, match, 1);
770                 printf("\n");
771
772                 return;
773         }
774
775 out:
776         printf("\tmatch %s\n", match->u.user.name);
777
778 }
779
780 static int connman_iptables_dump_entry(struct ipt_entry *entry,
781                                         struct connman_iptables *table)
782 {
783         struct xt_entry_target *target;
784         unsigned int offset;
785         int builtin;
786
787         offset = (char *)entry - (char *)table->blob_entries->entrytable;
788         target = ipt_get_target(entry);
789         builtin = is_hook_entry(table, entry);
790
791         if (entry_to_offset(table, entry) + entry->next_offset ==
792                                         table->blob_entries->size) {
793                 printf("End of CHAIN 0x%x\n", offset);
794                 return 0;
795         }
796
797         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
798                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
799                         target->data, entry, entry->elems,
800                         (char *)entry + entry->target_offset,
801                                 entry->next_offset);
802
803                 return 0;
804         } else if (builtin >= 0) {
805                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
806                         hooknames[builtin], entry, entry->elems,
807                         (char *)entry + entry->target_offset,
808                                 entry->next_offset);
809         } else {
810                 printf("RULE %p  match %p  target %p  size %d\n", entry,
811                         entry->elems,
812                         (char *)entry + entry->target_offset,
813                                 entry->next_offset);
814         }
815
816         dump_match(table, entry);
817         dump_target(table, entry);
818
819         return 0;
820 }
821
822 static void connman_iptables_dump_hook(struct connman_iptables *table)
823 {
824         int i;
825         printf("hooks: \n");
826         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
827                 if ((table->info->valid_hooks & (1 << i)))
828                         printf("%s entry %p underflow %p (%#x)\n",
829                                 hooknames[i],
830                                 table->blob_entries->entrytable +
831                                                 table->info->hook_entry[i],
832                                 table->blob_entries->entrytable +
833                                                 table->info->underflow[i],
834                                         table->info->underflow[i]);
835         }
836 }
837
838 static void connman_iptables_dump(struct connman_iptables *table)
839 {
840         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
841                 table->info->name,
842                 table->info->valid_hooks, table->info->num_entries,
843                 table->info->size);
844
845         connman_iptables_dump_hook(table);
846
847         ENTRY_ITERATE(table->blob_entries->entrytable,
848                         table->blob_entries->size,
849                         connman_iptables_dump_entry, table);
850
851 }
852
853 static int connman_iptables_get_entries(struct connman_iptables *table)
854 {
855         socklen_t entry_size;
856
857         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
858
859         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
860                                 table->blob_entries, &entry_size);
861 }
862
863 static int connman_iptables_replace(struct connman_iptables *table,
864                                         struct ipt_replace *r)
865 {
866         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
867                          sizeof(*r) + r->size);
868 }
869
870 static void connman_iptables_cleanup(struct connman_iptables *table)
871 {
872         GList *list;
873         struct connman_iptables_entry *entry;
874
875         close(table->ipt_sock);
876
877         for (list = table->entries; list; list = list->next) {
878                 entry = list->data;
879
880                 g_free(entry->entry);
881         }
882
883         g_free(table->info);
884         g_free(table->blob_entries);
885         g_free(table);
886
887         xtables_free_opts(1);
888 }
889
890 static int connman_iptables_commit(struct connman_iptables *table)
891 {
892         struct ipt_replace *repl;
893
894         repl = connman_iptables_blob(table);
895
896         return connman_iptables_replace(table, repl);
897 }
898
899 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
900 {
901         struct ipt_entry *new_entry;
902         int builtin;
903
904         new_entry = g_try_malloc0(entry->next_offset);
905         if (new_entry == NULL)
906                 return -ENOMEM;
907
908         memcpy(new_entry, entry, entry->next_offset);
909
910         builtin = is_hook_entry(table, entry);
911
912         return connman_add_entry(table, new_entry, NULL, builtin);
913 }
914
915 static struct connman_iptables *connman_iptables_init(const char *table_name)
916 {
917         struct connman_iptables *table;
918         socklen_t s;
919
920         table =  g_try_new0(struct connman_iptables, 1);
921         if (table == NULL)
922                 return NULL;
923
924         table->info =  g_try_new0(struct ipt_getinfo, 1);
925         if (table->info == NULL)
926                 goto err;
927
928         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
929         if (table->ipt_sock < 0)
930                 goto err;
931
932         s = sizeof(*table->info);
933         strcpy(table->info->name, table_name);
934         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
935                                                 table->info, &s) < 0)
936                 goto err;
937
938         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
939                                                 table->info->size);
940         if (table->blob_entries == NULL)
941                 goto err;
942
943         strcpy(table->blob_entries->name, table_name);
944         table->blob_entries->size = table->info->size;
945
946         if (connman_iptables_get_entries(table) < 0)
947                 goto err;
948
949         table->num_entries = 0;
950         table->old_entries = table->info->num_entries;
951         table->size = 0;
952
953         memcpy(table->underflow, table->info->underflow,
954                                 sizeof(table->info->underflow));
955         memcpy(table->hook_entry, table->info->hook_entry,
956                                 sizeof(table->info->hook_entry));
957
958         ENTRY_ITERATE(table->blob_entries->entrytable,
959                         table->blob_entries->size,
960                                 add_entry, table);
961
962
963         return table;
964
965 err:
966
967         connman_iptables_cleanup(table);
968
969         return NULL;
970 }
971
972
973 static struct option connman_iptables_opts[] = {
974         {.name = "append",        .has_arg = 1, .val = 'A'},
975         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
976         {.name = "list",          .has_arg = 2, .val = 'L'},
977         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
978         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
979         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
980         {.name = "jump",          .has_arg = 1, .val = 'j'},
981         {.name = "match",         .has_arg = 1, .val = 'm'},
982         {.name = "out-interface", .has_arg = 1, .val = 'o'},
983         {.name = "table",         .has_arg = 1, .val = 't'},
984         {NULL},
985 };
986
987 struct xtables_globals connman_iptables_globals = {
988         .option_offset = 0,
989         .opts = connman_iptables_opts,
990         .orig_opts = connman_iptables_opts,
991 };
992
993 int main(int argc, char *argv[])
994 {
995         struct connman_iptables *table;
996         struct xtables_match *xt_m;
997         struct xtables_target *xt_t;
998         char *table_name, *chain, *new_chain, *match_name, *target_name;
999         char *delete_chain, *flush_chain;
1000         int c;
1001         size_t size;
1002         gboolean dump, invert, delete;
1003
1004         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1005
1006         dump = FALSE;
1007         invert = FALSE;
1008         delete = FALSE;
1009         table_name = chain = new_chain = match_name = target_name = NULL;
1010         delete_chain = flush_chain = NULL;
1011         table = NULL;
1012         xt_m = NULL;
1013         xt_t = NULL;
1014
1015         while ((c = getopt_long(argc, argv,
1016            "-A:F:L::N:X:j:i:m:o:t:", connman_iptables_globals.opts, NULL)) != -1) {
1017                 switch (c) {
1018                 case 'A':
1019                         chain = optarg;
1020                         break;
1021
1022                 case 'F':
1023                         flush_chain = optarg;
1024                         break;
1025
1026                 case 'L':
1027                         dump = true;
1028                         break;
1029
1030                 case 'N':
1031                         new_chain = optarg;
1032                         break;
1033
1034                 case 'X':
1035                         delete = true;
1036                         delete_chain = optarg;
1037                         break;
1038
1039                 case 'j':
1040                         target_name = optarg;
1041                         xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1042
1043                         if (xt_t == NULL)
1044                                 break;
1045
1046                         size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1047
1048                         xt_t->t = g_try_malloc0(size);
1049                         if (xt_t->t == NULL)
1050                                 goto out;
1051                         xt_t->t->u.target_size = size;
1052                         strcpy(xt_t->t->u.user.name, target_name);
1053                         xt_t->t->u.user.revision = xt_t->revision;
1054                         if (xt_t->init != NULL)
1055                                 xt_t->init(xt_t->t);
1056                         connman_iptables_globals.opts =
1057                                 xtables_merge_options(
1058 #if XTABLES_VERSION_CODE > 5
1059                                                      connman_iptables_globals.orig_opts,
1060 #endif
1061                                                      connman_iptables_globals.opts,
1062                                                      xt_t->extra_opts,
1063                                                      &xt_t->option_offset);
1064                         if (connman_iptables_globals.opts == NULL)
1065                                 goto out;
1066
1067                         break;
1068
1069                 case 'i':
1070                         break;
1071
1072                 case 'm':
1073                         match_name = optarg;
1074
1075                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1076                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1077                         xt_m->m = g_try_malloc0(size);
1078                         if (xt_m == NULL)
1079                                 goto out;
1080                         xt_m->m->u.match_size = size;
1081                         strcpy(xt_m->m->u.user.name, xt_m->name);
1082                         xt_m->m->u.user.revision = xt_m->revision;
1083                         if (xt_m->init != NULL)
1084                                 xt_m->init(xt_m->m);
1085                         if (xt_m != xt_m->next) {
1086                                 connman_iptables_globals.opts =
1087                                         xtables_merge_options(
1088 #if XTABLES_VERSION_CODE > 5
1089                                                 connman_iptables_globals.orig_opts,
1090 #endif
1091                                                 connman_iptables_globals.opts,
1092                                                 xt_m->extra_opts,
1093                                                 &xt_m->option_offset);
1094                                 if (connman_iptables_globals.opts == NULL)
1095                                         goto out;
1096                         }
1097
1098                         break;
1099
1100                 case 'o':
1101                         break;
1102
1103                 case 't':
1104                         table_name = optarg;
1105                         break;
1106
1107                 case 1:
1108                         if (optarg[0] == '!' && optarg[1] == '\0') {
1109                                 if (invert)
1110                                         printf("Consecutive ! not allowed\n");
1111
1112                                 invert = TRUE;
1113                                 optarg[0] = '\0';
1114                                 continue;
1115                         }
1116
1117                         printf("Invalid option\n");
1118
1119                         return -1;
1120
1121                 default:
1122                         if (xt_t == NULL || xt_t->parse == NULL ||
1123                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
1124                                         &xt_t->tflags, NULL, &xt_t->t)) {
1125                                 if (xt_m == NULL || xt_m->parse == NULL)
1126                                         break;
1127
1128                                 xt_m->parse(c - xt_m->option_offset, argv,
1129                                         invert, &xt_m->mflags, NULL, &xt_m->m);
1130                         }
1131
1132                         break;
1133                 }
1134         }
1135
1136         if (table_name == NULL)
1137                 table_name = "filter";
1138
1139         table = connman_iptables_init(table_name);
1140         if (table == NULL)
1141                 return -1;
1142
1143         if (delete) {
1144                 if (delete_chain == NULL)
1145                         goto out;
1146
1147                 printf("Delete chain %s\n", delete_chain);
1148
1149                 connman_iptables_delete_chain(table, delete_chain);
1150
1151                 goto commit;
1152         }
1153
1154         if (flush_chain) {
1155                 printf("Flush chain %s\n", flush_chain);
1156
1157                 connman_iptables_flush_chain(table, flush_chain);
1158
1159                 goto commit;
1160         }
1161
1162         if (dump) {
1163                 connman_iptables_dump(table);
1164
1165                 return 0;
1166         }
1167
1168         if (chain && new_chain)
1169                 return -1;
1170
1171         if (new_chain) {
1172                 printf("New chain %s\n", new_chain);
1173
1174                 connman_iptables_add_chain(table, new_chain);
1175
1176                 goto commit;
1177         }
1178
1179         if (chain) {
1180                 if (target_name == NULL)
1181                         return -1;
1182
1183                 printf("Adding %s to %s (match %s)\n", target_name, chain, match_name);
1184
1185                 connman_iptables_add_rule(table, chain, target_name, xt_t,
1186                                         match_name, xt_m);
1187
1188                 goto commit;
1189         }
1190
1191 commit:
1192
1193         connman_iptables_commit(table);
1194
1195 out:
1196         connman_iptables_cleanup(table);
1197
1198         if (xt_t)
1199                 g_free(xt_t->t);
1200
1201         if (xt_m)
1202                 g_free(xt_m->m);
1203
1204         return 0;
1205 }