Android网络设置

路由操作

android4.4之前的版本,只使用了一份路由表,可以通过route命令操作
Android5.0之后,考虑要对多网络的支持,采用了多路由表,网络的功能实现完全转移到netd上面,主要包括ip,路由配置,dns代理设置,带宽控制和流量统计等

3 种路由类型

主机路由
主机路由是路由选择表中指向单个IP地址或主机名的路由记录。主机路由的Flags字段为H。例如,在下面的示例中,本地主机通过IP地址192.168.1.1的路由器到达IP地址为10.0.0.10的主机。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     -------            -----     ------    ---    ---    -----
10.0.0.10     192.168.1.1    255.255.255.255   UH       0    0      0    eth0

网络路由
网络路由是代表主机可以到达的网络。网络路由的Flags字段为N。例如,在下面的示例中,本地主机将发送到网络192.19.12的数据包转发到IP地址为192.168.1.1的路由器。

Destination    Gateway       Genmask Flags    Metric    Ref     Use    Iface
-----------    -------     -------         -----    -----   ---    ---    -----
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0

默认路由
当主机不能在路由表中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关)上。默认路由的Flags字段为G。例如,在下面的示例中,默认路由是IP地址为192.168.1.1的路由器。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     ------- -----      ------    ---    ---    -----
default       192.168.1.1     0.0.0.0    UG       0        0     0    eth0

route add [-net|-host] [网域或主机] netmask [mask] [gw|dev]

  • 查看路由:
    busybox route -n
  • 删除默认路由:
    busybox route del default
  • 添加一条默认路由
    busybox route add default gw 192.168.70.254 dev eth0
  • 删除一条主机路由 目标是一个主机
    busybox route del -host 192.168.7.1 dev eth0
  • 添加一条主机路由 目标是一个主机
    busybox route add -host 192.168.7.1 dev eth0
  • 删除一条网络路由
    busybox route del -net 192.168.0.0 netmask 255.255.248.0 dev eth0
  • 添加一条网络路由 添加网关/设置网关
    busybox route add -net 192.168.0.0 netmask 255.255.248.0 dev eth0

ip

android不识别route命令,可用ip route查看android路由表
设置 IP 地址,路由,安全加密以及其它一些网络参数的

ip route help
#显示路由表规则优先级
ip route list
#显示链路
ip link list 
#显示地址
ip address show 
#显示路由
ip route show 
#显示ARP映射表
ip neigh show 
#刷新路由缓存
ip route flush cache 
#为路由表增加表项
ip route add [IP/defaulte] via [src IP] dev [dev] table [table name]
#增加新的路由表
ip rule add from [IP] table [table name]
#
ip route del gw 172.16.2.254
#
ip route del 172.16.6.0/24 dev eth0

ndc

ndc interface getcfg eth0
ndc monitor
ndc interface list

ndc network create 100
ndc network interface add 100 eth0 
ndc network route add 100 eth0 0.0.0.0/0 192.168.3.1
ndc network route add 100 eth0 192.168.3.0/24 #(gateway)
ndc resolver setnetdns 100 eth0 192.168.1.1
ndc network default set 100

ndc (Native Daemon Connector),为开发者提供了一个用于管理的接口
ndc的原理其实就是通过socket连接上netd进行交互
源代码路径: system/netd/server/ndc.c
支持的命令以及参数可以在system/netd/server/CommandListener.cpp查看:

|||
|-|-|-|
| interface |list|
| | readrxcounter / readtxcounter|
| | getthrottle <iface> <”rx/tx”>|
| | setthrottle <iface> < rx_kbps/tx_kbps>|
| | driver <iface> < cmd> < args>|
| | route < add/remove> < iface> <”default/secondary”> < dst> < prefix> < gateway>|
| list_ttys ||
| ipfwd |status|
| |enable/disable
| tether |status|
| |start-reverse/stop-reverse|
| |stop<|
| |start< addr_1 addr_2 addr_3 addr_4 [addr_2n]>|
| |interface< add/remove/list>|
| |dnslist|
| |dnsset < addr_1> < addr_2>|
|nat |< enable/disable> < iface> < extface> < addrcnt>< nated-ipaddr/prelength>|
|pppd |attach< tty> < addr_local> < add_remote> < dns_1>< dns_2>|
| |detach< tty>|
|softap |startap/stopap|
| |fwreload< iface> < AP/P2P>|
| |clients|
| |status|
| |set< iface> < SSID> < wpa-psk/wpa2-psk/open> [< key>< channel> < preamble>< max SCB>]|
|resolver |setdefaultif< iface>|
| |setifdns< iface>< dns_1>< dns_2>|
| |flushdefaultif|
| |flushif< iface>|
|bandwith |enable/disable|
| |removequota/rq|
| |getquota/gq|
| |getiquota/giq< iface>|
| |setquota/sq< bytes> < iface>|
| |removequota/rqs< iface>|
| |removeiiquota/riq < iface>|
| |setiquota/sq < interface> < bytes>|
| |addnaughtyapps/ana < appUid>|
| |removenaughtyapps/rna < appUid>|
| |setgolbalalert/sga < bytes>|
| |debugsettetherglobalalert/dstga< iface0>< iface1>|
| |setsharedalert/ssa< bytes>|
| |removesharedalert/rsa|
| |setinterfacealert/sia< iface>< bytes>|
| |removeinterfacealert/ria< iface>|
| |gettetherstats/gts < iface0> < iface1>|
|idletimer |enable/disable|
| |add/remove < iface> < timeout> < classLabel>|
|firewall |enable/disable/is_enabled|
| |set_interface_rule < rmnet0> < allow/deny>|
| |set_egress_source_rule < ip_addr> < allow/deny>|
| |set_egress_dest_rule < ip_addr> < port> < allow/deny>|
| |set_uid_rule < uid> < allow/deny>|
|clatd |stop/status/start < iface>|

Netd是Android系统中专门负责网络管理和控制的后台daemon程序,其功能主要分三大块:

  • 设置防火墙(Firewall)、网络地址转换(NAT)、带宽控制(Bandwidth)、流量统计、无线网卡软接入点(Soft Access Point)控制,网络设备绑定(Tether)、个人局域网(pan)、PPP链接、配置路由表、interface配置管理等。
  • Android系统中DNS信息的缓存和管理。
  • 网络服务搜索(Net Service Discovery,简称NSD)功能,包括服务注册(Service Registration)、服务搜索(Service Browse)和服务名解析(Service Resolve)等。

iptables

配置防火墙,数据报处理,网络地址转换(NAT)以及其它每个包的处理

设置包转发

sysctl -w net.ipv4.ip_forward=1

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1

Android 以太网/有线网Ethernet功能开发:

https://blog.csdn.net/Purple7826/article/details/80608172
RK3399(Android7.1)–配置wifi和以太网共存

最近在工作开发的时候,遇到一个难题就是Rk3399开发板在既连接以太网(内网),又连接外网(wifi)是没有办法进行同时通信的。开机以后,会默认连接不到(外网)wifi,涉及到网络优先级以及路由规则的配置。

首先,我们要修改Rk3399源码
1、/services/core/java/com/android/server/ConnectivityService.java

 public class ConnectivityService extends IConnectivityManager.Stub {
   private static final String TAG = "ConnectivityService"; 
 
     private static final boolean DBG = true; 
     private static final boolean VDBG = false; 
 
     private static final boolean LOGD_RULES = false; 
+    private static final boolean LOGD_BLOCKED_NETWORKINFO = true; 
+     
+    // if true: 
+    //    wifi and ethernet can coexist, if wifi and ethernet connect together, prefered to use ethernet 
+    // if false: 
+    //    wifi and ethernet can't coexist, if wifi and ethernet connect together, will tear down wifi 
+    //    TODO: still have bug in this case to fix (like can't reconnect wifi when ethernet disconnect) 
+    private static final boolean ENABLE_NETWORK_COEXIST = true; 

}
 public class ConnectivityService extends IConnectivityManager.Stub {

 loge("Dead network still had at least " + nr); 
             break; 
         } 
-        nai.asyncChannel.disconnect();  //此处修改为下面的
+ 
+        if (ENABLE_NETWORK_COEXIST) { 
+            log("Skip teardownUnneededNetwork: " + nai.name()); 
+        } else { 
+            nai.asyncChannel.disconnect(); 
+        } 
   }

2、/java/com/android/server/ethernet/EthernetNetworkFactory.java

class EthernetNetworkFactory { 
     private static final String NETWORK_TYPE = "Ethernet"; 
     private static final String TAG = "EthernetNetworkFactory"; 
-    private static final int NETWORK_SCORE = 150; //此处修改为下面(优先级)
+    private static final int NETWORK_SCORE = 30; 
     private static final boolean DBG = true; 
     private static final boolean VDBG = false; 
}

3、/core/java/android/net/NetworkFactory.java

public class NetworkFactory extends Handler { 
 
     private void evalRequest(NetworkRequestInfo n) { 
         if (VDBG) log("evalRequest"); 
-        if (n.requested == false && n.score < mScore && 
+        if (n.requested == false && 0 < mScore && 
                 n.request.networkCapabilities.satisfiedByNetworkCapabilities( 
                 mCapabilityFilter) && acceptRequest(n.request, n.score)) { 
             if (VDBG) log("  needNetworkFor");
   }
}

4、最后编译烧录,开机之后,通过adb命令发现ping命令只能ping通以太网wifi中的一个时,则输入命令

ip rule add from all lookup main pref 9999

自动将这条路由规则配置好。

其他

cat /proc/net/dev
netcfg eth0 down/up/dhcp
ifconfig eth0 192.168.70.65 netmask 255.255.255.0 up