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