android双网卡,指定发包网卡。

android 5.1,双网卡,一个为有线,名为eth0,一个为wifi ,名为wlan0。
SO_BINDTODEVICE绑定发出包的网卡为eth0
测试包的资源
https://download.csdn.net/download/ssj901217/11457266

1、wifi不开,只接有线,路由表中,只有绑定的网卡。

发送的目标为同网段,直接使用ARP查询目标IP对应的MAC地址。然后发送给目标(DST MAC地址为目标的mac, DST IP为目标IP)

发送目标为不同网段,直接把包发给GW。(DST MAC地址为GW的mac, DST IP为目标IP)

例为绑定eth0,eth0 mac 0e:7e:ae:ad:62:62:

shell@su_ip:/ $ busybox ifconfig
eth0 Link encap:Ethernet HWaddr 0E:7E:AE:AD:62:62
inet addr:10.106.41.48 Bcast:10.106.41.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1900 errors:0 dropped:0 overruns:0 frame:0
TX packets:195 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:216666 (211.5 KiB) TX bytes:25388 (24.7 KiB)
Interrupt:88

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:37 errors:0 dropped:0 overruns:0 frame:0
TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1580 (1.5 KiB) TX bytes:1580 (1.5 KiB)


22|shell@su_ip:/ $ busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.106.41.0 * 255.255.255.0 U 0 0 0 eth0

测试包见nowifi.pcap

2、打开wifi,此时路由表中,有2个网卡,绑定的网卡不是默认路由(wifi会优先)

发送的目标为同网段,直接使用ARP查询目标IP对应的MAC地址。然后发送给目标

发送目标为不同网段,直接使用ARP查询目标IP对应的MAC地址。因为目标不在同一vlan下,所以ARP广播不到,发送会失败。

例为绑定eth0,eth0 mac 0e:7e:ae:ad:62:62:

shell@su_ip:/ # busybox ifconfig
eth0 Link encap:Ethernet HWaddr 0E:7E:AE:AD:62:62
inet addr:10.106.41.48 Bcast:10.106.41.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5087 errors:0 dropped:0 overruns:0 frame:0
TX packets:234 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:468784 (457.7 KiB) TX bytes:27906 (27.2 KiB)
Interrupt:88

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:88 errors:0 dropped:0 overruns:0 frame:0
TX packets:88 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3412 (3.3 KiB) TX bytes:3412 (3.3 KiB)

wlan0 Link encap:Ethernet HWaddr 28:ED:E0:D0:94:BF
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1452 (1.4 KiB) TX bytes:1287 (1.2 KiB)

shell@su_ip:/ # busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.106.41.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 wlan0

测试包见withwifi1.pcap

3、在2的基础上,关闭wifi。结果同2,

root@su_ip:/ # busybox ifconfig
eth0      Link encap:Ethernet  HWaddr 0E:7E:AE:AD:62:62
          inet addr:10.106.41.48  Bcast:10.106.41.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12057 errors:0 dropped:0 overruns:0 frame:0
          TX packets:513 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1012381 (988.6 KiB)  TX bytes:49194 (48.0 KiB)
          Interrupt:88

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:219 errors:0 dropped:0 overruns:0 frame:0
          TX packets:219 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:7824 (7.6 KiB)  TX bytes:7824 (7.6 KiB)

root@su_ip:/ # busybox route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.106.41.0     *               255.255.255.0   U     0      0        0 eth0

测试包见wificlose.pcap

解决不同网段的ARP问题:

对eth0增加route
root@su_ip:/ #ip route add  dev eth0 via 10.106.41.1
root@su_ip:/ # ip route show table main
default via 10.106.41.1 dev eth0
10.106.41.0/24 dev eth0  proto kernel  scope link  src 10.106.41.48
192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.103

您的支持是我持续创作的动力!!!
android双网卡,指定发包网卡。_第1张图片

你可能感兴趣的:(网络)