summaryrefslogtreecommitdiffstats
path: root/etc/inc/filter.inc
blob: 43ecafc77078459e529e55c9d68615a5206b386c (plain)
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
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
<?php
/* $Id$ */
/*
	filter.inc
	Copyright (C) 2004-2006 Scott Ullrich
        Copyright (C) 2005 	Bill Marquette
	Copyright (C) 2006 	Peter Allgeyer
	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");

if($config['system']['shapertype'] <> "m0n0")
	require_once ("shaper.inc");

/* holds the items that will be executed *AFTER* the filter is fully loaded */
$after_filter_configure_run = array();

function filter_pflog_start() {
	global $config, $g;
	
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_pflog_start() being called $mt\n";
	}

	$pid = `ps awwwux | grep -v "grep" | grep "tcpdump -s 256 -v -l -n -e -ttt -i pflog0"  | awk '{ print $2 }'`;
	if(!$pid)
		mwexec_bg("/usr/sbin/tcpdump -s 256 -v -l -n -e -ttt -i pflog0 | logger -t pf -p local0.info");

}

/* reload filter async */
function filter_configure() {
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_configure() being called $mt\n";
	}
	global $g;

	touch($g['tmp_path'] . "/filter_dirty");
}

/* reload filter sync */
function filter_configure_sync() {
	global $config, $g, $after_filter_configure_run;
	filter_pflog_start();
	update_filter_reload_status("Initializing");
	/* invalidate interface cache */
	get_interface_arr(true);
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_configure_sync() being called $mt\n";
	}

	/* load ipfw / dummynet early on if required */
	if($config['system']['dummynetshaper']) {
		$status = intval(`kldstat | grep ipfw | wc -l | awk '{ print $1 }'`);
		if($status == "0") {
			mwexec("/sbin/kldload ipfw");
			mwexec("/sbin/kldload dummynet");
		}
	} else {	
		/* check to see if any rules reference a schedule
		 * and if so load ipfw for later usage.
		 */
		foreach($config['filter']['rule'] as $rule) {
			if($rule['sched'])
				$time_based_rules = true;
		}
		if($time_based_rules == true) {
			$status = intval(`kldstat | grep ipfw | wc -l | awk '{ print $1 }'`);
			if($status == "0") {
				mute_kernel_msgs();
				mwexec("/sbin/kldload ipfw");
				unmute_kernel_msgs();
			}
			if ($config['system']['maximumstates'] <> "" && is_numeric($config['system']['maximumstates'])) {
			        /* Set ipfw states to user defined maximum states in Advanced menu. */
			        mwexec("sysctl net.inet.ip.fw.dyn_max={$config['system']['maximumstates']}");
			} else {
			        /* Set to default 10,000 */
			        mwexec("sysctl net.inet.ip.fw.dyn_max=10000");
			}
			exec("/sbin/ipfw delete set 9");
			exec("/sbin/ipfw delete 2");
			exec("/sbin/ipfw delete 3");
			}
        }

	$lan_if = $config['interfaces']['lan']['if'];
	$wan_if = get_real_wan_interface();

	/* generate aliases */
	if($g['booting'] == true) echo ".";
	update_filter_reload_status("Creating aliases");
	$aliases = filter_generate_aliases();
	/* generate nat rules */
	  if($g['booting'] == true) echo ".";
	update_filter_reload_status("Generating NAT rules");
	$natrules = filter_nat_rules_generate();
	/* generate pfctl rules */
	if($g['booting'] == true) echo ".";
	update_filter_reload_status("Generating filter rules");
	$pfrules = filter_rules_generate();

	if (isset($config['shaper']['enable']) and $config['system']['shapertype'] <> "m0n0") {
		/* generate altq interface setup parms */
		if($g['booting'] == true) echo ".";
		update_filter_reload_status("Generating ALTQ interfaces");
		$altq_ints = filter_setup_altq_interfaces();
		/* generate altq queues */
		if($g['booting'] == true) echo ".";
		update_filter_reload_status("Generating ALTQ queues");
		$altq_queues = filter_generate_altq_queues($altq_ints);
		/* generate altq rules */
		if($g['booting'] == true) echo ".";
		/* Setup a default rule that tags ALL packets as unshaped
		 * we'll match only unshaped packets in the shaper code later
		 * this allows the shaper to be first match
		 */
		$pf_altq_rules = "block in all tag unshaped label \"SHAPER: first match rule\"\n";
		update_filter_reload_status("Generating ALTQ rules");
		$pf_altq_rules .= filter_generate_pf_altq_rules();
	}

	update_filter_reload_status("Loading filter rules");

	/* enable pf if we need to, otherwise disable */
	if (!isset ($config['system']['disablefilter'])) {
		mwexec("/sbin/pfctl -e", true);
	} else {
		mwexec("/sbin/pfctl -d");
		unlink_if_exists("{$g['tmp_path']}/filter_loading");
		update_filter_reload_status("Filter is disabled.  Not loading rules.");
		return;
	}

	// Copy rules.debug to rules.debug.old
	if(file_exists("{$g['tmp_path']}/rules.debug"))
		exec("cp {$g['tmp_path']}/rules.debug {$g['tmp_path']}/rules.debug.old");

	$fd = fopen("{$g['tmp_path']}/rules.debug", "w");
	$rules = $aliases . " \n";

	update_filter_reload_status("Setting up logging information");

	$rules .= setup_logging_interfaces();

	if ($config['system']['optimization'] <> "") {
		$rules .= "set optimization {$config['system']['optimization']}\n";
		if ($config['system']['optimization'] == "conservative") {
			$rules .= "set timeout { udp.first 300, udp.single 150, udp.multiple 900 }\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 .= "set skip on pfsync0\n";

	update_filter_reload_status("Setting up SCRUB information");
	/* get our wan interface? */
	$wanif = get_real_wan_interface();

	/* disable scrub option */
	if(!isset($config['system']['disablescrub'])) {
		/* 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 = "";
	
		/* configure no-df for linux nfs and others */
		if ($config['system']['scrubnodf'])
			$scrubnodf = "no-df random-id";
		else
			$scrubnodf = "random-id";
		$rules .= "scrub all {$scrubnodf} {$mssclamp} fragment reassemble\n"; // reassemble all directions
	} else if ($config['interfaces']['wan']['mtu'] <> "" and is_numeric($config['interfaces']['wan']['mtu'])) {
		$rules .= "scrub {$mssclamp}\n"; // reassemble all directions
	}

	if($config['system']['shapertype'] <> "m0n0") {
		$rules.= "{$altq_ints}\n";
		$rules.= "{$altq_queues}\n";
	}
	$rules.= "{$natrules}\n";
	if($config['system']['shapertype'] <> "m0n0") 
		$rules.= "{$pf_altq_rules}\n";
	$rules.= "{$pfrules}\n";
	fwrite($fd, $rules);
        fclose($fd);

	$rules = "1"; // force to be diff from oldrules
	$oldrules = "2"; // force to be diff from rules

	if(file_exists("{$g['tmp_path']}/rules.debug"))
		$rules = file_get_contents("{$g['tmp_path']}/rules.debug");
	if(file_exists("{$g['tmp_path']}/rules.debug.old"))
		$oldrules = file_get_contents("{$g['tmp_path']}/rules.debug.old");

	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "pfctl being called at $mt\n";
	}
        $rules_loading = mwexec("/sbin/pfctl -o basic -f {$g['tmp_path']}/rules.debug");
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "pfctl done at $mt\n";
	}

	/* 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]}";
		if($line_error and $line_number) {
			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}");
			update_filter_reload_status("There were error(s) loading the rules: {$rules_error} - {$line_error}");
			return;
		}
	}

	unlink_if_exists("/usr/local/pkg/pf/carp_sync_client.php");

	/* run items scheduled for after filter configure run */
	$fda = fopen("/tmp/commands.txt", "w");
	foreach($after_filter_configure_run as $afcr) 
		fwrite($fda, $afcr . "\n");
	fclose($fda);
	if(file_exists("/tmp/commands.txt")) {
		mwexec("sh /tmp/commands.txt &");
		unlink("/tmp/commands.txt");
	}

	update_filter_reload_status("Running plugins");

	if(is_dir("/usr/local/pkg/pf/")) {
		/* process packager manager custom rules */
		update_filter_reload_status("Running plugins (pf)");
		run_plugins("/usr/local/pkg/pf/");
		update_filter_reload_status("Plugins completed.");
	}

	system_start_ftp_helpers();
	
	if($config['system']['shapertype'] == "m0n0") {
		require_once ("/etc/inc/m0n0/shaper.inc");
		shaper_configure();	
	}

	/* if time based rules are enabled then swap in the set */
	if($time_based_rules == true) {
		tdr_install_cron(true);
		tdr_install_set();
	} else {
		tdr_install_cron(false);
	}

	/*
	    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']);

	/* sync carp entries to other firewalls */
	update_filter_reload_status("Syncing CARP data");
	carp_sync_client();

	system_routing_configure();
	
	update_filter_reload_status("Done");

	return 0;
}

function filter_generate_aliases() {
	global $config, $g;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_generate_aliases() being called $mt\n";
	}
	$aliases = "";

	$i = 0;

	/* resolve interface ip addresses */
	$lanip = find_interface_ip($config['interfaces']['lan']['if']);
	$wanip = find_interface_ip(get_real_wan_interface());

	/* set wan and lan carp ips that need to be linked */
	$lan_carp_ints = link_ip_to_carp_interface($lanip);
	$wan_carp_ints = link_ip_to_carp_interface($wanip);

	/* start creating alias info */
	$aliases .= "# System Aliases \n";
	$aliases .= "loopback = \"{ lo0 }\"\n";
	
	/* lan */
	$aliases .= "lan = \"{ {$config['interfaces']['lan']['if']}{$lan_aliases} {$lan_carp_ints} }\"\n";

	/* wan */
    if($config['interfaces']['wan']['ipaddr'] == "pppoe" or $config['interfaces']['wan']['ipaddr'] == "pptp") {
		$aliases .= "ng0 = \"{ " . $config['interfaces']['wan']['if'] . " " . get_real_wan_interface() . " }\" \n";
		$aliases .= "wan = \"{ " . $config['interfaces']['wan']['if'] . " ng0 {$wan_carp_ints} }\"\n";
	} else {
		$aliases .= "wan = \"{ " . get_real_wan_interface() . " {$wanaliases} {$wan_carp_ints} }\"\n";
	}

	/* IPSec */
	$aliases .= "enc0 = \"{ enc0 }\"\n";

    /* used to count netgraph interfaces */
    $counter = 0;

    /* ng ordering is VERY important here.  do not alter order */
    if($config['pptpd']['mode'] == "server") {
		/* build pptp alias */
		$tmp = "pptp = \"{ ";
		$starting_pptp = 1;
		if($config['interfaces']['wan']['ipaddr'] == "pppoe")
			$starting_pptp = 1;
		for($x=$starting_pptp; $x<$g["n_pptp_units"]+$starting_pptp; $x++)
			$tmp .= "ng{$x} ";
		$counter = $x;
		$tmp .= "}\" \n";
		if($counter > 0)
			$aliases .= $tmp;
	}
    if($config['pppoe']['mode'] == "server") {
		/* build pppoe alias */
		$tmp = "pppoe = \"{ ";
		$starting_pppoe = 1;
		if($config['interfaces']['wan']['ipaddr'] == "pppoe")
			$starting_pppoe = 1;
		for($x=0; $x<$g["n_pppoe_units"]+$starting_pppoe; $x++) {
			$tmp .= "ng{$counter} ";
			$counter++;
		}
		$tmp .= "}\" \n";
		if($x > 0)
			$aliases .= $tmp;
	}

	$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 */
		/* do process tun interfaces for openvpn compatibility */
		/* if(stristr(filter_opt_interface_to_real($ifname), "tun") == true) continue; */
		$aliases .= convert_friendly_interface_to_friendly_descr($ifname) . " = \"{ " . filter_opt_interface_to_real($ifname);
