Documentation:NGW/ConnectingToHomeNetwork

From AVRFreaks Wiki

Jump to: navigation, search

Contents

[edit] Connecting your NGW100 to your home network

This HowTo is merely a collection of Linux tips and tricks to make networking your NGW100 with other PCs in a small network easier.

This article assumes that you are not overly familiar with Linux networking practises and so can be too basic for those more experienced in this area.


[edit] The Easy Way

If your access to the internet is via a router and you find your PC can't connect to the net when you plug your NGW into your PC via ethernet the issue is that the NGW100 is attempting to act as a router for your PC. The issue is that your PC is asking the NGW100 to resolve internet addresses, not your original router.

The quick solution to access your NGW100 via an ethernet connection to your PC whilst leaving your original router as the name resolver for you PC is to disable DNS services on the NGW100.

I've found the fastest way to do this is to rename the startup file which starts up the DNS server on the NGW100.

To do this, change directory to /etc/init.d. At the prompt type:

cd /etc/init.d

then you need to rename the file S15dnsmasq to something else. I'm just adding a tilde at the beginning:

mv S15dnsmasq ~S15dnsmasq


When you reboot the NGW100 you'll find the DNS service is not started and when your PC connects to the NGW100 via the LAN port, the NGW100 will not attempt to act as a Name server for interent addresses.

[edit] Hardware

The NGW100 has two ethernet ports. The one closest to the USB connector is the first one, usually designated as WAN (Wide Area Network), in Linux it is referred to as "eth0".

The second ethernet port, the one physically closest to the serial connection, is designated as LAN (Local Area Network). Within Linux it is termed "eth1".

There is no special requirement for using the ethernet ports on the NGW100 under Linux. They appear as stock standard, normal ethernet adapters to Linux, which they are. As such, if you get stuck, you can Google for normal Linux solutions to network problems and the information should be applicable to the NGW100.

So remember that eth0 is the WAN port and eth1 is the LAN port.

Most of the time you will connect to your local network via the LAN port (eth1)

[edit] Changing IP address

First of all, to talk to the NGW, connect a serial port to the DB9 connector on the board with a straight serial cable (not a null modem cable). When you open your comm software, select 115200,8,N,1,No Flow Control, and set up the terminal emulation as VT100.

The default configuration for the NGW100 is to have eth0 attempt to seek a DHCP assigned address at start up, if it cannot find a DHCP server it will fall back to the address, 10.11.12.1

The LAN port, eth1, will default to an address of 10.0.0.1

To see what IP addresses have been assigned to your NGW100 ethernet ports you use the 'ifconfig' command.

At a terminal connected to the NGW100, type:

"ifconfig -a"

This will display information for both "eth0" and "eth1" as well as "lo". "lo" is a loopback ethernet device, it maps to 127.0.0.1. If you are not familiar with lo do not worry, you can safely ignore its presence as it exists to help locally running programs and does not have a great affect on networking per se.

[edit] My network and why I wanted to change IP address

In my network I use the 192.168.*.* range of addresses. I have a central router than connects several PCs and also has an ADSL connection for Internet access. All my PCs are allocated a dynamic IP address starting at 192.168.1.33 . I use a subnet of 255.255.255.0 and my router/DHCP server is located at 192.168.1.254

I wanted my NGW100 to connect to the router to be seen on the network and be able to access the Internet. To this end I wanted my NGW100 to have an IP in the 192.168.1.* range and use the router at 192.168.1.254 as its gateway.

From now on when I refer to the IP address of the NGW100, I am referring to the IP address of eth1, the LAN port.

In this scenario there are two options available. First one could assign the NGW100 a static IP, for example 192.168.1.37

Secondly one could have the NGW100 ask the existing DHCP server on the network to assign an IP address dynamically. This avoids any conflicting IP addresses on the network.


[edit] Static IP

The process is to shutdown the ethernet port, start it up again with a new IP and then add an entry in the routing table to the network's gateway. The last step is so the NGW100 will be able to see the Internet via the router.

