The Firewall
Makronix turns on NetBSD's packet filter,
npf7,
and installs a rule set that lets nothing in that the machine did not ask
for. Outgoing connections work, their replies come back, DHCP and IPv6
set-up work, ping works, SSH works and the local network names work.
Everything else is blocked.
The default rule set
/etc/npf.conf on a freshly installed machine
is the following. It is the rule set that the installer writes and that
/usr/src/docs/reference/npf-default.conf records
for validation:
alg "icmp"
group default {
pass final on lo0 all
# out: everything, with the replies
pass stateful out final all
# DHCP replies (a broadcast, so no state) and IPv6 set-up
pass in final proto udp from any port 67 to any port 68
pass final family inet6 proto ipv6-icmp all
# ping
pass stateful in final family inet4 proto icmp all
# remote login
pass stateful in final proto tcp to any port ssh
# mDNS and DNS-SD (mdnsd): the multicast announcements, port 5353
pass in final proto udp to any port 5353
block in final all
}
Read it from the bottom up: everything that arrives is blocked
unless an earlier rule lets it in. The rules above it are the
exceptions. stateful means that the reply to a
connection the machine started is allowed without a rule of its
own.
Opening one port
A service that should be reachable from the network needs a rule
above the final block. For a web server on port 80, add this line
before block in final all:
pass stateful in final proto tcp to any port 80
Then check the file and reload the running rule set. A rule set
with a syntax error must not be loaded; npfctl validate
reads the file without touching the running one:
# npfctl validate /etc/npf.conf
# /etc/rc.d/npf reload
# npfctl show
# npfctl stats
npfctl show prints the rules that are active,
after the variables and groups have been expanded. If the line you
added is not there, the reload did not happen; the console and
/var/log/messages say why.
The firewall at boot
The service is enabled in /etc/rc.conf:
npf=YES
At boot, npf loads /etc/npf.conf
before the network is up. A machine that boots without a rule set is a
machine that is open for the time it takes to notice. After a change,
reload rather than restart; the existing connections survive a
reload.
NAT and a router
A machine that routes between two networks needs NAT and forwarding.
The rules are in npf.conf5;
the shape is a map line for the outgoing interface
and pass rules for the two directions. Forwarding
itself is a sysctl8
setting:
# sysctl -w net.inet.ip.forwarding=1
# echo 'net.inet.ip.forwarding=1' >> /etc/sysctl.conf
A router is a machine with two interfaces and a rule set that
describes both directions. Do not enable forwarding on a desktop that
has one interface; it has nothing to forward between.