//		if(link_int_to_bridge_interface($ifname))
//			$aliases .= " " . link_int_to_bridge_interface($ifname);
		$optip = find_interface_ip($config['interfaces'][$ifname]['if']);
		if($optip) {
			$opt_carp_ints = link_ip_to_carp_interface($optip);
			if($opt_carp_ints)
				$aliases .= $opt_carp_ints;
		}
		$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 get_vpns_list() {
	global $config;
	/* build list of vpns */
	$vpns = "";
	$isfirst = true;
	/* ipsec */
	if ($config['ipsec']['tunnel']) {
		foreach ($config['ipsec']['tunnel'] as $tunnel) {
			if ($isfirst == false)
				$vpns .= " ";
			$vpns .= $tunnel['remote-subnet'];
			$isfirst = false;
		}
	}
	/* openvpn */
	foreach (array('client', 'server') as $type) {
		$conf =& $config['installedpackages']["openvpn$type"]['config'];
		if (!is_array($conf)) continue;
		foreach ($conf as $tunnel) {
			if ($isfirst == false)
				$vpns .= " ";
			$vpns .= $tunnel['remote_network'];
			$isfirst = false;
		}
	}
	/* pppoe */
	if ($config['pppoe']['remoteip']) {
		if ($isfirst == false)
			$vpns .= " ";
		$vpns .= $config['pppoe']['remoteip'] ."/". $config['pppoe']['pppoe_subnet'];
		$isfirst = false;
	}
	$vpns .= " ";
	return $vpns;
}

function generate_optcfg_array(& $optcfg) {
	global $config;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "generate_optcfg_array() being called $mt\n";
	}

        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']);
								$oic['descr'] = $oc['descr'];
                        }

                        $optcfg['opt' . $i] = $oic;
                }
        }
}

function filter_flush_nat_table() {
	global $config, $g;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_flush_nat_table() being called $mt\n";
	}
	return mwexec("/sbin/pfctl -F nat");
}

function filter_flush_state_table() {
	global $config, $g;

	return mwexec("/sbin/pfctl -F state");
}

/* Generate a 'nat on' or 'no nat on' rule for given interface */
function filter_nat_rules_generate_if($if, $src = "any", $srcport = "", $dst = "any", $dstport = "", $natip = "", $natport = "", $nonat = false, $staticnatport = false) {
	global $config;

	/* XXX: billm - any idea if this code is needed? */
	if($src == "/32" || $src{0} == "/")
		return;

	/* Use interface name if IP isn't specified */
        if ($natip != "")
                $tgt = "{$natip}/32";
        else
                $tgt = "($if)";

	/* Add the hard set source port (useful for ISAKMP) */
        if ($natport != "")
		$tgt .= " port {$natport}";

	/* sometimes this gets called with "" instead of a value */
	if ($src == "")
		$src = "any";

	/* Match on this source port */
        if ($srcport != "")
		$src .= " port {$srcport}";

	/* sometimes this gets called with "" instead of a value */
	if ($dst == "")
		$dst = "any";

	/* Match on this dest port */
        if ($dstport != "")
		$dst .= " port {$dstport}";

	/* Allow for negating NAT entries */
	if ($nonat) {
		$nat = "no nat";
		$target = "";
	} else {
		$nat = "nat";
		$target = "-> {$tgt}";
	}

	/* outgoing static-port option, hamachi, Grandstream, VOIP, etc */
	if($staticnatport)
		$staticnatport_txt = " static-port";
	else
		if(!$natport)
			$staticnatport_txt = " port 1024:65535"; // set source port range
		else
			$staticnatport_txt = "";

	$if_friendly = convert_real_interface_to_friendly_descr($if);

	/* Put all the pieces together */
	if($if_friendly)
		$natrule = "{$nat} on \${$if_friendly} from {$src} to {$dst} {$target}{$staticnatport_txt}\n";

	return $natrule;
}

