device: Combine two if statements with identical outcome
[framework/connectivity/connman.git] / tools / iptables-test.c
1 /*
2  *  Connection Manager
3  *
4  *  Copyright (C) 2007-2012  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 #define XT_OPTION_OFFSET_SCALE 256
50
51 /* fn returns 0 to continue iteration */
52 #define _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
53 ({                                                              \
54         unsigned int __i;                                       \
55         int __n;                                                \
56         int __ret = 0;                                          \
57         type *__entry;                                          \
58                                                                 \
59         for (__i = 0, __n = 0; __i < (size);                    \
60              __i += __entry->next_offset, __n++) {              \
61                 __entry = (void *)(entries) + __i;              \
62                 if (__n < n)                                    \
63                         continue;                               \
64                                                                 \
65                 __ret = fn(__entry,  ## args);                  \
66                 if (__ret != 0)                                 \
67                         break;                                  \
68         }                                                       \
69         __ret;                                                  \
70 })
71
72 /* fn returns 0 to continue iteration */
73 #define _XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
74         _XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
75
76 #define ENTRY_ITERATE(entries, size, fn, args...) \
77         _XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
78
79 #define MIN_ALIGN (__alignof__(struct ipt_entry))
80
81 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
82
83 struct error_target {
84         struct xt_entry_target t;
85         char error[IPT_TABLE_MAXNAMELEN];
86 };
87
88 struct connman_iptables_entry {
89         int offset;
90         int builtin;
91
92         struct ipt_entry *entry;
93 };
94
95 struct connman_iptables {
96         int ipt_sock;
97
98         struct ipt_getinfo *info;
99         struct ipt_get_entries *blob_entries;
100
101         unsigned int num_entries;
102         unsigned int old_entries;
103         unsigned int size;
104
105         unsigned int underflow[NF_INET_NUMHOOKS];
106         unsigned int hook_entry[NF_INET_NUMHOOKS];
107
108         GList *entries;
109 };
110
111
112 static struct ipt_entry *get_entry(struct connman_iptables *table,
113                                         unsigned int offset)
114 {
115         return (struct ipt_entry *)((char *)table->blob_entries->entrytable +
116                                                                         offset);
117 }
118
119 static int is_hook_entry(struct connman_iptables *table,
120                                 struct ipt_entry *entry)
121 {
122         unsigned int i;
123
124         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
125                 if ((table->info->valid_hooks & (1 << i))
126                 && get_entry(table, table->info->hook_entry[i]) == entry)
127                         return i;
128         }
129
130         return -1;
131 }
132
133 static unsigned long entry_to_offset(struct connman_iptables *table,
134                                         struct ipt_entry *entry)
135 {
136         return (void *)entry - (void *)table->blob_entries->entrytable;
137 }
138
139 static int target_to_verdict(char *target_name)
140 {
141         if (!strcmp(target_name, LABEL_ACCEPT))
142                 return -NF_ACCEPT - 1;
143
144         if (!strcmp(target_name, LABEL_DROP))
145                 return -NF_DROP - 1;
146
147         if (!strcmp(target_name, LABEL_QUEUE))
148                 return -NF_QUEUE - 1;
149
150         if (!strcmp(target_name, LABEL_RETURN))
151                 return XT_RETURN;
152
153         return 0;
154 }
155
156 static gboolean is_builtin_target(char *target_name)
157 {
158         if (!strcmp(target_name, LABEL_ACCEPT) ||
159                 !strcmp(target_name, LABEL_DROP) ||
160                 !strcmp(target_name, LABEL_QUEUE) ||
161                 !strcmp(target_name, LABEL_RETURN))
162                 return TRUE;
163
164         return FALSE;
165 }
166
167 static gboolean is_jump(struct connman_iptables_entry *e)
168 {
169         struct xt_entry_target *target;
170
171         target = ipt_get_target(e->entry);
172
173         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
174                 struct xt_standard_target *t;
175
176                 t = (struct xt_standard_target *)target;
177
178                 switch (t->verdict) {
179                 case XT_RETURN:
180                 case -NF_ACCEPT - 1:
181                 case -NF_DROP - 1:
182                 case -NF_QUEUE - 1:
183                 case -NF_STOP - 1:
184                         return false;
185
186                 default:
187                         return true;
188                 }
189         }
190
191         return false;
192 }
193
194 static gboolean is_chain(struct connman_iptables *table,
195                                 struct connman_iptables_entry *e)
196 {
197         struct ipt_entry *entry;
198         struct xt_entry_target *target;
199
200         entry = e->entry;
201         if (e->builtin >= 0)
202                 return TRUE;
203
204         target = ipt_get_target(entry);
205         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET))
206                 return TRUE;
207
208         return FALSE;
209 }
210
211 static GList *find_chain_head(struct connman_iptables *table,
212                                 char *chain_name)
213 {
214         GList *list;
215         struct connman_iptables_entry *head;
216         struct ipt_entry *entry;
217         struct xt_entry_target *target;
218         int builtin;
219
220         for (list = table->entries; list; list = list->next) {
221                 head = list->data;
222                 entry = head->entry;
223
224                 /* Buit-in chain */
225                 builtin = head->builtin;
226                 if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
227                         break;
228
229                 /* User defined chain */
230                 target = ipt_get_target(entry);
231                 if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
232                     !strcmp((char *)target->data, chain_name))
233                         break;
234         }
235
236         return list;
237 }
238
239 static GList *find_chain_tail(struct connman_iptables *table,
240                                 char *chain_name)
241 {
242         GList *chain_head, *list;
243         struct connman_iptables_entry *tail;
244
245         chain_head = find_chain_head(table, chain_name);
246         if (chain_head == NULL)
247                 return NULL;
248
249         /* Then we look for the next chain */
250         for (list = chain_head->next; list; list = list->next) {
251                 tail = list->data;
252
253                 if (is_chain(table, tail))
254                         return list;
255         }
256
257         /* Nothing found, we return the table end */
258         return g_list_last(table->entries);
259 }
260
261 static void update_offsets(struct connman_iptables *table)
262 {
263         GList *list, *prev;
264         struct connman_iptables_entry *entry, *prev_entry;
265
266         for (list = table->entries; list; list = list->next) {
267                 entry = list->data;
268
269                 if (list == table->entries) {
270                         entry->offset = 0;
271
272                         continue;
273                 }
274
275                 prev = list->prev;
276                 prev_entry = prev->data;
277
278                 entry->offset = prev_entry->offset +
279                                         prev_entry->entry->next_offset;
280         }
281 }
282
283 static void update_targets_reference(struct connman_iptables *table,
284                                 struct connman_iptables_entry *entry_before,
285                                 struct connman_iptables_entry *modified_entry,
286                                 gboolean is_removing)
287 {
288         struct connman_iptables_entry *tmp;
289         struct xt_standard_target *t;
290         GList *list;
291         int offset;
292
293         offset = modified_entry->entry->next_offset;
294
295         for (list = table->entries; list; list = list->next) {
296                 tmp = list->data;
297
298                 if (!is_jump(tmp))
299                         continue;
300
301                 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
302
303                 if (is_removing == TRUE) {
304                         if (t->verdict >= entry_before->offset)
305                                 t->verdict -= offset;
306                 } else {
307                         if (t->verdict > entry_before->offset)
308                                 t->verdict += offset;
309                 }
310         }
311 }
312
313 static int connman_add_entry(struct connman_iptables *table,
314                                 struct ipt_entry *entry, GList *before,
315                                         int builtin)
316 {
317         struct connman_iptables_entry *e, *entry_before;
318
319         if (table == NULL)
320                 return -1;
321
322         e = g_try_malloc0(sizeof(struct connman_iptables_entry));
323         if (e == NULL)
324                 return -1;
325
326         e->entry = entry;
327         e->builtin = builtin;
328
329         table->entries = g_list_insert_before(table->entries, before, e);
330         table->num_entries++;
331         table->size += entry->next_offset;
332
333         if (before == NULL) {
334                 e->offset = table->size - entry->next_offset;
335
336                 return 0;
337         }
338
339         entry_before = before->data;
340
341         /*
342          * We've just appended/insterted a new entry. All references
343          * should be bumped accordingly.
344          */
345         update_targets_reference(table, entry_before, e, FALSE);
346
347         update_offsets(table);
348
349         return 0;
350 }
351
352 static int remove_table_entry(struct connman_iptables *table,
353                                         struct connman_iptables_entry *entry)
354 {
355         int removed = 0;
356
357         table->num_entries--;
358         table->size -= entry->entry->next_offset;
359         removed = entry->entry->next_offset;
360
361         g_free(entry->entry);
362
363         table->entries = g_list_remove(table->entries, entry);
364
365         return removed;
366 }
367
368 static int connman_iptables_flush_chain(struct connman_iptables *table,
369                                                 char *name)
370 {
371         GList *chain_head, *chain_tail, *list, *next;
372         struct connman_iptables_entry *entry;
373         int builtin, removed = 0;
374
375         chain_head = find_chain_head(table, name);
376         if (chain_head == NULL)
377                 return -EINVAL;
378
379         chain_tail = find_chain_tail(table, name);
380         if (chain_tail == NULL)
381                 return -EINVAL;
382
383         entry = chain_head->data;
384         builtin = entry->builtin;
385
386         if (builtin >= 0)
387                 list = chain_head;
388         else
389                 list = chain_head->next;
390
391         if (list == chain_tail->prev)
392                 return 0;
393
394         while (list != chain_tail->prev) {
395                 entry = list->data;
396                 next = g_list_next(list);
397
398                 removed += remove_table_entry(table, entry);
399
400                 list = next;
401         }
402
403         if (builtin >= 0) {
404                 struct connman_iptables_entry *e;
405
406                 entry = list->data;
407
408                 entry->builtin = builtin;
409
410                 table->underflow[builtin] -= removed;
411
412                 for (list = chain_tail; list; list = list->next) {
413                         e = list->data;
414
415                         builtin = e->builtin;
416                         if (builtin < 0)
417                                 continue;
418
419                         table->hook_entry[builtin] -= removed;
420                         table->underflow[builtin] -= removed;
421                 }
422         }
423
424         update_offsets(table);
425
426         return 0;
427 }
428
429 static int connman_iptables_delete_chain(struct connman_iptables *table,
430                                                 char *name)
431 {
432         GList *chain_head, *chain_tail;
433         struct connman_iptables_entry *entry;
434
435         chain_head = find_chain_head(table, name);
436         if (chain_head == NULL)
437                 return -EINVAL;
438
439         entry = chain_head->data;
440
441         /* We cannot remove builtin chain */
442         if (entry->builtin >= 0)
443                 return -EINVAL;
444
445         chain_tail = find_chain_tail(table, name);
446         if (chain_tail == NULL)
447                 return -EINVAL;
448
449         /* Chain must be flushed */
450         if (chain_head->next != chain_tail->prev)
451                 return -EINVAL;
452
453         remove_table_entry(table, entry);
454
455         entry = chain_tail->prev->data;
456         remove_table_entry(table, entry);
457
458         update_offsets(table);
459
460         return 0;
461 }
462
463 static int connman_iptables_add_chain(struct connman_iptables *table,
464                                         char *name)
465 {
466         GList *last;
467         struct ipt_entry *entry_head;
468         struct ipt_entry *entry_return;
469         struct error_target *error;
470         struct ipt_standard_target *standard;
471         u_int16_t entry_head_size, entry_return_size;
472
473         last = g_list_last(table->entries);
474
475         /*
476          * An empty chain is composed of:
477          * - A head entry, with no match and an error target.
478          *   The error target data is the chain name.
479          * - A tail entry, with no match and a standard target.
480          *   The standard target verdict is XT_RETURN (return to the
481          *   caller).
482          */
483
484         /* head entry */
485         entry_head_size = sizeof(struct ipt_entry) +
486                                 sizeof(struct error_target);
487         entry_head = g_try_malloc0(entry_head_size);
488         if (entry_head == NULL)
489                 goto err_head;
490
491         memset(entry_head, 0, entry_head_size);
492
493         entry_head->target_offset = sizeof(struct ipt_entry);
494         entry_head->next_offset = entry_head_size;
495
496         error = (struct error_target *) entry_head->elems;
497         strcpy(error->t.u.user.name, IPT_ERROR_TARGET);
498         error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
499         strcpy(error->error, name);
500
501         if (connman_add_entry(table, entry_head, last, -1) < 0)
502                 goto err_head;
503
504         /* tail entry */
505         entry_return_size = sizeof(struct ipt_entry) +
506                                 sizeof(struct ipt_standard_target);
507         entry_return = g_try_malloc0(entry_return_size);
508         if (entry_return == NULL)
509                 goto err;
510
511         memset(entry_return, 0, entry_return_size);
512
513         entry_return->target_offset = sizeof(struct ipt_entry);
514         entry_return->next_offset = entry_return_size;
515
516         standard = (struct ipt_standard_target *) entry_return->elems;
517         standard->target.u.user.target_size =
518                                 ALIGN(sizeof(struct ipt_standard_target));
519         standard->verdict = XT_RETURN;
520
521         if (connman_add_entry(table, entry_return, last, -1) < 0)
522                 goto err;
523
524         return 0;
525
526 err:
527         g_free(entry_return);
528 err_head:
529         g_free(entry_head);
530
531         return -ENOMEM;
532 }
533
534 static struct ipt_entry *new_rule(struct ipt_ip *ip,
535                         char *target_name, struct xtables_target *xt_t,
536                         struct xtables_rule_match *xt_rm)
537 {
538         struct xtables_rule_match *tmp_xt_rm;
539         struct ipt_entry *new_entry;
540         size_t match_size, target_size;
541
542         match_size = 0;
543         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL; tmp_xt_rm = tmp_xt_rm->next)
544                 match_size += tmp_xt_rm->match->m->u.match_size;
545
546         if (xt_t)
547                 target_size = ALIGN(xt_t->t->u.target_size);
548         else
549                 target_size = ALIGN(sizeof(struct xt_standard_target));
550
551         new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
552                                                                 match_size);
553         if (new_entry == NULL)
554                 return NULL;
555
556         memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
557
558         new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
559         new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
560                                                                 match_size;
561
562         match_size = 0;
563         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
564                                 tmp_xt_rm = tmp_xt_rm->next) {
565                 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
566                                         tmp_xt_rm->match->m->u.match_size);
567                 match_size += tmp_xt_rm->match->m->u.match_size;
568         }
569
570         if (xt_t) {
571                 struct xt_entry_target *entry_target;
572
573                 entry_target = ipt_get_target(new_entry);
574                 memcpy(entry_target, xt_t->t, target_size);
575         }
576
577         return new_entry;
578 }
579
580 static void update_hooks(struct connman_iptables *table, GList *chain_head,
581                                 struct ipt_entry *entry)
582 {
583         GList *list;
584         struct connman_iptables_entry *head, *e;
585         int builtin;
586
587         if (chain_head == NULL)
588                 return;
589
590         head = chain_head->data;
591
592         builtin = head->builtin;
593         if (builtin < 0)
594                 return;
595
596         table->underflow[builtin] += entry->next_offset;
597
598         for (list = chain_head->next; list; list = list->next) {
599                 e = list->data;
600
601                 builtin = e->builtin;
602                 if (builtin < 0)
603                         continue;
604
605                 table->hook_entry[builtin] += entry->next_offset;
606                 table->underflow[builtin] += entry->next_offset;
607         }
608 }
609
610 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
611                                 struct ipt_ip *ip, char *chain_name,
612                                 char *target_name, struct xtables_target *xt_t,
613                                 int *builtin, struct xtables_rule_match *xt_rm)
614 {
615         GList *chain_tail, *chain_head;
616         struct ipt_entry *new_entry;
617         struct connman_iptables_entry *head;
618
619         chain_head = find_chain_head(table, chain_name);
620         if (chain_head == NULL)
621                 return NULL;
622
623         chain_tail = find_chain_tail(table, chain_name);
624         if (chain_tail == NULL)
625                 return NULL;
626
627         new_entry = new_rule(ip, target_name, xt_t, xt_rm);
628         if (new_entry == NULL)
629                 return NULL;
630
631         update_hooks(table, chain_head, new_entry);
632
633         /*
634          * If the chain is builtin, and does not have any rule,
635          * then the one that we're inserting is becoming the head
636          * and thus needs the builtin flag.
637          */
638         head = chain_head->data;
639         if (head->builtin < 0)
640                 *builtin = -1;
641         else if (chain_head == chain_tail->prev) {
642                 *builtin = head->builtin;
643                 head->builtin = -1;
644         }
645
646         return new_entry;
647 }
648
649 static int connman_iptables_append_rule(struct connman_iptables *table,
650                                 struct ipt_ip *ip, char *chain_name,
651                                 char *target_name, struct xtables_target *xt_t,
652                                 struct xtables_rule_match *xt_rm)
653 {
654         GList *chain_tail;
655         struct ipt_entry *new_entry;
656         int builtin = -1, ret;
657
658         chain_tail = find_chain_tail(table, chain_name);
659         if (chain_tail == NULL)
660                 return -EINVAL;
661
662         new_entry = prepare_rule_inclusion(table, ip, chain_name,
663                                         target_name, xt_t, &builtin, xt_rm);
664         if (new_entry == NULL)
665                 return -EINVAL;
666
667         ret = connman_add_entry(table, new_entry, chain_tail->prev, builtin);
668         if (ret < 0)
669                 g_free(new_entry);
670
671         return ret;
672 }
673
674 static int connman_iptables_insert_rule(struct connman_iptables *table,
675                                 struct ipt_ip *ip, char *chain_name,
676                                 char *target_name, struct xtables_target *xt_t,
677                                 struct xtables_rule_match *xt_rm)
678 {
679         GList *chain_head;
680         struct ipt_entry *new_entry;
681         int builtin = -1, ret;
682
683         chain_head = find_chain_head(table, chain_name);
684         if (chain_head == NULL)
685                 return -EINVAL;
686
687         new_entry = prepare_rule_inclusion(table, ip, chain_name,
688                                         target_name, xt_t, &builtin, xt_rm);
689         if (new_entry == NULL)
690                 return -EINVAL;
691
692         if (builtin == -1)
693                 chain_head = chain_head->next;
694
695         ret = connman_add_entry(table, new_entry, chain_head, builtin);
696         if (ret < 0)
697                 g_free(new_entry);
698
699         return ret;
700 }
701
702 static gboolean is_same_ipt_entry(struct ipt_entry *i_e1,
703                                         struct ipt_entry *i_e2)
704 {
705         if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
706                 return FALSE;
707
708         if (i_e1->target_offset != i_e2->target_offset)
709                 return FALSE;
710
711         if (i_e1->next_offset != i_e2->next_offset)
712                 return FALSE;
713
714         return TRUE;
715 }
716
717 static gboolean is_same_target(struct xt_entry_target *xt_e_t1,
718                                         struct xt_entry_target *xt_e_t2)
719 {
720         if (xt_e_t1 == NULL || xt_e_t2 == NULL)
721                 return FALSE;
722
723         if (strcmp(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
724                 struct xt_standard_target *xt_s_t1;
725                 struct xt_standard_target *xt_s_t2;
726
727                 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
728                 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
729
730                 if (xt_s_t1->verdict != xt_s_t2->verdict)
731                         return FALSE;
732         } else {
733                 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
734                         return FALSE;
735
736                 if (strcmp(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
737                         return FALSE;
738         }
739
740         return TRUE;
741 }
742
743 static gboolean is_same_match(struct xt_entry_match *xt_e_m1,
744                                 struct xt_entry_match *xt_e_m2)
745 {
746         if (xt_e_m1 == NULL || xt_e_m2 == NULL)
747                 return FALSE;
748
749         if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
750                 return FALSE;
751
752         if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
753                 return FALSE;
754
755         if (strcmp(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
756                 return FALSE;
757
758         return TRUE;
759 }
760
761 static GList *find_existing_rule(struct connman_iptables *table,
762                                 struct ipt_ip *ip, char *chain_name,
763                                 char *target_name, struct xtables_target *xt_t,
764                                 struct xtables_match *xt_m,
765                                 struct xtables_rule_match *xt_rm)
766 {
767         GList *chain_tail, *chain_head, *list;
768         struct xt_entry_target *xt_e_t = NULL;
769         struct xt_entry_match *xt_e_m = NULL;
770         struct connman_iptables_entry *entry;
771         struct ipt_entry *entry_test;
772         int builtin;
773
774         chain_head = find_chain_head(table, chain_name);
775         if (chain_head == NULL)
776                 return NULL;
777
778         chain_tail = find_chain_tail(table, chain_name);
779         if (chain_tail == NULL)
780                 return NULL;
781
782         if (!xt_t && !xt_m)
783                 return NULL;
784
785         entry_test = new_rule(ip, target_name, xt_t, xt_rm);
786         if (entry_test == NULL)
787                 return NULL;
788
789         if (xt_t != NULL)
790                 xt_e_t = ipt_get_target(entry_test);
791         if (xt_m != NULL)
792                 xt_e_m = (struct xt_entry_match *)entry_test->elems;
793
794         entry = chain_head->data;
795         builtin = entry->builtin;
796
797         if (builtin >= 0)
798                 list = chain_head;
799         else
800                 list = chain_head->next;
801
802         for (; list != chain_tail->prev; list = list->next) {
803                 struct connman_iptables_entry *tmp;
804                 struct ipt_entry *tmp_e;
805
806                 tmp = list->data;
807                 tmp_e = tmp->entry;
808
809                 if (is_same_ipt_entry(entry_test, tmp_e) == FALSE)
810                         continue;
811
812                 if (xt_t != NULL) {
813                         struct xt_entry_target *tmp_xt_e_t;
814
815                         tmp_xt_e_t = ipt_get_target(tmp_e);
816
817                         if (!is_same_target(tmp_xt_e_t, xt_e_t))
818                                 continue;
819                 }
820
821                 if (xt_m != NULL) {
822                         struct xt_entry_match *tmp_xt_e_m;
823
824                         tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
825
826                         if (!is_same_match(tmp_xt_e_m, xt_e_m))
827                                 continue;
828                 }
829
830                 break;
831         }
832
833         g_free(entry_test);
834
835         if (list != chain_tail->prev)
836                 return list;
837
838         return NULL;
839 }
840
841 static int connman_iptables_delete_rule(struct connman_iptables *table,
842                                 struct ipt_ip *ip, char *chain_name,
843                                 char *target_name, struct xtables_target *xt_t,
844                                 struct xtables_match *xt_m,
845                                 struct xtables_rule_match *xt_rm)
846 {
847         struct connman_iptables_entry *entry;
848         GList *chain_tail, *list;
849         int builtin, removed;
850
851         removed = 0;
852
853         chain_tail = find_chain_tail(table, chain_name);
854         if (chain_tail == NULL)
855                 return -EINVAL;
856
857         list = find_existing_rule(table, ip, chain_name, target_name,
858                                                         xt_t, xt_m, xt_rm);
859         if (list == NULL)
860                 return -EINVAL;
861
862         entry = list->data;
863
864         if (entry == NULL)
865                 return -EINVAL;
866
867         builtin = entry->builtin;
868
869         /* We have deleted a rule,
870          * all references should be bumped accordingly */
871         if (list->next != NULL)
872                 update_targets_reference(table, list->next->data,
873                                                 list->data, TRUE);
874
875         removed += remove_table_entry(table, entry);
876
877         if (builtin >= 0) {
878                 list = list->next;
879                 if (list) {
880                         entry = list->data;
881                         entry->builtin = builtin;
882                 }
883
884                 table->underflow[builtin] -= removed;
885                 for (list = chain_tail; list; list = list->next) {
886                         entry = list->data;
887
888                         builtin = entry->builtin;
889                         if (builtin < 0)
890                                 continue;
891
892                         table->hook_entry[builtin] -= removed;
893                         table->underflow[builtin] -= removed;
894                 }
895         }
896
897         update_offsets(table);
898
899         return 0;
900 }
901
902 static int connman_iptables_compare_rule(struct connman_iptables *table,
903                                 struct ipt_ip *ip, char *chain_name,
904                                 char *target_name, struct xtables_target *xt_t,
905                                 struct xtables_match *xt_m,
906                                 struct xtables_rule_match *xt_rm)
907 {
908         struct connman_iptables_entry *entry;
909         GList *found;
910
911         found = find_existing_rule(table, ip, chain_name, target_name,
912                                                         xt_t, xt_m, xt_rm);
913         if (found == NULL)
914                 return -EINVAL;
915
916         entry = found->data;
917         if (entry == NULL)
918                 return -EINVAL;
919
920         return 0;
921 }
922
923
924 static int connman_iptables_change_policy(struct connman_iptables *table,
925                                                 char *chain_name, char *policy)
926 {
927         GList *chain_head;
928         struct connman_iptables_entry *entry;
929         struct xt_entry_target *target;
930         struct xt_standard_target *t;
931         int verdict;
932
933         verdict = target_to_verdict(policy);
934         if (verdict == 0)
935                 return -EINVAL;
936
937         chain_head = find_chain_head(table, chain_name);
938         if (chain_head == NULL)
939                 return -EINVAL;
940
941         entry = chain_head->data;
942         if (entry->builtin < 0)
943                 return -EINVAL;
944
945         target = ipt_get_target(entry->entry);
946
947         t = (struct xt_standard_target *)target;
948         t->verdict = verdict;
949
950         return 0;
951 }
952
953 static struct ipt_replace *connman_iptables_blob(struct connman_iptables *table)
954 {
955         struct ipt_replace *r;
956         GList *list;
957         struct connman_iptables_entry *e;
958         unsigned char *entry_index;
959
960         r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
961         if (r == NULL)
962                 return NULL;
963
964         memset(r, 0, sizeof(*r) + table->size);
965
966         r->counters = g_try_malloc0(sizeof(struct xt_counters)
967                                 * table->old_entries);
968         if (r->counters == NULL) {
969                 g_free(r);
970                 return NULL;
971         }
972
973         strcpy(r->name, table->info->name);
974         r->num_entries = table->num_entries;
975         r->size = table->size;
976
977         r->num_counters = table->old_entries;
978         r->valid_hooks  = table->info->valid_hooks;
979
980         memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
981         memcpy(r->underflow, table->underflow, sizeof(table->underflow));
982
983         entry_index = (unsigned char *)r->entries;
984         for (list = table->entries; list; list = list->next) {
985                 e = list->data;
986
987                 memcpy(entry_index, e->entry, e->entry->next_offset);
988                 entry_index += e->entry->next_offset;
989         }
990
991         return r;
992 }
993
994 static void dump_target(struct connman_iptables *table,
995                                 struct ipt_entry *entry)
996
997 {
998         struct xtables_target *xt_t;
999         struct xt_entry_target *target;
1000
1001         target = ipt_get_target(entry);
1002
1003         if (!strcmp(target->u.user.name, IPT_STANDARD_TARGET)) {
1004                 struct xt_standard_target *t;
1005
1006                 t = (struct xt_standard_target *)target;
1007
1008                 switch (t->verdict) {
1009                 case XT_RETURN:
1010                         printf("\ttarget RETURN\n");
1011                         break;
1012
1013                 case -NF_ACCEPT - 1:
1014                         printf("\ttarget ACCEPT\n");
1015                         break;
1016
1017                 case -NF_DROP - 1:
1018                         printf("\ttarget DROP\n");
1019                         break;
1020
1021                 case -NF_QUEUE - 1:
1022                         printf("\ttarget QUEUE\n");
1023                         break;
1024
1025                 case -NF_STOP - 1:
1026                         printf("\ttarget STOP\n");
1027                         break;
1028
1029                 default:
1030                         printf("\tJUMP @%p (0x%x)\n",
1031                                 (char*)table->blob_entries->entrytable +
1032                                 t->verdict, t->verdict);
1033                         break;
1034                 }
1035
1036                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1037                                                 XTF_LOAD_MUST_SUCCEED);
1038
1039                 if(xt_t->print != NULL)
1040                         xt_t->print(NULL, target, 1);
1041         } else {
1042                 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1043                 if (xt_t == NULL) {
1044                         printf("\ttarget %s\n", target->u.user.name);
1045                         return;
1046                 }
1047
1048                 if(xt_t->print != NULL) {
1049                         printf("\ttarget ");
1050                         xt_t->print(NULL, target, 1);
1051                         printf("\n");
1052                 }
1053         }
1054 }
1055
1056 static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
1057 {
1058         struct xtables_match *xt_m;
1059         struct xt_entry_match *match;
1060
1061         if (entry->elems == (unsigned char *)entry + entry->target_offset)
1062                 return;
1063
1064         match = (struct xt_entry_match *) entry->elems;
1065
1066         if (!strlen(match->u.user.name))
1067                 return;
1068
1069         xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1070         if (xt_m == NULL)
1071                 goto out;
1072
1073         if(xt_m->print != NULL) {
1074                 printf("\tmatch ");
1075                 xt_m->print(NULL, match, 1);
1076                 printf("\n");
1077
1078                 return;
1079         }
1080
1081 out:
1082         printf("\tmatch %s\n", match->u.user.name);
1083
1084 }
1085
1086 static int connman_iptables_dump_entry(struct ipt_entry *entry,
1087                                         struct connman_iptables *table)
1088 {
1089         struct xt_entry_target *target;
1090         unsigned int offset;
1091         int builtin;
1092
1093         offset = (char *)entry - (char *)table->blob_entries->entrytable;
1094         target = ipt_get_target(entry);
1095         builtin = is_hook_entry(table, entry);
1096
1097         if (entry_to_offset(table, entry) + entry->next_offset ==
1098                                         table->blob_entries->size) {
1099                 printf("End of CHAIN 0x%x\n", offset);
1100                 return 0;
1101         }
1102
1103         if (!strcmp(target->u.user.name, IPT_ERROR_TARGET)) {
1104                 printf("USER CHAIN (%s) %p  match %p  target %p  size %d\n",
1105                         target->data, entry, entry->elems,
1106                         (char *)entry + entry->target_offset,
1107                                 entry->next_offset);
1108
1109                 return 0;
1110         } else if (builtin >= 0) {
1111                 printf("CHAIN (%s) %p  match %p  target %p  size %d\n",
1112                         hooknames[builtin], entry, entry->elems,
1113                         (char *)entry + entry->target_offset,
1114                                 entry->next_offset);
1115         } else {
1116                 printf("RULE %p  match %p  target %p  size %d\n", entry,
1117                         entry->elems,
1118                         (char *)entry + entry->target_offset,
1119                                 entry->next_offset);
1120         }
1121
1122         dump_match(table, entry);
1123         dump_target(table, entry);
1124
1125         return 0;
1126 }
1127
1128 static void connman_iptables_dump_hook(struct connman_iptables *table)
1129 {
1130         int i;
1131         printf("hooks: \n");
1132         for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1133                 if ((table->info->valid_hooks & (1 << i)))
1134                         printf("%s entry %p underflow %p (%#x)\n",
1135                                 hooknames[i],
1136                                 table->blob_entries->entrytable +
1137                                                 table->info->hook_entry[i],
1138                                 table->blob_entries->entrytable +
1139                                                 table->info->underflow[i],
1140                                         table->info->underflow[i]);
1141         }
1142 }
1143
1144 static void connman_iptables_dump(struct connman_iptables *table)
1145 {
1146         printf("%s valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1147                 table->info->name,
1148                 table->info->valid_hooks, table->info->num_entries,
1149                 table->info->size);
1150
1151         connman_iptables_dump_hook(table);
1152
1153         ENTRY_ITERATE(table->blob_entries->entrytable,
1154                         table->blob_entries->size,
1155                         connman_iptables_dump_entry, table);
1156
1157 }
1158
1159 static int connman_iptables_get_entries(struct connman_iptables *table)
1160 {
1161         socklen_t entry_size;
1162
1163         entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1164
1165         return getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1166                                 table->blob_entries, &entry_size);
1167 }
1168
1169 static int connman_iptables_replace(struct connman_iptables *table,
1170                                         struct ipt_replace *r)
1171 {
1172         return setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1173                          sizeof(*r) + r->size);
1174 }
1175
1176 static void connman_iptables_cleanup(struct connman_iptables *table)
1177 {
1178         GList *list;
1179         struct connman_iptables_entry *entry;
1180
1181         close(table->ipt_sock);
1182
1183         for (list = table->entries; list; list = list->next) {
1184                 entry = list->data;
1185
1186                 g_free(entry->entry);
1187         }
1188
1189         g_free(table->info);
1190         g_free(table->blob_entries);
1191         g_free(table);
1192
1193         xtables_free_opts(1);
1194 }
1195
1196 static int connman_iptables_commit(struct connman_iptables *table)
1197 {
1198         struct ipt_replace *repl;
1199
1200         repl = connman_iptables_blob(table);
1201
1202         return connman_iptables_replace(table, repl);
1203 }
1204
1205 static int add_entry(struct ipt_entry *entry, struct connman_iptables *table)
1206 {
1207         struct ipt_entry *new_entry;
1208         int builtin;
1209
1210         new_entry = g_try_malloc0(entry->next_offset);
1211         if (new_entry == NULL)
1212                 return -ENOMEM;
1213
1214         memcpy(new_entry, entry, entry->next_offset);
1215
1216         builtin = is_hook_entry(table, entry);
1217
1218         return connman_add_entry(table, new_entry, NULL, builtin);
1219 }
1220
1221 static struct connman_iptables *connman_iptables_init(const char *table_name)
1222 {
1223         struct connman_iptables *table = NULL;
1224         char *module = NULL;
1225         socklen_t s;
1226
1227         if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1228                 goto err;
1229
1230         module = g_strconcat("iptable_", table_name, NULL);
1231         if (module == NULL)
1232                 goto err;
1233
1234         if (xtables_insmod(module, NULL, TRUE) != 0)
1235                 goto err;
1236
1237         g_free(module);
1238         module = NULL;
1239
1240         table =  g_try_new0(struct connman_iptables, 1);
1241         if (table == NULL)
1242                 return NULL;
1243
1244         table->info =  g_try_new0(struct ipt_getinfo, 1);
1245         if (table->info == NULL)
1246                 goto err;
1247
1248         table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1249         if (table->ipt_sock < 0)
1250                 goto err;
1251
1252         s = sizeof(*table->info);
1253         strcpy(table->info->name, table_name);
1254         if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1255                                                 table->info, &s) < 0)
1256                 goto err;
1257
1258         table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1259                                                 table->info->size);
1260         if (table->blob_entries == NULL)
1261                 goto err;
1262
1263         strcpy(table->blob_entries->name, table_name);
1264         table->blob_entries->size = table->info->size;
1265
1266         if (connman_iptables_get_entries(table) < 0)
1267                 goto err;
1268
1269         table->num_entries = 0;
1270         table->old_entries = table->info->num_entries;
1271         table->size = 0;
1272
1273         memcpy(table->underflow, table->info->underflow,
1274                                 sizeof(table->info->underflow));
1275         memcpy(table->hook_entry, table->info->hook_entry,
1276                                 sizeof(table->info->hook_entry));
1277
1278         ENTRY_ITERATE(table->blob_entries->entrytable,
1279                         table->blob_entries->size,
1280                                 add_entry, table);
1281
1282         return table;
1283
1284 err:
1285         g_free(module);
1286
1287         connman_iptables_cleanup(table);
1288
1289         return NULL;
1290 }
1291
1292 static struct option connman_iptables_opts[] = {
1293         {.name = "append",        .has_arg = 1, .val = 'A'},
1294         {.name = "compare",       .has_arg = 1, .val = 'C'},
1295         {.name = "delete",        .has_arg = 1, .val = 'D'},
1296         {.name = "flush-chain",   .has_arg = 1, .val = 'F'},
1297         {.name = "insert",        .has_arg = 1, .val = 'I'},
1298         {.name = "list",          .has_arg = 2, .val = 'L'},
1299         {.name = "new-chain",     .has_arg = 1, .val = 'N'},
1300         {.name = "policy",        .has_arg = 1, .val = 'P'},
1301         {.name = "delete-chain",  .has_arg = 1, .val = 'X'},
1302         {.name = "destination",   .has_arg = 1, .val = 'd'},
1303         {.name = "in-interface",  .has_arg = 1, .val = 'i'},
1304         {.name = "jump",          .has_arg = 1, .val = 'j'},
1305         {.name = "match",         .has_arg = 1, .val = 'm'},
1306         {.name = "out-interface", .has_arg = 1, .val = 'o'},
1307         {.name = "source",        .has_arg = 1, .val = 's'},
1308         {.name = "table",         .has_arg = 1, .val = 't'},
1309         {NULL},
1310 };
1311
1312 struct xtables_globals connman_iptables_globals = {
1313         .option_offset = 0,
1314         .opts = connman_iptables_opts,
1315         .orig_opts = connman_iptables_opts,
1316 };
1317
1318 static struct xtables_target *prepare_target(struct connman_iptables *table,
1319                                                         char *target_name)
1320 {
1321         struct xtables_target *xt_t = NULL;
1322         gboolean is_builtin, is_user_defined;
1323         GList *chain_head = NULL;
1324         size_t target_size;
1325
1326         is_builtin = FALSE;
1327         is_user_defined = FALSE;
1328
1329         if (is_builtin_target(target_name))
1330                 is_builtin = TRUE;
1331         else {
1332                 chain_head = find_chain_head(table, target_name);
1333                 if (chain_head != NULL && chain_head->next != NULL)
1334                         is_user_defined = TRUE;
1335         }
1336
1337         if (is_builtin || is_user_defined)
1338                 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1339                                                 XTF_LOAD_MUST_SUCCEED);
1340         else
1341                 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1342
1343         if (xt_t == NULL)
1344                 return NULL;
1345
1346         target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1347
1348         xt_t->t = g_try_malloc0(target_size);
1349         if (xt_t->t == NULL)
1350                 return NULL;
1351
1352         xt_t->t->u.target_size = target_size;
1353
1354         if (is_builtin || is_user_defined) {
1355                 struct xt_standard_target *target;
1356
1357                 target = (struct xt_standard_target *)(xt_t->t);
1358                 strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1359
1360                 if (is_builtin == TRUE)
1361                         target->verdict = target_to_verdict(target_name);
1362                 else if (is_user_defined == TRUE) {
1363                         struct connman_iptables_entry *target_rule;
1364
1365                         if (chain_head == NULL) {
1366                                 g_free(xt_t->t);
1367                                 return NULL;
1368                         }
1369
1370                         target_rule = chain_head->next->data;
1371                         target->verdict = target_rule->offset;
1372                 }
1373         } else {
1374                 strcpy(xt_t->t->u.user.name, target_name);
1375                 xt_t->t->u.user.revision = xt_t->revision;
1376                 if (xt_t->init != NULL)
1377                         xt_t->init(xt_t->t);
1378         }
1379
1380 #if XTABLES_VERSION_CODE > 5
1381         if (xt_t->x6_options != NULL)
1382                 connman_iptables_globals.opts =
1383                         xtables_options_xfrm(
1384                                 connman_iptables_globals.orig_opts,
1385
1386                                 connman_iptables_globals.opts,
1387                                 xt_t->x6_options,
1388                                 &xt_t->option_offset);
1389         else
1390 #endif
1391                 connman_iptables_globals.opts =
1392                         xtables_merge_options(
1393 #if XTABLES_VERSION_CODE > 5
1394                                 connman_iptables_globals.orig_opts,
1395 #endif
1396                                 connman_iptables_globals.opts,
1397                                 xt_t->extra_opts,
1398                                 &xt_t->option_offset);
1399
1400         if (connman_iptables_globals.opts == NULL) {
1401                 g_free(xt_t->t);
1402                 xt_t = NULL;
1403         }
1404
1405         return xt_t;
1406 }
1407
1408 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1409                         struct xtables_rule_match **xt_rm, char *match_name)
1410 {
1411         struct xtables_match *xt_m;
1412         size_t match_size;
1413
1414         if (match_name == NULL)
1415                 return NULL;
1416
1417         xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1418         match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1419
1420         xt_m->m = g_try_malloc0(match_size);
1421         if (xt_m->m == NULL)
1422                 return NULL;
1423
1424         xt_m->m->u.match_size = match_size;
1425         strcpy(xt_m->m->u.user.name, xt_m->name);
1426         xt_m->m->u.user.revision = xt_m->revision;
1427
1428         if (xt_m->init != NULL)
1429                 xt_m->init(xt_m->m);
1430
1431         if (xt_m == xt_m->next)
1432                 goto done;
1433
1434 #if XTABLES_VERSION_CODE > 5
1435         if (xt_m->x6_options != NULL)
1436                 connman_iptables_globals.opts =
1437                         xtables_options_xfrm(
1438                                 connman_iptables_globals.orig_opts,
1439                                 connman_iptables_globals.opts,
1440                                 xt_m->x6_options,
1441                                 &xt_m->option_offset);
1442         else
1443 #endif
1444                 connman_iptables_globals.opts =
1445                         xtables_merge_options(
1446 #if XTABLES_VERSION_CODE > 5
1447                                 connman_iptables_globals.orig_opts,
1448 #endif
1449                                 connman_iptables_globals.opts,
1450                                 xt_m->extra_opts,
1451                                 &xt_m->option_offset);
1452
1453         if (connman_iptables_globals.opts == NULL) {
1454                 g_free(xt_m->m);
1455                 xt_m = NULL;
1456         }
1457
1458 done:
1459         return xt_m;
1460 }
1461
1462 static int parse_ip_and_mask(const char *str, struct in_addr *ip, struct in_addr *mask)
1463 {
1464         char **tokens;
1465         uint32_t prefixlength;
1466         uint32_t tmp;
1467         int err;
1468
1469         tokens = g_strsplit(str, "/", 2);
1470         if (tokens == NULL)
1471                 return -1;
1472
1473         if (!inet_pton(AF_INET, tokens[0], ip)) {
1474                 err = -1;
1475                 goto out;
1476         }
1477
1478         if (tokens[1] != NULL) {
1479                 prefixlength = strtol(tokens[1], NULL, 10);
1480                 if (prefixlength > 31) {
1481                         err = -1;
1482                         goto out;
1483                 }
1484
1485                 tmp = ~(0xffffffff >> prefixlength);
1486         } else {
1487                 tmp = 0xffffffff;
1488         }
1489
1490         mask->s_addr = htonl(tmp);
1491         ip->s_addr = ip->s_addr & mask->s_addr;
1492         err = 0;
1493 out:
1494         g_strfreev(tokens);
1495
1496         return err;
1497 }
1498
1499 int main(int argc, char *argv[])
1500 {
1501         struct connman_iptables *table;
1502         struct xtables_rule_match *xt_rm, *tmp_xt_rm;
1503         struct xtables_match *xt_m, *xt_m_t;
1504         struct xtables_target *xt_t;
1505         struct ipt_ip ip;
1506         char *table_name, *chain, *new_chain, *match_name, *target_name;
1507         char *delete_chain, *flush_chain, *policy;
1508         int c, in_len, out_len;
1509         gboolean dump, invert, delete, insert, delete_rule, compare_rule;
1510
1511         xtables_init_all(&connman_iptables_globals, NFPROTO_IPV4);
1512
1513         dump = FALSE;
1514         invert = FALSE;
1515         delete = FALSE;
1516         insert = FALSE;
1517         delete_rule = FALSE;
1518         compare_rule = FALSE;
1519         chain = new_chain = match_name = target_name = NULL;
1520         delete_chain = flush_chain = policy = NULL;
1521         memset(&ip, 0, sizeof(struct ipt_ip));
1522         table = NULL;
1523         xt_rm = NULL;
1524         xt_m = NULL;
1525         xt_t = NULL;
1526
1527         /* extension's options will generate false-positives errors */
1528         opterr = 0;
1529
1530         while ((c = getopt_long(argc, argv,
1531                                 "-A:C:D:F:I:L::N:P:X:d:i:j:m:o:s:t:",
1532                                 connman_iptables_globals.opts, NULL)) != -1) {
1533                 switch (c) {
1534                 case 'A':
1535                         /* It is either -A, -C, -D or -I at once */
1536                         if (chain)
1537                                 goto out;
1538
1539                         chain = optarg;
1540                         break;
1541
1542                 case 'C':
1543                         /* It is either -A, -C, -D or -I at once */
1544                         if (chain)
1545                                 goto out;
1546
1547                         chain = optarg;
1548                         compare_rule = TRUE;
1549                         break;
1550
1551                 case 'D':
1552                         /* It is either -A, -C, -D or -I at once */
1553                         if (chain)
1554                                 goto out;
1555
1556                         chain = optarg;
1557                         delete_rule = TRUE;
1558                         break;
1559
1560                 case 'F':
1561                         flush_chain = optarg;
1562                         break;
1563
1564                 case 'I':
1565                         /* It is either -A, -C, -D or -I at once */
1566                         if (chain)
1567                                 goto out;
1568
1569                         chain = optarg;
1570                         insert = TRUE;
1571                         break;
1572
1573                 case 'L':
1574                         dump = true;
1575                         break;
1576
1577                 case 'N':
1578                         new_chain = optarg;
1579                         break;
1580
1581                 case 'P':
1582                         chain = optarg;
1583                         if (optind < argc)
1584                                 policy = argv[optind++];
1585                         else
1586                                 goto out;
1587
1588                         break;
1589
1590                 case 'X':
1591                         delete = true;
1592                         delete_chain = optarg;
1593                         break;
1594
1595                 case 'd':
1596                         if (!parse_ip_and_mask(optarg, &ip.dst, &ip.dmsk))
1597                                 break;
1598
1599                         if (invert)
1600                                 ip.invflags |= IPT_INV_DSTIP;
1601
1602
1603                         break;
1604
1605                 case 'i':
1606                         in_len = strlen(optarg);
1607
1608                         if (in_len + 1 > IFNAMSIZ)
1609                                 break;
1610
1611                         strcpy(ip.iniface, optarg);
1612                         memset(ip.iniface_mask, 0xff, in_len + 1);
1613
1614                         if (invert)
1615                                 ip.invflags |= IPT_INV_VIA_IN;
1616
1617                         break;
1618
1619                 case 'j':
1620                         target_name = optarg;
1621                         xt_t = prepare_target(table, target_name);
1622                         if (xt_t == NULL)
1623                                 goto out;
1624
1625                         break;
1626
1627                 case 'm':
1628                         match_name = optarg;
1629                         xt_m = prepare_matches(table, &xt_rm, match_name);
1630                         if (xt_m == NULL)
1631                                 goto out;
1632
1633                         break;
1634
1635                 case 'o':
1636                         out_len = strlen(optarg);
1637
1638                         if (out_len + 1 > IFNAMSIZ)
1639                                 break;
1640
1641                         strcpy(ip.outiface, optarg);
1642                         memset(ip.outiface_mask, 0xff, out_len + 1);
1643
1644                         if (invert)
1645                                 ip.invflags |= IPT_INV_VIA_OUT;
1646
1647                         break;
1648
1649                 case 's':
1650                         if (!parse_ip_and_mask(optarg, &ip.src, &ip.smsk))
1651                                 break;
1652
1653                         if (invert)
1654                                 ip.invflags |= IPT_INV_SRCIP;
1655
1656                         break;
1657
1658                 case 't':
1659                         table_name = optarg;
1660
1661                         table = connman_iptables_init(table_name);
1662                         if (table == NULL)
1663                                 return -1;
1664
1665                         break;
1666
1667                 case 1:
1668                         if (optarg[0] == '!' && optarg[1] == '\0') {
1669                                 if (invert)
1670                                         printf("Consecutive ! not allowed\n");
1671
1672                                 invert = TRUE;
1673                                 optarg[0] = '\0';
1674                                 continue;
1675                         }
1676
1677                         printf("Invalid option\n");
1678
1679                         return -1;
1680
1681                 default:
1682 #if XTABLES_VERSION_CODE > 5
1683                         if (xt_t != NULL && (xt_t->x6_parse != NULL ||
1684                                                 xt_t->parse != NULL) &&
1685                                         (c >= (int) xt_t->option_offset &&
1686                                         c < (int) xt_t->option_offset +
1687                                         XT_OPTION_OFFSET_SCALE)) {
1688                                 xtables_option_tpcall(c, argv,
1689                                                         invert, xt_t, NULL);
1690
1691                                 break;
1692                         }
1693
1694                         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1695                                                 tmp_xt_rm = tmp_xt_rm->next) {
1696                                 xt_m_t = tmp_xt_rm->match;
1697
1698                                 if (tmp_xt_rm->completed ||
1699                                                 (xt_m_t->x6_parse == NULL &&
1700                                                  xt_m_t->parse == NULL))
1701                                         continue;
1702
1703                                 if (c < (int) xt_m_t->option_offset ||
1704                                         c >= (int) xt_m_t->option_offset
1705                                         + XT_OPTION_OFFSET_SCALE)
1706                                         continue;
1707
1708                                 xtables_option_mpcall(c, argv,
1709                                                         invert, xt_m_t, NULL);
1710
1711                                 break;
1712                         }
1713 #else
1714                         if (xt_t == NULL || xt_t->parse == NULL ||
1715                                 !xt_t->parse(c - xt_t->option_offset,
1716                                 argv, invert, &xt_t->tflags, NULL, &xt_t->t)) {
1717
1718                                 for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1719                                                 tmp_xt_rm = tmp_xt_rm->next) {
1720                                         xt_m_t = tmp_xt_rm->match;
1721
1722                                         if (tmp_xt_rm->completed ||
1723                                                         xt_m_t->parse == NULL)
1724                                                 continue;
1725
1726                                         if (xt_m->parse(c - xt_m->option_offset,
1727                                                 argv, invert, &xt_m->mflags,
1728                                                 NULL, &xt_m->m))
1729                                                 break;
1730                                 }
1731                         }
1732 #endif
1733                         break;
1734                 }
1735
1736                 invert = FALSE;
1737         }
1738
1739 #if XTABLES_VERSION_CODE > 5
1740         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1741                                 tmp_xt_rm = tmp_xt_rm->next)
1742                 xtables_option_mfcall(tmp_xt_rm->match);
1743
1744         if (xt_t != NULL)
1745                 xtables_option_tfcall(xt_t);
1746 #else
1747         for (tmp_xt_rm = xt_rm; tmp_xt_rm != NULL;
1748                                 tmp_xt_rm = tmp_xt_rm->next)
1749                 if (tmp_xt_rm->match->final_check != NULL)
1750                         tmp_xt_rm->match->final_check(
1751                                         tmp_xt_rm->match->mflags);
1752
1753         if (xt_t != NULL && xt_t->final_check != NULL)
1754                 xt_t->final_check(xt_t->tflags);
1755 #endif
1756
1757         if (table == NULL) {
1758                 table_name = "filter";
1759
1760                 table = connman_iptables_init(table_name);
1761                 if (table == NULL)
1762                         return -1;
1763         }
1764
1765         if (delete) {
1766                 if (delete_chain == NULL)
1767                         goto out;
1768
1769                 printf("Delete chain %s\n", delete_chain);
1770
1771                 connman_iptables_delete_chain(table, delete_chain);
1772
1773                 goto commit;
1774         }
1775
1776         if (flush_chain) {
1777                 printf("Flush chain %s\n", flush_chain);
1778
1779                 connman_iptables_flush_chain(table, flush_chain);
1780
1781                 goto commit;
1782         }
1783
1784         if (dump) {
1785                 connman_iptables_dump(table);
1786
1787                 return 0;
1788         }
1789
1790         if (chain && new_chain)
1791                 return -1;
1792
1793         if (new_chain) {
1794                 printf("New chain %s\n", new_chain);
1795
1796                 connman_iptables_add_chain(table, new_chain);
1797
1798                 goto commit;
1799         }
1800
1801         if (chain) {
1802                 if (policy != NULL) {
1803                         printf("Changing policy of %s to %s\n", chain, policy);
1804
1805                         connman_iptables_change_policy(table, chain, policy);
1806
1807                         goto commit;
1808                 }
1809
1810                 if (compare_rule == TRUE) {
1811                         int ret;
1812
1813                         ret = connman_iptables_compare_rule(table, &ip,
1814                                 chain, target_name, xt_t, xt_m, xt_rm);
1815
1816                         if (ret == 0)
1817                                 printf("Rule exists.\n");
1818                         else
1819                                 printf("Rule does not exist.\n");
1820
1821                         goto out;
1822                 }
1823
1824                 if (delete_rule == TRUE) {
1825                         printf("Deleting %s to %s (match %s)\n", target_name,
1826                                         chain, match_name);
1827
1828                         connman_iptables_delete_rule(table, &ip, chain,
1829                                         target_name, xt_t, xt_m, xt_rm);
1830
1831                         goto commit;
1832                 }
1833
1834                 if (insert == TRUE) {
1835                         printf("Inserting %s to %s (match %s)\n", target_name,
1836                                         chain, match_name);
1837
1838                         connman_iptables_insert_rule(table, &ip, chain,
1839                                                 target_name, xt_t, xt_rm);
1840                 } else {
1841                         printf("Appending %s to %s (match %s)\n", target_name,
1842                                         chain, match_name);
1843
1844                         connman_iptables_append_rule(table, &ip, chain,
1845                                                 target_name, xt_t, xt_rm);
1846                 }
1847         }
1848
1849 commit:
1850
1851         connman_iptables_commit(table);
1852
1853 out:
1854         connman_iptables_cleanup(table);
1855
1856         if (xt_t)
1857                 g_free(xt_t->t);
1858
1859         if (xt_m)
1860                 g_free(xt_m->m);
1861
1862         return 0;
1863 }