Skip to content

Commit e89c4a5

Browse files
committed
Added non-blocking cache responses. Added UDP controls, after testing made TCP default. Updated README & INSTALL. Other minor fixes and clean up.
1 parent 4b928f1 commit e89c4a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+9650
-992
lines changed

Debugging.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#### Check to see if PF tables on the PFUI_Firewall server are growing
3+
`pfctl -t pfui_ipv4_domains -T [add|show|delete]`
4+
5+
#### Check if the redis database on the PFUI_Firewall server is growing and holding resolved IP entries (keys)
6+
```
7+
fw1# redis-cli
8+
127.0.0.1:6379> KEYS *
9+
1) "pfui_ipv4_domains^204.79.197.212"
10+
2) "pfui_ipv4_domains^67.199.248.13"
11+
3) "pfui_ipv4_domains^1.1.1.1"
12+
```
13+
14+
#### Check the metadata (values) for an example IP entry
15+
```
16+
127.0.0.1:6379> hgetall "pfui_ipv4_domains^1.1.1.1"
17+
1) "epoch"
18+
2) "1675846179"
19+
3) "ttl"
20+
4) "3600"
21+
5) "expires"
22+
6) "1675846753"
23+
```
24+

INSTALL.md

+129-42
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,169 @@
11

2-
## Installing PFUI_Unbound and Unbound (with Python Module support)
2+
## Installing PFUI_Firewall and PFUI_Unbound (Unbound with Python Module support)
33

4-
### OpenBSD
4+
PFUI_Firewall runs as a daemon instance on OpenBSD firewall(s) running PF.
5+
PFUI_Firewall listens on the configured network port for messages from PFUI_Unbound (DNS).
6+
Received messages are written directly into the configured PF temporary tables via IOCTL calls.
7+
Messages are also saved into redis for state and expiry tracking, and into a files to be
8+
read by PF at startup, making it reload safe.
9+
10+
/etc/pf.conf must also be configured with appropriate packet filtering rules.
11+
PFUI_Firewall simply installs the IP addresses sent to it by PFUI_Unbound (DNS servers) into
12+
the configured PF tables. PF must then filter against those tables as desired.
13+
14+
#### NOTE: For both the auto and manual methods;
15+
If you want pfui_firewall to start at boot, disable unbound with `rcctl restart pfui_firewall`
16+
and edit `/etc/rc.conf.local` ensuring the following three lines exist.
17+
18+
```
19+
pfui_firewall=""
20+
pfui_firewall_user="root"
21+
pkg_scripts=redis pfui_firewall pfui_unbound
22+
```
523

24+
NB; If you want a normal Unbound (for localhost) and a PFUI Unbound (for clients) on the same firewall,
25+
either configure the default Unbound to only listen on lookback, or use different rdomain per lookup domain/daemon.
626

27+
If your pkg_scripts already has other options, just add these three to your existing line
28+
TODO: The root user is required only to talk to the /dev/pf IOCTL and to open the configured network listening port
29+
for receiving messages from all instances of PFUI_Unbound. Set group permissions for _pfui_firewall user, and run pfui_firewall as _pfui_firewall user
30+
31+
## Install
32+
```
733
export PKG_PATH=http://ftp.openbsd.org/pub/OpenBSD/%v/packages/%a/
834
pkg_add -i bash
35+
36+
# All edge OpenBSD Firewalls
937
install_pfui_firewall.sh
38+
39+
# All internal DNS Servers
1040
install_pfui_unbound.sh
41+
```
1142

43+
## Configure
44+
```
45+
OpenBSD PF Firewall(s); Configure PFUI_Firewall `/etc/pfui_firewall.yml`
46+
Unbound DNS Resolver(s); Configure PFUI_Unbound `/var/unbound/etc/pfui_unbound.yml`
47+
```
1248

49+
Warning; UDP mode (`SOCKET_PROTO: UDP`) is _not_ recommended (experimental) as Unbound's Python Module executes every DNS lookup,
50+
using a unique network socket to PFUI_Firewall for each lookup. With UDP's default timers, the socket
51+
remains (5mins) after the connection/PFUI_Firewall is updated, thus blocking subsequent connections until timeout.
52+
UDP therefore only supports ~213Qps (~64000ports / 5m / 60s).
1353

