/
iptables.c
3893 lines (3102 loc) · 82.7 KB
/
iptables.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Connection Manager
*
* Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <xtables.h>
#include <inttypes.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
#include "connman.h"
#include "src/shared/util.h"
/*
* Some comments on how the iptables API works (some of them from the
* source code from iptables and the kernel):
*
* - valid_hooks: bit indicates valid IDs for hook_entry
* - hook_entry[ID] offset to the chain start
* - overflows should be end of entry chains, and uncodintional policy nodes.
* - policy entry: last entry in a chain
* - user chain: end of last builtin + policy entry
* - final entry must be error node
* - Underflows must be unconditional and use the STANDARD target with
* ACCEPT/DROP
* - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
* - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
* - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
* table header/meta info. The table is appended after the header. The entries
* are of the type struct ipt_entry.
* - After the ipt_entry the matches are appended. After the matches
* the target is appended.
* - ipt_entry->target_offset = Size of ipt_entry + matches
* - ipt_entry->next_offset = Size of ipt_entry + matches + target
* - IPT_SO_SET_REPLACE is used to write a table (contains the complete
* - hook_entry and overflow mark the begining and the end of a chain, e.g
* entry hook: pre/in/fwd/out/post -1/0/352/504/-1
* underflow: pre/in/fwd/out/post -1/200/352/904/-1
* means that INPUT starts at offset 0 and ends at 200 (the start offset to
* the last element). FORWARD has one entry starting/ending at 352. The entry
* has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
* which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
* the filter table.
* - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
* writing that table looks like this:
*
* filter valid_hooks 0x0000000e num_entries 5 size 856
* entry hook: pre/in/fwd/out/post -1/0/376/528/-1
* underflow: pre/in/fwd/out/post -1/224/376/528/-1
* entry 0x699d30 offset 0 size 224
* RULE match 0x699da0 target 0x699dd0
* match mark match 0x3e7
* target LOG flags 0 level 4
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x699e10 offset 224 size 152
* RULE match 0x699e80 target 0x699e80
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x699ea8 offset 376 size 152
* RULE match 0x699f18 target 0x699f18
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x699f40 offset 528 size 152
* RULE match 0x699fb0 target 0x699fb0
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x699fd8 offset 680 size 176
* USER CHAIN (ERROR) match 0x69a048 target 0x69a048
*
* Reading the filter table looks like this:
*
* filter valid_hooks 0x0000000e num_entries 5 size 856
* entry hook: pre/in/fwd/out/post -1/0/376/528/-1
* underflow: pre/in/fwd/out/post -1/224/376/528/-1
* entry 0x25fec28 offset 0 size 224
* CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
* match mark match 0x3e7
* target LOG flags 0 level 4
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x25fed08 offset 224 size 152
* RULE match 0x25fed78 target 0x25fed78
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x25feda0 offset 376 size 152
* CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x25fee38 offset 528 size 152
* CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
* target ACCEPT
* src 0.0.0.0/0.0.0.0
* dst 0.0.0.0/0.0.0.0
* entry 0x25feed0 offset 680 size 176
* End of CHAIN
*/
/*
* Values for the index values used here are defined as equal for both IPv4
* and IPv6 (NF_IP_* and NF_IP6_*) in Netfilter headers.
*/
static const char *hooknames[] = {
[NF_IP_PRE_ROUTING] = "PREROUTING",
[NF_IP_LOCAL_IN] = "INPUT",
[NF_IP_FORWARD] = "FORWARD",
[NF_IP_LOCAL_OUT] = "OUTPUT",
[NF_IP_POST_ROUTING] = "POSTROUTING",
};
#define LABEL_ACCEPT "ACCEPT"
#define LABEL_DROP "DROP"
#define LABEL_QUEUE "QUEUE"
#define LABEL_RETURN "RETURN"
#define XT_OPTION_OFFSET_SCALE 256
struct connman_iptables_entry {
int type;
unsigned int offset;
int builtin;
int counter_idx;
struct ipt_entry *entry;
struct ip6t_entry *entry6;
};
struct connman_iptables {
int type;
char *name;
int ipt_sock;
struct ipt_getinfo *info;
struct ipt_get_entries *blob_entries;
struct ip6t_getinfo *info6;
struct ip6t_get_entries *blob_entries6;
unsigned int num_entries;
unsigned int old_entries;
unsigned int size;
unsigned int underflow[NF_INET_NUMHOOKS];
unsigned int hook_entry[NF_INET_NUMHOOKS];
GList *entries;
};
static GHashTable *table_hash = NULL;
static GHashTable *table_hash_ipv6 = NULL;
static bool debug_enabled = false;
struct iptables_ip {
int type;
struct ipt_ip *ip;
struct ip6t_ip6 *ip6;
};
struct iptables_replace {
int type;
struct ipt_replace *r;
struct ip6t_replace *r6;
};
typedef int (*iterate_entries_cb_t)(struct connman_iptables_entry *entry,
int builtin, unsigned int hook,
size_t size, unsigned int offset,
void *user_data);
static u_int16_t iptables_entry_get_next_offset(
struct connman_iptables_entry *entry)
{
if (!entry)
return 0;
switch (entry->type) {
case AF_INET:
return entry->entry ? entry->entry->next_offset : 0;
case AF_INET6:
return entry->entry6 ? entry->entry6->next_offset : 0;
}
return 0;
}
static u_int16_t iptables_entry_get_target_offset(
struct connman_iptables_entry *entry)
{
if (!entry)
return 0;
switch (entry->type) {
case AF_INET:
return entry->entry ? entry->entry->target_offset : 0;
case AF_INET6:
return entry->entry6 ? entry->entry6->target_offset : 0;
}
return 0;
}
static unsigned char *iptables_entry_get_elems(
struct connman_iptables_entry *entry)
{
if (!entry)
return NULL;
switch (entry->type) {
case AF_INET:
return entry->entry ? entry->entry->elems : NULL;
case AF_INET6:
return entry->entry6 ? entry->entry6->elems : NULL;
}
return NULL;
}
static struct xt_entry_target *iptables_entry_get_target(
struct connman_iptables_entry *entry)
{
if (!entry)
return NULL;
switch (entry->type) {
case AF_INET:
return entry->entry ? ipt_get_target(entry->entry) : NULL;
case AF_INET6:
return entry->entry6 ? ip6t_get_target(entry->entry6) : NULL;
}
return NULL;
}
static struct xt_counters *iptables_entry_get_counters(
struct connman_iptables_entry *entry)
{
if (!entry)
return NULL;
switch (entry->type) {
case AF_INET:
return entry->entry ? &entry->entry->counters : NULL;
case AF_INET6:
return entry->entry6 ? &entry->entry6->counters : NULL;
}
return NULL;
}
static void iptables_entry_free(struct connman_iptables_entry *entry)
{
if (!entry)
return;
g_free(entry->entry);
g_free(entry->entry6);
g_free(entry);
}
static const char *iptables_table_get_info_name(struct connman_iptables* table)
{
if (!table)
return NULL;
switch (table->type) {
case AF_INET:
return table->info->name;
case AF_INET6:
return table->info6->name;
}
return NULL;
}
static unsigned int iptables_table_get_info_num_entries(
struct connman_iptables* table)
{
if (!table)
return 0;
switch (table->type) {
case AF_INET:
return table->info->num_entries;
case AF_INET6:
return table->info6->num_entries;
}
return 0;
}
static unsigned int iptables_table_get_info_size(struct connman_iptables* table)
{
if (!table)
return 0;
switch (table->type) {
case AF_INET:
return table->info->size;
case AF_INET6:
return table->info6->size;
}
return 0;
}
static unsigned int iptables_table_get_info_valid_hooks(
struct connman_iptables* table)
{
if (!table)
return 0;
switch (table->type) {
case AF_INET:
return table->info->valid_hooks;
case AF_INET6:
return table->info6->valid_hooks;
}
return 0;
}
static unsigned int *iptables_table_get_info_hook_entry(
struct connman_iptables* table)
{
if (!table)
return NULL;
switch (table->type) {
case AF_INET:
return table->info->hook_entry;
case AF_INET6:
return table->info6->hook_entry;
}
return NULL;
}
static unsigned int *iptables_table_get_info_underflow(
struct connman_iptables* table)
{
if (!table)
return NULL;
switch (table->type) {
case AF_INET:
return table->info->underflow;
case AF_INET6:
return table->info6->underflow;
}
return NULL;
}
static unsigned int iptables_table_get_entries_size(
struct connman_iptables* table)
{
if (!table)
return 0;
switch (table->type) {
case AF_INET:
return table->blob_entries->size;
case AF_INET6:
return table->blob_entries6->size;
}
return 0;
}
static const char *get_error_target(int type)
{
switch (type) {
case AF_INET:
return IPT_ERROR_TARGET;
case AF_INET6:
return IP6T_ERROR_TARGET;
default:
return XT_ERROR_TARGET;
}
}
static const char *get_standard_target(int type)
{
switch (type) {
case AF_INET:
return IPT_STANDARD_TARGET;
case AF_INET6:
return IP6T_STANDARD_TARGET;
default:
return XT_STANDARD_TARGET;
}
}
static struct connman_iptables *hash_table_lookup(int type,
const char *table_name) {
switch (type) {
case AF_INET:
return g_hash_table_lookup(table_hash, table_name);
case AF_INET6:
return g_hash_table_lookup(table_hash_ipv6, table_name);
}
return NULL;
}
static bool hash_table_replace(int type,
char *table_name,
struct connman_iptables *table) {
switch (type) {
case AF_INET:
return g_hash_table_replace(table_hash, table_name, table);
case AF_INET6:
return g_hash_table_replace(table_hash_ipv6, table_name, table);
}
return false;
}
static bool hash_table_remove(int type, const char *table_name)
{
switch (type) {
case AF_INET:
return g_hash_table_remove(table_hash, table_name);
case AF_INET6:
return g_hash_table_remove(table_hash_ipv6, table_name);
}
return false;
}
static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
{
unsigned int h;
if (*valid_hooks == 0)
return NF_INET_NUMHOOKS;
h = __builtin_ffs(*valid_hooks) - 1;
*valid_hooks ^= (1 << h);
return h;
}
static int iterate_entries(struct connman_iptables_entry *entries,
unsigned int valid_hooks,
unsigned int *hook_entry,
unsigned int *underflow,
size_t size, iterate_entries_cb_t cb,
void *user_data)
{
unsigned int offset, h, hook;
int builtin, err;
struct connman_iptables_entry entry;
if (!entries)
return -EINVAL;
switch (entries->type) {
case AF_INET:
if (!entries->entry)
return -EINVAL;
break;
case AF_INET6:
if (!entries->entry6)
return -EINVAL;
break;
default:
return -EINVAL;
}
h = next_hook_entry_index(&valid_hooks);
hook = h;
entry.type = entries->type;
entry.entry = entries->entry;
entry.entry6 = entries->entry6;
for (offset = 0; offset < size;
offset += iptables_entry_get_next_offset(&entry)) {
builtin = -1;
switch (entries->type) {
case AF_INET:
entry.entry = (void* )entries->entry + offset;
break;
case AF_INET6:
entry.entry6 = (void* )entries->entry6 + offset;
break;
}
/*
* Updating builtin, hook and h is very tricky.
* The rules are:
* - builtin is only set to the current hook number
* if the current entry is the hook entry (aka chain
* head). And only for builtin chains, never for
* the user chains.
* - hook is the current hook number. If we
* look at user chains it needs to be NF_INET_NETNUMHOOKS.
* - h is the next hook entry. Thous we need to be carefully
* not to access the table when h is NF_INET_NETNUMHOOKS.
*/
if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
builtin = h;
hook = h;
}
if (h == NF_INET_NUMHOOKS)
hook = h;
if (h < NF_INET_NUMHOOKS && underflow[h] <= offset)
h = next_hook_entry_index(&valid_hooks);
err = cb(&entry, builtin, hook, size, offset, user_data);
if (err < 0)
return err;
}
return 0;
}
static int print_entry(struct connman_iptables_entry *entry, int builtin,
unsigned int hook, size_t size,
unsigned int offset, void *user_data)
{
iterate_entries_cb_t cb;
struct xt_counters *counters;
cb = user_data;
counters = iptables_entry_get_counters(entry);
DBG("entry %p hook %u offset %u size %u packets %"PRIu64" "
"bytes %"PRIu64, entry, hook, offset,
iptables_entry_get_next_offset(entry),
(uint64_t) counters->pcnt, (uint64_t) counters->bcnt);
return cb(entry, builtin, hook, size, offset, NULL);
}
static int target_to_verdict(const char *target_name)
{
if (!g_strcmp0(target_name, LABEL_ACCEPT))
return -NF_ACCEPT - 1;
if (!g_strcmp0(target_name, LABEL_DROP))
return -NF_DROP - 1;
if (!g_strcmp0(target_name, LABEL_QUEUE))
return -NF_QUEUE - 1;
if (!g_strcmp0(target_name, LABEL_RETURN))
return XT_RETURN;
return 0;
}
static bool is_builtin_target(const char *target_name)
{
if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
!g_strcmp0(target_name, LABEL_DROP) ||
!g_strcmp0(target_name, LABEL_QUEUE) ||
!g_strcmp0(target_name, LABEL_RETURN))
return true;
return false;
}
static bool is_jump(struct connman_iptables_entry *e)
{
struct xt_entry_target *target;
target = iptables_entry_get_target(e);
if (!target)
return false;
if (!g_strcmp0(target->u.user.name, get_standard_target(e->type))) {
struct xt_standard_target *t;
t = (struct xt_standard_target *)target;
switch (t->verdict) {
case XT_RETURN:
case -NF_ACCEPT - 1:
case -NF_DROP - 1:
case -NF_QUEUE - 1:
case -NF_STOP - 1:
return false;
default:
return true;
}
}
return false;
}
static bool is_fallthrough(struct connman_iptables_entry *e)
{
struct xt_entry_target *target;
target = iptables_entry_get_target(e);
if (!target)
return false;
if (!g_strcmp0(target->u.user.name, get_standard_target(e->type))) {
struct xt_standard_target *t;
t = (struct xt_standard_target *)target;
if (t->verdict == 0)
return true;
}
return false;
}
static bool is_chain(struct connman_iptables *table,
struct connman_iptables_entry *e)
{
struct xt_entry_target *target;
if (!e)
return false;
if (e->builtin >= 0)
return true;
target = iptables_entry_get_target(e);
if (!target)
return false;
if (!g_strcmp0(target->u.user.name, get_error_target(e->type)))
return true;
return false;
}
static GList *find_chain_head(struct connman_iptables *table,
const char *chain_name)
{
GList *list;
struct connman_iptables_entry *head;
struct xt_entry_target *target;
int builtin;
switch (table->type) {
case AF_INET:
case AF_INET6:
break;
default:
return NULL;
}
for (list = table->entries; list; list = list->next) {
head = list->data;
/* Buit-in chain */
builtin = head->builtin;
if (builtin >= 0 && !g_strcmp0(hooknames[builtin], chain_name))
break;
/* User defined chain */
target = iptables_entry_get_target(head);
if (!target)
continue;
if (!g_strcmp0(target->u.user.name,
get_error_target(table->type)) &&
!g_strcmp0((char *)target->data, chain_name))
break;
}
return list;
}
static GList *find_chain_tail(struct connman_iptables *table,
const char *chain_name)
{
struct connman_iptables_entry *tail;
GList *chain_head, *list;
chain_head = find_chain_head(table, chain_name);
if (!chain_head)
return NULL;
/* Then we look for the next chain */
for (list = chain_head->next; list; list = list->next) {
tail = list->data;
if (is_chain(table, tail))
return list;
}
/* Nothing found, we return the table end */
return g_list_last(table->entries);
}
static void update_offsets(struct connman_iptables *table)
{
GList *list, *prev;
struct connman_iptables_entry *entry, *prev_entry;
for (list = table->entries; list; list = list->next) {
entry = list->data;
if (list == table->entries) {
entry->offset = 0;
continue;
}
prev = list->prev;
prev_entry = prev->data;
entry->offset = prev_entry->offset +
iptables_entry_get_next_offset(
prev_entry);
}
}
static void update_targets_reference(struct connman_iptables *table,
struct connman_iptables_entry *entry_before,
struct connman_iptables_entry *modified_entry,
bool is_removing)
{
struct connman_iptables_entry *tmp;
struct xt_standard_target *t;
GList *list;
unsigned int offset;
offset = iptables_entry_get_next_offset(modified_entry);
for (list = table->entries; list; list = list->next) {
tmp = list->data;
if (!is_jump(tmp))
continue;
t = (struct xt_standard_target *)
iptables_entry_get_target(tmp);
if (!t)
continue;
if (is_removing) {
if (t->verdict >= entry_before->offset)
t->verdict -= offset;
} else {
if (t->verdict > entry_before->offset)
t->verdict += offset;
}
}
if (is_fallthrough(modified_entry)) {
t = (struct xt_standard_target *)
iptables_entry_get_target(modified_entry);
if (!t)
return;
t->verdict = entry_before->offset +
iptables_entry_get_target_offset(modified_entry) +
XT_ALIGN(sizeof(struct xt_standard_target));
t->target.u.target_size =
XT_ALIGN(sizeof(struct xt_standard_target));
}
}
static int iptables_add_entry(struct connman_iptables *table,
struct connman_iptables_entry *entry,
GList *before, int builtin, int counter_idx)
{
struct connman_iptables_entry *e, *entry_before;
if (!table) {
return -EINVAL;
}
e = g_try_malloc0(sizeof(struct connman_iptables_entry));
if (!e)
return -ENOMEM;
switch (table->type) {
case AF_INET:
e->entry = entry->entry;
break;
case AF_INET6:
e->entry6 = entry->entry6;
break;
default:
g_free(e);
return -EINVAL;
}
e->type = entry->type;
e->builtin = builtin;
e->counter_idx = counter_idx;
table->entries = g_list_insert_before(table->entries, before, e);
table->num_entries++;
table->size += iptables_entry_get_next_offset(e);
if (!before) {
e->offset = table->size -
iptables_entry_get_next_offset(e);
return 0;
}
entry_before = before->data;
/*
* We've just appended/insterted a new entry. All references
* should be bumped accordingly.
*/
update_targets_reference(table, entry_before, e, false);
update_offsets(table);
return 0;
}
static int remove_table_entry(struct connman_iptables *table,
struct connman_iptables_entry *entry)
{
int removed = 0;
u_int16_t next_offset;
next_offset = iptables_entry_get_next_offset(entry);
table->num_entries--;
table->size -= next_offset;
removed = next_offset;
table->entries = g_list_remove(table->entries, entry);
iptables_entry_free(entry);
return removed;
}
static void delete_update_hooks(struct connman_iptables *table,
int builtin, GList *chain_head,
int removed)
{
struct connman_iptables_entry *e;
GList *list;
e = chain_head->data;
e->builtin = builtin;
table->underflow[builtin] -= removed;
for (list = chain_head->next; list; list = list->next) {
e = list->data;
if (e->builtin < 0)
continue;
table->hook_entry[e->builtin] -= removed;
table->underflow[e->builtin] -= removed;
}
}
static int iptables_flush_chain(struct connman_iptables *table,
const char *name)
{
GList *chain_head, *chain_tail, *list, *next;
struct connman_iptables_entry *entry;
int builtin, removed = 0;
DBG("table %s chain %s", table->name, name);
chain_head = find_chain_head(table, name);
if (!chain_head)
return -EINVAL;
chain_tail = find_chain_tail(table, name);
if (!chain_tail)
return -EINVAL;
entry = chain_head->data;
builtin = entry->builtin;
if (builtin >= 0)
list = chain_head;
else
list = chain_head->next;
if (list == chain_tail->prev)
return 0;
while (list != chain_tail->prev) {
entry = list->data;
next = g_list_next(list);
removed += remove_table_entry(table, entry);
list = next;
}
if (builtin >= 0)
delete_update_hooks(table, builtin, chain_tail->prev, removed);
update_offsets(table);
return 0;
}
static int iptables_add_chain(struct connman_iptables *table,
const char *name)
{
GList *last;
struct ipt_entry *entry_head = NULL;
struct ipt_entry *entry_return = NULL;
struct ip6t_entry *entry6_head = NULL;
struct ip6t_entry *entry6_return = NULL;
struct connman_iptables_entry entry = { 0 };
struct xt_error_target *error = NULL;
struct ipt_standard_target *standard = NULL;
u_int16_t entry_head_size, entry_return_size;
size_t entry_struct_size = 0;
size_t xt_error_target_size = 0;
size_t standard_target_size = 0;
DBG("table %s chain %s", table->name, name);
entry.type = table->type;
/* Do not allow to add duplicate chains */
if (find_chain_head(table, name))
return -EEXIST;
last = g_list_last(table->entries);
xt_error_target_size = XT_ALIGN(sizeof(struct xt_error_target));
/*
* An empty chain is composed of:
* - A head entry, with no match and an error target.
* The error target data is the chain name.
* - A tail entry, with no match and a standard target.
* The standard target verdict is XT_RETURN (return to the
* caller).
*/
/* head entry */
switch (entry.type) {
case AF_INET:
entry_struct_size = XT_ALIGN(sizeof(struct ipt_entry));
entry_head_size = entry_struct_size + xt_error_target_size;
entry_head = g_try_malloc0(entry_head_size);
if (!entry_head)
goto err_head;
entry_head->target_offset = entry_struct_size;
entry_head->next_offset = entry_head_size;
error = (struct xt_error_target *) entry_head->elems;
entry.entry = entry_head;