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