Building your own router out of old spare parts has many advantages over buying a pre-made canned router by say Linksys. The biggest one by far is control over the connection. The other advantages are left up to your imagination; just about anything can be done in this scenario, it's just a matter of needing it.
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 rp-pppoe), and conclude with more elaborate and fun things that can be done (port forwarding, traffic shaping, proxies/caching, etc...).
Before getting started, there's a few basic requirements you must meet. First, you'll need a computer that has at least 2 Network Interface Cards (NICs) in it. Next, you'll need the configuration settings for your internet connection (may include things like IP/DNS/Gateway/username/password). Finally, you'll need a bit of spare time and some Gentoo loving.
The conventions used in this guide are:
Your kernel needs to have the drivers running for both your NICs. To see if
your cards are already setup, just run
# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:60:F5:07:07:B8
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)
Interrupt:11 Base address:0x9800
eth1 Link encap:Ethernet HWaddr 00:60:F5:07:07:B9
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)
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
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 required (*), suggested (x), and 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 tables support (required for filtering/masq/NAT)
[*] 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
There are many ways to connect to the internet so I'll just cover the ones I'm familiar with. That leaves us with ADSL (PPPoE) and cable modems (static/dynamic). If there are other methods out there, feel free to write up a little blurb and e-mail me. Feel free to skip any of the following sections in this chapter that don't apply to you. This chapter is just about getting the router connected to the internet via eth1.
All the fancy PPPoE software has been bundled up into one little nice package
nowadays called
(Replace 'vla9h924' with your username and 'password' with your password) # nano /etc/ppp/pppoe.conf# Ethernet card connected to ADSL modem ETH=eth1# ADSL user name. USER=vla9h924 # nano /etc/ppp/pap-secrets# client server secret "vla9h924" * "password" # nano /etc/conf.d/netAdd an entry for config_eth1 and set it to adsl: config_eth1=( "adsl" ) # ln -s net.eth0 /etc/init.d/net.eth1 # rc-update add net.eth1 default # /etc/init.d/net.eth1 start
If you have a static IP then you will need a few more details than if you have a dynamic IP. For static users, you will need your IP, gateway, and DNS servers.
Dynamic IP Users: # emerge dhcpcd # nano /etc/conf.d/netYou'll need an entry like so: config_eth1=( "dhcp" )Static IP Users: # nano /etc/conf.d/netYou'll need entries like so: ifconfig_eth1=( "66.92.78.102 broadcast 66.92.78.255 netmask 255.255.255.0" ) routes_eth1=( "default gw 66.92.78.1" ) # nano /etc/resolv.confAdd one line per DNS server: nameserver 123.123.123.123Dynamic and Static Setup: # ln -s net.eth0 /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.
This step is a breeze compared to the previous one.
# nano /etc/conf.d/netAdd a line like the following: ifconfig_eth0=( "192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0" ) # rc-update add net.eth0 default # /etc/init.d/net.eth0 start
I bet it'd be nice if everyone else in your house could just plug their computers into the network and things would just work. No need to remember mind-numbing details or make them stare at confusing configuration screens! Life would be grand eh? Introducing the Dynamic Host Configuration Protocol (DHCP) and why you should care.
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 (dhcpd), 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
# emerge dhcp # nano /etc/dhcp/dhcpd.conf(Here is a sample configuration file:) authoritative; ddns-update-style interim; subnet 192.168.0.0 netmask 255.255.255.0 { range 192.168.0.100 192.168.0.250; default-lease-time 259200; max-lease-time 518400; option subnet-mask 255.255.255.0; option broadcast-address 192.168.0.255; option routers 192.168.0.1; option domain-name-servers 192.168.0.1; } # nano /etc/conf.d/dhcp(Set IFACE="eth0") # rc-update add dhcp default # /etc/init.d/dhcp start
Now your little router is a bona-fide DHCP server! Plugin those computers and
watch them work! With Windows systems you should go into the TCP/IP Properties
and select the 'Obtain an IP address automatically' and 'Obtain DNS server
address automatically' options. Sometimes the changes aren't instantaneous, so
you may have to run a command prompt and run
When people want to visit a place on the internet, they remember names, not a
string of useless numbers. After all, what's easier to remember, ebay.com or
66.135.192.87? 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 '66.135.192.87' (what our computers
understand). For more information about DNS, you can always visit
You may have noticed in the previous section that we told the DHCP clients we have a DNS server at 192.168.0.1. You may also remember that 192.168.0.1 is our little router that we're making. I don't remember setting up a DNS server ... so let's do so now!
# emerge dnsmasq # nano /etc/conf.d/dnsmasqAdd "-i eth0" to DNSMASQ_OPTS # rc-update add dnsmasq default # /etc/init.d/dnsmasq start
Well that was quick, but what did we do? The great thing is, we didn't have to do very much! You're welcome to choose other DNS servers if you're more comfortable with them, but the reason dnsmasq is great is because it was designed to do exactly what we want and nothing more. It's a little DNS caching/forwarding server for local networks. We're not looking to provide DNS for our own domain here, just offer simple DNS services to everyone else on our LAN.
At this point, people on your network can talk to each other and they can look up hostnames via DNS, but they still can't actually connect to the internet. While you may think that's great (more bandwidth for you!), I bet they're not too happy just yet.
This is where NAT steps in. NAT is a way of connecting multiple computers in a
private LAN to the internet when you only have a smaller number of IP addresses
availabe to you. Typically you were 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
First we flush our current rules # iptables -F # iptables -t nat -FThen we lock our services so they only work from the LAN # iptables -I INPUT 1 -i eth0 -j ACCEPT # iptables -I INPUT 1 -i lo -j ACCEPT # iptables -A INPUT -p UDP --dport bootps -i ! eth0 -j REJECT # iptables -A INPUT -p UDP --dport domain -i ! eth0 -j REJECT(Optional) Allow access to our ssh server from the WAN # iptables -A INPUT -p TCP --dport ssh -i eth1 -j ACCEPTDrop TCP / UDP packets to privileged ports # iptables -A INPUT -p TCP -i ! eth0 -d 0/0 --dport 0:1023 -j DROP # iptables -A INPUT -p UDP -i ! eth0 -d 0/0 --dport 0:1023 -j DROPFinally we add the rules for NAT # iptables -I FORWARD -i eth0 -d 192.168.0.0/255.255.0.0 -j DROP # iptables -A FORWARD -i eth0 -s 192.168.0.0/255.255.0.0 -j ACCEPT # iptables -A FORWARD -i eth1 -d 192.168.0.0/255.255.0.0 -j ACCEPT # iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADETell the kernel that ip forwarding is OK # echo 1 > /proc/sys/net/ipv4/ip_forward # for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; doneThis is so when we boot we don't have to run the rules by hand # /etc/init.d/iptables save # rc-update add iptables default # nano /etc/sysctl.confAdd/Uncomment the following lines: net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 1
Once you've typed out all of that, the rest of your network should now be able to use the internet as if they were directly connected themselves.
Believe it or not, you're done :). From here on out, I'll cover a bunch of common topics that may interest you. Everything in this chapter is completely optional.
Sometimes you would like to be able to host services on a computer behind the router, or just to make your life easier when connecting remotely. Perhaps you want to run a FTP, HTTP, SSH, or VNC server on one or more machines behind your router and be able to connect to them all. The only caveat is that you can only have one service/machine combo per port. For example, there is no practical way to setup three FTP servers behind your router and then try to connect to them all through port 21; only one can be on port 21 while the others would have to be on say port 123 and port 567.
All the port forwarding rules are of the form
Forward port 2 to ssh on an internal host # iptables -t nat -A PREROUTING -p tcp --dport 2 -i eth1 -j DNAT --to 192.168.0.2:22FTP forwarding to an internal host # iptables -t nat -A PREROUTING -p tcp --dport 21 -i eth1 -j DNAT --to 192.168.0.56HTTP forwarding to an internal host # iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.0.56VNC forwarding for internal hosts # iptables -t nat -I PREROUTING -p tcp --dport 5900 -i eth1 -j DNAT --to 192.168.0.2 # iptables -t nat -I PREROUTING -p tcp --dport 5901 -i eth1 -j DNAT --to 192.168.0.3:5900If you want to VNC in to 192.168.0.3, then just add ':1' to the router's hostname Bittorrent forwarding # iptables -t nat -A PREROUTING -p tcp --dport 6881:6889 -i eth1 -j DNAT --to 192.168.0.2Game Cube Warp Pipe support # iptables -t nat -A PREROUTING -p udp --dport 4000 -i eth1 -j DNAT --to 192.168.0.56Playstation2 Online support # iptables -t nat -A PREROUTING -p tcp --dport 10070:10080 -i eth1 -j DNAT --to 192.168.0.11 # iptables -t nat -A PREROUTING -p udp --dport 10070:10080 -i eth1 -j DNAT --to 192.168.0.11
Internet Relay Chat utilizes the ident service pretty heavily. Now that the
IRC clients are behind the router, we need a way to host ident for both the
router and the clients. One such server has been created called
# emerge midentd # rc-update add midentd default # /etc/init.d/midentd start
There are a few other ident servers in portage. Depending on your needs, I
would recommend checking out
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).
Many people run ntp clients on their computers. Obviously, the more clients in
the world, the larger the load the ntp servers need to shoulder. In
environments like home networks though, we can help keep the load down on
public servers while still providing the proper time to all our computers. As
an added bonus, our private updates will be a lot faster for the clients too!
All we have to do is run a ntp server on our router that synchronizes itself
with the public internet servers while providing the time to the rest of the
computers in the network. To get started, simply
# nano /etc/conf.d/ntp-clientCustomize if you wish but the defaults should be fine # rc-update add ntp-client default # nano /etc/ntp.confAdd the follwing lines: restrict default ignore restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrapThese 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/ntpdCustomize 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
Now, on your clients, have them
# nano /etc/conf.d/ntp-clientChange the 'pool.ntp.org' server in the NTPCLIENT_OPTS variable to '192.168.0.1' # rc-update add ntp-client default # /etc/init.d/ntp-client start
Sometimes it's nice to run your own Simple Mail Transfer Protocol (SMTP) server on the router. You may have your own reason for wanting to do so, but I run it so that the users see mail as being sent instantly and the work of retrying/routing is left up to the mail server. Some ISPs also don't allow for mail relaying for accounts that aren't part of their network (like Verizon). Also, you can easily throttle the delivery of mail so that large attachments won't seriously lag your connection for half an hour.
# emerge qmailmake sure the output of `hostname` is correct # ebuild /var/db/pkg/*-*/qmail-1.03-r*/*.ebuild config # iptables -I INPUT -p tcp --dport smtp -i ! eth0 -j REJECT # ln -s /var/qmail/supervise/qmail-send /service/qmail-send # ln -s /var/qmail/supervise/qmail-smtpd /service/qmail-smtpd # cd /etc # nano tcp.smtpAdd an entry like so to the allow section: 192.168.0.:allow,RELAYCLIENT="" # tcprules tcp.smtp.cdb rules.tmp < tcp.smtp # rc-update add svscan default # /etc/init.d/svscan start
I'm a huge fan of qmail, but you're free to use a different mta :). When you
setup e-mail on the hosts in your network, tell them that their SMTP server is
192.168.0.1 and everything should be peachy. You might want to visit the
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
| Utility | Description |
|---|---|
If you experience odd errors (such as not being 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:
# iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
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
I have no final notes other than if you experience any troubles with the guide,
please contact