一、组网需求

如图所示,总部防火墙为静态固定IP地址,分支防火墙为PPPOE/DHCP动态拨号获取的IP地址,需要通过IPsec VPN(接口模式)将总部和分支2个局域网连接起来,实现192.168.0.0/24与192.168.1.0/24两个网段的通信。

二、网络拓扑

       

三、配置要点

1、配置FortiGate1和FortiGate2的FortiGuard DDNS

    1)为FortiGate1配置FortiGuard DDNS(可选,FortiGate1为固定IP,是否DDNS无所谓)
    2)为FortiGate2配置FortiGuard DDNS(要使用静态IPsec VPN,必须配置DDNS,并且拥有一个可用公网IP,而非运营商给分配的私网地址)

2、配置FortiGate1

    1)基本上网配置
    2)根据模板配置IPsec VPN(默认即为IPSEC接口模式)
    3)针对模板配置的优化建议
    4)IPsec VPN模板里面具体配置了什么?

3、配置FortiGate2

    1)基本上网配置
    2)根据模板配置IPsec VPN(默认即为IPSEC接口模式)
    3)针对模板配置的优化建议
    4)IPsec VPN模板里面具体配置了什么?

说明:如果要删除IPSEC VPN第一阶段、第二阶段时,需要先删除被调用的路由与防火墙安全策略。

四、配置步骤

1、配置FortiGate1和FortiGate2的FortiGuard DDNS

1)为FortiGate1配置FortiGuard DDNS(可选,FortiGate1为固定IP,是否DDNS无所谓)

2)为FortiGate2配置FortiGuard DDNS(要使用静态IPsec VPN,必须配置DDNS,并且拥有一个可用公网IP,而非运营商给分配的私网地址)

2、配置FortiGate1 (BJ)

1) 基本上网配置

配置详细过程请参照 "路由模式典型功能--单线上网--静态地址线路上网配置"一节:

接口IP配置如下:

路由配置如下

2) 根据模板配置IPsec VPN(默认即为IPSEC接口模式)

进入:虚拟专网--IPSEC隧道--"新建"

可以选择根据向导进行相应的IPsec VPN模板进行配置:

 

说明:如果最后完成模板部署的时候报错,那么需要注意,不要有旧的配置存在,比如相关VPN的策略、VPN的路由、VPN的IP地址对象,需要把这些内容删除干净才可以顺利的部署向导模板,一般第一次部署不会遇到报错。

3) 针对模板配置的优化建议
优化建议一:在第一阶段中开启DPD周期性检测(每隔10s检测一次Peer状态),实现快速的检测并切换VPN隧道的目的。

相关命令行:
      config vpn ipsec phase1-interface
    edit "VPN-TO-SH"
        set dpd on-idle
    next
      end
        
优化建议二:开启自动协商,主动让隧道UP起来,而非使用VPN业务的时候再去触发VPN的协商,这样可以减少业务的丢包,同时可快速的感知VPN隧道的状态。

config vpn ipsec phase2-interface
  edit "VPN-TO-SH"
   set auto-negotiate enable
  next
end

----FortiGate1(BJ)的配置全部完成----

4)IPsec VPN模板里面具体配置了什么

4.1) IPsec VPN的配置:


第一阶段配置:
FortiGate1_BeiJing # config vpn ipsec phase1-interface
FortiGate1_BeiJing (phase1-interface) # show
config vpn ipsec phase1-interface
    edit "VPN-TO-SH"
        set type ddns
        set interface "wan1"
        set peertype any
        set proposal aes128-sha256 aes256-sha256 aes128-sha1 aes256-sha1
        set dpd on-idle
        set comments "VPN: VPN-TO-SH (Created by VPN wizard)"
        set remotegw-ddns "side1-sh.fortiddns.com"
        set psksecret Fortinet123#
    next
end
FortiGate1_BeiJing (phase1-interface) # show full-configuration // 可看到所有默认参数和配置
第二阶段配置:
FortiGate1_BeiJing # config vpn ipsec phase2-interface
FortiGate1_BeiJing (phase2-interface) #
FortiGate1_BeiJing (phase2-interface) # show
config vpn ipsec phase2-interface
    edit "VPN-TO-SH"
        set phase1name "VPN-TO-SH"
        set proposal aes128-sha1 aes256-sha1 aes128-sha256 aes256-sha256 aes128gcm aes256gcm chacha20poly1305
        set comments "VPN: VPN-TO-SH (Created by VPN wizard)"
        set src-addr-type name
        set dst-addr-type name
        set src-name "VPN-TO-SH_local"
        set dst-name "VPN-TO-SH_remote"
    next
end
FortiGate1_BeiJing (phase2-interface) # show full-configuration // 可看到所有默认参数和配置
IP地址对象和对象组:
config firewall address
    edit "VPN-TO-SH_local_subnet_1"
        set allow-routing enable
        set subnet 192.168.0.0 255.255.255.0
    next
    edit "VPN-TO-SH_remote_subnet_1"
        set allow-routing enable
        set subnet 192.168.1.0 255.255.255.0
    next
end
config firewall addrgrp
    edit "VPN-TO-SH_local"
        set member "VPN-TO-SH_local_subnet_1"
        set comment "VPN: VPN-TO-SH (Created by VPN wizard)"
        set allow-routing enable
    next
    edit "VPN-TO-SH_remote"
        set member "VPN-TO-SH_remote_subnet_1"
        set comment "VPN: VPN-TO-SH (Created by VPN wizard)"
        set allow-routing enable
    next
end

4.2) IPsec VPN策略的配置:

 配置 LAN内网接口道VPN隧道接口的策略 和 VPN隧道接口到内网LAN接口的策略,在策略的配置中“VPN-TO-SH”就像是一个普通接口一样调用即可:


config firewall policy
    edit 0
        set name "vpn_VPN-TO-SH_local"
        set srcintf "lan"
        set dstintf "VPN-TO-SH"
        set srcaddr "VPN-TO-SH_local"
        set dstaddr "VPN-TO-SH_remote"
        set action accept
        set schedule "always"
        set service "ALL"
        set comments "VPN: VPN-TO-SH (Created by VPN wizard)"
    next
    edit 0
        set name "vpn_VPN-TO-SH_remote"
        set srcintf "VPN-TO-SH"
        set dstintf "lan"
        set srcaddr "VPN-TO-SH_remote"
        set dstaddr "VPN-TO-SH_local"
        set action accept
        set schedule "always"
        set service "ALL"
        set comments "VPN: VPN-TO-SH (Created by VPN wizard)"
    next
end

4.3) IPsec VPN路由的配置:配置 到对方业务网段的路由指向VPN隧道接口“VPN-TO-SH”,在路由的配置中“VPN-TO-SH”就像是一个普通接口一样调用即可

config router static
    edit 0
        set device "VPN-TO-SH"
        set comment "VPN: VPN-TO-SH (Created by VPN wizard)"
        set dstaddr "VPN-TO-SH_remote"
    next
    edit 0
        set distance 254    // 管理距离254
        set comment "VPN: VPN-TO-SH (Created by VPN wizard)"
        set blackhole enable
        set dstaddr "VPN-TO-SH_remote"
    next
end

说明:相关VPN的路由配置中“黑洞路由的意义”:

你可能会遇到以下的VPN业务问题:通过VPN的SIP电话,时不时中断,无法向服务器成功注册 ?通过VPN的Radius认证无法经常会无法认证成功? 通过VPN的AP注册到总部时不时会中断?持续的PRGT监控ping总部的服务器,时不时会显示中断? 
VPN有时候会因为各种原因重新连接,比如互联网不稳定,PPPOE重新连接更换公网IP地址等,VPN tunnel此时会出现短暂的DOWN,而去往对方的VPN业务网段的路由也会短暂消失,此时VPN的业务流量(SIP注册请求/Radius/Capwap/ICMP)会因为查询到了默认路由而走向了  WAN1(Internet),从而产生了错误的UDP-NAT-Seesion,此时即便VPN tunnel再次UP,VPN业务网段的路由再次恢复,SIP等VPN旧的业务流量依旧会走到错误的Session上去,从而引起该业务异常。 
排查思路:通过diagnose sniffer pa “port 5060”4 以及查看session (diagnose sys session list)去判断问题。 
解决此类问题的办法有三种:
办法一:配置去往VPN业务网段的“黑洞路由”,管理距离为254,VPN正常的情况下此黑洞路由不生效,而当VPN中断的时候,黑洞路由浮起来并且生效,将VPN中断时候的VPN流量丢弃,避免将流量转发互联网产生错误的session。
办法二:配置源接口:LAN,目的接口:WAN1,源IP:本地内网网段,目的IP:对端内网网段,动作:丢包的策略。将此去往Internet的私网(无用的)流量丢弃掉,避免FGT产生这种错误的session,从而避免了UDP业务时不时中断的问题。
办法三:在全局下开启“set snat-route-change enable”,一旦路由发生变化,将相关的会话flag置位为“Dirty”,清除路由缓存并重新查找目的IP的下一跳地址,这样VPN隧道恢复的时候,流量也会相应的切换到VPN隧道里面来。
FGT # config system global
FGT (global) # set snat-route-change enable
FGT (global) # end

