Home Router Guide Mike Frysinger This document details how to turn an old Gentoo machine into a router for connecting your home network to the internet. 1.38 2007-07-27 Introduction


This guide will show you how to setup Network Address Translation (NAT) on the router (kernel and iptables), add and configure common services (Domain Name System (DNS) via dnsmasq, dhcp via dhcpcd, ADSL via ppp), and conclude with more elaborate and fun things that can be done (port forwarding, traffic shaping, proxies/caching, etc...).


The conventions used in this guide are:

  • LAN utilizes the private 192.168.0.xxx network
  • router is hardcoded to the standard IP
Kernel setup (know thyself first)


# ifconfig -a
          BROADCAST MULTICAST  MTU:1500  Metric:1
          collisions:0 txqueuelen:1000 
          Interrupt:11 Base address:0x9800 

          BROADCAST MULTICAST  MTU:1500  Metric:1
          collisions:0 txqueuelen:1000 
          Interrupt:10 Base address:0x9400 

If you do not see your two cards showing up and you're not sure what kind of cards you have, try running lspci | grep Ethernet. You can get that from emerge pciutils. Once you have this information, go into your kernel and add support for the correct drivers.

The next thing you'll need is support for iptables and NAT (and packet shaping if you want). The following list is split up into always required (*), required only for adsl via PPPoE (a), suggested for everyone (x), and only for shaper (s) features. It does not matter whether you build the features into the kernel or as a module so long as when the feature is needed, the correct module(s) are loaded (module loading is left to the reader as a fun exercise however).

Networking options  --->
   [*] TCP/IP networking
      [*] IP: advanced router
   [*] Network packet filtering (replaces ipchains)
If you use 2.4.x, you have to enable the following for DHCP:
   [*] Socket Filtering

   IP: Netfilter Configuration  --->
      [*] Connection tracking (required for masq/NAT)
         [x] FTP protocol support
         [x] IRC protocol support
         [*] IP range match support
         [x] MAC address match support
         [*] Multiple port match support
         [*] Packet filtering
            [*] REJECT target support
            [x] REDIRECT target support
         [*] Full NAT
            [*] MASQUERADE target support
         [s] Packet mangling
            [s] MARK target support
         [x] LOG target support

   QoS and/or fair queueing  --->
      [s] QoS and/or fair queueing
         [s] HTB packet scheduler
         [s] Ingress Qdisc

   [a] PPP (point-to-point protocol) support
      [a] PPP filtering
      [a] PPP support for async serial ports
      [a] PPP support for sync tty ports
      [a] PPP Deflate compression
      [a] PPP BSD-Compress compression
      [a] PPP over Ethernet
Some things may be slightly different in a 2.4 vs 2.6 kernel, but you should be able to figure it out :). Even among 2.6 kernels, these options have a tendency to move around. Good luck!
Hug the WAN (a.k.a. The Internet)



All the fancy PPPoE software that used to be provided by rp-pppoe (Roaring Penguin) has been integrated into the standard PPP package. Simply emerge ppp and you'll be on your way. Remember how I said you'll need username/password information? Well I wasn't lying so I hope you have it now! Load up /etc/conf.d/net in your favorite editor and set it up.

In order for the following net settings to work, you must have baselayout-1.12.9 or later installed on your system.
(Replace 'vla9h924' with your username and 'boogie' with your password)

# nano /etc/conf.d/net
Tell baselayout to use adsl over eth1 for ppp0:
config_ppp0=( "ppp" )
plugins_ppp0=( "pppoe" )
	There may be other settings you want, see /etc/conf.d/net.example

# ln -s net.lo /etc/init.d/net.ppp0
# rc-update add net.ppp0 default
# /etc/init.d/net.ppp0 start
When the DSL interface comes up, it will create ppp0. Although your NIC is called eth1, the IP is actually bound to ppp0. From now on, when you see examples that utilize 'eth1', substitute with 'ppp0'. Make sure you change the permissions of the /etc/conf.d/net file so that only root can read/write it since you're sticking your username/password in it. For people transitioning from the rp-pppoe package, or for people who hit weird connection resets, see the MTU section in the Troubleshooting chapter.
Cable and/or dynamic/static IP


Dynamic IP Users:
# emerge dhcpcd
# nano /etc/conf.d/net
You'll need an entry like so:
config_eth1=( "dhcp" )

