How to add USB network share in Android system


Test platform: mt2712, Android 9 0,Linux4. nine
Usage scenario:
Android car machine connects T-box, mobile phone and PC through USB cable, and then uses their network to surf the Internet.
In fact, the Android system already supports USB shared network, but the USB shared network has not been turned on in the corresponding product system. At this time, some configurations of USB network sharing need to be added to make the product support this function.

1, Code modification:

1. Add RNDIS support to the kernel

CONFIG_USB_USBNET=y
CONFIG_USB_NET_DM9601=y
CONFIG_USB_NET_SR9700=y
CONFIG_USB_NET_SMSC75XX=y
CONFIG_USB_NET_SMSC95XX=y
+CONFIG_USB_NET_RNDIS_HOST=y

Specific network card name:
Path: kernel-4.9/drivers/net/usb/usbnet.c om c

usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
{
.........................................
	dev->net = net;
	strcpy (net->name, "usb%d");
	printk("usbnet_probe usb");
	memcpy (net->dev_addr, node_id, sizeof node_id);
........................................
	// allow device-specific bind/init procedures
	// NOTE net->name still not usable ...
	if (info->bind) {
		// heuristic:  "usb%d" for links we know are two-host,
		// else "eth%d" when there's reasonable doubt.  userspace
		// can rename the link if it knows better.
		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
		    ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
		     (net->dev_addr [0] & 0x02) == 0))
				strcpy (net->name, "eth%d");
		/* WLAN devices should always be named "wlan%d" */
		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
			strcpy(net->name, "wlan%d");
		/* WWAN devices should always be named "wwan%d" */
		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
			strcpy(net->name, "wwan%d");
		}
	}

The network card name is usbx by default. If it is Ethernet, the name is ethx. If it is WLAN, the name is WLAN X.

2.init. Two configuration services for usb0 are added in RC

Add two configuration services for usb0 to enable the framework to configure the usb0 network card (service is the core of USB network sharing)
On the MT2712 platform, the modified file is init MT2712. ab.emmc. rc

#Start dhcp for usb network allocation
service dhcpcd_usb0 /system/bin/dhcpcd -ABDKL -f/system/etc/dhcpcd/dhcpcd.conf
    class main
    disabled
    oneshot
    seclabel u:r:dhcp:s0
#Start usb network allocation ip
service iprenew_usb0 /system/bin/dhcpcd -n
    class main
    disabled
    oneshot
    seclabel u:r:dhcp:s0

3. Modify configuration in frameworks

Corresponding to the Android source code, add configuration in the frameworks (this is mainly about how to start the network and select the network)
frameworks/base/core/res/res/values/config.xml
The original is:

<string translatable="false" name="config_ethernet_iface_regex">eth\\d</string>

That is, Ethernet is supported
Amend to read:

<string translatable="false" name="config_ethernet_iface_regex">usb\\d</string>

USB shared network is supported.
It can also be modified to support Ethernet and USB shared networks at the same time, and then APP decides which network to use according to the actual situation.

<string translatable="false" name="config_ethernet_iface_regex">eth\\d|usb\\d</string>

config_ ethernet_ iface_ The calling process of regex is as follows:
Path: Frameworks / opt / net / Ethernet / Java / COM / Android / server / Ethernet / Ethernet tracker java

