在做Android6.0 carplay部分工作的时候,出现可一个很奇怪的问题:没有WIFI连接AP的时候,carplay连接正常,但是一旦WIFI连接AP之后,carplay会断开连接。
我们的手机连接到车机的时候车机会虚拟出一个usb0的网卡,并且carplay的数据是通过IPV6协议传输的。由于本人对IPV6一点都不懂,所以以下都是通过尝试和猜测去调试的。
首先看没有WIFI连接的情况
手机连接之后执行 ifconfig usb0 up 然后查看网卡是否正常
添加默认路由 ip -6 route add default dev usb0
root@sabresd_6dq:/ # ifconfig usb0
usb0 Link encap:Ethernet HWaddr 6E:E5:80:99:88:83
inet6 addr: fe80::f4db:d7ff:fe8a:c26e/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:33 errors:0 dropped:0 overruns:0 frame:0
TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6924 TX bytes:5720
没有WIFI连接的时候路由策略如下
root@sabresd_6dq:/ # ip -6 rule
0: from all lookup local
10000: from all fwmark 0xc0000/0xd0000 lookup legacy_system
13000: from all fwmark 0x10063/0x1ffff lookup local_network
15000: from all fwmark 0x0/0x10000 lookup legacy_system
16000: from all fwmark 0x0/0x10000 lookup legacy_network
17000: from all fwmark 0x0/0x10000 lookup local_network
23000: from all fwmark 0x0/0xffff uidrange 0-0 lookup main
32000: from all unreachable
root@sabresd_6dq:/ # ip -6 route list table main
default dev usb0 metric 1024
可以看到默认路由表里面有了路由
我们在看inet6的路由
root@sabresd_6dq:/ # route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
fe80::/64 :: U 256 0 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 2 0 usb0
::/0 :: U 1024 0 1 usb0
::1/128 :: U 0 0 1 lo
fe80::f4db:d7ff:fe8a:c26e/128 :: U 0 14 1 lo
ff02::2/128 :: UC 0 3 0 usb0
ff02::16/128 :: UC 0 2 0 usb0
ff02::fb/128 :: UC 0 572 0 usb0
ff02::1:ff8a:c26e/128 :: UC 0 1 0 usb0
ff00::/8 :: U 256 0 4 usb0
fe80::104e:cefc:4317:9f5f是手机端的IPV6地址,我们ping6之后发现是通的,此时的连接没有异常
root@sabresd_6dq:/ # ping6 fe80::104e:cefc:4317:9f5f%usb0
PING fe80::104e:cefc:4317:9f5f%usb0(fe80::104e:cefc:4317:9f5f) 56 data bytes
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=1 ttl=64 time=0.999 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=2 ttl=64 time=1.00 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=3 ttl=64 time=0.912 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=4 ttl=64 time=0.939 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=5 ttl=64 time=0.892 ms
--- fe80::104e:cefc:4317:9f5f%usb0 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 0.892/0.948/1.001/0.055 ms
当我们的WIFI打开并且连接一个AP之后,发现路由策略多了几条关于WLAN0的
root@sabresd_6dq:/ # ip -6 rule
0: from all lookup local
10000: from all fwmark 0xc0000/0xd0000 lookup legacy_system
10500: from all oif wlan0 uidrange 0-0 lookup wlan0
13000: from all fwmark 0x10063/0x1ffff lookup local_network
13000: from all fwmark 0x10065/0x1ffff lookup wlan0
14000: from all oif wlan0 lookup wlan0
15000: from all fwmark 0x0/0x10000 lookup legacy_system
16000: from all fwmark 0x0/0x10000 lookup legacy_network
17000: from all fwmark 0x0/0x10000 lookup local_network
19000: from all fwmark 0x65/0x1ffff lookup wlan0
22000: from all fwmark 0x0/0xffff lookup wlan0
23000: from all fwmark 0x0/0xffff uidrange 0-0 lookup main
32000: from all unreachable
root@sabresd_6dq:/ # ip -6 route list table main
default dev usb0 metric 1024
可以看到默认路由表里面有了路由
于是查看这个时候的路由表
root@sabresd_6dq:/ # route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
fe80::/64 :: U 256 0 0 wlan0
fe80::/64 :: U 1024 0 0 wlan0
fe80::/64 :: U 256 0 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 8 5 usb0
::/0 :: U 1024 0 1 usb0
::1/128 :: U 0 0 1 lo
fe80::215:83ff:fe01:20/128 :: U 0 0 1 lo
fe80::f4db:d7ff:fe8a:c26e/128 :: U 0 20 1 lo
ff02::fb/128 :: UC 0 14 0 usb0
ff02::fb/128 :: UC 0 11 0 wlan0
ff02::1:ff01:20/128 :: UC 0 2 0 wlan0
ff00::/8 :: U 256 0 1 usb0
ff00::/8 :: U 256 0 2 wlan0
发现之前的路由还是在的,于是执行下面的ping6发现不通了
root@sabresd_6dq:/ # ping6 fe80::104e:cefc:4317:9f5f%usb0
connect: Network is unreachable
查看wlan0的路由规则发现没有usb0的通路
于是我又执行了 ip -6 route add default dev usb0 table wlan0 然后查看路由表
root@sabresd_6dq:/ # route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
2000::/128 :: UC 0 3 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 4 3 usb0
fe80::/64 :: U 256 0 0 wlan0
fe80::/64 :: U 1024 0 0 wlan0
::/0 :: U 1024 0 2 usb0
fe80::/64 :: U 256 0 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 8 5 usb0
::/0 :: U 1024 0 1 usb0
::1/128 :: U 0 0 1 lo
fe80::215:83ff:fe01:20/128 :: U 0 0 1 lo
fe80::f4db:d7ff:fe8a:c26e/128 :: U 0 25 1 lo
ff02::fb/128 :: UC 0 310 0 usb0
ff02::fb/128 :: UC 0 385 0 wlan0
ff02::1:ff01:20/128 :: UC 0 84 0 wlan0
ff00::/8 :: U 256 0 1 usb0
ff00::/8 :: U 256 0 2 wlan0
然后再ping
root@sabresd_6dq:/ # ping6 fe80::104e:cefc:4317:9f5f%usb0
PING fe80::104e:cefc:4317:9f5f%usb0(fe80::104e:cefc:4317:9f5f) 56 data bytes
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=1 ttl=64 time=1.22 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=2 ttl=64 time=0.880 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=3 ttl=64 time=0.980 ms
--- fe80::104e:cefc:4317:9f5f%usb0 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.880/1.027/1.223/0.148 ms
竟然又通了,这个时候就想到了会不会是路由策略的问题,难道是WIFI连接之后默认的使用WLAN0的路由策略???这个我还没找到原因,希望大神指导一下。
于是想了一个其他方法,删除原来添加在wlan0里面的默认路由,创建一个等级更高的路由策略,并且指定所有经过usb0的的数据都使用这个路由策略,fe80::f4db:d7ff:fe8a:c26e是我们车机上usb0的IPV6地址
ip -6 route del default dev usb0 table wlan0
ip -6 rule add fromfe80::f4db:d7ff:fe8a:c26e/64 table 1 pref 100
ip -6 route add default dev usb0 table 1
root@sabresd_6dq:/ # ip -6 rule
0: from all lookup local
100: from fe80::f4db:d7ff:fe8a:c26e/64 lookup 1
10000: from all fwmark 0xc0000/0xd0000 lookup legacy_system
10500: from all oif wlan0 uidrange 0-0 lookup wlan0
13000: from all fwmark 0x10063/0x1ffff lookup local_network
13000: from all fwmark 0x10066/0x1ffff lookup wlan0
14000: from all oif wlan0 lookup wlan0
15000: from all fwmark 0x0/0x10000 lookup legacy_system
16000: from all fwmark 0x0/0x10000 lookup legacy_network
17000: from all fwmark 0x0/0x10000 lookup local_network
19000: from all fwmark 0x66/0x1ffff lookup wlan0
22000: from all fwmark 0x0/0xffff lookup wlan0
23000: from all fwmark 0x0/0xffff uidrange 0-0 lookup main
32000: from all unreachable
root@sabresd_6dq:/ # route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
::/0 :: U 1024 0 0 usb0
2000::/128 :: UC 0 9 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 9 5 usb0
fe80::/64 :: U 256 0 0 wlan0
fe80::/64 :: U 1024 0 0 wlan0
::/0 :: U 1024 0 2 usb0
fe80::/64 :: U 256 0 0 usb0
fe80::104e:cefc:4317:9f5f/128 :: UC 0 8 5 usb0
::/0 :: U 1024 0 1 usb0
::1/128 :: U 0 0 1 lo
fe80::215:83ff:fe01:20/128 :: U 0 0 1 lo
fe80::f4db:d7ff:fe8a:c26e/128 :: U 0 30 1 lo
ff02::2/128 :: UC 0 3 0 usb0
ff02::fb/128 :: UC 0 438 0 usb0
ff02::fb/128 :: UC 0 528 0 wlan0
ff02::1:ff01:20/128 :: UC 0 111 0 wlan0
ff00::/8 :: U 256 0 2 usb0
ff00::/8 :: U 256 0 2 wlan0
然后再ping
root@sabresd_6dq:/ # ping6 fe80::104e:cefc:4317:9f5f%usb0
PING fe80::104e:cefc:4317:9f5f%usb0(fe80::104e:cefc:4317:9f5f) 56 data bytes
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=1 ttl=64 time=1.22 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=2 ttl=64 time=0.880 ms
64 bytes from fe80::104e:cefc:4317:9f5f: icmp_seq=3 ttl=64 time=0.980 ms
--- fe80::104e:cefc:4317:9f5f%usb0 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 0.880/1.027/1.223/0.148 ms
这个方法是可以暂时解决问题,但是我想弄清楚的是WIFI连接之后,是不是默认使用wlan0的路由策略?还有IPV6的路由信息貌似也看不懂啊,后续再研究
另外可以参考以下博客:
http://blog.csdn.net/u012246195/article/details/53427111 Android 4G ppp 拨号后的路由问题
http://blog.csdn.net/midion9/article/details/49245857 Linux的路由和策略路由
https://yq.aliyun.com/articles/64861 linux IP 命令使用举例