如何configurationlinux路由/过滤,将数据包从一个接口发送出去,通过一个网桥,并进入同一个盒子的另一个接口

我试图testing一个以太网桥接设备。 我有一个Linux机箱上的多个以太网端口。 我希望将数据包从一个接口(例如IP 192.168.1.1的eth0)发送到另一个接口(例如IP为192.168.1.2的eth1)在同一个子网上。

我意识到通常你不要在同一个子网上configuration两个接口,如果你直接把内核路由到每个接口,而不是通过networking。 我怎样才能覆盖这种行为,使stream量到192.168.1.2出去的192.168.1.1接口,反之亦然?

提前致谢!

使用networking名称空间。 感觉就像是在运行一个虚拟机,但它不是一个虚拟机,只是看起来像一个独立的IP栈。

ip netns add otherhost ip netns exec otherhost /bin/bash 

这将在其他otherhostnetworking命名空间下打开一个shell。 如果您检查其中的networkingconfiguration,则会看到没有界面。 就好像你正在运行一个不同的主机。

现在,将eth1接口移动到其他otherhostnetworking命名空间:

 ip link set eth1 netns otherhost 

现在,其他otherhost名称空间有您的eth1接口。 如果它是一个单独的主机,像configuration它一样进行configuration,然后对默认networking命名空间中的eth0执行相同的操作。 就这么简单。

请注意,如果将所有shellclosures到其他otherhost ,则networking名称空间将消失,其接口将被移回默认的networking名称空间。

https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt

accept_local – BOOLEAN接受具有本地源地址的数据包。 结合适当的路由select,这可以用来通过线路在两个本地接口之间引导数据包,并使它们正确接受。 默认为FALSE

使用sysctl -w net.ipv4.conf.eth0.accept_local=1

通过电线发送数据包。

尝试

 #mark packets from 192.168.1.1 to 192.168.1.2 iptables -t mangle -I OUTPUT -s 192.168.1.1 -d 192.168.1.2 -j MARK --set-mark 11 #mark packets from 192.168.1.2 to 192.168.1.1 iptables -t mangle -I OUTPUT -s 192.168.1.2 -d 192.168.1.1 -j MARK --set-mark 12 #add routing table for 192.168.1.1 ip ru a fwmark 11 table 11 ip ra 192.168.1.2 dev eth0 t 11 #add routing table for 192.168.1.2 ip ru a fwmark 12 table 12 ip ra 192.168.1.1 dev eth1 t 12 

男子IP,男子iptables的更多信息