Monday, March 22, 2010

Internet over USB-OTG on BeagleBoard using NAT


 Firefox working in BeagleBoard


NAT stands for Network Address Translation. This concept allows to route a packet with changed address header so that the packet can be transported from one network to another.

Here is how it works for BeagleBoard.

Power up the BeagleBoard and connect it to your host computer using USART using minicom.
once you have logged in, enable g_ether.ko driver by

# cd /lib/modules/2.6.29-omap1/kernel/drivers/usb/gadget/
# insmod g_ether.ko

Now the driver is enabled. Now connect the USB-OTG cable between beagleboard and host computer. Configure the connection as follows:-

# ifconfig usb0 192.168.1.1 netmask 255.255.255.0
# route add default gw 192.168.1.5
# vi /etc/resolv.conf
    Add hostIP as nameserver, your hostIP will be 192.168.1.5 as explained later
    -> nameserver 192.168.1.5

# export http_proxy="http://192.168.36.204:8080"

the last step set the variable to our local proxy and port number , change this according to the connection on your host machine.

On the Host computer:-

# ifconfig usb0 192.168.1.5 netmask 255.255.255.0
# sudo vi /etc/sysctl.conf
   uncomment the line   net.ipv4.ip_forward=1 , to enable forwarding of packets.
# sudo sysctl -p
# sudo cat /proc/sys/net/ipv4/ip_forward
   to check that the change has been made and routing is enabled
# sudo iptables --table nat --append POSTROUTING --out-interface eth0 -j  MASQUERADE
# sudo iptables --append FORWARD --in-interface usb0 -j ACCEPT


This will establish the connection of beagleboard to the internet. Test the connection using ping command.

Now you can realise the power of opkg and install the packages, driver directly from the repositories

7 comments:

  1. Great post..! It was really helpful! thanx a lot!

    ReplyDelete
  2. After i did all you said, I am not able to ping external URLs. I can successfully ping 192.168.1.5 from beagleboard, and 192.168.1.1 from the host. Also I am able to scp files from the host to the board, but not in the other way. Please help to get the internet working on my board.
    Thanks,

    ReplyDelete
  3. Hi Shubham,
    I am vipul from Bangalore. We have been experimenting with beagleboard since quite few days. So far, we could compile android source & have got bootable SD card. The next thing that we want to try is connect beagleboard to our laptop & possibly have ssh tunnel established to BB from host machine (ubuntu). Please, let me know how can this be done? Can u share contact detail (email/cell) so that i can get in touch with you.
    -Vipul (vip04pat@yahoo.co.in)

    ReplyDelete
  4. This is how I got it all to work, and what I have to do after every reboot:

    ON BEAGLEBOARD run the following:
    modprobe g_ether
    [Now insert the USB cable to your Linux computer]

    ON COMPUTER run:

    [The following commands assumes port usb0 is used and that eth1 (in my case my WLAN) is used. Since I'm using a router which has the subnet 192.168.1.* , I can't use this subnet, which is why I use subnet 192.168.2.*]

    sudo ifconfig usb0 192.168.2.5 netmask 255.255.255.0
    sudo iptables --table nat --append POSTROUTING --out-interface eth1 -j MASQUERADE
    sudo iptables --append FORWARD --in-interface usb0 -j ACCEPT

    ON BEAGLEBOARD run:
    [nameserver ip is the default route of the Internet you get to your computer, in my case through a router. The http proxy ip is the ip my computer get its internet from.]

    ifconfig usb0 192.168.2.10 netmask 255.255.255.0
    route add default gw 192.168.2.5
    vi /etc/resolv.conf
    [paste the following and save file] nameserver 192.168.1.1
    export http_proxy="http://192.168.1.2:80"


    Now everything should hopefully work. If you can ping your computer but not, ex www.google.com, you should try to ping www.google.com from your computer to get on of google's IPs. Then ping that IP from beagleboard, if that works you know it's just something wrong with your nameserver config above...

    //Ola

    ReplyDelete
  5. Great ! Thank you !
    Xav

    ReplyDelete
  6. Guys, please help
    when I use this command (on computer):
    sudo iptables --append FORWARD --in-interface usb0 -j ACCEPT

    I get
    iptables: No chain/target/match by that name.

    ReplyDelete
  7. Whenever I try to run the "sudo ifconfig usb0 192.168.2.5 netmask 255.255.255.0" on the beagle board it keeps telling me:
    "SIOCSIFADDR: Permission denied
    SIOCSIFFLAGS: Permission denied
    SIOCSIFNETMASK: Permission denied"
    how do I fix this?

    ReplyDelete