3、配置FortiGate2 (SH)
1) 基本上网配置
接口IP配置如下(WAN1采用DHCP/PPPOE获取到200.1.1.2的IP,网关为200.1.1.254):


2) 根据模板配置IPsec VPN(默认即为IPSEC接口模式)

进入:虚拟专网--IPSEC隧道--"新建"

可以选择根据向导进行相应的IPsec VPN模板进行配置(由于总部是固定静态IP,因此远端设备选择IP地址100.1.1.2即可,当然也可以配置成步骤1中配置的FortiGuard DDNS域名:center-bj.fortiddns.com,此处选择配置静态固定IP):

说明:如果最后完成模板部署的时候报错,那么需要注意,不要有旧的配置存在,比如相关VPN的策略、VPN的路由、VPN的IP地址对象,需要把这些内容删除干净才可以顺利的部署向导模板,一般第一次部署不会遇到报错。

3) 针对模板配置的优化建议
优化建议一:在第一阶段中开启DPD周期性检测(每隔10s检测一次Peer状态),实现快速的检测并切换VPN隧道的目的。

相关命令行:
config vpn ipsec phase1-interface
    edit "VPN-TO-BJ"
        set dpd on-idle
    next
end

优化建议二:开启自动协商,主动让隧道UP起来,而非使用VPN业务的时候再去触发VPN的协商,这样可以减少业务的丢包,同时可快速的感知VPN隧道的状态。


config vpn ipsec phase2-interface
    edit "VPN-TO-BJ"
        set auto-negotiate enable
    next
end
----FortiGate2(SH)的配置全部完成----

4)IPsec VPN模板里面具体配置了什么
4.1) IPsec VPN的配置:


第一阶段配置:
FGT60D4614022596 # config vpn ipsec phase1-interface
FGT60D4614022596 (phase1-interface) # show
config vpn ipsec phase1-interface
    edit "VPN-TO-BJ"
        set interface "wan1"
        set peertype any
        set proposal aes128-sha256 aes256-sha256 aes128-sha1 aes256-sha1
        set comments "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set wizard-type static-fortigate
        set remote-gw 100.1.1.2
        set psksecret Fortinet123#
    next
end
FGT60D4614022596 (phase1-interface) # show full-configuration # show full-configuration // 可看到所有默认参数和配置

第二阶段配置:
FGT60D4614022596 # config vpn ipsec phase2-interface
FGT60D4614022596 (phase2-interface) # show
config vpn ipsec phase2-interface
    edit "VPN-TO-BJ"
        set phase1name "VPN-TO-BJ"
        set proposal aes128-sha1 aes256-sha1 aes128-sha256 aes256-sha256
        set comments "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set src-addr-type name
        set dst-addr-type name
        set src-name "VPN-TO-BJ_local"
        set dst-name "VPN-TO-BJ_remote"
    next
end
FGT60D4614022596 (phase2-interface) #  show full-configuration // 可看到所有默认参数和配置

IP地址对象和对象组:
config firewall address
    edit "VPN-TO-BJ_local_subnet_1"
        set allow-routing enable
        set subnet 192.168.1.0 255.255.255.0
    next
    edit "VPN-TO-BJ_remote_subnet_1"
        set allow-routing enable
        set subnet 192.168.0.0 255.255.255.0
    next 
end
config firewall addrgrp
    edit "VPN-TO-BJ_local"
        set member "VPN-TO-BJ_local_subnet_1"
        set comment "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set allow-routing enable
    next
    edit "VPN-TO-BJ_remote"
        set member "VPN-TO-BJ_remote_subnet_1"
        set comment "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set allow-routing enable
    next
