Openswan tunnel to Juniper SSG

Just a small gathering of information on how I’ve setup a tunnel between a Centos 6.3, with openswan and NETKEY ipsec stack, and a Juniper SSG. Before we start configuring, lets define IP’s nets and address (by the way, those are not the real IP’s). We are link two networks with this tunnel, not a network-to-client configuration.

On the Centos side we have:

  • Name: Office City A
  • External Ip:
  • Internal Network:
  • Internal Gateway Ip:

On the Juniper SSG we have:

  • Name: Office City B
  • External Ip:
  • Internal Network:
  • Internal Gateway Ip:

Pre-shared Key: my-long-and-secret-key

Centos Side

First we need to install and configure the centos box. That should be fairly simple, start by installing openswan:

yum install openswan

Now we have to edit /etc/ipsec.conf. The default config should be fine for us, but we have to make sure that the line which includes the configs “.conf” stored under /etc/ipsec.d/ is uncommented. Your config file should look something like this:

# /etc/ipsec.conf - Openswan IPsec configuration file
# Manual:     ipsec.conf.5
# Please place your own config files in /etc/ipsec.d/ ending in .conf

version	2.0	# conforms to second version of ipsec.conf specification

# basic configuration
config setup
	# Debug-logging controls: "none" for (almost) none, "all" for lots.
	# klipsdebug=none
	# plutodebug="control parsing"
	# For Red Hat Enterprise Linux and Fedora, leave protostack=netkey
	# Enable this if you see "failed to find any available worker"
	# nhelpers=0