EthernetTracker(Context context, Handler handler) {
    mHandler = handler;

    // The services we use.
    IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
    mNMService = INetworkManagementService.Stub.asInterface(b);
	Get the above settings here config_ethernet_iface_regex Value of
    // Interface match regex.
    mIfaceMatch = context.getResources().getString(
            com.android.internal.R.string.config_ethernet_iface_regex);

    // Read default Ethernet interface configuration from resources
    final String[] interfaceConfigs = context.getResources().getStringArray(
            com.android.internal.R.array.config_ethernet_interfaces);
    for (String strConfig : interfaceConfigs) {
        parseEthernetConfig(strConfig);
    }

    mConfigStore = new EthernetConfigStore();

    NetworkCapabilities nc = createNetworkCapabilities(true /* clear default capabilities */);
    mFactory = new EthernetNetworkFactory(handler, context, nc);
    mFactory.register();
}

    private void maybeTrackInterface(String iface) {
        if (DBG) Log.i(TAG, "maybeTrackInterface " + iface);
        // If we don't already track this interface, and if this interface matches
        // our regex, start tracking it.
        if (!iface.matches(mIfaceMatch) || mFactory.hasInterface(iface)) {
            return;
        }

        if (mIpConfigForDefaultInterface != null) {
            updateIpConfiguration(iface, mIpConfigForDefaultInterface);
            mIpConfigForDefaultInterface = null;
        }
        addInterface(iface);
    }

For Ethernet and USB shared networks, please refer to: https://blog.csdn.net/xcyansmile/article/details/109361979

2, Debug

The Android car is connected with the T-box through USB. The Android car is the Host and the T-box is the Device.
1. After USB connection, input lsusb in Android car to confirm that a new USB device is inserted.

Bus 003 Device 001: ID 1d6b:0002
Bus 001 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 2c7c:0125
Bus 004 Device 001: ID 1d6b:0003
Bus 002 Device 001: ID 1d6b:0003

Before insertion:

Bus 003 Device 001: ID 1d6b:0002
Bus 001 Device 001: ID 1d6b:0002
Bus 004 Device 001: ID 1d6b:0003
Bus 002 Device 001: ID 1d6b:0003

2. Check whether the network card generates ifconfig -a

console:/ # ifconfig -a
ip6tnl0   Link encap:UNSPEC  
          NOARP  MTU:1452  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

p2p0      Link encap:Ethernet  HWaddr 02:76:1c:60:1a:16
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

wlan0     Link encap:Ethernet  HWaddr 00:76:1c:60:1a:16  Driver mtk_sdio_client
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

ip_vti0   Link encap:UNSPEC  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

tunl0     Link encap:UNSPEC  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

ip6_vti0  Link encap:UNSPEC  
          NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:0 TX bytes:0 

usb0      Link encap:Ethernet  HWaddr 36:28:7d:39:88:1b  Driver rndis_host
          inet addr:192.168.225.24  Bcast:192.168.225.255  Mask:255.255.255.0 
          inet6 addr: fe80::c888:d5a1:7f11:5ea0/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:250 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:333 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:72265 TX bytes:70616 

eth0      Link encap:Ethernet  HWaddr 00:55:7b:b5:7d:f7  Driver dwmac-mediatek
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 
          Interrupt:13 Base address:0x4000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet6 addr: ::1/128 Scope: Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:43 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:3860 TX bytes:3860

The network card usb0 is generated here.

3. Confirm whether the networking is successful: ifconfig

console:/ # ifconfig
wlan0     Link encap:Ethernet  HWaddr 00:76:1c:60:1a:16  Driver mtk_sdio_client
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:0 TX bytes:0 

usb0      Link encap:Ethernet  HWaddr 36:28:7d:39:88:1b  Driver rndis_host
          inet addr:192.168.225.24  Bcast:192.168.225.255  Mask:255.255.255.0 
          inet6 addr: fe80::c888:d5a1:7f11:5ea0/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:279 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:361 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:73769 TX bytes:74424 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0 
          inet6 addr: ::1/128 Scope: Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:43 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1 
          RX bytes:3860 TX bytes:3860

Seeing usb0 indicates that the networking is successful, * * RX bytes:73769 TX bytes:74424 "* * indicates the amount of data received and sent.

Note: ensure that the network card name generated in the Linux kernel is consistent with the network card name used in the frameworks.

reference resources: https://blog.csdn.net/xcyansmile/article/details/109361979

Added by snakez on Sat, 22 Jan 2022 12:51:23 +0200