Static IP Users:
# nano /etc/conf.d/net
You'll need entries like so:
config_eth1=( " broadcast netmask" )
routes_eth1=( "default gw" )
# nano /etc/resolv.conf
Add one line per DNS server:

Dynamic and Static Setup:
# ln -s net.lo /etc/init.d/net.eth1
# rc-update add net.eth1 default
# /etc/init.d/net.eth1 start

You should be all set to go now.

Hug the LAN (bring along some friends)

This step is a breeze compared to the previous one.

# nano /etc/conf.d/net
Add a line like the following:
config_eth0=( " broadcast netmask" )
# rc-update add net.eth0 default
# /etc/init.d/net.eth0 start
LAN Services (because we're nice people)
DHCP Server


DHCP is exactly what its name implies. It's a protocol that allows you to dynamically configure other hosts automatically. You run a DHCP server on the router, give it all the information about your network (valid IPs, DNS servers, gateways, etc...), and then when the other hosts start up, they run a DHCP client to automatically configure themselves. No fuss, no muss! For more information about DHCP, you can always visit Wikipedia.

We'll use a package called dnsmasq which provides both DHCP and DNS services. For now lets just focus on the DHCP aspect. Note that if you want to run a different DHCP server, you can find another example in the Fun Things chapter. Also, if you wish to tinker with the DHCP server settings, just read the comments in /etc/dnsmasq.conf. All the defaults should work fine though.

# emerge dnsmasq
# nano /etc/dnsmasq.conf
Add this line to enable dhcp:
Restrict dnsmasq to just the LAN interface

# rc-update add dnsmasq default
# /etc/init.d/dnsmasq start


DNS Server

When people want to visit a place on the internet, they remember names, not a string of funky numbers. After all, what's easier to remember, ebay.com or This is where the DNS steps in. DNS servers run all over the internet, and whenever someone wants to visit 'ebay.com', these servers turn 'ebay.com' (what we understand) into '' (what our computers understand). For more information about DNS, you can always visit Wikipedia.

Since we're using dnsmasq for our DHCP server, and it includes a DNS server, you've got nothing left to do here! Your little router is already providing DNS to its DHCP clients. Bet you wish everything was this easy ;).


NAT (a.k.a. IP-masquerading)


This is where Network Address Translation (NAT) steps in. NAT is a way of connecting multiple computers in a private LAN to the internet when you have a smaller number of public IP addresses available to you. Typically you are given 1 IP by your ISP, but you want to let your whole house connect to the internet. NAT is the magic that makes this possible. For more information about NAT, you can always visit Wikipedia.

First we flush our current rules
# iptables -F
# iptables -t nat -F

Setup default policies to handle unmatched traffic
# iptables -P INPUT ACCEPT
# iptables -P OUTPUT ACCEPT
# iptables -P FORWARD DROP

Copy and paste these examples ...
# export LAN=eth0
# export WAN=eth1

Then we lock our services so they only work from the LAN
# iptables -I INPUT 1 -i ${LAN} -j ACCEPT
# iptables -I INPUT 1 -i lo -j ACCEPT
# iptables -A INPUT -p UDP --dport bootps -i ! ${LAN} -j REJECT
# iptables -A INPUT -p UDP --dport domain -i ! ${LAN} -j REJECT

(Optional) Allow access to our ssh server from the WAN
# iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT

Drop TCP / UDP packets to privileged ports
# iptables -A INPUT -p TCP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP
# iptables -A INPUT -p UDP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP

Finally we add the rules for NAT
# iptables -I FORWARD -i ${LAN} -d -j DROP
# iptables -A FORWARD -i ${LAN} -s -j ACCEPT
# iptables -A FORWARD -i ${WAN} -d -j ACCEPT
# iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE
Tell the kernel that ip forwarding is OK

# /etc/init.d/iptables save
# rc-update add iptables default
# nano /etc/sysctl.conf
Add/Uncomment the following lines:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1

If you have a dynamic internet address you probably want to enable this:
net.ipv4.ip_dynaddr = 1


The ip_dynaddr option is useful for dial on demand systems or when your ISP gives out dynamic addresses. This works around the problem where a connection is attempted before the internet interface is fully setup. Really this just provides for a smoother network experience for users behind your router.

Fun Things (for a rainy day)


Port Forwarding


