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
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
|
<?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");
function filter_resync() {
global $config, $g;
mwexec("/sbin/pfctl -y"); /* XXX */
}
function filter_pflog_start() {
global $config, $g;
mute_kernel_msgs();
mwexec("/sbin/ifconfig pflog0 up && pflogd -sD");
mwexec_bg("/usr/sbin/tcpdump -n -e -ttt -i pflog0 | logger -t pf -p local0.info");
unmute_kernel_msgs();
}
/* reload filter async */
function filter_configure() {
touch("/tmp/filter_dirty");
}
/* reload filter sync */
function filter_configure_sync() {
/* file an alert that the rules are being reloaded */
file_notice("Operation in progress...", "The filter rules are currently reloading. You will receive a notice if there is an error.", "", "");
if(file_exists("/tmp/filter_dirty"))
unlink("/tmp/filter_dirty");
global $config, $g;
$lan_if = $config['interfaces']['lan']['if'];
$wan_if = get_real_wan_interface();
/* generate aliases */
$aliases = filter_generate_aliases();
/* generate nat rules */
$natrules = filter_nat_rules_generate();
/* generate pfctl rules */
$pfrules = filter_rules_generate();
if(isset($config['shaper']['enable'])) {
/* generate altq interface setup parms */
$altq_ints = filter_setup_altq_interfaces();
/* generate altq queues */
$altq_queues = filter_generate_altq_queues($altq_ints);
/* generate 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();
//$rules.= "set state-policy if-bound\n";
$optimization = "normal";
if($config['system']['optimization'] <> "") $optimization = $config['system']['optimization'];
$rules.= "set optimization " . $optimization . "\n";
if($config['system']['maximumstates'] <> "" && is_numeric($config['system']['maximumstates'])) {
/* User defined maximum states in Advanced menu. */
$maximumstates = $config['system']['maximumstates'];
$rules.= "set limit states " . $maximumstates . "\n";
}
$rules.= "\n";
$rules.= "scrub on {$wanif} 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) {
$rules_loading .= 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 /tmp/rules.debug`;
$line_split = split("\n", $rules_file);
if(is_array($line_split))
$line_error = "The line in question reads [{$line_number}]:<br>" . $line_split[$line_number];
file_notice("filter_load", "There was error(s) loading the rules:<p><pre>" . $rules_error . "\n" . $line_error . "</pre>", "Filter Reload", "");
return "There was error(s) loading the rules:<p><pre>" . $rules_error . "\n" . $line_error . "</pre>";
}
if(file_exists("/usr/local/pkg/pf/carp_sync_client.php"))
unlink("/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 = return_filename_as_string("/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_altq_queue_scheduler_type($associatedrule) {
global $config;
return $config['system']['schedulertype'];
}
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 find_default_queue($interface) {
global $config;
$qconfig = $config;
/* quick return if we've already seen the default queue for this interface */
if (isset($queue_cache['defq'][$interface]))
return $queue_cache['defq'][$interface];
if (is_array($qconfig['shaper']['queue'])) {
foreach ($qconfig['shaper']['queue'] as $queue) {
if(isset($queue['defaultqueue']) and ($queue['defaultqueue'] <> "")) {
/* If this is a child queue */
if(isset($queue['attachtoqueue'])) {
if(is_subqueue_used_on_interface($queue['attachtoqueue'], $interface)) {
$queue_cache['defq'][$interface] = $queue['name'];
return $queue['name'];
}
} else {
$queue_cache['defq'][$interface] = $queue['name'];
return $queue['name'];
}
}
}
}
}
function get_ack_queue($interface) {
global $config;
global $queue_cache;
/* quick return if we've already seen the ack queue for this interface */
if (isset($queue_cache['ackq'][$interface]))
return $queue_cache['ackq'][$interface];
$qconfig = $config;
if (is_array($qconfig['shaper']['queue'])) {
foreach ($qconfig['shaper']['queue'] as $queue) {
if(isset($queue['ack']))
if(isset($queue['attachtoqueue']))
if(is_subqueue_used_on_interface($queue['attachtoqueue'], $interface)) {
/* Add to cache */
$queue_cache['ackq'][$interface] = $queue['name'];
return $queue['name'];
}
}
}
}
function filter_generate_altq_queues($altq_ints) {
global $config;
$altq_rules = "";
if (is_array($config['shaper']['queue'])) {
foreach ($config['shaper']['queue'] as $rule) {
$options = "";
// check to make sure we're actually using this queue.
//if(stristr($altq_ints, $rule['name']) !== FALSE) {
$altq_rules .= "queue " . $rule['name'] . " ";
if (isset($rule['bandwidth']) and $rule['bandwidth'] <> "")
$altq_rules .= "bandwidth " . $rule['bandwidth'] . $rule['bandwidthtype'] . " ";
if (isset($rule['priority']) and $rule['priority'] <> "")
$altq_rules .= "priority " . $rule['priority'] . " ";
if(isset($rule['red']) and $rule['red'] <> "")
$options .= " red";
if(isset($rule['borrow']) and $rule['borrow'] <> "")
$options .= " borrow";
if(isset($rule['ecn']) and $rule['ecn'] <> "")
$options .= " ecn";
if(isset($rule['rio']) and $rule['rio'] <> "")
$options .= " rio";
if(isset($rule['defaultqueue']) and $rule['defaultqueue'] <> "")
$options .= " default";
if(isset($rule['upperlimit']) and $rule['upperlimit'] <> "") {
$options .= " upperlimit(" . $rule['upperlimit1'] . " " . $rule['upperlimit2'] . " " . $rule['upperlimit3'] . ")";
}
if(isset($rule['linkshare']) and $rule['linkshare'] <> "") {
$options .= " linkshare(" . $rule['linkshare1'] . " " . $rule['linkshare2'] . " " . $rule['linkshare3'] . ")";
}
if(isset($rule['realtime']) and $rule['realtime'] <> "") {
$options .= " realtime(" . $rule['realtime1'] . " " . $rule['realtime2'] . " " . $rule['realtime3'] . ")";
}
$scheduler_type = $config['system']['schedulertype'];
$altq_rules .= $scheduler_type . " ";
if($options)
$altq_rules .= "(". $options . " )";
$fsq="";
foreach($config['shaper']['queue'] as $q) {
if($q['attachtoqueue'] == $rule['name']) {
if($fsq == "")
$altq_rules .= "{ ";
else if($fsq == "1")
$altq_rules .= ", ";
$altq_rules .= $q['name'];
$fsq = "1";
}
}
if($fsq == "1")
$altq_rules .= " }";
$altq_rules .= "\n";
//}
}
}
return $altq_rules;
}
function filter_get_rule_real_interface($associatedrulenumber) {
global $config;
$x=0;
foreach($config['shaper']['rule'] as $rule) {
if($x == $associatedrulenumber)
return $rule['interface'];
$x++;
}
}
/* Find a queue that's attached to this one and see if that queue is used on this interface */
function is_subqueue_used_on_interface($queuename, $interface) {
global $config;
$qconfig = $config;
if (!is_array($qconfig['shaper']['queue'])) return 0;
foreach ($qconfig['shaper']['queue'] as $queue) {
if($queue['attachtoqueue'] == $queuename)
$subqueue_interface = filter_is_queue_being_used_on_interface($queue['name'], $interface);
/* Useful debugging code for when queues are messed up
* echo "{$subqueue_interface}/{$interface}/{$queue['name']}/{$queuename}\n";
*/
if ($subqueue_interface != ""){
return 1;
}
}
return 0;
}
function filter_is_queue_being_used_on_interface($queuename, $interface) {
global $config;
$lconfig = $config;
if(!is_array($lconfig['shaper']['rule'])) return;
foreach($lconfig['shaper']['rule'] as $rule) {
if(($rule['inqueue'] == $queuename && $rule['interface'] == $interface))
return $interface;
}
return;
}
function filter_setup_altq_interfaces() {
global $config;
$altq_rules = "";
$queue_names = "";
$is_first = "";
if(!is_array($config['shaper']['queue'])) return;
$ifdescrs = array('wan', 'lan');
for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
$ifdescrs[] = "opt" . $j;
}
foreach ($ifdescrs as $ifdescr => $ifname) {
$queue_names = "";
$is_first = "";
$workting_with_interface = $ifname;
foreach ($config['shaper']['queue'] as $queue) {
$rule_interface = "";
$q = $queue;
$rule_interface = filter_is_queue_being_used_on_interface($q['name'], $workting_with_interface);
if ($rule_interface == $workting_with_interface) {
if(!isset($q['attachtoqueue'])) {
if($is_first) $queue_names .= ", ";
$queue_names .= $q['name'];
$is_first = "1";
}
} else {
if(isset($q['parentqueue']) && ($q['parentqueue'] <> "")) {
if(is_subqueue_used_on_interface($q['name'], $workting_with_interface)) {
$queue_names .= " ";
$queue_names .= $q['name'];
$seen[$q['name']] = 1;
}
}
}
}
if($queue_names <> ""){
$altq_rules .= "altq on " . $config['interfaces'][$ifname]['if'] . " ";
if($config['interfaces'][$ifname]['bandwidth'] <> "")
$bandwidth = " bandwidth " . $config['interfaces'][$ifname]['bandwidth'] . $config['interfaces'][$ifname]['bandwidthtype'];
$altq_rules .= $config['system']['schedulertype'] . $bandwidth . " ";
$altq_rules .= "queue { " . $queue_names . " }";
}
$altq_rules .= "\n";
}
return $altq_rules;
}
function is_queue_attached_children($name) {
global $config;
$status = "";
if (!is_array($config['shaper']['queue'])) return 0;
foreach ($config['shaper']['queue'] as $queue) {
if($queue['attachtoqueue'] == $name) return 1;
}
return 0;
}
function queue_interface_recursive($queuename) {
global $config;
foreach($config['shaper']['queue'] as $queue) {
if($queue['attachtoqueue'] == $queuename) {
$status = queue_interface_recursive($queue['name']);
if($status <> "") return $status;
}
foreach($config['shaper']['rule'] as $rule) {
if($rule['inqueue'] == $queuename)
return $rule['interface'];
}
}
}
function is_subqueue($name) {
global $config;
$queues = $config['shaper']['queue']; /* must assign to keep from corrupting in memory $config */
$status = "";
if (!is_array($queues)) return 0;
foreach ($queues as $queue) {
if($queue['attachtoqueue'] == $name) return 1;
}
return 0;
}
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";
$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_generate_pf_altq_rules() {
/* I don't think we're in IPFW anymore Toto */
global $config, $g;
$wancfg = $config['interfaces']['wan'];
$lancfg = $config['interfaces']['lan'];
$pptpdcfg = $config['pptpd'];
$lanif = $lancfg['if'];
$wanif = get_real_wan_interface();
$lanip = $lancfg['ipaddr'];
$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
$lansn = $lancfg['subnet'];
/* optional interfaces */
$optcfg = array();
generate_optcfg_array($optcfg);
if ($pptpdcfg['mode'] == "server") {
$pptpip = $pptpdcfg['localip'];
$pptpsa = $pptpdcfg['remoteip'];
$pptpsn = $g['pptp_subnet'];
if($config['pptp']['pptp_subnet'] <> "")
$pptpsn = $config['pptp']['pptp_subnet'];
}
/* generate rules */
if (isset($config['shaper']['rule']))
foreach ($config['shaper']['rule'] as $rule) {
/* don't include disabled rules */
if (isset($rule['disabled'])) {
$i++;
continue;
}
/* does the rule deal with a PPTP interface? */
if ($rule['interface'] == "pptp") {
if ($pptpdcfg['mode'] != "server") {
$i++;
continue;
}
$nif = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$nif = $config['pptp']['n_pptp_units'];
$ispptp = true;
} else {
if (strstr($rule['interface'], "opt")) {
if (!array_key_exists($rule['interface'], $optcfg)) {
$i++;
continue;
}
}
$nif = 1;
$ispptp = false;
}
if ($pptpdcfg['mode'] != "server") {
if (($rule['source']['network'] == "pptp") ||
($rule['destination']['network'] == "pptp")) {
$i++;
continue;
}
}
if (strstr($rule['source']['network'], "opt")) {
if (!array_key_exists($rule['source']['network'], $optcfg)) {
$i++;
continue;
}
}
if (strstr($rule['destination']['network'], "opt")) {
if (!array_key_exists($rule['destination']['network'], $optcfg)) {
$i++;
continue;
}
}
/* check for unresolvable aliases */
if ($rule['source']['address'] && !alias_expand($rule['source']['address'])) {
$i++;
continue;
}
if ($rule['destination']['address'] && !alias_expand($rule['destination']['address'])) {
$i++;
continue;
}
for ($iif = 0; $iif < $nif; $iif++) {
foreach ( array('in', 'out') as $direction) {
$line = "pass {$direction} on ";
if ($ispptp) {
$line .= " ng" . ($iif+1);
} else {
$if = $config['interfaces'][$rule['interface']]['if'];
if ($rule['interface'] == "wan")
if($direction=="in")
$if = $wanif;
else
$if = $lanif;
else if($rule['interface'] == "lan")
if($direction=="in")
$if = $lanif;
else
$if = $wanif;
$line .= " {$if} ";
}
if (isset($rule['protocol'])) {
$line .= "proto {$rule['protocol']} ";
}
/* source address */
if (isset($rule['source']['any']) || $direction == "out") {
$src = "any";
} else if ($rule['source']['network']) {
if (strstr($rule['source']['network'], "opt")) {
$src = $optcfg[$rule['source']['network']]['sa'] . "/" .
$optcfg[$rule['source']['network']]['sn'];
} else {
switch ($rule['source']['network']) {
case 'lan':
$src = "$lansa/$lansn";
break;
case 'pptp':
$src = "$pptpsa/$pptpsn";
break;
}
}
} else if ($rule['source']['address']) {
$src = $rule['source']['address'];
}
if (!$src) {
printf("No source address found in rule $i\n");
break;
}
if (isset($rule['source']['not'])) {
$line .= "from ! $src ";
} else {
$line .= "from $src ";
}
if (!isset($rule['protocol']) || in_array($rule['protocol'], array("tcp","udp"))) {
if ($rule['source']['port']) {
/*
* Check to see if port is a alias. If so grab it and
* enclose it in { } to pass to pf.
*
* Otherwise combine the portrange into one if its only
* one item.
*/
$src = alias_expand($rule['source']['port']);
if($src <> "") {
$line .= "port {$rule['destination']['port']}";
} else {
$srcport = explode("-", $rule['source']['port']);
if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
$line .= "port {$srcport[0]} ";
} else {
$line .= "port {$srcport[0]}:{$srcport[1]} ";
}
}
}
}
/* 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'];
} else {
switch ($rule['destination']['network']) {
case 'lan':
$dst = "$lansa/$lansn";
break;
case 'pptp':
$dst = "$pptpsa/$pptpsn";
break;
}
}
} else if ($rule['destination']['address']) {
$dst = $rule['destination']['address'];
}
if (!$dst) {
printf("No destination address found in rule $i\n");
break;
}
if (isset($rule['destination']['not'])) {
$line .= "to ! $dst ";
} else {
$line .= "to $dst ";
}
if (!isset($rule['protocol']) || in_array($rule['protocol'], array("tcp","udp"))) {
if ($rule['destination']['port']) {
$dst = alias_expand($rule['destination']['port']);
/*
* Check to see if port is a alias. If so grab it and
* enclose it in { } to pass to pf.
*
* Otherwise combine the portrange into one if its only
* one item.
*/
if($dst <> "") {
$line .= "port {$rule['destination']['port']}";
} else {
$dstport = explode("-", $rule['destination']['port']);
if ((!$dstport[1]) || ($dstport[0] == $dstport[1])) {
$line .= "port {$dstport[0]} ";
} else {
$line .= "port {$dstport[0]}:{$dstport[1]} ";
}
}
}
}
if ($rule['iptos'])
$line .= "tos {$rule['iptos']} ";
/*
$inflags = explode(",", $rule['tcpflags']);
$flags = " flags ";
foreach ($tcpflags as $tcpflag) {
if (array_search($tcpflag, $inflags) !== false) {
if($tcpflag == "SYN")
$flags .= "S/";
else if($tcpflag == "CWR")
$flags .= "W";
else
$flags .= substr($tcpflag, 1, 1);
} else {
}
}
if($flags <> " flags ")
$line .= "{$flags} ";
*/
$qtag = "{$direction}queue";
$line .= " keep state tag {$rule[$qtag]} ";
$line .= "\n";
$shaperrules .= $line;
}
}
$i++;
}
return $shaperrules;
}
function filter_altq_get_queuename($queuenum) {
global $config;
$x=0;
foreach($config['shaper']['queue'] as $rule) {
if($x == $queuenum)
return $rule['name'];
$x++;
}
}
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;
$nat = "nat";
if($nonat <> "")
$nat = "no nat";
if ($target)
$tgt = $target . "/32";
else
$tgt = "($if)";
if ($dst == "")
$dst = "any";
if ($srcport)
$src .= " port " . $srcport;
if ($natport)
$tgt .= " port " . $natport;
$natrule = <<<EOD
$nat on $if from $src to $dst -> $tgt
EOD;
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'];
$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']['network'];
else
$dst = $obent['destination']['network'];
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'], $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, "");
}
$natrules .= filter_nat_rules_generate_if($wanif,
$lansa . "/" . $lancfg['subnet'], null, "", null, null, "");
/* 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']);
$natrules .= filter_nat_rules_generate_if($wanif,
$optsa . "/" . $optcfg['subnet'], null, "", null, null, "");
}
}
/* 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, "");
}
/* static routes
if (is_array($config['staticroutes']['route'])) {
foreach ($config['staticroutes']['route'] as $route) {
if ($route['interface'] != "wan")
$natrules .= filter_nat_rules_generate_if($wanif,
$route['network'], 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";
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'] <> "") {
$natrules .= "rdr on $wanif proto ipv6 from any to any port 0 -> " . "{$config['diag']['ipv6nat']['ipaddr']}\n";
}
if (isset($config['nat']['rule'])) {
$natrules .= "# NAT Inbound Redircts\n";
foreach ($config['nat']['rule'] as $rule) {
$extport = explode("-", $rule['external-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();
if (!$rule['interface'] || ($rule['interface'] == "wan"))
$natif = $wanif;
else if($rule['interface'] == "pptp")
$natif = "pptp";
else
$natif = $config['interfaces'][$rule['interface']]['if'];
$lanif = $lancfg['if'];
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 {$rule['local-port']}";
else
$natrules .=
"rdr on $natif proto " . $rule['protocol'] . " from any to {$extaddr} port {$extport[0]} -> {$target} port {$rule['local-port']}";
} 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)
$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();
/* captive portal redirects */
if(isset($config['captiveportal']['enable'])) {
$natrules .= "# Captive Portal\n";
$cp_interface = filter_translate_type_to_real_interface($config['captiveportal']['interface']);
$lan_ip = $config['interfaces']['lan']['ipaddr'];
$natrules .= "no rdr on {$cp_interface} proto tcp from <captiveportal> to any \n";
$natrules .= "rdr on " . $cp_interface . " proto tcp from any to any port { 80 443 } -> {$lan_ip} port 8000\n\n";
}
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'];
$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";
return;
}
/* do not process this rule if the attached interface is disabled */
// if(!isset($config['interfaces'][$rule['interface']]['enable']))
// return;
$pptpdcfg = $config['pptpd'];
if ($pptpdcfg['mode'] == "server") {
$pptpip = $pptpdcfg['localip'];
$pptpsa = $pptpdcfg['remoteip'];
$pptpsn = $g['pptp_subnet'];
if($config['pptp']['pptp_subnet'] <> "")
$pptpsn = $config['pptp']['pptp_subnet'];
}
/* does the rule deal with a PPTP interface? */
if ($rule['interface'] == "pptp") {
if ($pptpdcfg['mode'] != "server") {
return "# mode != server " . $rule['descr'] . "\n";
return;
}
$nif = $g['n_pptp_units'];
if($config['pptp']['n_pptp_units'] <> "")
$nif = $config['pptp']['n_pptp_units'];
$ispptp = 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'];
return;
}
}
$nif = 1;
$ispptp = false;
}
if ($pptpdcfg['mode'] != "server") {
if (($rule['source']['network'] == "pptp") ||
($rule['destination']['network'] == "pptp")) {
return "# source network or destination network == pptp on " . $rule['descr'];
return;
}
}
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'];
return;
}
}
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'];
return;
}
}
/* check for unresolvable aliases */
if ($rule['source']['address'] && !alias_expand($rule['source']['address'])) {
return "# unresolvable source aliases " . $rule['descr'];
return;
}
if ($rule['destination']['address'] && !alias_expand($rule['destination']['address'])) {
return "# unresolvable dest aliases " . $rule['descr'];
return;
}
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 {
$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 {
// 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("/tmp/{$rule['if']}_router"))
$rg = return_filename_as_string("/tmp/{$rule['if']}_router");
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'] <> "") {
$int = guess_interface_from_ip($rule['gateway']);
$line .= " route-to (" . $int . " " . $rg . ") ";
}
/* Setup outgoing load balancing */
if ($config['nat']['outgoingloadbalancing']['rule'] <> "" and
isset($config['nat']['outgoingloadbalancing']['enable'])
and $rule['interface'] == "lan") {
$tmprules = " route-to { ";
$isfirst = 0;
foreach ($config['nat']['outgoingloadbalancing']['rule'] as $rules) {
if($rules['gatewayip'] <> "") {
if($isfirst == 1) $tmprules .= ", ";
$lbinf = guess_interface_from_ip($rules['gatewayip']);
$tmprules .= " (" . $lbinf . " " . $rules['gatewayip'] . ") ";
$isfirst = 1;
}
}
$tmprules .= "} round-robin sticky-address ";
$line .= $tmprules;
}
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;
}
if (isset($rule['source']['not'])) $src = "!" . $src;
}
} else if ($rule['source']['address']) {
$not = "";
if (isset($rule['source']['not'])) $not = "!";
$src = "{ " . $not . alias_expand($rule['source']['address']). " }";
}
if (!$src || ($src == "/")) {
return "# at the break!";
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;
}
}
if (isset($rule['destination']['not'])) $dst = " !" . $dst;
} else if ($rule['destination']['address']) {
$not = "";
if (isset($rule['destination']['not'])) $not = "! ";
$dst = "{ " . $not . alias_expand($rule['destination']['address']) . " }";
}
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'] <> "") {
if($rule['statetype'] == "modulate state" or $rule['statetype'] == "synproxy state") {
if($rule['protocol'] == "tcp")
$line .= $rule['statetype'] . " ";
} elseif ($rule['statetype'] == "none") {
} else {
if($rule['statetype'] <> "")
$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'];
$lanif = $lancfg['if'];
$wanif = get_real_wan_interface();
$lanip = $lancfg['ipaddr'];
$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
$lansn = $lancfg['subnet'];
/* 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'];
}
/* 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'])
$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/$lansn 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;
}
/*
* captive portal, pf version
* first pass captive portal interface traffic to port 8000
* then block every incoming packet on the interface (non quick)
* then follow up with an allow items in the captiveportal anchor to flow
*/
if(isset($config['captiveportal']['enable'])) {
$cp_interface = filter_translate_type_to_real_interface($config['captiveportal']['interface']);
$cp_ip = find_interface_ip($cp_interface);
$lan_ip = $config['interfaces']['lan']['ipaddr'];
$ipfrules .= "\n# Setup Captive portal policy rules\n";
$ipfrules .= "table <captiveportal>\n";
$ipfrules .= "block in on {$cp_interface} from any to any label \"block items initially for cp\"\n";
$ipfrules .= "pass in on {$cp_interface} proto { tcp udp } from any to {$cp_ip} port { 8000 53 } keep state label \"pass items to cp web serv\"\n";
$ipfrules .= "pass in on {$cp_interface} proto { tcp udp } from any to {$lanip} port { 8000 53 } keep state label \"pass items to cp web serv\"\n";
$ipfrules .= "pass in on {$cp_interface} from <captiveportal> to any keep state label \"allow captive portal authd users\"\n";
}
$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 {
$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 {
$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");
/* deterimine 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'])) {
foreach ($config['ipsec']['tunnel'] as $tunnel) {
$remote_gateway = $tunnel['remote-gateway'];
$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'] ." udp\"\n";
$ipfrules .= "pass quick on " . $wanif . " proto udp from " . $remote_gateway . " to " . $ipsec_ip . " port = 500 keep state label \"". $tunnel['descr'] ." udp\"\n";
$ipfrules .= "pass quick on " . $wanif . " proto esp from " . $ipsec_ip . " to " . $remote_gateway . " keep state label \"IPSEC: ". $tunnel['descr'] ." esp proto\"\n";
$ipfrules .= "pass quick on " . $wanif . " proto esp from " . $remote_gateway . " to " . $ipsec_ip . " keep state label \"IPSEC: ". $tunnel['descr'] ." esp proto\"\n";
$ipfrules .= "pass quick on " . $wanif . " proto ah from " . $ipsec_ip . " to " . $remote_gateway . " keep state label \"IPSEC: ". $tunnel['descr'] ." ah proto\"\n";
$ipfrules .= "pass quick on " . $wanif . " proto ah from " . $remote_gateway . " to " . $ipsec_ip . " keep state label \"IPSEC: ". $tunnel['descr'] ." ah proto\"\n";
$ipfrules .= "pass quick on " . $lanif . " from " . $tunnel['remote-subnet'] . " to " . $local_subnet . " keep state label \"IPSEC: " . $tunnel['descr'] ."\"\n";
$ipfrules .= "pass quick on " . $lanif . " from " . $local_subnet . " to " . $tunnel['remote-subnet'] . " keep state label \"IPSEC: " . $tunnel['descr'] ."\"\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'];
$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 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\"\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') {
$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);
$cli = new XML_RPC_Client('/xmlrpc.php', $url);
$cli->setCredentials('admin', $password);
$resp = $cli->send($msg);
}
function carp_sync_client() {
global $config;
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'] != "" ) {
$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['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(count($sections) > 0) {
carp_sync_xml($synchronizetoip, $carp['password'], $sections, $sections_xml);
$cli = new XML_RPC_Client('/xmlrpc.php', $synchronizetoip);
$msg = new XML_RPC_Message('pfsense.filter_configure', array(new XML_RPC_Value($carp['password'], 'string')));
$cli->setCredentials('admin', $carp['password']);
$cli->send($msg);
}
}
}
}
}
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']} ";
}
?>
|