AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1079879
Accepted
fiedl
fiedl
Asked: 2021-10-08 05:46:51 +0800 CST2021-10-08 05:46:51 +0800 CST 2021-10-08 05:46:51 +0800 CST

使用网关主机通过 IPSec 隧道路由流量

  • 772

考虑到strongswan wiki,这似乎是一个标准问题,但我无法让它正常工作。

网络布局

网络布局

本地站点 ( clientand gateway) 在我的控制之下,远程站点 ( remote gatewayand remote server) 不在。IPSec 隧道是一个拆分隧道,因此只有对10.10.0.0/16子网的请求通过 IPSec 隧道发送。

目标

我希望与client进行通信remote server,例如创建一个ssh或一个smb连接。

我已经做过的

  • gateway我已经在和之间建立了 IPSec 隧道remote gateway。

  • 我已在以下位置启用 ip 转发gateway:

    sysctl net.ipv4.ip_forward=1
    
  • 我在以下位置创建了一个 NAT gateway:

    iptables -t nat -I POSTROUTING -m policy --pol ipsec --dir out -j ACCEPT
    iptables -t nat -A POSTROUTING -j MASQUERADE
    
  • 在client我已经通过以下方式路由流量gateway:

    ip route del default
    ip route add default via 192.168.144.4
                           # 192.168.144.4 is the gateway
    

有什么作用

  • IPSec隧道已建立且稳定。
  • 登录后,我gateway可以成功。我也可以。我可以。pingremote gatewayremote serverpingclientping google.com
  • 登录后client,我可以,ping google.com我可以。随着on the我看到from the正在通过.pinggatewaytcpdump icmpgatewayping google.comclientgateway

什么不起作用,但

我不能ping从它remote server的clientIP 中获取。

client$ ping -c 1 10.10.12.7
PING 10.10.12.7 (10.10.12.7): 56 data bytes

--- 10.10.12.7 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

从tcpdump开始gateway,看起来ping是发送的,但不是通过隧道转发的:

gateway$ tcpdump icmp
13:19:18.122999 IP 192.168.144.7 > 10.10.12.7: ICMP echo request, id 15, seq 0, length 64
13:19:18.123038 IP gateway > 10.10.12.7: ICMP echo request, id 15, seq 0, length 64
13:19:18.127534 IP ac5.nue3.m-online.net > gateway: ICMP net 10.10.12.7 unreachable, length 36
13:19:18.127556 IP ac5.nue3.m-online.net > 192.168.144.7: ICMP net 10.10.12.7 unreachable, length 36

与ac5.nue3.m-online.net本地站点的互联网服务提供商一样,我认为数据包不是通过 IPSec 隧道路由的,而是gateway通过remote server. (如果我将 IPSec 隧道设为完整隧道而不是拆分隧道,我会得到相同的结果。)

任何帮助或见解将不胜感激!

编辑:ipsec statusall在gateway

gateway > ipsec statusall
Status of IKE charon daemon (strongSwan 5.8.2, Linux 5.4.0-88-generic, x86_64):
  uptime: 7 minutes, since Oct 08 08:18:24 2021
  malloc: sbrk 3112960, mmap 0, used 1081456, free 2031504
  worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 4
  loaded plugins: charon test-vectors ldap pkcs11 tpm aesni aes rc2 sha2 sha1 md5 mgf1 rdrand random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl gcrypt af-alg fips-prf gmp curve25519 agent chapoly xcbc cmac hmac ctr ccm gcm ntru drbg curl attr kernel-netlink resolve socket-default connmark farp stroke updown eap-identity eap-aka eap-md5 eap-gtc eap-mschapv2 eap-dynamic eap-radius eap-tls eap-ttls eap-peap eap-tnc xauth-generic xauth-eap xauth-pam tnc-tnccs dhcp lookip error-notify certexpire led addrblock unity counters
Listening IP addresses:
  192.168.144.4
Connections:
example-ipsec:  %any...vpn1.example.com  IKEv2, dpddelay=300s
example-ipsec:   local:  [[email protected]] uses pre-shared key authentication
example-ipsec:   remote: [[email protected]] uses pre-shared key authentication
example-ipsec:   child:  dynamic === 0.0.0.0/0 TUNNEL, dpdaction=clear
Security Associations (1 up, 0 connecting):
example-ipsec[1]: ESTABLISHED 7 minutes ago, 192.168.144.4[[email protected]]...<public-ip-of-the-remote-gateway>[[email protected]]
example-ipsec[1]: I: 9d7c74f670bbda86_i* c12b3b4a236b7018_r, pre-shared key reauthentication in 2 hours
example-ipsec[1]: IKE proposal: AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048
example-ipsec{1}:  INSTALLED, TUNNEL, reqid 1, ESP in UDP SPIs: cf66ad72_i af3c9348_o
example-ipsec{1}:  AES_CBC_256/HMAC_SHA2_256_128, 442 bytes_i (4 pkts, 434s ago), 485 bytes_o (6 pkts, 433s ago), rekeying in 38 minutes
example-ipsec{1}:   10.10.102.235/32 === 0.0.0.0/0

这是在ipsec statusall将fromgateway发送到. from不会更改输出中的“字节” 。输出中的“字节”对应于我从 发送到.pingclientremote serverpinggatewaypinggatewayremote server

编辑:/etc/ipsec.conf在gateway:

# /etc/ipsec.conf

conn example-ipsec
  left = %defaultroute
  leftsourceip = %config
  leftid = "[email protected]"
  right = vpn1.example.com
  rightid = "[email protected]"
  rightsubnet = 0.0.0.0/0
  leftfirewall = yes
  installpolicy = yes
  keyexchange = ikev2
  type = tunnel
  auto = start
  leftauth = psk
  rightauth = psk
  dpdaction = clear
  dpddelay = 300s
iptables ipsec ip-forwarding
  • 1 1 个回答
  • 316 Views

1 个回答

  • Voted
  1. Best Answer
    ecdsa
    2021-10-11T23:49:46+08:002021-10-11T23:49:46+08:00

    由于连接使用虚拟 IP 地址(leftsourceip=%config导致10.10.102.235/32作为本地流量选择器),您必须将流量 NAT 到该地址,而不是您通过的主机的物理地址,MASQUERADE以便匹配 IPsec 策略和隧道流量(-I到将其插入顶部):

    iptables -t nat -I POSTROUTING  -j SNAT --to-source 10.10.102.235
    

    如果虚拟 IP 不是静态分配的(例如基于客户端的身份)并且可能会更改,您可以在虚拟 IP 传入的自定义 updown 脚本(通过配置)中动态安装/删除 SNAT 规则。leftupdown$PLUTO_MY_SOURCEIP

    正如您最初所说,这是一个拆分隧道设置(远程流量选择器0.0.0.0/0实际上并没有反映),您还可以添加例如-d 10.10.0.0/16到 SNAT 规则以仅处理到该子网的数据包,其他流量不会被 natted和隧道(您可以保留该MASQUERADE流量的规则)。这也可以通过 IPsec 策略 ( rightsubnet=10.10.0.0/16) 强制执行,然后您可以在$PLUTO_PEER_CLIENTupdown 脚本中使用该策略。

    • 2

相关问题

  • OpenVPN 的 Linux IP 转发 - 正确的防火墙设置?

  • iptables 单个规则中的多个源 IP

  • 存储 iptables 规则的规范方法是什么

  • 使用 iptables 和 dhcpd 进行端口转发

  • 根据 Apache 日志数据自动修改 iptables 以阻止行为不良的客户端

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve