0%

RNDIS

RNDIS网络,一台Linux设备可以作为 usb device,也可以作为 usb host

RNDIS

全称 Remote Network Device Interface Specification

HOST

Symbol: USB_NET_RNDIS_HOST [=y]
    Prompt: Host for RNDIS and ActiveSync devices (EXPERIMENTAL)
      Defined at drivers/net/usb/Kconfig:226
      Depends on: NETDEVICES && USB && NET && USB_USBNET && EXPERIMENTAL
      Location:
        -> Device Drivers
          -> Network device support (NETDEVICES [=y])
            -> USB Network Adapters
              -> Multi-purpose USB Networking Framework (USB_USBNET [=y])
      Selects: USB_NET_CDCETHER
      Selected by: USB_NET_RNDIS_WLAN && NETDEVICES && !S390 && USB && WLAN_80211 && EXPERIMENTAL

Device

Symbol: USB_ETH_RNDIS [=y]
Prompt: RNDIS support
    Defined at drivers/usb/gadget/Kconfig:499
    Depends on: <choice> && USB_ETH
    Location:
    -> Device Drivers
        -> USB support (USB_SUPPORT [=y])
        -> USB Gadget Support (USB_GADGET [=y])
            -> USB Gadget Drivers (<choice> [=y])
            -> Ethernet Gadget (with CDC Ethernet support) (USB_ETH [=y])

RNDIS gadget driver module can be inserted using $ insmod g_ether.ko

You can, alternatively, change the for USB Gadget Drivers to <> and this will compile the drivers into the kernel, so you won’t have to load the module. You will need to first de-select all child modules under USB Gadget Drivers, then press space bar over USB Gadget Drivers to change the to a <>.

The RNDIS Gadget driver will create an Ethernet device by the name usb0. You need to assign an IP address to the device and bring up the device. The typical command for that would be:

ifconfig usb0 <IP_ADDR> netmask 255.255.255.0 up

使用以上命令不能正常上网,使用udhpc来完成IP地址分配

插入 USB Device

[   19.980000] usb 1-1: new high speed USB device using EHCI-NationalChip and address 3
[   20.140000] usb 1-1: configuration #1 chosen from 1 choice
[Hotplug] dev: 1-1
[Hotplug] action: add
[Hotplug] dev: 1-1:1.0
[Hotplug] action: add
[   20.270000] usb0: register 'rndis_host' at usb-EHCI-NationalChip-1, RNDIS device, e6:92:79:c4:b1:cb

查看设备

[root@root /]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 32:11:B7:CC:97:9F
          inet addr:192.168.111.18  Bcast:192.168.111.255  Mask:255.255.254.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5254 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:6781768 (6.4 MiB)  TX bytes:2538 (2.4 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  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:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr E6:92:79:C4:B1:CB
          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 (0.0 B)  TX bytes:0 (0.0 B)

udhpc

[root@root /]# ifconfig usb0 up
[root@root /]# udhcpc -t 20 -T 1 -n -q -i usb0
udhcpc (v1.21.0) started
Setting IP address 0.0.0.0 on usb0
Sending discover...
Sending discover...
Sending discover...
Sending select for 192.168.42.36...
Lease of 192.168.42.36 obtained, lease time 3600
Setting IP address 192.168.42.36 on usb0
Deleting routers
route: SIOCDELRT: No such process
Adding router 192.168.42.129
Recreating /tmp/etc/resolv.conf
 Adding DNS server 192.168.42.129

Ref

  1. Linux下使用USB网络
  2. Linux下使用USB网络