end


4.2) IPsec VPN策略的配置:
配置 LAN内网接口道VPN隧道接口的策略 和 VPN隧道接口到内网LAN接口的策略,在策略的配置中“VPN-TO-BJ”就像是一个普通接口一样调用即可:


config firewall policy
    edit 0
        set name "vpn_VPN-TO-BJ_local"
        set srcintf "internal"
        set dstintf "VPN-TO-BJ"
        set srcaddr "VPN-TO-BJ_local"
        set dstaddr "VPN-TO-BJ_remote"
        set action accept
        set schedule "always"
        set service "ALL"
        set comments "VPN: VPN-TO-BJ (Created by VPN wizard)"
    next
    edit 0
        set name "vpn_VPN-TO-BJ_remote"
        set srcintf "VPN-TO-BJ"
        set dstintf "internal"
        set srcaddr "VPN-TO-BJ_remote"
        set dstaddr "VPN-TO-BJ_local"
        set action accept
        set schedule "always"
        set service "ALL"
        set comments "VPN: VPN-TO-BJ (Created by VPN wizard)"
    next
end


4.3) IPsec VPN路由的配置:
配置 到对方业务网段的路由指向VPN隧道接口“VPN-TO-SHA”,在路由的配置中“VPN-TO-BJ”就像是一个普通接口一样调用即可


config router static
    edit 0
        set device "VPN-TO-BJ"
        set comment "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set dstaddr "VPN-TO-BJ_remote"
    next
    edit 0
        set distance 254
        set comment "VPN: VPN-TO-BJ (Created by VPN wizard)"
        set blackhole enable
        set dstaddr "VPN-TO-BJ_remote"
    next
end

说明:
相关VPN的路由配置中“黑洞路由的意义”:

你可能会遇到以下的VPN业务问题:通过VPN的SIP电话,时不时中断,无法向服务器成功注册 ?通过VPN的Radius认证无法经常会无法认证成功? 通过VPN的AP注册到总部时不时会中断?持续的PRGT监控ping总部的服务器,时不时会显示中断?

VPN有时候会因为各种原因重新连接,比如互联网不稳定,PPPOE重新连接更换公网IP地址等,VPN tunnel此时会出现短暂的DOWN,而去往对方的VPN业务网段的路由也会短暂消失,此时VPN的业务流量(SIP注册请求/Radius/Capwap/ICMP)会因为查询到了默认路由而走向了WAN1(Internet),从而产生了错误的UDP-NAT-Seesion,此时即便VPN tunnel再次UP,VPN业务网段的路由再次恢复,SIP等VPN旧的业务流量依旧会走到错误的Session上去,从而引起该业务异常。

排查思路:通过diagnose sniffer pa “port 5060”4 以及查看session (diagnose sys session list)去判断问题。

解决此类问题的办法有三种:
办法一:配置去往VPN业务网段的“黑洞路由”,管理距离为254,VPN正常的情况下此黑洞路由不生效,而当VPN中断的时候,黑洞路由浮起来并且生效,将VPN中断时候的VPN流量丢弃,避免将流量转发互联网产生错误的session。
办法二:配置源接口:LAN,目的接口:WAN1,源IP:本地内网网段,目的IP:对端内网网段,动作:丢包的策略。将此去往Internet的私网(无用的)流量丢弃掉,避免FGT产生这种错误的session,从而避免了UDP业务时不时中断的问题。
办法三:在全局下开启“set snat-route-change enable”,一旦路由发生变化,将相关的会话flag置位为“Dirty”,清除路由缓存并重新查找目的IP的下一跳地址,这样VPN隧道恢复的时候,流量也会相应的切换到VPN隧道里面来。
FGT # config system global
FGT (global) # set snat-route-change enable
FGT (global) # end
       

五、检查配置结果
VPN业务测试:
查看VPN监视器,观察状态: 进入"监视器"--"IPsec监测"   
   

    
业务测试:FortiGate1(BJ)到FortiGate2(SH):
两个网络采用 ping测试方式,192.168.1.0/24和192.168.0.0/24两个内网可以相互访问。