function is_one_to_one_or_server_nat_rule($iptocheck) {
	global $config, $target;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "is_one_to_one_or_server_nat_rule() being called $mt\n";
	}

	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, $after_filter_configure_run;

	$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 .= "nat-anchor \"pftpx/*\"\n";

	$natrules .= "nat-anchor \"natearly/*\"\n";
	$natrules .= "nat-anchor \"natrules/*\"\n";

	$natrules .= "# FTP proxy\n";
	$natrules .= "rdr-anchor \"pftpx/*\"\n";

	update_filter_reload_status("Creating 1:1 rules...");

	/* 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'];

			if($natif)
				$natrules .= "binat on $natif from {$natent['internal']}/{$sn} to any -> {$natent['external']}/{$sn}\n";
		}
	}

	$natrules .= "\n# Outbound NAT rules\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) {

				update_filter_reload_status("Creating advanced outbound rule {$obent['descr']}");

				$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['dstport'],
					$obent['target'],
					$obent['natport'],
					isset($obent['nonat']),
					isset($obent['staticnatport'])
					);
			}
		}
	} else {
		/* standard outbound rules (one for each interface) */
		update_filter_reload_status("Creating outbound NAT rules");

		$natrules .= filter_nat_rules_generate_if($wanif,
			"{$lansa}/{$lancfg['subnet']}", 500, "", 500, null, 500, false);
		$natrules .= filter_nat_rules_generate_if($wanif,
			"{$lansa}/{$lancfg['subnet']}", 5060, "", 5060, null, 5060, false);
		$natrules .= filter_nat_rules_generate_if($wanif,
			"{$lansa}/{$lancfg['subnet']}");

		$optints = array();
		generate_optcfg_array($optints);

		/* generate lan nat mappings for opts with a gateway opts */
		foreach($optints as $oc) {
			$opt_interface = $oc['if'];
			if (interface_has_gateway("$opt_interface")) {
				$natrules .= filter_nat_rules_generate_if($opt_interface,
					"{$lansa}/{$lancfg['subnet']}", 500, "", 500, null, 500, false);
				$natrules .= filter_nat_rules_generate_if($opt_interface,
					"{$lansa}/{$lancfg['subnet']}", 5060, "", 5060, null, 5060, false);
				$natrules .= filter_nat_rules_generate_if($opt_interface,
					"{$lansa}/{$lancfg['subnet']}");
			}
		}

		/* optional interfaces */
		for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
			update_filter_reload_status("Creating outbound rules (opt{$i})");
			$optcfg = $config['interfaces']['opt' . $i];

			if ((isset ($optcfg['enable'])) && (!$optcfg['bridge']) && (!interface_has_gateway("opt{$i}"))) {
				$optsa = gen_subnet($optcfg['ipaddr'], $optcfg['subnet']);

				/* create outbound nat entries for primary wan */
				$natrules .= filter_nat_rules_generate_if($wanif,
					"{$optsa}/{$optcfg['subnet']}", 500, "", 500, null, 500, false);
				$natrules .= filter_nat_rules_generate_if($wanif,
					"{$optsa}/{$optcfg['subnet']}", 5060, "", 5060, null, 5060, false);
				$natrules .= filter_nat_rules_generate_if($wanif,
					"{$optsa}/{$optcfg['subnet']}", null, "", null, null, null, isset($optcfg['nonat']));

				/* create outbound nat entries for all opt wans */
				foreach($optints as $oc) {
					$opt_interface = $oc['if'];
					if (interface_has_gateway("$opt_interface")) {
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$optsa}/{$optcfg['subnet']}", 500, "", 500, null, 500, false);
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$optsa}/{$optcfg['subnet']}", 5060, "", 5060, null, 5060, false);
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$optsa}/{$optcfg['subnet']}", null, "", 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}", 500, "", 500, null, 500, false);
			$natrules .= filter_nat_rules_generate_if($wanif,
				"{$pptpdcfg['remoteip']}/{$pptp_subnet}", 5060, "", 5060, null, 5060, false);
			$natrules .= filter_nat_rules_generate_if($wanif,
				"{$pptpdcfg['remoteip']}/{$pptp_subnet}");

			/* generate nat mappings for opts with a gateway opts */
			foreach($optints as $oc) {
				$opt_interface = $oc['if'];
				if ((is_private_ip($pptpdcfg['remoteip'])) && (interface_has_gateway($opt_interface))) {
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pptpdcfg['remoteip']}/{$pptp_subnet}", 500, "", 500, null, 500, false);
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pptpdcfg['remoteip']}/{$pptp_subnet}", 5060, "", 5060, null, 5060, false);
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pptpdcfg['remoteip']}/{$pptp_subnet}");
				}
			}
		}

		/* 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}", 500, "", 500, null, 500, false);
			$natrules .= filter_nat_rules_generate_if($wanif,
				"{$pppoecfg['remoteip']}/{$pppoe_subnet}", 5060, "", 5060, null, 5060, false);
			$natrules .= filter_nat_rules_generate_if($wanif,
				"{$pppoecfg['remoteip']}/{$pppoe_subnet}");

			/* generate nat mappings for opts with a gateway opts */
			foreach($optints as $oc) {
				$opt_interface = $oc['if'];
				if ((is_private_ip($pppoecfg['remoteip'])) && (interface_has_gateway($opt_interface))) {
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pppoecfg['remoteip']}/{$pppoe_subnet}", 500, "", 500, null, 500, false);
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pppoecfg['remoteip']}/{$pppoe_subnet}", 5060, "", 5060, null, 5060, false);
					$natrules .= filter_nat_rules_generate_if($opt_interface,
						"{$pppoecfg['remoteip']}/{$pppoe_subnet}");
				}
			}
		}

		/* static routes */
		if (is_array($config['staticroutes']['route'])) {
			foreach ($config['staticroutes']['route'] as $route) {
				$netip = explode("/", $route['network']);
				if ((! interface_has_gateway($route['interface'])) && (is_private_ip($netip[0]))) {
					$natrules .= filter_nat_rules_generate_if($wanif,
						"{$route['network']}", 500, "", 500, null, 500, false);
					$natrules .= filter_nat_rules_generate_if($wanif,
						"{$route['network']}", 5060, "", 5060, null, 5060, false);
					$natrules .= filter_nat_rules_generate_if($wanif,
						"{$route['network']}", "", null);
				}
				/* generate nat mapping for static routes on opts */
				foreach($optints as $oc) {
					$opt_interface = $oc['if'];
					if ((! interface_has_gateway($route['interface'])) && (is_private_ip($netip[0])) && (interface_has_gateway($opt_interface))) {
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$route['network']}", 500, "", 500, null, 500, false);
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$route['network']}", 5060, "", 5060, null, 5060, false);
						$natrules .= filter_nat_rules_generate_if($opt_interface,
							"{$route['network']}", "", null);
					}
				}

			}
		}

	}

	$natrules .= "\n#SSH Lockout Table\n";
	$natrules .= "table <sshlockout> persist\n\n";

	/* is SPAMD insalled? */
	if (is_package_installed("spamd") == 1) {
		$natrules .= "\n# spam table \n";

		$natrules .= "table <whitelist> persist\n";
		$natrules .= "table <blacklist> persist\n";
		$natrules .= "table <spamd> persist\n";
		if(file_exists("/var/db/whitelist.txt"))
			$natrules .= "table <spamd-white> persist file \"/var/db/whitelist.txt\"\n";
		$natrules .= "rdr pass on {$wanif} proto tcp from <blacklist> to port smtp -> 127.0.0.1 port spamd\n";
		$natrules .= "rdr pass on {$wanif} proto tcp from <spamd> to port smtp -> 127.0.0.1 port spamd\n";
		$natrules .= "rdr pass on {$wanif} proto tcp from !<spamd-white> to port smtp -> 127.0.0.1 port spamd\n";
		if($config['installedpackages']['spamdsettings']['config'])
			foreach($config['installedpackages']['spamdsettings']['config'] as $ss)
				$nextmta = $ss['nextmta'];
		if($nextmta <> "") {
			$natrules .= "rdr pass on {$wanif} proto tcp from <spamd-white> to port smtp -> {$nextmta} port smtp\n";
		}
	}

	/* load balancer anchor */
	$natrules .= "\n# Load balancing anchor - slbd updates\n";
	$natrules .= "rdr-anchor \"slb\"\n";

	update_filter_reload_status("Setting up FTP helper");

	$natrules .= "\n# FTP Proxy/helper\n";
	/* build an array of interfaces to work with */
	$iflist = array("lan" => "LAN");
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
		$iflist['opt' . $i] = "opt{$i}";
	$interface_counter = 0;
	$vpns_list = get_vpns_list();
	/* prevent 1:1 ips from pftpx, they will be handled by ftp-sesame */
	if($config['nat']['onetoone'])
		foreach ($config['nat']['onetoone'] as $vipent)
			$onetoone_list .= "{$vipent['internal']} ";
	if($onetoone_list)
		$natrules .= "table <onetoonelist> { $onetoone_list }\n";
	if($vpns_list)
		$natrules .= "table <vpns> { $vpns_list }\n";
	/* loop through all interfaces and handle pftpx redirections */
	foreach ($iflist as $ifent => $ifname) {
		$ifname_lower = convert_friendly_interface_to_friendly_descr(strtolower($ifname));
		$realif = convert_friendly_interface_to_real_interface_name(strtolower($ifname));
		$int_ip = find_interface_ip($realif);
		if(isset($config['interfaces'][strtolower($ifname)]['disableftpproxy'])) {
			if($g['debug'])
				log_error("Filter: FTP proxy disabled for interface {$ifname} - ignoring.");
			$interface_counter++;
			continue;
		}
		if(stristr($ifname, "opt")) {
			if(!isset($config['interfaces'][$ifname]['enable'])) {
				continue;
			}
		}
		/* are we in routed mode? no source nat rules and not a outside interface? */
		/* If we have advanced outbound nat we skip the FTP proxy, we use ftpsesame */
		if((isset($config['nat']['advancedoutbound']['enable'])) && (! interface_has_gateway($ifname))) {
			$sourcenat = 0;
			/* we are using advanced outbound nat, are we in routing mode? */
			$realif = convert_friendly_interface_to_real_interface_name($ifname);
			/* if the interface address lies within a outbound NAT source network we should skip */
			if(! empty($config['nat']['advancedoutbound']['rule'])) {
				foreach($config['nat']['advancedoutbound']['rule'] as $natnetwork) {
					if(ip_in_subnet($int_ip, $natnetwork['source']['network'])) {
						/* if the interface address is matched in the AON Rule we need the ftp proxy */
						$sourcenat++;
					}
				}
			}
			if($sourcenat == 0) {
				if($g['debug'])
					log_error("Filter: No AON rule matched for interface {$ifname} - not using the FTP proxy");
				$interface_counter++;
				continue;
			} else {
				if($g['debug'])
					log_error("Filter: AON Rule matched for interface {$ifname} - using FTP proxy");
			}
		}
		$tmp_port = 8021 + $interface_counter;
		$tmp_interface = convert_friendly_interface_to_real_interface_name($ifname);
		$ifname_lower = strtolower(convert_friendly_interface_to_friendly_descr($ifname));
		$vpns = get_vpns_list();
		/* if the user has defined, include the alias so that we do not redirect ftp
		   connections across the tunnels to pftpx */
		$int_ip = find_interface_ip($tmp_interface);
		/* if interface lacks an ip, dont setup a rdr for ftp.  they are most likely on a bridged interface */
		if($int_ip and $vpns_list)
			if($ifname_lower) {
				$natrules .= "no rdr on $tmp_interface proto tcp from any to <vpns> port 21\n";
				if($onetoone_list)
					$natrules .= "no rdr on $tmp_interface proto tcp from <onetoonelist> to any port 21\n";
			}
		if($ifname_lower)
			$natrules .= "rdr on $tmp_interface proto tcp from any to any port 21 -> 127.0.0.1 port {$tmp_port}\n";
		$interface_counter++;
	}
	$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 \$wan proto ipv6 from any to any -> {$config['diag']['ipv6nat']['ipaddr']}\n";
	}

	if(file_exists("/var/etc/inetd.conf"))
		mwexec("rm /var/etc/inetd.conf");
	touch("/var/etc/inetd.conf");

	if (isset($config['nat']['rule'])) {
		$natrules .= "# NAT Inbound Redirects\n";

		if(!isset($config['system']['disablenatreflection'])) {
			$inetd_fd = fopen("/var/etc/inetd.conf","w");
			/* start redirects on port 19000 of localhost */
			$starting_localhost_port = 19000;
		}

		foreach ($config['nat']['rule'] as $rule) {

			update_filter_reload_status("Creating NAT rule {$rule['descr']}");

			/* if item is an alias, expand */
			$extport = "";
			unset($extport);
			if(alias_expand($rule['external-port']))
				$extport[0] = alias_expand_value($rule['external-port']);
			else
				$extport = explode("-", $rule['external-port']);

			/* if item is an alias, expand */
			if(alias_expand($rule['local-port']))
				$localport = "";
			else
				$localport = " port {$rule['local-port']}";

			$target = alias_expand_host($rule['target']);

			if (!$target) {
				$natrules .= "# Unresolvable alias {$rule['target']}\n";
				continue;       /* unresolvable alias */
			}

			# use tables for aliases in rdr
			if (!is_ipaddr($target)) {
				$natrules .= "table <{$rule['target']}> { $target }\n";
				$target = "<{$rule['target']}>";
			}

			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($target))
				$target = alias_expand($target);
			if(alias_expand($extaddr))
				$extaddr = alias_expand($extaddr);

			/*
			 *    If FTP Proxy Helper is enabled and the
			 *    operator has requested a port forward to
			 *    a ftp server then launch a helper
			 */
			$dontinstallrdr = false;
			if($target <> "") {
				$external_address = $rule['external-address'];
				if($extport[0] == "21" and !isset($config['interfaces'][strtolower($rule['interface'])]['disableftpproxy'])) {
					$helpers = exec("/bin/ps awux | grep \"{$target} -b {$external_address}\" | grep -v grep");
					if(!$helpers) {
						if($external_address == "")
							$external_address = find_interface_ip(get_real_wan_interface());
						/*   install a pftpx helper, do not set a rule.  also use the delay filter configure run
						 *   routines because if this is the first bootup the filter is not completely configured
						 *   and thus pf is not fully running.   otherwise we end up with: pftpx: pf is disabled
						 */
						if(isset($config['shaper']['enable'])) {
							if(isset($config['ezshaper']['step5']['p2pcatchall']))  {
								$shaper_queue = "-q qP2PUp ";
							} else {
								$upq = "q" . convert_friendly_interface_to_friendly_descr($config['ezshaper']['step2']['outside_int']);
								$shaper_queue = "-q {$upq}def ";
							}
						} else {
							$shaper_queue = "";
						}
						$after_filter_configure_run[] = "/usr/local/sbin/pftpx {$shaper_queue}-f {$target} -b {$external_address} -c 21 -g 21";
					}
					$dontinstallrdr = true;
				}
			}

			if($extaddr == "")
				$dontinstallrdr = true;

			$rdr_on = convert_real_interface_to_friendly_descr($rule['interface']);

			if($dontinstallrdr == false) {
				/* is rule a port range? */
				if ((!$extport[1]) || ($extport[0] == $extport[1])) {

					switch ($rule['protocol']) {
						case "tcp/udp":
							if($natif) {
								if($rule['external-port'] <> $rule['local-port'])
									$natrules .= "{$nordr}rdr on $natif proto { tcp udp } from any to {$extaddr} port { {$extport[0]} } -> {$target}{$localport}";
								else
									$natrules .= "{$nordr}rdr on $natif proto { tcp udp } from any to {$extaddr} port { {$extport[0]} } -> {$target}";
							}
							break;
						case "udp":
						case "tcp":
							if($extport[0])
								if($natif) {
									if($rule['external-port'] <> $rule['local-port'])
										$natrules .= "rdr on $natif proto {$rule['protocol']} from any to {$extaddr} port { {$extport[0]} } -> {$target}{$localport}";
									else
										$natrules .= "rdr on $natif proto {$rule['protocol']} from any to {$extaddr} port { {$extport[0]} } -> {$target}";
								}
							else
								if($natif)
									$natrules .= "rdr on $natif proto {$rule['protocol']} from any to {$extaddr} -> {$target}{$localport}";
							break;
						default:
							$natrules .= "rdr on $natif proto {$rule['protocol']} from any to {$extaddr} -> {$target}";
							break;
					}
				} else {
					switch ($rule['protocol']) {
						case "tcp/udp":
							if($natif)
								$natrules .= "{$nordr}rdr on $natif proto { tcp udp } from any to {$extaddr} port {$extport[0]}:{$extport[1]} -> {$target}{$localport}:*";
							break;
						case "udp":
						case "tcp":
							if($natif)
								$natrules .= "{$nordr}rdr on $natif proto {$rule['protocol']} from any to {$extaddr} port {$extport[0]}:{$extport[1]} -> {$target}{$localport}:*";
							break;
						default:
							if($natif)
								$natrules .= "{$nordr}rdr on $natif proto {$rule['protocol']} from any to {$extaddr} -> {$target}";
					}
				}
			}

			/*    does this rule redirect back to a internal host?
			 *    if so, add some extra goo to help this work.
			 */
			$rule_friendly_if  = convert_friendly_interface_to_real_interface_name($rule['interface']);
			$rule_interface_ip = find_interface_ip($rule_friendly_if);
			$rule_interface_subnet = $config['interfaces'][$rule['interface']]['subnet'];
			$rule_subnet = gen_subnet($rule_interface_ip, $rule_interface_subnet);
			if($rule['external-address'] == "any" and $rule['interface'] == "lan") {
				$natrules .= "\n";
				if($rule_friendly_if)
					$natrules .= "no nat on {$rule_friendly_if} proto tcp from {$rule_friendly_if} to {$rule_subnet}/{$rule_interface_subnet}\n";
				if($rule_friendly_if)
					$natrules .= "nat on {$rule_friendly_if} proto tcp from {$rule_subnet}/{$rule_interface_subnet} to {$target} port {$extport[0]} -> {$rule_friendly_if}\n";
			}

			if(!isset($config['system']['disablenatreflection'])) {

				update_filter_reload_status("Setting up reflection");

				$natrules .= "\n# Reflection redirects\n";
				foreach ($iflist as $ifent => $ifname) {

					/* do not process interfaces with gateways*/
					if($config['interfaces'][$ifname]['gateway'] <> "")
						continue;

					/* do not process interfaces that will end up with gateways */
					if($config['interfaces'][$ifname]['ipaddr'] == "dhcp" or
					   $config['interfaces'][$ifname]['ipaddr'] == "bigpond" or
					   $config['interfaces'][$ifname]['ipaddr'] == "pppoe" or
					   $config['interfaces'][$ifname]['ipaddr'] == "pptp")
						continue;

					$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);

					if($extport[1])
						$range_end = ($extport[1]);
					else
						$range_end = ($extport[0]);

					$range_end++;

					if($rule['local-port'])
						$lrange_start = $rule['local-port'];

					if($range_end - $extport[0] > 500) {
						$range_end = $extport[0]+1;
						log_error("Not installing nat reflection rules for a port range > 500");
					} else {
						/* only install reflection rules for < 19991 items */
						if($starting_localhost_port < 19991) {
							$loc_pt = $lrange_start;
							for($x=$extport[0]; $x<$range_end; $x++) {

								$xxx = $x;

								/*   do not install reflection rules for FTP.  This simply
								 *   opens up pandoras box.
								 */
								if($xxx == "21")
									continue;

								update_filter_reload_status("Creating reflection rule for {$rule['descr']}...");

								$ifname_real = convert_friendly_interface_to_friendly_descr(strtolower($ifname));

								if($config['system']['reflectiontimeout']) 
									$reflectiontimeout = $config['system']['reflectiontimeout'];
								else 
									$reflectiontimeout = "2000";											

								switch($rule['protocol']) {

									case "tcp/udp":
										$protocol = "{ tcp udp }";
										$toadd_array = array();
										if(is_alias($loc_pt)) {
											$loc_pt_translated = alias_expand_value($loc_pt);
											if(stristr($loc_pt_translated, " ")) {
												/* XXX: we should deal with multiple ports */
												$loc_pt_translated_split = split(" ", $loc_pt_translated);
												foreach($loc_pt_translated_split as $lpts)
													$toadd_array[] = $lpts;
											} else {
												$toadd_array[] = $loc_pt_translated;
											}
										} else {
											$loc_pt_translated = $loc_pt;
											$toadd_array[] = $loc_pt_translated;
										}
										foreach($toadd_array as $tda){
											fwrite($inetd_fd, "{$starting_localhost_port}\tstream\ttcp/udp\tnowait/0\tnobody\t/usr/bin/nc nc -w {$reflectiontimeout} {$target} {$tda}\n");
									  		if($ifname_real)
									  			$natrules .= "rdr on \${$ifname_real} proto tcp from any to {$extaddr} port { {$xxx} } -> 127.0.0.1 port {$starting_localhost_port}\n";
									  		$starting_localhost_port++;
									  		fwrite($inetd_fd, "{$starting_localhost_port}\tstream\ttcp/udp\tnowait/0\tnobody\t/usr/bin/nc nc -u -w {$reflectiontimeout} {$target} {$tda}\n");
									  		if($ifname_real)
									  			$natrules .= "rdr on \${$ifname_real} proto udp from any to {$extaddr} port { {$xxx} } -> 127.0.0.1 port {$starting_localhost_port}\n";
									  		$xxx++;
									  		$starting_localhost_port++;
										}
										break;
									case "tcp":
									case "udp":
										$protocol = $rule['protocol'];
										$toadd_array = array();
										if(is_alias($loc_pt)) {
											$loc_pt_translated = alias_expand_value($loc_pt);
											if(stristr($loc_pt_translated, " ")) {
												/* XXX: we should deal with multiple ports */
												$loc_pt_translated_split = split(" ", $loc_pt_translated);
												foreach($loc_pt_translated_split as $lpts)
													$toadd_array[] = $lpts;
											} else {
												$toadd_array[] = $loc_pt_translated;
											}
										} else {
											$loc_pt_translated = $loc_pt;
											$toadd_array[] = $loc_pt_translated;
										}
										foreach($toadd_array as $tda){
											if($protocol == "udp") {
												$socktype = "dgram";
												$dash_u = "-u ";
											} else {
												$socktype = "stream";
												$dash_u = "";
											}
											if($config['system']['reflectiontimeout']) 
												$reflectiontimeout = $config['system']['reflectiontimeout'];
											else 
												$reflectiontimeout = "2000";
											fwrite($inetd_fd, "{$starting_localhost_port}\t{$socktype}\t{$protocol}\tnowait/0\tnobody\t/usr/bin/nc nc {$dash_u}-w {$reflectiontimeout} {$target} {$tda}\n");
											if($ifname_real)
									  			$natrules .= "rdr on \${$ifname_real} proto {$protocol} from any to {$extaddr} port { {$xxx} } -> 127.0.0.1 port {$starting_localhost_port}\n";
									  		$xxx++;
									  		$starting_localhost_port++;
										}
										break;
									default:
										break;
								}
								$loc_pt++;
								if($starting_localhost_port > 19990) {
									log_error("Not installing nat reflection rules. Maximum 1,000 reached.");
									$x = $range_end+1;
								}
							}
						}
					}

				}

			}

			$natrules .= "\n";
		}

		if(!isset($config['system']['disablenatreflection'])) {
			fclose($inetd_fd);
			$helpers = trim(exec("/bin/ps ax | /usr/bin/grep inetd | /usr/bin/grep -v grep | /usr/bin/grep 127"));
			if(!$helpers)
				mwexec("/usr/sbin/inetd -wW -R 0 -a 127.0.0.1 /var/etc/inetd.conf");
			else
				mwexec("/usr/bin/killall -HUP inetd", true);

		}
	}

	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 \$wan proto gre from any to any -> $pptpdtarget
