案件设想(卡利LinuxRaspBerry Pi 2 B+ 上的 v2):
luis@Fresoncio:~$ sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.18.25.1 0.0.0.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 100 0 0 eth1
default 192.168.35.100 0.0.0.0 UG 101 0 0 eth2
... etc (see full table below)
以太网(RJ45 电缆)卡(NIC):
luis@Fresoncio:~$ sudo ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.246 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::ba27:ebff:fe92:feb3 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 3013014 bytes 296362931 (282.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9910966 bytes 558782777 (532.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.33 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::b117:1000:39bb:f8f0 prefixlen 64 scopeid 0x20<link>
ether a0:ce:c8:09:e3:ef txqueuelen 1000 (Ethernet)
RX packets 97636 bytes 7342211 (7.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 471 bytes 31163 (30.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.35.104 netmask 255.255.255.0 broadcast 192.168.35.255
inet6 fe80::805b:dfe3:8c3b:61ff prefixlen 64 scopeid 0x20<link>
ether a0:ce:c8:09:f8:84 txqueuelen 1000 (Ethernet)
RX packets 276507 bytes 18134656 (17.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 215 bytes 19996 (19.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 96 bytes 12362 (12.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 96 bytes 12362 (12.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
--- etc (see fulll table below)
可以看出,这台电脑有3 个网卡。ping 到互联网工作指定 NIC 时使用所有这些:
luis@Fresoncio:~$ sudo ping -I eth0 -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 172.18.25.246 eth0: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=36.7 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=308 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=54 time=85.5 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=54 time=151 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=54 time=56.6 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 36.753/127.695/308.472/98.316 ms
luis@Fresoncio:~$ sudo ping -I eth1 -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.1.33 eth1: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=43.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=41.0 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=40.7 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=55 time=41.5 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=55 time=40.9 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 40.757/41.517/43.303/0.962 ms
luis@Fresoncio:~$ sudo ping -I eth2 -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.35.104 eth2: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=50.7 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=37.4 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=49.2 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=55 time=37.7 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=55 time=53.2 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 37.499/45.711/53.250/6.723 ms
现在失败案例设想 (乌班图 v16在笔记本电脑上):
luis@Hipatio:~$ sudo route
Tabla de rutas IP del núcleo
Destino Pasarela Genmask Indic Métric Ref Uso Interfaz
default Router- 0.0.0.0 UG 0 0 0 enp2s0
default 192.168.22.1 0.0.0.0 UG 100 0 0 enx3c18a00b753d
link-local * 255.255.0.0 U 1000 0 0 enp2s0
192.168.11.0 * 255.255.255.0 U 0 0 0 enp2s0
192.168.22.0 * 255.255.255.0 U 100 0 0 enx3c18a00b753d
这次主打的是2 个 RJ45 电缆 NIC:
luis@Hipatio:~$ ifconfig
enp2s0 Link encap:Ethernet direcciónHW 68:f7:28:d2:a4:ce
Direc. inet:192.168.11.119 Difus.:192.168.11.255 Másc:255.255.255.0
Dirección inet6: fe80::6af7:28ff:fed2:a4ce/64 Alcance:Enlace
ACTIVO DIFUSIÓN FUNCIONANDO MULTICAST MTU:1500 Métrica:1
Paquetes RX:454517 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:1940326 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:1000
Bytes RX:73243397 (73.2 MB) TX bytes:2778755895 (2.7 GB)
enx3c18a00b753d Link encap:Ethernet direcciónHW 3c:18:a0:0b:75:3d
Direc. inet:192.168.22.51 Difus.:192.168.22.255 Másc:255.255.255.0
Dirección inet6: fe80::4c9d:de01:a97a:fd42/64 Alcance:Enlace
ACTIVO DIFUSIÓN FUNCIONANDO MULTICAST MTU:1500 Métrica:1
Paquetes RX:194 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:355 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:1000
Bytes RX:20096 (20.0 KB) TX bytes:39265 (39.2 KB)
lo Link encap:Bucle local
Direc. inet:127.0.0.1 Másc:255.0.0.0
Dirección inet6: ::1/128 Alcance:Anfitrión
ACTIVO BUCLE FUNCIONANDO MTU:65536 Métrica:1
Paquetes RX:60 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:60 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:1
Bytes RX:5099 (5.0 KB) TX bytes:5099 (5.0 KB)
这次失败(无应答)从非默认 NIC 执行 ping 操作时:
luis@Hipatio:~$ ping -I enx3c18a00b753d -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.22.51 enx3c18a00b753d: 56(84) bytes of data.
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 3999ms
这默认一个有效好的:
luis@Hipatio:~$ ping -I enp2s0 -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) from 192.168.11.119 enp2s0: 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=51.6 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=52.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=50.9 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=55 time=50.8 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=55 time=51.0 ms
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 50.812/51.498/52.995/0.826 ms
luis@Hipatio:~$ ping -c 5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=51 time=57.9 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=51 time=57.0 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=51 time=57.0 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=51 time=57.1 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=51 time=57.6 ms
同样的测试也失败了在适用于 RaspBerry Pi 2 B+ 的 Ubuntu Mate v16(这次是 4 个 RJ45 网卡):只有默认的一个可以工作。所以我想知道问题是否应该出现在 Ubuntu 上(我确信这个 ping 测试适用于较旧的 Ubuntu 版本,例如 v14)。不管怎样,知道如何解决任何 Linux 的问题对我来说是最好的。
这里发生了什么事,我该怎么办从特定 NIC 执行 ping 操作?
注1:第一个示例中的 Kali 工作发行版有多个 VPN 同时工作(我想说读起来有点混乱),所以我删掉了表格route
。这是完整的版本(我想说没有必要,但是......谁知道呢?):
luis@Fresoncio:~$ sudo route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.18.25.1 0.0.0.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 100 0 0 eth1
default 192.168.35.100 0.0.0.0 UG 101 0 0 eth2
115.red-79-157- 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
67.red-79-157-8 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
187.red-81-32-1 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
227.red-81-32-1 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
82.red-81-33-18 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
190.red-83-44-1 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
152.red-83-44-2 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
249.red-83-44-2 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
10.red-83-52-23 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
172.red-83-52-2 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
206.red-83-52-2 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
33.244.222.87.d 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
32.246.222.87.d 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
235.248.222.87. 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
151.18.16.95.dy 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
196.48.16.95.dy 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
143.49.16.95.dy 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
189.54.16.95.dy 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
122.56.16.95.dy 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
11.81.20.95.dyn 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
237.204.22.95.d 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
177.206.22.95.d 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
163.red-95-123- 172.18.25.1 255.255.255.255 UGH 0 0 0 eth0
172.18.25.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
192.168.35.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
192.168.35.0 0.0.0.0 255.255.255.0 U 100 0 0 eth2
192.168.210.0 0.0.0.0 255.255.255.0 U 0 0 0 ppp0
192.168.211.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0
...这是完整的ifconfig
:
luis@Fresoncio:~$ sudo ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.25.246 netmask 255.255.255.0 broadcast 172.18.25.255
inet6 fe80::ba27:ebff:fe92:feb3 prefixlen 64 scopeid 0x20<link>
ether b8:27:eb:92:fe:b3 txqueuelen 1000 (Ethernet)
RX packets 3018864 bytes 296745295 (282.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9911793 bytes 558868110 (532.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.33 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::b117:1000:39bb:f8f0 prefixlen 64 scopeid 0x20<link>
ether a0:ce:c8:09:e3:ef txqueuelen 1000 (Ethernet)
RX packets 97908 bytes 7362465 (7.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 480 bytes 31821 (31.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.35.104 netmask 255.255.255.0 broadcast 192.168.35.255
inet6 fe80::805b:dfe3:8c3b:61ff prefixlen 64 scopeid 0x20<link>
ether a0:ce:c8:09:f8:84 txqueuelen 1000 (Ethernet)
RX packets 277209 bytes 18180584 (17.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 223 bytes 20612 (20.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 96 bytes 12362 (12.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 96 bytes 12362 (12.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1446
inet 192.168.210.141 netmask 255.255.255.0 destination 192.168.210.1
ppp txqueuelen 3 (Point-to-Point Protocol)
RX packets 461 bytes 43679 (42.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 423 bytes 90174 (88.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet 192.168.211.141 netmask 255.255.255.0 destination 192.168.211.141
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 100 (UNSPEC)
RX packets 14 bytes 704 (704.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
笔记2: 新线索。通过研究,我发现多路由能力可能需要该选项CONFIG_IP_MROUTE_MULTIPLE_TABLES
或CONFIG_IP_MULTIPLE_TABLES
在核心。
我怀疑这一点,只要这是输出失败电脑:
luis@Hipatio:/usr/src/linux-headers-4.4.0-62-generic$ cat .config | grep "multiple" -i
# CONFIG_OPROFILE_EVENT_MULTIPLEX is not set
CONFIG_NEED_MULTIPLE_NODES=y
CONFIG_IP_MULTIPLE_TABLES=y
# CONFIG_IP_MROUTE_MULTIPLE_TABLES is not set
CONFIG_IPV6_MULTIPLE_TABLES=y
CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
# Multiplexer I2C Chip support
这是在职的电脑:
luis@Fresoncio:/usr/src/kernel$ cat .config | grep "multiple" -i
CONFIG_IP_MULTIPLE_TABLES=y
CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
CONFIG_IPV6_MULTIPLE_TABLES=y
CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
# Multiplexer I2C Chip support
注3:经过测试(或者我相信)的方法参考笔记2通过修改/etc/default/grub
行:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash CONFIG_IP_MROUTE_MULTIPLE_TABLES=y"
可以看出,现在系统以该参数启动yes
:
luis@Hipatio:~$ cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-4.4.0-62-generic root=UUID=66464d51-851f-4623-b5cb-1699901a4a9b ro quiet splash CONFIG_IP_MROUTE_MULTIPLE_TABLES=y vt.handoff=7
可悲的是,与没有结果。 ping 仍然失败。
答案1
默认情况下,系统实际上对给定目的地使用一条路由,即您通过ip route get 8.8.8.8
.如果它在一个接口上收到的数据包不是上一个命令显示的数据包,它会认为这是某种欺骗尝试并将其丢弃。如果禁用反向路径过滤,它将不会执行此操作:
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/enx3c18a00b753d/rp_filter.
如果可能,并且使用默认路由总是可能的,则首选松散模式(使用值 2,同时为 enp2s0 保留 1)。
关于您的评论,enp2s0 不存在竞争条件:路由表不会更改,也不会被这些设置更改,因此不会发生竞争。请参阅ip-sysctl.txt Linux 内核文档 更多细节。
现在,为了你的目标,这不是真正应该做的。您必须设置一条路线桌子每个接口/提供商和源IP(顺便说一下,这就是 CONFIG_IP_MULTIPLE_TABLES 的用途。忘记 CONFIG_IP_MROUTE* 它是用于多播的)。然后使用一些选择逻辑ip
rule
命令。不止一种可能性。例如通过使用 iptables 标记数据包并将这些标记用于更多路由规则,或者正如您的示例之一所做的那样,为特定目的地设置特定路线,甚至更简单(请参阅最后)。
所有详细信息均附有示例:
http://lartc.org/howto/lartc.rpdb.multiple-links.html
在这里直接放置一个工作示例有点长,但似乎很容易理解(包括一些警告等)。请注意,在这种情况下,您不必再更改 rp_filter ,因为数据包将始终通过预期接口到达,即使(并且因为)路由选择算法不像以前那么简单。
一旦就位(而不是基于路由的负载平衡示例),您可以使用 iptables 执行操作。例如,在 nat 表中组合statistic
模块,--every
以便动态负载平衡与您使用的多个 SNAT(每个提供商一个)的每个新连接。