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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
|
<?php
/* $Id$ */
/*
filter.inc
Copyright (C) 2004 Scott Ullrich
Copyright (C) 2005 Bill Marquette
All rights reserved.
originally part of m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* include all configuration functions */
require_once("functions.inc");
require_once("pkg-utils.inc");
require_once("notices.inc");
require_once("shaper.inc");
function filter_pflog_start() {
global $config, $g;
mute_kernel_msgs();
mwexec("/sbin/ifconfig pflog0 up && pflogd");
mwexec_bg("/usr/sbin/tcpdump -l -n -e -ttt -i pflog0 | logger -t pf -p local0.info");
unmute_kernel_msgs();
}
/* reload filter async */
function filter_configure() {
global $g;
touch($g['tmp_path'] . "/filter_dirty");
}
/* reload filter sync */
function filter_configure_sync() {
global $config, $g;
unlink_if_exists("{$g['tmp_path']}/filter_dirty");
unlink_if_exists("{$g['tmp_path']}/config.cache");
$lan_if = $config['interfaces']['lan']['if'];
$wan_if = get_real_wan_interface();
/* generate aliases */
if($g['booting'] == true) echo " aliases ";
$aliases = filter_generate_aliases();
/* generate nat rules */
if($g['booting'] == true) echo " rules ";
$natrules = filter_nat_rules_generate();
/* generate pfctl rules */
if($g['booting'] == true) echo " filtering ";
$pfrules = filter_rules_generate();
if(isset($config['shaper']['enable'])) {
/* generate altq interface setup parms */
if($g['booting'] == true) echo " altq-interfaces ";
$altq_ints = filter_setup_altq_interfaces();
/* generate altq queues */
if($g['booting'] == true) echo " altq-queues ";
$altq_queues = filter_generate_altq_queues($altq_ints);
/* generate altq rules */
if($g['booting'] == true) echo " altq-rules ";
$pf_altq_rules = filter_generate_pf_altq_rules();
}
/* enable pf if we need to, otherwise disable */
if( !isset( $config['system']['disablefilter'] ) ) {
mwexec("/sbin/pfctl -e");
} else {
mwexec("/sbin/pfctl -d");
return;
}
/* get our wan interface? */
$wanif = get_real_wan_interface();
/* set up MSS clamping */
if ($config['interfaces']['wan']['mtu'] <> "" and is_numeric($config['interfaces']['wan']['mtu']))
$mssclamp = "max-mss " . (intval($config['interfaces']['wan']['mtu'] - 40));
else if ($config['interfaces']['wan']['ipaddr'] == "pppoe")
$mssclamp = "max-mss 1452";
else
$mssclamp = "";
$fd = fopen("{$g['tmp_path']}/rules.debug", "w");
$rules = $aliases . " \n";
$rules .= setup_logging_interfaces();
if($config['system']['optimization'] <> "")
$rules.= "set optimization {$config['system']['optimization']}\n";
else
$rules.= "set optimization normal\n";
if($config['system']['maximumstates'] <> "" && is_numeric($config['system']['maximumstates'])) {
/* User defined maximum states in Advanced menu. */
$rules.= "set limit states {$config['system']['maximumstates']}\n";
}
$rules.= "\n";
$rules.= "scrub on {$wanif} all {$mssclamp}\n"; // reassemble all directions
/* loop through optional interfaces. if a gateway is set, lets scrub em down! */
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
if($config['interfaces']["opt" . $j]['gateway'] <> "") {
$if = convert_friendly_interface_to_real_interface_name("opt{$j}");
$rules.= "scrub on {$if} all {$mssclamp}\n"; // reassemble all directions
}
}
$rules.= "{$altq_ints}\n";
$rules.= "{$altq_queues}\n";
$rules.= "{$natrules}\n";
$rules.= "{$pf_altq_rules}\n";
$rules.= "{$pfrules}\n";
fwrite($fd, $rules);
fclose($fd);
$rules_loading = mwexec("/sbin/pfctl -f {$g['tmp_path']}/rules.debug");
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue'])) {
foreach ($config['shaper']['queue'] as $queue) {
mwexec("/sbin/pfctl -a {$queue['name']} -f {$g['tmp_path']}/{$queue['name']}.rules");
}
}
/* check for a error while loading the rules file. if an error has occured
then output the contents of the error to the caller */
if($rules_loading <> 0) {
$rules_error = exec_command("/sbin/pfctl -f {$g['tmp_path']}/rules.debug");
$line_error = split("\:", $rules_error);
$line_number = $line_error[1];
$rules_file = `/bin/cat {$g['tmp_path']}/rules.debug`;
$line_split = split("\n", $rules_file);
if(is_array($line_split))
$line_error = "The line in question reads [{$line_number}]: {$line_split[$line_number-1]}";
file_notice("filter_load", "There were error(s) loading the rules: {$rules_error} {$line_error}", "Filter Reload", "");
log_error("There were error(s) loading the rules: {$rules_error} - {$line_error}");
return;
}
unlink_if_exists("/usr/local/pkg/pf/carp_sync_client.php");
/* process packager manager custom rules */
$files = return_dir_as_array("/usr/local/pkg/pf/");
if($files <> "") {
foreach ($files as $file) {
if($file <> "") {
$text = file_get_contents("/usr/local/pkg/pf/" . $file);
if($text) {
if(stristr($file, ".sh") == true) {
mwexec("/usr/local/pkg/pf/" . $file . " start");
} else {
if(stristr($file,"CVS") == false) {
if($g['booting'] == true)
echo "\t{$file}... ";
eval($text);
if($g['booting'] == true)
echo "done.\n";
}
}
}
}
}
}
/* sync carp entries to other firewalls */
carp_sync_client();
/*
we need a way to let a user run a shell cmd after each
filter_configure() call. run this xml command after
each change.
*/
if($config['system']['afterfilterchangeshellcmd'] <> "") {
mwexec($config['system']['afterfilterchangeshellcmd']);
}
return 0;
}
function filter_get_rule_interface($associatedrulenumber) {
global $config;
foreach ($config['shaper']['rule'] as $rule) {
if($rule['descr'] == $associatedrule) return $rule['interface'];
}
return $config['shaper']['rule'][$associatedrulenumber]['interface'];
}
function filter_get_rule_real_interface($associatedrulenumber) {
global $config;
$x=0;
foreach($config['shaper']['rule'] as $rule) {
if($x == $associatedrulenumber)
return $rule['interface'];
$x++;
}
}
function filter_generate_aliases() {
global $config;
$aliases = "";
$i = 0;
$lanip = find_interface_ip($config['interfaces']['lan']['if']);
$wanip = find_interface_ip(get_real_wan_interface());
$lan_aliases = " " . link_ip_to_carp_interface($lanip);
$wan_aliases = " " . link_ip_to_carp_interface($wanip);
$aliases .= "# System Aliases \n";
$aliases .= "lan = \"{ {$config['interfaces']['lan']['if']}{$lan_aliases} }\"\n";
$aliases .= "wan = \"{ " . get_real_wan_interface() . "{$wan_aliases} }\"\n";
$aliases .= "pptp = \"{ ng1 ng2 ng3 ng4 ng5 ng6 ng7 ng8 ng9 ng10 ng11 ng12 ng13 ng14 }\"\n";
$aliases .= "pppoe = \"{ ng1 ng2 ng3 ng4 ng5 ng6 ng7 ng8 ng9 ng10 ng11 ng12 ng13 ng14 }\"\n";
$ifdescrs = array();
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
$ifdescrs['opt' . $j] = "opt" . $j;
}
$bridgetracker = 0;
foreach ($ifdescrs as $ifdescr => $ifname) {
/* do not process tun interfaces */
if(stristr(filter_opt_interface_to_real($ifname), "tun") == true) continue;
$aliases .= filter_get_opt_interface_descr($ifname) . " = \"{ " . filter_opt_interface_to_real($ifname);
if($config['interfaces'][$ifname]['bridge'] <> "") {
$aliases .= " bridge{$bridgetracker} ";
$bridgetracker++;
}
$aliases .= " }\"\n";
}
$aliases .= "# User Aliases \n";
/* Setup pf groups */
if (isset($config['aliases']['alias'])) {
foreach ($config['aliases']['alias'] as $alias) {
$extraalias = "";
$ip = find_interface_ip($alias['address']);
$extraalias = " " . link_ip_to_carp_interface($ip);
$aliases .= "{$alias['name']} = \"{ {$alias['address']}{$extralias} }\"\n";
}
}
return $aliases;
}
function generate_optcfg_array(& $optcfg) {
global $config;
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
$oc = $config['interfaces']['opt' . $i];
if (isset($oc['enable']) && $oc['if']) {
$oic = array();
$oic['if'] = $oc['if'];
if ($oc['bridge']) {
if (!strstr($oc['bridge'], "opt") ||
isset($config['interfaces'][$oc['bridge']]['enable'])) {
if (is_ipaddr($config['interfaces'][$oc['bridge']]['ipaddr'])) {
$oic['ip'] = $config['interfaces'][$oc['bridge']]['ipaddr'];
$oic['sn'] = $config['interfaces'][$oc['bridge']]['subnet'];
$oic['sa'] = gen_subnet($oic['ip'], $oic['sn']);
}
}
$oic['bridge'] = 1;
} else {
$oic['ip'] = $oc['ipaddr'];
$oic['sn'] = $oc['subnet'];
$oic['sa'] = gen_subnet($oic['ip'], $oic['sn']);
}
$optcfg['opt' . $i] = $oic;
}
}
}
function filter_flush_nat_table() {
global $config, $g;
return mwexec("/sbin/pfctl -F nat");
}
function filter_flush_state_table() {
global $config, $g;
return mwexec("/sbin/pfctl -F state");
}
function filter_nat_rules_generate_if($if, $src, $srcport, $dst, $natport, $target, $nonat) {
global $config;
if($src=="/32")
return;
if($src{0} == "/")
return;
$nat = "nat";
if ($target)
$tgt = $target . "/32";
else
$tgt = "($if)";
if($nonat == true) {
$nat = "no nat";
$second_half_of_rule = "";
} else {
$second_half_of_rule = " -> {$tgt}";
}
if ($dst == "")
$dst = "any";
if ($srcport)
$src .= " port " . $srcport;
if ($natport)
$tgt .= " port " . $natport;
$natrule = "{$nat} on {$if} from {$src} to {$dst} {$second_half_of_rule}\n";
return $natrule;
}
function is_one_to_one_or_server_nat_rule($iptocheck) {
global $config;
if($config['nat']['onetoone'] <> "")
foreach($config['nat']['onetoone'] as $onetoone) {
if(ip_in_subnet($iptocheck,$onetoone['internal']."/".$onetoone['subnet']) == true)
return true;
if($onetoone['internal'] == $target)
return true;
}
if($config['nat']['servernat'] <> "")
foreach($config['nat']['servernat'] as $onetoone) {
$int = explode("/", $onetoone['ipaddr']);
if(ip_in_subnet($iptocheck,$onetoone['ipaddr']."/".$onetoone['subnet']) == true)
return true;
if($onetoone['ipaddr'] == $target)
return true;
}
if($config['nat']['rule'] <> "")
foreach($config['nat']['rule'] as $onetoone) {
$int = explode("/", $onetoone['target']);
if(ip_in_subnet($iptocheck,$onetoone['target']."/".$onetoone['subnet']) == true)
return true;
if($onetoone['target'] == $target)
return true;
}
return FALSE;
}
function filter_nat_rules_generate() {
global $config, $g;
$wancfg = $config['interfaces']['wan'];
$lancfg = $config['interfaces']['lan'];
$pptpdcfg = $config['pptpd'];
$pppoecfg = $config['pppoe'];
$wanif = get_real_wan_interface();
$lanif = $config['interfaces']['lan']['if'];
$lanip = $config['interfaces']['lan']['ipaddr'];
$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
$natrules = "";
$natrules .= "nat-anchor \"pftpx/*\"\n";
$natrules .= "nat-anchor \"natearly/*\"\n";
$natrules .= "nat-anchor \"natrules/*\"\n";
/* any 1:1 mappings? */
if (is_array($config['nat']['onetoone'])) {
foreach ($config['nat']['onetoone'] as $natent) {
if (!is_numeric($natent['subnet']))
$sn = 32;
else
$sn = $natent['subnet'];
if (!$natent['interface'] || ($natent['interface'] == "wan"))
$natif = $wanif;
else
$natif = $config['interfaces'][$natent['interface']]['if'];
$natrules .= "binat on {$natif} from {$natent['internal']}/{$sn} to any -> {$natent['external']}/{$sn}\n";
}
}
/* outbound rules - advanced or standard */
if (isset($config['nat']['advancedoutbound']['enable'])) {
/* advanced outbound rules */
if (is_array($config['nat']['advancedoutbound']['rule'])) {
foreach ($config['nat']['advancedoutbound']['rule'] as $obent) {
$src = $obent['source']['network'];
if (isset($obent['destination']['not']) && !isset($obent['destination']['any']))
$dst = "!" . $obent['destination']['address'];
else
$dst = $obent['destination']['address'];
if (!$obent['interface'] || ($obent['interface'] == "wan"))
$natif = $wanif;
else
$natif = $config['interfaces'][$obent['interface']]['if'];
$natrules .= filter_nat_rules_generate_if($natif, $src, $obent['sourceport'], $dst, $obent['natport'],
$obent['target'], isset($obent['nonat']));
}
}
} else {
/* standard outbound rules (one for each interface) */
/* create ipsec passthru rule if requested */
if (isset($config['nat']['ipsecpassthru']['enable'])) {
$natrules .= filter_nat_rules_generate_if($wanif,
"{$lansa}/{$lancfg['subnet']}", 500, "", 500, null, false);
}
$natrules .= filter_nat_rules_generate_if($wanif,
"{$lansa}/{$lancfg['subnet']}", null, "", null, null, false);
/* optional interfaces */
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
$optcfg = $config['interfaces']['opt' . $i];
if (isset($optcfg['enable']) && !$optcfg['bridge']) {
$optsa = gen_subnet($optcfg['ipaddr'], $optcfg['subnet']);
/* setup nat mappings for lan -> opt[$i]
* interface if a gateway is defined
*/
if($optcfg['gateway'] <> "")
$natrules .= filter_nat_rules_generate_if($optcfg['if'],
"{$lansa}/{$lancfg['subnet']}", null, "", null, null, isset($optcfg['nonat']));
$natrules .= filter_nat_rules_generate_if($wanif,
"{$optsa}/{$optcfg['subnet']}", null, "", null, null, isset($optcfg['nonat']));
}
}
/* PPTP subnet */
if ($pptpdcfg['mode'] == "server") {
$pptp_subnet = $g['pptp_subnet'];
if($config['pptp']['pptp_subnet'] <> "")
$pptp_subnet = $config['pptp']['pptp_subnet'];
$natrules .= filter_nat_rules_generate_if($wanif,
"{$pptpdcfg['remoteip']}/{$pptp_subnet}", null, "", null, null, "");
}
/* PPPoE subnet */
if ($pppoecfg['mode'] == "server") {
$pppoe_subnet = $g['pppoe_subnet'];
if($config['pppoe']['pppoe_subnet'] <> "")
$pppoe_subnet = $config['pppoe']['pppoe_subnet'];
$natrules .= filter_nat_rules_generate_if($wanif,
"{$pppoecfg['remoteip']}/{$pppoe_subnet}", null, "", null, null, "");
}
}
$natrules .= "#SSH Lockout Table\n";
$natrules .= "table <sshlockout> persist\n\n";
$natrules .= "\n# spam table \n";
$natrules .= "table <spamd> persist\n\n";
/* load balancer anchor */
$natrules .= "\n# Load balancing anchor - slbd updates\n";
$natrules .= "rdr-anchor \"slb\"\n";
if(!isset($config['system']['disableftpproxy'])) {
$optcfg = array();
generate_optcfg_array($optcfg);
$natrules .= "# FTP proxy\n";
$natrules .= "rdr-anchor \"pftpx/*\"\n";
$natrules .= "rdr on {$lanif} proto tcp from any to any port 21 -> 127.0.0.1 port 8021\n";
# go through optional interfaces, setting up pftpx for them as well.
foreach($optcfg as $oc) {
$natrules .= "rdr on {$oc['if']} proto tcp from any to any port 21 -> 127.0.0.1 port 8021\n";
}
$natrules .= "\n";
}
/* DIAG: add ipv6 NAT, if requested */
if (isset($config['diag']['ipv6nat']['enable']) and $config['diag']['ipv6nat']['ipaddr'] <> "") {
/* XXX: FIX ME! IPV6 */
$natrules .= "rdr on $wanif proto ipv6 from any to any -> {$config['diag']['ipv6nat']['ipaddr']}\n";
}
if (isset($config['nat']['rule'])) {
$natrules .= "# NAT Inbound Redirects\n";
foreach ($config['nat']['rule'] as $rule) {
/* if item is an alias, expand */
if(alias_expand($rule['external-port']))
$extport = alias_expand($rule['external-port']);
else
$extport = explode("-", $rule['external-port']);
/* if item is an alias, expand */
if(alias_expand($rule['local-port']))
$localport = alias_expand($rule['local-port']);
else
$localport = $rule['local-port'];
$target = alias_expand_host($rule['target']);
if (!$target)
continue; /* unresolvable alias */
if ($rule['external-address'])
if($rule['external-address'] <> "any")
$extaddr = $rule['external-address'] . "/32";
else
$extaddr = $rule['external-address'];
else
$extaddr = get_current_wan_address($rule['interface']);
if (!$rule['interface'] || ($rule['interface'] == "wan"))
$natif = $wanif;
else if($rule['interface'] == "pptp")
$natif = "pptp";
else if($rule['interface'] == "pppoe")
$natif = "pppoe";
else
$natif = $config['interfaces'][$rule['interface']]['if'];
$lanif = $lancfg['if'];
/*
* Expand aliases
* XXX: may want to integrate this into pf macros
*/
if(alias_expand($extport[0]))
$extport[0] = alias_expand($extport[0]);
if(alias_expand($extport[1]))
$extport[1] = alias_expand($extport[1]);
if(alias_expand($target))
$target = alias_expand($target);
if(alias_expand($localport))
$localport = alias_expand($localport);
if(alias_expand($extaddr))
$extaddr = alias_expand($extaddr);
if ((!$extport[1]) || ($extport[0] == $extport[1])) {
if($rule['protocol'] == "tcp/udp")
$natrules .=
"rdr on $natif proto { tcp udp } from any to {$extaddr} port {$extport[0]} -> {$target} port {$localport}";
else
$natrules .=
"rdr on $natif proto {$rule['protocol']} from any to {$extaddr} port {$extport[0]} -> {$target} port {$localport}";
} else {
if($rule['protocol'] == "tcp/udp")
$natrules .=
"rdr on $natif proto { tcp udp } from any to {$extaddr} port {$extport[0]}:{$extport[1]} -> {$target} port {$extport[0]}:*";
else
$natrules .=
"rdr on $natif proto {$rule['protocol']} from any to {$extaddr} port {$extport[0]}:{$extport[1]} -> {$target} port {$extport[0]}:*";
}
$natrules .= "\n";
}
}
if ($pptpdcfg['mode'] && $pptpdcfg['mode'] != "off") {
if ($pptpdcfg['mode'] == "server")
$pptpdtarget = "127.0.0.1";
else if ($pptpdcfg['mode'] == "redir")
$pptpdtarget = $pptpdcfg['redir'];
if ($pptpdcfg['mode'] == "redir") {
$natrules .= <<<EOD
# PPTP
rdr on $wanif proto gre from any to any -> $pptpdtarget
rdr on $wanif proto tcp from any to any port 1723 -> $pptpdtarget
EOD;
}
}
if (is_package_installed("squid") == 1)
if(is_process_running("squid"))
$natrules .= "rdr on {$lanif} inet proto tcp from any to !{$lanip} port www -> 127.0.0.1 port 3128\n";
$natrules .= process_carp_nat_rules();
return $natrules;
}
function run_command_return_string($cmd) {
$fd = popen($cmd, "r");
while(!feof($fd)) {
$tmp .= fread($fd,49);
}
fclose($fd);
return $tmp;
}
function generate_user_filter_rule_arr($rule, $ngcounter) {
$ret = array();
$line = generate_user_filter_rule($rule, $ngcounter);
$ret['rule'] = $line;
if($rule['descr'] != "" and $line != "")
$ret['descr'] = "label \"USER_RULE: {$rule['descr']}\"";
else
$ret['descr'] = "label \"USER_RULE\"";
$ret['ackq'] = get_ack_queue($rule['interface']);
return $ret;
}
function generate_user_filter_rule($rule, $ngcounter) {
global $config, $g;
$wancfg = $config['interfaces']['wan'];
$lancfg = $config['interfaces']['lan'];
$pptpdcfg = $config['pptpd'];
$pppoecfg = $config['pppoe'];
$lanif = $lancfg['if'];
$wanif = get_real_wan_interface();
$lanip = $lancfg['ipaddr'];
$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
$lansn = $lancfg['subnet'];
$optcfg = array();
generate_optcfg_array($optcfg);
/* don't include disabled rules */
if (isset($rule['disabled'])) {
return "# rule " . $rule['descr'] . " disabled \n";
}
$pptpdcfg = $config['pptpd'];
$pppoecfg = $config['pppoe'];
if ($pptpdcfg['mode'] == "server") {
$pptpip = $pptpdcfg['localip'];
$pptpsa = $pptpdcfg['remoteip'];
$pptpsn = $g['pptp_subnet'];
if($config['pptp']['pptp_subnet'] <> "")
$pptpsn = $config['pptp']['pptp_subnet'];
}
if ($pppoecfg['mode'] == "server") {
$pppoeip = $pppoecfg['localip'];
$pppoesa = $pppoecfg['remoteip'];
$pppoesn = $g['pppoe_subnet'];
if($config['pppoe']['pppoe_subnet'] <> "")
$pppoesn = $config['pppoe']['pppoe_subnet'];
}
/* does the rule deal with a PPTP interface? */
if ($rule['interface'] == "pptp") {
if ($pptpdcfg['mode'] != "server") {
return "# mode != server " . $rule['descr'] . "\n";
}
$nif = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$nif = $config['pptp']['n_pptp_units'];
$ispptp = true;
} else if($rule['interface'] == "pppoe") {
if ($pppoecfg['mode'] != "server") {
return "# mode != server " . $rule['descr'] . "\n";
}
$nif = $g['n_pppoe_units'];
if($config['pppoe']['n_pppoe_units'] <> "")
$nif = $config['pppoe']['n_pppoe_units'];
$ispppoe = true;
} else {
/* Check to see if the interface is opt and in our opt list */
if (strstr($rule['interface'], "opt")) {
if (!array_key_exists($rule['interface'], $optcfg)) {
$item = "";
foreach($optcfg as $oc) $item .= $oc['if'];
return "# {$real_int} {$item} {$rule['interface']} array key does not exist for " . $rule['descr'];
}
}
$nif = 1;
$ispptp = false;
$ispppoe = false;
}
if ($pptpdcfg['mode'] != "server") {
if (($rule['source']['network'] == "pptp") ||
($rule['destination']['network'] == "pptp")) {
return "# source network or destination network == pptp on " . $rule['descr'];
}
}
if ($rule['source']['network'] && strstr($rule['source']['network'], "opt")) {
if (!array_key_exists($rule['source']['network'], $optcfg)) {
return "# {$rule['source']['network']} !array_key_exists source network " . $rule['descr'];
}
}
if ($rule['destination']['network'] && strstr($rule['destination']['network'], "opt")) {
if (!array_key_exists($rule['destination']['network'], $optcfg)) {
$item = "";
foreach($optcfg as $oc) $item .= $oc;
return "# {$item} {$rule['destination']['network']} !array_key_exists dest network " . $rule['descr'];
}
}
/* check for unresolvable aliases */
if ($rule['source']['address'] && !alias_expand($rule['source']['address'])) {
return "# unresolvable source aliases {$rule['descr']}";
}
if ($rule['destination']['address'] && !alias_expand($rule['destination']['address'])) {
return "# unresolvable dest aliases {$rule['descr']}";
}
$ifdescrs = array();
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
$ifdescrs[] = "opt" . $i;
for ($iif = 0; $iif < $nif; $iif++) {
$type = $rule['type'];
if ($type != "pass" && $type != "block" && $type != "reject") {
/* default (for older rules) is pass */
$type = "pass";
}
if ($type == "reject") {
/* special reject packet */
if ($rule['protocol'] == "tcp") {
$line = "block return-rst";
} else if ($rule['protocol'] == "udp") {
$line = "block return-icmp";
} else if ($rule['protocol'] == "tcp/udp") {
$line = "block return";
} else {
$line = "block";
}
} else {
$line = $type;
}
/* ensure the direction is in */
$line .= " in ";
if (isset($rule['log']))
$line .= "log ";
$line .= "quick ";
if ($ispptp) {
$line .= "on ng" . (intval($ngcounter)+1) . " ";
} else if ($ispppoe) {
$line .= "on ng" . (intval($ngcounter)+1) . " ";
} else {
// translate wan, man, lan, opt to real interface.
$interface = $rule['interface'];
$temp = filter_get_opt_interface_descr($interface);
if($temp <> "") $interface = $temp;
if(isset($rule['destination']['address'])) {
$canadd = 0;
/* because pf will not allow a interface for proxyARP
type traffic lets check if its in use and if so leave
off the interface */
if(is_one_to_one_or_server_nat_rule($rule['destination']['address']))
$canadd = 0;
}
if($canadd == 0)
$line .= "on \$" . $interface . " ";
}
/* set the gateway for this item if its defined */
if($config['interfaces'][$rule['interface']]['gateway'] <> "")
$rg = $config['interfaces'][$rule['interface']]['gateway'];
$ri = filter_translate_type_to_real_interface($rule['interface']);
/*
* check to see if /tmp/{$rule['if']}_router exists. This file
* is created by dhclient for 2nd wan interfaces, etc.
*/
if(file_exists("{$g['tmp_path']}/{$rule['if']}_router"))
$rg = file_get_contents("{$g['tmp_path']}/{$rule['if']}_router");
/* do not process reply-to for gateway'd rules */
if($rule['gateway'] == "") {
if($rg <> "" and $ri <> "" and stristr($rule['interface'],"opt") == true) {
$line .= "reply-to (" . $ri . " " . $rg . ") ";
}
}
/* if user has selected a custom gateway, lets work with it */
if($rule['gateway'] <> "") {
$foundlb = 0;
if(is_array($config['load_balancer']['lbpool'])) {
foreach($config['load_balancer']['lbpool'] as $lb) {
if($lb['name'] == $rule['gateway']) {
$gateway = $rule['gateway'];
/*
* is $gateway a interface name?
* if so, lets find out the gateway address
* from /tmp/router_bleh.router
*/
if(in_array($gateway, $ifdescrs)==true) {
if(is_file("{$g['tmp_path']}/{$gateway}_router")) {
$return_gateway = file_get_contents("{$g['tmp_path']}/{$gateway}_router");
} else {
log_error("Could not find {$g['tmp_path']}/{$gateway}_router. Needed for dhcp gateway information");
continue;
}
}
$line .= " route-to { ";
/* if /tmp/$lbname.pool exists then read in our gateway hints from slbd */
if(file_exists("{$g['tmp_path']}/{$lb['name']}.pool")) {
$lbs = split("\n", file_get_contents("{$g['tmp_path']}/{$lb['name']}.pool"));
} else {
$lbs = $lb['servers'];
}
/* iterate through $lbs and setup items accordingly */
foreach($lbs as $server) {
if($foundlb == 1)
$line .= ", ";
/* we are seperating the monitor address from the gateway
* using the | character
*/
if(stristr($server, "|") == true) {
$split_ip = split("\|", $server);
$rule['gateway'] = $split_ip[0];
$gateway = $split_ip[0];
}
/* determine interface from gateway address */
$int = guess_interface_from_ip($gateway);
$line .= "( {$int} {$gateway} ) ";
if($int == "") {
$line = "# error resolving load balancing {$gateway}";
}
$foundlb = 1;
}
$line .= "} round-robin ";
}
}
}
/* we're not using load balancing, just setup gateway */
if($foundlb == 0) {
$gateway = $rule['gateway'];
/*
* is $gateway a interface name?
* if so, lets find out the gateway address
* from /tmp/router_bleh.router
*/
if(in_array($gateway, $ifdescrs)==true) {
$int=filter_opt_interface_to_real($gateway);
if(is_file("{$g['tmp_path']}/{$int}_router")) {
$gatewayip = file_get_contents("{$g['tmp_path']}/{$int}_router");
$gatewayip = rtrim($gatewayip);
$line .= " route-to ( {$int} {$gatewayip} ) ";
} else {
log_error("Could not find {$g['tmp_path']}/{$int}_router. Needed for dhcp gateway information");
continue;
}
} else {
/* user picked a real gateway ip */
$gatewayip = $rule['gateway'];
$int = guess_interface_from_ip($gatewayip);
$line .= " route-to ( {$int} {$gatewayip} ) ";
}
}
}
if (isset($rule['protocol'])) {
if($rule['protocol'] == "tcp/udp")
$line .= "proto { tcp udp } ";
elseif($rule['protocol'] == "icmp")
$line .= "inet proto icmp ";
else
$line .= "proto {$rule['protocol']} ";
} else {
if($rule['source']['port'] <> "" || $rule['destination']['port'] <> "") {
$line .= "proto tcp ";
}
}
/* source address */
if (isset($rule['source']['any'])) {
$src = "any";
} else if ($rule['source']['network']) {
if (strstr($rule['source']['network'], "opt")) {
$src = $optcfg[$rule['source']['network']]['sa'] . "/" .
$optcfg[$rule['source']['network']]['sn'];
if (isset($rule['source']['not'])) $src = " !{$src}";
} else {
switch ($rule['source']['network']) {
case 'lan':
$src = "{$lansa}/{$lansn}";
break;
case 'pptp':
$src = "{$pptpsa}/{$pptpsn}";
break;
case 'pppoe':
$src = "{$pppoesa}/{$pppoesn}";
break;
}
if (isset($rule['source']['not'])) $src = "!{$src}";
}
} else if ($rule['source']['address']) {
$expsrc = alias_expand($rule['source']['address']);
if (isset($rule['source']['not']))
$not = "!";
else
$not = "";
if (stristr($expsrc, "$"))
$src = "{$not} {$expsrc}";
else
$src = "{ {$not} {$expsrc} }";
}
if (!$src || ($src == "/")) {
return "# at the break!";
}
$line .= "from $src ";
if (in_array($rule['protocol'], array("tcp","udp","tcp/udp"))) {
if ($rule['source']['port']) {
$srcport = explode("-", $rule['source']['port']);
if(alias_expand($srcport[0]))
$srcporta = alias_expand($srcport[0]);
else
$srcporta = $srcport[0];
if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
if(alias_expand($srcport[0]))
$line .= "port { {$srcporta} } ";
else
$line .= "port = {$srcporta} ";
} else if (($srcport[0] == 1) && ($srcport[1] == 65535)) {
/* no need for a port statement here */
} else if ($srcport[1] == 65535) {
$line .= "port >= {$srcport[0]} ";
} else if ($srcport[0] == 1) {
$line .= "port <= {$srcport[1]} ";
} else {
$srcport[0]--;
$srcport[1]++;
$line .= "port {$srcport[0]} >< {$srcport[1]} ";
}
}
/* OS signatures */
if (($rule['protocol'] == "tcp") && ($rule['os'] <> ""))
$line .= "os {$rule['os']} ";
}
/* destination address */
if (isset($rule['destination']['any'])) {
$dst = "any";
} else if ($rule['destination']['network']) {
if (strstr($rule['destination']['network'], "opt")) {
$dst = $optcfg[$rule['destination']['network']]['sa'] . "/" .
$optcfg[$rule['destination']['network']]['sn'];
if (isset($rule['destination']['not'])) $dst = " !{$dst}";
} else {
switch ($rule['destination']['network']) {
case 'lan':
$dst = "{$lansa}/{$lansn}";
break;
case 'pptp':
$dst = "{$pptpsa}/{$pptpsn}";
break;
case 'pppoe':
$dst = "{$ppoesa}/{$pppoesn}";
break;
}
if (isset($rule['destination']['not'])) $dst = " !{$dst}";
}
} else if ($rule['destination']['address']) {
$expdst = alias_expand($rule['destination']['address']);
if (isset($rule['destination']['not']))
$not = "! ";
else
$not = "";
if (stristr($expdst, "$"))
$dst = "{$not} {$expdst}";
else
$dst = "{ {$not} {$expdst} }";
}
if (!$dst || ($dst == "/")) {
return "# returning at $dst == \"/\"";
break;
}
$line .= "to $dst ";
if (in_array($rule['protocol'], array("tcp","udp","tcp/udp"))) {
if ($rule['destination']['port']) {
$dstport = explode("-", $rule['destination']['port']);
if(alias_expand($dstport[0]))
$dstporta = alias_expand($dstport[0]);
else
$dstporta = $dstport[0];
if ((!$dstport[1]) || ($dstport[0] == $dstport[1])) {
if(alias_expand($dstport[0]))
$line .= "port {$dstporta} ";
else
$line .= "port = {$dstporta} ";
} else if (($dstport[0] == 1) && ($dstport[1] == 65535)) {
/* no need for a port statement here */
} else if ($dstport[1] == 65535) {
$line .= "port >= {$dstport[0]} ";
} else if ($dstport[0] == 1) {
$line .= "port <= {$dstport[1]} ";
} else {
$dstport[0]--;
$dstport[1]++;
$line .= "port {$dstport[0]} >< {$dstport[1]} ";
}
}
}
if (($rule['protocol'] == "icmp") && $rule['icmptype']) {
$line .= "icmp-type {$rule['icmptype']} ";
}
if ($type == "pass") {
if( isset($rule['source-track']) or isset($rule['max-src-nodes']) or isset($rule['max-src-states']) )
if($rule['protocol'] == "tcp")
$line .= "flags S/SA ";
/*
# keep state
works with TCP, UDP, and ICMP.
# modulate state
works only with TCP. pfSense will generate strong Initial Sequence Numbers (ISNs)
for packets matching this rule.
# synproxy state
proxies incoming TCP connections to help protect servers from spoofed TCP SYN floods.
This option includes the functionality of keep state and modulate state combined.
# none
do not use state mechanisms to keep track. this is only useful if your doing advanced
queueing in certain situations. please check the faq.
*/
if(isset($rule['statetype']) && $rule['statetype'] <> "") {
switch($rule['statetype']) {
case "none":
break;
case "modulate state":
case "synproxy state":
if($rule['protocol'] == "tcp")
$line .= "{$rule['statetype']} ";
break;
default:
$line .= "{$rule['statetype']} ";
}
} else {
$line .= "keep state ";
}
if( isset($rule['source-track']) and $rule['source-track'] <> "" or
isset($rule['max-src-nodes']) and $rule['max-src-nodes'] <> "" or
isset($rule['max-src-conn-rate']) and $rule['max-src-conn-rate'] <> "" or
isset($rule['max-src-conn-rates']) and $rule['max-src-conn-rates'] <> "" or
isset($rule['max-src-states']) and $rule['max-src-states'] <> "" ) {
$line .= "( ";
if(isset($rule['source-track']) and $rule['source-track'] <> "")
$line .= "source-track rule ";
if(isset($rule['max-src-nodes']) and $rule['max-src-nodes'] <> "")
$line .= "max-src-nodes " . $rule['max-src-nodes'] . " ";
if(isset($rule['max-src-states']) and $rule['max-src-states'] <> "")
$line .= "max-src-states " . $rule['max-src-states'] . " ";
if(isset($rule['statetimeout']) and $rule['statetimeout'] <> "")
$line .= "tcp.established " . $rule['statetimeout'] . " ";
if(isset($rule['max-src-conn-rate']) and $rule['max-src-conn-rate'] <> ""
and isset($rule['max-src-conn-rates']) and $rule['max-src-conn-rates'] <> "") {
$line .= "max-src-conn-rate " . $rule['max-src-conn-rate'] . " ";
$line .= "/" . $rule['max-src-conn-rates'] . ", overload <virusprot> flush global ";
}
$line .= " ) ";
}
}
if ($type == "reject" && $rule['protocol'] == "tcp") {
/* special reject packet */
$line .= "flags S/SA ";
}
}
return $line;
}
function filter_rules_generate() {
global $config, $g;
$wancfg = $config['interfaces']['wan'];
$lancfg = $config['interfaces']['lan'];
$pptpdcfg = $config['pptpd'];
$pppoecfg = $config['pppoe'];
$lanif = $lancfg['if'];
$wanif = get_real_wan_interface();
$lanip = $lancfg['ipaddr'];
$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
$lansn = $lancfg['subnet'];
if($lansa)
$lansa_sn_combo = "{$lansa}/{$lansn}";
else
$lansa_sn_combo = "192.168.1.1/32";
/* optional interfaces */
$optcfg = array();
generate_optcfg_array($optcfg);
# BEGIN OF firewall rules
$ipfrules="anchor \"firewallrules\"\n";
if ($pptpdcfg['mode'] == "server") {
$pptpip = $pptpdcfg['localip'];
$pptpsa = $pptpdcfg['remoteip'];
$pptpsn = $g['pptp_subnet'];
if($config['pptp']['pptp_subnet'] <> "")
$pptpsn = $config['pptp']['pptp_subnet'];
}
if ($pppoecfg['mode'] == "server") {
$pppoeip = $pppoecfg['localip'];
$pppoesa = $pppoecfg['remoteip'];
$pppoesn = $g['pppoe_subnet'];
if($config['pppoe']['pppoe_subnet'] <> "")
$pppoesn = $config['pppoe']['pppoe_subnet'];
}
/* default block logging? */
if (!isset($config['syslog']['nologdefaultblock']))
$log = "log";
else
$log = "";
/* if squid is installed, lets install its rule */
if (is_package_installed("squid") == 1) {
$ipfrules .= "pass in quick on {$lanif} inet proto tcp from any to 127.0.0.1 port 3128 flags S/SA keep state label \"transparent proxy\"\n";
$ipfrules .= "pass out quick on {$wanif} inet proto tcp from ($wanif) to any port www flags S/SA keep state label \"transparent proxy\"\n";
}
$ipfrules .= <<<EOD
# loopback
anchor "loopback"
pass in quick on lo0 all label "pass loopback"
pass out quick on lo0 all label "pass loopback"
# package manager early specific hook
anchor "packageearly"
# carp
anchor "carp"
EOD;
if(!isset($config['system']['disableftpproxy'])) {
$ipfrules .= <<<EOD
# enable ftp-proxy
anchor "ftpproxy"
anchor "pftpx/*"
pass in quick on $wanif inet proto tcp from port 20 to ($wanif) port > 49000 user proxy flags S/SA keep state label "FTP PROXY: PASV mode data connection"
EOD;
if(isset($config['system']['rfc959workaround'])) {
$ipfrules .= <<<EOD
# Fix sites that violate RFC 959 which specifies that the data connection
# be sourced from the command port - 1 (typically port 20)
# This workaround doesn't expose us to any extra risk as we'll still only allow
# connections to the firewall on a port that ftp-proxy is listening on
pass in quick on $wanif inet proto tcp from any to ($wanif) port > 49000 user proxy flags S/SA keep state label "FTP PROXY: RFC959 violation workaround"
EOD;
$optcfg = array();
generate_optcfg_array($optcfg);
foreach($optcfg as $oc) {
if($oc['gateway'] <> "")
$ipfrules .= "pass in quick on {$oc['if']} inet proto tcp from any to ({$oc['if']}) port > 49000 user proxy flags S/SA keep state label \"FTP PROXY: RFC959 violation workaround\" \n";
}
}
}
$ipfrules .= <<<EOD
# allow access to DHCP server on LAN
anchor "dhcpserverlan"
pass in quick on $lanif proto udp from any port = 68 to 255.255.255.255 port = 67 label "allow access to DHCP server on LAN"
pass in quick on $lanif proto udp from any port = 68 to $lanip port = 67 label "allow access to DHCP server on LAN"
pass out quick on $lanif proto udp from $lanip port = 67 to any port = 68 label "allow access to DHCP server on LAN"
EOD;
/* allow access to DHCP server on optional interfaces */
foreach ($optcfg as $on => $oc) {
if (isset($config['dhcpd'][$on]['enable']) && (!$oc['bridge'])) {
$ipfrules .= <<<EOD
# allow access to DHCP server on {$on}
anchor "dhcpserver"
pass in quick on {$oc['if']} proto udp from any port = 68 to 255.255.255.255 port = 67 label "allow access to DHCP server"
pass in quick on {$oc['if']} proto udp from any port = 68 to {$oc['ip']} port = 67 label "allow access to DHCP server"
pass out quick on {$oc['if']} proto udp from {$oc['ip']} port = 67 to any port = 68 label "allow access to DHCP server"
pass out on {$oc['if']} proto icmp from any to any keep state label "allow access to DHCP server"
EOD;
}
}
/* pass traffic between statically routed subnets and the subnet on the
interface in question to avoid problems with complicated routing
topologies */
if (is_array($config['staticroutes']['route']) && count($config['staticroutes']['route'])) {
foreach ($config['staticroutes']['route'] as $route) {
unset($sa);
if ($route['interface'] == "lan") {
$sa = $lansa;
$sn = $lansn;
$if = $lanif;
} else if (strstr($route['interface'], "opt")) {
$oc = $optcfg[$route['interface']];
if ($oc['ip']) {
$sa = $oc['sa'];
$sn = $oc['sn'];
$if = $oc['if'];
}
}
if ($sa) {
$ipfrules .= <<<EOD
anchor "staticrouted"
pass in quick on {$if} from {$sa}/{$sn} to {$route['network']} label "pass traffic between statically routed subnets"
pass in quick on {$if} from {$route['network']} to {$sa}/{$sn} label "pass traffic between statically routed subnets"
pass out quick on {$if} from {$sa}/{$sn} to {$route['network']} label "pass traffic between statically routed subnets"
pass out quick on {$if} from {$route['network']} to {$sa}/{$sn} label "pass traffic between statically routed subnets"
EOD;
}
}
}
$ipfrules .= <<<EOD
# WAN spoof check
anchor "wanspoof"
block in $log quick on $wanif from $lansa/$lansn to any label "WAN spoof check"
EOD;
foreach ($optcfg as $oc) {
if (!$oc['bridge'])
if($oc['sa'] <> "")
$ipfrules .= "block in $log quick on $wanif from {$oc['sa']}/{$oc['sn']} to any label \"interface spoof check\"\n";
}
/* allow PPTP traffic if PPTP client is enabled on WAN */
if ($wancfg['ipaddr'] == "pptp") {
$ipfrules .= <<<EOD
# allow PPTP client
anchor "pptpclient"
pass in quick on {$wancfg['if']} proto gre from any to any keep state label "allow PPTP client"
pass out quick on {$wancfg['if']} proto gre from any to any keep state label "allow PPTP client"
pass in quick on {$wancfg['if']} proto tcp from any port = 1723 to any flags S/SA keep state label "allow PPTP client"
pass out quick on {$wancfg['if']} proto tcp from any to any port = 1723 flags S/SA keep state label "allow PPTP client"
EOD;
}
$ipfrules .= <<<EOD
# allow our DHCP client out to the WAN
# XXX - should be more restrictive
# (not possible at the moment - need 'me' like in ipfw)
anchor "wandhcp"
pass out quick on $wanif proto udp from any port = 68 to any port = 67 label "allow dhcp client out wan"
block in $log quick on $wanif proto udp from any port = 67 to {$lansa_sn_combo} port = 68 label "allow dhcp client out wan"
pass in quick on $wanif proto udp from any port = 67 to any port = 68 label "allow dhcp client out wan"
# LAN/OPT spoof check (needs to be after DHCP because of broadcast addresses)
EOD;
/* LAN spoof check */
$ipfrules .= filter_rules_spoofcheck_generate('lan', $lanif, $lansa, $lansn, $log);
/* OPT spoof check */
foreach ($optcfg as $on => $oc) {
if ($oc['ip'])
$ipfrules .= filter_rules_spoofcheck_generate($on, $oc['if'], $oc['sa'], $oc['sn'], $log);
}
/* block private networks on WAN? */
if (isset($config['interfaces']['wan']['blockpriv'])) {
$ipfrules .= <<<EOD
# block anything from private networks on WAN interface
anchor "spoofing"
block in $log quick on $wanif from 10.0.0.0/8 to any label "block private networks from wan block 10/8"
block in $log quick on $wanif from 127.0.0.0/8 to any label "block private networks from wan block 127/8"
block in $log quick on $wanif from 172.16.0.0/12 to any label "block private networks from wan block 172.16/12"
block in $log quick on $wanif from 192.168.0.0/16 to any label "block private networks from wan block 192.168/16"
EOD;
}
/*
* Support for allow limiting of TCP connections by establishment rate
* Useful for protecting against sudden outburts, etc.
*/
$ipfrules .= <<<EODF
# Support for allow limiting of TCP connections by establishment rate
anchor "limitingesr"
table <virusprot>
EODF;
/* block bogon networks on WAN */
/* http://www.cymru.com/Documents/bogon-bn-nonagg.txt */
/* file is automatically in cron every 3000 minutes */
if (isset($config['interfaces']['wan']['blockbogons'])) {
$ipfrules .= <<<EOD
# block bogon networks
# http://www.cymru.com/Documents/bogon-bn-nonagg.txt
anchor "wanbogons"
table <bogons> persist file "/etc/bogons"
block in $log quick on $wanif from <bogons> to any label "block bogon networks from wan"
EOD;
}
$ipfrules .= <<<EOD
# let out anything from the firewall host itself and decrypted IPsec traffic
# pass out quick on $wanif all keep state label "let out anything from firewall host itself"
EOD;
$ipfrules .= create_firewall_outgoing_rules_to_itself();
/* group heads for optional interfaces */
foreach ($optcfg as $on => $oc) {
$ipfrules .= <<<EOD
# let out anything from the firewall host itself and decrypted IPsec traffic
pass out quick on {$oc['if']} all keep state label "let out anything from firewall host itself"
EOD;
}
if (!isset($config['system']['webgui']['noantilockout'])) {
$ipfrules .= <<<EOD
# make sure the user cannot lock himself out of the webGUI or SSH
anchor "anti-lockout"
pass in quick from {$lansa}/{$lansn} to $lanip keep state label "anti-lockout web rule"
EOD;
}
/* PPTPd enabled? */
if ($pptpdcfg['mode'] && ($pptpdcfg['mode'] != "off")) {
if ($pptpdcfg['mode'] == "server")
$pptpdtarget = "127.0.0.1";
else
$pptpdtarget = $pptpdcfg['redir'];
$ipfrules .= <<<EOD
# PPTPd rules
anchor "pptp"
#pass quick proto gre from any to {$pptpdtarget} keep state label "allow gre pptpd"
#pass quick proto tcp from any to {$pptpdtarget} port = 1723 keep state label "allow pptpd {$pptpdtarget}"
# XXX: fix me!
pass quick proto gre from any to any keep state label "allow gre pptpd"
pass quick proto tcp from any to any port = 1723 keep state label "allow pptpd {$pptpdtarget}"
EOD;
}
/* BigPond client enabled? */
if ($wancfg['ipaddr'] == "bigpond") {
$ipfrules .= <<<EOD
# BigPond heartbeat rules
anchor "bigpond"
pass in quick proto udp from any to any port = 5050 keep state label "BigPond heartbeat"
# package manager late specific hook
anchor "packagelate"
EOD;
}
$ipfrules .= "\n# SSH lockout\n";
$ipfrules .= "block in log proto tcp from <sshlockout> to any port 22 label \"sshlockout\"\n\n";
$ipfrules .= "\n# User-defined rules follow\n";
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue']) && isset($config['filter']['rule'])) {
/* Pre-cache all our rules so we only have to generate them once */
/* XXX: billm - twice really, this needs to be made useable for "normal rules too" */
$rule_arr = array();
$ipfrules .= "# Anchors for rules that might be matched by queues\n";
foreach ($config['filter']['rule'] as $rule) {
$line = "";
if (!isset($rule['disabled'])) {
if ($rule['interface'] == "pptp") {
$n_pptp_units = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$nif = $config['pptp']['n_pptp_units'];
for($xxx=0; $xxx < $n_pptp_units; $xxx++) {
/*
* now that PPTP server are user rules, detect
* that user is setting the pptp server rule
* and setup for all netgraph interfaces
*/
$rule_arr[] = generate_user_filter_rule_arr($rule, $xxx);
}
} else if($rule['interface'] == "pppoe") {
$n_pppoe_units = $g['n_pppoe_units'];
if($config['pppoe']['n_pppoe_units'] <> "")
$nif = $config['pppoe']['n_pppoe_units'];
for($xxx=0; $xxx < $n_pppoe_units; $xxx++) {
/*
* now that pppoe server are user rules, detect
* that user is setting the pppoe server rule
* and setup for all netgraph interfaces
*/
$rule_arr[] = generate_user_filter_rule_arr($rule, $xxx);
}
} else {
$rule_arr[] = generate_user_filter_rule_arr($rule, 0);
}
}
}
/* This is ugly, but we generate one anchor per queue */
foreach ($config['shaper']['queue'] as $queue) {
/* Add anchor to rules */
$ipfrules .= "anchor {$queue['name']} tagged {$queue['name']}\n";
/* Create rules for anchors */
$fd = fopen("{$g['tmp_path']}/{$queue['name']}.rules", "w");
/* aliases don't recurse to anchors */
$line = filter_generate_aliases();
fwrite($fd, $line);
foreach($rule_arr as $rule) {
if($rule['ackq'] != "")
$line = "{$rule['rule'] } queue ({$queue['name']}, {$rule['ackq']}) {$rule['descr']}\n";
else
$line = "{$rule['rule'] } queue {$queue['name']} {$rule['descr']}\n";
fwrite($fd, $line);
}
fclose($fd);
}
}
if (isset($config['filter']['rule'])) {
foreach ($config['filter']['rule'] as $rule) {
$line = "";
if (!isset($rule['disabled'])) {
if ($rule['interface'] == "pptp") {
$n_pptp_units = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$nif = $config['pptp']['n_pptp_units'];
for($xxx=0; $xxx < $n_pptp_units; $xxx++) {
/*
* now that PPTP server are user rules, detect
* that user is setting the pptp server rule
* and setup for all netgraph interfaces
*/
$line = generate_user_filter_rule($rule, $xxx);
if($line <> "") {
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue'])) {
$defq = find_default_queue($rule['interface']);
$ackq = get_ack_queue($rule['interface']);
if (($defq != "") and ($ackq != ""))
$line .= " queue ({$defq}, {$ackq}) ";
}
$ipfrules .= $line . "\n";
}
}
} else if($rule['interface'] == "pppoe") {
$n_pppoe_units = $g['n_pppoe_units'];
if($config['pppoe']['n_pppoe_units'] <> "")
$nif = $config['pppoe']['n_pppoe_units'];
for($xxx=0; $xxx < $n_pppoe_units; $xxx++) {
/*
* now that pppoe server are user rules, detect
* that user is setting the pppoe server rule
* and setup for all netgraph interfaces
*/
$line = generate_user_filter_rule($rule, $xxx);
if($line <> "") {
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue'])) {
$defq = find_default_queue($rule['interface']);
$ackq = get_ack_queue($rule['interface']);
if (($defq != "") and ($ackq != ""))
$line .= " queue ({$defq}, {$ackq}) ";
}
$ipfrules .= $line . "\n";
} else {
$ipfrules .= "# Error creating {$rule['descr']} \n";
}
}
} else {
$line = generate_user_filter_rule($rule, 0);
if($line <> "")
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue'])) {
$defq = find_default_queue($rule['interface']);
$ackq = get_ack_queue($rule['interface']);
if (($defq != "") and ($ackq != ""))
$line .= " queue ({$defq}, {$ackq}) ";
}
// label
if($rule['descr'] <> "" and $line <> "")
$line .= " label \"USER_RULE: {$rule['descr']}\" ";
else
$line .= " label \"USER_RULE\" ";
}
}
$line .= "\n";
$ipfrules .= $line;
}
}
$ipfrules .= process_carp_rules();
$ipfrules .= "\n# VPN Rules\n";
$lan_ip = $config['interfaces']['lan']['ipaddr'];
$lan_subnet = $config['interfaces']['lan']['subnet'];
$wanif = get_real_wan_interface();
$wan_ip = find_interface_ip($wanif);
$internal_subnet = gen_subnet($lan_ip, $lan_subnet) . "/" . $config['interfaces']['lan']['subnet'];
/* Is IP Compression enabled? */
if(isset($config['ipsec']['ipcomp']))
exec("/sbin/sysctl net.inet.ipcomp.ipcomp_enable=1");
else
exec("/sbin/sysctl net.inet.ipcomp.ipcomp_enable=0");
/* determine ipsec address */
$ipsec_ip = $wan_ip;
/* set failover ip if defined */
if(isset($config['installedpackages']['sasyncd']['config']))
foreach($config['installedpackages']['sasyncd']['config'] as $sasyncd) {
if($sasyncd['ip'] <> "")
$ipsec_ip = $sasyncd['ip'];
}
if(is_array($config['ipsec']['tunnel']) && isset($config['ipsec']['enable'])) {
foreach ($config['ipsec']['tunnel'] as $tunnel) {
/* if tunnel is disabled, lets skip to next item */
if(isset($tunnel['disabled']))
continue;
/* is this a dynamic dns hostname? */
$remote_gateway = gethostbyname($tunnel['remote-gateway']);
if($remote_gateway == "")
$remote_gateway = $tunnel['remote-gateway'];
/* do not add items with blank remote_gateway */
if(!$remote_gateway) {
$ipfrules .= "# ERROR! Remote gateway not found on ... pass quick on {$wanif} proto udp from {$ipsec_ip} to {$remote_gateway} port = 500 keep state label \"IPSEC: {$tunnel['descr']} udp\"\n";
continue;
}
$local_subnet = return_vpn_subnet($tunnel['local-subnet']);
$ipfrules .= "pass quick on {$wanif} proto udp from {$ipsec_ip} to {$remote_gateway} port = 500 keep state label \"IPSEC: {$tunnel['descr']} - outbound isakmp\"\n";
$ipfrules .= "pass quick on {$wanif} proto udp from {$remote_gateway} to {$ipsec_ip} port = 500 keep state label \"IPSEC: {$tunnel['descr']} - inbound isakmp\"\n";
$ipfrules .= "pass quick on {$wanif} proto esp from {$ipsec_ip} to {$remote_gateway} keep state label \"IPSEC: {$tunnel['descr']} - outbound esp proto\"\n";
$ipfrules .= "pass quick on {$wanif} proto esp from {$remote_gateway} to {$ipsec_ip} keep state label \"IPSEC: {$tunnel['descr']} - inbound esp proto\"\n";
$ipfrules .= "pass quick on {$wanif} proto ah from {$ipsec_ip} to {$remote_gateway} keep state label \"IPSEC: {$tunnel['descr']} - outbound ah proto\"\n";
$ipfrules .= "pass quick on {$wanif} proto ah from {$remote_gateway} to {$ipsec_ip} keep state label \"IPSEC: {$tunnel['descr']} - inbound ah proto\"\n";
$ipfrules .= "pass quick on {$lanif} from {$tunnel['remote-subnet']} to {$local_subnet} keep state label \"IPSEC: {$tunnel['descr']} - remote to local\"\n";
$ipfrules .= "pass quick on {$lanif} from {$local_subnet} to {$tunnel['remote-subnet']} keep state label \"IPSEC: {$tunnel['descr']} - local to remote\"\n";
}
}
/* is mobile ipsec enabled? if so lets allow some pretty
* loose rules to allow mobile clients to phone in.
*/
$ipseccfg = $config['ipsec'];
if (isset($ipseccfg['mobileclients']['enable'])) {
$ipfrules .= "pass quick on {$wanif} proto udp from any to any port = 500 keep state label \"IPSEC: Mobile - inbound isakmp\"\n";
$ipfrules .= "pass quick on {$wanif} proto esp from any to any keep state label \"IPSEC: Mobile - inbound esp proto\"\n";
$ipfrules .= "pass quick on {$wanif} proto ah from any to any keep state label \"IPSEC: Mobile - inbound ah proto\"\n";
}
$ipfrules .= <<<EOD
#---------------------------------------------------------------------------
# default rules (just to be sure)
#---------------------------------------------------------------------------
block in $log quick all label "Default block all just to be sure."
block out $log quick all label "Default block all just to be sure."
EOD;
return $ipfrules;
}
function filter_rules_spoofcheck_generate($ifname, $if, $sa, $sn, $log) {
global $g, $config;
$ipfrules = "antispoof for {$if}\n";
return $ipfrules;
}
function setup_logging_interfaces() {
global $config;
$rules = "";
$i = 0;
$ifdescrs = array('wan', 'lan');
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
$ifdescrs['opt' . $j] = "opt" . $j;
}
foreach ($ifdescrs as $ifdescr => $ifname) {
/* do not work with tun interfaces */
if(stristr(filter_translate_type_to_real_interface($ifname), "tun") == true) continue;
$int = filter_translate_type_to_real_interface($ifname);
$rules .= "set loginterface {$int}\n";
}
return $rules;
}
function create_firewall_outgoing_rules_to_itself() {
global $config, $g;
$i = 0;
$rule .= "# pass traffic from firewall -> out\n";
$rule .= "anchor \"firewallout\"\n";
$ifdescrs = array('wan', 'lan');
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
$ifdescrs['opt' . $j] = "opt" . $j;
/* go through primary and optional interfaces */
foreach ($ifdescrs as $ifdescr => $ifname) {
$return_gateway = $config['interfaces'][$ifname]['gateway'];
$ints = array();
$int = filter_translate_type_to_real_interface($ifname);
if ($config['interfaces'][$ifname]['ipaddr'] == "pppoe")
$int = "ng0";
$ip = find_interface_ip($int);
if($ip <> "") {
if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue'])) {
foreach ($config['shaper']['queue'] as $queue) {
/* XXX - billm: temp fix for pppoe, needs better fix */
if($config['interfaces'][$ifname]['ipaddr'] != "")
$rule .="pass out quick on {$int} all keep state tagged {$queue['name']} queue {$queue['name']} label \"let out anything from firewall host itself\"\n";
else
$rule .="pass out quick on {$int} all keep state tagged {$queue['name']} queue {$queue['name']} label \"let out anything from firewall host itself\"\n";
}
}
/* XXX - billm: temp fix for pppoe, needs better fix */
if($config['interfaces'][$ifname]['ipaddr'] != "")
$rule .="pass out quick on {$int} all keep state label \"let out anything from firewall host itself\"\n";
else
$rule .="pass out quick on {$int} all keep state label \"let out anything from firewall host itself\"\n";
}
}
/* is bridging turned on? */
for($x=0; $x<10; $x++) {
if(does_interface_exist("bridge{$x}") == true) {
$rule .="pass out quick on bridge{$x} all keep state label \"let out anything from firewall host itself\"\n";
}
}
/* netgraph pptp outgoing */
$n_pptp_units = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$n_pptp_units = $config['pptp']['n_pptp_units'];
for($x=0; $x<$n_pptp_units; $x++) {
if(does_interface_exist("ng{$x}") == true) {
$rule .="pass out quick on ng{$x} all keep state label \"let out anything from firewall host itself pptp\"\n";
}
}
/* netgraph pppoe outgoing */
$n_pppoe_units = $g['n_pppoe_units'];
if($config['pppoe']['n_pppoe_units'] <> "")
$n_pppoe_units = $config['pppoe']['n_pppoe_units'];
for($x=0; $x<$n_pppoe_units; $x++) {
if(does_interface_exist("ng{$x}") == true) {
$rule .="pass out quick on ng{$x} all keep state label \"let out anything from firewall host itself pppoe\"\n";
}
}
/* setup outgoing gif tunnels */
$number_of_gifs = find_last_gif_device();
for($x=0; $x<$number_of_gifs; $x++) {
if(does_interface_exist("gif{$x}") == true) {
$rule .="pass out quick on gif{$x} all keep state label \"let out anything from firewall host itself ipsec gif\"\n";
}
}
return $rule;
}
function process_carp_nat_rules() {
global $g, $config;
$lines = "";
if($config['installedpackages']['carp']['config'] != "")
foreach($config['installedpackages']['carp']['config'] as $carp) {
$ip = $carp['ipaddress'];
if($ip <> "any") {
$ipnet = "any";
} else {
$int = find_ip_interface($ip);
$carp_int = find_carp_interface($ip);
}
if($int != false and $int != $wan_interface) {
$ipnet = convert_ip_to_network_format($ip, $carp['netmask']);
$lines .= "nat on {$int} inet from {$ipnet} to any -> ({$carp_int}) \n";
}
}
return $lines;
}
function process_carp_rules() {
global $g, $config;
$lines = "";
/* return if there are no carp configured items */
if($config['installedpackages']['carpsettings']['config'] <> "" or
$config['virtualip']['vip'] <> "") {
$lines .= "pass quick proto carp keep state\n";
$lines .= "pass quick proto pfsync";
}
return $lines;
}
function carp_sync_xml($url, $password, $section, $section_xml, $method = 'pfsense.restore_config_section', $port) {
global $config, $g;
if($g['booting'])
return;
$params = array(new XML_RPC_Value($password, 'string'),
new XML_RPC_Value($section, 'array'),
new XML_RPC_Value($section_xml, 'array'));
$msg = new XML_RPC_Message($method, $params);
log_error("begin synchronize XML configuration data to $url on port $port");
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$cli->setCredentials($config['system']['username'], $password);
$resp = $cli->send($msg, "240");
if(!$resp) {
log_error("A communication error occured while syncing configuration data with {$url}:{$port} - " . $cli->errstr);
file_notice("sync_settings", "A communication error occured while syncing configuration data with {$url}:{$port} - " . $cli->errstr, "Settings Sync", "");
} elseif($resp->faultCode()) {
log_error("An error code was received while syncing configuration data with {$url}:{$port} - Code " . $resp->faultCode() . ":" . $resp->faultString() );
file_notice("sync_settings", "An error code was received while syncing configuration data with {$url}:{$port} - Code " . $resp->faultCode() . ":" . $resp->faultString(), "Settings Sync", "");
} else {
log_error("Configuration data was successfully synced to {$url}:{$port}");
}
}
function carp_sync_client() {
global $config, $g;
if($g['booting'])
return;
if($config['installedpackages']['carpsettings']['config'] != "" and
is_array($config['installedpackages']['carpsettings']['config'])) {
$already_processed = 1;
foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
if($carp['synchronizetoip'] != "" ) {
if($config['system']['webgui']['protocol'] != "") {
$synchronizetoip = $config['system']['webgui']['protocol'];
$synchronizetoip .= "://";
}
$port = $config['system']['webgui']['port'];
/* if port is empty lets rely on the protocol selection */
if($port == "") {
if($config['system']['webgui']['protocol'] == "http") {
$port = "80";
} else {
$port = "443";
}
}
$synchronizetoip .= $carp['synchronizetoip'];
$sections = array();
$sections_xml = array();
if($carp['synchronizerules'] != "" and is_array($config['filter'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("filter"), 'string');
$sections[] = new XML_RPC_Value('filter', 'string');
}
if($carp['synchronizenat'] != "" and is_array($config['nat'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("nat"), 'string');
$sections[] = new XML_RPC_Value('nat', 'string');
}
if($carp['synchronizealiases'] != "" and is_array($config['aliases'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("aliases"), 'string');
$sections[] = new XML_RPC_Value('aliases', 'string');
}
if($carp['synchronizedhcpd'] != "" and is_array($config['dhcpd'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("dhcpd"), 'string');
$sections[] = new XML_RPC_Value('dhcpd', 'string');
}
if($carp['synchronizewol'] != "" and is_array($config['wol'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("wol"), 'string');
$sections[] = new XML_RPC_Value('wol', 'string');
}
if($carp['synchronizetrafficshaper'] != "" and is_array($config['shaper'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("shaper"), 'string');
$sections[] = new XML_RPC_Value('shaper', 'string');
}
if($carp['synchronizestaticroutes'] != "" and is_array($config['staticroutes'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("staticroutes"), 'string');
$sections[] = new XML_RPC_Value('staticroutes', 'string');
}
if($carp['synchronizevirtualip'] != "" and is_array($config['virtualip'])) {
$sections_xml[] = new XML_RPC_Value(backup_vip_config_section(), 'string');
$sections[] = new XML_RPC_Value('virtualip', 'string');
}
if($carp['synchronizelb'] != "" and is_array($config['load_balancer'])) {
$sections_xml[] = new XML_RPC_Value(backup_config_section("load_balancer"), 'string');
$sections[] = new XML_RPC_Value('load_balancer', 'string');
}
if(count($sections) > 0) {
carp_sync_xml($synchronizetoip, $carp['password'], $sections, $sections_xml, 'pfsense.restore_config_section', $port);
$cli = new XML_RPC_Client('/xmlrpc.php', $synchronizetoip, $port);
$msg = new XML_RPC_Message('pfsense.filter_configure', array(new XML_RPC_Value($carp['password'], 'string')));
$cli->setCredentials($config['system']['username'], $carp['password']);
$cli->send($msg, "900");
/* signal a carp reload */
$msg = new XML_RPC_Message('pfsense.interfaces_carp_configure_xmlrpc', array(new XML_RPC_Value($carp['password'], 'string')));
$cli->send($msg, "900");
}
}
}
}
}
function return_vpn_subnet($adr) {
global $config;
if ($adr['address']) {
list($padr, $pmask) = explode("/", $adr['address']);
if (is_null($pmask))
return "{$padr}/32";
return "{$padr}/{$pmask}";
}
/* XXX: do not return wan, lan, etc */
if(strstr($adr['network'], "wan") or strstr($adr['network'], "lan") or strstr($adr['network'], "opt"))
return convert_ip_to_network_format($config['interfaces'][$adr['network']]['ipaddr'],
$config['interfaces'][$adr['network']]['subnet']);
/* fallback - error */
return " # error - {$adr['network']} ";
}
?>
|