tools: Factorization of target reference update in iptables-test
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/socket.h>
28 #include <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 void update_targets_reference(struct connman_iptables *table,
282                                 struct connman_iptables_entry *entry_before,
283                                 struct connman_iptables_entry *modified_entry)
284 {
285         struct connman_iptables_entry *tmp;
286         struct xt_standard_target *t;
287         GList *list;
288
289         for (list = table->entries; list; list = list->next) {
290                 tmp = list->data;
291
292                 if (!is_jump(tmp))
293                         continue;
294
295                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
296
297                 if (t->verdict > entry_before->offset)
298                         t->verdict += modified_entry->entry->next_offset;
299         }
300 }
301
302 static int connman_add_entry(struct connman_iptables *table,
303                                 struct ipt_entry *entry, GList *before,
304                                         int builtin)
305 {
306         struct connman_iptables_entry *e, *entry_before;
307
308         if (table == NULL)
309                 return -1;
310
311         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
312         if (e == NULL)
313                 return -1;
314
315         e->entry = entry;
316         e->builtin = builtin;
317
318         table->entries = g_list_insert_before(table->entries, before, e);
319         table->num_entries++;
320         table->size += entry->next_offset;
321
322         if (before == NULL) {
323                 e->offset = table->size - entry->next_offset;
324
325                 return 0;
326         }
327
328         entry_before = before->data;
329
330         /*
331          * We've just appended/insterted a new entry. All references
332          * should be bumped accordingly.
333          */
334         update_targets_reference(table, entry_before, e);
335
336         update_offsets(table);
337
338         return 0;
339 }
340
341 static int remove_table_entry(struct connman_iptables *table,
342                                         struct connman_iptables_entry *entry)
343 {
344         int removed = 0;
345
346         table->num_entries--;
347         table->size -= entry->entry->next_offset;
348         removed = entry->entry->next_offset;
349
350         g_free(entry->entry);
351
352         table->entries = g_list_remove(table->entries, entry);
353
354         return removed;
355 }
356
357 static int connman_iptables_flush_chain(struct connman_iptables *table,
358                                                 char *name)
359 {
360         GList *chain_head, *chain_tail, *list, *next;
361         struct connman_iptables_entry *entry;
362         int builtin, removed = 0;
363
364         chain_head = find_chain_head(table, name);
365         if (chain_head == NULL)
366                 return -EINVAL;
367
368         chain_tail = find_chain_tail(table, name);
369         if (chain_tail == NULL)
370                 return -EINVAL;
371
372         entry = chain_head->data;
373         builtin = entry->builtin;
374
375         if (builtin >= 0)
376                 list = chain_head;
377         else
378                 list = chain_head->next;
379
380         if (list == chain_tail->prev)
381                 return 0;
382
383         while (list != chain_tail->prev) {
384                 entry = list->data;
385                 next = g_list_next(list);
386
387                 removed += remove_table_entry(table, entry);
388
389                 list = next;
390         }
391
392         if (builtin >= 0) {
393                 struct connman_iptables_entry *e;
394
395                 entry = list->data;
396
397                 entry->builtin = builtin;
398
399                 table->underflow[builtin] -= removed;
400
401                 for (list = chain_tail; list; list = list->next) {
402                         e = list->data;
403
404                         builtin = e->builtin;
405                         if (builtin < 0)
406                                 continue;
407
408                         table->hook_entry[builtin] -= removed;
409                         table->underflow[builtin] -= removed;
410                 }
411         }
412
413         update_offsets(table);
414
415         return 0;
416 }
417
418 static int connman_iptables_delete_chain(struct connman_iptables *table,
419                                                 char *name)
420 {
421         GList *chain_head, *chain_tail;
422         struct connman_iptables_entry *entry;
423
424         chain_head = find_chain_head(table, name);
425         if (chain_head == NULL)
426                 return -EINVAL;
427
428         entry = chain_head->data;
429
430         /* We cannot remove builtin chain */
431         if (entry->builtin >= 0)
432                 return -EINVAL;
433
434         chain_tail = find_chain_tail(table, name);
435         if (chain_tail == NULL)
436                 return -EINVAL;
437
438         /* Chain must be flushed */
439         if (chain_head->next != chain_tail->prev)
440                 return -EINVAL;
441
442         remove_table_entry(table, entry);
443
444         entry = chain_tail->prev->data;
445         remove_table_entry(table, entry);
446
447         update_offsets(table);
448
449         return 0;
450 }
451
452 static int connman_iptables_add_chain(struct connman_iptables *table,
453                                         char *name)
454 {
455         GList *last;
456         struct ipt_entry *entry_head;
457         struct ipt_entry *entry_return;
458         struct error_target *error;
459         struct ipt_standard_target *standard;
460         u_int16_t entry_head_size, entry_return_size;
461
462         last = g_list_last(table->entries);
463
464         /*
465          * An empty chain is composed of:
466          * - A head entry, with no match and an error target.
467          *   The error target data is the chain name.
468          * - A tail entry, with no match and a standard target.
469          *   The standard target verdict is XT_RETURN (return to the
470          *   caller).
471          */
472
473         /* head entry */
474         entry_head_size = sizeof(struct ipt_entry) +
475                                 sizeof(struct error_target);
476         entry_head = g_try_malloc0(entry_head_size);
477         if (entry_head == NULL)
478                 goto err_head;
479
480         memset(entry_head, 0, entry_head_size);
481
482         entry_head->target_offset = sizeof(struct ipt_entry);
483         entry_head->next_offset = entry_head_size;
484
485         error = (struct error_target *) entry_head->elems;
486         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
487         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
488         strcpy(error->error, name);
489
490         if (connman_add_entry(table, entry_head, last, -1) < 0)
491                 goto err_head;
492
493         /* tail entry */
494         entry_return_size = sizeof(struct ipt_entry) +
495                                 sizeof(struct ipt_standard_target);
496         entry_return = g_try_malloc0(entry_return_size);
497         if (entry_return == NULL)
498                 goto err;
499
500         memset(entry_return, 0, entry_return_size);
501
502         entry_return->target_offset = sizeof(struct ipt_entry);
503         entry_return->next_offset = entry_return_size;
504
505         standard = (struct ipt_standard_target *) entry_return->elems;
506         standard->target.u.user.target_size =
507                                 ALIGN(sizeof(struct ipt_standard_target));
508         standard->verdict = XT_RETURN;
509
510         if (connman_add_entry(table, entry_return, last, -1) < 0)
511                 goto err;
512
513         return 0;
514
515 err:
516         g_free(entry_return);
517 err_head:
518         g_free(entry_head);
519
520         return -ENOMEM;
521 }
522
523 static struct ipt_entry *
524 new_rule(struct ipt_ip *ip, char *target_name,
525                 struct xtables_target *xt_t,
526                 char *match_name, struct xtables_match *xt_m)
527 {
528         struct ipt_entry *new_entry;
529         size_t match_size, target_size;
530
531         if (xt_m)
532                 match_size = xt_m->m->u.match_size;
533         else
534                 match_size = 0;
535
536         if (xt_t)
537                 target_size = ALIGN(xt_t->t->u.target_size);
538         else
539                 target_size = ALIGN(sizeof(struct xt_standard_target));
540
541         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
542                                                                 match_size);
543         if (new_entry == NULL)
544                 return NULL;
545
546         memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
547
548         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
549         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
550                                                                 match_size;
551         if (xt_m) {
552                 struct xt_entry_match *entry_match;
553
554                 entry_match = (struct xt_entry_match *)new_entry->elems;
555                 memcpy(entry_match, xt_m->m, match_size);
556         }
557
558         if (xt_t) {
559                 struct xt_entry_target *entry_target;
560
561                 entry_target = ipt_get_target(new_entry);
562                 memcpy(entry_target, xt_t->t, target_size);
563         }
564
565         return new_entry;
566 }
567
568 static void update_hooks(struct connman_iptables *table, GList *chain_head,
569                                 struct ipt_entry *entry)
570 {
571         GList *list;
572         struct connman_iptables_entry *head, *e;
573         int builtin;
574
575         if (chain_head == NULL)
576                 return;
577
578         head = chain_head->data;
579
580         builtin = head->builtin;
581         if (builtin < 0)
582                 return;
583
584         table->underflow[builtin] += entry->next_offset;
585
586         for (list = chain_head->next; list; list = list->next) {
587                 e = list->data;
588
589                 builtin = e->builtin;
590                 if (builtin < 0)
591                         continue;
592
593                 table->hook_entry[builtin] += entry->next_offset;
594                 table->underflow[builtin] += entry->next_offset;
595         }
596 }
597
598 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
599                                 struct ipt_ip *ip, char *chain_name,
600                                 char *target_name, struct xtables_target *xt_t,
601                                 char *match_name, struct xtables_match *xt_m,
602                                 int *builtin)
603 {
604         GList *chain_tail, *chain_head;
605         struct ipt_entry *new_entry;
606         struct connman_iptables_entry *head;
607
608         chain_head = find_chain_head(table, chain_name);
609         if (chain_head == NULL)
610                 return NULL;
611
612         chain_tail = find_chain_tail(table, chain_name);
613         if (chain_tail == NULL)
614                 return NULL;
615
616         new_entry = new_rule(ip, target_name, xt_t, match_name, xt_m);
617         if (new_entry == NULL)
618                 return NULL;
619
620         update_hooks(table, chain_head, new_entry);
621
622         /*
623          * If the chain is builtin, and does not have any rule,
624          * then the one that we're inserting is becoming the head
625          * and thus needs the builtin flag.
626          */
627         head = chain_head->data;
628         if (head->builtin < 0)
629                 *builtin = -1;
630         else if (chain_head == chain_tail->prev) {
631                 *builtin = head->builtin;
632                 head->builtin = -1;
633         }
634
635         return new_entry;
636 }
637
638 static int
639 connman_iptables_append_rule(struct connman_iptables *table,
640                                 struct ipt_ip *ip, char *chain_name,
641                                 char *target_name, struct xtables_target *xt_t,
642                                 char *match_name, struct xtables_match *xt_m)
643 {
644         GList *chain_tail;
645         struct ipt_entry *new_entry;
646         int builtin = -1, ret;
647
648         chain_tail = find_chain_tail(table, chain_name);
649         if (chain_tail == NULL)
650                 return -EINVAL;
651
652         new_entry = prepare_rule_inclusion(table, ip, chain_name,
653                         target_name, xt_t, match_name, xt_m, &builtin);
654         if (new_entry == NULL)
655                 return -EINVAL;
656
657         ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
658         if (ret < 0)
659                 g_free(new_entry);
660
661         return ret;
662 }
663
664 static int connman_iptables_insert_rule(struct connman_iptables *table,
665                                 struct ipt_ip *ip, char *chain_name,
666                                 char *target_name, struct xtables_target *xt_t,
667                                 char *match_name, struct xtables_match *xt_m)
668 {
669         GList *chain_head;
670         struct ipt_entry *new_entry;
671         int builtin = -1, ret;
672
673         chain_head = find_chain_head(table, chain_name);
674         if (chain_head == NULL)
675                 return -EINVAL;
676
677         new_entry = prepare_rule_inclusion(table, ip, chain_name,
678                         target_name, xt_t, match_name, xt_m, &builtin);
679         if (new_entry == NULL)
680                 return -EINVAL;
681
682         ret = connman_add_entry(table, new_entry, chain_head->next, builtin);
683         if (ret < 0)
684                 g_free(new_entry);
685
686         return ret;
687 }
688
689 static struct ipt_replace *
690 connman_iptables_blob(struct connman_iptables *table)
691 {
692         struct ipt_replace *r;
693         GList *list;
694         struct connman_iptables_entry *e;
695         unsigned char *entry_index;
696
697         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
698         if (r == NULL)
699                 return NULL;
700
701         memset(r, 0, sizeof(*r) + table->size);
702
703         r->counters = g_try_malloc0(sizeof(struct xt_counters)
704                                 * table->old_entries);
705         if (r->counters == NULL) {
706                 g_free(r);
707                 return NULL;
708         }
709
710         strcpy(r->name, table->info->name);
711         r->num_entries = table->num_entries;
712         r->size = table->size;
713
714         r->num_counters = table->old_entries;
715         r->valid_hooks  = table->info->valid_hooks;
716
717         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
718         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
719
720         entry_index = (unsigned char *)r->entries;
721         for (list = table->entries; list; list = list->next) {
722                 e = list->data;
723
724                 memcpy(entry_index, e->entry, e->entry->next_offset);
725                 entry_index += e->entry->next_offset;
726         }
727
728         return r;
729 }
730
731 static void dump_target(struct connman_iptables *table,
732                                 struct ipt_entry *entry)
733
734 {
735         struct xtables_target *xt_t;
736         struct xt_entry_target *target;
737
738         target = ipt_get_target(entry);
739
740         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
741                 struct xt_standard_target *t;
742
743                 t = (struct xt_standard_target *)target;
744
745                 switch (t->verdict) {
746                 case XT_RETURN:
747                         printf("\ttarget RETURN\n");
748                         break;
749
750                 case -NF_ACCEPT - 1:
751                         printf("\ttarget ACCEPT\n");
752                         break;
753
754                 case -NF_DROP - 1:
755                         printf("\ttarget DROP\n");
756                         break;
757
758                 case -NF_QUEUE - 1:
759                         printf("\ttarget QUEUE\n");
760                         break;
761
762                 case -NF_STOP - 1:
763                         printf("\ttarget STOP\n");
764                         break;
765
766                 default:
767                         printf("\tJUMP @%p (0x%x)\n",
768                                 (char*)table->blob_entries->entrytable +
769                                 t->verdict, t->verdict);
770                         break;
771                 }
772
773                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
774                                                 XTF_LOAD_MUST_SUCCEED);
775
776                 if(xt_t->print != NULL)
777                         xt_t->print(NULL, target, 1);
778         } else {
779                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
780                 if (xt_t == NULL) {
781                         printf("\ttarget %s\n", target->u.user.name);
782                         return;
783                 }
784
785                 if(xt_t->print != NULL) {
786                         printf("\ttarget ");
787                         xt_t->print(NULL, target, 1);
788                         printf("\n");
789                 }
790         }
791 }
792
793 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
794 {
795         struct xtables_match *xt_m;
796         struct xt_entry_match *match;
797
798         if (entry->elems == (unsigned char *)entry + entry->target_offset)
799                 return;
800
801         match = (struct xt_entry_match *) entry->elems;
802
803         if (!strlen(match->u.user.name))
804                 return;
805
806         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
807         if (xt_m == NULL)
808                 goto out;
809
810         if(xt_m->print != NULL) {
811                 printf("\tmatch ");
812                 xt_m->print(NULL, match, 1);
813                 printf("\n");
814
815                 return;
816         }
817
818 out:
819         printf("\tmatch %s\n", match->u.user.name);
820
821 }
822
823 static int connman_iptables_dump_entry(struct ipt_entry *entry,
824                                         struct connman_iptables *table)
825 {
826         struct xt_entry_target *target;
827         unsigned int offset;
828         int builtin;
829
830         offset = (char *)entry - (char *)table->blob_entries->entrytable;
831         target = ipt_get_target(entry);
832         builtin = is_hook_entry(table, entry);
833
834         if (entry_to_offset(table, entry) + entry->next_offset ==
835                                         table->blob_entries->size) {
836                 printf("End of CHAIN 0x%x\n", offset);
837                 return 0;
838         }
839
840         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
841                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
842                         target->data, entry, entry->elems,
843                         (char *)entry + entry->target_offset,
844                                 entry->next_offset);
845
846                 return 0;
847         } else if (builtin >= 0) {
848                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
849                         hooknames[builtin], entry, entry->elems,
850                         (char *)entry + entry->target_offset,
851                                 entry->next_offset);
852         } else {
853                 printf("RULE %p  match %p  target %p  size %d\n", entry,
854                         entry->elems,
855                         (char *)entry + entry->target_offset,
856                                 entry->next_offset);
857         }
858
859         dump_match(table, entry);
860         dump_target(table, entry);
861
862         return 0;
863 }
864
865 static void connman_iptables_dump_hook(struct connman_iptables *table)
866 {
867         int i;
868         printf("hooks: \n");
869         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
870                 if ((table->info->valid_hooks & (1 << i)))
871                         printf("%s entry %p underflow %p (%#x)\n",
872                                 hooknames[i],
873                                 table->blob_entries->entrytable +
874                                                 table->info->hook_entry[i],
875                                 table->blob_entries->entrytable +
876                                                 table->info->underflow[i],
877                                         table->info->underflow[i]);
878         }
879 }
880
881 static void connman_iptables_dump(struct connman_iptables *table)
882 {
883         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
884                 table->info->name,
885                 table->info->valid_hooks, table->info->num_entries,
886                 table->info->size);
887
888         connman_iptables_dump_hook(table);
889
890         ENTRY_ITERATE(table->blob_entries->entrytable,
891                         table->blob_entries->size,
892                         connman_iptables_dump_entry, table);
893
894 }
895
896 static int connman_iptables_get_entries(struct connman_iptables *table)
897 {
898         socklen_t entry_size;
899
900         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
901
902         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
903                                 table->blob_entries, &entry_size);
904 }
905
906 static int connman_iptables_replace(struct connman_iptables *table,
907                                         struct ipt_replace *r)
908 {
909         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
910                          sizeof(*r) + r->size);
911 }
912
913 static void connman_iptables_cleanup(struct connman_iptables *table)
914 {
915         GList *list;
916         struct connman_iptables_entry *entry;
917
918         close(table->ipt_sock);
919
920         for (list = table->entries; list; list = list->next) {
921                 entry = list->data;
922
923                 g_free(entry->entry);
924         }
925
926         g_free(table->info);
927         g_free(table->blob_entries);
928         g_free(table);
929
930         xtables_free_opts(1);
931 }
932
933 static int connman_iptables_commit(struct connman_iptables *table)
934 {
935         struct ipt_replace *repl;
936
937         repl = connman_iptables_blob(table);
938
939         return connman_iptables_replace(table, repl);
940 }
941
942 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
943 {
944         struct ipt_entry *new_entry;
945         int builtin;
946
947         new_entry = g_try_malloc0(entry->next_offset);
948         if (new_entry == NULL)
949                 return -ENOMEM;
950
951         memcpy(new_entry, entry, entry->next_offset);
952
953         builtin = is_hook_entry(table, entry);
954
955         return connman_add_entry(table, new_entry, NULL, builtin);
956 }
957
958 static struct connman_iptables *connman_iptables_init(const char *table_name)
959 {
960         struct connman_iptables *table;
961         socklen_t s;
962
963         table =  g_try_new0(struct connman_iptables, 1);
964         if (table == NULL)
965                 return NULL;
966
967         table->info =  g_try_new0(struct ipt_getinfo, 1);
968         if (table->info == NULL)
969                 goto err;
970
971         table->ipt_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
972         if (table->ipt_sock < 0)
973                 goto err;
974
975         s = sizeof(*table->info);
976         strcpy(table->info->name, table_name);
977         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
978                                                 table->info, &s) < 0)
979                 goto err;
980
981         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
982                                                 table->info->size);
983         if (table->blob_entries == NULL)
984                 goto err;
985
986         strcpy(table->blob_entries->name, table_name);
987         table->blob_entries->size = table->info->size;
988
989         if (connman_iptables_get_entries(table) < 0)
990                 goto err;
991
992         table->num_entries = 0;
993         table->old_entries = table->info->num_entries;
994         table->size = 0;
995
996         memcpy(table->underflow, table->info->underflow,
997                                 sizeof(table->info->underflow));
998         memcpy(table->hook_entry, table->info->hook_entry,
999                                 sizeof(table->info->hook_entry));
1000
1001         ENTRY_ITERATE(table->blob_entries->entrytable,
1002                         table->blob_entries->size,
1003                                 add_entry, table);
1004
1005
1006         return table;
1007
1008 err:
1009
1010         connman_iptables_cleanup(table);
1011
1012         return NULL;
1013 }
1014
1015 static struct xtables_target *prepare_target(struct connman_iptables *table,
1016                                                         char *target_name)
1017 {
1018         struct xtables_target *xt_t = NULL;
1019         gboolean is_builtin, is_user_defined;
1020         GList *chain_head = NULL;
1021         size_t target_size;
1022
1023         is_builtin = FALSE;
1024         is_user_defined = FALSE;
1025
1026         if (is_builtin_target(target_name))
1027                 is_builtin = TRUE;
1028         else {
1029                 chain_head = find_chain_head(table, target_name);
1030                 if (chain_head != NULL && chain_head->next != NULL)
1031                         is_user_defined = TRUE;
1032         }
1033
1034         if (is_builtin || is_user_defined)
1035                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1036                                                 XTF_LOAD_MUST_SUCCEED);
1037         else
1038                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1039
1040         if (xt_t == NULL)
1041                 return NULL;
1042
1043         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1044
1045         xt_t->t = g_try_malloc0(target_size);
1046         if (xt_t->t == NULL)
1047                 return NULL;
1048
1049         xt_t->t->u.target_size = target_size;
1050
1051         if (is_builtin || is_user_defined) {
1052                 struct xt_standard_target *target;
1053
1054                 target = (struct xt_standard_target *)(xt_t->t);
1055                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1056
1057                 if (is_builtin == TRUE)
1058                         target->verdict = target_to_verdict(target_name);
1059                 else if (is_user_defined == TRUE) {
1060                         struct connman_iptables_entry *target_rule;
1061
1062                         if (chain_head == NULL) {
1063                                 g_free(xt_t->t);
1064                                 return NULL;
1065                         }
1066
1067                         target_rule = chain_head->next->data;
1068                         target->verdict = target_rule->offset;
1069                 }
1070         } else {
1071                 strcpy(xt_t->t->u.user.name, target_name);
1072                 xt_t->t->u.user.revision = xt_t->revision;
1073                 if (xt_t->init != NULL)
1074                         xt_t->init(xt_t->t);
1075         }
1076
1077         return xt_t;
1078 }
1079
1080 static struct option connman_iptables_opts[] = {
1081         {.name = "append",        .has_arg = 1, .val = 'A'},
1082         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1083         {.name = "insert",        .has_arg = 1, .val = 'I'},
1084         {.name = "list",          .has_arg = 2, .val = 'L'},
1085         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1086         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1087         {.name = "destination",   .has_arg = 1, .val = 'd'},
1088         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1089         {.name = "jump",          .has_arg = 1, .val = 'j'},
1090         {.name = "match",         .has_arg = 1, .val = 'm'},
1091         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1092         {.name = "source",        .has_arg = 1, .val = 's'},
1093         {.name = "table",         .has_arg = 1, .val = 't'},
1094         {NULL},
1095 };
1096
1097 struct xtables_globals connman_iptables_globals = {
1098         .option_offset = 0,
1099         .opts = connman_iptables_opts,
1100         .orig_opts = connman_iptables_opts,
1101 };
1102
1103 int main(int argc, char *argv[])
1104 {
1105         struct connman_iptables *table;
1106         struct xtables_match *xt_m;
1107         struct xtables_target *xt_t;
1108         struct ipt_ip ip;
1109         char *table_name, *chain, *new_chain, *match_name, *target_name;
1110         char *delete_chain, *flush_chain;
1111         int c, in_len, out_len;
1112         size_t size;
1113         gboolean dump, invert, delete, insert;
1114         struct in_addr src, dst;
1115
1116         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1117
1118         dump = FALSE;
1119         invert = FALSE;
1120         delete = FALSE;
1121         insert = FALSE;
1122         table_name = chain = new_chain = match_name = target_name = NULL;
1123         delete_chain = flush_chain = NULL;
1124         memset(&ip, 0, sizeof(struct ipt_ip));
1125         table = NULL;
1126         xt_m = NULL;
1127         xt_t = NULL;
1128
1129         while ((c = getopt_long(argc, argv, "-A:F:I:L::N:X:d:i:j:m:o:s:t:",
1130                                 connman_iptables_globals.opts, NULL)) != -1) {
1131                 switch (c) {
1132                 case 'A':
1133                         /* It is either -A, -D or -I at once */
1134                         if (chain)
1135                                 goto out;
1136
1137                         chain = optarg;
1138                         break;
1139
1140                 case 'F':
1141                         flush_chain = optarg;
1142                         break;
1143
1144                 case 'I':
1145                         /* It is either -A, -D or -I at once */
1146                         if (chain)
1147                                 goto out;
1148
1149                         chain = optarg;
1150                         insert = TRUE;
1151                         break;
1152
1153                 case 'L':
1154                         dump = true;
1155                         break;
1156
1157                 case 'N':
1158                         new_chain = optarg;
1159                         break;
1160
1161                 case 'X':
1162                         delete = true;
1163                         delete_chain = optarg;
1164                         break;
1165
1166                 case 'd':
1167                         if (!inet_pton(AF_INET, optarg, &dst))
1168                                 break;
1169
1170                         ip.dst = dst;
1171                         inet_pton(AF_INET, "255.255.255.255", &ip.dmsk);
1172
1173                         if (invert)
1174                                 ip.invflags |= IPT_INV_DSTIP;
1175
1176                         break;
1177
1178                 case 'i':
1179                         in_len = strlen(optarg);
1180
1181                         if (in_len + 1 > IFNAMSIZ)
1182                                 break;
1183
1184                         strcpy(ip.iniface, optarg);
1185                         memset(ip.iniface_mask, 0xff, in_len + 1);
1186
1187                         if (invert)
1188                                 ip.invflags |= IPT_INV_VIA_IN;
1189
1190                         break;
1191
1192                 case 'j':
1193                         target_name = optarg;
1194                         break;
1195
1196                 case 'm':
1197                         match_name = optarg;
1198
1199                         xt_m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, NULL);
1200                         size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1201                         xt_m->m = g_try_malloc0(size);
1202                         if (xt_m == NULL)
1203                                 goto out;
1204                         xt_m->m->u.match_size = size;
1205                         strcpy(xt_m->m->u.user.name, xt_m->name);
1206                         xt_m->m->u.user.revision = xt_m->revision;
1207                         if (xt_m->init != NULL)
1208                                 xt_m->init(xt_m->m);
1209                         if (xt_m != xt_m->next) {
1210                                 connman_iptables_globals.opts =
1211                                         xtables_merge_options(
1212 #if XTABLES_VERSION_CODE > 5
1213                                                 connman_iptables_globals.orig_opts,
1214 #endif
1215                                                 connman_iptables_globals.opts,
1216                                                 xt_m->extra_opts,
1217                                                 &xt_m->option_offset);
1218                                 if (connman_iptables_globals.opts == NULL)
1219                                         goto out;
1220                         }
1221
1222                         break;
1223
1224                 case 'o':
1225                         out_len = strlen(optarg);
1226
1227                         if (out_len + 1 > IFNAMSIZ)
1228                                 break;
1229
1230                         strcpy(ip.outiface, optarg);
1231                         memset(ip.outiface_mask, 0xff, out_len + 1);
1232
1233                         if (invert)
1234                                 ip.invflags |= IPT_INV_VIA_OUT;
1235
1236                         break;
1237
1238                 case 's':
1239                         if (!inet_pton(AF_INET, optarg, &src))
1240                                 break;
1241
1242                         ip.src = src;
1243                         inet_pton(AF_INET, "255.255.255.255", &ip.smsk);
1244
1245                         if (invert)
1246                                 ip.invflags |= IPT_INV_SRCIP;
1247
1248                         break;
1249
1250                 case 't':
1251                         table_name = optarg;
1252                         break;
1253
1254                 case 1:
1255                         if (optarg[0] == '!' && optarg[1] == '\0') {
1256                                 if (invert)
1257                                         printf("Consecutive ! not allowed\n");
1258
1259                                 invert = TRUE;
1260                                 optarg[0] = '\0';
1261                                 continue;
1262                         }
1263
1264                         printf("Invalid option\n");
1265
1266                         return -1;
1267
1268                 default:
1269                         if (xt_t == NULL || xt_t->parse == NULL ||
1270                             !xt_t->parse(c - xt_t->option_offset, argv, invert,
1271                                         &xt_t->tflags, NULL, &xt_t->t)) {
1272                                 if (xt_m == NULL || xt_m->parse == NULL)
1273                                         break;
1274
1275                                 xt_m->parse(c - xt_m->option_offset, argv,
1276                                         invert, &xt_m->mflags, NULL, &xt_m->m);
1277                         }
1278
1279                         break;
1280                 }
1281         }
1282
1283         if (table_name == NULL)
1284                 table_name = "filter";
1285
1286         table = connman_iptables_init(table_name);
1287         if (table == NULL)
1288                 return -1;
1289
1290         if (delete) {
1291                 if (delete_chain == NULL)
1292                         goto out;
1293
1294                 printf("Delete chain %s\n", delete_chain);
1295
1296                 connman_iptables_delete_chain(table, delete_chain);
1297
1298                 goto commit;
1299         }
1300
1301         if (flush_chain) {
1302                 printf("Flush chain %s\n", flush_chain);
1303
1304                 connman_iptables_flush_chain(table, flush_chain);
1305
1306                 goto commit;
1307         }
1308
1309         if (dump) {
1310                 connman_iptables_dump(table);
1311
1312                 return 0;
1313         }
1314
1315         if (chain && new_chain)
1316                 return -1;
1317
1318         if (new_chain) {
1319                 printf("New chain %s\n", new_chain);
1320
1321                 connman_iptables_add_chain(table, new_chain);
1322
1323                 goto commit;
1324         }
1325
1326         if (chain) {
1327                 xt_t = prepare_target(table, target_name);
1328                 if (xt_t == NULL)
1329                         goto out;
1330
1331                 connman_iptables_globals.opts =
1332                         xtables_merge_options(
1333 #if XTABLES_VERSION_CODE > 5
1334                                         connman_iptables_globals.orig_opts,
1335 #endif
1336                                         connman_iptables_globals.opts,
1337                                         xt_t->extra_opts,
1338                                         &xt_t->option_offset);
1339                 if (connman_iptables_globals.opts == NULL)
1340                         goto out;
1341
1342                 if (insert == TRUE) {
1343                         printf("Inserting %s to %s (match %s)\n", target_name,
1344                                         chain, match_name);
1345
1346                         connman_iptables_insert_rule(table, &ip, chain,
1347                                         target_name, xt_t, match_name, xt_m);
1348                 } else {
1349                         printf("Appending %s to %s (match %s)\n", target_name,
1350                                         chain, match_name);
1351
1352                         connman_iptables_append_rule(table, &ip, chain,
1353                                 target_name, xt_t, match_name, xt_m);
1354                 }
1355         }
1356
1357 commit:
1358
1359         connman_iptables_commit(table);
1360
1361 out:
1362         connman_iptables_cleanup(table);
1363
1364         if (xt_t)
1365                 g_free(xt_t->t);
1366
1367         if (xt_m)
1368                 g_free(xt_m->m);
1369
1370         return 0;
1371 }