[HOW-TO] Install routed OpenVPN server in a VIMAGE portjail

Status
Not open for further replies.

qwertymodo

Contributor
Joined
Apr 7, 2014
Messages
144
After much frustration with the available documentation, I've finally managed to get OpenVPN installed and configured inside a VIMAGE portjail that allows me to access my FreeNAS box over the internet, so I figured I'd document the process here. A lot of the issues I had were due to slight differences in the systems that the guides were written for, resulting in slightly different paths or other minor differences that I banged my head against the wall over for far too long, so to be clear, this guide was written for a portjail on FreeNAS 9.2.1.5. Things may change, so if you're on a different version, I can't guarantee you won't experience exactly the same kinds of issues as I had. Note, this is NOT a guide for setting up the FreeNAS box as a client, i.e. for VPN tunneling of bittorrent traffic or the like. This is for tunneling INTO your FN box remotely. For the sake of this guide, I'm using the 192.168.1.0/24 subnet as my actual home subnet (i.e. the FN box and all of its jails exist on that subnet), and I'm using the 172.16.1.0/24 subnet as the VPN subnet (i.e. VPN clients will be assigned addresses on that subnet). Any references to these subnets should be altered to match your network. Also, I just got it working last night, and I'm writing this guide having retraced my steps with a new jail this morning, but if I'm missing anything, let me know.

First things first, open the Jails tab in the Web GUI and click Add Jails. Give it a name, select portjail as the type, select Autostart and VIMAGE, and uncheck NAT and vanilla. The rest of the defaults should be sufficient.

If you want to be able to SSH into your jail instead of using the Web GUI's shell icon, follow the instructions here

Now, we install openvpn from the ports collection:

Code:
cd /usr/ports/security/openvpn && make install clean


The defaults should be fine, just be sure that easy-rsa is selected for installation.

Now we can begin setting up our certificate authority. Basically every guide I've seen suggests copying the easy-rsa scripts outside of their default location so they don't get wiped out by an update. I chose /usr/local/etc/openvpn (this is also where I store my server config).

Code:
mkdir /usr/local/etc/openvpn
cp -r /usr/local/share/easy-rsa /usr/local/etc/openvpn
cd /usr/local/etc/openvpn/easy-rsa


Now, we need to edit the vars file. Open it in your editor of choice, and edit the following lines

Code:
KEY_COUNTRY
KEY_PROVINCE
KEY_CITY
KEY_ORG
KEY_EMAIL
KEY_CN
KEY_NAME
KEY_OU


You can also change the KEY_SIZE to 2048 or 4096, if you're paranoid. This will cause the initial DH key generation to take much longer.

Now, we can set up our CA (these scripts don't like csh, so remain in sh for the rest of this guide, also note that the second command has two dots with a space between them)
Code:
sh
. ./vars
./clean-all
./build-ca


Next is the server key

Code:
./build-key-server server


When it asks if you want to sign the certificate, say yes. Do the same when it asks if you want to commit the changes. A challenge password is optional, but recommended.

Now for the client keys. Run this once for each client.
Code:
./build-key {clientname}


Finally, we generate our Diffie-Hellman key. This will take awhile, especially if you increased the KEY_SIZE variable.

Code:
./build-dh


It's also a good idea to create an "HMAC firewall" to help block DoS attacks and UDP port flooding

Code:
openvpn --genkey --secret keys/ta.key


Now, let's copy our keys out of the default directory so that they won't accidentally get deleted if you run ./clean-all

Code:
cp -r keys ..


Next, you'll need to create your OpenVPN server config file. I named mine server.conf and placed it in /usr/local/etc/openvpn. You can find example config files here. Note that we're creating a routed connection, not a bridge, so ignore any lines dealing with bridged connections or the tap interface.

Now in order to allow traffic on the VPN subnet to access your main subnet, you'll either need a static route on your router or else you'll need to configure NAT using IPFW in your jail. I opted for the latter. First of all, you'll need to find the name of the jail's network interface. Since this is a VIMAGE jail, you should have an interface named epairNb, where N is dependent on the number of VIMAGE jails you currently have. In order to determine the actual name, just run

Code:
ifconfig -a


Create the file /usr/local/etc/ipfw.rules and add the following lines (modify them for your network and interface name)

Code:
IPFW -q -f flush
IPFW -q nat 1 config if epairNb
IPFW -q add nat 1 all from 172.16.1.0/24 to any out via epairNb
IPFW -q add nat 1 all from any to any in via epairNb


Save ipfw.rules and open /etc/rc.conf.local and add these lines

Code:
openvpn_enable="YES"
openvpn_if="tun"
openvpn_configfile="/usr/local/etc/openvpn/server.conf"
cloned_interfaces="tun"
gateway_enable="YES"
firewall_enable="YES"
firewall_script="/usr/local/etc/ipfw.rules"


Now, in order to apply all of your changes, stop and restart the jail. As the final step, you'll need to port forward the openvpn port (1174 by default, set by your config file) to the IP address of your jail. You'll also need to copy the ca.crt, ta.key, {clientname}.key, and {clientname}.crt to your client machine, as well as generating a client config file, which may need to be be given the .ovpn extension, depending on the client you're using.

If you've done everything correctly, you should now be able to access your VPN from your client machine. If you can't connect, check your port forwarding settings on the router. You should be forwarding the OpenVPN port to the jail's IP address, which is in your main subnet (in my example, that's 192.168.1.203), NOT the VPN address (i.e. 172.16.1.xxx). If you're able to connect to the VPN, but can't talk to your FreeNAS machine, check that you can ping the VPN subnet's gateway (in my case, 172.16.1.1). If you can do that, but can't access anything on the main subnet, it's an issue with the NAT/static route, or else you neglected to include a push route line in your server config.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Oooo! I like it! I'm gonna move this to the guides section! Good job!
 

