Ethernet over USB

From Qi-Hardware
Jump to: navigation, search


Recent uboot/kernel images have Ethernet-over-USB support enabled by default, which you can use to give the NanoNote a connection to the Internet via another Internet-connected computer such as a desktop or laptop PC, referred to as the "host" in this document.

[edit] How It Works

This built-in networking support is provided by a gadget driver which lets the NanoNote advertise certain kinds of USB endpoints through its USB Peripheral port. These endpoints permit communications of specific kinds such as the "CDC Ethernet" mode described here, a more conventional serial communications mode, USB Mass Storage support (so that the NanoNote can behave like a USB flash drive), and also a low-level raw mode that exposes USB bulk data transfers. The Linux kernel configuration interface - part of the image building process - exposes these different gadget drivers and will let you either build them into the kernel image itself or provide them as separate modules.

[edit] Setup on the Host

To let your NanoNote use your host's Internet connection over USB, you have to set up a network interface for the USB communication and enable NAT and routing on your host. You can then set a default route on your NanoNote via your host.

In this document, we assume that usb0 will be the interface used by the host to communicate with the NanoNote.

[edit] Preventing Interface Conflicts

On some systems there are programs which attempt to manage the different network interfaces, and this can get in the way of the instructions below.

If you are using Debian/Ubuntu with NetworkManager installed you may need to edit /etc/NetworkManager/NetworkManager.conf, making sure the managed=false setting is applied to the usb0 interface.

On some systems, however, there is no /etc/NetworkManager/NetworkManager.conf file, but you may be able to prevent interference with the interface by NetworkManager by using the following declaration in /etc/network/interfaces:

iface usb0 inet manual

This by itself can be used to reserve the interface for manual configuration.

If you use Ubuntu, you might consider removing the default interface management package, replacing it with wicd, if only to avoid the above configuration modifications because wicd ignores usb0. However, it can still be useful to make configuration adjustments and this is covered below.

[edit] Network Configuration

The NanoNote's IP address is set to by default.

On your Linux/Unix host run...

$ ifconfig usb0 give your host an IP address which is in the same subnet as your NanoNote.

To review that, you can use ifconfig:

$ ifconfig usb0

You should get the following:

usb0      Link encap:Ethernet  HWaddr 16:90:89:ea:82:6f  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::1490:89ff:feea:826f/64 Scope:Link
          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)

To verify the proper connection, you can try to ping your NanoNote from your host and vice versa (note: NanoNote has the 101 in the end).

$ ping # pinging your NanoNote from your host

Now we have successfully established a network connection between host and NanoNote.

To let your NanoNote use your host's Internet connection, we have to enable routing and NAT on the host's side.

[edit] NAT Configuration

To switch on forwarding, run the following command as root:

# echo "1" > /proc/sys/net/ipv4/ip_forward

If this fails, you can make sure that you have NAT support enabled by running the following command:

# modprobe iptable_nat

Now, instruct the networking system to forward traffic to the Internet:

# eth0 in this example is the interface on your host connected to the internet, adjust if needed
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

[edit] Editing the Host's Network Configuration

Add the following to your /etc/network/interfaces file in order to be able to use ifup usb0 and ifdown usb0 and to implement the above network and NAT configuration in a more permanent fashion:

allow-hotplug usb0
iface usb0 inet static
       up iptables -A POSTROUTING -t nat -j MASQUERADE -s
       up sysctl -w net.ipv4.ip_forward=1
       up route add -host usb0
       down iptables -D POSTROUTING -t nat -j MASQUERADE -s

This is optional if you want to manage the usb0 interface manually or using your own scripts. For such purposes, the following definition should suffice:

iface usb0 inet manual

[edit] Setup on the NanoNote

[edit] Network Configuration

This is no longer required after OpenWrt Release 2010-12-14

Log into your NanoNote, either with...

$ telnet

...or, if you already set a root password on your NanoNote, with...

$  ssh root@

Then issue the following commands:

$ route add default gw # set a route on your NanoNote through your host
$ echo "nameserver" >> /etc/resolv.conf

In this example we use Google's nameserver ( but you can use any you like.

We're done now! :)

You may want to verify your setup by connecting an outer server by your NanoNote with pinging e.g.

$ ping

You should see something like this:

PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=57 time=44.2 ms
64 bytes from ( icmp_seq=2 ttl=57 time=44.1 ms

And "voilà"! your Ben is now connected to the evil Internet. :)

[edit] Transfer Files

Using scp:

$ scp PC_FILE root@
$ scp root@ ./

Using sftp:

$ sftp root@
sftp> get FILE_NAME

using sshfs, like mount a disk to your pc.

$ sshfs root@ /mountpoint
$ fusermount -u /mountpoint

To make this work with the dropbear SSH server:

$ opkg install gesftpserver
$ ln -s /usr/bin/gesftpserver /usr/libexec/sftp-server
$ opkg install openssh-sftp-server

I had file corruption using gesftpserver - r228-1. Maybe it was unrelated, maybe not.

[edit] Automatic Script Files

[edit] UDEV

  1. Save the following as /etc/udev/rules.d/72-BenNanoNote-net.rules:
    SUBSYSTEM=="usb", ACTION=="add", ATTR{idVendor}=="0525", ATTR{idProduct}=="a4a1", RUN+="/usr/local/bin/"
  2. Save the following as /usr/local/bin/, and don't forget to issue chmod +x /usr/local/bin/!
    if ( /usr/bin/lsusb -t -d "0525:a4a1" ); then
            echo .
            echo "Ben NanoNote found, setting up USB network ... "
            if !( /sbin/lsmod | grep 'ip_tables' ) && ( /sbin/modprobe -l ip_tables ); then
            	/sbin/modprobe ip_tables
            	echo "ip_tables is now loaded"
                	echo "ip_tables already loaded"
            if ( grep '0' /proc/sys/net/ipv4/ip_forward ); then
            	echo "1" > /proc/sys/net/ipv4/ip_forward
            	echo "IP forwarding is now enabled"
                	echo "IP forwarding already enabled"
            if !( /sbin/iptables -L -t nat -v | grep $GATEWAY_IF ); then
            	/usr/sbin/iptables -t nat -A POSTROUTING -o $GATEWAY_IF -j MASQUERADE
            	echo "Routing is now enabled"
                	echo "Routing already setup on "$GATEWAY_IF
            /sbin/ifconfig usb0 netmask
  3. Reboot, or log in as root and run udevadm control --reload-rules
  4. Connect your Nanonote, observe the magic!

Note this will not work when the Nanonote is connected to the PC at boot time, only if you plug it in while udev is running!

[edit] Static Mac Address

This can by done by add the following text to the kernel command line:


(See also issues_7.)

If your rootfs has mtd.nn you can using these commands:

mtd.nn fw_setenv_default
fw_setenv bootargs   g_ether.host_addr=72:8d:1f:c4:e8:ca mem=32M console=tty0 console=ttyS0,57600n8 ubi.mtd=2 rootfstype=ubifs root=ubi0:rootfs rw rootwait

If your rootfs doesn't have mtd.nn, please run these commands before you change the default bootargs:

fw_setenv bootargs mem=32M console=tty0 console=ttyS0,57600n8 ubi.mtd=2 rootfstype=ubifs root=ubi0:rootfs rw rootwait
fw_setenv bootcmd nand read 0x80600000 0x400000 0x200000\;bootm
fw_setenv bootargsfromsd mem=32M console=tty0 console=ttyS0,57600n8 rootfstype=ext2 root=/dev/mmcblk0p1 rw rootwait
fw_setenv bootcmdfromsd mmc init\; ext2load mmc 0 0x80600000 /boot/uImage\; bootm
then :
fw_setenv bootargs g_ether.host_addr=72:8d:1f:c4:e8:ca mem=32M console=tty0 console=ttyS0,57600n8 ubi.mtd=2 rootfstype=ubifs root=ubi0:rootfs rw rootwait 

Always check mtd.nn for the latest default bootloader ENVs.

[edit] authorized_keys

Keys for the root user go into /etc/dropbear/authorized_keys, not into /root/.ssh.

[edit] sftp

$ opkg update
$ opkg install openssh-sftp-server  (works with dropbear)

[edit] Issues

Rebooting BEN or SIE and makes ssh connections freeze and I need to make a new connection, why?

The issue is on the host, as some popular distros will try to automatically set up a new usb0 interface even after you configured it. By adding...

iface usb0 inet manual /etc/network/interfaces, then NetworkManager daemon won't try to assign an IP with dhcp or avahi.

Personal tools