User Tools

Site Tools


doc:appunti:linux:sa:ipsec_strongswan

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
doc:appunti:linux:sa:ipsec_strongswan [2021/02/05 16:14] – [Verifica] niccolodoc:appunti:linux:sa:ipsec_strongswan [2023/11/14 11:27] – [File di configurazione] niccolo
Line 10: Line 10:
  
 In alternativa al pacchetto **strongswan** è possibile installare **[[#alternativa_charon-systemd|charon-systemd]]**, che offre alcuni vantaggi in termini di semplicità di integrazione con **systemd**, ma non utilizza il tradizionale file di configurazione **/etc/ipsec.conf** né i tradizionali processi **/usr/lib/ipsec/starter** e **/usr/lib/ipsec/charon**. In alternativa al pacchetto **strongswan** è possibile installare **[[#alternativa_charon-systemd|charon-systemd]]**, che offre alcuni vantaggi in termini di semplicità di integrazione con **systemd**, ma non utilizza il tradizionale file di configurazione **/etc/ipsec.conf** né i tradizionali processi **/usr/lib/ipsec/starter** e **/usr/lib/ipsec/charon**.
 +
 +===== File di configurazione =====
 +
 +Qesti gli indirizzi IP coinvolti:
 +
 +  * **Lato Left (host locale)**
 +    * IP pubblico: **132.82.168.98**
 +    * Classe IP privata: **172.17.48.96/29** (broadcast 172.17.48.103)
 +  * **Lato Right (host remoto)**
 +    * IP pubblico: **134.191.21.5**
 +    * Classe IP privata: **172.17.48.80/28** (broadcast 172.17.48.95)
 +
 +**/etc/ipsec.conf**
 +
 +<file>
 +config setup
 +        # strictcrlpolicy=yes
 +        # uniqueids = no
 +        charondebug="all"
 +        # More control on Charon debug. Default level is 1 "control",
 +        # level 2 is "controlmore".
 +        #charondebug="ike 2, knl 2, cfg 2, net 2, esp 2, dmn 2, mgr 2"
 +        uniqueids = yes
 +
 +include /etc/ipsec.d/office1-office2.conf
 +</file>
 +
 +**/etc/ipsec.secrets**
 +
 +<file>
 +include /etc/ipsec.d/office1-office2.secrets.inc
 +</file>
 +
 +
 +**/etc/ipsec.d/office1-office2.conf**
 +
 +<file>
 +conn office1-office2
 +        type=tunnel
 +        auto=start
 +        keyexchange=ikev2
 +        authby=secret
 +        left=132.82.168.98
 +        leftsubnet=172.17.48.97/29
 +        right=134.191.21.5
 +        rightsubnet=172.17.48.81/28
 +        ike=aes256-sha256-modp1536
 +        esp=aes256-sha256-modp1536
 +        aggressive=no                 
 +        keyingtries=%forever          
 +        ikelifetime=86400s            
 +        lifetime=28800s
 +        dpddelay=30s
 +        dpdtimeout=120s
 +        dpdaction=restart
 +        closeaction=restart
 +</file>
 +
 +L'opzione ''closeaction=restart'' dovrebbe servire a far ripartire la connessione nel caso in cui il remote invii un segnale DELETE, altrimenti si rischia che la connessione termini con questo log e non riparta più:
 +
 +<file>
 +charon: 07[IKE] received DELETE for IKE_SA office1-office2[5]
 +charon: 07[IKE] deleting IKE_SA office1-office2[5]
 +                between 132.82.168.98[213.182.68.98]...134.191.21.5[134.191.21.5]
 +ipsec[30830]: 07[IKE] received DELETE for IKE_SA office1-office2[5]
 +ipsec[30830]: 07[IKE] deleting IKE_SA office1-office2[5]
 +              between 132.82.168.98[213.182.68.98]...134.191.21.5[134.191.21.5]
 +</file>
 +
 +**/etc/ipsec.d/office1-office2.secrets.inc** (impostare **mode 0600**):
 +
 +<file>
 +# ------- Site 1 Gateway (office1-office2) -------
 +132.82.168.98 134.191.21.5 : PSK "de66979eaa77587d6b0e74d5bf871565"
 +
 +# ------- Site 2 Gateway (office2-office1) -------
 +134.191.21.5 132.82.168.98 : PSK "de66979eaa77587d6b0e74d5bf871565"
 +</file>
 +
 +===== Configurazione Shorewall =====
 +
 +**/etc/shorewall/rules**
 +
 +<file>
 +ACCEPT   net:134.191.21.5    $FW    esp
 +ACCEPT   net:134.191.21.5    $FW    udp     500
 +ACCEPT   net:134.191.21.5    $FW    udp     4500
 +</file>
 +
 +**ATTENZIONE**: In effetti la porta **4500/UDP** viene usata solo se il traffico IPsec deve attraversare qualche apparato che fa **NAT** e che non potrebbe trasportare il protocollo ESP (che non ha porte). In tal caso il traffico ESP viene incapsulato in pacchetti UDP con la porta 4500.
 +
 +**/etc/shorewall/tunnels**
 +
 +<file>
 +ipsec    net    134.191.21.5  # Remote IPSEC gateway
 +</file>
 +
 +**/etc/shorewall/zones**
 +
 +<file>
 +sec    ipv4
 +</file>
 +
 +**/etc/shorewall/hosts**
 +
 +<file>
 +sec    eth0:172.17.48.80/28,134.191.21.5 ipsec
 +</file>
  
 ===== Abilitare e avviare il servizio ===== ===== Abilitare e avviare il servizio =====
Line 22: Line 130:
  
 ===== Verifica ===== ===== Verifica =====
 +
 +==== Avvio della connessione ====
 +
 +Per avviare la VPN si può utilizzare il comando **ipsec start** (attenzione, perché questo comando avvia il demone fuori dal controllo di **systemd**).
 +
 +<code>
 +ipsec start
 +Starting strongSwan 5.7.2 IPsec [starter]...
 +</code>
 +
 +In **syslog** si trovano le seguenti righe:
 +
 +<code>
 +charon: 00[DMN] Starting IKE charon daemon (strongSwan 5.7.2, Linux 4.19.0-12-amd64, x86_64)
 +charon: 07[IKE] initiating IKE_SA office1-office2[1] to 134.191.21.5
 +charon: 07[NET] sending packet: from 132.82.168.98[500] to 134.191.21.5[500] (932 bytes)
 +charon: 09[NET] received packet: from 134.191.21.5[500] to 132.82.168.98[500] (360 bytes)
 +charon: 09[IKE] authentication of '132.82.168.98' (myself) with pre-shared key
 +charon: 09[IKE] establishing CHILD_SA office1-office2{1}
 +charon: 10[IKE] CHILD_SA office1-office2{1} established with SPIs cdd18e01_i ...
 +</code>
 +
 +In Debian 10, che utilizza systemd, è opportuno utilizzare **systemctl** invece di invocare direttamente **ipsec** (che supporta gli eventuali parametri ''stop'', ''restart'', ''status'', ''statusall''). Vedere sopra come [[#abilitare_e_avviare_il_servizio|abilitare e avviare il servizio]].
 +
 +==== Tracciato tcpdump ====
 +
 +Con tcpdump è possibile vedere l'inizio della connessione:
 +
 +<code>
 +18:10:58.636146 IP 134.191.21.5.500 > 132.82.168.98.500: isakmp: parent_sa ikev2_init[I]
 +18:10:58.650161 IP 132.82.168.98.500 > 134.191.21.5.500: isakmp: parent_sa ikev2_init[R]
 +18:10:58.713248 IP 134.191.21.5.4500 > 132.82.168.98.4500: NONESP-encap: isakmp: child_sa  ikev2_auth[I]
 +18:10:58.714463 IP 132.82.168.98.4500 > 134.191.21.5.4500: NONESP-encap: isakmp: child_sa  ikev2_auth[R]
 +18:10:59.773185 IP 134.191.21.5.500 > 132.82.168.98.500: isakmp: parent_sa ikev2_init[I]
 +18:10:59.786532 IP 132.82.168.98.500 > 134.191.21.5.500: isakmp: parent_sa ikev2_init[R]
 +18:10:59.851299 IP 134.191.21.5.4500 > 132.82.168.98.4500: NONESP-encap: isakmp: child_sa  ikev2_auth[I]
 +18:10:59.852578 IP 132.82.168.98.4500 > 134.191.21.5.4500: NONESP-encap: isakmp: child_sa  ikev2_auth[R]
 +</code>
 +==== Verifica stato della connessione ====
  
 Per verificare lo stato della VPN si utilizza il comando **ipsec statusall**, ecco un esempio di output per una connessione funzionante: Per verificare lo stato della VPN si utilizza il comando **ipsec statusall**, ecco un esempio di output per una connessione funzionante:
Line 39: Line 186:
 office1-office2:   local:  [132.82.168.98] uses pre-shared key authentication office1-office2:   local:  [132.82.168.98] uses pre-shared key authentication
 office1-office2:   remote: [134.191.21.5] uses pre-shared key authentication office1-office2:   remote: [134.191.21.5] uses pre-shared key authentication
-office1-office2:   child:  172.16.48.96/29 === 172.16.48.80/28 TUNNEL, dpdaction=restart+office1-office2:   child:  172.17.48.96/29 === 172.17.48.80/28 TUNNEL, dpdaction=restart
 Security Associations (1 up, 0 connecting): Security Associations (1 up, 0 connecting):
 office1-office2[1]: ESTABLISHED 31 seconds ago, 132.82.168.98[132.82.168.98]...134.191.21.5[134.191.21.5] office1-office2[1]: ESTABLISHED 31 seconds ago, 132.82.168.98[132.82.168.98]...134.191.21.5[134.191.21.5]
Line 52: Line 199:
 Se la VPN non è attiva, l'output è vuoto. Se la VPN non è attiva, l'output è vuoto.
  
-Per avviare la VPN si può utilizzare il comando **ipsec start** (attenzione, perché questo comando avvia il demone fuori dal controllo di **systemd**). 
  
-<code> +==== Connessione PSK fallita ====
-ipsec start +
-Starting strongSwan 5.7.2 IPsec [starter]... +
-</code>+
  
-In **syslog** si trovano le seguenti righe:+Ecco cosa appare in syslog quando una connessione fallisce per via della chiave condivisa PSK:
  
 <code> <code>
-charon: 00[DMNStarting IKE charon daemon (strongSwan 5.7.2, Linux 4.19.0-12-amd64, x86_64+charon: 13[NETreceived packet: from 134.191.21.5[500] to 132.82.168.98[500] (376 bytes
-charon: 07[IKE] initiating IKE_SA office1-office2[1] to 134.191.21.5 +charon: 13[IKE] 134.191.21.5 is initiating an IKE_SA 
-charon: 07[NETsending packetfrom 132.82.168.98[500to 134.191.21.5[500] (932 bytes) +charon: 13[CFGselected proposalIKE:AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_1536 
-charon: 09[NET] received packet: from 134.191.21.5[500to 132.82.168.98[500(360 bytes) +charon: 13[IKE] remote host is behind NAT 
-charon: 09[IKE] authentication of '132.82.168.98(myself) with pre-shared key +charon: 14[CFG] looking for peer configs matching 132.82.168.98[%any]...134.191.21.5[134.191.21.5] 
-charon: 09[IKEestablishing CHILD_SA office1-office2{1+charon: 14[CFGselected peer config 'office1-office2' 
-charon: 10[IKECHILD_SA office1-office2{1} established with SPIs cdd18e01_i ...+charon: 14[IKE] no shared key found for '%any' - '134.191.21.5
 +charon: 14[ENCgenerating IKE_AUTH response 1 [ N(AUTH_FAILED) ]
 </code> </code>
- 
-In Debian 10, che utilizza systemd, è opportuno utilizzare **systemctl** invece di invocare direttamente **ipsec** (che supporta gli eventuali parametri ''stop'', ''restart'', ''status'', ''statusall''). Vedere sopra come [[#abilitare_e_avviare_il_servizio|abilitare e avviare il servizio]]. 
  
 ===== Servizi systemd ===== ===== Servizi systemd =====
  
   * **strongswan.service** - Installato dal pacchetto ''strongswan-starter'', è responsabile dell'avvio del processo ''/usr/lib/ipsec/starter''.   * **strongswan.service** - Installato dal pacchetto ''strongswan-starter'', è responsabile dell'avvio del processo ''/usr/lib/ipsec/starter''.
-  * **ipsec.service** - Installato dal pacchetto ''strongswan-starter''FIXME Cosa avvia? È necessario? +  * **ipsec.service** - Installato dal pacchetto ''strongswan-starter''Questo servizio viene abilitato, disabilitato, avviato o fermato automaticamente quando si agisce su ''strongswan.service''.
  
 ===== Processi in esecuzione ===== ===== Processi in esecuzione =====
doc/appunti/linux/sa/ipsec_strongswan.txt · Last modified: 2024/05/06 16:02 by niccolo