rdr on \$wan proto tcp from any to any port 1723 -> $pptpdtarget

EOD;
		}
	}

	if (is_package_installed('squid') && file_exists('/usr/local/pkg/squid.inc')) {
		require_once('squid.inc');
		$natrules .= squid_generate_rules('nat');
	}

	if (is_package_installed('clamav') && file_exists('/usr/local/pkg/clamav.inc')) {
		require_once('clamav.inc');
		$natrules .= clamav_generate_rules('nat');
	}
	
	if (is_package_installed('frickin') && file_exists('/usr/local/pkg/frickin.inc')) {
		require_once ('frickin.inc');
		$natrules .= frickin_generate_rules('nat');
	}
	
	if (is_package_installed('siproxd') && file_exists('/usr/local/pkg/siproxd.inc')) {
		require_once('siproxd.inc');
		$natrules .= siproxd_generate_rules('nat');
	}

	$natrules .= process_carp_nat_rules();

	$natrules .= "# IMSpector rdr anchor\n";
	$natrules .= "rdr-anchor \"imspector\"\n";

	$natrules .= "# UPnPd rdr anchor\n";
	$natrules .= "rdr-anchor \"miniupnpd\"\n";

	return $natrules;
}

function run_command_return_string($cmd) {
	global $config;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "generate_user_filter_rule() being called $mt\n";
	}

	$fd = popen($cmd, "r");
	while(!feof($fd)) {
		$tmp .= fread($fd,49);
	}
	fclose($fd);
	return $tmp;
}

