07
2019
05
17:24:19

Linux 下基于路由 IPsec 的花式实践



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

动机

IPsec 转发流量有两种方式,一种是基于策略,另外一种是基于路由。本文将尝试阐述基于路由的 IPsec 核心概念和实现原理并引出一些花式玩法。

IPsec 带给计算机世界的困惑

在计算机世界里面,核心的一个概念就是路由,其定义了某个指定的数据包要从哪个接口出去的规则。很多软件和命令都是基于这个事实来设计的。譬如 iproute 包,各类路由软件,iptables 等等。

但是在 IPsec 的世界里面,默认是根据内核里面的规则直接就进行了封装和转发,这导致用户理解困难,软件无法正常运作。举例而言,你没有办法通过增加一条路由来告诉计算机某个地址要通过 IPsec 隧道来走。

要解决这个问题,就只能用计算机世界里面最常用的手段:增加一层封装,虚拟出来一个设备,使其符合原来的世界规则。

这样从设计上来说就变成了:

  1. IPsec 只管建立通道和进行封装转发

  2. 虚拟设备只管数据包怎么处理和路由

打通虚拟设备和 IPsec 的任督二脉

要从实际上解决这个问题,有两种方法。

第一种方法:VTI (Virtual Tunnel Interface) 的原理

最初往 Linux 增加 VTI  功能的作者提到:Virtual tunnel interface is a way to represent policy based IPsec tunnels as virtual interfaces in linux. This is similar to Cisco's VTI (virtual tunnel interface) and Juniper's representaion of secure tunnel (st.xx). The advantage of representing an IPsec tunnel as an interface is that it is possible to plug Ipsec tunnels into the routing protocol infrastructure of a router. Therefore it becomes possible to influence the packet path by toggling the link state of the tunnel or based on routing metrics.

这表明了 VTI 是一种虚拟网络设备。在 Linux 的实现中该设备主要作用是将经过这个设备的网络流量打上标签,IPsec的策略根据这个标签和相关信息来判断流量该怎么转发。

看到这个做法,对 iptables 熟悉的同学肯定马上就联想到了 iptables 打标签给 tc 来控制流量,通过 iptables 打标签来给 iproute 来控制路由等做法。是的,他们是师出同门,做法上是类同的。所以,如果你通过 iptables 给网络流量打上标签,也是能够实现让其数据包被 IPsec 转发的效果的。

第一种方法:VTI 的实践

  1. 要实现这种做法,需要在 IPsec 的配置里面约定好标签(配置项为mark)的值。

  2. VTI 里面 local 和 remote 地址至少需要有一项跟 IPsec 里面的 left,remote相同,key 的参数要跟 IPsec 里面的 mark 一致。

  3. VTI 设备本身可以单独配置地址,该地址可以用来做 NAT 等

sudo ip tunnel add vti6 mode vti local ${local_ip) remote 0.0.0.0 key 6sudo ip addr add ${tunnel_ip) dev vti6
sudo ip link set vti6 up
sudo ip route add ${remote_site_lan) dev vti6 src ${tunnel_ip)
sudo iptables -t nat -A POSTROUTING -o vti6 -j SNAT --to-source ${tunnel_ip}

第一种方法:花式玩法 iptables 篇

如果要仅仅通过 iptables 打标签就使得数据包通过 IPsec 转发,那么要怎么做呢?

参考下面的命令:

sudo iptables -t mangle -I PREROUTING -s ${remote_peer} -j MARK --set-mark 0x6
sudo iptables -t mangle -I PREROUTING -d ${remote_site_lan} -j MARK --set-mark 0x6
sudo iptables -t mangle -I OUTPUT -d ${remote_site_lan} -j MARK --set-mark 0x6

小窍门:可以通过 ip xfrm policy 查看 ipsec 的策略,包括其 mark 值等。可以通过 iptables -L 来查看 mark 的匹配情况。tcpdump 是看不到 mark 值的,因为 mark 值在内核维护,不体现在包的内容上面。

注意:

  1. 要注意发送出去的地址跟规则相符合,必要的话可以做个 SNAT

  2. 可以通过抓 remote_peer 的包来判断包是不是通过 IPsec 发送出去。

第一种方法:VTI 的作用和用其他虚拟设备的可能性

用 tuntap 设备,然后用 iptables fwmark 是不是可以和 VTI 有同样的效果呢?
如果是这样,那么 VTI 里面的 local 和 remote 有什么用途呢?

第一种方法:一些解答 - VTI 的地址

VTI 代码里面会去检查 IPsec 相关的策略(代码net/ipv4/ip_vti.c),这里涉及到 local 和 remote 地址,所以这两个地址不能随意设置,要不然就会被拒绝转发。从某种意义上来说,做这个检查是多余的,会使得系统耦合比较重。不过既然VTI作者的本意是通过 VTI 来表示 IPsec,这本身也是无可厚非。

第一种方法:另一种玩法实践 - 用 tuntap 设备

ip tuntap add tuntap mode tap ${tap_name}ip link set ${tap_name} up
ip addr add ${tunnel_ip} dev ${tap_name}ip route add ${remote_site_lan} dev ${tap_name} src ${tunnel_ip}iptables -t mangle -A PREROUTING -s ${remote_peer} -j MARK --set-mark ${mark}iptables -t mangle -A OUTPUT -o ${tap_name} -j MARK --set-mark ${mark}

你会看到 tap 设备的状态是 link down,NO-CARRIER,不过没关系,照样能用,只要数据包丢到内核去了就会被处理掉。

第二种方法:内部封装(GRE封装)

在 Strongswan 里面提到了 GRE 封装,相当于IPsec将GRE封装的包传过去,然后 GRE 封装里面又有源地址和目的地址。这种做法其实只要允许两个 Peer 之间加密就好了,底层怎么路由,封装 IPsec 都不管。我们常见的 L2TP,譬如 IP over IP 等等,其实也都是属于这个范畴的。

这种做法的缺点是两端需要单独再起一个服务来实现这些封装。

参考链接

https://www.spinics.net/lists/netdev/msg202714.html
https://wiki.strongswan.org/projects/strongswan/wiki/RouteBasedVPN
https://vincent.bernat.ch/en/blog/2017-route-based-vpn
https://www.juniper.net/documentation/software/screenos/screenos6.3.0/630_ce_VPN.pdf
https://www.inetzero.com/bgp-over-ipsec/
https://www.juniper.net/documentation/en_US/junos/topics/topic-map/security-route-based-ipsec-vpns.html
https://github.com/strongswan/strongswan/blob/master/testing/tests/ikev2/nat-virtual-ip/hosts/moon/etc/nat_updown
https://networkengineering.stackexchange.com/questions/10342/backup-ip-for-site-2-site-vpn-juniper-srx
https://kb.juniper.net/InfoCenter/index?page=content&id=KB14330
https://wiki.strongswan.org/projects/strongswan/wiki/Strongswanconf
https://libreswan.org/wiki/Route-based_VPN_using_VTI
https://www.ibm.com/support/knowledgecenter/en/SST55W_4.3.0/liaca/liaca_cfg_ipsec_vti.html
https://elixir.bootlin.com/linux/v4.1/source/net/ipv4/ip_vti.c#L78



作者:丁鸿辉
链接:https://www.jianshu.com/p/26f0828f2abd


本文链接:https://hqyman.cn/post/645.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: