前一篇文章中 ,笔者尝试了用Openwrt和RouterOS配合IPIP和EoIP隧道进行二层组网。但由于Openwrt的EoIP包是一个 用户态的包 ,AX6s转发EoIP流量的性能并不是特别好(有线500Mbps,无线300Mbps),同时RouterOS也是一个收费系统,并不是所有人都会只为了组网来购买RouterOS的授权。鉴于此,笔者也在下文提供一种将IPoE替换为GRETAP协议进行二层组网的方案,此方案仅需一台运行于实验室内网的通用Linux设备即可(本文使用的是Debian11)。


GRETAP?

相比于GRE,GRETAP是类似EoIP的二层协议,因此我们同样可以将其用于二层组网。但很可惜 RouterOS不支持GRETAP (怀疑是为了推广自家的EoIP协议),因此笔者只能使用另一台Linux设备和Openwrt路由器进行组网了。

网络结构

此处依然简要列出一下代配置的网络结构:

  • 实验室路由器:ikuai,向实验室内设备分配 192.168.102.0/24 的IP。

  • 安装在实验室的Debian服务器:Debian 11,有两个网络接口,接口 ens192 获取校园网IP 10.20.1.1 ;接口 ens224 连接ikuai,配置前暂不分配IP。

  • 安装在宿舍的Openwrt路由器:Openwrt 22.03.0,WAN口分配 10.20.1.2 的校园网IP。

配置IPIP隧道

由于学校内网封锁GRE协议,因此配置的第一步依然是在两侧设置用于承载GRETAP数据的的IPIP隧道。

Debian 侧

启用IPv4转发:

echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p

安装ipip和桥接包:

apt install ipip bridge-utils

添加隧道:

# 启用内核模块
modprobe ip_gre
modprobe ipip
# 添加隧道
ip tunnel add ipip2 local 10.20.2.223 remote 10.16.212.255
ip link set ipip2 up
ip addr add 6.0.0.1/24 dev ipip2

Openwrt 侧

先安装IPIP相关的包:

opkg update
opkg install luci-proto-ipip

重启路由器,随后添加IPIP interface:

Network > Interface > Add new interface...
name: ipip1
protocal: IPv4-in-IPv4 (RFC2003)
Remote IPv4 address or FQDN: 10.20.1.1
Local IPv4 address: 10.20.1.2
Bind interface: wan
同时也需要在Advanced Settings里去掉Use Default Gateway的选项。

给IPIP的interface分配一个静态IP:

Network > Interface > Add new interface...
name: ipip1static
protocal: Static
Device: ipip-ipip0
IPv4 address: 6.0.0.2/24
IPv4 gateway: 6.0.0.1
同时也需要在Advanced Settings里去掉Use Default Gateway的选项。

RouterOS和Openwrt一侧都配置好IPIP隧道后,可以互相ping一下对方的隧道IP ( 6.0.0.1 6.0.0.2 )来检查一下隧道是否通了。

添加GRETAP隧道并桥接上级网口

Debian 侧

添加隧道:

# gretap tunnel
# 如果不添加IPIP隧道,可以将remote和local的ip根据实际情况修改
ip link add name gretap1 type gretap remote 6.0.0.2 local 6.0.0.1
# mtu请根据实际情况设置
ip link set gretap1 up mtu 1420

桥接配置:

# bridge
ip link add grebr0 type bridge
ip link set ens224 master grebr0
ip link set gretap1 master grebr0
ip link set grebr0 up

不过笔者在配置时发现,按照上述配置拉起网桥后,GRETAP另一侧的Openwrt似乎无法收到ikuai发回的DHCP offer包, 但在ens224端口上执行 dhclient -i ens224 -4 -v 后莫名其妙的解决了该问题

Openwrt 侧

安装GRE相关的包并重启:

opkg update
opkg install luci-proto-gre ip-full

添加GRETAP interface:

Network > Interface > Add new interface...
name: gretap1
protocal: GRETAP Tunnel over IPv4
Remote IPv4 address or FQDN: 6.0.0.1
Local IPv4 address: 6.0.0.2
同时也需要在Advanced Settings里去掉Use Default Gateway的选项。

Debian 侧配置隧道开机自启

如要让Debian一侧的IPIP和GRETAP隧道开机自启,可以用systemd在开机时运行以下脚本:

/root/gretap-spark.sh

#modprobe
modprobe ip_gre
modprobe ipip
#ipip tunnel
ip tunnel add ipip2 local 10.20.2.223 remote 10.16.212.255
ip link set ipip2 up
ip addr add 6.0.0.1/24 dev ipip2
# gretap tunnel
ip link add name gretap1 type gretap remote 6.0.0.2 local 6.0.0.1
ip link set gretap1 up mtu 1420
# bridge
ip link add grebr0 type bridge
ip link set ens224 master grebr0
ip link set gretap1 master grebr0
ip link set grebr0 up
dhclient -i ens224 -4

/lib/systemd/system/gre-and-ipip-tunnel.service

[Unit]
Description=bring up gretap tunnel
After=network-online.target
[Service]
Type=simple
User=root
ExecStartPre=/bin/sh -c 'until ping -c1 1.2.4.8; do sleep 1; done;'
ExecStart=/bin/bash /root/gretap-spark.sh
[Install]
WantedBy=multi-user.target