Linux-Mint

networkmanager-strongswan vpn - 通過 VPN 路由特定 IP

  • March 23, 2020

我正在嘗試在 Linux Mint 19.2 上設置 VPN。

我正在使用network-manager-strongswan所以我在下面添加了這個名為VPN的文件/etc/NetworkManager/system-connections/

[connection]
id=VPN
uuid=be1d4fd1-bbaa-4aa9-9fdc-e293bf16fe67
type=vpn
autoconnect=false
permissions=
timestamp=1582680217

[vpn]
address=vpn********.it
certificate=
encap=yes
ipcomp=no
method=eap
password-flags=0
proposal=no
user=user
virtual=yes
service-type=org.freedesktop.NetworkManager.strongswan

[vpn-secrets]
password=password

[ipv4]
dns-search=
ignore-auto-dns=true
ignore-auto-routes=true
method=auto

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
ip6-privacy=0
method=ignore

連接很好,我可以在專用網路上使用 ssh 訪問。最大的問題是,連接後我無法上網,連接到 vpn 鎖定所有其他地址。

我在配置中添加了ignore-auto-routes標誌,為什麼我的連接被鎖定?


ip輸出*

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
   inet6 ::1/128 scope host 
      valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
   link/ether 00:0c:29:3c:de:1b brd ff:ff:ff:ff:ff:ff
   inet yy.16.209.132/24 brd yy.16.209.255 scope global dynamic noprefixroute ens33
      valid_lft 1656sec preferred_lft 1656sec
   inet yy.26.199.18/32 scope global ens33
      valid_lft forever preferred_lft forever
   inet6 fe80::216e:bcc0:3b4f:44b2/64 scope link noprefixroute 
      valid_lft forever preferred_lft forever

路由 -n輸出

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         yy.16.209.2    0.0.0.0         UG    20100  0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33
yy.16.209.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
yy.26.199.18   0.0.0.0         255.255.255.255 UH    50     0        0 ens33
yy.26.199.18   0.0.0.0         255.255.255.255 UH    100    0        0 ens33

ip xfrm 策略輸出NO VPN

src yy.16.209.0/24 dst yy.16.209.0/24 
   dir fwd priority 175423 
src yy.16.209.0/24 dst yy.16.209.0/24 
   dir in priority 175423 
src yy.16.209.0/24 dst yy.16.209.0/24 
   dir out priority 175423 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir fwd priority 183615 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir in priority 183615 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir out priority 183615 
src fe80::/64 dst fe80::/64 
   dir fwd priority 134463 
src fe80::/64 dst fe80::/64 
   dir in priority 134463 
src fe80::/64 dst fe80::/64 
   dir out priority 134463 
src yy.26.199.18/32 dst 0.0.0.0/0 
   dir out priority 383615 
   tmpl src yy.16.209.132 dst xx.xx.124.58
       proto esp spi 0xc57cfb3f reqid 7 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir fwd priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 7 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir in priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 7 mode tunnel
src ::1/128 dst ::1/128 
   dir fwd priority 68927 
src ::1/128 dst ::1/128 
   dir in priority 68927 
src ::1/128 dst ::1/128 
   dir out priority 68927 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0

ip xfrm 策略輸出UNDER VPN

src yy.26.199.18/32 dst 0.0.0.0/0 
   dir out priority 383615 
   tmpl src yy.16.209.132 dst xx.xx.124.58
       proto esp spi 0xc787ea42 reqid 2 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir fwd priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 2 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir in priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 2 mode tunnel
src fe80::/64 dst fe80::/64 
   dir fwd priority 134463 
src fe80::/64 dst fe80::/64 
   dir in priority 134463 
src fe80::/64 dst fe80::/64 
   dir out priority 134463 
src ::1/128 dst ::1/128 
   dir fwd priority 68927 
src ::1/128 dst ::1/128 
   dir in priority 68927 
src ::1/128 dst ::1/128 
   dir out priority 68927 
src yy.16.209.0/24 dst yy.16.209.0/24 
   dir fwd priority 175423 
src yy.16.209.0/24 dst yy.16.209.0/24 
   dir in priority 175423 
src yy.16.209.0/24 dst yy.16.209.0/24 
   dir out priority 175423 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir fwd priority 183615 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir in priority 183615 
src 169.254.0.0/16 dst 169.254.0.0/16 
   dir out priority 183615 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket in priority 0 
src 0.0.0.0/0 dst 0.0.0.0/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0 
src ::/0 dst ::/0 
   socket in priority 0 
src ::/0 dst ::/0 
   socket out priority 0

比較輸出我可以看到在連接 VPN 時添加了這一部分:

src yy.26.199.18/32 dst 0.0.0.0/0 
   dir out priority 383615 
   tmpl src yy.16.209.132 dst xx.xx.124.58
       proto esp spi 0xc787ea42 reqid 2 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir fwd priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 2 mode tunnel
src 0.0.0.0/0 dst yy.26.199.18/32 
   dir in priority 383615 
   tmpl src xx.xx.124.58 dst yy.16.209.132
       proto esp reqid 2 mode tunnel

我嘗試了很多東西,但沒有運氣。

  • 試圖將Metricyy.26.199.18 的值增加到 1050 和 1100。
  • route del default&&ip route add default via yy.26.199.18 dev ens33
  • 試圖刪除路由條目 yy.26.199.18 但沒有任何變化

還有很多其他愚蠢的東西。

所以我想在通過 VPN 路由特定地址時將我的連接用於“普通網際網路”。

有可能的?

Strongswan使用策略路由,通過xfrm策略配置:

  1. dir out策略說明如何處理來自172.26.199.18/32to的數據包0.0.0.0/0:它們需要加密並通過 VPN 隧道發送。
  2. 和策略說明當加密數據包進入時要做什麼。如果在解密後,來自 to 的數據包dir in不是來自指定的隧道,則將其丟棄。明文數據包不受此限制。dir fwd``0.0.0.0/0``172.26.199.18/32

為了限制 VPN 網路的使用,您有三種解決方案:

  1. 這些規則中出現的網路是在您的 IKE 伺服器和遠端伺服器之間協商的。your_ip/32NetworkManager 外掛總是建議在和之間建立一個隧道0.0.0.0/0。如果遠端伺服器建議0.0.0.0/0並且10.0.0.0/8生成的連接將是: between your_ip/32and 10.0.0.0/8。因此,您需要請遠端端點的管理員正確配置他的 IKE 守護程序。
  2. 您可以放棄使用 NetworkManager 作為 VPN 隧道,安裝charon-systemd並創建一個簡單的配置文件(例如/etc/swanctl/conf.d/my_vpn.conf):
connections {
   my_vpn {
       version = 2
       local_addrs=%any
       remote_addrs=vpn********.it
       local {
           auth = eap
           eap_id = your_username
       }
       remote {
           auth = pubkey
           id = server_name
       }
       children {
           my_tunnel {
               #local_ts = dynamic
               remote_ts = remote_network
           }
       }
   }
}

重新載入守護程序 ( systemctl reload strongswan-swanctl) 並執行:

swanctl --initiate --ike my_vpn --child my_tunnel
  1. 添加您自己的策略,其priority值小於自動安裝的值,這將允許未加密的流量,例如:
ip xfrm policy add src your_ip/32 dst 192.0.2.0/24 \
   dir out priority 1000
ip xfrm policy add src 192.0.2.0/24 dst your_ip/32 \
   dir in priority 1000

這將允許明文流量到192.0.2.0/24.

引用自:https://unix.stackexchange.com/questions/571602