54+
Default TCP TIME_WAIT = 60; **~1,066Qps** (~64000 / 60) - per PFUI_Firewall\
55+
TCP TW=10 (`sysctl net.inet.tcp.keepidle=10`); **~6,400qps** (64000 / 10) - per PFUI_Firewall
1456

15-
#### Install Packages
16-
```
17-
export PKG_PATH=http://ftp.openbsd.org/pub/OpenBSD/%v/packages/%a/
57+
Even if your use case is <200Qps, UDP is still not recommended as logic to handle high load has not been developed due to the socket
58+
limitation above. One way to solve this could be a UDP proxy, to provide persistent sockets to PFUI_Firewall, however such userland UDP proxy which would negate
59+
any speed advantages of UDP.
1860

19-
pkg_add -i python%2
20-
pkg_add -i py-setuptools
21-
pkg_add -i py-pip
22-
ln -s /usr/local/bin/python2 /usr/local/bin/python
61+
Instead, TCP has been optimised to perform within 20% of the theoretical minimum latency of UDP.
62+
Eg, UDP messaging can unblock PFUI_Firewall in ~1000uS (1ms), and TCP can unblock PFUI_Firewall in ~1,200us (1.2ms).
63+
Because PFUI leverages TCP signalling in the kernel network stack, at high load latency does not increase much.\
64+
UDP latency increases more rapidly due to userland CPU contention as load increases.
2365

24-
pkg_add -i python%3
66+
# Manual Install
67+
The above installation scripts are better maintained than this process below.
68+
It is recommended to review the above install scripts as well.
69+
70+
------------------------------
71+
72+
### PFUI_Unbound - Dependencies
73+
```
74+
export PKG_PATH=http://ftp.openbsd.org/pub/OpenBSD/%v/packages/%a/
75+
pkg_add -i python%3.8
2576
pkg_add -i py3-setuptools
2677
pkg_add -i py3-pip
78+
ln -s `which python3` /usr/local/bin/python
79+
pkg_add -i swig git bash cmake libconfig libiconv bison gawk mawk
80+
```
2781

28-
pkg_add -i swig
29-
pkg_add -i git
82+
#### TODO: PFUI_Unbound Dependencies
83+
python3 -m pip install -r ./requirements_unbound.txt
84+
85+
### PFUI_Unbound - Download Unbound source
86+
```
87+
git clone --depth 20 https://github.com/NLnetLabs/unbound.git /tmp/unbound
88+
# --depth 20 helps with shallow clone errors
3089
```
3190

32-
#### Build Unbound with Python Module Support
91+
#### Default Unbound build options in OpenBSD port, `unbound -V` (ref only);
92+
```
93+
--enable-allsymbols
94+
--with-ssl=/usr
95+
--with-libevent=/usr
96+
--with-libexpat=/usr
97+
--without-pythonmodule
98+
--with-chroot-dir=/var/unbound
99+
--with-pidfile=
100+
--with-rootkey-file=/var/unbound/db/root.key
101+
--with-conf-file=/var/unbound/etc/unbound.conf
102+
--with-username=_unbound
103+
--disable-shared
104+
--disable-explicit-port-randomisation
105+
--without-pthreads
33106
```
34-
git clone https://github.com/NLnetLabs/unbound.git /tmp/unbound
35107

108+
### PFUI_Unbound - Build Unbound with Python Module Support enabled
109+
```
36110
cd /tmp/unbound
37111
./configure --enable-allsymbols \
38-
--with-ssl=/usr \
39-
--with-libevent=/usr \
40-
--with-libexpat=/usr \
41-
--with-pythonmodule \
42-
--with-chroot-dir=/var/unbound \
43-
--with-pidfile="" \
44-
--with-rootkey-file=/var/unbound/db/root.key \
45-
--with-conf-file=/var/unbound/etc/unbound.pfui.conf \
46-
--with-username=_unbound \
47-
--disable-shared
112+
--with-ssl=/usr \
113+
--with-libevent=/usr \
114+
--with-libexpat=/usr \
115+
--with-pythonmodule \
116+
--with-chroot-dir=/var/unbound \
117+
--with-pidfile="" \
118+
--with-rootkey-file=/var/unbound/db/root.key \
119+
--with-conf-file=/var/unbound/etc/pfui_unbound.conf \
120+
--with-username=_unbound \
121+
--disable-shared \
122+
--disable-explicit-port-randomisation \
123+
--without-pthreads
48124
49125
make && make install
126+
```
50127

128+
#### Copy PFUI_Firewall files
129+
```
130+
cp -f "${DIR}/pfui_firewall.py" /usr/local/sbin/pfui_firewall.py
131+
chmod 755 /usr/local/sbin/pfui_firewall.py
132+
133+
cp -f "${DIR}/pfui_firewall.yml" /etc/pfui_firewall.yml
134+
chmod 644 /etc/pfui_firewall.yml
51135
136+
cp -f "${DIR}/rc.d/pfui_firewall" /etc/rc.d/pfui_firewall
137+
chmod 555 /etc/rc.d/pfui_firewall
138+
139+
cp -f "${DIR}/examples/pf.conf" /etc/pf-pfui-example.conf
52140
```
53141

54-
#### TODO: Copy in all PFUI_Unbound files (/var/unbound/etc/)
55-
chmod 755 ./pfui_firewall.py
142+
### Install Python Libraries and Redis
143+
```
144+
python3 -m pip install redis pyyaml service
145+
pkg_add -i redis
146+
rcctl enable redis
147+
rcctl start redis
148+
```
149+
```
56150
chmod 755 ./update_root_servers.sh
57151
chmod 755 ./update_filtered_domains.sh
58152
59153
chmod 555 /etc/rc.d/pfui_unbound
60154
61155
chmod 755 ./update_root_servers.sh
62156
chmod 755 ./update_filtered_domains.sh
63-
64-
#### TODO: PFUI_Unbound Dependencies
65-
python3 -m pip install -r ./requirements.txt
157+
```
66158

67159
#### TODO: Section for Unbound service
68160

69-
#### TODO: Section for Unbound config
161+
#### TODO: Unbound config
162+
A good tool for automatically downloading the latest bad-reputation domains into DNS Blocklists can be found here;
163+
https://www.geoghegan.ca/unbound-adblock.html
70164

71165
#### TODO: Section for Unbound data sources
72-
./update_root_servers.sh
73-
./update_filtered_domains.sh
166+
./update_root_hints.sh
74167

75168
```
76169
rcctl enable pfui_unbound
@@ -91,32 +184,26 @@ rcctl start pfui_unbound
91184

