Ho scritto questa guida per la configurazione di OpenVPN su AGTEF e AGTHP in modalità routing.
Gli indirizzi usati per la lan sono quelli canonici 192.168.1.x.
Gli indirizzi usati per la rete vpn sono quelli canonici 10.8.0.x.
Pertanto se volete utilizzare i vostri indirizzi dovete modificare gli script che ho postato, soprattutto nella parte firewalling.
In questo esempio non è previsto l'utilizzo di interfacce grafiche (es. Luci), tutta la configurazione è manuale.
Verificare di avere opkg configurato correttamente per accedere al repository
#AGTEF < 1.1.0
https://repository.ilpuntotecnico.com/files/roleo/public/agtef/brcm63xx-tch/packages#AGTHP < 1.1.0
https://repository.ilpuntotecnico.com/files/roleo/public/agthp/brcm63xx-tch/packages#AGTEF/AGTHP >= 1.1.0
https://repository.ilpuntotecnico.com/files/roleo/public/agtef/1.1.0/brcm63xx-tch/packagesed eseguire un aggiornamento con
opkg update
Se la procedura fallisce a causa di errori openssl eseguire il comando seguente per installare openssl-util:
#AGTEF < 1.1.0
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agtef/brcm63xx-tch/packages/base/openssl-util_1.0.2n-1_brcm63xx-tch.ipk
#AGTHP < 1.1.0
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agthp/brcm63xx-tch/packages/base/openssl-util_1.0.2n-1_brcm63xx-tch.ipk
#AGTEF/AGTHP >= 1.1.0
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agtef/1.1.0/brcm63xx-tch/packages/base/openssl-util_1.0.2q-2_brcm63xx-tch.ipk
e poi riprovare l'aggiornamento.
Sostituire la libreria openssl esistente con la versione 1.0.2n-1 o 1.0.2q-2:
#AGTEF < 1.1.0
opkg remove libopenssl --force-depends
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agtef/brcm63xx-tch/packages/base/libopenssl_1.0.2n-1_brcm63xx-tch.ipk
#AGTHP < 1.1.0
opkg remove libopenssl --force-depends
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agthp/brcm63xx-tch/packages/base/libopenssl_1.0.2n-1_brcm63xx-tch.ipk
#AGTEF/AGTHP >= 1.1.0
opkg remove libopenssl --force-depends
opkg install https://repository.ilpuntotecnico.com/files/roleo/public/agtef/1.1.0/brcm63xx-tch/packages/base/libopenssl_1.0.2q-2_brcm63xx-tch.ipk
A questo punto ho seguito la guida che era disponibile all'indirizzo
https://openwrt.org/docs/guide-user/services/vpn/openvpn/basic e che ora è cambiata:
Installare openvpn e le relative dipendenze:
opkg install openvpn-openssl openssl-util
Eseguire lo script di generazione delle chiavi:
#!/bin/sh
# Creating Directory Structure
VPN_DIR="/etc/openvpn"
PKI_DIR="$VPN_DIR/ssl"
if [ -d "$PKI_DIR" ]
then
rm -rf "$PKI_DIR"
fi
mkdir -p "$PKI_DIR"
chmod -R 600 "$PKI_DIR"
cd "$PKI_DIR"
touch index.txt index
echo 1000 > serial
cp -f /etc/ssl/openssl.cnf "$PKI_DIR"
# Customizing openssl.cnf
PKI_CONF="$PKI_DIR/openssl.cnf"
sed -i "
s:\\\\:/:g
/^dir/ s:=.*:= $PKI_DIR:
/^new_certs_dir/ s:=.*:= $PKI_DIR:
/.*Name/ s:= match:= optional:
/organizationName_default/ s:= .*:= WWW Ltd.:
/stateOrProvinceName_default/ s:= .*:= London:
/countryName_default/ s:= .*:= GB:
/default_days/ s:=.*:= 3650:
/default_bits/ s:=.*:= 4096:
" "$PKI_CONF"
cat << "EOF" >> "$PKI_CONF"
[ vpnserver ]
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
[ vpnclient ]
keyUsage = digitalSignature
extendedKeyUsage = clientAuth
EOF
# Generating Server PSK and CA, Server, & Client Certs
# Generating Certifcate Authority Cert & Key
openssl req -batch -nodes -new -keyout "ca.key" -out "ca.crt" -x509 -config "$PKI_CONF" -days "3650"
# Generating Server Cert & Key
openssl req -batch -nodes -new -keyout "my-server.key" -out "my-server.csr" -subj "/CN=my-server" -config "$PKI_CONF"
# Signing Server Cert
openssl ca -batch -keyfile "ca.key" -cert "ca.crt" -in "my-server.csr" -out "my-server.crt" -config "$PKI_CONF" -extensions "vpnserver"
# Generating Client Cert & Key
# PASSPHRASE MUST BE SET (4 chars minimum, 16+ chars recommended)
openssl req -batch -nodes -new -keyout "my-client.key" -out "my-client.csr" -subj "/CN=my-client" -config "$PKI_CONF"
# Signing Client Cert
openssl ca -batch -keyfile "ca.key" -cert "ca.crt" -in "my-client.csr" -out "my-client.crt" -config "$PKI_CONF" -extensions "vpnclient"
# Generating OpenVPN TLS PSK
openvpn --genkey --secret "tls-auth.key"
# Generating Diffie-Hellman Cert
# May take a while to complete (~25m on WRT3200ACM)
openssl dhparam -out "dh2048.pem" 2048
# Correcting Permissions
chmod 600 tls-auth.key dh2048.pem ca.key my-server.key my-client.key
# Copying Certs & Keys to $VPN_DIR
cp tls-auth.key dh2048.pem ca.crt my-server.* my-client.* "$VPN_DIR"
# Returning to initial working directory
cd -
# Done
Eseguire i seguenti comandi uci per impostare la configurazione di openvpn (personalizzate chiaramente ciò che ritenete opportuno):
uci set openvpn.vpnserver='openvpn'
uci set openvpn.vpnserver.enabled='1'
uci set openvpn.vpnserver.dev='tun'
uci set openvpn.vpnserver.proto='udp'
uci set openvpn.vpnserver.port='1194'
uci set openvpn.vpnserver.tls_server='1'
uci set openvpn.vpnserver.mode='server'
uci set openvpn.vpnserver.server='10.8.0.0 255.255.255.0'
uci set openvpn.vpnserver.comp_lzo='yes'
uci set openvpn.vpnserver.keepalive='10 120'
uci set openvpn.vpnserver.persist_key='1'
uci set openvpn.vpnserver.persist_tun='1'
uci set openvpn.vpnserver.cipher='aes-256-cbc'
uci set openvpn.vpnserver.auth='sha256'
uci set openvpn.vpnserver.ca='/etc/openvpn/ca.crt'
uci set openvpn.vpnserver.cert='/etc/openvpn/my-server.crt'
uci set openvpn.vpnserver.key='/etc/openvpn/my-server.key'
uci set openvpn.vpnserver.dh='/etc/openvpn/dh2048.pem'
uci set openvpn.vpnserver.tls_auth='/etc/openvpn/tls-auth.key 0'
uci set openvpn.vpnserver.log='/var/log/openvpn.log'
uci set openvpn.vpnserver.status='/var/log/openvpn-status.log'
uci set openvpn.vpnserver.verb=3
# uci add_list openvpn.vpnserver.push='redirect-gateway def1'
# uci add_list openvpn.vpnserver.push='route-gateway dhcp'
uci add_list openvpn.vpnserver.push='route 192.168.1.0 255.255.255.0'
uci add_list openvpn.vpnserver.push='dhcp-option DNS 208.67.222.222'
uci add_list openvpn.vpnserver.push='dhcp-option DNS 208.67.220.220'
uci commit openvpn
Eseguire openvpn:
/etc/init.d/openvpn enable
/etc/init.d/openvpn start
Creare sul proprio client OpenVPN il file di configurazione come segue:
client
dev tun
proto udp
remote my-server-name 1194
remote-cert-tls server
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert my-client.crt
key my-client.key
tls-auth tls-aut.key
comp-lzo
verb 3
key-direction 1
cipher aes-256-cbc
auth sha256
Nella stessa cartella copiare dal server i file ca.crt, my-client.crt, my-client.key e tls-auth.key
Se preferite avere la configurazione ovpn con le chiavi incorporate per gestire un file solo, usate il seguente script che concatena i file in un file unico.
#!/bin/sh
PKI_DIR="/etc/openvpn"
VPN_SERV="my-server-name"
# Configuration parameters
VPN_PORT="$(uci get openvpn.vpnserver.port)"
VPN_PROTO="$(uci get openvpn.vpnserver.proto)"
VPN_DEV="$(uci get openvpn.vpnserver.dev)"
TLS_KEY="$(sed -e "/^#/d;/^\w/N;s/\n//" "${PKI_DIR}/tls-auth.key")"
CA_CERT="$(openssl x509 -in "${PKI_DIR}/ca.crt")"
NL=$'\n'
VPNC_ID="my-client"
VPNC_CONF="${PKI_DIR}/${VPNC_ID}.ovpn"
VPNC_CONF_2="${PKI_DIR}/${VPNC_ID}_2.ovpn"
VPNC_CERT="$(openssl x509 -in "${PKI_DIR}/${VPNC_ID}.crt")"
VPNC_KEY="$(cat "${PKI_DIR}/${VPNC_ID}.key")"
cat << EOF > "${VPNC_CONF_2}"
client
dev ${VPN_DEV}
proto ${VPN_PROTO}
remote ${VPN_SERV} ${VPN_PORT}
remote-cert-tls server
resolv-retry infinite
nobind
persist-key
persist-tun
#ca ca.crt
#cert my-client.crt
#key my-client.key
#tls-auth tls-aut.key
comp-lzo
verb 3
key-direction 1
cipher aes-256-cbc
auth sha256
<ca>${NL}${CA_CERT}${NL}</ca>
<cert>${NL}${VPNC_CERT}${NL}</cert>
<key>${NL}${VPNC_KEY}${NL}</key>
<tls-auth>${NL}${TLS_KEY}${NL}</tls-auth>
EOF
Eseguire i seguenti comandi uci per impostare l'interfaccia di rete e le regole di firewalling:
uci set network.vpn0=interface
uci set network.vpn0.ifname=tun0
uci set network.vpn0.proto=none
uci set network.vpn0.auto=1
uci set firewall.Allow_OpenVPN_Inbound=rule
uci set firewall.Allow_OpenVPN_Inbound.target=ACCEPT
uci set firewall.Allow_OpenVPN_Inbound.src=*
uci set firewall.Allow_OpenVPN_Inbound.proto=udp
uci set firewall.Allow_OpenVPN_Inbound.dest_port=1194
uci set firewall.vpn=zone
uci set firewall.vpn.name=vpn
uci set firewall.vpn.network=vpn0
uci set firewall.vpn.input=ACCEPT
uci set firewall.vpn.forward=REJECT
uci set firewall.vpn.output=ACCEPT
uci set firewall.vpn.masq=1
uci set firewall.vpn_forwarding_lan_in=forwarding
uci set firewall.vpn_forwarding_lan_in.src=vpn
uci set firewall.vpn_forwarding_lan_in.dest=lan
uci set firewall.vpn_forwarding_lan_out=forwarding
uci set firewall.vpn_forwarding_lan_out.src=lan
uci set firewall.vpn_forwarding_lan_out.dest=vpn
uci commit network
/etc/init.d/network reload
uci commit firewall
/etc/init.d/firewall reload
A questo punto non resta che testare la connessione per verificare che funzioni.
Mi sono poi accorto che, una volta connesso, riuscivo ad accedere solo al router.
Quindi ho dovuto aggiungere una regola di firewalling per accedere anche a tutta la LAN 192.168.1.x.
Ecco la regola:
uci set firewall.vpn_snat_lan_out=redirect
uci set firewall.vpn_snat_lan_out.src=vpn
uci set firewall.vpn_snat_lan_out.dest=lan
uci set firewall.vpn_snat_lan_out.src_ip=10.8.0.0/24
uci set firewall.vpn_snat_lan_out.src_dip=192.168.1.1
uci set firewall.vpn_snat_lan_out.proto=all
uci set firewall.vpn_snat_lan_out.target=SNAT
uci commit firewall
/etc/init.d/firewall reload
- EDIT -
Se volete che anche il traffico internet del client passi per la vpn leggete qualche post più avanti:
https://www.ilpuntotecnico.com/forum/index.php/topic,77856.msg223384.html#msg223384- AGGIORNAMENTO 17/01/2018 -Inserita compatibilità AGTHP.
- AGGIORNAMENTO 13/07/2018 -Inserita nuova procedura di generazione delle chiavi.
- AGGIORNAMENTO 03/01/2019 -Modifica algoritmo e bug-fixing.
- AGGIORNAMENTO 25/04/2019 -Aggiornamento script di generazione dei certificati.