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