function generate_user_filter_rule_arr($rule, $ngcounter) {
	global $config;
	update_filter_reload_status("Creating filter rules {$rule['descr']} ...");
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "generate_user_filter_rule() being called $mt\n";
	}
	$ret = array();
	$line = generate_user_filter_rule($rule, $ngcounter);
	$ret['rule'] = $line;
	$ret['interface'] = $rule['interface'];
	if ($line[0] != '#') {
		if($rule['descr'] != "" and $line != "")
			$ret['descr'] = "label \"USER_RULE: " . str_replace('"', '', $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;
		global $table_cache;
		global $schedule_enabled;

		if($config['schedules']) {
			foreach($config['schedules']['schedule'] as $sched) {
				$schedule_enabled = true;
				break;
			}
		}

		if(isset($config['system']['developerspew'])) {
			$mt = microtime();
			echo "generate_user_filter_rule() being called $mt\n";
		}

		/* Setup cache array if not already existing */
		if (!isset($table_cache)) {
			if ($g['debug'])
				echo "Creating table cache\n";
			$table_cache = array();
		}

		update_filter_reload_status("Creating filter rules {$rule['descr']} ...");

		$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'];

		$int = "";

		$optcfg = array();
		generate_optcfg_array($optcfg);

		$curwanip = get_current_wan_address();

		/* don't include disabled rules */
		if (isset($rule['disabled'])) {
			return "# rule " . $rule['descr'] . " disabled ";
		}

		$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 "";
			$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 " # Error creating pppoe rule";
			}
			$nif = $g['n_pppoe_units'];
			if($config['pppoe']['n_pppoe_units'] <> "")
				$nif = $config['pppoe']['n_pppoe_units'];
			$ispppoe = true;
		} else if(!isset($rule['interface'])) {
			return '# Interface empty for rule: '.$rule['descr'];
		} 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)) {
				$optmatch = "";
				if(preg_match("/opt([0-999])/", $rule['source']['network'], $optmatch)) {
					$real_opt_int = convert_friendly_interface_to_real_interface_name("opt" . $optmatch[1]);
					$opt_ip = find_interface_ip($real_opt_int);
					if(!$opt_ip)
						return "# unresolvable optarray $real_opt_int - $optmatch[0] - $opt_ip";
				} else {
					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)) {
				if(preg_match("/opt([0-999])/", $rule['destination']['network'], $optmatch)) {
					$real_opt_int = convert_friendly_interface_to_real_interface_name("opt" . $optmatch[1]);
					$opt_ip = find_interface_ip($real_opt_int);
					if(!$opt_ip)
						return "# unresolvable oparray $real_opt_int - $optmatch[0] - $opt_ip";
				} else {
					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'])) {
			file_notice("Filter_Reload", "# unresolvable source aliases {$rule['descr']}");
			return "# unresolvable source aliases {$rule['descr']}";
		}
		if ($rule['destination']['address'] && !alias_expand($rule['destination']['address'])) {
			file_notice("Filter_Reload", "# unresolvable dest aliases {$rule['descr']}");
			return "# unresolvable dest aliases {$rule['descr']}";
		}

		$ifdescrs = array();
		for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
			$ifdescrs[] = "opt" . $i;

		update_filter_reload_status("Setting up pass/block rules");

		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 */
                                $aline['type'] = "block return";
			} else {
				$aline['type'] = $type;
			}

			/* ensure the direction is in */
			$aline['direction'] = " in ";

			if (isset($rule['log']))
				$aline['log'] = "log ";

			$aline['quick'] = "quick ";

			if ($ispptp) {
				$aline['interface'] = "on \$pptp ";
			} else if ($ispppoe) {
				$aline['interface'] = "on \$pppoe ";
			} 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; // XXX: billm - eh?  this is a nice little noop
					/* 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)
					$aline['interface'] = "on \$" . convert_real_interface_to_friendly_descr($rule['interface']) . " ";
			}


			/* set the gateway interface */
			$ri = filter_translate_type_to_real_interface($rule['interface']);

			update_filter_reload_status("Setting up pass/block rules {$rule['descr']}");

			/*
			 *    check to see if /tmp/{${ri}_router exists.  This file
			 *    is created by dhclient for 2nd wan interfaces, etc.
			 *    else get gateway from the interface config
			 */
			if(file_exists("{$g['tmp_path']}/{$ri}_router")) {
				$rg = file_get_contents("{$g['tmp_path']}/{$ri}_router");
				$rg = rtrim($rg);
			} elseif ($config['interfaces'][$rule['interface']]['gateway'] <> "")  {
				$rg = $config['interfaces'][$rule['interface']]['gateway'];
			}

			/* do not process reply-to for gateway'd rules */
			if(($rule['gateway'] == "") and ($ri != "") and ($rg != "") and (!isset($config['filter']['disablereplyto']))) {
				$aline['reply'] = "reply-to (" . $ri . " " . $rg . ") ";
			}

			/* if user has selected a custom gateway, lets work with it */
			if($rule['gateway'] <> "") {
				$foundlb = 0;
				$routeto = " route-to { ";
				if(is_array($config['load_balancer']['lbpool'])) {
					foreach($config['load_balancer']['lbpool'] as $lb) {
						update_filter_reload_status("Creating load balancing item...");
						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;
								}
							}

							/* check monitor IP status */
							$apinger_status = return_apinger_status();
							foreach ($lb['servers'] as $lbsvr) {
								$lbsvr_split = split("\|", $lbsvr);
								if(preg_match("/down/i", $apinger_status[$lbsvr_split[1]]['status'])) {
									echo "{$apinger_status[$lbsvr_split[1]]['status']}\n";
									continue;
								} else {
									$lbs[] = $lbsvr_split[1];
								}
							}
							if(count($lbs) == 0) {
									log_error("There are no servers found in the status file, using XML config settings!");
								foreach ($lb['servers'] as $lbsvr) {
									$lbsvr_split = split("\|", $lbsvr);
									$lbs[] = $lbsvr_split[1];
								}
							}

							/* If we want failover we only return the first (top) server from the list
							 * and work our way down from there. This way we order the failover order.
							 */
							if($lb['behaviour'] == "failover") {
								$firstsrv = $lbs[0];
								$lbs = array("$firstsrv");
							}

							/* create server/gateway gateway/monitor array */
							$l = 0;
							$lbconfig = array();
		                                        foreach ($lb['servers'] as $lbsvr) {
								$lbsvr_split=split("\|", $lbsvr);
								$lbconfig['gateway'][$l] = $lbsvr_split[0];
								$lbconfig['monitor'][$l] = $lbsvr_split[1];
								$l++;
							}
							$lbconfig_count = count($lbconfig['gateway']);

							$l = 0;
							while($l < $lbconfig_count) {
								/* iterate through $lbs and setup items accordingly */
								foreach($lbs as $server) {
									if ($server == "")
										continue;
									unset($gateway, $int);
									if ($lbconfig['monitor'][$l] == $server) {
										/* determine interface gateway */
										if(is_ipaddr($lbconfig['gateway'][$l])) {
											$int = guess_interface_from_ip($lbconfig['gateway'][$l]);
											$gateway = $lbconfig['gateway'][$l];
											log_error("SLBD pool {$lb['name']} is old style. Please recreate.");
										} else if(interface_has_gateway($lbconfig['gateway'][$l])) {
											$int = convert_friendly_interface_to_real_interface_name($lbconfig['gateway'][$l]);
											$gateway = get_interface_gateway($lbconfig['gateway'][$l]);
										}
										if(($int <> "") && ($gateway <> "")) {
											if($g['debug'])
												log_error("Setting up route with {$lbconfig['gateway'][$l]} om $int for monitor {$lbconfig['monitor'][$l]} on gateway $gateway");
											if($foundlb == 1)
												$routeto .= ", ";
											$routeto .=  "( {$int} {$gateway} ) ";
											$foundlb = 1;
										}
										/* we have a match, go forth and try the next LB item so we don't setup multiples incorrectly */
										$l++;
										continue;
									}
								}
								$l++;
							}
							/* If we want failover just use route-to else round-robin */
							if($lb['behaviour'] == "failover") {
								$routeto .= "} ";
							} else {
								$routeto .= "} round-robin ";
								if(isset($config['system']['lb_use_sticky'])) 
									$routeto .= " sticky-address ";									
							}
						}
					}
					/* Add the load balanced gateways */
					if ($foundlb == 1)
						$aline['route'] = $routeto;
				}
				/*  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);
							if (is_ipaddr($gatewayip)) {
								if($int) {
									$aline['route'] = " route-to ( {$int} {$gatewayip} ) ";
								} else {
                                                                        log_error("An error occurred while trying to determine the real interface name for the gateway $gateway");        
                                                                }
							}
						} else {
							log_error("Could not find {$g['tmp_path']}/{$int}_router.  Needed for dhcp gateway information");
							continue;
						}
					} else {
						/* user picked a real gateway ip */
						if(is_ipaddr($rule['gateway'])) {
							$gatewayip = $rule['gateway'];
							$int = guess_interface_from_ip($gatewayip);
							$aline['route'] = " route-to ( " . guess_interface_from_ip($rule['gateway']) . " {$rule['gateway']} ) ";
						}
					}
				}
			}

			if (isset($rule['protocol'])) {
				if($rule['protocol'] == "tcp/udp")
					$aline['prot'] = "proto { tcp udp } ";
				elseif($rule['protocol'] == "icmp")
					$aline['prot'] = "inet proto icmp ";
				else
					$aline['prot'] = "proto {$rule['protocol']} ";
			} else {
				if($rule['source']['port'] <> "" || $rule['destination']['port'] <> "") {
					$aline['prot'] = "proto tcp ";
				}
			}

			update_filter_reload_status("Creating rule {$rule['descr']}");

			/* 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}";
					/* check for opt$NUMip here */
					$matches = "";
					if (preg_match("/opt([0-9999])ip/", $rule['source']['network'], $matches)) {
						$optnum = $matches[1];
						$real_int = convert_friendly_interface_to_real_interface_name("opt{$optnum}");
						$src = find_interface_ip($real_int);
					}
				} else {
					switch ($rule['source']['network']) {
						case 'wanip':
							$src = $curwanip;
							break;
						case 'lanip':
							$src = $lanip;
							break;
						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, "$")) {
					if($not) {
						$src = "{";
						foreach(preg_split("/[\s]+/", alias_expand_value($rule['source']['address'])) as $item) {
							if($item != "") {
								$src .= " {$not}{$item}";
							}
						}
						/* added support for tables */
						$src .= " 0/0 }";
						$src_table = "<not" . $rule['source']['address'] . ">";
					}
					else {
						$src = "{ {$not} " . alias_expand_value($rule['source']['address']) . " } ";
						$src_table = "<" . $rule['source']['address'] . ">";
					}

					/* support for tables */
					$src_table_line = "table $src_table {$src}\n";
					$src = $src_table;
				}
				else
					$src = "{ {$not} {$expsrc} }";
			}

			if (!$src || ($src == "/")) {
				return "# at the break!";
			}

			$aline['src'] = "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]))
							$aline['srcport'] = "port {$srcporta} ";
						else
							$aline['srcport'] = "port = {$srcporta} ";
					} else if (($srcport[0] == 1) && ($srcport[1] == 65535)) {
						/* no need for a port statement here */
					} else if ($srcport[1] == 65535) {
						$aline['srcport'] = "port >= {$srcport[0]} ";
					} else if ($srcport[0] == 1) {
						$aline['srcport']= "port <= {$srcport[1]} ";
					} else {
						$srcport[0]--;
						$srcport[1]++;
						$aline['srcport'] = "port {$srcport[0]} >< {$srcport[1]} ";
					}
				}
                                /* OS signatures */
                                if (($rule['protocol'] == "tcp") && ($rule['os'] <> ""))
                                        $aline['os'] = "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'];
					/* check for opt$NUMip here */
					$matches = "";
					if (preg_match("/opt([0-9999])ip/", $rule['destination']['network'], $matches)) {
						$optnum = $matches[1];
						$real_int = convert_friendly_interface_to_real_interface_name("opt{$optnum}");
						$dst = find_interface_ip($real_int);
					}
					if (isset($rule['destination']['not'])) $dst = " !{$dst}";
				} else {
					switch ($rule['destination']['network']) {
						case 'wanip':
							$dst = $curwanip;
							break;
						case 'lanip':
							$dst = $lanip;
							break;
						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, "$")) {
                                        if($not) {
                                                $dst = "{";
                                                foreach(preg_split("/[\s]+/", alias_expand_value($rule['destination']['address'])) as $item) {
                                                        if($item != "") {
                                                                $dst .= " {$not}{$item}";
                                                        }
                                                }
                                                /* added support for tables */
						$dst .= " 0/0 }";
						$dst_table = "<not" . $rule['destination']['address'] . ">";
                                        }
                                        else {
                                                $dst = "{ {$not} " . alias_expand_value($rule['destination']['address']) . " } ";
						$dst_table = "<" . $rule['destination']['address'] . ">";
                                        }

					/* support for tables */
					$dst_table_line = "table $dst_table {$dst}\n";
					$dst = $dst_table;
                                }
				else
					$dst = "{ {$not} {$expdst} }";
			}

			if (!$dst || ($dst == "/")) {
				return "# returning at dst $dst == \"/\"";
			}

			$aline['dst'] = "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]))
							$aline['dstport'] = "port {$dstporta} ";
						else
							$aline['dstport'] = "port = {$dstporta} ";
					} else if (($dstport[0] == 1) && ($dstport[1] == 65535)) {
						/* no need for a port statement here */
					} else if ($dstport[1] == 65535) {
						$aline['dstport'] = "port >= {$dstport[0]} ";
					} else if ($dstport[0] == 1) {
						$aline['dstport'] = "port <= {$dstport[1]} ";
					}  else {
						$dstport[0]--;
						$dstport[1]++;
						$aline['dstport'] = "port {$dstport[0]} >< {$dstport[1]} ";
					}
				}
			}

			if (($rule['protocol'] == "icmp") && $rule['icmptype']) {
				$aline['icmp-type'] = "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")
						$aline['flags'] = "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.
				*/
				$noadvoptions = false;
				if(isset($rule['statetype']) && $rule['statetype'] <> "") {
					switch($rule['statetype']) {
						case "none":
							$noadvoptions = true;
                                                        $aline['flags'] = "no state ";
							break;
						case "modulate state":
						case "synproxy state":
							if($rule['protocol'] == "tcp")
								$aline['flags'] = "{$rule['statetype']} ";
							break;
						default:
							$aline['flags'] = "{$rule['statetype']} ";
					}
				} else {
					$aline['flags'] = "keep state ";
				}
				if($noadvoptions == false) 
					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'] <> "" or
					      isset($rule['statetimeout']) and $rule['statetimeout'] <> "") {
						$aline['flags'] .= "( ";
						if(isset($rule['source-track']) and $rule['source-track'] <> "")
							$aline['flags'] .= "source-track rule ";
						if(isset($rule['max-src-nodes']) and $rule['max-src-nodes'] <> "")
							$aline['flags'] .= "max-src-nodes " . $rule['max-src-nodes'] . " ";
						if(isset($rule['max-src-states']) and $rule['max-src-states'] <> "")
							$aline['flags'] .= "max-src-states " . $rule['max-src-states'] . " ";
						if(isset($rule['statetimeout']) and $rule['statetimeout'] <> "")
							$aline['flags'] .= "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'] <> "") {
							$aline['flags'] .= "max-src-conn-rate " . $rule['max-src-conn-rate'] . " ";
							$aline['flags'] .= "/" . $rule['max-src-conn-rates'] . ", overload <virusprot> flush global ";
						   }
						$aline['flags'] .= " ) ";
					}
			}
			if ($type == "reject" && $rule['protocol'] == "tcp") {
				/* special reject packet */
				$aline['flags'] .= "flags S/SA ";
			}
	}

	/* cache entries */
	if (isset($src_table))
		if (isset($table_cache[$src_table])) {
			if ($g['debug'])
				echo "{$src_table} found in cache\n";
		} else {
			if ($g['debug'])
				echo "{$src_table} NOT found in cache...adding\n";
			$table_cache[$src_table] = $src_table_line;
		}
	if (isset($dst_table))
		if (isset($table_cache[$dst_table])) {
			if ($g['debug'])
				echo "{$dst_table} found in cache\n";
		} else {
			if ($g['debug'])
				echo "{$dst_table} NOT found in cache...adding\n";
			$table_cache[$dst_table] = $dst_table_line;
		}

	/* exception(s) to a user rules can go here. */
	/* rules with a gateway or pool should create another rule for routing to local networks or vpns */
	/* we only trigger this for a rule with the destination of any and without a gateway */
	if (($aline['route'] <> "") && ($aline['type'] == "pass") && ($dst == "any") && (! interface_has_gateway($aline['interface']))) {
		/* negate VPN/PPTP/PPPoE networks for load balancer rules */
		$vpns = " to <vpns> ";
		$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] . $aline['prot'] .
			$aline['src'] . $aline['srcport'] . $aline['os'] . $vpns . $aline['dstport'].
			$aline['icmp-type'] . $aline['flags'] .
			" label \"NEGATE_ROUTE: Negate policy route for local network(s)\"\n";
	}

	/* piece together the actual user rule */
	$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] . $aline['reply'] .
		$aline['route'] . $aline['prot'] . $aline['src'] . $aline['srcport'] . $aline['os'] . $aline['dst'] .
		$aline['dstport'] . $aline['icmp-type'] . $aline['flags'];

	/* is a time based rule schedule attached? */
	if($rule['sched']) {
		if($config['schedules']) {
			foreach($config['schedules']['schedule'] as $sched) {
				if($sched['name'] == $rule['sched'])
					$schedule_xml_block = $sched;
					$schedule_enabled = true;
			}
		}
		if($schedule_xml_block)
			$status = get_time_based_rule_status($schedule_xml_block);
		if($status) {
			if($g['debug'])
				log_error("[TDR DEBUG] status true -- rule type '$type'");
			if($type == "block") {
				// active deny rules should deny
				$ipfw_rule = tdr_create_ipfw_rule($rule, "deny");
				tdr_install_rule($ipfw_rule);
			} else {
				// active allow rules should allow
				$ipfw_rule = tdr_create_ipfw_rule($rule, "allow");
				tdr_install_rule($ipfw_rule);
			}
			return "$line";
		} else {
            /*   rule is turned off, if type == pass, deny traffic until 
             *   active else allow traffic until active 
             */
            if($type == "pass") {
            	// inactive pass rules should deny
				$ipfw_rule = tdr_create_ipfw_rule($rule, "deny");
				tdr_install_rule($ipfw_rule);
			} else {
				// inactive block rules should skipto
				$ipfw_rule = tdr_create_ipfw_rule($rule, "skipto");
				tdr_install_rule($ipfw_rule);			
			}
			return "# $line";
		}
	} else {
		if($schedule_enabled) {
			// no schedule allow rules should simply allow
			$ipfw_rule = tdr_create_ipfw_rule($rule, "allow");
			tdr_install_rule($ipfw_rule);			
		}
		return $line;			
	}
}

function filter_rules_generate() {
	global $config, $g, $table_cache;

	update_filter_reload_status("Creating default rules");

	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_rules_generate() being called $mt\n";
	}

	$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'];

	$wanip = find_interface_ip(get_real_wan_interface());

	if($lansa)
		$lansa_sn_combo = "{$lansa}/{$lansn}";
	else
		$lansa_sn_combo = "192.168.1.1/32";

	/* optional interfaces */
	$optcfg = array();
	generate_optcfg_array($optcfg);

	if (is_package_installed('squid') && file_exists('/usr/local/pkg/squid.inc')) {
		require_once('squid.inc');
		$ipfrules .= squid_generate_rules('filter');
	}

	if (is_package_installed('clamav') && file_exists('/usr/local/pkg/clamav.inc')) {
		require_once('clamav.inc');
		$ipfrules .= clamav_generate_rules('filter');
	}

	if (is_package_installed('clamav') && file_exists('/usr/local/pkg/clamav.inc')) {
		require_once('clamav.inc');
		$ipfrules .= clamav_generate_rules('filter');
	}

	if (is_package_installed('frickin') && file_exists('/usr/local/pkg/frickin.inc')) {
		require_once ('frickin.inc');
		$ipfrules .= frickin_generate_rules('filter');
	}

	if (is_package_installed('siproxd') && file_exists('/usr/local/pkg/sipproxd.inc')) {
		require_once('sipproxd.inc');
		$ipfrules .= siproxd_generate_rules('filter');
	}
	
	/* if captive portal is enabled, ensure that access to this port
	 * is allowed on a locked down interface
	 */
	if (isset($config['captiveportal']['enable'])) {
		$cp_interface = $config['captiveportal']['interface'];
		$cp_interface_real = convert_friendly_interface_to_real_interface_name($cp_interface);
		$cp_interface_ip = find_interface_ip($cp_interface_real);
		if($cp_interface_ip and $cp_interface_real)
			$ipfrules .= "pass in quick on {$cp_interface_real} proto tcp from any to {$cp_interface_ip} port { 8000 8001 } keep state\n";
	}

	/* ftp-sesame */
	$ipfrules .= "anchor \"ftpsesame/*\" \n";

	# 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 = "";

	$ipfrules .= <<<EOD

