小米路由器mini + OpenWrt +OpenVPN 实现 Site-to-Site

来源:互联网 发布:新网域名跳转微博 编辑:程序博客网 时间:2024/05/16 08:52

小米路由器mini + OpenWrt +OpenVPN 实现 Site-to-Site 

 

郑重提醒:刷机有风险!如果怕怕滴,就别刷了。

 

此前我这边公司采用的是H3CIPSec VPN做的 Site-to-Site,据H3C的专业工程师说是要实现Site-to-Site,需要两边都必须在公网,IP地址可以静态也可以动态。H3C的解决方案也算便宜,但就是要求客户端都要有公网IP这点感觉有点笨笨的,穿越防火墙能力最强的VPNOpenVPN莫属了,因为其可以通过UDP通讯,并且端口可以任意设定,这一特性使我爱上了OpenVPN

现在来解释一下何谓 Site-to-SiteSite-to-Site故名思义是网到网,也就是通过VPN把两个原本孤立的网络透明地连接起来。OpenVPN配合适当的路由设置完全可以实现,并且客户端只要能上网就可以把网络透明地连接起来,这种方案只需要服务器端有固定的公网IP,甚至通过那些花生壳什么的,动态的公网IP理论上也可以。

 

 

下面我来简单说明一下此文想要折腾什么东西出来。

如下图的网络结构示意图中,最终目的是想实现 Server SideLAN A192.168.0.0/24)Client SideLAN B172.31.7.0/24)实现自由访问。

 网络结构

网络中除了 Xiaomi Router Mini(简称 XRM)而外,其它均为原有设备。后文描述内容,主要针对Open VPN相关设备:XRMLinux ServerFirewall/GateWay

 

以下操作正常前的假定条件:

LAN A/B可以正常访问Internet;

服务器端防火墙您可以更改配置(NAT时需要);

您熟悉SSH;

您可以配置基本的OpenVPN,保证连通性;

您具备网络基本知识、路由、防火墙/Linux上的iptables

 

一、购买mini 

等了几个周二,终于可以下单买了。小米选那个什么货运公司啊?从成都的环球中心送华阳车站也不送,还好那天我本来正好有事要去市区,自己到货运公司提货了。

可惜没有拍照哦,已经拆掉了。

二、刷机前的准备工作

材料:一个U盘是必须的、一根网线、一把小螺丝刀(能撑那个 Reset 按钮的,回形针也可以),案例中提到的其它服务器、防火墙、PC等是原有设备。

小米官网有很多怎么刷到开发版,启用 SSH 功能的文档,我这里只是简单说明一下。

http://rom.xiaomi.cn/中选择最新的开发版,改名 miwifi.bin

https://d.miwifi.com/rom/ssh 下载工具包 miwifi_ssh.bin,并记下下载按钮左边的 root 密码(这个是ssh登录的密码,要记下哦)。

把下载的两个文件一并拷贝到U盘,把U盘插入XRMUSB口,拨掉XRM的电源,用小螺丝刀撑住Reset按钮(有明显感觉),再插回XRM电源,直到指示灯黄闪,可以松开Reset按钮,等待指示灯变成蓝色,刷机完成。

这一步完成把XRM准备在可刷OpenWRT的状态。

三、刷入 OpenWrt

为什么我们要刷其它的定制版?因为雷布斯太过稳重,选择了2.xx版的内核,很多OpenwrtIPK包安装都存在问题(而且我也找了,目前Openwrt库里面根本就没有与之内核对应版本的IPK存在),为了后面的安装工作顺利进行,我们直接把操作系统作个最新的升级!

OpenWrt 目前已经直接支持小米mini路由器了,最新的版本是15.05。下载地址:

https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/openwrt-15.05-ramips-mt7620-xiaomi-miwifi-mini-squashfs-sysupgrade.bin

 

您可以直接通过SSH登录到XRM,通过执行 wget 加上面的链接地址直接下载。国内访问速度很慢,不过还好,总算是能访问,没有被直接墙掉。

我是在电脑上下载后,再通过rcp 命令拷贝到 XRM上的。

SSH登录到XRM后,到刚才固件所拷贝的位置(我是放在 /tmp 下面), 执行

mtd -r openwrt-15.05-ramips-mt7620-xiaomi-miwifi-mini-squashfs-sysupgrade.bin OS1

其中OS1,可能是firware,原因是已经刷过Openwrt等其它的版本ROM时,它们会改写这个标识,实测,原理还未深入研究。

等待其完成后,会显示红灯!吓吓的,就因此我一度认为OpenWRT官网放出的ROM有问题,还去找PandoraBox这些,折腾了N久,还是官网上别个直接有说明(Please note that the OpenWRT binary defaults to the red color of the led instead of the orange/blue during and after finishing the boot.),刷出来默认就是亮红灯,,真是亮瞎了人的眼啊!!

了解更多官方关于 小米路由器mini 支持情况,请访问:

https://wiki.openwrt.org/toh/xiaomi/mini

 

四、配置XRM安装OpenVPN

等待启动完成后,XRMWIFI不可用,原因是15.05默认情况下没有开启WIFI,那也没有关系,我们插网线到 XRM LAN 口,本地IP改为自动获取后,可以看到其默认IP段是192.168.1.xXRM的管理IP是 192.168.1.1

通过SSH登录 XRM。执行 ssh root@192.168.1.1

4.1 通过opkg安装

直接 opkg update 半天没有反应,不知是怎么回事。后来看浏览器中访问 downloads.openwrt.org 时是启用的 https,那就把配置改成通过 ssl 试试。

vi /etc/opkg.conf

dest root /

dest ram /tmp

lists_dir ext /var/opkg-lists

option overlay_root /overlay

src/gz chaos_calmer_base https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/base

src/gz chaos_calmer_luci https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/luci

src/gz chaos_calmer_packages https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/packages

src/gz chaos_calmer_routing https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/routing

src/gz chaos_calmer_telephony https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/telephony

src/gz chaos_calmer_management https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/management

option check_signature 1

  很简单的修改把http 替换成 https 就行了,:1,$s/http/https/g

保存后,执行 opkg update,还是不成功,提示不支持 openssl ,我了个去,这个官方ROM也太懒了,连个 openssl 库都难得一起做进去啊。没有办法,只有手工整了。

下面两个包从 https://downloads.openwrt.org/chaos_calmer/15.05/ramips/mt7620/packages/base 下面单独手工下载:

libopenssl_1.0.2d-1_ramips_24kec.ipk

openssl-util_1.0.2d-1_ramips_24kec.ipk

下载了N次才成功啊,我的 100M光纤入户,我不得不怀疑是不是假的。还好文件都超级小。

先 opkg install libopenssl_1.0.2d-1_ramips_24kec.ipk, 再 opkg install openssl-util_1.0.2d-1_ramips_24kec.ipk, 安装成都后,再执行 opkg update,等等它慢慢地完成吧!一次不行,再多操作几次。

更新之后的安装就简单啦。

执行 opkg install openvpn-openssl

等待其安装完成。嗯很简单吧!安装好了。

4.2 手动安装

既然是手动安装,我们就得手动下载openvpn相关的安装包,需要从4.1中那个 opkg.conf中所列的目录中下载相应的文件(几个目录里面找哦!大部分都在 base目录中),清单:

kmod-tun libopenssl libpthread zlib liblzo openvpn-openssl

好像就上面几个吧,不对也没有关系,只要你记得是安装 openvpn-openssl 就行,因为 opkg install 的时候,会告诉你缺失的依赖包名,再下载就行。

XRM上执行 opkg install xxx.ipk 逐个安装吧。

 

五、配置 OpenVPN

OpenVPN的官方网站是获取OpenVPN资源的最佳途径。不知什么时候 openvpn.net 又不再被墙了,福音啊。

这里就不详述OpenVPN相关知识了(因为我也不大懂哦 ::P)。

 

OpenVPN Howto 

https://openvpn.net/index.php/open-source/documentation/howto.html

 

5.1 服务器端配置

使用样本配置文件,在此基础上进行以下修改:

port 8690 # 您喜欢用的端口号

dev tun  # 使用 tunnel 隧道

proto udp #网络达人都说UDP穿透防火墙能力超级强,这在企业部署时是必要的。

server 10.8.0.0 255.255.255.0 # 设定VPN tun 虚拟网卡的网络范围。