qwertymodo

Contributor
Joined
Apr 7, 2014
Messages
144
Give it a try and let me know if I've missed any steps. Also, doing this has helped me understand VIMAGE, and why your minecraft jail tutorial caused the jail's IP to show up as an option for the Web GUI. Using VIMAGE on the jail gets rid of that due to the virtualized network stack (rather than just creating an IPv4 alias). Also, without VIMAGE, you can't ssh into the jail, you just end up ssh-ing into the FreeNAS box. I'd imagine it would also cause issues for something like this.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Yes, VIMAGE is the only way to do what you want to do with VPN.
 

qwertymodo

Contributor
Joined
Apr 7, 2014
Messages
144
Hmm... I just tried wiping everything and following my guide from scratch, and the routing isn't working. If I add a static route to the VPN subnet on my router, everything works great, but the NAT isn't working right. I wonder if maybe that was supposed to be done on the FreeNAS side of things rather than in the jail, maybe? Here is where I got my info on that part, if anybody can chime in on that, I'd appreciate it. Networking is definitely not me forte.
 

Scareh

Contributor
Joined
Jul 31, 2012
Messages
182
trying to follow your guide on a test system to get a feel for it:
Code:
root@OpenVPN:/usr/local/etc/openvpn/easy-rsa # sh
# . ./vars
: not found
: not found
: not found
: not found
: not found
/whichopensslcnf: not foundvpn/easy-rsa
: not found
: not found
NOTE: If you run ./clean-all, I will be doing a rm -rf on /usr/local/etc/openvpn/keys-rsa
: not found
: not found
: not found
: not found
: not found
# ./clean-all
: No such file or directorypn/easy-rsa
# ./build-ca
/pkitool: not foundcal/etc/openvpn/easy-rsa
#

any suggestions on what i'm doing wrong?
Code:
# . ./vars
build-ca          build-dh          build-inter       build-key
build-key-pass    build-key-pkcs12  build-key-server  build-req
build-req-pass    clean-all         inherit-inter     list-crl
openssl-0.9.6.cnf openssl-0.9.8.cnf openssl-1.0.0.cnf pkitool
revoke-full       sign-req          vars              whichopensslcnf

he seems to be finding the files pretty fine.
 

qwertymodo

Contributor
Joined
Apr 7, 2014
Messages
144
Maybe cd after sh? I just rewrote that part of the guide yesterday when I realized sh worked (before I had instructions on installing bash, which took forever, because the easy-rsa scripts don't play nice with csh), so it's possible I may have missed something silly like that. Also, you did copy the files there in the previous step, right? Try ls to check that.

Sent from my Galaxy Nexus using Tapatalk
 

Scareh

Contributor
Joined
Jul 31, 2012
Messages
182
yeah i copied the files:
Code:
[root@freenas] ~# jls
   JID  IP Address      Hostname                      Path
     1  -               OpenVPN                       /mnt/Mirror/OpenVPN
[root@freenas] ~# jexec 1 csh
root@OpenVPN:/ # sh
# cd /usr/local/etc/openvpn/easy-rsa/
# ls
build-ca                build-req               openssl-1.0.0.cnf
build-dh                build-req-pass          pkitool
build-inter             clean-all               revoke-full
build-key               inherit-inter           sign-req
build-key-pass          list-crl                vars
build-key-pkcs12        openssl-0.9.6.cnf       whichopensslcnf
build-key-server        openssl-0.9.8.cnf
# . ./vars
: not found
: not found
: not found
: not found
: not found
/whichopensslcnf: not foundvpn/easy-rsa
: not found
: not found
NOTE: If you run ./clean-all, I will be doing a rm -rf on /usr/local/etc/openvpn             /keys-rsa
: not found
: not found
: not found
: not found
: not found
#


sry for all the late reply, its a test system at work which i only can tinker with during lunchbreaks ;-)
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
I can't get any of this to work. I managed to install (I think) the openvpn server inside the VIMAGE'd portjail, "sockstat" seems to show it is listening, but no matter how many times I look over the OpenVPN website for the client configuration, I just can't make a successful connection. I also tried to ping the VPN gateway from the server machine, on the VPN address, and got nothing. That's certainly a bad sign.

