Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:services:vpn:openvpn:server [2021/11/16 22:23] – Added section on dynamic assignment of IPv6 subnet to openvpn server. mm28ajosdocs:guide-user:services:vpn:openvpn:server [2023/10/14 05:14] – use service invocation vgaetera
Line 1: Line 1:
 ====== OpenVPN server ====== ====== OpenVPN server ======
-{{section>meta:infobox:howto_links#cli_skills&noheader&nofooter&noeditbutton}}+{{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
  
 ===== Introduction ===== ===== Introduction =====
   * This how-to describes the method for setting up [[wp>OpenVPN|OpenVPN]] server on OpenWrt.   * This how-to describes the method for setting up [[wp>OpenVPN|OpenVPN]] server on OpenWrt.
   * Follow [[docs:guide-user:services:vpn:openvpn:client|OpenVPN client]] for client setup and [[docs:guide-user:services:vpn:openvpn:extras|OpenVPN extras]] for additional tuning.   * Follow [[docs:guide-user:services:vpn:openvpn:client|OpenVPN client]] for client setup and [[docs:guide-user:services:vpn:openvpn:extras|OpenVPN extras]] for additional tuning.
 +  * It requires OpenWrt 21.02+ with OpenVPN 2.5+ supporting ''tls-crypt-v2''.
 +    * OpenWrt 19.07 users with OpenVPN 2.4 should refer to an [[docs:guide-user:services:vpn:openvpn:server?rev=1632708683|older revision]].
  
 ===== Goals ===== ===== Goals =====
Line 12: Line 14:
 ==== 1. Preparation ==== ==== 1. Preparation ====
 Install the required packages. Install the required packages.
-Specify the VPN server configuration parameters.+Specify configuration parameters for VPN server.
  
 <code bash> <code bash>
Line 20: Line 22:
  
 # Configuration parameters # Configuration parameters
-OVPN_DIR="/etc/openvpn" +VPN_DIR="/etc/openvpn" 
-OVPN_PKI="/etc/easy-rsa/pki" +VPN_PKI="/etc/easy-rsa/pki" 
-OVPN_PORT="1194" +VPN_PORT="1194" 
-OVPN_PROTO="udp" +VPN_PROTO="udp" 
-OVPN_POOL="192.168.8.0 255.255.255.0" +VPN_POOL="192.168.8.0 255.255.255.0" 
-OVPN_DNS="${OVPN_POOL%.* *}.1" +VPN_DNS="${VPN_POOL%.* *}.1" 
-OVPN_DOMAIN="$(uci get dhcp.@dnsmasq[0].domain)"+VPN_DN="$(uci -q get dhcp.@dnsmasq[0].domain)"
  
-# Fetch WAN IP address+# Fetch server address 
 +NET_FQDN="$(uci -q get ddns.@service[0].lookup_host)"
 . /lib/functions/network.sh . /lib/functions/network.sh
 network_flush_cache network_flush_cache
 network_find_wan NET_IF network_find_wan NET_IF
 network_get_ipaddr NET_ADDR "${NET_IF}" network_get_ipaddr NET_ADDR "${NET_IF}"
-OVPN_SERV="${NET_ADDR}" 
- 
-# Fetch FQDN from DDNS client 
-NET_FQDN="$(uci -q get ddns.@service[0].lookup_host)" 
 if [ -n "${NET_FQDN}" ] if [ -n "${NET_FQDN}" ]
-then OVPN_SERV="${NET_FQDN}"+then VPN_SERV="${NET_FQDN}" 
 +else VPN_SERV="${NET_ADDR}"
 fi fi
 </code> </code>
Line 48: Line 48:
 <code bash> <code bash>
 # Configuration parameters # Configuration parameters
-export EASYRSA_PKI="${OVPN_PKI}" +cat << EOF > /etc/profile.d/easy-rsa.sh 
-export EASYRSA_REQ_CN="ovpnca"+export EASYRSA_PKI="${VPN_PKI}" 
 +export EASYRSA_TEMP_DIR="/tmp" 
 +export EASYRSA_CERT_EXPIRE="3650"
 export EASYRSA_BATCH="1" export EASYRSA_BATCH="1"
 +EOF
 +. /etc/profile.d/easy-rsa.sh
  
 # Remove and re-initialize PKI directory # Remove and re-initialize PKI directory
Line 86: Line 90:
 uci set firewall.ovpn.name="Allow-OpenVPN" uci set firewall.ovpn.name="Allow-OpenVPN"
 uci set firewall.ovpn.src="wan" uci set firewall.ovpn.src="wan"
-uci set firewall.ovpn.dest_port="${OVPN_PORT}" +uci set firewall.ovpn.dest_port="${VPN_PORT}" 
-uci set firewall.ovpn.proto="${OVPN_PROTO}"+uci set firewall.ovpn.proto="${VPN_PROTO}"
 uci set firewall.ovpn.target="ACCEPT" uci set firewall.ovpn.target="ACCEPT"
 uci commit firewall uci commit firewall
-/etc/init.d/firewall restart+service firewall restart
 </code> </code>
  
Line 99: Line 103:
 # Configure VPN service and generate client profiles # Configure VPN service and generate client profiles
 umask go= umask go=
-OVPN_DH="$(cat ${OVPN_PKI}/dh.pem)" +VPN_DH="$(cat ${VPN_PKI}/dh.pem)" 
-OVPN_CA="$(openssl x509 -in ${OVPN_PKI}/ca.crt)" +VPN_CA="$(openssl x509 -in ${VPN_PKI}/ca.crt)" 
-ls ${OVPN_PKI}/issued \+ls ${VPN_PKI}/issued \
 | sed -e "s/\.\w*$//" \ | sed -e "s/\.\w*$//" \
-| while read -r OVPN_ID+| while read -r VPN_ID
 do do
-OVPN_TC="$(cat ${OVPN_PKI}/private/${OVPN_ID}.pem)" +VPN_TC="$(cat ${VPN_PKI}/private/${VPN_ID}.pem)" 
-OVPN_KEY="$(cat ${OVPN_PKI}/private/${OVPN_ID}.key)" +VPN_KEY="$(cat ${VPN_PKI}/private/${VPN_ID}.key)" 
-OVPN_CERT="$(openssl x509 -in ${OVPN_PKI}/issued/${OVPN_ID}.crt)" +VPN_CERT="$(openssl x509 -in ${VPN_PKI}/issued/${VPN_ID}.crt)" 
-OVPN_EKU="$(echo "${OVPN_CERT}" | openssl x509 -noout -purpose)" +VPN_EKU="$(echo "${VPN_CERT}" | openssl x509 -noout -purpose)" 
-case ${OVPN_EKU} in+case ${VPN_EKU} in
 (*"SSL server : Yes"*) (*"SSL server : Yes"*)
-OVPN_CONF="${OVPN_DIR}/${OVPN_ID}.conf" +VPN_CONF="${VPN_DIR}/${VPN_ID}.conf" 
-cat << EOF > ${OVPN_CONF} ;;+cat << EOF > ${VPN_CONF} ;;
 user nobody user nobody
 group nogroup group nogroup
 dev tun dev tun
-port ${OVPN_PORT+port ${VPN_PORT
-proto ${OVPN_PROTO+proto ${VPN_PROTO
-server ${OVPN_POOL}+server ${VPN_POOL}
 topology subnet topology subnet
 client-to-client client-to-client
Line 124: Line 128:
 persist-tun persist-tun
 persist-key persist-key
-push "dhcp-option DNS ${OVPN_DNS}" +push "dhcp-option DNS ${VPN_DNS}" 
-push "dhcp-option DOMAIN ${OVPN_DOMAIN}"+push "dhcp-option DOMAIN ${VPN_DN}"
 push "redirect-gateway def1" push "redirect-gateway def1"
 push "persist-tun" push "persist-tun"
 push "persist-key" push "persist-key"
 <dh> <dh>
-${OVPN_DH}+${VPN_DH}
 </dh> </dh>
 EOF EOF
 (*"SSL client : Yes"*) (*"SSL client : Yes"*)
-OVPN_CONF="${OVPN_DIR}/${OVPN_ID}.ovpn" +VPN_CONF="${VPN_DIR}/${VPN_ID}.ovpn" 
-cat << EOF > ${OVPN_CONF} ;;+cat << EOF > ${VPN_CONF} ;;
 user nobody user nobody
 group nogroup group nogroup
Line 141: Line 145:
 nobind nobind
 client client
-remote ${OVPN_SERV} ${OVPN_PORT} ${OVPN_PROTO}+remote ${VPN_SERV} ${VPN_PORT} ${VPN_PROTO}
 auth-nocache auth-nocache
 remote-cert-tls server remote-cert-tls server
 EOF EOF
 esac esac
-cat << EOF >> ${OVPN_CONF}+cat << EOF >> ${VPN_CONF}
 <tls-crypt-v2> <tls-crypt-v2>
-${OVPN_TC}+${VPN_TC}
 </tls-crypt-v2> </tls-crypt-v2>
 <key> <key>
-${OVPN_KEY}+${VPN_KEY}
 </key> </key>
 <cert> <cert>
-${OVPN_CERT}+${VPN_CERT}
 </cert> </cert>
 <ca> <ca>
-${OVPN_CA}+${VPN_CA}
 </ca> </ca>
 EOF EOF
 done done
-/etc/init.d/openvpn restart +service openvpn restart 
-ls ${OVPN_DIR}/*.ovpn+ls ${VPN_DIR}/*.ovpn
 </code> </code>
  
-Perform OpenWrt [[docs:guide-user:troubleshooting:backup_restore|backup]]. +Basic openvpn server configuration is now complete. 
-Extract client profiles from the archive and import them to your clients. +  - Perform OpenWrt [[docs:guide-user:troubleshooting:backup_restore|backup]]. 
- +  - Either extract client profile from the archive fileor use SCP to retrieve the <color #ed1c24>/etc/openvpn/client.ovpn</colorfile from the router  
-===== Dynamically provide IPv6 GUA-subnet to OpenVPN server ===== +  - Review/edit the IP address for the 'remoteline contained within the <color #ed1c24>client.ovpn</color> file  
-To assign a global-unicast (GUA) IPv6 address from the IPv6 prefix of the WAN interface to OpenVPN clientswe must assign a free subnet of the delegated WAN IPv6 prefix to the OpenVPN server via the openvpn config option ''server_ipv6''. However, as for most users the delegated IPv6 prefix is non-static, a dynamic/automatic assignment is required. +  Import the client.ovpn profile into your clients.
- +
-To dynamically assign a free subnet, in this example /64, of the delegated WAN prefix to the OpenVPN server, we must define a dedicated VPN interface of type ''static''. In this example, the interface is named ''LANvpn'' and the device is ''tun1''. Refer to [[https://forum.openwrt.org/t/openvpn-dynamically-assign-client-ipv6-gua-with-dynamic-wan-ipv6-prefix/104590/5|this]] for a detailed discussion. +
- +
-<code bash> +
-config interface 'LANvpn' +
- option proto 'static' +
- option device 'tun1' +
- option ip6assign '64' +
- option ip6class 'wan6' +
-</code> +
- +
-Next, we add the following hotplug script ''/etc/hotplug.d/iface/30-ipv6pdchange'' to assign the IPv6 subnet of the ''LANvpn'' interface to our OpenVPN server. This way, we can rely on the subnetting routines of OpenWRT to find a free subnet for the OpenVPN server considering the IPv6 configurations of other interfaces. The hotplug script is triggered only on IPv6 prefix changes and assumes the OpenVPN server is named ''LAN'' and is defined in ''/etc/config/openvpn''+
- +
-<code bash> +
-#!/bin/sh +
- +
-# if ipv6 address has changed on WAN6 go for it +
-if [ "$INTERFACE" = "wan6" -a "$IFUPDATE_ADDRESSES" = "1"+
-then +
-    ################## +
-    # PD fix +
-    ################## +
-    # reload interface to update PD to downstream interfaces, refer to bug described here https://forum.openwrt.org/t/delegated-ipv6-prefix-not-updated/56135 +
-    /sbin/ifup wan6 +
- +
-    # wait some time to get IPv6 up +
-    sleep 30 +
-     +
-    ################## +
-    # IPv6 OpenVPN assignment +
-    ################## +
-     +
-    # source network functions +
-    source /lib/functions/network.sh +
- +
-    # get IPv6 of openvpn interface +
-    network_get_ipaddr6 IPV6_OVPN "LANvpn" +
-     +
-    if [[ ! -z $IPV6_OVPN ]] +
-    then      +
-     # get subnet size of OpenVPN interface +
-     NET_ASSIGN="$(uci get network.LANvpn.ip6assign)" +
- +
-     # set openvpn server-ipv6 option +
-     uci set openvpn.LAN.server_ipv6="$IPV6_OVPN/$NET_ASSIGN" +
-     uci commit openvpn +
- +
-     # reload openvpn +
-     /etc/init.d/openvpn reload      +
-    fi +
-fi +
-</code> +
  
 +For an additional .ovpn after completing the above:
 +  - Run this [[docs:guide-user:services:vpn:openvpn:extras#multi-client]] script.
 +  - Now make a script consisting of the "Configuration parameters" of Part 1 above and all of Part 4 above and run it. Note that the "remote" line may be missing in the new ovpn (use the original as a reference for that).
 ===== Testing ===== ===== Testing =====
 {{section>docs:guide-user:services:vpn:wireguard:server#testing&noheader&nofooter&noeditbutton}} {{section>docs:guide-user:services:vpn:wireguard:server#testing&noheader&nofooter&noeditbutton}}
Line 231: Line 186:
 <code bash> <code bash>
 # Restart services # Restart services
-/etc/init.d/log restart; /etc/init.d/openvpn restart; sleep 10+service log restart; service openvpn restart; sleep 10
  
 # Log and status # Log and status
Line 239: Line 194:
 pgrep -f -a openvpn pgrep -f -a openvpn
 ip address show; ip route show table all ip address show; ip route show table all
-ip rule show; iptables-save -c +ip rule show; ip -6 rule show; nft list ruleset
-ip -6 rule show; ip6tables-save -c+
  
 # Persistent configuration # Persistent configuration
Line 246: Line 200:
 head -v -n -0 /etc/openvpn/*.conf head -v -n -0 /etc/openvpn/*.conf
 </code> </code>
 +
 +===== Notes =====
 +For beginners to OpenVPN server, this PDF guide may be helpful.  It is based on above cli instructions with additional note and tips.
 +[[https://www.dropbox.com/s/idjzqs3cyyb1zai/7-OpenVPN%20Server%20for%20HH5A.pdf?dl=0|OpenVPN server setup guide for BT Home Hub 5A]]
  
  • Last modified: 2023/10/29 01:00
  • by vgaetera