All the port forwarding rules are of the form iptables -t nat -A PREROUTING [-p protocol] --dport [external port on router] -i ${WAN} -j DNAT --to [ip/port to forward to]. Unfortunately, iptables does not accept hostnames when port forwarding. If you are forwarding an external port to the same port on the internal machine, you can omit the destination port. See the iptables(8) man page for more information.

Copy and paste these examples ...
# export LAN=eth0
# export WAN=eth1

Forward port 2 to ssh on an internal host
# iptables -t nat -A PREROUTING -p tcp --dport 2 -i ${WAN} -j DNAT --to

FTP forwarding to an internal host
# iptables -t nat -A PREROUTING -p tcp --dport 21 -i ${WAN} -j DNAT --to

HTTP forwarding to an internal host
# iptables -t nat -A PREROUTING -p tcp --dport 80 -i ${WAN} -j DNAT --to

VNC forwarding for internal hosts
# iptables -t nat -I PREROUTING -p tcp --dport 5900 -i ${WAN} -j DNAT --to
# iptables -t nat -I PREROUTING -p tcp --dport 5901 -i ${WAN} -j DNAT --to

SAMBA forwarding to an internal host (excess ports to cover Windows)
# iptables -t nat -I PREROUTING -p tcp --dport 135 -i ${WAN} -j DNAT --to
# iptables -t nat -I PREROUTING -p tcp --dport 139 -i ${WAN} -j DNAT --to
# iptables -t nat -I PREROUTING -p tcp --dport 445 -i ${WAN} -j DNAT --to
# iptables -t nat -I PREROUTING -p udp --dport 137:138 -i ${WAN} -j DNAT --to
# iptables -t nat -I PREROUTING -p udp --dport 445 -i ${WAN} -j DNAT --to

Bittorrent forwarding
# iptables -t nat -A PREROUTING -p tcp --dport 6881:6889 -i ${WAN} -j DNAT --to

eDonkey/eMule forwarding
# iptables -t nat -A PREROUTING -p tcp --dport 4662 -i ${WAN} -j DNAT --to

Game Cube Warp Pipe support
# iptables -t nat -A PREROUTING -p udp --dport 4000 -i ${WAN} -j DNAT --to

Playstation 2 Online support
# iptables -t nat -A PREROUTING -p tcp --dport 10070:10080 -i ${WAN} -j DNAT --to
# iptables -t nat -A PREROUTING -p udp --dport 10070:10080 -i ${WAN} -j DNAT --to

Xbox Live
# iptables -t nat -A PREROUTING -p tcp --dport 3074 -i ${WAN} -j DNAT --to
# iptables -t nat -A PREROUTING -p udp --dport 3074 -i ${WAN} -j DNAT --to
# iptables -t nat -A PREROUTING -p udp --dport 88 -i ${WAN} -j DNAT --to
Identd (for IRC)


# emerge midentd
# rc-update add midentd default
# /etc/init.d/midentd start


Time Server

Keeping your system time correct is essential in maintaining a healthy system. One of the most common ways of accomplishing this is with the Network Time Protocol (NTP) and the ntp package (which provides implementations for both server and client).


# nano /etc/conf.d/ntp-client
Customize if you wish but the defaults should be fine
# rc-update add ntp-client default

# nano /etc/ntp.conf
Add the follwing lines:
restrict default ignore
restrict mask notrust nomodify notrap
These will allow only ntp clients with an IP
address in the 192.168.0.xxx range to use your ntp server
# nano /etc/conf.d/ntpd
Customize if you wish but the defaults should be fine
# rc-update add ntpd default

# /etc/init.d/ntp-client start
# /etc/init.d/ntpd start
You should make sure that you allow inbound and outbound communication on the ntp port (123/udp) when setting up the server. The client just needs outbound access on port 123 over udp.


# nano /etc/conf.d/ntp-client
# rc-update add ntp-client default
# /etc/init.d/ntp-client start
Rsync Server

For those who run multiple Gentoo boxes on the same lan, you often want to keep from having every machine running emerge sync with remote servers. By setting up a local rsync, you save on both your bandwidth and the Gentoo rsync servers' bandwidth. It's pretty simple to do.

For a much more in-depth rsync guide, please see the official rsync guide.

Since every Gentoo machine requires rsync, theres no need to emerge it. Edit the default /etc/rsyncd.conf config file, uncomment the [gentoo-portage] section, and make sure you add an address option. All the other defaults should be fine.

pid file = /var/run/rsyncd.pid
use chroot = yes
read only = yes
address =

  path = /mnt/space/portage
  comment = Gentoo Linux Portage tree
  exclude = /distfiles /packages

