Home > Articles > Operating Systems, Server

  • Print
  • + Share This
This chapter is from the book

6.4 FreeS/WAN—An IPsec Implementation for Linux

FreeS/WAN is the project name of Linux's IPsec implementation. The Free part is obvious; S/WAN is the Secure Wide Area Network project, which is working to ensure that the various IPsec implementations really do interoperate when the time comes. (The VPN Consortium, http://www.vpnc.org/, is also involved in this eNort.) One important aspect of the FreeS/WAN project is the emphasis on using code and algorithms free from patent problems, and, insofar as possible, free from export (or at least use) restrictions in countries that view cryptography as a weapon. In the United States, where I am writing this book, things seems to have taken a tiny step forward and two giant leaps backward with the new regulations from the Commerce Department's Bureau of Export Administration (or BXA) and the DMCA (Digital Millennium Copyright Act). As inconceivable as it is, it is illegal for any American to contribute code (or even a patch) to the FreeS/WAN project. The FreeS/WAN documentation makes a point of noting that the U.S. government has very little sense of humor about this issue and does not consider good intentions to be su-cient excuse. Beware. Instead of going oN on a rant about how very misguided I believe some of these laws to be, you should research the matter and form your own opinion. Therefore, those of you who live in free countries are spared, and all of us can focus on using FreeS/WAN, which has been graciously provided by contributors in countries without these sorts of restrictions. FreeS/WAN consists of several components that must mesh to make everything tick. These are listed here so that you can start building a mental map of how the pieces fit together.

  • The IPsec protocol itself, the part that encrypts packets before sending them and decrypts them on the other side, is implemented by KLIPS, or Kernel Level IP Security. This is a set of kernel patches distributed with FreeS/WAN.

  • A public-key exchange service, somewhat similar to public GPG and PGP key servers. As with any public-key algorithm, public keys can be made openly available and freely distributed, but the burden is still on you to verify the authenticity of the key before using it. This portion of IPsec is handled by the IKE (Internet Key Exchange), which has been implemented within FreeS/WAN by a daemon named pluto. (I'm not sure, but would guess that this is a humorous reference to Kerberos, a.k.a. Cerebus.) If you're going to support large numbers of users, say in the case where you're using IPsec as your VPN protocol for all of your field users, the IKE component is often referred to as PKI (Public Key Infrastructure), or X.509.

  • A set of user-space tools to configure and manage the kernel module. This is /usr/sbin/ ipsec itself, which thankfully can be used as the the single interface to the tools used to generate keys, configure the links, start IPsec, start pluto, gather statistics, etc.

There is a lot to FreeS/WAN and IPsec, enough to write a book on it easily, especially when you get into the details of deployment using different types of keys and diNerent implementations. I'm certainly not the person to write that book, and I don't want to confuse the issue by presenting information that cannot be adequately covered. We're going to set up FreeS/WAN as a tunnel between two Linux systems so that we can enjoy the routing benefits of the tunnel and the safety of encryption for the traJc flowing over it. Therefore, we are necessarily only scratching the surface.

6.4.1 FreeS/WAN Distribution—Obtaining and Building

The primary source for the upstream package is ftp://ftp.xs4all.nl/pub/crypto/freeswan/. Since the cryptography export laws of various countries are continually in flux, there aren't many mirrors. Note this also means that the distribution CD that comes with your favorite Linux CD may not have the distribution's version of the package either, so you may have to search a little, since the package will need to be pulled from a network site (possibly not the primary site for the distribution). You may also have problems with support (at least from distributions based in the United States), since such activity is all regulated. This is a shame, because building and installing FreeS/WAN from the upstream source takes time and resources that would be better spent not duplicating e.ort.I was able to get the Debian package installed and running in a matter of minutes (most of those minutes being the part where I had to patch my kernel, since the upstream kernel does not contain the KLIPS code, once again due to legal restrictions).If you're using your distribution's packages, follow the included instructions.W e'll walk through the steps to build FreeS/WAN using the upstream tarball.

  1. H ave a copy of the recent kernel source tree in /usr/src/linux/.I f you're pulling a copy of the kernel source just for this, you should go ahead and con.gure your kernel once, using the con.guration method of your choice.

  2. L ocate and install copies of libgmp3-dev and libgmp3 (these are the Debian package names), as these are required for the build and run time, respectively.G MP is the GNU Multi-Precision math library and is required by FreeS/WAN for key calculations. If you're really working from scratch, you can pull the source for the libraries from http://www.swox.com/gmp/.

  3. Extract the tarball and cd freeswan-1.91.I nvoke make menugo, which will patch the kernel source and give you another chance to review the con.guration, or menu ogo if you're quite sure that the kernel con.guration is how you want it.B y default (and if you use the latter), IPsec will be compiled directly into the kernel, so use menugo if you'd prefer it to be a module.The options you're concerned with are:

    Networking options --->
    <*> IP Security Protocol (FreeS/WAN IPSEC)
    --- IPSec options (FreeS/WAN)
    [*] IPSEC: IP-in-IP encapsulation (tunnel mode)
    [*] IPSEC: Authentication Header
    [*] HMAC-MD5 authentication algorithm
    [*] HMAC-SHA1 authentication algorithm
    [*] IPSEC: Encapsulating Security Payload
    [*] 3DES encryption algorithm
    [*] IPSEC: IP Compression
    [*] IPSEC Debugging Option
  4. Ge t a cup of co.ee, catch up on email, or take a nap (which depends upon how long it takes your box to build a 2.4 kernel). The kernel build should complete without error, and it will also install the user-space tools into the default location of /usr/local/lib/ipsec/. You'll still need to cd /usr/src/linux ; make bzlilo or similar to get the new kernel into a place where you can boot from it.A lternatively, try make kinstall from the freeswan-1.91 directory. After these steps, you should have the .les /etc/ipsec.conf and /etc/ipsec.secrets (which will already contain a generated key pair for this system), the tools as noted above, and a system startup script called ipsec in /etc/init.d/ or wherever your system startup directory is.B ut don't reboot yet. If you do, ipsec will start at boot time.Y ou have a halfway working con.guration at this point, a con.guration that will add routes to the ipsec0 virtual device and could possibly make for a long boot process waiting for DNS lookups (or worse, mounts) to time out.Pl us, you'll probably need console access to get back on the machine.

6.4.2 Configuring FreeS/WAN and ipsec

Although there is potentially quite a lot to configuring FreeS/WAN, to achieve a basic two-system topology you need only generate key pairs on the routers at each end of the tunnel, place these into /etc/ipsec.secrets, exchange the public keys from these key pairs, edit /etc/ipsec.conf on one system and copy it to the other (nice touch, IMO), and then fire it up. The first step is done for you automatically when using the upstream package. If you're using a distribution package, check to see if keys were generated (they aren't in the current Debian package). If they weren't, or you'd like to generate a new keypair at any time, you can use ipsec rsasigkey 2048 >./myipseckey to generate your key. You'll need to add the contents of myipseckey to the appropriate place in /etc/ipsec.secrets for that system only. You can use a number other than 2048, the number of bits in the key, but I couldn't get FreeS/WAN to use a key shorter than 512 bits. In general, you need to protect the contents of /etc/ipsec.secrets just as you would your own GPG private key. The file contains the private key that identifies your system as you, so if someone has it, they can spoof that system's identity. (Therefore, destroy the myipseckey file, watch file permissions on the file in /etc/, think about who has access to backup media, and so on.)

