August 6, 2004

Somewhere

Everything's got to start somewhere. So I'm starting a BLOG.

Posted by benjamin at 10:40 PM

Binpatch

Playing with binpatch. It's a nice idea, but the packages store the compilation date & time in some of the files (like the kernel), so there's no way to get reliable MD5's between the master site & the patches I build. Oh well, it's better than nothing.

One bugger with the system as it comes from http://www.openbsd.org.mx/en/projects/binpatch.html is that the patches don't build without fixing the makefile. The bsd.binpatch.mk that comes with (for 3.5) doesn't unpack sys.tar.gz in the correct location. Patch below.

kagome# rcsdiff -u bsd.binpatch.mk
===================================================================
RCS file: bsd.binpatch.mk,v
retrieving revision 1.1
diff -u -r1.1 bsd.binpatch.mk
--- /tmp/T0r32753 Fri Aug 6 22:45:00 2004
+++ bsd.binpatch.mk Fri Aug 6 22:34:30 2004
@@ -191,7 +191,7 @@
@echo "===> Extracting sources"
@mkdir -p ${WRKSRC}/sys && \
tar xzpf ${DISTDIR}/src.tar.gz -C ${WRKSRC}&& \
- tar xzpf ${DISTDIR}/sys.tar.gz -C ${WRKSRC}/sys && \
+ tar xzpf ${DISTDIR}/sys.tar.gz -C ${WRKSRC}&& \
touch -f ${.TARGET}

# Extracts the OpenBSD installation files


+-------------------------
|
| The binary patch(es) has/have been created in
| /usr/binpatch/packages
|
| To install a binpatch just unpack it under /, e.g.:
|
| # cd /usr/binpatch/packages
| # tar xzpf binpatch-3.5-i386-001.tgz -C /
|
+-------------------------

Posted by benjamin at 10:46 PM

PF Tricks

Running a firewall as a bridge can have great advantages, but proxying is difficult. Here's the info I wish I had when I started trying to figure this out.

