Android USB tethering相关代码

1. 代码位置

packages/apps/Settings/src/com/android/settings/TetherSettings.java
frameworks/base/services/java/com/android/server/connectivity/Tethering.java
frameworks/base/core/java/android/net/ConnectivityManager.java
frameworks/base/services/core/java/com/android/server/ConnectivityService.java
frameworks/base/services/java/com/android/server/NetworkManagementService.java
system/netd/CommandListener.cpp,TetherController.cpp,NatController.cpp

 

2. 调用流程
packages/apps/Settings/src/com/android/settings/TetherSettings.java
private void setUsbTethering(boolean enabled) {
       ConnectivityManager cm =
           (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
       mUsbTether.setChecked(false);
       if (cm.setUsbTethering(enabled) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
           mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
           return;
       }
       mUsbTether.setSummary("");
}
调用关系
   setUsbTethering(true);
        cm.setUsbTethering(enabled)
           mService.setUsbTethering(enable);   frameworks/base/core/java/android/net/ConnectivityManager.java
                mTethering.setUsbTethering(enable);   frameworks/base/services/core/java/com/android/server/ConnectivityService.java

frameworks/base/services/core/java/com/android/server/connectivity/Tethering.java
setUsbTethering()
    mUsbTetherRequested = true;
    usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
   
 onReceive()   
    if (action.equals(UsbManager.ACTION_USB_STATE)) {
       if (usbConnected && mRndisEnabled && mUsbTetherRequested)
           tetherUsb(true);
               tether(iface)       "Tethering rndis0"
                  sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED)
TetherInterfaceSM(InitialState):  CMD_TETHER_REQUESTED (2)                
                     sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED 1)
                     transitionTo(mStartingState);
TetherInterfaceSM(mStartingState)
                     Tethering.this.configureUsbIface
                        ifcg.setInterfaceUp();
                        mNMService.setInterfaceConfig(iface, ifcg);  INetworkManagementService:networkManagerService.java
                            CommandListener::setcfg()            "CommandListener: Setting iface cfg"    "CommandListener: Trying to bring up rndis0"
                     sendTetherStateChangedBroadcast();
                           "Tethering: sendTetherStateChangedBroadcast 0, 1, 0"
                     transitionTo(mTetheredState);
TetherInterfaceSM(mTetheredState)
                     mNMService.tetherInterface(mIfaceName);
                         tetherInterface()   networkManagerService.java
                            CommandListener: TetherCmd::runCommand. argc: 4. argv[0]: tether
                                 TetherController::tetherInterface()    TetherController.cpp
                                     addV6RtrAdvIface()
                                     applyDnsInterfaces()
                     "Tethering: Tethered rndis0"

TetherMasterSM(InitialState): CMD_TETHER_MODE_REQUESTED(1)
                      transitionTo(mTetherModeAliveState);
TetherMasterSM(TetherModeAliveState)
                      turnOnMasterTetherSettings();
                            mNMService.setIpForwardingEnabled(true);
                                setIpFwdEnabled()  TetherController.cpp
                                     echo 1 > /proc/sys/net/ipv4/ip_forward
                                     echo 2 > /proc/sys/net/ipv6/conf/all/proxy_ndp
                                     echo 2 > /proc/sys/net/ipv6/conf/all/forwarding
                            mNMService.startTethering(mDhcpRange);
                                TetherCmd::runCommand. argc: 18. argv[0]: tether   CommandListener.cpp
                                     TetherController::startTethering   "Starting tethering services"
                                          exec("/system/bin/dnsmasq")
                                             "dnsmasq : started, version 2.51 cachesize 150"
                            mNMService.setDnsForwarders(mDefaultDnsServers);
                                TetherController:setDnsForwarders     "Sending update msg to dnsmasq"
                                     update_dns:8.8.8.8:8.8.4.4
                                     update_dns:123.123.123.123:123.123.123.124
                      chooseUpstreamType(mTryCell);
                            "chooseUpstreamType(true), preferredApn =5, got type=0"
                            notifyTetheredOfNewUpstreamIface
                                 "notifying tethered with iface =rmnet0"
                                 sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED)