To view the usage statement for ipsec rsasigkey, invoke it with the --help argument, which is valid for any ipsec command form, much like ip; ipsec alone will show you the allowed commands. If you're looking for the command itself, the upstream package drops this executable into /usr/local/sbin/, so you may want to add this to your PATH. Before we dig into the configuration file, let's take a look at our simple topology in Figure 6.3. The default configuration file uses the labels of left and right to diNerentiate between the two systems in a peer-to-peer topology like the one we're going to build here.

Figure 6.3Figure 6.3 FreeS/WAN Basic Configuration Topology

The figure labels interfaces the way that /etc/ipsec.conf does, so if your topology is similar, you can simply plug in the values and go. Note that you need not have two separate gateways between your two systems—there may be one router, or the left and right systems may be part of the same subnet. The same is true for the leftsubnet and the rightsubnet; if they don't exist, just comment them out in the configuration file. Both the subnet and nexthop values are there so that FreeS/WAN can configure routes over the ipsecN devices accordingly. In the following example configuration, we're configuring a single workstation, vivaldi, to set up a secure connection to drone1, and to route over that secure tunnel.

# basic configuration on drone1
config setup

conn %default
             keyingtries=0              # be persistent
             authby=rsasig              # use rsakeys

conn vivaldi-drone1

The first section contains general parameters global to the entire setup and probably will not need to modified. FreeS/WAN logs a good bit of information via syslog, even when the debug modes are set to none, so you probably won't need to set these to their full value, which is all. The uniqueids parameter is used to indicate that if a second connection comes in from the same ID, the first connection should be torn down. Leave it like that to avoid confusion (otherwise you may need to restart ipsec on both ends of a connection after a parameter change). The second section contains parameters to be applied to all connection (conn) sections in the file. In this file we have only one connection defined, but you can list as many as you'd like. Note that you'll need to assign a unique id to every node that will be running IPsec, so the use of a DNS name is a natural choice. There is another reason for this as well. DNS can be configured to serve those rsasigkeys, so that you aren't constantly trying to cut and paste them between systems. (Just take the necessary precautions to ensure that someone doesn't spoof your DNS.) After placing the same file on vivaldi, we're ready to test our configuration.

6.4.3 FreeS/WAN in Use

To start FreeS/WAN, use either /etc/init.d/ipsec start or ipsec --setup start, both of which actually run the same script. You'll need to do this on both ends and be patient while pluto figures things out. Most likely you will receive some warning messages during the startup about needing to disable reverse path (or route) filtering on all of the interfaces involved with your configuration. This is necessary basically because, in a way, you're communicating over two interfaces simultaneously, the ipsecN interface along with your physical Ethernet device. You may send something from vivaldi to drone1 while restarting FreeS/WAN on vivaldi, and therefore before the IPsec link is up, but once vivaldi has that, it can respond via the tunnel. By disabling the reverse path filtering, you're saying that this is OK and allowing asymmetrical routing. (In general, this is to be avoided, but there are situations where it is required, this being one of them.) Execute echo "0" >/proc/sys/net/ipv4/conf/intf/rp filter for each of your physical and virtual ipsec devices used in the ipsec configuration, as the warning messages will instruct you (although it's not abundantly clear to me why the startup script can take care of the ipsec devices for you). A successful startup will look something like:

