Home > Articles > Operating Systems, Server

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 192.168.0.0/16 over that secure tunnel.

# basic configuration on drone1
config setup
             interfaces=%defaultroute
             klipsdebug=none
             plutodebug=none
             plutoload=%search
             plutostart=%search
             uniqueids=yes

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

conn vivaldi-drone1
             leftid=@vivaldi.mancill.com
             leftrsasigkey=0sAQO8...
             left=192.168.16.55
             leftnexthop=192.168.16.13
             rightid=@drone1.mancill.com
             rightrsasigkey=0x0103....
             right=192.168.18.11
             rightnexthop=192.168.18.13
             rightsubnet=192.168.0.0/16
             auto=start 

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020