push “route 192.168.0.0 255.255.255.0” # 这个路由信息会被推送到客户端。指示客户端要访问 192.168.0.0/24需要通过 tun 转发报文。

client-config-dir /etc/openvpn/ccd # /etc/openvpn 下创建 ccd 目录,用于存放特定客户端接入时需要做的设定。后面在 CCD 一节中专门说明。 

route 172.31.4.0 255.255.255.0 #设定服务器端在加载当前配置时,为本地增加 172.31.4.0/24网络需要通过 tun 转发。

client-to-client # 去掉前面的 使其处于设定可用状态。

log-append /var/log/openvpn.log #日志信息添加到 指定文件的末尾。

 

其它主要配置项中跟证书相关的应配置妥当,在未正常工作前建议打开 openvpn 的日志功能,以便分析检查配置是否正常。

关于CCD

Openvpn在实现Site-to-Site时,必须为OpenVPN Server设定当前连接的客户端链路路由信息,这个目的是通过ccd 来实现的。在指定 client-config-dir 指定的目录目录下面创建连接客户端证书的 Common Name为文件名的文件,案例假定 Common Name 为 client2,其client2文件内容如下:

iroute 172.31.4.0 255.255.255.0  # 告诉Openvpn Server 需要在内部为172.31.4.0/24网络转发报文。

 

iroute route 配置项没有多余项,都是必须的,route Linux 内核所需要的路由,iroute 是 OpenVPN Server 需要路由的信息。

 

以上是关于 OpenVPN Server 的配置,要保证报文转发正常,请检查 cat /proc/sys/net/ipv4/ip_forward 是否为 1,通过编辑 /etc/sysctl.conf 文件中启用net.ipv4.ip_forward=1 可以有效打开路由转发功能。

通过 tcpdump 结合 iptables -L -nv 指令可以检查数据报文的转发情况。

5.2 客户端配置

1)XRM后台配置

以下配置通过 ssh 登录到XRM进行操作。

与其它终端类型的openvpn 配置方式 一致,在 /etc 下建立 目录  openvpn 把需要的 key 全部放入 /etc/openvpn 下。

同时增加连接配置文件client_test.ovpn 。修改其对应的证书及OpenVPN Server信息。

完成与常规终端相同的配置设置后,再修改 /etc/config/openvpn 文件 。

只保留一项 config openvpn ,类似以下段落:

package openvpn

 

config openvpn client_test

        # Set to 1 to enable this instance:

        option enabled 1

 

        # Include OpenVPN configuration

        option config /etc/openvpn/client_test.ovpn

 

完成以上设置后,基本在后台通过SSH执行的配置就完成。

2)启用客户端OpenVPN

重新路由器, reboot

六、测试连接

如果您的网络环境跟我的一样 OpenVPN Server 还处于防火墙内的话,就需要在防火墙上通过NATOpenVPN Server的端口映射到公网IP上。

Openvpn 客户端正常连接上服务器后。就可以直接在172.31.4.0/24网络中,访问服务器端局域网192.168.0.0/24。按5.1中的服务器端进行配置,正常情况下,可以直接在192.168.0.0/24中直接访问172.31.4.0/24中任意主机。

但是反过来从服务器端的网络(192.168.0.0/24)直接访问客户端网络(172.31.4.0/24)会成功吗?我们 ping 的时候会失望地看到返回

From x.x.x.x icmp_seq=1 Destination Port Unreachable

这是怎么回事呢?我也折腾了老久,终于搞定在XRM openwrt的防火墙上开通了 site-to-site 的通道!

第一步是为 network 增加一配置 /etc/config/network

config interface 'vpn'

        option ifname 'tun0'

第二步,修改防火墙配置 /etc/config/firewall

config zone

        option name 'vpn'

        option input 'ACCEPT'

        option output 'ACCEPT'

        option forward 'ACCEPT'

        option network 'vpn'

config forwarding                

        option src 'vpn'         

        option dest 'lan'        

 

OK 了吧!

七、可能的问题

7.1关于OpenVPN Site-to-Site/译文()

Including multiple machines on the server side when using a routed VPN (dev tun)

采用TUN 模式路由VPN时,在服务器端实现多机连接

 

