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