tmancill@drone1:~# sudo /etc/init.d/ipsec start
ipsec_setup: Starting FreeS/WAN IPsec U1.9/K1.91...
ipsec_setup: 102 "vivaldi-drone1" #1: STATE_MAIN_I1: initiate
ipsec_setup: 104 "vivaldi-drone1" #1: STATE_MAIN_I2: from STATE_MAIN_I1; sent MI2, expecting MR2
ipsec_setup: 106 "vivaldi-drone1" #1: STATE_MAIN_I3: from STATE_MAIN_I2; sent MI3, expecting MR3
ipsec_setup: 004 "vivaldi-drone1" #1: STATE_MAIN_I4: ISAKMP SA established
ipsec_setup: 110 "vivaldi-drone1" #6: STATE_QUICK_I1: initiate
ipsec_setup: 004 "vivaldi-drone1" #6: STATE_QUICK_I2: sent QI2, IPsec SA established 

If you've set up a sniNer somewhere along the path between the two systems, you can watch the key negotiations occur on port 500 (which will obviously need to be allowed to pass through intermediary firewalls and NATs). If you are behind something that may hinder this communication, specify this in the configuration file under the connection-specific information with rightfirewall=yes for the right side, left for the left. I include some sniNer output that details the key exchange as well as some encapsulating security paylog (ESP) packets, which is now as much as any eavesdropper will determine about the communication between these two systems.

tmancill@drone3:/tmp$ sudo tcpdump -n host vivaldi and host drone1
tcpdump: listening on eth0
vivaldi.500 > drone1.500: isakmp: phase 1 I ident: [|sa] (DF)
drone1.500 > vivaldi.500: isakmp: phase 1 R ident: [|sa] (DF)
vivaldi.500 > drone1.500: isakmp: phase 1 I ident: [|ke] (DF)
drone1.500 > vivaldi.500: isakmp: phase 1 R ident: [|ke] (DF)
vivaldi.500 > drone1.500: isakmp: phase 1 I ident[E]: [|id] (DF)
drone1.500 > vivaldi.500: isakmp: phase 1 R ident[E]: [|id] (DF)
vivaldi.500 > drone1.500: isakmp: phase 2/others I oakley-quick[E]: [|hash] (DF)
drone1.500 > vivaldi.500: isakmp: phase 2/others R oakley-quick[E]: [|hash] (DF)
vivaldi.500 > drone1.500: isakmp: phase 2/others I oakley-quick[E]: [|hash] (DF)
drone1 > vivaldi: ESP(spi=0x2c525b1b,seq=0x1)
vivaldi > drone1: ESP(spi=0xf1a4e13d,seq=0x8) 