TetherInterfaceSM(TetheredState) CMD_TETHER_CONNECTION_CHANGED                 
                      mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
                           "Enabling NAT - Tethered Iface = rndis0 newUpstreamIfaceName =rmnet0"
                               NatController: enableNat(intIface=<rndis0>, extIface=<rmnet0>)
                                   /system/bin/iptables -t nat -A natctrl_nat_POSTROUTING -o rmnet0 -j MASQUERADE
                                   /system/bin/iptables -A natctrl_FORWARD -i rmnet0 -o rndis0 -m state --state ESTABLISHED,RELATED -g natctrl_tether_counters
                                   /system/bin/iptables -A natctrl_FORWARD -i rndis0 -o rmnet0 -m state --state INVALID -j DROP
                                   /system/bin/iptables -A natctrl_FORWARD -i rndis0 -o rmnet0 -g natctrl_tether_counters
                                   /system/bin/iptables -A natctrl_tether_counters -i rndis0 -o rmnet0 -j RETURN
                                   /system/bin/iptables -A natctrl_tether_counters -i rmnet0 -o rndis0 -j RETURN
                                   /system/bin/iptables -D natctrl_FORWARD -j DROP
                                   /system/bin/iptables -A natctrl_FORWARD -j DROP

system/netd/CommandListener.cpp
setcfg
  ifc_up()   system/core/libnetutils/ifc_utils.c
     ifc_set_flags(name, IFF_UP, 0);
        ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr)

 

3.dnsmasq正常log

D CommandListener: TetherCmd::runCommand. argc: 16. argv[0]: tether                              
D TetherController: Starting tethering services                                                  
D TetherController: Sending update msg to dnsmasq [update_ifaces:rndis0]                         
D TetherController: Sending update msg to dnsmasq [update_dns:8.8.8.8:8.8.4.4]                   
I dnsmasq : started, version 2.51 cachesize 150                                                  
I dnsmasq : compile time options: no-IPv6 GNU-getopt no-DBus no-I18N DHCP no-scripts no-TFTP     
W dnsmasq : warning: no upstream servers configured                                              
I dnsmasq : DHCP, IP range 192.168.48.2 -- 192.168.48.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.47.2 -- 192.168.47.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.46.2 -- 192.168.46.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.45.2 -- 192.168.45.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.44.2 -- 192.168.44.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.43.2 -- 192.168.43.254, lease time 1h                         
I dnsmasq : DHCP, IP range 192.168.42.2 -- 192.168.42.254, lease time 1h                         
I dnsmasq : read /etc/hosts - 1 addresses                                                        
I dnsmasq : using nameserver 8.8.4.4#53                                                          
I dnsmasq : using nameserver 8.8.8.8#53                                                          
D TetherController: Sending update msg to dnsmasq [update_dns:221.130.33.52:221.130.33.60]       
I dnsmasq : using nameserver 221.130.33.60#53                                                    
I dnsmasq : using nameserver 221.130.33.52#53                                                    
I dnsmasq : DHCPREQUEST(rndis0) 192.168.42.5 02:19:03:66:78:76                                   
I dnsmasq : DHCPACK(rndis0) 192.168.42.5 02:19:03:66:78:76 pcname                                
W dnsmasq : Ignoring domain corpusers.net for DHCP host name pcname                              
I dnsmasq : DHCPREQUEST(rndis0) 192.168.42.5 02:19:03:66:78:76                                   
I dnsmasq : DHCPACK(rndis0) 192.168.42.5 02:19:03:66:78:76 pcname                                
W dnsmasq : Ignoring domain corpusers.net for DHCP host name pcname                              
I dnsmasq : DHCPINFORM(rndis0) 192.168.42.5 02:19:03:66:78:76                                    
I dnsmasq : DHCPACK(rndis0) 192.168.42.5 02:19:03:66:78:76 pcname                                 

 

4. Debug方法
 
4.1. 手机端运行ip r l/netcfg检查rmnet0, rndis0是否up;如rndis0没有up,检查ifc_up()函数及usb kernel part
4.2. PC端运行ipconfig检查是否从手机分配到ip address(关闭PC端的其他网络,如局域网/WLAN);如没有检查进程dnsmasq是否启动

你可能感兴趣的:(android,usb,tetherin)