Once the VPN is operational in a point-to-point capacity between client and server, it may be desirable to expand the scope of the VPN so that clients can reach multiple machines on the server network, rather than only the server machine itself.

 

 

For the purpose of this example, we will assume that the server-side LAN uses a subnet of 10.66.0.0/24 and the VPN IP address pool uses 10.8.0.0/24 as cited in the server directive in the OpenVPN server configuration file.

在这个案例中,我们假定服务器端 LAN 为 10.66.0.0/24,同时其服务器端OpenVPN配置文件中设定 VPN IP地址池为 10.8.0.0/24

 

First, you must advertise the 10.66.0.0/24 subnet to VPN clients as being accessible through the VPN. This can easily be done with the following server-side config file directive:

第一步,必须通知VPN客户端能通过VPN访问 10.66.0.0/24 子网。这个目的可以通过以下服务器端配置简单实现:

 

push "route 10.66.0.0 255.255.255.0"

 

Next, you must set up a route on the server-side LAN gateway to route the VPN client subnet (10.8.0.0/24) to the OpenVPN server (this is only necessary if the OpenVPN server and the LAN gateway are different machines).

下一步,需要在服务器端网络(10.66.0.0/24)网关中设定到VPN客户端子网( 10.8.0.0/24)的路由(如果 OpenVPN 服务没有运行在服务器端网络的网关上的话,这一步是必须的)。

 

Make sure that you've enabled IP and TUN/TAP forwarding on the OpenVPN server machine.

同时,请确认你已经在 OpenVPN 服务器上启用IP转发。 /proc/sys/net/ipv4/ip_forward

 

Including multiple machines on the server side when using a bridged VPN (dev tap)

采用桥接 TAP 模式 VPN时,在服务器端实现多机连接

One of the benefits of using ethernet bridging is that you get this for free without needing any additional configuration.

采用以太网桥接方式的好处就是不需要配置就可以获取这个功能。

 

Including multiple machines on the client side when using a routed VPN (dev tun)

采用TUN 模式路由VPN时,在客户器端实现多机连接

 

In a typical road-warrior or remote access scenario, the client machine connects to the VPN as a single machine. But suppose the client machine is a gateway for a local LAN (such as a home office), and you would like each machine on the client LAN to be able to route through the VPN.

典型的道路战士或者远程访问场景下,客户端以单机的形式通过VPN连接。但我们假设这个客户端机器是一个LAN的网关,同时你希望LAN内的每台机器都可以通过VPN可路由。

 

 

For this example, we will assume that the client LAN is using the 192.168.4.0/24 subnet, and that the VPN client is using a certificate with a common name of client2. Our goal is to set up the VPN so that any machine on the client LAN can communicate with any machine on the server LAN through the VPN.

举例来说,假定客户端LAN为 192.168.4.0/24,同时VPN采用 client2 这个标识名称的证书连接VPN。我们的目标是通过VPN使客户端内的所有机器能够与服务器端网内的所有机器通讯。

 

Before setup, there are some basic prerequisites which must be followed:

开始之前,下面的先决条件需要具备:

 

    The client LAN subnet (192.168.4.0/24 in our example) must not be exported to the VPN by the server or any other client sites which are using the same subnet. Every subnet which is joined to the VPN via routing must be unique.

把服务器当网关的所有子网 或者 其它连接VPN的客户端网络不能与 客户端LAN192.168.4.0/24) 相同,每一个通过VPN连接的子网必须是不同的。

 

    The client must have a unique Common Name in its certificate ("client2" in our example), and the duplicate-cn flag must not be used in the OpenVPN server configuration file.

服务器中 duplicate-cn 配置不能使用,同时所有客户端的标识名称都不能一致。

 

下面开始配置。

First, make sure that IP and TUN/TAP forwarding is enabled on the client machine.

第一步,确保客户端机器上的 ip_forward 是开启的。

 

Next, we will deal with the necessary configuration changes on the server side. If the server configuration file does not currently reference a client configuration directory, add one now:

第二步,我们需要在服务器端配置文件中作一些改动。如果服务器端配置文件中当前没有指向客户端配置目录,现在就增加:

    client-config-dir ccd

 