If you are so inclined, now would be the perfect time to try out that -E option for tcpdump, where you supply the secret key to decrypt the contents of the ESP packets. Note that your distribution's copy of tcpdump might not be able to handle the crypto code (due to the same export regulations mentioned earlier), so you may have to dig around to find a binary, or compile your own.

Once you have your link up and running, you can use the ipsec0 device like most any network device, adding routes to it and starting packet sniNers to monitor it (which will give you access to the unencrypted packets on the wire). You can use several of the FreeS/WAN tools to get your bearing. ipsec eroute will display routes on your system that will result in use of the encrypted communications channels. By the way, if you set up multiple connections and mistakenly configure the leftsubnet or rightsubnet so that there are multiple routes to the same subnet, you may see an error message about an eroute being unavailable (as it's masked by another). ipsec look will show you a goodly amount of information about your IPsec connection(s), and ipsec barf gives you a ton of information, including configuration details, that can be used as part of a bug report or request for assistance. (Look carefully at the output and you'll see that it does not share your keys.) Even if you don't plan on filing a bug, you can use this command as a starting point for exploring what's pertinent to your FreeS/WAN configuration.

6.4.4 More Elaborate FreeS/WAN Configurations

It is trivial to add additional connections to /etc/ipsec.conf. Each connection will begin with the conn lname-rname statement (actually, the connection name is arbitrary, but using the lname-rname convention makes good sense). Since your first connection always starts up on ipsec0, don't expect to have the second connection in the file attach to the virtual device ipsec1. That's not how it works. Connections will share a given ipsecN interface as long as that is the interface being used to transmit and receive for that connection. This is because each ipsecN is bound to a physical device (see ipsec tncfg for the bindings). You'll note that the current KLIPS implementation allows for four ipsecN interfaces, meaning four diNerent physical interfaces—likely more than enough for most systems. When adding connections, there is no need to maintain consistency in the left/right semantics other than for your own sanity. If you build a topology with a closed path, e.g., a triangle, someone who is a lefty will have to be a righty in at least one of the connections.

FreeS/WAN can also be configured to support what is known as opportunistic connections. In this configuration, the system will attempt to establish IPsec communications for outgoing connections (or incoming connections, if it is configured for such), once again using DNS as a mechanism for retrieving the public key for the system on the other end. This demonstrates the real potential of IPsec—no more TCP wrappers, the reverse DNS lookup will be replaced by a key lookup (which can be compared to a cache of keys on our local system for an added layer of protection, similar to how ssh clients work), and once that checks out, you will have secure communication between the two endpoints without any further configuration required.

If we want to (i.e., we're truly paranoid enough), we can go back to cesium and xenon and, with just a little more work, replace our IPIP tunnels with IPsec using FreeS/WAN. When you really start working with FreeS/WAN, take the time to read the excellent documentation included in the doc/ directory of the source tree. And to reiterate, we're merely scratching the surface with FreeS/WAN and with secure tunneling software in this section. If you control both ends (and have a compiler on both ends), I'd recommend taking a look at VTun, which not only provides security, but incorporates compression and traJc shaping into the same tool. It is available at http://vtun.sourceforge.net/. If you're not sure what you want, but you haven't come across it yet, there is an excellent site specializing in cryptography software (all types, not just tunnels) for Linux at http://munitions.vipul.net/.

Now we're going to switch gears a little bit and talk about IP version 6. It may seem like a change in direction, but the IPsec protocol used in FreeS/WAN is actually a backport of the security features designed into IPv6. You'll also see that eventually we're going to end up tunneling again—this time to run IPv6 within IPv4 over links that don't yet speak IPv6.

  • + Share This
  • 🔖 Save To Your Account