Setting up mGRE tunnel
We start by adding mGRE tunnels to our network configuration.
Contents of /etc/networking/interfaces
...
auto gre1
iface gre1 inet static
pre-up ip tunnel add $IFACE mode gre key 42 ttl 64 dev br0 || true
address 192.168.148.2
netmask 255.255.255.255
post-down ip tunnel del $IFACE || true
Note: In conjunction with IPsec VPNs this allows passing of routing information between connected networks.
Note: A standard GRE tunnel will specify its start and endpoint. In case of the mGRE tunnel we do not assign an endpoint, and we provide it an ip address.
Note: A tunnel key is a 32-bit number is assigned to both ends of the tunnel. A key is added with the add gre tunnel command, and can be modified or deleted with the set gre tunnel command. The tunnel key provides a weak form of security because packets injected into the tunnel by an external party are rejected unless they contain the correct tunnel key value. The key also allows packets to travel through specific tunnels in multi-point networks because the key identifies each end of one tunnel.
Setting up IPSec VPN
To encrypt this tunnel, and the traffic in it, we will use ipsec. for ipsec we will use strongswan which has the vici plugin, see:
The vici plugin provides VICI, the Versatile IKE Configuration Interface. As its name indicates, it provides an interface for external applications to not only configure, but also to control and monitor the IKE daemon charon.
for this we also need a modified version of strongswan, provided by fabled.
apk add strongswan
Contents of /etc/swanctl/swanctl.conf
connections {
dmvpn {
version = 2
pull = no
mobike = no
dpd_delay = 15
dpd_timeout = 30
fragmentation = yes
unique = replace
rekey_time = 4h
reauth_time = 13h
proposals = aes256-sha512-ecp384
local {
auth = psk
id = spoke1
}
remote {
auth = psk
}
children {
dmvpn {
esp_proposals = aes256-sha512-ecp384
local_ts = dynamic[gre]
remote_ts = dynamic[gre]
inactivity = 90m
rekey_time = 100m
mode = transport
dpd_action = clear
reqid = 1
}
}
}
}
Note: To control the IPSec VPN, NHRP will talk to Strongswan via its vici plugin (Versatile IKE Configuration Interface).
Note: You will need a modified version of Strongswan by fabled which you can find in Alpine Linux Git repository
Generate PKI certificates
Tip: The ipsec pki command suite allows you to run a simple public key infrastructure. Generate RSA and ECDSA public key pairs, create PKCS#10 certificate requests containing subjectAltNames, create X.509 self-signed end entity and root CA certificates, issue end entity and intermediate CA certificates signed by the private key of a CA and containing subjectAltNames, CRL distribution points and URIs of OCSP servers. You can also extract raw public keys from private keys, certificate requests and certificates and compute two kinds of SHA1-based key IDs.
First, generate a private key, the default generates a 2048 bit RSA key
ipsec pki --gen > caKey.der
Now self-sign a CA certificate using the generated key:
ipsec pki --self --in caKey.der --dn "C=CH, O=strongSwan, CN=strongSwan CA" --ca > caCert.der
Adjust the distinguished name (DN) to your needs, it will be included in all issued certificates.
For each peer, i.e. for all VPN clients and VPN gateways in your network, generate an individual private key and issue a matching certificate using your new CA:
ipsec pki --gen > peerKey.der
ipsec pki --pub --in peerKey.der
Note: The second command extracts the public key and issues a certificate using your CA.
In case end entity certificates have to be revoked, Certificate Revocation Lists (CRLs) may be generated with the ipsec pki --signcrl command:
ipsec pki --signcrl --cacert caCert.der --cakey caKey.der --reason superseded --cert peerCert.der > crl.der
Note: The certificate given with --cacert must be either a CA certificate or a certificate with the crlSign extended key usage (--flag crlSign).
Install certificates
On each peer store the following certificates and keys in the /etc/ipsec.d/ subdirectory tree:
/etc/ipsec.d/private/peerKey.der holds the private key of the given peer.
/etc/ipsec.d/certs/peerCert.der holds the end entity certificate of the given peer.
/etc/ipsec.d/cacerts/caCert.der holds the CA certificate which issued and signed all peer certificates.
Tip: Never store the private key caKey.der of the Certification Authority (CA) on a host with constant direct access to the Internet (e.g. a VPN gateway), since a theft of this master signing key will completely compromise your PKI.
Optionally, the CRL may be stored in the following directory (if the certificate contains an URL to a CRL, it will be fetched on demand):
/etc/ipsec.d/crls/crl.der holds the CRL signed by the CA (or a certificate containing the crlSign EKU).