Network Architecture

               ISP1                 ISP2               ISP3
                 |                    |                  |
                 +---------------ISP-Router--------------+
                                      |
                                      |
                                    Border
                                    Router
                                      |
                                     sk0
                              Ext-Sorting-Router
                              sk1    sk2     sk3
                               |      |       |
               +---------------+      |       +------------+
               |                      |                    |
            Traffic                   |                 Traffic
            Shaper                    |                  Shaper
               |                      | B                  |
               |                      | y                  | D
               |                      | p                  | e
 +----------------+                   | a                  | f
 | bridge     em2 |                   | s                  | a
 | firewall   ] [ |                   | s                  | u
 |  em0       em1 |                   |                    | l
 +----------------+                   |                    | t
    |          |                      |                    |
    |          |                      |                    |
    |          |                      |                    |
    |          +---------------+      |        +-----------+
    |                          |      |        |
    |                         xl1    xl2      xl3
    |                         Int-Sorting-Router
    |                                xl0
    |                                 |
    |                                 |
    |                                gx0
    |                          AggregationRouter
    |                            gx4 gx1 gx3  gx2
    |                             /   |   |    \
    |                            /    |   |     \
    |                           /     |   |      +--------Research Network
    |                          /      |   |                 (10.0.0.0/10)
    |                         /       |   |
    |                        /        |   +------Main Network
    |                       /         |           (10.64.0.0/10)
    |                      /          |
    |                     /           +-------Limited Network
    +---Management Net---+                     (10.128.0.0/10)
        (192.168.0.0/16)

Infrastructure Network (172.16.0.0/12) - primarily /30's for router links

The network this firewall was designed for looked like the diagram above. The
firewall is built using OpenBSD and pf running as a bridge, with transparent
proxying of web traffic. The Internal Sorting Router is used to direct traffic
from the Research Network across the Bypass link; traffic from the Main &
Management Networks thru the "Default" link (and thus a traffic shaper); and
traffic from the "Limited Network" thru the firewall & traffic shaper. The
ISR uses SOURCE ROUTING to perform these tasks.

The External Sorting Router routes traffic back across the proper link based on
the destination. One thing to note in this setup is that traffic between the
4 networks does not reach the ISR, because the Aggregation Router routes it.

Note: the interface names are arbitrary, but valid, and chosen to be unique
across all devices in the diagram. Also, the device numbers are chosen to give
an indication of the normal path for a packet, and device number 0 is used to
indicate the default route.

On the firewall, em1 & em2 form a bridge, and em0 is the management/proxy
interface. All proxied web requests reach the outside world via em0.

By design, OpenBSD's bridge implementation does not give direct access to the
traffic for user-space processes. Thus, to pluck packets off of the bridge, we
have to jump into some of the less well documented features of pf.

With pf disabled, and the bridge enabled, a host in the limited network will
send traffic along the path gx1:gx0:xl0:xl1:em1:em2:sk1:sk0, and traffic will
return along the path sk0:sk1:em2:em1:xl1:xl0:gx0:gx1.

Transparent Proxy

To enable transparent proxying, we build the transparent flavor of squid, and
configure it as described in the widely available online documentaion. In our
case, we set is up listening on 127.0.0.1:3128 . I'll skip the details of
our squid config, and let you use your imagination to determine what you want
to do.

Interface Configuration

I'll go into more depth about why the interfaces need to be configured this
way, but let's get it working now, and explain why & how later.

On the Firewall

# ifconfig em0 192.168.1.1 netmask 0xffff0000 up
# ifconfig em1 172.16.32.1 netmask 0xfffffffc up

On ISR

# ifconfig xl1 172.16.18.2 netmask 0xfffffffc up
# ifconfig xl1 aliax 172.16.32.2 netmask 0xfffffffc up

On ESR

# ifconfig sk1 172.16.18.1 netmask 0xfffffffc up

We now have a point-to-point between ISR and ESR, and a separate
point-to-point between ISR and Firewall(em1).

Static Routing on Firewall

On the firewall, we need the IP stack to know how to send packets to the
hosts we're proxying for.

# route add 10.128.0.0/10 127.0.0.1

Configuring pf

I'll show the critical parts of the pf configuration here, and explain them more
after.

    table <limited> const { 10.128.0.0/10 }
    # i
    rdr on em1 proto tcp from <limited> to any port 80 -> 127.0.0.1 port 3128
    # ii
    pass  in quick log on em1 route-to (lo0 127.0.0.1) proto tcp from <limited> \
                to any port 80 keep state
    # iii
    pass out quick log on lo0 route-to (em1 172.16.32.2) proto tcp from 127.0.0.1 \
                port 3128 to <limited> keep state

Examination

When a host in (10.128.9.9) sends a tcp packet to 129.128.5.191:80
(www.openbsd.org), here's what happens:

Notation: Original packet O: sIP(sMAC):sport -> dIP(dMAC):dport
Modified packet M: sIP(sMAC):sport -> dIP(dMAC):dport

1. The rdr rule(i) rewrites the IP and TCP headers, changing the destination IP and port
of the packet. The packet still has a destination MAC address of sk1 because that's what
ISR determined to be the MAC address of the next-hop router.
O: 10.128.9.9(xl1):39999 -> 129.128.5.191(sk1):80
M: 10.128.9.9(xl1):39999 -> 127.0.0.1(sk1):3128

2. The route-to rule (ii) rewrites the destination MAC address by looking up the MAC of
127.0.0.1, and adjusting the packet. The packet is then sent out on lo0.
O: 10.128.9.9(xl1):39999 -> 127.0.0.1(sk1):3128
M: 10.128.9.9(xl1):39999 -> 127.0.0.1(lo0):3128

3. The proxy process listening on 127.0.0.1:3128 get's the packet (SYN), and sends a reply
(SYN/ACK) to the requesting host (10.128.9.9). The IP stack knows that these packets are to
be sent out on lo0 because of the static route we created earlier.
O: 127.0.0.1(lo0):3128 -> 10.128.9.9(lo0):39999

4. Before sending the packet out, the kernel sends it to pf for processing. Here rule (iii)
is triggered, and it rewrites the destination MAC with the MAC address of 172.16.32.2(xl1).
The packet is then sent to em1 to be output.
O: 127.0.0.1(lo0):3128 -> 10.128.9.9(lo0):39999
M: 127.0.0.1(em1):3128 -> 10.128.9.9(xl1):39999

5. The packet from 127.0.0.1:3128 -> 10.128.9.9:xxx gets processed by the rdr rule (i) and
the source address and TCP port are rewritten to the ones the client expects
(129.128.5.191:80), and the packet is sent out the interface em1.
O: 127.0.0.1(em1):3128 -> 10.128.9.9(xl1):39999
M: 129.128.5.191(em1):80 -> 10.128.9.9(xl1):39999

6. The internal routing from ISR to the requesting host processes the packet, and the
requesting host gets a response it was expecting. The MAC addresses will change along
the way as the packet traverses the routers in the path.

The remainder of the TCP conversation will follow this loop for packets being sent to/from
the proxy interface.

Can't I Skip ... ?

For all of this to work, the IP stack & kernel need enough information to rewrite all parts
of the packet headers. Each of the parts above plays a part in this.

Without the static route to the network, the stack will follow the default route
and send the packet out em0 with a source address of 127.0.0.1. This will not work.

Without the point-to-point link from em1 to xl1, the stack does not know which MAC address
to use when it puts the packet back on the bridge. In my testing, I saw packets on em1 with
the source MAC and destination MAC both having the MAC address of em1. This will not
work.
I found this to be the most odd, considering 'ifconfig bridge0' showed that the bridge knew
xl1's MAC address, but the bridge is will insulated from the rest of the stack, and will not
share this information. This is far better than having the bridge build in assumptions which
will be wrong for many cases.

Hope that helps.

Posted by benjamin at 10:56 PM

August 11, 2004

OpenSSL Certs

I pulled out some parts of others' scripts to make self-signed SSL Certs for apache in OpenBSD.

#!/bin/sh
svrname="test.server"
destdir="/var/www/conf/$svrname"
prv="private"
dname="server"

sk="$destdir/$prv/$dname.key"
csr="$destdir/$prv/$dname.csr"
crt="$destdir/$dname.crt"


echo $sk
echo $csr
echo $crt

exit

/usr/sbin/openssl genrsa -out $sk 1024
/usr/sbin/openssl req -new -key $sk -out $csr
/usr/sbin/openssl x509 -req -days 365 -in $csr \
        -signkey $sk -out $crt
Posted by benjamin at 12:07 PM