# We use the mighty pf, we cannot be fooled.
block quick proto { tcp, udp } from any port = 0 to any
block quick proto { tcp, udp } from any to any port = 0

# snort2c
table <snort2c> persist
block quick from <snort2c> to any label "Block snort2c hosts"
block quick from any to <snort2c> label "Block snort2c hosts"

EOD;

        if(!isset($config['system']['ipv6allow'])) {
                $ipfrules .= "# Block all IPv6\n";
                $ipfrules .= "block in quick inet6 all\n";
                $ipfrules .= "block out quick inet6 all\n";
        }

        $ipfrules .= <<<EOD
# loopback
anchor "loopback"
pass in quick on \$loopback all label "pass loopback"
pass out quick on \$loopback all label "pass loopback"

# package manager early specific hook
anchor "packageearly"


# carp
anchor "carp"

EOD;

if($wanip)
 	$ipfrules .= <<<EOD

# permit wan interface to ping out (ping_hosts.sh)
pass quick proto icmp from {$wanip} to any keep state

EOD;

        $ipfrules .= <<<EOD

# NAT Reflection rules

EOD;

        if (isset($config['nat']['rule'])) {
                $natrules .= "# NAT Inbound Redirects\n";

                if(!isset($config['system']['disablenatreflection'])) {
                        //$fd = fopen("/var/etc/inetd.conf","w");
                        /* start redirects on port 19000 of localhost */
                        $starting_localhost_port = 18999;
                }

                foreach ($config['nat']['rule'] as $rule) {

                        update_filter_reload_status("Creating NAT rule {$rule['descr']}");

                        /* if item is an alias, expand */
                        if(alias_expand($rule['external-port']))
                                $extport[0] = alias_expand_value($rule['external-port']);
                        else
                                $extport = explode("-", $rule['external-port']);

                        /* if item is an alias, expand */
                        if(alias_expand($rule['local-port']))
                                $localport = "";
                        else
                                $localport = " port {$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($target))
                                $target = alias_expand($target);
                        if(alias_expand($extaddr))
                                $extaddr = alias_expand($extaddr);

                        if(!isset($config['system']['disablenatreflection'])) {

                                /* if list */
                                $iflist = array("lan" => "LAN");
                        for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
                                $iflist['opt' . $i] = "opt{$i}";

                        foreach ($iflist as $ifent => $ifname) {

                                /* do not process interfaces with gateways*/
                                if($config['interfaces'][$ifname]['gateway'] <> "")
                                        continue;

                                        /* do not process interfaces that will end up with gateways */
                                        if($config['interfaces'][$ifname]['ipaddr'] == "dhcp" or
                                           $config['interfaces'][$ifname]['ipaddr'] == "bigpond" or
                                           $config['interfaces'][$ifname]['ipaddr'] == "pppoe" or
                                           $config['interfaces'][$ifname]['ipaddr'] == "pptp")

                                                continue;

                                        $ifname_real = convert_friendly_interface_to_real_interface_name($ifname);

                                        if($extport[1])
                                                $range_end = ($extport[1]);
                                        else
                                                $range_end = ($extport[0]);

                                        $range_end++;

                                        if($rule['local-port'])
                                                $lrange_start = $rule['local-port'];

                                        if($range_end - $extport[0] > 500) {
                                                $range_end = $extport[0]+1;
                                                log_error("Not installing nat reflection rules for a port range > 500");
                                        } else {
                                                /* only install reflection rules for < 19991 items */
                                                if($starting_localhost_port < 19991) {
                                                        $loc_pt = $lrange_start;
                                                        for($x=$extport[0]; $x<$range_end; $x++) {

                                                                $starting_localhost_port++;
                                                                $ifname_real = convert_friendly_interface_to_friendly_descr(strtolower($ifname));

                                                                switch($rule['protocol']) {
                                                                        case "tcp/udp":
                                                                                $protocol = "{ tcp udp }";
                                                                                $ipfrules .= "pass in quick on \${$ifname_real} inet proto tcp from any to \$loopback port {$starting_localhost_port} keep state label \"NAT REFLECT: Allow traffic to localhost\"\n";
                                                                                $starting_localhost_port++;
                                                                                $ipfrules .= "pass in quick on \${$ifname_real} inet proto udp from any to \$loopback port {$starting_localhost_port} keep state label \"NAT REFLECT: Allow traffic to localhost\"\n";
                                                                                break;
                                                                        case "tcp":
                                                                        case "udp":
                                                                                $protocol = $rule['protocol'];
                                                                                $ipfrules .= "pass in quick on \${$ifname_real} inet proto {$rule['protocol']} from any to \$loopback port {$starting_localhost_port} keep state label \"NAT REFLECT: Allow traffic to localhost\"\n";
                                                                                break;
                                                                        default:
                                                                                break;
                                                                }
                                                                $loc_pt++;
                                                                if($starting_localhost_port > 19990) {
                                                                        log_error("Not installing nat reflection rules. Maximum 1,000 reached.");
                                                                        $x = $range_end+1;
                                                                }
                                                        }
                                                }
                                        }
                                }

                        }
                }
        }

	$ipfrules .= <<<EOD

# allow access to DHCP server on LAN
anchor "dhcpserverlan"
pass in quick on \$lan 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 \$lan proto udp from any port = 68 to $lanip port = 67 label "allow access to DHCP server on LAN"
pass out quick on \$lan 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 ($config[interfaces][$on][ipaddr] == "dhcp" ) {
			$friendly_on = filter_get_opt_interface_descr($on);
			$ipfrules .= <<<EOD

# Not installing DHCP server firewall rules for $friendly_on which is configured for DHCP.

EOD;
		} elseif (isset($config['dhcpd'][$on]['enable']) && (!$oc['bridge'])  ||
				($oc['bridge'] && isset($config['dhcpd'][$oc['bridge']]['enable']))) {

				$friendly_on = filter_get_opt_interface_descr($on);

			$ipfrules .= <<<EOD

# allow access to DHCP server on {$on}
anchor "dhcpserver{$friendly_on}"
pass in quick on \${$friendly_on} proto udp from any port = 68 to 255.255.255.255 port = 67 label "allow access to DHCP server"
pass in quick on \${$friendly_on} proto udp from any port = 68 to {$oc['ip']} port = 67 label "allow access to DHCP server"
pass out quick on \${$friendly_on} proto udp from {$oc['ip']} port = 67 to any port = 68 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 */
	$sa = "";
	if (isset($config['filter']['bypassstaticroutes']) && is_array($config['staticroutes']['route']) && count($config['staticroutes']['route'])) {
		$ipfrules .= <<<EOD
anchor "staticroutes"

EOD;
		foreach ($config['staticroutes']['route'] as $route) {
			unset($sa);
			$friendly_int = convert_friendly_interface_to_friendly_descr($route['interface']);
			if ($route['interface'] == "lan") {
				$sa = $lansa;
				$sn = $lansn;
				$if = $lanif;
				$friendly_int = "lan";
			} 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
pass in quick on \${$friendly_int} from {$sa}/{$sn} to {$route['network']} no state label "pass traffic between statically routed subnets"
pass in quick on \${$friendly_int} from {$route['network']} to {$sa}/{$sn} no state label "pass traffic between statically routed subnets"
pass out quick on \${$friendly_int} from {$sa}/{$sn} to {$route['network']} no state label "pass traffic between statically routed subnets"
pass out quick on \${$friendly_int} from {$route['network']} to {$sa}/{$sn} no state label "pass traffic between statically routed subnets"

EOD;
			}
		}
	}

	/* install wan spoof check rule if lan address exists */
	if($lansa) {
		if(!isset($config['interfaces']['wan']['spoofmac'])) {
			$ipfrules .= <<<EOD

# WAN spoof check
anchor "wanspoof"
block in $log quick on \$wan from $lansa/$lansn to any label "WAN spoof check"

EOD;

		}
	}

	foreach ($optcfg as $oc) {
		if (!$oc['bridge'])
			if($oc['sa'] <> "")
				if(isset($oc['enable']))
					$ipfrules .= "block in $log quick on \$wan 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 \$wan proto gre from any to any modulate state label "allow PPTP client"
pass in quick on \$wan proto gre from any to any modulate state label "allow PPTP client"
pass in quick on \$wan proto tcp from any port = 1723 to any flags S/SA modulate state label "allow PPTP client"
pass in quick on \$wan proto tcp from any to any port = 1723 flags S/SA modulate state label "allow PPTP client"

EOD;
	}

	if ($wancfg['ipaddr'] == "dhcp") {

		$ipfrules .= <<<EOD

# allow our DHCP client out to the WAN
anchor "wandhcp"
pass out quick on \$wan proto udp from any port = 68 to any port = 67 label "allow dhcp client out wan"

EOD;
	}

if($config['interfaces']['lan']['bridge'] <> "wan" and $config['interfaces']['wan']['bridge'] <> "lan")
	$ipfrules .= "block in $log quick on \$wan proto udp from any port = 67 to {$lansa_sn_combo} port = 68 label \"block dhcp client out wan\"\n";

	$ipfrules .= <<<EOD

# LAN/OPT spoof check (needs to be after DHCP because of broadcast addresses)

