windows及linux l2tp vpn拨号后路由设置
2014-08-06 17:47:33 阿炯

windows L2TP/IPSec 预共享密钥VPN客户端连接设置
打开新建的连接,点击“属性”,打开“安全”页签,选择“高级”,点"设置",数据加密选择“可选加密(没有加密也可以连接)”,“允许这些协议”至少要选择"Microsoft CHAP 版本2(MS-CHAP v2)"。如果vpn服务器后端使用了radius认证方式且其使用了ldap方式存储用户信息时,则在拨号时不能选择加密,这点需要注意,不然会失败的。

回到“安全”页签,点击"IPSec设置",输入共享密钥。

选择“网络”标签页,VPN类型选择为“L2TP IPSec VPN”。

然后在连接界面输入用户名和密码连接即可。

NAT后面的L2TP/IPSec服务器
windows默认不允许连接到NAT设备后面的L2TP/IPSec服务器,拨号时会出现错误“错误809:无法建立计算机与 VPN 服务器之间的网络连接,因为远程服务器未响应。”

如果要访问这样的服务器,需要修改注册表:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IPSec下面新建DWORD项AssumeUDPEncapsulationContextOnSendRule,修改其值为"2",重新启动计算机。

VPN拨号错误

“错误789:L2TP链接尝试失败,因为安全层在初始化与远程计算的协商时遇到一个错误。”

解决办法:注册表键HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Rasman\Parameters下新建DWORD项ProhibitIpSec,修改其值为"1"。重新启动计算机。

ProhibitIpSec 注册表值设置为 1 时,基于 Windows 2000 的计算机不会创建使用 CA 身份验证的自动筛选器,而是检查本地 IPSec 策略或 Active Directory IPSec 策略。

VPN拨号路由设置

VPN拨号后默认会添加一条默认路由,默认网关指向对端地址,如果对端不转发数据包,则客户端无法访问互联网,就算转发,通过VPN服务器访问互联网效率也比较低下。

可以通过禁止VPN连接添加默认路由,手动修改路由表,以便只有必要的流量通过VPN,其他流量走正常的路径。

打开VPN连接,点击“属性”,选择“网络”页签,选中“Internet协议(TCP/IP)”,点击“属性”,继续点击"高级",在“常规”页签中,不要选择"在远程网络上使用默认网关",确定后关闭打开的窗口。

拨号成功后查看本地分配的IP地址,比如为10.100.0.2,然后添加用于VPN网络的静态路由

cmd>route add 10.100.0.0 mask 255.255.255.0 10.100.0.2

这样VPN网络和互联网可以互不影响


在linux使用route指令添加路由

在debian下拨号到vpn的方法请参考:
Debian下配置l2tp vpn客户端
又一种访问内部服务的方法

语法如下:
route add -net $NET netmask $MASK gw $GATEWAY
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

可在网卡的配置文件加入指令
You need to add above syntax to post-up command as follows:

post-up command
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

上述指令将会在接口启动后运行。

Example

Edit /etc/network/interfaces, enter:
$ sudo vi /etc/network/interfaces
#---------------------------------------#
# Feel free to change IP and gateway    #
# as per your local setup and routing   #
# policy                                #
# Last edited by root @ 23/Oct/2012     #
#---------------------------------------#
 
#--------------------------------------------#
# Setup the loopback network interface (lo0) #
#--------------------------------------------#
auto lo
iface lo inet loopback
 
#--------------------------------------------#
# Setup eth0 - connected to private LAN/VLAN #
#--------------------------------------------#
auto eth0
allow-hotplug eth0
iface eth0 inet static
 address 10.70.201.5
 netmask 255.255.255.192
### Ubuntu Linux add persistent route command ###
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6
 
#----------------------------------------#
# Setup eth1 - connected to the Internet #
#----------------------------------------#
auto eth1
allow-hotplug eth1
iface eth1 inet static
 address 205.153.203.98
 netmask 255.255.255.248
### Ubuntu Linux - This is your default gateway ###
 gateway 205.153.203.97
 
保存后重启该网络接口,可以使用下面的指令来验证是否是预期的设置。
$ /sbin/route -nv

$ ip route show

发送imcp包(ping)来测试到主机或网络的路由是否正确
$ ping lan-ip-address
$ ping 10.90.21.6
$ ping www.freeoa.net

List Kernel’s Routing Cache Information
列出内核中缓存的路由信息

Kernel maintains the routing cache information to route the packets faster. We can list the kernel’s routing cache information by using the -C flag.

内核会维护缓存路由表,这样包转发更快,我们可以通过使用'-c'标志来列出内核中的路由缓存信息。

$ route -Cn

Reject Routing to a Particular Host or Network
阻止到特定主机或网络的路由

$ route add -host 192.168.1.51 reject

这样本机到192.168.1.51的路由就中断了,包也不到达那台机器。

阻止整个网络
$ route add -net 192.168.1.0 netmask 255.255.255.0 reject


Win7使用L2TP VPN出现809错误解决之法

修改注册表:regedit

Windows Registry Editor Version 5.00

;[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\RasMan\Parameters]
;"ProhibitIpSec"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\RasMan\Parameters]
"ProhibitIpSec"=-

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent]
"AssumeUDPEncapsulationContextOnSendRule"=dword:00000002

将以下几个服务设为自动:services.msc

IPsec Policy Agent
Routing and Remote Access
Remote Access Auto Connection Manager
Remote Access Connection Manager
Secure Socket Tunneling Protocol Service

重启计算机,即可使用L2TP。