In the above directive, ccd should be the name of a directory which has been pre-created in the default directory where the OpenVPN server daemon runs. On Linux this tends to be /etc/openvpn and on Windows it is usually \Program Files\OpenVPN\config. When a new client connects to the OpenVPN server, the daemon will check this directory for a file which matches the common name of the connecting client. If a matching file is found, it will be read and processed for additional configuration file directives to be applied to the named client.

上述指令中,ccd 是运行 OpenVPN 服务端进程的目录 。Linux系统中是 /etc/openvpn,在Windows系统中通常是 \Program Files\OpenVPN\config。当一个新的客户端连接到 OpenVPN 服务端时,服务进程会在这个目录中检查是否存在与客户端标识名称一致的配置文件。如果找到,它将处理该文件中的指令。

 

The next step is to create a file called client2 in the ccd directory. This file should contain the line:

下一步是在 ccd 目录中创建 client2 文件,这个文件中包含以下内容:

 

    iroute 192.168.4.0 255.255.255.0

 

This will tell the OpenVPN server that the 192.168.4.0/24 subnet should be routed to client2.

这将通知OpenVPN服务端 192.168.4.0/24 这个子网的报文是路由到 client2

 

Next, add the following line to the main server config file (not the ccd/client2 file):

接下来,在OpenVPN配置主文件中加入以下内容(不是 ccd/client2):

 

    route 192.168.4.0 255.255.255.0

 

Why the redundant route and iroute statements, you might ask? The reason is that route controls the routing from the kernel to the OpenVPN server (via the TUN interface) while iroute controls the routing from the OpenVPN server to the remote clients. Both are necessary.

您可能会问为什么要冗余配置 route 和 iroute 指令呢? 理由这两个都是必要的,Linux通过 route 控制内核中路由报文到 OpenVPN服务端后,OpenVPN 服务端需要 iroute 指令控制报文从OpenVPN服务端到远程客户端。

 

Next, ask yourself if you would like to allow network traffic between client2's subnet (192.168.4.0/24) and other clients of the OpenVPN server. If so, add the following to the server config file.

如果您希望连接到OpenVPN服务端子网之间可以通讯,就需要在服务端配置文件中作如下更改:

 

    client-to-client

    push "route 192.168.4.0 255.255.255.0"

 

This will cause the OpenVPN server to advertise client2's subnet to other connecting clients.

这们可以使用把 client2 的网络信息通知到其它客户端(添加了路由信息到其它客户端)。

 

The last step, and one that is often forgotten, is to add a route to the server's LAN gateway which directs 192.168.4.0/24 to the OpenVPN server box (you won't need this if the OpenVPN server box is the gateway for the server LAN). Suppose you were missing this step and you tried to ping a machine (not the OpenVPN server itself) on the server LAN from 192.168.4.8? The outgoing ping would probably reach the machine, but then it wouldn't know how to route the ping reply, because it would have no idea how to reach 192.168.4.0/24. The rule of thumb to use is that when routing entire LANs through the VPN (when the VPN server is not the same machine as the LAN gateway), make sure that the gateway for the LAN routes all VPN subnets to the VPN server machine.

最后也是最容易遗忘的一步,为OpenVPN服务器网络的网关增加 192.168.4.0/24 网络到 OpenVPN 服务端的路由(您不需要在意OpenVPN服务端是否是服务端网络的网关)。假设我们忘记此步骤,这时您试图从服务器网络的一台机器(不是OpenVPN服务器本身) ping 192.168.4.8 会发生什么? ping 的发出报文可能可以到达那台机器,但是ping 的回应包却因为不知道路由而无法知道该如何投送。要想服务器端的网络内任何一台机器可以通过VPN访问客户端网络时,请确保服务器端网关设定到VPN客户端网络的报文应由VPN服务器转发。

 

Similarly, if the client machine running OpenVPN is not also the gateway for the client LAN, then the gateway for the client LAN must have a route which directs all subnets which should be reachable through the VPN to the OpenVPN client machine.

类似的,如果运行OpenVPN客户端应用的机器不是客户端网络的网关时,需要在客户端的网关上指定需要访问的VPN网络都要通过 运行VPN客户端应用的主机转发。

原创粉丝点击