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