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