FortiGate2(SH)到FortiGate1(BJ):


   
# diagnose sniffer packet any "host 192.168.1.111 and icmp" 4
interfaces=[any]
filters=[host 192.168.1.111 and icmp]
3.572632 lan in 192.168.0.111 -> 192.168.1.111: icmp: echo request
3.572702 VPN-TO-SH out 192.168.0.111 -> 192.168.1.111: icmp: echo request
3.573386 VPN-TO-SH in 192.168.1.111 -> 192.168.0.111: icmp: echo reply
3.573418 lan out 192.168.1.111 -> 192.168.0.111: icmp: echo reply

4.576875 lan in 192.168.0.111 -> 192.168.1.111: icmp: echo request
4.576940 VPN-TO-SH out 192.168.0.111 -> 192.168.1.111: icmp: echo request
4.577643 VPN-TO-SH in 192.168.1.111 -> 192.168.0.111: icmp: echo reply
4.577674 lan out 192.168.1.111 -> 192.168.0.111: icmp: echo reply

5.590891 lan in 192.168.0.111 -> 192.168.1.111: icmp: echo request
5.590934 VPN-TO-SH out 192.168.0.111 -> 192.168.1.111: icmp: echo request
5.591673 VPN-TO-SH in 192.168.1.111 -> 192.168.0.111: icmp: echo reply
5.591700 lan out 192.168.1.111 -> 192.168.0.111: icmp: echo reply

6.604824 lan in 192.168.0.111 -> 192.168.1.111: icmp: echo request
6.604869 VPN-TO-SH out 192.168.0.111 -> 192.168.1.111: icmp: echo request
6.605548 VPN-TO-SH in 192.168.1.111 -> 192.168.0.111: icmp: echo reply
6.605576 lan out 192.168.1.111 -> 192.168.0.111: icmp: echo reply

# diagnose sniffer packet any "host 200.1.1.2" 4
interfaces=[any]
filters=[host 200.1.1.2]
3.282015 wan1 out 100.1.1.2 -> 200.1.1.2: ESP(spi=0xd7b56919,seq=0xa)
3.283160 wan1 in 200.1.1.2 -> 100.1.1.2: ESP(spi=0xd9f36896,seq=0xa)

4.285467 wan1 out 100.1.1.2 -> 200.1.1.2: ESP(spi=0xd7b56919,seq=0xb)
4.286371 wan1 in 200.1.1.2 -> 100.1.1.2: ESP(spi=0xd9f36896,seq=0xb)

5.299488 wan1 out 100.1.1.2 -> 200.1.1.2: ESP(spi=0xd7b56919,seq=0xc)
5.300167 wan1 in 200.1.1.2 -> 100.1.1.2: ESP(spi=0xd9f36896,seq=0xc)

6.313449 wan1 out 100.1.1.2 -> 200.1.1.2: ESP(spi=0xd7b56919,seq=0xd)
6.314078 wan1 in 200.1.1.2 -> 100.1.1.2: ESP(spi=0xd9f36896,seq=0xd)

设备相关VPN状态的查看,仅仅在FortiGate1(BJ)上查看:
FG100E4Q16003872 # get router  info routing-table all

Routing table for VRF=0
Codes: K - kernel, C - connected, S - static, R - RIP, B - BGP
       O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
       * - candidate default

S*      0.0.0.0/0 [10/0] via 100.1.1.254, wan1
C       100.1.1.0/24 is directly connected, wan1
C       192.168.0.0/24 is directly connected, lan
S       192.168.1.0/24 [10/0] is directly connected, VPN-TO-SH

说明:关于sniffer抓VPN业务和ESP的包
抓取IPsec VPN的IKE协商包:
diagnose sniffer packet any "host 202.1.1.2 and (port 500 or port 4500)" 4

抓取IPsec VPN的ESP加密数据包:
diagnose sniffer packet any "host 200.1.1.2 and esp" 4

抓取IPsec VPN的明文业务数据包:
diagnose sniffer packet any "host 192.168.1.111 and icmp" 4

注意:由于存在IPsec VPN芯片加速,因此可能数据包会抓不完全,主要指“ESP数据和明文业务数据”抓不全,因此有时候需要将VPN隧道的NP加速关闭:
FortiGate1_BeiJing # config vpn ipsec phase1-interface        
FortiGate1_BeiJing (phase1-interface) # edit VPN-TO-SH        
FortiGate1_BeiJing (VPN-TO-SH) # set npu-offload disable                                                                        
FortiGate1_BeiJing (VPN-TO-SH) # end