This only verifies what I've long thought to be true: OpenVPN configuration within jails (VIMAGE or not) is nuanced in some way, and won't submit to a simple one page guide.

:(

As I don't really need OpenVPN, I can't justify spending a bunch of time on it; but I hope someone can add more information to the guide.
 

unknownuser

Cadet
Joined
Jul 24, 2014
Messages
4
I am also having issues connecting with my client to the server, however sockstat does not show that my vpn portjail is listening. I have ensured that my port is forwarded on my router as well. I am running 9.2.1.6.

*EDIT*

I actually cannot start service openvpn, hence why I cannot connect. I am having an issue with my openvpn config. The variable "dev tun" is responsible for the error "ifconfig: interface tun0 does not exist".

Here are my jail settings:

Code:
Name: vpn
IPv4 address: 192.168.0.6
IPv4 netmask: /24
IPv4 aliases:
IPv4 bridge address:
IPv4 default gateway:
IPv6 address:
IPv6 prefix:
IPv6 bridge address:
IPv6 bridge prefix:
IPv6 bridge aliases:
IPv6 default gateway:
MAC: 02:b3:d6:00:0c:0b
Sysctls:allow.raw_sockets=true
autostart: Yes
VIMAGE: Yes
NAT: No


Server.conf file

Code:
#################################################
# Sample OpenVPN 2.0 config file for            #
# multi-client server.                          #
#                                               #
# This file is for the server side              #
# of a many-clients  one-server                 #
# OpenVPN configuration.                        #
#                                               #
# OpenVPN also supports                         #
# single-machine  single-machine                #
# configurations (See the Examples page         #
# on the web site for more info).               #
#                                               #
# This config should work on Windows            #
# or Linux/BSD systems.  Remember on            #
# Windows to quote pathnames and use            #
# double backslashes, e.g.:                     #
# "C:\\Program Files\\OpenVPN\\config\\foo.key" #
#                                               #
# Comments are preceded with '#' or ';'         #
#################################################

# Which local IP address should OpenVPN
# listen on? (optional)
;192.168.0.6

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one.  You will need to
# open up this port on your firewall.
port 1194

# TCP or UDP server?
;proto tcp
proto udp

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Windows adapter name
# from the Network Connections panel if you
# have more than one.  On XP SP2 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh dh1024.pem

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 172.16.1.0 255.255.255.0

# Maintain a record of client  virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 192.168.0.1 255.255.255.0 192.168.0.220 192.168.0.230

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
push "route 192.168.1.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# the TUN/TAP interface to the internet in
# order for this to work properly).
# CAVEAT: May break client's network config if
# client's local DHCP server packets get routed
# through the tunnel.  Solution: make sure
# client's local DHCP server is reachable via
# a more specific route than the default route
# of 0.0.0.0/0.0.0.0.
;push "redirect-gateway"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
;client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
;tls-auth ta.key 0 # This file is secret

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

# The maximum number of concurrently connected
# clients we want to allow.
;max-clients 100

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
;user nobody
;group nobody

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "\Program Files\OpenVPN\log" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
;log-append  openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20


Client conf

Code:
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
;proto tcp
proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
;remote 192.168.0.6 1194
;remote remole.com 1194
192.168.0.1 1194
;remote 173.2.93.174 1194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert nils.crt
key nils.key

# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server".  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server".  The build-key-server
# script in the easy-rsa folder will do this.
ns-cert-type server

# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20


ipfw.rules

Code:
IPFW -q -f flush
IPFW -q nat 1 config if epair4b
IPFW -q add nat 1 all from 172.16.1.0/24 to any out via epair4b
IPFW -q add nat 1 all from any to any in via epair4b


Thank you for your help!
 
Last edited:

robles

Explorer
Joined
Jul 29, 2014
Messages
89
qwertymodo, can you post your server.conf please? I'm also trying to do this but I don't know how to create the 172.16.1.0/24 subnet in the server config so it gets pushed to the client.

Also, is it possible to push the default gateway? we could also create a VPN with this and route all the local traffic through it.

Edit: Here's my current config file. It does work and I can reach other clients, but I had to add a static route to my router so it could find my 172.16.1.0/24 OpenVPN network. Is there any way to simplify this?

Code:
port 10011
proto udp
dev tun
ca /mnt/openvpn/keys/ca.crt
cert /mnt/openvpn/keys/server.crt
key /mnt/openvpn/keys/server.key
dh /mnt/openvpn/keys/dh1024.pem
server 172.16.1.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 10.0.0.0 255.255.255.0"
keepalive 10 120
comp-lzo
persist-key
persist-tun
verb 3
 
Last edited:
Status
Not open for further replies.
Top