To change the IP of eth1 we again turn to ifconfig.

To shutdown eth1 the command is:

"ifconfig eth1 down"

To start it back up with an new IP of 192.168.1.37 the command is:

"ifconfig eth1 192.168.1.37 netmask 255.255.255.0 up"

If you want to confirm that eth1 has the new address the command is:

"ifconfig -a eth1"

You should be greeted with a reply similar to the screenshot below.

Image:Ngw100_networking_static_ip.png

The NGW100 should now be able to ping other PCs on the network, be accessible via FTP and SMB at the new address etc. It will not however be able to access the Internet. For example it will not be able to access NTP time data to update the date and time.

To remedy this you need to add an entry in the routing table that tells Linux where to find the gateway that allows access to the internet. In the current scenario the gateway is the router and hence is found at 192.168.1.254


To direct the NGW100 to this address execute the following command:

"route add default gw 192.168.1.254"


If you want to see the routing table, execute this command:

"route -n"


The NGW100 should now have access to the Internet. You can test this by running "ping www.google.com" or by updating the date/time of the NGW100 by executing "/etc/init.d/S23ntp" and then "date" to confirm the date/time


[edit] Dynamic IP

A dynamic IP is obtained from the network's DHCP server when the ethernet device first attempts to connect to the network. As such it is usually given an address one higher than the last one issued.

A different program to ifconfig is used to negotiation an address with the DHCP server. There are several DHCP clients used by Linux systems but the NGW100 uses "udhcpc" which comes as part of the Busybox embedded package of utilities.

To allocate a dynamic address the ethernet adaptor must first be shutdown with:

"ifconfig eth1 down"


Then the following command is executed:

"udhcpc -i eth1 -h NGW100"

If you want to use a different host name than "NGW100" then replace NGW100 in the command above with whatever you have called your NGW100 Board.

Hopefully the negotiation for an address was successful and you will be presented with something similar to this: Image:Ngw100_networking_dynamic_ip.png

You may need to set up the gateway entry in the routing table as outlined above if you want Internet access.

[edit] Boot time configuration

This is all well and good, but the settings are lost when you reboot the system. In order to make these permanent, you need to edit the configuration files.

Just for reference, the boot sequence looks like this...

CPU reset and runs the Bootloader.
The bootloader runs and loads the kernel from the internal FLASH.
The kernel runs and starts the init process.
The init process runs starts the /etc/init.d/rcS script.
The rcS script calls a bunch of other scripts to initialize the system and finally start the shell.

The script that we are interested in is S10networking file in the /etc/init.d directory. We will be editing this with the vi editor. I will give detailed commands on how to use the vi editor, because it is a real pain if you are used to a typical GUI editor. Alternatively, you can copy the file off the system (via FTP/SMB/or the SD card) and edit it on a more friendly machine. Be advised that Windows' Notepad is _not_ a good editor to use because it inserts carriage return/linefeed bytes at the end of the line. The Linux shell will barf if the script files contain carriage returns. Either use an editor that can save to the "Unix" format, or get a tool that will strip the CRs out of the file. You should be able to invoke "sed" to clean it up with the following command.

sed 's/.$//' S10Networking

Anyway, back to the networking stuff. The first thing that I noticed is that the WAN port (eth0) will try to get an address via the DHCP client before going with a static IP. The LAN port was always statically configured. The first thing that I did was to change the LAN port to do DHCP, just like the WAN port.