EOD;

	/* LAN spoof check */
	$lanbridge = false;
	foreach($config['interfaces'] as $int)
		if($int['bridge'] == "lan")
			$lanbridge = true;
	if(!$lanbridge)
		$ipfrules .= filter_rules_spoofcheck_generate('lan', $lanif, $lansa, $lansn, $log);
	$wanbridge = false;
	foreach($config['interfaces'] as $int)
		if($int['bridge'] == "wan")
			$lanbridge = true;
	if($config['interfaces']['lan']['bridge'] == "wan") 
		$wanbridge = true;

	/* OPT spoof check */
	foreach ($optcfg as $on => $oc) {
   		$isbridged = false;
		foreach ($optcfg as $on2 => $oc2) {
			if ($oc2['bridge'] && $oc2['bridge'] == $on) {
				$isbridged = true;
				break;
			}
		}
		if ($oc['ip'] && !(($oc['bridge'] || $isbridged) && isset($config['bridge']['filteringbridge'])))
			$ipfrules .= filter_rules_spoofcheck_generate($on, $oc['if'], $oc['sa'], $oc['sn'], $log);
	}

	$ipfrules .= "\nanchor \"spoofing\"\n";

	/* block private networks on WAN? */
	if (isset($config['interfaces']['wan']['blockpriv'])) {
		if($wanbridge == false) {
			$ipfrules .= <<<EOD

# block anything from private networks on WAN interface
anchor "spoofing"
antispoof for \$wan
block in $log quick on \$wan from 10.0.0.0/8 to any label "block private networks from wan block 10/8"
block in $log quick on \$wan from 127.0.0.0/8 to any label "block private networks from wan block 127/8"
block in $log quick on \$wan from 172.16.0.0/12 to any label "block private networks from wan block 172.16/12"
block in $log quick on \$wan 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>
block in quick from <virusprot> to any label "virusprot overload table"

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 \$wan from <bogons> to any label "block bogon networks from wan"

EOD;
	}

if (!isset($config['shaper']['enable']) && !is_array($config['shaper']['queue']) and $config['system']['shapertype'] <> "m0n0") {

	$ipfrules .= <<<EOD

# let out anything from the firewall host itself and decrypted IPsec traffic
pass out quick on \$lan proto icmp keep state label "let out anything from firewall host itself"
pass out quick on \$wan proto icmp keep state label "let out anything from firewall host itself"

# tcp.closed 5 is a workaround for load balancing, squid and a few other issues.
# ticket (FEN-857512) in centipede tracker.
pass out quick on $wanif all keep state ( tcp.closed 5 ) 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) {

		$friendly_on = convert_friendly_interface_to_friendly_descr($on);

		if($oc['descr']) 
			$friendly_on = $oc['descr'];		
				
		$ipfrules .= <<<EOD


# let out anything from the firewall host itself and decrypted IPsec traffic
pass out quick on {$oc['if']} proto icmp keep state ( tcp.closed 5 ) label "let out anything from firewall host itself"
pass out quick on \${$friendly_on} all keep state ( tcp.closed 5 ) label "let out anything from firewall host itself"

EOD;

	}

	if($config['interfaces']['wan']['ipaddr'] == "pppoe")
		$ipfrules .= <<<EOD
# permit wan interface to ping out (ping_hosts.sh)
pass out quick on ng0 proto icmp keep state ( tcp.closed 5 ) label "let out anything from firewall host itself"

EOD;

	if (!isset($config['system']['webgui']['noantilockout'])) {

		if($lansa and $lansn) {

			$ipfrules .= <<<EOD

# make sure the user cannot lock himself out of the webGUI or SSH
anchor "anti-lockout"
pass in quick on $lanif from any to $lanip keep state label "anti-lockout web rule"

EOD;
		}
	}

	/* PPTPd enabled? */
	if ($pptpdcfg['mode'] && ($pptpdcfg['mode'] != "off")) {

		if ($pptpdcfg['mode'] == "server")
			$pptpdtarget = get_current_wan_address();
		else
			$pptpdtarget = $pptpdcfg['redir'];

		if($pptpdtarget) {
			if(!isset($config['system']['disablevpnrules'])) {
				$ipfrules .= <<<EOD

# PPTPd rules
anchor "pptp"
pass in quick on \$wan proto gre from any to $pptpdtarget keep state label "allow gre pptpd"
pass in quick on \$wan proto tcp from any to $pptpdtarget port = 1723 modulate state label "allow pptpd {$pptpdtarget}"

EOD;
			}

		} else {
			/*    this shouldnt ever happen but instead of breaking the clients ruleset
			 *    log an error.
                         */
			log_error("ERROR!  PPTP enabled but could not resolve the \$pptpdtarget");
		}
	}

	/* 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 quick proto tcp from <sshlockout> to any port 22 label \"sshlockout\"\n\n";

	$ipfrules .= "anchor \"ftpproxy\"\n";
	$ipfrules .= "anchor \"pftpx/*\"\n";

	$ipfrules .= process_carp_rules();

	if (isset($config['filter']['rule'])) {
		/* Pre-cache all our rules so we only have to generate them once */
		$rule_arr = array();
		foreach ($config['filter']['rule'] as $rule) {
			update_filter_reload_status("Pre-caching {$rule['descr']}...");
			$line = "";
			if (!isset($rule['disabled'])) {
				if ($rule['interface'] == "pptp") {
					/* we have a pptp rule but its turned off, ignore */
					if(!$config['pptpd']['mode'] == "server")
						continue;
					$n_pptp_units = $g['n_pptp_units'];
					if($config['pptp']['n_pptp_units'] <> "")
					$nif = $config['pptp']['n_pptp_units'];
					/*
					*   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, 0);
				} else if($rule['interface'] == "pppoe") {
					if(!$config['pppoe']['mode'] == "server")
						continue;
					$n_pppoe_units = $g['n_pppoe_units'];
					if($config['pppoe']['n_pppoe_units'] <> "")
						$nif = $config['pppoe']['n_pppoe_units'];
					/*
					*   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, 0);
				} else {
					$rule_arr[] = generate_user_filter_rule_arr($rule, 0);
				}
			}
		}

		$ipfrules .= "\n# User-defined aliases follow\n";
		/* tables for aliases */
		foreach($table_cache as $table) {
			$ipfrules .= $table;
		}

		/* Shaper rules */
		if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue']) && isset($config['filter']['rule']) and $config['system']['shapertype'] <> "m0n0")  {
			
			$ipfrules .= "\n# Anchors for rules that might be matched by queues\n";

			/* This is ugly, but we generate one anchor per queue */
			foreach ($config['shaper']['queue'] as $queue) {
				update_filter_reload_status("Creating filter anchor for {$queue['name']} ...");
				/* Add anchor to rules */
				$ipfrules .= "anchor {$queue['name']} tagged {$queue['name']}\n";
				$ipfrules .= "load anchor {$queue['name']} from \"{$g['tmp_path']}/{$queue['name']}.rules\"\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);
			}
		}

		$ipfrules .= "\n# User-defined rules follow\n";
		/* Generate user rule lines */
		foreach($rule_arr as $rule) {
			$line = "";
			if (!isset($rule['disabled'])) {
				$line = $rule['rule'];
				if($line <> "") {
					/* Add default queue if we're using the shaper */
					if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue']) and $config['system']['shapertype'] <> "m0n0") {
						$defq = find_default_queue($rule['interface']);
						$ackq = $rule['ackq'];
						if (($defq != "") and ($ackq != ""))
							$line .= " queue ({$defq}, {$ackq}) ";
					}
					/* label */
					$line .= " {$rule['descr']}";
				}
			}
			$line .= "\n";
			$ipfrules .= $line;
		}
	}

	update_filter_reload_status("Creating 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);
	if($wan_ip) {
		$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");

		/* build an interface collection */
		$ifdescrs = array ("wan");
		for ($j = 1; isset ($config['interfaces']['opt' . $j]); $j++) {
			if(isset($config['interfaces']['opt' . $j]['enable']))
				$ifdescrs['opt' . $j] = filter_get_opt_interface_descr("opt" . $j);
		}

		if(is_array($config['ipsec']['tunnel']) && isset($config['ipsec']['enable'])) {
			foreach ($config['ipsec']['tunnel'] as $tunnel) {
				if(isset($tunnel['disabled']))
					continue;
				update_filter_reload_status("Creating IPSEC tunnel items {$tunnel['descr']}...");
				/* if tunnel is disabled, lets skip to next item */
				$ipsec_ips = array(get_current_wan_address($tunnel['interface']));
				/* is this a dynamic dns hostname? */
				if(!is_ipaddr($tunnel['remote-gateway'])) {
					$remote_gateway = resolve_retry($tunnel['remote-gateway']);
				} else {
					$remote_gateway = $tunnel['remote-gateway'];
				}
				/* do not add items with blank remote_gateway */
				if(!is_ipaddr($remote_gateway)) {
					$ipfrules .= "# ERROR!  Remote gateway not found on {$tunnel['remote-gateway']}\n";
					continue;
				}
				$local_subnet = return_vpn_subnet($tunnel['local-subnet']);
				foreach($ifdescrs as $iface) {
					foreach($ipsec_ips as $interface_ip) {
						if($iface == "wan")
							$interface_ip = find_interface_ip(get_real_wan_interface());
						else
							$interface_ip = find_interface_ip(convert_friendly_interface_to_real_interface_name($iface));
						if(!$interface_ip) 
							continue;
						if(!$remote_gateway) 
							continue;
						if(isset($config['system']['disablevpnrules']))
							continue;
							
  						$shorttunneldescr = substr($tunnel['descr'], 0, 26);
						$ipfrules .= "pass out quick on \${$iface} proto udp from any to {$remote_gateway} port = 500 keep state label \"IPSEC: {$shorttunneldescr} - outbound isakmp\"\n";
						$ipfrules .= "pass in quick on \${$iface} proto udp from {$remote_gateway} to any port = 500 keep state label \"IPSEC: {$shorttunneldescr} - inbound isakmp\"\n";
						if (isset($tunnel['natt'])) {
							$ipfrules .= "pass out quick on \${$iface} proto udp from any to {$remote_gateway} port = 4500 keep state label \"IPSEC: {$shorttunneldescr} - outbound nat-t\"\n";
							$ipfrules .= "pass in quick on \${$iface} proto udp from {$remote_gateway} to any port = 4500 keep state label \"IPSEC: {$shorttunneldescr} - inbound nat-t\"\n";
						}
						if ($tunnel['p2']['protocol'] == 'esp') {
							$ipfrules .= "pass out quick on \${$iface} proto esp from any to {$remote_gateway} keep state label \"IPSEC: {$shorttunneldescr} - outbound esp proto\"\n";
							$ipfrules .= "pass in quick on \${$iface} proto esp from {$remote_gateway} to any keep state label \"IPSEC: {$shorttunneldescr} - inbound esp proto\"\n";
						}
						if ($tunnel['p2']['protocol'] == 'ah') {
							$ipfrules .= "pass out quick on \${$iface} proto ah from any to {$remote_gateway} keep state label \"IPSEC: {$shorttunneldescr} - outbound ah proto\"\n";
							$ipfrules .= "pass in quick on \${$iface} proto ah from {$remote_gateway} to any keep state label \"IPSEC: {$shorttunneldescr} - inbound ah proto\"\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'])) {
			if(!isset($config['system']['disablevpnrules'])) {
				foreach($ifdescrs as $iface) {
					$ipfrules .= "pass in quick on \${$iface} proto udp from any to any port = 500 keep state label \"IPSEC: Mobile - inbound isakmp\"\n";
					$ipfrules .= "pass in quick on \${$iface} proto esp from any to any keep state label \"IPSEC: Mobile - inbound esp proto\"\n";
					$ipfrules .= "pass in quick on \${$iface} proto ah from any to any  keep state label \"IPSEC: Mobile - inbound ah proto\"\n";
					if (isset($ipseccfg['mobileclients']['natt'])) {
						$ipfrules .= "pass in quick on \${$iface} proto udp from any to any port = 4500 keep state label \"IPSEC: Mobile - inbound nat-t\"\n";
					}
				}
			}
		}
	}
	$ipfrules .= <<<EOD

pass in quick on $lanif inet proto tcp from any to \$loopback port 8021 keep state label "FTP PROXY: Allow traffic to localhost"
pass in quick on $lanif inet proto tcp from any to \$loopback port 21 keep state label "FTP PROXY: Allow traffic to localhost"
pass in quick on $wanif inet proto tcp from port 20 to ($wanif) port > 49000 flags S/SA keep state label "FTP PROXY: PASV mode data connection"

EOD;

	if(!isset($config['system']['disableftpproxy'])) {

		$ipfrules .= "# enable ftp-proxy\n";

		$optcfg = array();
		generate_optcfg_array($optcfg);
		$ftp_counter = "8022";
		foreach($optcfg as $oc) {
			if(!isset($oc['gateway']) && $oc['if'] <> "") {
				$ipfrules .= "pass in quick on " . $oc['if'] . " inet proto tcp from any to \$loopback port {$ftp_counter} keep state label \"FTP PROXY: Allow traffic to localhost\"\n";
				$ipfrules .= "pass in quick on " . $oc['if'] . " inet proto tcp from any to \$loopback port 21 keep state label \"FTP PROXY: Allow traffic to localhost\"\n";
			}
			$ftp_counter++;
		}

		if(isset($config['system']['rfc959workaround'])) {
			$ipfrules .= <<<EODEOD

# 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 flags S/SA keep state label "FTP PROXY: RFC959 violation workaround"

EODEOD;

			$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 flags S/SA keep state label \"FTP PROXY: RFC959 violation workaround\" \n";
			}
		}
	}

	$ipfrules .= <<<EOD

# IMSpector
anchor "imspector"

# uPnPd
anchor "miniupnpd"

#---------------------------------------------------------------------------
# default deny rules
#---------------------------------------------------------------------------
block in $log quick all label "Default deny rule"
block out $log quick all label "Default deny rule"

EOD;

	return $ipfrules;
}

function filter_rules_spoofcheck_generate($ifname, $if, $sa, $sn, $log) {

	global $g, $config;

	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "filter_rules_spoofcheck_generate() being called $mt\n";
	}

	$ipfrules = "antispoof for {$if}\n";

	return $ipfrules;

}

function setup_logging_interfaces() {
	global $config;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "setup_logging_interfaces() being called $mt\n";
	}
	$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;

	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "create_firewall_outgoing_rules_to_itself() being called $mt\n";
	}

	$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 the interface is pppoe, set the ng0 interface */
		update_filter_reload_status("Creating IPSEC tunnel items {$tunnel['descr']}...");
		$ip = find_interface_ip($int);
		if ($config['interfaces'][$ifname]['ipaddr'] == "pppoe")
			$int = " { " . filter_translate_type_to_real_interface($ifname) . " ng0 } ";
		if (isset($config['shaper']['enable']) && is_array($config['shaper']['queue']) and $config['system']['shapertype'] <> "m0n0") {
			$ackq = get_ack_queue($ifname);
			$defq = find_default_queue($ifname);
			/* Handle all tagged packets */
			foreach ($config['shaper']['queue'] as $queue) {
				if(!filter_is_queue_being_used_on_interface($queue['name'], $ifname, 'out'))
                	continue;
				if ($ackq == "" || $defq == "") {
					/* Shaper must not be enabled on this interface */
					$q = "";
				} else {
					$q = "queue ({$queue['name']}, {$ackq})";
				}
				$rule .="pass out quick on {$int} all keep state tagged {$queue['name']} {$q} label \"let out anything from firewall host itself\"\n";
			}
			/* Handle untagged packets */
			if ($ackq == "" || $defq == "") {
				/* Shaper must not be enabled on this interface */
				$q = "";
			} else {
				$q = "queue ({$defq}, {$ackq})";
			}
			$rule .="pass out quick on {$int} all keep state {$q} label \"let out anything from firewall host itself\"\n";
		} else {
			/* first add a rule for the real interface, then for ng0 */
			$rule .="pass out quick on {$int} all keep state label \"let out anything from firewall host itself\"\n";
		}
	}

	update_filter_reload_status("Setting up pptp items");
	if($config['pptpd']['mode'] == "server")
		$rule .="pass out quick on \$pptp all keep state label \"let out anything from firewall host itself pptp\"\n";

	update_filter_reload_status("Setting up pppoe items");
	if($config['pppoe']['mode'] == "server")
		$rule .="pass out quick on \$pppoe all keep state label \"let out anything from firewall host itself pppoe\"\n";

	update_filter_reload_status("Setting up gif tunnels");
	/* setup outgoing gif tunnels */
	$number_of_gifs = find_last_gif_device();
	$number_of_gifs++;
	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";
	}

	update_filter_reload_status("Setting up tun interfaces (openvpn)");
	/* openvpn tun interfaces.  check for 1000. */
	for($x=0; $x<1000; $x++) {
		if(does_interface_exist("tun{$x}") == true) {
			$rule .="pass out quick on tun{$x} all keep state label \"let out anything from firewall host itself openvpn\"\n";
			$friendlytunif = convert_real_interface_to_friendly_interface_name("tun{$x}");
			/* If the interface has a gateway we do not add a pass in rule. */
			/* Some people use a TUN tunnel with public IP as a Multiwan interface */
			if(interface_has_gateway("tun{$x}")) {
				$rule .= "# Not adding default pass in rule for interface $friendlytunif - tun{$x} with a gateway!\n";
			} elseif (!isset($config['system']['disablevpnrules'])) {
				$rule .="pass in quick on tun{$x} all keep state label \"let out anything from firewall host itself openvpn\"\n";
			}
		}
	}
	for($x=0; $x<1000; $x++) {
		if(does_interface_exist("tap{$x}") == true) {
			$rule .="pass out quick on tap{$x} all keep state label \"let out anything from firewall host itself openvpn\"\n";
			$friendlytapif = convert_real_interface_to_friendly_interface_name("tap{$x}");
			/* If the interface has a gateway we do not add a pass in rule. */
			/* Some people use a TAP tunnel with public IP as a Multiwan interface */
			if(interface_has_gateway("tap{$x}")) {
				$rule .= "# Not adding default pass in rule for interface $friendlytapif - tap{$x} with a gateway!\n";
			} elseif (!isset($config['system']['disablevpnrules'])) {
				$rule .="pass in quick on tap{$x} all keep state label \"let out anything from firewall host itself openvpn\"\n";
			}
		}
	}

	/* permit internal ipsec outbound traffic */
	$rule .="pass out quick on \$enc0 keep state label \"IPSEC internal host to host\"";

	return $rule;
}