Then you need to start the service (again, the defaults are OK).

# /etc/init.d/rsyncd start
# rc-update add rsyncd default

Only thing left is to set tell your clients to sync against the router.

Mail Server


# emerge netqmail
make sure the output of `hostname` is correct
# emerge --config netqmail
# iptables -I INPUT -p tcp --dport smtp -i ! ${LAN} -j REJECT
# cd /etc/tcprules.d
# nano tcp.qmail-smtp

Add an entry like so to the allow section:

# make
# rc-update add svscan default
# /etc/init.d/svscan start


Full DHCP Server

Earlier we used dnsmasq to provide DHCP service to all our clients. For most people with a simple small LAN, this is perfect. But you may need something with more features. Thus we turn to a full-featured DHCP server as provided by the ISC folks.

# emerge dhcp
# nano /etc/dhcp/dhcpd.conf
(Here is a sample configuration file:)
ddns-update-style interim;
subnet netmask {
    default-lease-time 259200;
    max-lease-time 518400;
    option subnet-mask;
    option broadcast-address;
    option routers;
    option domain-name-servers;
# nano /etc/conf.d/dhcpd
(Set IFACE="eth0")
# rc-update add dhcpd default
# /etc/init.d/dhcpd start

This is the minimal setup required to replace the dnsmasq DHCP functionality that we used earlier. Speaking of which, you did remember to disable the DHCP features in dnsmasq didn't you? If not, you should do so now (just comment out the dhcp-range setting in /etc/dnsmasq.conf and restart the service).

Connect Another LAN (or two or three or ...)

Sometimes you have need of connecting the router to another LAN. Maybe you want to hook up a group of friends temporarily, or you're a neat freak and want to section off different groups of computers, or you're just really really bored. Whatever the reasons, extending the router to other LAN networks should be pretty straightforward. In the following examples, I will assume that this new network is connected via a third ethernet card, namely eth2.

First you need to configure the interface. Just take the instructions in the 4.1 code listing and replace eth0 with eth2 and 192.168.0 with 192.168.1.

Then you need to tweak dnsmasq to service the new interface. Just edit the /etc/conf.d/dnsmasq file again and append -i eth2 to DNSMASQ_OPTS; using -i multiple times is OK. Then edit /etc/dnsmasq.conf and add another line like the dhcp-range line in the 5.1 code listing, replacing 192.168.0 with 192.168.1. Having multiple dhcp-range lines is OK too.

Finally, see the rules in the 5.2 code listing and duplicate the rules that have -i ${LAN} in them. You may want to create another variable, say LAN2, to make things easier.

Useful Tools

If you're having trouble getting your computers to communicate, you may way to try out the following tools (they can all be found in the net-analyzer portage category):

wiresharkGUI tool to view all raw network data according to filterstcpdumpConsole tool to dump all raw network data according to filtersiptrafncurses based IP LAN monitorettercapncurses based network monitor/control
Utility Description
DHCP Fails To Start

When starting the dhcp init.d script for the first time, it may fail to load but neglect to give you any useful info.

# /etc/init.d/dhcp start
 * Setting ownership on dhcp.leases ...          [ ok ]
 * Starting dhcpd ...                            [ !! ]

The trick is to know where dhcpd is sending its output. Simply browse to /var/log and read the log files. Since the exact log file depends on the package you are using as a syslog, try running grep -Rl dhcpd /var/log to narrow down the possibilities. Chances are you made a typo in your config file. You could also try running dhcpd -d -f (short for debug / foreground) and debug the error based upon the output.

Incorrect MTU Value

If you experience odd errors (such as not being able to access some webpages while others load fine), you may be having Path MTU Discovery trouble. The quick way to test is to run this iptables command:


This will affect all new connections, so just refresh the website you're having problems with in order to test. In case it helps, the standard MTU value for 100mbit ethernet connections is 1500; this value also applies to PPPoA. For PPPoE connections it is 1492. For more info, you should read Chapter 15 of the Linux Advanced Routing & Traffic Control HOWTO.

Unable to connect two machines directly

If (for whatever reason) you want to connect two machines directly together without a hub or switch, a regular ethernet cable will likely not work, unless you have an Auto MDI/MDI-X (also known as "autosensing") capable network adapter. You will need a different cable called a crossover cable. This Wikipedia page explains the low level details.

Final Notes