I opened the file with the command "vi /etc/init.d/S10networking". I then used the arrow keys to go down to the "if ${IFCONFIG} eth1 10.11.12.2 " line. Delete next 7 lines with the command "7dd". Then arrow back to the to the "echo -n " * setup eth0 (WAN) ... "" line. You can then yank the next 20 lines into a buffer (essentially vi's clipboard) with the command "20yy". Then arrow down to the bottom before the exit line and paste your copied stuff with a "p".

Now we need to make a few changes so we don't try and reconfigure the WAN port. Do a "yy" and "p" on the STATIC_IP line at the top of the file. You should now have two identical lines. Arrow to the beginning of the first one and press the "i" (for insert) key. Now type in "WAN_" to make the label WAN_STATIC_IP. Repeat the process to make a LAN_STATIC_IP. Press the "ESC" key to escape from insert mode back to command mode. Go over to the IP numbers and delete the appropriate characters with the "x" command. Then insert your desired static IPs with the "i" command.

Go to the first "echo -n " * setup eth0 ... " line and add a (WAN) after the eth0 (arrow to the spot, press "i" and the type (WAN) or whatever). This will make the boot screen a little more informative, since the ports are labeled WAN and LAN on the circuit board.

Scan down to the first place the STATIC_IP label was used. To do this do a forward slash and your search string. So you would type "/STATIC_IP" and press ENTER. This will take you to the first STATIC_IP string below the cursor. Insert a WAN_ before this so that eth0 is using WAN_STATIC_IP. To search again for the same term just do a "/" and press ENTER. This will repeat the last search. There are two WAN_STATIC_IP labels to change.

Then go to the next "echo -n" line ("/echo -n" ENTER) and change this eth0 to a eth1. You can do this by arrowing over so your cursor is on the 0 of eth0. Then press the X key to delete the character. Then press "i1" to insert a 1. You should now have eth1. Press ESC to escape back to command mode.

You need to go through the next 20 lines or so and check to make sure that the commands are using eth1 and LAN_STATIC_IP. You probably also want to change the echo line to have a "LAN" in it somewhere. In the end you will have a S10networking file that looks like this.

[edit] S10networking

#!/bin/sh

UDHCPC=`which udhcpc`
IFCONFIG=`which ifconfig`
AWK=`which awk`

WAN_STATIC_IP="10.11.12.1"
LAN_STATIC_IP="10.11.12.2"

if [ ! -x "${UDHCPC}" ]; then
        echo "[ MISSING ]"
        exit 1
fi

#************************************************** WAN **********************
echo -n " * setup eth0 (WAN) ...          "
if ${UDHCPC} -n -i eth0 > /dev/null 2> /dev/null; then
       if [ -x "${AWK}" ]; then
               IPADDR=`ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`
       else
               IPADDR="N/A"
       fi
       echo "[ DHCP ] ($IPADDR)"
else
       if [ ! -x "${IFCONFIG}" ]; then
               echo "[ MISSING ]"
               exit 1
       fi

       if ${IFCONFIG} eth0 ${WAN_STATIC_IP} netmask 255.255.255.0 > /dev/null 2> /dev/null; then
               echo "[ STATIC ] (${WAN_STATIC_IP})"
       else
               echo "[ FAILED ]"
               exit 1
       fi
fi

#************************************************** LAN **********************
if ${UDHCPC} -n -i eth1 > /dev/null 2> /dev/null; then
	if [ -x "${AWK}" ]; then
		IPADDR=`ifconfig eth1 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`
	else
		IPADDR="N/A"
	fi
	echo "[ OK ] ($IPADDR)"
else
	if [ ! -x "${IFCONFIG}" ]; then
		echo "[ MISSING ]"
		exit 1
	fi

	if ${IFCONFIG} eth1 ${LAN_STATIC_IP} netmask 255.255.255.0 > /dev/null 2> /dev/null; then
		echo "[ STATIC ] (${LAN_STATIC_IP})"
	else
		echo "[ FAILED ]"
		exit 1
	fi
fi

exit 0



[edit] Conclusion

I hope this article has helped and saved time learning how to tweak the network settings on the NGW100 (and learn a little vi!).

Dingo_aus wrote about the run-time configuration of the newtork, and saider72 added the information about the S10networking file. Either of us can be contacted on the www.avrfreaks.net forum.

Note: To once you have changed the IP, you'll need to alter configuration files for all services to allow the new IP address

Personal tools