ip-up/ip-down
scripts.
We need a new chain, here named dod
, which handles the
dialout permissions. This chain accepts and logs each packet which
is allowed to trigger a dialout, and silently denies everything
else. In the following example outgoing packets to telnet, ssh,
http, smtp, nntp, pop3, imap ports and echo requests (ping) are allowed.
ipchains -N dod ipchains -A dod -p icmp --icmp-type ping -j ACCEPT -l ipchains -A dod -p tcp --dport telnet -j ACCEPT -l ipchains -A dod -p tcp --dport ssh -j ACCEPT -l ipchains -A dod -p tcp --dport http -j ACCEPT -l ipchains -A dod -p tcp --dport smtp -j ACCEPT -l ipchains -A dod -p tcp --dport nntp -j ACCEPT -l ipchains -A dod -p tcp --dport pop3 -j ACCEPT -l ipchains -A dod -p tcp --dport imap2 -j ACCEPT -l ipchains -A dod -p tcp --dport imap3 -j ACCEPT -l #ipchains -A dod -p udp --dport domain -j ACCEPT -l ipchains -A dod -j DENY
The -l
parameter is to log which
packet triggered the dialout. (Not strictly needed with ISDN, which
can do this logging by itself, but useful in more complicated setups
involving tunnels, request-route scripts, etc.)
Note the last line which denies everything not specifically allowed
(and does not log because this would perhaps log too much - of
course logging can be useful for debugging here too).
Note also the commented rule for DNS lookups. This is a conscious policy decision. On one hand, you probably want this enabled, because most actions on the network start with a DNS lookup. On the other hand, perhaps you don't want the stray lookups done by many applications (like sendmail) to trigger a dialout. Disabling them is more safe but less convenient.
Now we set up the output chain. We check first that only the correct IP address goes out, and we use a placeholder address and dummy device when the device is inactive. If a packet is sent from the placeholder address to an ISDN device, this means the network is down and should probably be brought up: we route it to the dialout chain. Everything else going out over ISDN must be invalid.
ipchains -I output 1 -i ippp99 -s 192.168.254.254 -j ACCEPT ipchains -A output -i ippp+ -s 192.168.254.254 -j dod ipchains -A output -i ippp+ -j REJECTThe first rule is placed at a fixed location in the chain because it has to be updated dynamically.
When an ISDN device goes up, we replace the first rule in the output chain by one with the right parameters. Likewise, when it goes down again, we re-instate the placeholder. Using a placeholder instead of inserting/deleting rules has the advantage that nothing will accidently cause this rule to be duplicated or lost.
# /etc/ppp/ip-up ipchains -R output 1 -i $1 -s $4 -j ACCEPT ifconfig lo:1 down # /etc/ppp/ip-down ifconfig $1 192.168.254.254 ipchains -R output 1 -i ippp99 -s 192.168.254.254 -j ACCEPT ifconfig lo:1 $4 netmask 255.255.255.255The
lo:1
stuff is explained below.
echo "1" >/proc/sys/net/ipv4/ip_dynaddrUnfortunately, this works only for TCP sockets in SYN_SENT state, i.e. before the connection is established. (Not a problem with the kernel implementation but a limitation of TCP.)
For the second output rule to work, new sockets have to get the
placeholder address (192.168.254.254) when the connection is down.
We ensure this by assigning the device this special address in
ip-down
. It is also necessary to initialize all
potential dialout devices to this address in the system startup
somewhere (not shown here).
So far we have already the complete configuration!
Look at the output chain after ip-down
. The first
rule is the match-nothing dummy again. The second one does not match
because it always has the placeholder source address. It won't match
old sockets who carry the now invalid ISDN address. But the third
rule matches and generates a reject packet. Normal routing would
send this over the default route (think about where the source and
destination addresses are!), which is not what we want. So we
send them over the loopback device back to ourselves, which is
achieved by bringing up a loopback alias.
This is not completely what we want, because the reject packet doesn't cause the connection to be aborted. To help with this case, a kernel patch has been developed. It is at ftp://ftp.suse.com/pub/people/ak/v2.2/iff-dynamic-2.2.14-2.gz. With this patch, just give the "dynamic" flag to ifconfig for the ippp devices. Then all active TCP connections on this device will be reset immediately when the connection goes down.
dod
chain too:
insert a rule like
ipchains -I output 2 -i cipcb0 -j dod(
cipcb0
is the tunnel device) into the output chain on
initialization, change it in ip-up
to ACCEPT, and
change it back in ip-down
.
With this daemon, the default route goes to sl0
instead of an ISDN device, so the output rule matching the dial-out
candidates has to use this device: replace the second rule by
ipchains -A output -i sl0 -s 192.168.254.254 -j dodand add at the end another "guard" rule:
ipchains -A output -i sl0 -j REJECT