function process_carp_nat_rules() {
	global $g, $config;

	update_filter_reload_status("Creating CARP NAT rules");

	$wan_interface = get_real_wan_interface();

	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "process_carp_nat_rules() being called $mt\n";
	}
	$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']);
		    if($int)
		    	$lines .= "nat on {$int} inet from {$ipnet} to any -> ({$carp_int}) \n";
		}
	    }
	return $lines;
}

function process_carp_rules() {
	global $g, $config;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "process_carp_rules() being called $mt\n";
	}
	$lines = "";
	/* return if there are no carp configured items */
	if($config['installedpackages']['carpsettings']['config'] <> "" or
	   $config['virtualip']['vip'] <> "") {
		$lines .= "pass quick proto carp\n";
		$lines .= "pass quick proto pfsync";
	}
	return $lines;
}

function remove_special_characters($string) {
		$match_array = "";
        preg_match_all("/[a-zA-Z0-9\_\-]+/",$string,$match_array);
        $string = "";
        foreach($match_array[0] as $ma) {
                if($string <> "")
                        $string .= " ";
                $string .= $ma;
        }
        return $string;
}

function carp_sync_xml($url, $password, $sections, $port = 80, $method = 'pfsense.restore_config_section') {
	global $config, $g;

	if($g['booting'])
		return;

	update_filter_reload_status("Syncing CARP data to {$url}");

	/* make a copy of config */
	$config_copy = $config;

	/* strip out nosync items */
	for ($x = 0; $x < count($config_copy['nat']['advancedoutbound']['rule']); $x++) {
		$config_copy['nat']['advancedoutbound']['rule'][$x]['descr'] = remove_special_characters($config_copy['nat']['advancedoutbound']['rule'][$x]['descr']);
		if (isset ($config_copy['nat']['advancedoutbound']['rule'][$x]['nosync'])) {
			array_splice($config_copy['nat']['advancedoutbound']['rule'],$x,1);
			$x--;
		}
	}
	for ($x = 0; $x < count($config_copy['nat']['rule']); $x++) {
		$config_copy['nat']['rule'][$x]['descr'] = remove_special_characters($config_copy['nat']['rule'][$x]['descr']);
		if (isset ($config_copy['nat']['rule'][$x]['nosync'])) {
			array_splice($config_copy['nat']['rule'],$x,1);
			$x--;
		}
	}
	for ($x = 0; $x < count($config_copy['filter']['rule']); $x++) {
		$config_copy['filter']['rule'][$x]['descr'] = remove_special_characters($config_copy['filter']['rule'][$x]['descr']);
		if (isset ($config_copy['filter']['rule'][$x]['nosync'])) {
			array_splice($config_copy['filter']['rule'],$x,1);
			$x--;
		}
	}
	for ($x = 0; $x < count($config_copy['aliases']['alias']); $x++) {
		$config_copy['aliases']['alias'][$x]['descr'] = remove_special_characters($config_copy['aliases']['alias'][$x]['descr']);
		if (isset ($config_copy['aliases']['alias'][$x]['nosync'])) {
			array_splice($config_copy['aliases']['alias'],$x,1);
			$x--;		
		}
	}
	for ($x = 0; $x < count($config_copy['dnsmasq']['hosts']); $x++) {
		$config_copy['dnsmasq']['hosts'][$x]['descr'] = remove_special_characters($config_copy['dnsmasq']['hosts'][$x]['descr']);
		if (isset ($config_copy['dnsmasq']['hosts'][$x]['nosync'])) {
			array_splice($config_copy['dnsmasq']['hosts'],$x,1);
			$x--;		
		}
	}
	for ($x = 0; $x < count($config_copy['virtualip']['vip']); $x++) {
		$config_copy['virtualip']['vip'][$x]['descr'] = remove_special_characters($config_copy['virtualip']['vip'][$x]['descr']);
		if (isset ($config_copy['virtualip']['vip'][$x]['nosync']) or $config_copy['virtualip']['vip'][$x]['mode'] == "proxyarp") {
			array_splice($config_copy['virtualip']['vip'],$x,1);
			$x--;		
		}
	}
	for ($x = 0; $x < count($config_copy['ipsec']['tunnel']); $x++) {
		$config_copy['ipsec']['tunnel'][$x]['descr'] = remove_special_characters($config_copy['ipsec']['tunnel'][$x]['descr']);
		if (isset ($config_copy['ipsec']['tunnel'][$x]['nosync'])) {
			array_splice($config_copy['ipsec']['tunnel'],$x,1);
			$x--;		
		}
	}

	foreach($sections as $section) {
		/* we can't use array_intersect_key()
		   due to the vip 'special case' */
		if($section != 'virtualip') {
			$xml[$section] = $config_copy[$section];
		} else {
			$xml[$section] = backup_vip_config_section();
		}
	}

	$params = array(
			XML_RPC_encode($password),
			XML_RPC_encode($xml)
		);

	$numberofruns = 0;
	while($numberofruns < 2) {
		log_error("Beginning XMLRPC sync to {$url}:{$port}.");
		$msg = new XML_RPC_Message($method, $params);
		$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
		$username = $config['system']['username'];
		$cli->setCredentials($username, $password);
		if($numberofruns == 1)
			$cli->setDebug(1);
		/* send our XMLRPC message and timeout after 240 seconds */
		$resp = $cli->send($msg, "240");
		if(!$resp) {
			$error = "A communications error occured while attempting XMLRPC sync with username {$username} {$url}:{$port}.";
			log_error($error);
			file_notice("sync_settings", $error, "Settings Sync", "");
		} elseif($resp->faultCode()) {
			$error = "An error code was received while attempting XMLRPC sync with username {$username} {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
			log_error($error);
			file_notice("sync_settings", $error, "Settings Sync", "");
		} else {
			log_error("XMLRPC sync successfully completed with {$url}:{$port}.");
			$numberofruns = 3;
		}
		$numberofruns++;
	}
}

function carp_sync_client() {

	global $config, $g;

	update_filter_reload_status("Building CARP sync information");

	if($g['booting'])
		return;

	if(is_array($config['installedpackages']['carpsettings']['config'])) {
	    foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
		if($carp['synchronizetoip'] != "" ) {
		    /*
		     * XXX: The way we're finding the port right now is really suboptimal -
		     *	    we can't assume that the other machine is setup identically.
		     */
		    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'];
		    if($carp['synchronizerules'] != "" and is_array($config['filter'])) {
			$sections[] = 'filter';
		    }
		    if($carp['synchronizenat'] != "" and is_array($config['nat'])) {
			$sections[] = 'nat';
		    }
		    if($carp['synchronizealiases'] != "" and is_array($config['aliases'])) {
			$sections[] = 'aliases';
		    }
		    if($carp['synchronizedhcpd'] != "" and is_array($config['dhcpd'])) {
			$sections[] = 'dhcpd';
		    }
		    if($carp['synchronizewol'] != "" and is_array($config['wol'])) {
			$sections[] = 'wol';
		    }
		    if($carp['synchronizetrafficshaper'] != "" and is_array($config['shaper'])) {
			$sections[] = 'shaper';
		    }
		    if($carp['synchronizestaticroutes'] != "" and is_array($config['staticroutes'])) {
			$sections[] = 'staticroutes';
		    }
		    if($carp['synchronizevirtualip'] != "" and is_array($config['virtualip'])) {
			$sections[] = 'virtualip';
		    }
		    if($carp['synchronizelb'] != "" and is_array($config['load_balancer'])) {
			$sections[] = 'load_balancer';
		    }
		    if($carp['synchronizeipsec'] != "" and is_array($config['ipsec'])) {
			$sections[] = 'ipsec';
		    }
		    if($carp['synchronizednsforwarder'] != "" and is_array($config['dnsmasq'])) {
		        $sections[] = 'dnsmasq';
		    }
		    if($carp['synchronizeschedules'] != "" and is_array($config['schedules'])) {
		        $sections[] = 'schedules';
		    }
		    if(count($sections) > 0) {
				update_filter_reload_status("Signaling CARP reload signal...");
			    carp_sync_xml($synchronizetoip, $carp['password'], $sections, $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')));
				$username = $config['system']['username'];
			    $cli->setCredentials($username, $carp['password']);
			    $cli->send($msg, "900");
			    /* signal a carp reload */
			    $msg = new XML_RPC_Message('pfsense.interfaces_carp_configure');
			    $cli->send($msg, "900");
				break;
		    }
		}
	    }
	}

}

function return_vpn_subnet($adr) {
	global $config;
	if(isset($config['system']['developerspew'])) {
		$mt = microtime();
		echo "return_vpn_subnet() being called $mt\n";
	}

        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']} ";

}

?>
OpenPOWER on IntegriCloud