92185
### OpenBSD
93186

94-
#### Install Packages
187+
#### PFUI_Firewall - Dependencies
95188
```
96-
pkg_add -i python%2
97-
pkg_add -i py-setuptools
98-
pkg_add -i py-pip
99-
100-
ln -s /usr/local/bin/python2 /usr/local/bin/python
101-
102189
pkg_add -i python%3
103190
pkg_add -i py3-setuptools
104191
pkg_add -i py3-pip
105-
192+
ln -s `which python3` /usr/local/bin/python
106193
pkg_add -i redis
107194
rcctl enable redis
108195
rcctl restart redis
109196
```
110197

111-
#### Copy Files (/etc/ & /usr/local/sbin/)
198+
#### PFUI_Firewall - Copy Files (/etc/ & /usr/local/sbin/)
112199
```
113200
cp ./pfui_firewall.py /usr/local/sbin/pfui_firewall.py
114201
chmod 755 /usr/local/sbin/pfui_firewall.py
115202
cp ./pfui_firewall.yml /etc/pfui_firewall.yml
116203
chmod 644 /etc/pfui_firewall.yml
117204
```
118205

119-
#### Enable PFUI_Firewall service
206+
#### PFUI_Firewall - Enable Service
120207
```
121208
rcctl enable pfui_firewall
122209
rcctl start pfui_firewall

0 commit comments

Comments
 (0)