#You may put your configuration (.conf) file in the "/etc/ipsec.d/" and uncomment this.
include /etc/ipsec.d/*.conf

You also need to make sure that file /etc/ipsec.secrets includes all “.secret” files under /etc/ipsec.d/. It should read like:

include /etc/ipsec.d/*.secrets

We have to create the config file for our tunnel, let’s name it “office_b_tun”. The new config will be stored under /etc/ipsec.d/office_b_tun.conf. The content of the file should be:

conn office_b_tun

We need to set the PSK for the tunnel, so edit the file /etc/ipsec.d/office_b_tun.secrets. PSK "my-long-and-secret-key"

As I don’t have two NIC’s on my server, I’ve setup an alias for eth0. This is not needed if you have two NIC’s. Edit /etc/sysconfig/network-scripts/ifcfg-eth0:0:


Restart your network, and start ipsec.

/etc/init.d/ipsec start

Finish configuring the Juniper, and then check the output of ipsec auto --status, it should read something like “IPsec SA established” and “ISAKMP SA established”. Verify your routes and test the tunnel.

Juniper SSG

We can configure the junipers using either the WebUI or the CLI, so I’ll describe first how to configure using the WebUi, and latter I’ll show the CLI config lines. I’m doing a Route Based VPN config as it adds more flexibility to my setup, you can use a Policy Based VPN if you wish, but I’m not covering that here (see a sample config here).

Some extra info we need to know on the Juniper side, is that I have a VPN Zone bound to trust-vr. I recommend that you create a zone for your VPN’s tunnels, as it makes easier to add trafic policies to it later.

Tunnel Interface

Go to Network -> Interface, select “Tunnel IF” and click the New button. Select a not used tunnel number, mine is 1. Also, make sure you select the Zone (VR) as “vpn” and that it’s an unnumbered interface. Click Ok. That’s it for the Tunnel Interface.

VPN AutoKey Gateway

Now we need to setup the VPN Gateway, for that go to VPN -> AutoKey Advanced -> Gateway. Click on the New button. Name the gateway as “gw_to_office_a”. Make sure “Static IP Address” is selected, and fill in the IPv4/v6 Address/Hostname field. The remote IP address is

Click on Advanced button. On that page, enter the Pre-shared Key “my-long-and-secret-key”. Select the correct outgoing interface, mine is “Ethernet0/0”.

On the Security Level field, select “pre-g2-3des-md5“. It’s really important that you get this right!

Make sure the Mode (Initiator) is set to Main. That’s it, just click Ok to save the gateway configuration.


Time to setup the AutoKey IKE VPN, so go to VPN -> AutoKey IKE. Click on New button. I’ll name this vpn as “vpn_to_office_a”. Make sure you selected “gw_to_office_a” as the predefined gateway. Click on Advanced.

On the advanced configuration page, set the security level as “g2-esp-3des-md5“. That’s really import, otherwise the tunnel will not work.

Bind the VPN to tunnel interface “tunnel.1”. Check “Proxy-ID Check”, “VPN Monitor”, “Optimize”, “Rekey”. Select as source interface, your external port, mine is “Ethernet0/0”. Fill in the destination IP, the remote internal gateway ip address,

Click Ok to save the tunnel.


We need to setup the Proxy-ID for the tunnel, go to the AutoKey IKE listing, click on Proxy ID for the “vpn_to_office_a” tunnel. Add the following:

Service: ANY

Click on New, and that’s it.


We need to set a static route to Centos network, as it’s not running a dynamic routing daemon (such as RIP, OSPF, BGP, …). Go to Network -> Routing -> Destination. Select “trust-vr” and click New.

The route we want to add is, using as gateway the interface “tunnel.1” with the address Make the route permanent, set the preference to 20, and add a description “office A network”.

Click Ok to save it.


As I’m connecting two Trusted networks, I’ll allow any trafic incoming from VPN to Trusted and from Trusted to VPN. You can, and should, set tighter policies as you see fit.


You can configure the VPN using the CLI, use the following commands, adapt as need.

set zone id 100 "vpn"
set interface "tunnel.1" zone "vpn"
set interface tunnel.1 ip unnumbered interface ethernet0/0
set ike gateway "gw_to_office_a" address Main outgoing-interface "ethernet0/0" preshare "my-long-and-secret-key" proposal "pre-g2-3des-md5"
set ike respond-bad-spi 1
set ike ikev2 ike-sa-soft-lifetime 60
unset ike ikeid-enumeration
unset ike dos-protection
unset ipsec access-session enable
set ipsec access-session maximum 5000
set ipsec access-session upper-threshold 0
set ipsec access-session lower-threshold 0
set ipsec access-session dead-p2-sa-timeout 0
unset ipsec access-session log-error
unset ipsec access-session info-exch-connected
unset ipsec access-session use-error-log
set vpn "vpn_to_office_a" gateway "gw_to_office_a" no-replay tunnel idletime 0 proposal "g2-esp-3des-md5" 
set vpn "vpn_to_office_a" monitor source-interface ethernet0/0 destination-ip optimized rekey
set vpn "vpn_to_office_a" id 0xa bind interface tunnel.1
unset interface tunnel.1 acvpn-dynamic-routing
set url protocol websense
set vpn "vpn_to_office_a" proxy-id check
set vpn "vpn_to_office_a" proxy-id local-ip remote-ip "ANY" 
set route interface tunnel.1 gateway preference 20 permanent description "office A network"


On the Office A network, try to ping a machine on B Office network, something like:


On the Office B network, try to ping a machine on A Office network, something like:


If you got ping’s, everything is up and running! Have fun!

On hardware tutorials

A friend of mine sent me this post from Phillip Burgess on why Arduino foster innovation on the long run. And he is absolutely right, Arduino does provide the basic environment for learning electronics and basic concepts on computer science and programming.

But I think we could do better. Arduino lack of proper debugging interfaces is a real problem. But it is easily fixable, just create and kickstart an open Arduino Debugger, using the debugWire or the default JTAG interface.

What is my main issue is the lack of in-depth tutorials and manuals, for the ones that need to go the extra mile – learn and explore the full capabilities of the platform. The VIC-20 and the PET computers (or toys) had really great manuals, including the whole schematic and full programming documentation. We don’t have that for Arduino, we have lots of scattered tutorials most on the same subject, without adding much to it.

The basic example, of one of this tutorials, is the “hardware” hello world: blinking a LED. We, electrical engineers, computer scientists, physicists, chemists, take for granted what a LED is, what a CPU does, what an algorithm is, etc. So we just write the really basic steps for doing this hello world. That’s more than enough for us, but it’s just the tip of the iceberg for a beginner and sometimes even for someone with a technology background.

Why can’t a tutorial begin explaining some basic physics of how a LED works, why a through hole led has different length legs, and how an embedded cpu has digital and analog inputs and outputs. LED is just ubiquitous nowadays, but no one really knows how it works (ok, you physicists should know it pretty well). Couldn’t we, as a collective work, write better tutorials? Have a physicist write a basic outline of a PN junction and how it can emit light. Then ask an electrical engineer to write basic concepts of an output port and a computer scientist could write on how a CPU is just a “dumb” serial worker and how software is translated to that “dumb” worker.

I bet that we would have better engineers, fewer frustrated engineers working on software and not on hardware, and happier computer scientist willing to go to the really low-level of the CPU. Just because they now know all the effort that have gone in the develop of that “toy”.

Maybe I should try to write this tutorial…

Open Hardware Projects that I would like to create or be part of

For the last couple of weeks I’ve had several ideas of products that I would like do create as an Open Hardware project. Some of them might be just crazy grandeur – of doing something somewhat impossible – and would have prohibitive costs for something open sourced. Just to be sure, I don’t want to build an open source lunar module (although that would be wicked). Others are doable with a small funding and reusing other open hardware projects.

Letting the impossible projects aside, for now, I would really like to create these with the help of anyone willing to share knowledge and learn something new in the process. So here is the list (with no proper order):


There are several DIY beer in the internet, lots of kits – some really basic, others really advanced. Some brewers share the way they brew, but none is really Open Source, or a full project. The idea is to have a small brewery able to brew 40 liters of beer, or less. The system should control all the pumps, heaters, connections needed.

Agriculture/Garden Sensors Network

An active soil monitor with high quality measurements, able to send periodic data on temperature, moisture, pH, etc. Battery powered, with a solar charger, battery should last for at last 10 days. The collected data could be feed to an automatic irrigation system, or just enable better crops.

Telecine System

Since I’ve discovered some really old 8mm films that my grandpa made in the 1960’s, my interest on the subject grew. Converting an old film to a digital media is really expensive, but the mechanics and electronics needed are somewhat simple, and have been in use for ages. The mechanics are known to work since circa 1880, the electronics (CCD’s) have been in use since 1971.


There are several high-speed data acquisition systems being developed by CERN. Reuse one of these designs, add a FPGA, a USB 3.0 port, and you have a really basic oscilloscope. Want more channels? Add a backplane for connecting up to 4 acquisition cards and stream all the data to the USB port. Connect the device to a PC, tablet, etc and you have a really good scope. The cost of this system would be probably be bound by the cost of the ADC, which is quite expensive (especially in small quantities).


  • FMC ADC 1G 10B 2CHA
  • FMC ADC 250M 12B 2CHA
  • Portable Ultrasound

    A friend of mine, who is a doctor, always talk about the benefits of using ultrasound to early diagnostics. But ultrasound devices for diagnostic are just too expensive to be used by a broader range of physicians, like those on remote areas, accidents, etc. Ultrasound has been in development since circa 1950, which means that the basic electronic needed is simple. The data processing algorithms are way more complicated than the electronics. My idea is to focus on the electronics to have a basic working device, just acquiring all the information needed to feed the proper imaging algorithms. Main challenge is having a fast (2MHz ~ 18Mhz) switch with high voltage (~90V).

    Asterisk, OpenVPN and QoS

    Installing a VoIP system is nowadays an easy task, just install Asterisk, have a few SIP clients and you have an ‘instant’ telephone system. But your system will not be as reliable as the one offered by any telecom company. Why? Quality of Service, or for short, QoS.

    Telecom companies use sophisticated hierarchies of systems to deliver the needed QoS. Backbones uses SDH systems, where one can guarantee the bandwidth and throughput for any kind of data. So if you specify that a voice packet should be delivered in 10ms, it will get delivered in that time span. Now when it comes to IP networks, you have no guarantee that your packet will be delivered in that time frame, which is a good thing when you’re downloading files, opening web pages, and so on. But when it comes to voice and video streaming, it’s a real mess. So you must create some QoS rules for your packets.

    Asterisk has this real nice feature for aggregating multiple servers so that it works as a single phone network. The only problem is that this feature is not really secure, so as to mitigate that, one can always create VPN’s (Virtual Private Networks). But how does that impact your QoS solution? Well, depends on what kind, or how you configure your VPN, with OpenVPN it’s quite simple.

    Just as a reminder, for the rest of the article when I say QoS, I really mean the QoS of the gateway of your network. The gateway is the one place that will enforce the needed quality of service (okay, on bigger networks you will have multiple routers which will need to be configured for QoS too).

    Don’t get too excited with QoS, even though you did everything by the book that doesn’t mean that your ISP will use TOS field the same way you did. By that I mean, you won’t solve any problem with QoS if the problem is not on how you route packets to the internet. If you have full control of your link and all the router in between your networks, you’re a lucky guy!

    The Network

    We have some computers, servers, and IP phones on each network. The OpenVPN tunnel server doesn’t need to be the same as the gateway, as long as you export the correct ports to that server. Make sure you also add the correct gateway for the packets that should be tunneled (ie. packets for the network that originates on the network). On the image, the tunnel is represented by the red lines.

    A sample network using Asterisk and OpenVPN


    I don’t intend to give a full how-to on OpenVPN, just a basic configuration, with a highlight on how to get QoS for the tunneled packets. Besides that, configuring OpenVPN is really simple.

    First you have to create your own Certificate Authority (CA). You can use something like tinyca or minica, or the command line version, described here. Remember that you will need one certificate per client. After that is just a matter of writing a really simple text file. Below are a sample configuration, known to work well integrating two Asterisk servers.


    # OpenVPN server
    # Listen to local ip address only
    # Should be exported on the router
    port 1194
    proto udp
    dev tun
    # SSL/TLS CA and keys
    ca ca.crt
    # Diffie Hellman Parameters
    dh dh1024.pem
    # Server tunnel
    ifconfig-pool-persist ipp.txt
    push "route"
    client-config-dir client-configs
    keepalive 10 120
    # Drop privileges
    user nobody
    group nogroup
    # Persist
    # Logs
    verb 5
    status /var/log/openvpn.log
    # Fork to the background


    The highlighted line is the one which will make the QoS work for the encrypted packets. If you think that passing the TOS (Type of service) is a security fault, don’t panic, just create another tunnel for passing your sensitive data – and that’s really easy to do with OpenVPN.

    # OpenVPN client
    # Interface for tunnel
    # Protocol and Port
    dev tun0
    proto udp
    port 1194
    # SSL/TLS CA and keys
    ca /etc/openvpn/certs/ca.crt
    cert /etc/openvpn/certs/remote1.mynetwork.crt
    key /etc/openvpn/keys/remote1.mynetwork.key
    # Symmetric cipher
    cipher BF-CBC
    # Remote server to connect to. Can be domain name or IP address.
    # Check if the tunnel went down and restart it. 
    # 10 is the ping interval number and 120 is the timeout to restart.
    keepalive 10 120
    # This is need so we can apply QoS to the tunnel
    # Drop privileges
    user nobody
    group nogroup
    # Use a persistent key and tunnel interface.
    # Log to file instead of syslog
    log-append /var/log/openvpn.log
    verb 4
    # Fork to the background

    If you can ping the remote server, using the internal IP address, then your tunnel is up and running.


    I suppose that you already know how to configure an Asterisk server, if you don’t you can follow my guide (it’s a bit outdated, I might update it soon).

    Getting IAX2 working is really simple too, so I won’t describe it. If you’re using FreePBX, you can follow this guide. Remember to use the internal IP’s from your network.

    Make sure your asterisk installation is tagging the correct TOS for the packets. On my FreePBX install it already had the correct configuration set on /etc/asterisk/sip_general_additional.conf. Check your asterisk configuration for the following lines:


    This tags your voice data as Expedited Forwarding, normal SIP packets get Class Selector 3 and video data gets Assured Forwarding, Class 4, with drop precedence 1. More on what all this means shortly.


    Getting the right choice of tools for your specific QoS application is a hard problem. You can have some traffic shaping algorithms, congestion avoidance mechanisms and quite a few packet scheduling algorithms. I’m not an expert on how all these different types of algorithms work, or what is the best solution for your case. I’m just putting together some information that I think is relevant. One can always read all the RFC’s about QoS.

    First things first, the mentioned TOS field is now called DSCP (Differentiated Services Code Point), it replaces the TOS field and is specified for IPv4 and IPv6 (for reference RFC2474 is the specification). It tries to maintain backward compatibility with the TOS field. Most networks use the following traffic classes:

    • Default PHB — which is typically best-effort traffic
    • Expedited Forwarding (EF) PHB — dedicated to low-loss, low-latency traffic
    • Assured Forwarding (AF) PHB — gives assurance of delivery under prescribed conditions
    • Class Selector PHBs — which maintain backward compatibility with the IP Precedence field.

    That is what EF, CS3 and AF41 means, just a common way of signalling that your packet is important, or not that much. But just tagging your packets won’t get you far. For now, you’ve got your Asterisk correctly tagging the packets, and your tunnel to preserve them. Time to add the magic to classify and prioritise the packets!

    Linux Traffic Control

    Linux has the tc tool for configuring and setting up a QoS policy. With it you can configure different kinds of queueing disciplines and classes. This queues acts directly on net devices, so you have to configure it per device. In the example below we have an ADSL modem on ppp0 device.

    TC allows you to configure classful and classless disciples, each one supporting different scheduling algorithms. We will use Hierarchy Token Bucket (HTB) for the classful packets (the ones that got tagged by Asterisk), and Stochastic Fairness Queueing (SFQ) for the classless packets. After getting your queues configured you have to inform iptables that it should use the queue, that’s basically setting up some CLASSIFY targets. You definitely can add some MARK rules to tag your packets, but we don’t need it, Asterisk is doing that job for us.

    First we will configure what is the maximum bandwidth allowed, in this case we have an 1000kbps uplink that we want to add a QoS policy. The following table illustrates the QoS policy required for the network. As we are using an asymmetric connection, we will limit the upload bandwidth to 95% of the nominal speed.

    Class Nominal rate Maximum rate Priority Packets
    Real time 47.5kbps 95kbps 0 ICMP, SYN, RST, ACK
    High 522.5kbps 950kbps 1 EF and CS3 packets
    Regular 190kbps 950kbps 2 Regular traffic, HTTP, SSH, etc
    Bulk 190kbps 950kbps 3  
    QoS Policy

    With the queues in place you just have to add the necessary iptable rules. The rules will classify the packets that have the DSCP tag using the same classes that you defined using tc. That’s it, your QoS is now in place. Just make sure you add and remove the rules according to the status of your link (in this case ppp0). The script bellow is called by /etc/ppp/ip-up.d and /etc/ppp/ip-down.d, with the start and stop targets respectively.

    # !/bin/bash
    # 20110916 - Leonardo Santos <leonardo at aligera dot com dot br>
    # Initial version. It only uses the iptables target CLASSIFY.
    # For the QoS to work, Asterisk has to tag the packets with the right DSCP.
    # The OpenVPN tunnel must be passing along the DSCP field, and not blanking it out.
    # uplink in kbps
    do_iptables() {
            iptables -$1 POSTROUTING -t mangle -p icmp -j CLASSIFY --set-class 1:$CLASS_RT
            iptables -$1 POSTROUTING -t mangle -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j CLASSIFY --set-class 1:$CLASS_RT
            iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class cs3 -j CLASSIFY --set-class 1:$CLASS_HIGH
            iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class ef -j CLASSIFY --set-class 1:$CLASS_HIGH
    add_rules() {
            tc qdisc add dev $DEV root handle 1: htb default $CLASS_BULK
            tc class add dev $DEV parent 1: classid 1:1 htb rate ${CEIL}kbit ceil ${CEIL}kbit
            tc class add dev $DEV parent 1:1 classid 1:$CLASS_RT   htb rate $((1*$CEIL/20))kbit  ceil $(($CEIL/10))kbit prio 0
            tc class add dev $DEV parent 1:1 classid 1:$CLASS_HIGH htb rate $((11*$CEIL/20))kbit ceil ${CEIL}kbit       prio 1
            tc class add dev $DEV parent 1:1 classid 1:$CLASS_REG  htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 2
            tc class add dev $DEV parent 1:1 classid 1:$CLASS_BULK htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 3
            tc qdisc add dev $DEV parent 1:$CLASS_HIGH handle 120: sfq perturb 10
            tc qdisc add dev $DEV parent 1:$CLASS_BULK handle 130: sfq perturb 10
            do_iptables A
    del_rules() {
            tc qdisc del dev $DEV root
            do_iptables D
    show_status() {
            tc -s -d class show dev $DEV
            tc -s -d qdisc show dev $DEV
    case $1 in
                    echo "Usage: $0 {start|stop|restart|status}"
                    exit 1

    I would like to thank Leonardo Santos for putting the script together and letting me publish it, and for being a good friend.