forked from nodogsplash/nodogsplash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfw_iptables.c
1083 lines (936 loc) · 35.8 KB
/
fw_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
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* 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, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
* *
\********************************************************************/
/** @internal
@file fw_iptables.c
@brief Firewall iptables functions
@author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
@author Copyright (C) 2007 Paul Kube <nodogsplash@kokoro.ucsd.edu>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include "common.h"
#include "safe.h"
#include "conf.h"
#include "auth.h"
#include "client_list.h"
#include "fw_iptables.h"
#include "debug.h"
#include "util.h"
#include "tc.h"
// iptables v1.4.17
#define MIN_IPTABLES_VERSION (1 * 10000 + 4 * 100 + 17)
static char *_iptables_compile(const char[], const char[], t_firewall_rule *);
static int _iptables_append_ruleset(const char[], const char[], const char[]);
static int _iptables_init_marks(void);
/** Used to mark packets, and characterize client state. Unmarked packets are considered 'preauthenticated' */
unsigned int FW_MARK_PREAUTHENTICATED; /**< @brief 0: Actually not used as a packet mark */
unsigned int FW_MARK_AUTHENTICATED; /**< @brief The client is authenticated */
unsigned int FW_MARK_BLOCKED; /**< @brief The client is blocked */
unsigned int FW_MARK_TRUSTED; /**< @brief The client is trusted */
unsigned int FW_MARK_MASK; /**< @brief Iptables mask: bitwise or of the others */
extern pthread_mutex_t client_list_mutex;
extern pthread_mutex_t config_mutex;
/**
* Make nonzero to supress the error output of the firewall during destruction.
*/
static int fw_quiet = 0;
/**
* Used to configure use of --or-mark vs. --set-mark
*/
static const char* markop = "--set-mark";
/**
* Used to configure use of mark mask, or not
*/
static const char* markmask = "";
/** Return a string representing a connection state */
const char *
fw_connection_state_as_string(int mark)
{
if (mark == FW_MARK_PREAUTHENTICATED)
return "Preauthenticated";
if (mark == FW_MARK_AUTHENTICATED)
return "Authenticated";
if (mark == FW_MARK_TRUSTED)
return "Trusted";
if (mark == FW_MARK_BLOCKED)
return "Blocked";
return "ERROR: unrecognized mark";
}
/** @internal */
int
_iptables_init_marks()
{
/* Check FW_MARK values are distinct. */
if (FW_MARK_BLOCKED == FW_MARK_TRUSTED ||
FW_MARK_TRUSTED == FW_MARK_AUTHENTICATED ||
FW_MARK_AUTHENTICATED == FW_MARK_BLOCKED) {
debug(LOG_ERR, "FW_MARK_BLOCKED, FW_MARK_TRUSTED, FW_MARK_AUTHENTICATED not distinct values.");
return -1;
}
/* Check FW_MARK values nonzero. */
if (FW_MARK_BLOCKED == 0 ||
FW_MARK_TRUSTED == 0 ||
FW_MARK_AUTHENTICATED == 0) {
debug(LOG_ERR, "FW_MARK_BLOCKED, FW_MARK_TRUSTED, FW_MARK_AUTHENTICATED not all nonzero.");
return -1;
}
FW_MARK_PREAUTHENTICATED = 0; /* always 0 */
/* FW_MARK_MASK is bitwise OR of other marks */
FW_MARK_MASK = FW_MARK_BLOCKED | FW_MARK_TRUSTED | FW_MARK_AUTHENTICATED;
debug(LOG_INFO,"Iptables mark %s: 0x%x",
fw_connection_state_as_string(FW_MARK_PREAUTHENTICATED),
FW_MARK_PREAUTHENTICATED);
debug(LOG_INFO,"Iptables mark %s: 0x%x",
fw_connection_state_as_string(FW_MARK_AUTHENTICATED),
FW_MARK_AUTHENTICATED);
debug(LOG_INFO,"Iptables mark %s: 0x%x",
fw_connection_state_as_string(FW_MARK_TRUSTED),
FW_MARK_TRUSTED);
debug(LOG_INFO,"Iptables mark %s: 0x%x",
fw_connection_state_as_string(FW_MARK_BLOCKED),
FW_MARK_BLOCKED);
return 0;
}
/** @internal */
int
_iptables_check_mark_masking()
{
/* See if kernel supports mark or-ing */
fw_quiet = 1; /* do it quietly */
if (0 == iptables_do_command("-t mangle -I PREROUTING 1 -j MARK --or-mark 0x%x", FW_MARK_BLOCKED)) {
iptables_do_command("-t mangle -D PREROUTING 1"); /* delete test rule we just inserted */
debug(LOG_DEBUG, "Kernel supports --or-mark.");
markop = "--or-mark";
} else {
debug(LOG_INFO,"Kernel does not support iptables --or-mark. Using --set-mark instead.");
markop = "--set-mark";
}
/* See if kernel supports mark masking */
if (0 == iptables_do_command("-t filter -I FORWARD 1 -m mark --mark 0x%x/0x%x -j REJECT", FW_MARK_BLOCKED, FW_MARK_MASK)) {
iptables_do_command("-t filter -D FORWARD 1"); /* delete test rule we just inserted */
debug(LOG_DEBUG, "Kernel supports mark masking.");
char *tmp = NULL;
safe_asprintf(&tmp,"/0x%x",FW_MARK_MASK);
markmask = tmp;
} else {
debug(LOG_INFO, "Kernel does not support iptables mark masking. Using empty mask.");
markmask = "";
}
debug(LOG_INFO, "Iptables mark op \"%s\" and mark mask \"%s\".", markop, markmask);
fw_quiet = 0; /* restore verbosity */
return 0;
}
/** @internal */
int
iptables_do_command(const char *format, ...)
{
va_list vlist;
char *fmt_cmd = NULL;
s_config *config;
char *iptables;
int rc;
int i;
va_start(vlist, format);
safe_vasprintf(&fmt_cmd, format, vlist);
va_end(vlist);
config = config_get_config();
iptables = config->ip6 ? "ip6tables" : "iptables";
for (i = 0; i < 5; i++) {
if (fw_quiet) {
rc = execute("%s --wait %s > /dev/null 2>&1", iptables, fmt_cmd);
} else {
rc = execute("%s --wait %s", iptables, fmt_cmd);
}
if (rc == 4) {
/* iptables error code 4 indicates a resource problem that might
* be temporary. So we retry to insert the rule a few times. (Mitar) */
sleep(1);
} else {
break;
}
}
free(fmt_cmd);
return rc;
}
/**
* @internal
* Compiles a struct definition of a firewall rule into a valid iptables
* command.
* @arg table Table containing the chain.
* @arg chain Chain that the command will be (-A)ppended to.
* @arg rule Definition of a rule into a struct, from conf.c.
*/
static char *
_iptables_compile(const char table[], const char chain[], t_firewall_rule *rule)
{
char command[MAX_BUF];
char *mode;
mode = NULL;
memset(command, 0, MAX_BUF);
switch (rule->target) {
case TARGET_DROP:
mode = "DROP";
break;
case TARGET_REJECT:
mode = "REJECT";
break;
case TARGET_ACCEPT:
mode = "ACCEPT";
break;
case TARGET_LOG:
mode = "LOG";
break;
case TARGET_ULOG:
mode = "ULOG";
break;
}
snprintf(command, sizeof(command), "-t %s -A %s ", table, chain);
if (rule->mask != NULL) {
snprintf((command + strlen(command)),
(sizeof(command) - strlen(command)),
"-d %s ", rule->mask);
}
if (rule->protocol != NULL) {
snprintf((command + strlen(command)),
(sizeof(command) - strlen(command)),
"-p %s ", rule->protocol);
}
if (rule->port != NULL) {
snprintf((command + strlen(command)),
(sizeof(command) - strlen(command)),
"--dport %s ", rule->port);
}
if (rule->ipset != NULL) {
snprintf((command + strlen(command)),
(sizeof(command) - strlen(command)),
"-m set --match-set %s dst ", rule->ipset);
}
snprintf((command + strlen(command)),
(sizeof(command) - strlen(command)),
"-j %s", mode);
/* XXX The buffer command, an automatic variable, will get cleaned
* off of the stack when we return, so we strdup() it. */
return(safe_strdup(command));
}
/**
* @internal
* append all the rules in a rule set.
* @arg ruleset Name of the ruleset
* @arg table Table containing the chain.
* @arg chain IPTables chain the rules go into
*/
static int
_iptables_append_ruleset(const char table[], const char ruleset[], const char chain[])
{
t_firewall_rule *rule;
char *cmd;
int ret = 0;
debug(LOG_DEBUG, "Loading ruleset %s into table %s, chain %s", ruleset, table, chain);
for (rule = get_ruleset_list(ruleset); rule != NULL; rule = rule->next) {
cmd = _iptables_compile(table, chain, rule);
debug(LOG_DEBUG, "Loading rule \"%s\" into table %s, chain %s", cmd, table, chain);
ret |= iptables_do_command(cmd);
free(cmd);
}
debug(LOG_DEBUG, "Ruleset %s loaded into table %s, chain %s", ruleset, table, chain);
return ret;
}
int
iptables_block_mac(const char mac[])
{
return iptables_do_command("-t mangle -A " CHAIN_BLOCKED " -m mac --mac-source %s -j MARK %s 0x%x", mac, markop, FW_MARK_BLOCKED);
}
int
iptables_unblock_mac(const char mac[])
{
return iptables_do_command("-t mangle -D " CHAIN_BLOCKED " -m mac --mac-source %s -j MARK %s 0x%x", mac, markop, FW_MARK_BLOCKED);
}
int
iptables_allow_mac(const char mac[])
{
return iptables_do_command("-t mangle -I " CHAIN_BLOCKED " -m mac --mac-source %s -j RETURN", mac);
}
int
iptables_unallow_mac(const char mac[])
{
return iptables_do_command("-t mangle -D " CHAIN_BLOCKED " -m mac --mac-source %s -j RETURN", mac);
}
int
iptables_trust_mac(const char mac[])
{
return iptables_do_command("-t mangle -A " CHAIN_TRUSTED " -m mac --mac-source %s -j MARK %s 0x%x", mac, markop, FW_MARK_TRUSTED);
}
int
iptables_untrust_mac(const char mac[])
{
return iptables_do_command("-t mangle -D " CHAIN_TRUSTED " -m mac --mac-source %s -j MARK %s 0x%x", mac, markop, FW_MARK_TRUSTED);
}
int get_iptables_version()
{
char buf[256];
int minor;
int major;
int patch;
int rc;
rc = execute_ret(buf, sizeof(buf), "iptables -V");
if (rc == 0 && sscanf(buf, "iptables v%d.%d.%d", &major, &minor, &patch) == 3) {
return major * 10000 + minor * 100 + patch;
} else {
return -1;
}
}
/** Initialize the firewall rules.
*/
int
iptables_fw_init(void)
{
s_config *config;
int iptables_version;
char *gw_interface = NULL;
char *gw_ip = NULL;
char *gw_address = NULL;
char *gw_iprange = NULL;
int gw_port = 0;
char *fas_remoteip = NULL;
int fas_port = 0;
int traffic_control;
int set_mss, mss_value;
t_MAC *pt;
t_MAC *pb;
t_MAC *pa;
int rc = 0;
int macmechanism;
debug(LOG_NOTICE, "Initializing firewall rules");
LOCK_CONFIG();
config = config_get_config();
gw_interface = safe_strdup(config->gw_interface); /* must free */
/* ip4 vs ip6 differences */
const char *ICMP_TYPE;
if (config->ip6) {
/* ip6 addresses must be in square brackets like [ffcc:e08::1] */
safe_asprintf(&gw_ip, "[%s]", config->gw_ip); /* must free */
ICMP_TYPE = "icmp6";
} else {
gw_ip = safe_strdup(config->gw_ip); /* must free */
ICMP_TYPE = "icmp";
}
if (config->fas_port) {
fas_remoteip = safe_strdup(config->fas_remoteip); /* must free */
fas_port = config->fas_port;
}
gw_address = safe_strdup(config->gw_address); /* must free */
gw_iprange = safe_strdup(config->gw_iprange); /* must free */
gw_port = config->gw_port;
pt = config->trustedmaclist;
pb = config->blockedmaclist;
pa = config->allowedmaclist;
macmechanism = config->macmechanism;
set_mss = config->set_mss;
mss_value = config->mss_value;
traffic_control = config->traffic_control;
FW_MARK_BLOCKED = config->fw_mark_blocked;
FW_MARK_TRUSTED = config->fw_mark_trusted;
FW_MARK_AUTHENTICATED = config->fw_mark_authenticated;
UNLOCK_CONFIG();
iptables_version = get_iptables_version();
if (iptables_version < 0) {
debug(LOG_ERR, "Cannot get iptables version.");
return -1;
}
if (iptables_version < MIN_IPTABLES_VERSION) {
debug(LOG_ERR, "Unsupported iptables version v%d.%d.%d, needs at least v%d.%d.%d.",
(iptables_version / 10000),
(iptables_version % 10000) / 100,
(iptables_version % 100),
(MIN_IPTABLES_VERSION / 10000),
(MIN_IPTABLES_VERSION % 10000) / 100,
(MIN_IPTABLES_VERSION % 100)
);
return -1;
}
/* Set up packet marking methods */
rc |= _iptables_init_marks();
rc |= _iptables_check_mark_masking();
/*
*
**************************************
* Set up mangle table chains and rules
*
*/
/* Create new chains in the mangle table */
rc |= iptables_do_command("-t mangle -N " CHAIN_TRUSTED); /* for marking trusted packets */
rc |= iptables_do_command("-t mangle -N " CHAIN_BLOCKED); /* for marking blocked packets */
rc |= iptables_do_command("-t mangle -N " CHAIN_ALLOWED); /* for marking allowed packets */
rc |= iptables_do_command("-t mangle -N " CHAIN_INCOMING); /* for counting incoming packets */
rc |= iptables_do_command("-t mangle -N " CHAIN_OUTGOING); /* for marking authenticated packets, and for counting outgoing packets */
/* Assign jumps to these new chains */
rc |= iptables_do_command("-t mangle -I PREROUTING 1 -i %s -s %s -j " CHAIN_OUTGOING, gw_interface, gw_iprange);
rc |= iptables_do_command("-t mangle -I PREROUTING 2 -i %s -s %s -j " CHAIN_BLOCKED, gw_interface, gw_iprange);
rc |= iptables_do_command("-t mangle -I PREROUTING 3 -i %s -s %s -j " CHAIN_TRUSTED, gw_interface, gw_iprange);
rc |= iptables_do_command("-t mangle -I POSTROUTING 1 -o %s -d %s -j " CHAIN_INCOMING, gw_interface, gw_iprange);
/* Rules to mark as trusted MAC address packets in mangle PREROUTING */
for (; pt != NULL; pt = pt->next) {
rc |= iptables_trust_mac(pt->mac);
}
/* Rules to mark as blocked MAC address packets in mangle PREROUTING */
if (MAC_BLOCK == macmechanism) {
/* with the MAC_BLOCK mechanism,
* MAC's on the block list are marked as blocked;
* everything else passes */
for (; pb != NULL; pb = pb->next) {
rc |= iptables_block_mac(pb->mac);
}
} else if (MAC_ALLOW == macmechanism) {
/* with the MAC_ALLOW mechanism,
* MAC's on the allow list pass;
* everything else is to be marked as blocked */
// So, append at end of chain a rule to mark everything blocked
rc |= iptables_do_command("-t mangle -A " CHAIN_BLOCKED " -j MARK %s 0x%x", markop, FW_MARK_BLOCKED);
// But insert at beginning of chain rules to pass allowed MAC's
for (; pa != NULL; pa = pa->next) {
rc |= iptables_allow_mac(pa->mac);
}
} else {
debug(LOG_ERR, "Unknown MAC mechanism: %d", macmechanism);
rc = -1;
}
/* Set up for traffic control */
if (traffic_control) {
rc |= tc_init_tc();
}
/*
* End of mangle table chains and rules
**************************************
*/
/*
*
**************************************
* Set up nat table chains and rules (ip4 only)
*
*/
if (!config->ip6) {
/* Create new chains in nat table */
rc |= iptables_do_command("-t nat -N " CHAIN_OUTGOING);
/*
* nat PREROUTING chain
*/
// packets coming in on gw_interface jump to CHAIN_OUTGOING
rc |= iptables_do_command("-t nat -I PREROUTING -i %s -s %s -j " CHAIN_OUTGOING, gw_interface, gw_iprange);
// CHAIN_OUTGOING, packets marked TRUSTED ACCEPT
rc |= iptables_do_command("-t nat -A " CHAIN_OUTGOING " -m mark --mark 0x%x%s -j RETURN", FW_MARK_TRUSTED, markmask);
// CHAIN_OUTGOING, packets marked AUTHENTICATED ACCEPT
rc |= iptables_do_command("-t nat -A " CHAIN_OUTGOING " -m mark --mark 0x%x%s -j RETURN", FW_MARK_AUTHENTICATED, markmask);
// CHAIN_OUTGOING, append the "preauthenticated-users" ruleset
rc |= _iptables_append_ruleset("nat", "preauthenticated-users", CHAIN_OUTGOING);
// Allow access to remote FAS - CHAIN_OUTGOING and CHAIN_TO_INTERNET packets for remote FAS, ACCEPT
if (fas_port && strcmp(fas_remoteip, gw_ip)) {
rc |= iptables_do_command("-t nat -A " CHAIN_OUTGOING " -p tcp --destination %s --dport %d -j ACCEPT", fas_remoteip, fas_port);
}
// CHAIN_OUTGOING, packets for tcp port 80, redirect to gw_port on primary address for the iface
rc |= iptables_do_command("-t nat -A " CHAIN_OUTGOING " -p tcp --dport 80 -j DNAT --to-destination %s", gw_address);
// CHAIN_OUTGOING, other packets ACCEPT
rc |= iptables_do_command("-t nat -A " CHAIN_OUTGOING " -j ACCEPT");
}
/*
* End of nat table chains and rules (ip4 only)
**************************************
*/
/*
*
**************************************
* Set up filter table chains and rules
*
*/
// Create new chains in the filter table
rc |= iptables_do_command("-t filter -N " CHAIN_TO_INTERNET);
rc |= iptables_do_command("-t filter -N " CHAIN_TO_ROUTER);
rc |= iptables_do_command("-t filter -N " CHAIN_AUTHENTICATED);
rc |= iptables_do_command("-t filter -N " CHAIN_TRUSTED);
rc |= iptables_do_command("-t filter -N " CHAIN_TRUSTED_TO_ROUTER);
/*
* filter INPUT chain
*/
// packets coming in on gw_interface jump to CHAIN_TO_ROUTER
rc |= iptables_do_command("-t filter -I INPUT -i %s -s %s -j " CHAIN_TO_ROUTER, gw_interface, gw_iprange);
// CHAIN_TO_ROUTER packets marked BLOCKED DROP
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -m mark --mark 0x%x%s -j DROP", FW_MARK_BLOCKED, markmask);
// CHAIN_TO_ROUTER, invalid packets DROP
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -m conntrack --ctstate INVALID -j DROP");
// CHAIN_TO_ROUTER, related and established packets ACCEPT
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
// CHAIN_TO_ROUTER, bogus SYN packets DROP
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -p tcp --tcp-flags SYN SYN \\! --tcp-option 2 -j DROP");
// CHAIN_TO_ROUTER, packets to HTTP listening on gw_port on router ACCEPT
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -p tcp --dport %d -j ACCEPT", gw_port);
// CHAIN_TO_ROUTER, packets to HTTP listening on fas_port on router ACCEPT
if (fas_port && !strcmp(fas_remoteip, gw_ip)) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -p tcp --dport %d -j ACCEPT", fas_port);
}
// CHAIN_TO_ROUTER, packets marked TRUSTED:
/* if trusted-users-to-router ruleset is empty:
* use empty ruleset policy
* else:
* jump to CHAIN_TRUSTED_TO_ROUTER, and load and use users-to-router ruleset
*/
if (is_empty_ruleset("trusted-users-to-router")) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -m mark --mark 0x%x%s -j %s", FW_MARK_TRUSTED, markmask, get_empty_ruleset_policy("trusted-users-to-router"));
} else {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -m mark --mark 0x%x%s -j " CHAIN_TRUSTED_TO_ROUTER, FW_MARK_TRUSTED, markmask);
// CHAIN_TRUSTED_TO_ROUTER, related and established packets ACCEPT
rc |= iptables_do_command("-t filter -A " CHAIN_TRUSTED_TO_ROUTER " -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
// CHAIN_TRUSTED_TO_ROUTER, append the "trusted-users-to-router" ruleset
rc |= _iptables_append_ruleset("filter", "trusted-users-to-router", CHAIN_TRUSTED_TO_ROUTER);
// CHAIN_TRUSTED_TO_ROUTER, any packets not matching that ruleset REJECT
rc |= iptables_do_command("-t filter -A " CHAIN_TRUSTED_TO_ROUTER " -j REJECT --reject-with %s-port-unreachable", ICMP_TYPE);
}
// CHAIN_TO_ROUTER, other packets:
/* if users-to-router ruleset is empty:
* use empty ruleset policy
* else:
* load and use users-to-router ruleset
*/
if (is_empty_ruleset("users-to-router")) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -j %s", get_empty_ruleset_policy("users-to-router"));
} else {
/* CHAIN_TO_ROUTER, append the "users-to-router" ruleset */
rc |= _iptables_append_ruleset("filter", "users-to-router", CHAIN_TO_ROUTER);
/* everything else, REJECT */
rc |= iptables_do_command("-t filter -A " CHAIN_TO_ROUTER " -j REJECT --reject-with %s-port-unreachable", ICMP_TYPE);
}
/*
* filter FORWARD chain
*/
// packets coming in on gw_interface jump to CHAIN_TO_INTERNET
rc |= iptables_do_command("-t filter -I FORWARD -i %s -s %s -j " CHAIN_TO_INTERNET, gw_interface, gw_iprange);
// CHAIN_TO_INTERNET packets marked BLOCKED DROP
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m mark --mark 0x%x%s -j DROP", FW_MARK_BLOCKED, markmask);
// CHAIN_TO_INTERNET, invalid packets DROP
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m conntrack --ctstate INVALID -j DROP");
// CHAIN_TO_INTERNET, deal with MSS
if (set_mss) {
/* XXX this mangles, so 'should' be done in the mangle POSTROUTING chain.
* However OpenWRT standard S35firewall does it in filter FORWARD,
* and since we are pre-empting that chain here, we put it in */
if (mss_value > 0) { /* set specific MSS value */
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", mss_value);
} else { /* allow MSS as large as possible */
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu");
}
}
// Allow access to remote FAS - CHAIN_OUTGOING and CHAIN_TO_INTERNET packets for remote FAS, ACCEPT
if (fas_port && strcmp(fas_remoteip, gw_ip)) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -p tcp --destination %s --dport %d -j ACCEPT", fas_remoteip, fas_port);
}
/* CHAIN_TO_INTERNET, packets marked TRUSTED: */
/* if trusted-users ruleset is empty:
* use empty ruleset policy
* else:
* jump to CHAIN_TRUSTED, and load and use trusted-users ruleset
*/
if (is_empty_ruleset("trusted-users")) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m mark --mark 0x%x%s -j %s", FW_MARK_TRUSTED, markmask, get_empty_ruleset_policy("trusted-users"));
} else {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m mark --mark 0x%x%s -j " CHAIN_TRUSTED, FW_MARK_TRUSTED, markmask);
// CHAIN_TRUSTED, related and established packets ACCEPT
rc |= iptables_do_command("-t filter -A " CHAIN_TRUSTED " -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
// CHAIN_TRUSTED, append the "trusted-users" ruleset
rc |= _iptables_append_ruleset("filter", "trusted-users", CHAIN_TRUSTED);
// CHAIN_TRUSTED, any packets not matching that ruleset REJECT
rc |= iptables_do_command("-t filter -A " CHAIN_TRUSTED " -j REJECT --reject-with %s-port-unreachable", ICMP_TYPE);
}
/* CHAIN_TO_INTERNET, packets marked AUTHENTICATED: */
/* if authenticated-users ruleset is empty:
* use empty ruleset policy
* else:
* jump to CHAIN_AUTHENTICATED, and load and use authenticated-users ruleset
*/
if (is_empty_ruleset("authenticated-users")) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m mark --mark 0x%x%s -j %s", FW_MARK_AUTHENTICATED, markmask, get_empty_ruleset_policy("authenticated-users"));
} else {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -m mark --mark 0x%x%s -j " CHAIN_AUTHENTICATED, FW_MARK_AUTHENTICATED, markmask);
// CHAIN_AUTHENTICATED, related and established packets ACCEPT
rc |= iptables_do_command("-t filter -A " CHAIN_AUTHENTICATED " -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT");
// CHAIN_AUTHENTICATED, append the "authenticated-users" ruleset
rc |= _iptables_append_ruleset("filter", "authenticated-users", CHAIN_AUTHENTICATED);
// CHAIN_AUTHENTICATED, any packets not matching that ruleset REJECT
rc |= iptables_do_command("-t filter -A " CHAIN_AUTHENTICATED " -j REJECT --reject-with %s-port-unreachable", ICMP_TYPE);
}
/* CHAIN_TO_INTERNET, other packets: */
/* if preauthenticated-users ruleset is empty:
* use empty ruleset policy
* else:
* load and use authenticated-users ruleset
*/
if (is_empty_ruleset("preauthenticated-users")) {
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -j %s ", get_empty_ruleset_policy("preauthenticated-users"));
} else {
rc |= _iptables_append_ruleset("filter", "preauthenticated-users", CHAIN_TO_INTERNET);
}
// CHAIN_TO_INTERNET, all other packets REJECT
rc |= iptables_do_command("-t filter -A " CHAIN_TO_INTERNET " -j REJECT --reject-with %s-port-unreachable", ICMP_TYPE);
/*
* End of filter table chains and rules
**************************************
*/
free(gw_interface);
free(gw_iprange);
free(gw_ip);
free(gw_address);
free(fas_remoteip);
return rc;
}
/** Remove the firewall rules
* This is used when we do a clean shutdown of nodogsplash,
* and when it starts, to make sure there are no rules left over from a crash
*/
int
iptables_fw_destroy(void)
{
fw_quiet = 1;
s_config *config;
int traffic_control;
LOCK_CONFIG();
config = config_get_config();
traffic_control = config->traffic_control;
UNLOCK_CONFIG();
if (traffic_control) {
debug(LOG_DEBUG, "Destroying our tc hooks");
tc_destroy_tc();
}
debug(LOG_DEBUG, "Destroying our iptables entries");
/* Everything in the mangle table */
debug(LOG_DEBUG, "Destroying chains in the MANGLE table");
iptables_fw_destroy_mention("mangle", "PREROUTING", CHAIN_TRUSTED);
iptables_fw_destroy_mention("mangle", "PREROUTING", CHAIN_BLOCKED);
iptables_fw_destroy_mention("mangle", "PREROUTING", CHAIN_ALLOWED);
iptables_fw_destroy_mention("mangle", "PREROUTING", CHAIN_OUTGOING);
iptables_fw_destroy_mention("mangle", "POSTROUTING", CHAIN_INCOMING);
iptables_do_command("-t mangle -F " CHAIN_TRUSTED);
iptables_do_command("-t mangle -F " CHAIN_BLOCKED);
iptables_do_command("-t mangle -F " CHAIN_ALLOWED);
iptables_do_command("-t mangle -F " CHAIN_OUTGOING);
iptables_do_command("-t mangle -F " CHAIN_INCOMING);
iptables_do_command("-t mangle -X " CHAIN_TRUSTED);
iptables_do_command("-t mangle -X " CHAIN_BLOCKED);
iptables_do_command("-t mangle -X " CHAIN_ALLOWED);
iptables_do_command("-t mangle -X " CHAIN_OUTGOING);
iptables_do_command("-t mangle -X " CHAIN_INCOMING);
/* Everything in the nat table (ip4 only) */
if (!config->ip6) {
debug(LOG_DEBUG, "Destroying chains in the NAT table");
iptables_fw_destroy_mention("nat", "PREROUTING", CHAIN_OUTGOING);
iptables_do_command("-t nat -F " CHAIN_OUTGOING);
iptables_do_command("-t nat -X " CHAIN_OUTGOING);
}
/* Everything in the filter table */
debug(LOG_DEBUG, "Destroying chains in the FILTER table");
iptables_fw_destroy_mention("filter", "INPUT", CHAIN_TO_ROUTER);
iptables_fw_destroy_mention("filter", "FORWARD", CHAIN_TO_INTERNET);
iptables_do_command("-t filter -F " CHAIN_TO_ROUTER);
iptables_do_command("-t filter -F " CHAIN_TO_INTERNET);
iptables_do_command("-t filter -F " CHAIN_AUTHENTICATED);
iptables_do_command("-t filter -F " CHAIN_TRUSTED);
iptables_do_command("-t filter -F " CHAIN_TRUSTED_TO_ROUTER);
iptables_do_command("-t filter -X " CHAIN_TO_ROUTER);
iptables_do_command("-t filter -X " CHAIN_TO_INTERNET);
iptables_do_command("-t filter -X " CHAIN_AUTHENTICATED);
iptables_do_command("-t filter -X " CHAIN_TRUSTED);
iptables_do_command("-t filter -X " CHAIN_TRUSTED_TO_ROUTER);
fw_quiet = 0;
return 0;
}
/*
* Helper for iptables_fw_destroy
* @param table The table to search
* @param chain The chain in that table to search
* @param mention A word to find and delete in rules in the given table+chain
*/
int
iptables_fw_destroy_mention(
const char *table,
const char *chain,
const char *mention
)
{
s_config *config;
char *iptables;
FILE *p = NULL;
char *command = NULL;
char *command2 = NULL;
char line[MAX_BUF];
char rulenum[10];
int retval = -1;
debug(LOG_DEBUG, "Checking all mention of %s in chain %s of table %s", mention, chain, table);
config = config_get_config();
iptables = config->ip6 ? "ip6tables" : "iptables";
safe_asprintf(&command, "%s -t %s -L %s -n --line-numbers -v", iptables, table, chain);
if ((p = popen(command, "r"))) {
/* Skip first 2 lines */
while (!feof(p) && fgetc(p) != '\n');
while (!feof(p) && fgetc(p) != '\n');
/* Loop over entries */
while (fgets(line, sizeof(line), p)) {
/* Look for mention */
if (strstr(line, mention)) {
/* Found mention - Get the rule number into rulenum*/
if (sscanf(line, "%9[0-9]", rulenum) == 1) {
/* Delete the rule: */
debug(LOG_DEBUG, "Deleting rule %s from %s.%s because it mentions %s", rulenum, table, chain, mention);
safe_asprintf(&command2, "-t %s -D %s %s", table, chain, rulenum);
iptables_do_command(command2);
free(command2);
retval = 0;
/* Do not keep looping - the captured rulenums will no longer be accurate */
break;
}
}
}
pclose(p);
}
free(command);
if (retval == 0) {
/* Recurse just in case there are more in the same table+chain */
iptables_fw_destroy_mention(table, chain, mention);
}
return (retval);
}
/** Insert or delete firewall mangle rules marking a client's packets.
*/
int
iptables_fw_authenticate(t_client *client)
{
int rc = 0, download_limit, upload_limit, traffic_control;
s_config *config;
char upload_ifbname[16];
config = config_get_config();
sprintf(upload_ifbname, "ifb%d", config->upload_ifb);
LOCK_CONFIG();
traffic_control = config->traffic_control;
download_limit = config->download_limit;
upload_limit = config->upload_limit;
UNLOCK_CONFIG();
if ((client->download_limit > 0) && (client->upload_limit > 0)) {
download_limit = client->download_limit;
upload_limit = client->upload_limit;
}
debug(LOG_NOTICE, "Authenticating %s %s", client->ip, client->mac);
/* This rule is for marking upload (outgoing) packets, and for upload byte counting */
rc |= iptables_do_command("-t mangle -A " CHAIN_OUTGOING " -s %s -m mac --mac-source %s -j MARK %s 0x%x", client->ip, client->mac, markop, FW_MARK_AUTHENTICATED);
rc |= iptables_do_command("-t mangle -A " CHAIN_INCOMING " -d %s -j MARK %s 0x%x", client->ip, markop, FW_MARK_AUTHENTICATED);
/* This rule is just for download (incoming) byte counting, see iptables_fw_counters_update() */
rc |= iptables_do_command("-t mangle -A " CHAIN_INCOMING " -d %s -j ACCEPT", client->ip);
if (traffic_control) {
rc |= tc_attach_client(config->gw_interface, download_limit, upload_ifbname, upload_limit, client->id, client->ip);
}
return rc;
}
int
iptables_fw_deauthenticate(t_client *client)
{
int download_limit, upload_limit, traffic_control;
s_config *config;
char upload_ifbname[16];
int rc = 0;
config = config_get_config();
sprintf(upload_ifbname, "ifb%d", config->upload_ifb);
LOCK_CONFIG();
traffic_control = config->traffic_control;
download_limit = config->download_limit;
upload_limit = config->upload_limit;
UNLOCK_CONFIG();
if ((client->download_limit > 0) && (client->upload_limit > 0)) {
download_limit = client->download_limit;
upload_limit = client->upload_limit;
}
/* Remove the authentication rules. */
debug(LOG_NOTICE, "Deauthenticating %s %s", client->ip, client->mac);
rc |= iptables_do_command("-t mangle -D " CHAIN_OUTGOING " -s %s -m mac --mac-source %s -j MARK %s 0x%x", client->ip, client->mac, markop, FW_MARK_AUTHENTICATED);
rc |= iptables_do_command("-t mangle -D " CHAIN_INCOMING " -d %s -j MARK %s 0x%x", client->ip, markop, FW_MARK_AUTHENTICATED);
rc |= iptables_do_command("-t mangle -D " CHAIN_INCOMING " -d %s -j ACCEPT", client->ip);
if (traffic_control) {
rc |= tc_detach_client(config->gw_interface, download_limit, upload_ifbname, upload_limit, client->id);
}
return rc;
}
/** Return the total upload usage in bytes */
unsigned long long int
iptables_fw_total_upload()
{
FILE *output;
const char *script;
char target[MAX_BUF];
int rc;
unsigned long long int counter;
/* Look for outgoing traffic */
script = "iptables -v -n -x -t mangle -L PREROUTING";
output = popen(script, "r");
if (!output) {
debug(LOG_ERR, "popen(): %s", strerror(errno));
return 0;
}
/* skip the first two lines */
while (('\n' != fgetc(output)) && !feof(output)) {}
while (('\n' != fgetc(output)) && !feof(output)) {}
while (!feof(output)) {
rc = fscanf(output, "%*d %llu %s ", &counter, target);
if (2 == rc && !strcmp(target,CHAIN_OUTGOING)) {
debug(LOG_DEBUG, "Total outgoing Bytes=%llu", counter);
pclose(output);
return counter;
}
/* eat rest of line */
while (('\n' != fgetc(output)) && !feof(output)) {}
}
pclose(output);
debug(LOG_WARNING, "Can't find target %s in mangle table", CHAIN_OUTGOING);
return 0;
}
/** Return the total download usage in bytes */
unsigned long long int
iptables_fw_total_download()
{
FILE *output;
const char *script;
char target[MAX_BUF];
int rc;
unsigned long long int counter;
/* Look for incoming traffic */
script = "iptables -v -n -x -t mangle -L POSTROUTING";
output = popen(script, "r");
if (!output) {
debug(LOG_ERR, "popen(): %s", strerror(errno));
return 0;
}
/* skip the first two lines */
while (('\n' != fgetc(output)) && !feof(output)) {}
while (('\n' != fgetc(output)) && !feof(output)) {}
while (!feof(output)) {
rc = fscanf(output, "%*s %llu %s ", &counter, target);
if (2 == rc && !strcmp(target, CHAIN_INCOMING)) {
debug(LOG_DEBUG, "Total incoming Bytes=%llu", counter);
pclose(output);
return counter;
}
/* eat rest of line */
while (('\n' != fgetc(output)) && !feof(output)) {}
}
pclose(output);
debug(LOG_WARNING, "Can't find target %s in mangle table", CHAIN_INCOMING);
return 0;
}
/** Update the counters of all the clients in the client list */
int
iptables_fw_counters_update(void)
{
FILE *output;
char *script;
char ip[INET6_ADDRSTRLEN];
char target[MAX_BUF];
int rc;
int af;
s_config *config;
unsigned long long int counter;
t_client *p1;
struct sockaddr_storage tempaddr;