/[gentoo]/xml/htdocs/doc/en/home-router-howto.xml
Gentoo

Contents of /xml/htdocs/doc/en/home-router-howto.xml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (hide annotations) (download) (as text)
Mon Aug 21 10:47:36 2006 UTC (8 years, 2 months ago) by vapier
Branch: MAIN
Changes since 1.49: +15 -5 lines
File MIME type: application/xml
add a small section on ip_dynaddr for Kristian

1 vapier 1.1 <?xml version='1.0' encoding='UTF-8'?>
2     <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
3 vapier 1.50 <!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/home-router-howto.xml,v 1.49 2006/08/21 10:21:57 vapier Exp $ -->
4 vapier 1.1
5 vapier 1.36 <guide link="/doc/en/home-router-howto.xml" lang="en">
6 vapier 1.1 <title>Home Router Guide</title>
7    
8     <author title="Author">
9     <mail link="vapier@gentoo.org">Mike Frysinger</mail>
10     </author>
11    
12     <abstract>
13     This document details how to turn an old Gentoo machine into a router
14     for connecting your home network to the internet.
15     </abstract>
16    
17 vapier 1.36 <!-- The content of this document is released into the public domain -->
18     <license/>
19    
20 vapier 1.50 <version>1.31</version>
21     <date>2006-08-21</date>
22 vapier 1.1
23     <chapter>
24     <title>Introduction</title>
25     <section>
26     <body>
27    
28     <p>
29 neysx 1.23 Building your own router out of old spare parts has many advantages over buying
30     a pre-made canned router by say Linksys. The biggest one by far is control
31     over the connection. The other advantages are left up to your imagination;
32     just about anything can be done in this scenario, it's just a matter of needing
33     it.
34 vapier 1.1 </p>
35    
36     <p>
37 neysx 1.23 This guide will show you how to setup Network Address Translation (NAT) on the
38     router (kernel and iptables), add and configure common services (Domain Name
39     System (DNS) via dnsmasq, dhcp via dhcpcd, ADSL via rp-pppoe), and conclude
40     with more elaborate and fun things that can be done (port forwarding, traffic
41     shaping, proxies/caching, etc...).
42 vapier 1.1 </p>
43    
44     <p>
45 neysx 1.23 Before getting started, there's a few basic requirements you must meet. First,
46     you'll need a computer that has at least 2 Network Interface Cards (NICs) in
47     it. Next, you'll need the configuration settings for your internet connection
48     (may include things like IP/DNS/Gateway/username/password). Finally, you'll
49     need a bit of spare time and some Gentoo loving.
50 vapier 1.1 </p>
51    
52     <p>
53     The conventions used in this guide are:
54     </p>
55 neysx 1.23
56 vapier 1.1 <ul>
57     <li>eth0 - NIC connected to the Local Area Network (LAN)</li>
58     <li>eth1 - NIC connected to the Wide Area Network (WAN)</li>
59     <li>LAN utilizes the private 192.168.0.xxx network</li>
60     <li>router is hardcoded to the standard 192.168.0.1 IP</li>
61 vapier 1.3 <li>router is running Linux 2.4 or 2.6; you're on your own with 2.0/2.2</li>
62 vapier 1.1 </ul>
63    
64     <impo>
65 neysx 1.23 Due to security precautions, I would highly suggest you shut down any unneeded
66     services on the router until we have a chance to get the firewall up and
67     rolling. To view the currently running services, just run <c>rc-status</c>.
68 vapier 1.1 </impo>
69    
70     </body>
71     </section>
72     </chapter>
73    
74     <chapter>
75     <title>Kernel setup (know thyself first)</title>
76     <section>
77     <body>
78    
79     <p>
80 neysx 1.23 Your kernel needs to have the drivers running for both your NICs. To see if
81     your cards are already setup, just run <c>ifconfig</c>. Your output may differ
82     slightly from the following, that's fine. What matters is that the interface
83     shows up at all.
84 vapier 1.1 </p>
85 neysx 1.23
86 vapier 1.1 <pre caption="Checking NICs">
87     # <i>ifconfig -a</i>
88     eth0 Link encap:Ethernet HWaddr 00:60:F5:07:07:B8
89     BROADCAST MULTICAST MTU:1500 Metric:1
90     RX packets:0 errors:0 dropped:0 overruns:0 frame:0
91     TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
92     collisions:0 txqueuelen:1000
93     RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
94     Interrupt:11 Base address:0x9800
95    
96     eth1 Link encap:Ethernet HWaddr 00:60:F5:07:07:B9
97     BROADCAST MULTICAST MTU:1500 Metric:1
98     RX packets:0 errors:0 dropped:0 overruns:0 frame:0
99     TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
100     collisions:0 txqueuelen:1000
101     RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
102     Interrupt:10 Base address:0x9400
103     </pre>
104 neysx 1.23
105 vapier 1.1 <p>
106 neysx 1.23 If you do not see your two cards showing up and you're not sure what kind of
107 vapier 1.49 cards you have, try running <c>lspci | grep Ethernet</c>. You can get that
108     from <c>emerge pciutils</c>. Once you have this information, go into your
109     kernel and add support for the correct drivers.
110 vapier 1.1 </p>
111    
112     <p>
113 neysx 1.23 The next thing you'll need is support for iptables and NAT (and packet shaping
114 vapier 1.35 if you want). The following list is split up into always required (*),
115     required only for adsl via PPPoE (a), suggested for everyone (x), and only
116     for shaper (s) features. It does not matter whether you build the features
117     into the kernel or as a module so long as when the feature is needed, the
118     correct module(s) are loaded (module loading is left to the reader as a fun
119 neysx 1.23 exercise however).
120 vapier 1.1 </p>
121 neysx 1.23
122 vapier 1.1 <pre caption="Network Options">
123 neysx 1.23 Networking options ---&gt;
124     [*] TCP/IP networking
125     [*] IP: advanced router
126     [*] Network packet filtering (replaces ipchains)
127 vapier 1.20 <comment>If you use 2.4.x, you have to enable the following for DHCP:</comment>
128 neysx 1.23 [*] Socket Filtering
129 vapier 1.1
130 neysx 1.23 IP: Netfilter Configuration ---&gt;
131     [*] Connection tracking (required for masq/NAT)
132     [x] FTP protocol support
133     [x] IRC protocol support
134     [*] IP tables support (required for filtering/masq/NAT)
135     [*] IP range match support
136     [x] MAC address match support
137     [*] Multiple port match support
138     [*] Packet filtering
139     [*] REJECT target support
140     [x] REDIRECT target support
141     [*] Full NAT
142     [*] MASQUERADE target support
143     [s] Packet mangling
144     [s] MARK target support
145     [x] LOG target support
146    
147     QoS and/or fair queueing ---&gt;
148     [s] QoS and/or fair queueing
149     [s] HTB packet scheduler
150     [s] Ingress Qdisc
151 vapier 1.35
152     [a] PPP (point-to-point protocol) support
153     [a] PPP filtering
154     [a] PPP support for async serial ports
155     [a] PPP support for sync tty ports
156     [a] PPP Deflate compression
157     [a] PPP BSD-Compress compression
158     [a] PPP over Ethernet
159 vapier 1.1 </pre>
160 neysx 1.23
161 vapier 1.1 <note>
162 neysx 1.23 Somethings may be slightly different in a 2.4 vs 2.6 kernel, but you should be
163     able to figure it out :).
164 vapier 1.1 </note>
165    
166     </body>
167     </section>
168     </chapter>
169    
170     <chapter>
171     <title>Hug the WAN (a.k.a. The Internet)</title>
172    
173     <section>
174     <title>Intro</title>
175     <body>
176 neysx 1.23
177 vapier 1.1 <p>
178 neysx 1.23 There are many ways to connect to the internet so I'll just cover the ones I'm
179     familiar with. That leaves us with ADSL (PPPoE) and cable modems
180     (static/dynamic). If there are other methods out there, feel free to write up
181     a little blurb and e-mail me. Feel free to skip any of the following sections
182     in this chapter that don't apply to you. This chapter is just about getting
183     the router connected to the internet via eth1.
184 vapier 1.1 </p>
185 neysx 1.23
186 vapier 1.1 </body>
187     </section>
188     <section>
189     <title>ADSL and PPPoE</title>
190     <body>
191    
192     <p>
193 neysx 1.23 All the fancy PPPoE software has been bundled up into one little nice package
194     nowadays called <uri link="http://www.roaringpenguin.com/">Roaring
195     Penguin</uri>. Simply <c>emerge rp-pppoe</c> and you'll be on your way.
196     Remember how I said you'll need username/password information? Well I wasn't
197     lying so I hope you have it now! Load up <path>/etc/ppp/pppoe.conf</path> in
198 vapier 1.1 your favorite editor and set it up.
199     </p>
200    
201 vapier 1.8 <note>
202 vapier 1.39 In order for the following net settings to work, you must have
203     baselayout-1.11.14 or later installed on your system.
204 vapier 1.8 </note>
205    
206 vapier 1.1 <pre caption="Setting up eth1">
207     <comment>(Replace 'vla9h924' with your username and 'password' with your password)</comment>
208    
209     # <i>nano /etc/ppp/pap-secrets</i>
210 neysx 1.23 <comment># client server secret</comment>
211     "vla9h924" * "password"
212 vapier 1.1 # <i>nano /etc/conf.d/net</i>
213 vapier 1.39 <comment>Tell baselayout to use adsl for your eth1:</comment>
214 vapier 1.25 config_eth1=( "adsl" )
215 vapier 1.39 user_eth1=( "vla9h924" )
216 vapier 1.30 # <i>ln -s net.lo /etc/init.d/net.eth1</i>
217 vapier 1.1 # <i>rc-update add net.eth1 default</i>
218     # <i>/etc/init.d/net.eth1 start</i>
219     </pre>
220    
221 vapier 1.3 <warn>
222 vapier 1.39 When the DSL interface comes up, it will create ppp0. Although your NIC is
223     called eth1, the IP is actually bound to ppp0. From now on, when you see
224     examples that utilize 'eth1', substitute with 'ppp0'.
225 vapier 1.3 </warn>
226 vapier 1.1
227     </body>
228     </section>
229    
230     <section>
231     <title>Cable and/or dynamic/static IP</title>
232     <body>
233    
234     <p>
235 vapier 1.4 If you have a static IP then you will need a few more details than if
236 vapier 1.1 you have a dynamic IP. For static users, you will need your IP,
237     gateway, and DNS servers.
238     </p>
239    
240     <pre caption="Setting up eth1">
241     <comment>Dynamic IP Users:</comment>
242     # <i>emerge dhcpcd</i>
243     # <i>nano /etc/conf.d/net</i>
244 neysx 1.23 <comment>You'll need an entry like so:</comment>
245 vapier 1.25 config_eth1=( "dhcp" )
246 vapier 1.1
247     <comment>Static IP Users:</comment>
248     # <i>nano /etc/conf.d/net</i>
249 neysx 1.23 <comment>You'll need entries like so:</comment>
250 vapier 1.43 config_eth1=( "66.92.78.102 broadcast 66.92.78.255 netmask 255.255.255.0" )
251 neysx 1.23 routes_eth1=( "default gw 66.92.78.1" )
252 vapier 1.1 # <i>nano /etc/resolv.conf</i>
253 neysx 1.23 <comment>Add one line per DNS server:</comment>
254     nameserver 123.123.123.123
255 vapier 1.1
256     <comment>Dynamic and Static Setup:</comment>
257 vapier 1.30 # <i>ln -s net.lo /etc/init.d/net.eth1</i>
258 vapier 1.1 # <i>rc-update add net.eth1 default</i>
259     # <i>/etc/init.d/net.eth1 start</i>
260     </pre>
261    
262     <p>
263     You should be all set to go now.
264     </p>
265    
266     </body>
267     </section>
268     </chapter>
269    
270     <chapter>
271     <title>Hug the LAN (bring along some friends)</title>
272     <section>
273     <body>
274    
275     <p>
276     This step is a breeze compared to the previous one.
277     </p>
278    
279     <pre caption="Setting up eth0">
280     # <i>nano /etc/conf.d/net</i>
281 neysx 1.23 <comment>Add a line like the following:</comment>
282 vapier 1.43 config_eth0=( "192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0" )
283 vapier 1.1 # <i>rc-update add net.eth0 default</i>
284     # <i>/etc/init.d/net.eth0 start</i>
285     </pre>
286    
287     </body>
288     </section>
289     </chapter>
290    
291     <chapter>
292     <title>LAN Services (because we're nice people)</title>
293    
294     <section>
295     <title>DHCP Server</title>
296     <body>
297 neysx 1.23
298 vapier 1.1 <p>
299 neysx 1.23 I bet it'd be nice if everyone else in your house could just plug their
300     computers into the network and things would just work. No need to remember
301     mind-numbing details or make them stare at confusing configuration screens!
302     Life would be grand eh? Introducing the Dynamic Host Configuration Protocol
303     (DHCP) and why you should care.
304 vapier 1.1 </p>
305    
306 vapier 1.2 <p>
307     DHCP is exactly what its name implies. It's a protocol that allows you
308 neysx 1.23 to dynamically configure other hosts automatically. You run a DHCP server on
309 vapier 1.33 the router, give it all the information about your network (valid IPs,
310 neysx 1.23 DNS servers, gateways, etc...), and then when the other hosts start up, they
311     run a DHCP client to automatically configure themselves. No fuss, no muss!
312     For more information about DHCP, you can always visit <uri
313     link="http://en.wikipedia.org/wiki/DHCP">Wikipedia</uri>.
314 vapier 1.2 </p>
315    
316 vapier 1.33 <p>
317     We'll use a package called dnsmasq which provides both DHCP and DNS services.
318     For now lets just focus on the DHCP aspect. Note that if you want to run a
319     different DHCP server, you can find another example in the Fun Things chapter.
320     Also, if you wish to tinker with the DHCP server settings, just read the
321     comments in <path>/etc/dnsmasq.conf</path>. All the defaults should work fine
322     though.
323     </p>
324    
325     <pre caption="Setting up a DHCP server">
326     # <i>emerge dnsmasq</i>
327     # <i>nano /etc/dnsmasq.conf</i>
328 vapier 1.43 <comment>Add this line to enable dhcp:</comment>
329 vapier 1.33 dhcp-range=192.168.0.100,192.168.0.250,72h
330 vapier 1.43 <comment>Restrict dnsmasq to just the LAN interface</comment>
331     interface=eth0
332 vapier 1.33
333     # <i>rc-update add dnsmasq default</i>
334     # <i>/etc/init.d/dnsmasq start</i>
335 vapier 1.1 </pre>
336    
337     <p>
338 neysx 1.23 Now your little router is a bona-fide DHCP server! Plugin those computers and
339     watch them work! With Windows systems you should go into the TCP/IP Properties
340     and select the 'Obtain an IP address automatically' and 'Obtain DNS server
341     address automatically' options. Sometimes the changes aren't instantaneous, so
342 vapier 1.31 you may have to open a command prompt and run <c>ipconfig /release</c> and
343 neysx 1.23 <c>ipconfig /renew</c>. But enough about Windows, let's get back to our
344     favorite penguin.
345 vapier 1.1 </p>
346 neysx 1.23
347 vapier 1.1 </body>
348     </section>
349    
350     <section>
351     <title>DNS Server</title>
352     <body>
353 neysx 1.23
354 vapier 1.2 <p>
355 neysx 1.23 When people want to visit a place on the internet, they remember names, not a
356 vapier 1.33 string of funky numbers. After all, what's easier to remember, ebay.com or
357 neysx 1.23 66.135.192.87? This is where the DNS steps in. DNS servers run all over the
358     internet, and whenever someone wants to visit 'ebay.com', these servers turn
359     'ebay.com' (what we understand) into '66.135.192.87' (what our computers
360     understand). For more information about DNS, you can always visit <uri
361     link="http://en.wikipedia.org/wiki/DNS">Wikipedia</uri>.
362 vapier 1.2 </p>
363 vapier 1.1
364     <p>
365 vapier 1.33 Since we're using dnsmasq for our DHCP server, and it includes a DNS server,
366     you've got nothing left to do here! Your little router is already providing
367     DNS to its DHCP clients. Bet you wish everything was this easy ;).
368 vapier 1.1 </p>
369    
370     <p>
371 vapier 1.33 You're welcome to choose other DNS servers if you're more comfortable with
372     them, but the reason dnsmasq is great is because it was designed to do exactly
373     what we want and nothing more. It's a little DNS caching/forwarding server for
374     local networks. We're not looking to provide DNS for our own domain here, just
375     offer simple DNS services to everyone else on our LAN.
376 vapier 1.1 </p>
377    
378     </body>
379     </section>
380    
381     <section>
382 vapier 1.4 <title>NAT (a.k.a. IP-masquerading)</title>
383 vapier 1.1 <body>
384    
385     <p>
386 neysx 1.23 At this point, people on your network can talk to each other and they can look
387     up hostnames via DNS, but they still can't actually connect to the internet.
388     While you may think that's great (more bandwidth for you!), I bet they're not
389     too happy just yet.
390 vapier 1.1 </p>
391    
392 vapier 1.2 <p>
393 vapier 1.33 This is where Network Address Translation (NAT) steps in. NAT is a way of
394     connecting multiple computers in a private LAN to the internet when you have a
395     smaller number of public IP addresses available to you. Typically you are given
396     1 IP by your ISP, but you want to let your whole house connect to the internet.
397     NAT is the magic that makes this possible. For more information about NAT, you
398     can always visit <uri link="http://en.wikipedia.org/wiki/NAT">Wikipedia</uri>.
399 vapier 1.2 </p>
400    
401     <note>
402 neysx 1.23 Before we get started, make sure you have iptables on your system. Although it
403     is automatically installed on most systems, you may not have it. If you don't,
404     just run <c>emerge iptables</c>.
405 vapier 1.2 </note>
406    
407 vapier 1.1 <pre caption="Setting up iptables">
408     <comment>First we flush our current rules</comment>
409     # <i>iptables -F</i>
410     # <i>iptables -t nat -F</i>
411    
412 vapier 1.33 <comment>Setup default policies to handle unmatched traffic</comment>
413 vapier 1.32 # <i>iptables -P INPUT ACCEPT</i>
414     # <i>iptables -P OUTPUT ACCEPT</i>
415     # <i>iptables -P FORWARD DROP</i>
416    
417 vapier 1.30 <comment>Copy and paste these examples ...</comment>
418     # <i>export LAN=eth0</i>
419     # <i>export WAN=eth1</i>
420    
421 vapier 1.1 <comment>Then we lock our services so they only work from the LAN</comment>
422 vapier 1.30 # <i>iptables -I INPUT 1 -i ${LAN} -j ACCEPT</i>
423 vapier 1.1 # <i>iptables -I INPUT 1 -i lo -j ACCEPT</i>
424 vapier 1.30 # <i>iptables -A INPUT -p UDP --dport bootps -i ! ${LAN} -j REJECT</i>
425     # <i>iptables -A INPUT -p UDP --dport domain -i ! ${LAN} -j REJECT</i>
426 vapier 1.1
427 vapier 1.21 <comment>(Optional) Allow access to our ssh server from the WAN</comment>
428 vapier 1.30 # <i>iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT</i>
429 vapier 1.21
430 vapier 1.1 <comment>Drop TCP / UDP packets to privileged ports</comment>
431 vapier 1.30 # <i>iptables -A INPUT -p TCP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP</i>
432     # <i>iptables -A INPUT -p UDP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP</i>
433 vapier 1.1
434     <comment>Finally we add the rules for NAT</comment>
435 vapier 1.30 # <i>iptables -I FORWARD -i ${LAN} -d 192.168.0.0/255.255.0.0 -j DROP</i>
436     # <i>iptables -A FORWARD -i ${LAN} -s 192.168.0.0/255.255.0.0 -j ACCEPT</i>
437     # <i>iptables -A FORWARD -i ${WAN} -d 192.168.0.0/255.255.0.0 -j ACCEPT</i>
438     # <i>iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE</i>
439 vapier 1.1 <comment>Tell the kernel that ip forwarding is OK</comment>
440     # <i>echo 1 > /proc/sys/net/ipv4/ip_forward</i>
441     # <i>for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done</i>
442    
443     <comment>This is so when we boot we don't have to run the rules by hand</comment>
444     # <i>/etc/init.d/iptables save</i>
445     # <i>rc-update add iptables default</i>
446 vapier 1.14 # <i>nano /etc/sysctl.conf</i>
447 vapier 1.50 <comment>Add/Uncomment the following lines:</comment>
448 vapier 1.14 net.ipv4.ip_forward = 1
449 vapier 1.50 net.ipv4.conf.default.rp_filter = 1
450    
451     <comment>If you have a dynamic internet address you probably want to enable this:</comment>
452     net.ipv4.ip_dynaddr = 1
453 vapier 1.1 </pre>
454    
455     <p>
456 neysx 1.23 Once you've typed out all of that, the rest of your network should now be able
457     to use the internet as if they were directly connected themselves.
458 vapier 1.1 </p>
459    
460 vapier 1.50 <p>
461     The ip_dynaddr option is useful for dial on demand systems or when your ISP
462     gives out dynamic addresses. This works around the problem where a connection
463     is attempted before the internet interface is fully setup. Really this just
464     provides for a smoother network experience for users behind your router.
465     </p>
466    
467 vapier 1.3 </body>
468     </section>
469     </chapter>
470    
471     <chapter>
472     <title>Fun Things (for a rainy day)</title>
473    
474     <section>
475     <title>Intro</title>
476     <body>
477 neysx 1.23
478 vapier 1.1 <p>
479 neysx 1.23 Believe it or not, you're done :). From here on out, I'll cover a bunch of
480     common topics that may interest you. Everything in this chapter is completely
481     optional.
482 vapier 1.1 </p>
483 neysx 1.23
484 vapier 1.3 </body>
485     </section>
486 vapier 1.1
487 vapier 1.3 <section>
488     <title>Port Forwarding</title>
489     <body>
490 neysx 1.23
491 vapier 1.3 <p>
492 neysx 1.23 Sometimes you would like to be able to host services on a computer behind the
493     router, or just to make your life easier when connecting remotely. Perhaps you
494     want to run a FTP, HTTP, SSH, or VNC server on one or more machines behind your
495     router and be able to connect to them all. The only caveat is that you can
496     only have one service/machine combo per port. For example, there is no
497     practical way to setup three FTP servers behind your router and then try to
498     connect to them all through port 21; only one can be on port 21 while the
499     others would have to be on say port 123 and port 567.
500 vapier 1.3 </p>
501    
502     <p>
503 neysx 1.23 All the port forwarding rules are of the form <c>iptables -t nat -A PREROUTING
504 vapier 1.30 [-p protocol] --dport [external port on router] -i ${WAN} -j DNAT --to [ip/port
505 vapier 1.33 to forward to]</c>. Unfortunately, iptables does not accept hostnames when port
506     forwarding. If you are forwarding an external port to the same port on the
507     internal machine, you can omit the destination port. See the iptables(8) man
508     page for more information.
509 vapier 1.3 </p>
510    
511 swift 1.28 <pre caption="Running the iptables commands">
512 vapier 1.30 <comment>Copy and paste these examples ...</comment>
513     # <i>export LAN=eth0</i>
514     # <i>export WAN=eth1</i>
515    
516 vapier 1.3 <comment>Forward port 2 to ssh on an internal host</comment>
517 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p tcp --dport 2 -i ${WAN} -j DNAT --to 192.168.0.2:22</i>
518 vapier 1.3
519     <comment>FTP forwarding to an internal host</comment>
520 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p tcp --dport 21 -i ${WAN} -j DNAT --to 192.168.0.56</i>
521 vapier 1.3
522     <comment>HTTP forwarding to an internal host</comment>
523 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p tcp --dport 80 -i ${WAN} -j DNAT --to 192.168.0.56</i>
524 vapier 1.3
525     <comment>VNC forwarding for internal hosts</comment>
526 vapier 1.30 # <i>iptables -t nat -I PREROUTING -p tcp --dport 5900 -i ${WAN} -j DNAT --to 192.168.0.2</i>
527     # <i>iptables -t nat -I PREROUTING -p tcp --dport 5901 -i ${WAN} -j DNAT --to 192.168.0.3:5900</i>
528 vapier 1.3 <comment>If you want to VNC in to 192.168.0.3, then just add ':1' to the router's hostname</comment>
529    
530     <comment>Bittorrent forwarding</comment>
531 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p tcp --dport 6881:6889 -i ${WAN} -j DNAT --to 192.168.0.2</i>
532 vapier 1.15
533 vapier 1.33 <comment>eDonkey/eMule forwarding</comment>
534     # <i>iptables -t nat -A PREROUTING -p tcp --dport 4662 -i ${WAN} -j DNAT --to 192.168.0.55</i>
535    
536 vapier 1.15 <comment>Game Cube Warp Pipe support</comment>
537 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p udp --dport 4000 -i ${WAN} -j DNAT --to 192.168.0.56</i>
538 vapier 1.15
539 vapier 1.33 <comment>Playstation 2 Online support</comment>
540 vapier 1.30 # <i>iptables -t nat -A PREROUTING -p tcp --dport 10070:10080 -i ${WAN} -j DNAT --to 192.168.0.11</i>
541     # <i>iptables -t nat -A PREROUTING -p udp --dport 10070:10080 -i ${WAN} -j DNAT --to 192.168.0.11</i>
542 vapier 1.34
543     <comment>Xbox Live</comment>
544     # <i>iptables -t nat -A PREROUTING -p tcp --dport 3074 -i ${WAN} -j DNAT --to 192.168.0.69</i>
545     # <i>iptables -t nat -A PREROUTING -p udp --dport 3074 -i ${WAN} -j DNAT --to 192.168.0.69</i>
546     # <i>iptables -t nat -A PREROUTING -p udp --dport 88 -i ${WAN} -j DNAT --to 192.168.0.69</i>
547 vapier 1.3 </pre>
548    
549     <note>
550 neysx 1.37 If you have other common / cool examples, please <mail
551     link="vapier@gentoo.org">e-mail me</mail>.
552 vapier 1.3 </note>
553 neysx 1.23
554 vapier 1.3 </body>
555     </section>
556    
557     <section>
558     <title>Identd (for IRC)</title>
559     <body>
560 neysx 1.23
561 vapier 1.3 <p>
562 neysx 1.23 Internet Relay Chat utilizes the ident service pretty heavily. Now that the
563     IRC clients are behind the router, we need a way to host ident for both the
564     router and the clients. One such server has been created called
565     <c>midentd</c>.
566 vapier 1.3 </p>
567    
568     <pre caption="Setting up ident">
569     # <i>emerge midentd</i>
570     # <i>rc-update add midentd default</i>
571     # <i>/etc/init.d/midentd start</i>
572     </pre>
573    
574     <p>
575 neysx 1.23 There are a few other ident servers in portage. Depending on your needs, I
576     would recommend checking out <c>oidentd</c> and <c>fakeidentd</c>.
577 vapier 1.3 </p>
578 neysx 1.23
579 vapier 1.3 </body>
580     </section>
581    
582 vapier 1.5 <!--
583     <section>
584     <title>Traffic Shaping</title>
585     <body>
586     <p>
587     This is an attempt to simply and Gentooify the <uri link="http://www.tldp.org/HOWTO/ADSL-Bandwidth-Management-HOWTO/">ADSL Bandwidth Management HOWTO</uri>
588     found over at the TLDP. Feel free to refer to the original document
589     for more details.
590     </p>
591    
592     <p>
593     Here we will be setting up what some people refer to as a "Packet Shaper",
594     <uri link="http://en.wikipedia.org/wiki/Traffic_shaping">"Traffic Shaping"</uri>,
595     or <uri link="http://en.wikipedia.org/wiki/QoS">"Quality of Service"</uri>.
596     Simply put, we want to setup rules on our router that will slow down
597     certain activities (like sending large e-mails or downloading from P2P
598     networks) while keeping other activities (like browsing the web or playing
599     online video games) reasonably fast. A 30 second difference in a video
600     game is a lot worse than a 30 second difference in downloading large
601     files :).
602     </p>
603    
604     <p>
605     The first thing is to make sure your kernel has all the features added to
606     it. See the chapter on <uri link="#doc_chap2">Kernel setup</uri> for more
607     information. Next, you will need to <c>emerge iptables iputils</c> so that
608     you will have access to the <c>iptables</c>, <c>ip</c>, and <c>tc</c>
609     commands.
610     </p>
611    
612     <p>
613     Before we jump into the commands, let's cover a little of the theory. The
614     way this whole system works is to classify common network streams and then
615     to prioritize them. You use iptables to classify network streams, iputils
616     to define the different priority levels, and the kernel to adjust speeds.
617     Just remember that although you can control outbound traffic pretty tightly
618     (from the LAN to the WAN), your ability to control inbound traffic (from
619     the WAN to the LAN) is somewhat limited. Just remember that the following
620     examples are to get your feet wet; if you want more then I'd suggest
621     reading up on the subject. In this example, we will be using the
622     <uri link="http://luxik.cdi.cz/~devik/qos/htb/">Hierarchical Token Buckets (HTB)</uri>
623     packet scheduling algorithm. Still with me? Great, let's start shaping :).
624     </p>
625    
626     <pre caption="Setup">
627     DEV=eth1 <comment>NIC connected to WAN</comment>
628     RATE_OUT=100 <comment>Available outbound bandwidth (in kilobits [kb])</comment>
629     RATE_IN=1400 <comment>Available inbound bandwidth (in kb)</comment>
630    
631     <comment>Here we initialize the priority system. The 45 is used to set the default classification level.</comment>
632     ip link set dev ${DEV} qlen 30
633     tc qdisc add dev ${DEV} root handle 1: htb default 45
634     tc class add dev ${DEV} parent 1: classid 1:1 htb rate ${RATE_OUT}kbit
635     </pre>
636    
637     <p>
638     Here we initialized the system which will be used to prioritize all of
639     our network traffic. We created our queue, told it to use the HTB
640     algorithm, and set the default classification level to '45'. The
641     default is completely arbitrary, as are the levels we choose from
642     here on out. The only thing that matters is how the levels compare
643     relatively; a level '10' packet will be given preference over a
644     level '45' packet. Let's move on to declaring different levels.
645     </p>
646    
647     <pre caption="Declaring levels">
648     tc class add dev $DEV parent 1:1 classid 1:10 htb rate $rkbit ceil $tkbit prio $p
649     tc qdisc add dev $DEV parent 1:10 handle 10: sfq
650     </pre>
651     </body>
652     </section>
653     -->
654    
655 vapier 1.3 <section>
656 vapier 1.9 <title>Time Server</title>
657     <body>
658 neysx 1.23
659 vapier 1.9 <p>
660 vapier 1.24 Keeping your system time correct is essential in maintaining a healthy system.
661 neysx 1.23 One of the most common ways of accomplishing this is with the Network Time
662     Protocol (NTP) and the ntp package (which provides implementations for both
663     server and client).
664 vapier 1.9 </p>
665    
666     <p>
667 neysx 1.23 Many people run ntp clients on their computers. Obviously, the more clients in
668     the world, the larger the load the ntp servers need to shoulder. In
669     environments like home networks though, we can help keep the load down on
670     public servers while still providing the proper time to all our computers. As
671     an added bonus, our private updates will be a lot faster for the clients too!
672     All we have to do is run a ntp server on our router that synchronizes itself
673     with the public internet servers while providing the time to the rest of the
674     computers in the network. To get started, simply <c>emerge ntp</c> on the
675 vapier 1.9 router.
676     </p>
677    
678     <pre caption="Setting up the NTP server">
679     # <i>nano /etc/conf.d/ntp-client</i>
680     <comment>Customize if you wish but the defaults should be fine</comment>
681     # <i>rc-update add ntp-client default</i>
682    
683     # <i>nano /etc/ntp.conf</i>
684 neysx 1.23 <comment>Add the follwing lines:</comment>
685 vapier 1.9 restrict default ignore
686     restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap
687 neysx 1.23 <comment>These will allow only ntp clients with an IP
688     address in the 192.168.0.xxx range to use your ntp server</comment>
689 vapier 1.9 # <i>nano /etc/conf.d/ntpd</i>
690     <comment>Customize if you wish but the defaults should be fine</comment>
691 vapier 1.17 # <i>rc-update add ntpd default</i>
692 vapier 1.9
693     # <i>/etc/init.d/ntp-client start</i>
694     # <i>/etc/init.d/ntpd start</i>
695     </pre>
696    
697 vapier 1.22 <note>
698 neysx 1.23 You should make sure that you allow inbound and outbound communication on the
699     ntp port (123/udp) when setting up the server. The client just needs outbound
700     access on port 123 over udp.
701 vapier 1.22 </note>
702    
703 vapier 1.9 <p>
704 neysx 1.23 Now, on your clients, have them <c>emerge ntp</c> also. However, we will just
705     run the ntp client so setup is a lot simpler.
706 vapier 1.9 </p>
707    
708     <pre caption="Setting up a NTP client">
709     # <i>nano /etc/conf.d/ntp-client</i>
710     <comment>Change the 'pool.ntp.org' server in the NTPCLIENT_OPTS variable to '192.168.0.1'</comment>
711     # <i>rc-update add ntp-client default</i>
712     # <i>/etc/init.d/ntp-client start</i>
713     </pre>
714 neysx 1.23
715 vapier 1.9 </body>
716     </section>
717    
718     <section>
719 vapier 1.29 <title>Rsync Server</title>
720     <body>
721    
722     <p>
723     For those who run multiple Gentoo boxes on the same lan, you often want to
724     keep from having every machine running <c>emerge sync</c> with remote
725     servers. By setting up a local rsync, you save on both your bandwidth and
726     the Gentoo rsync servers' bandwidth. It's pretty simple to do.
727     </p>
728 neysx 1.45
729 vapier 1.29 <note>
730 neysx 1.45 For a much more in-depth rsync guide, please see the official <uri
731     link="/doc/en/rsync.xml#local">rsync guide</uri>.
732 vapier 1.29 </note>
733    
734     <p>
735     Since every Gentoo machine requires rsync, theres no need to emerge it. Edit
736     the default <path>/etc/rsyncd.conf</path> config file, uncomment the
737     <c>[gentoo-portage]</c> section, and make sure you add an <c>address</c>
738     option. All the other defaults should be fine.
739     </p>
740    
741     <pre caption="Rsync server config">
742     pid file = /var/run/rsyncd.pid
743     use chroot = yes
744     read only = yes
745     address = 192.168.0.1
746    
747     [gentoo-portage]
748 neysx 1.41 path = /mnt/space/portage
749     comment = Gentoo Linux Portage tree
750     exclude = /distfiles /packages
751 vapier 1.29 </pre>
752    
753     <p>
754     Then you need to start the service (again, the defaults are OK).
755     </p>
756    
757     <pre caption="Starting the rsync server">
758     # <i>/etc/init.d/rsyncd start</i>
759     # <i>rc-update add rsyncd default</i>
760     </pre>
761    
762     <p>
763     Only thing left is to set tell your clients to sync against the router.
764     </p>
765    
766     <pre caption="Client SYNC settings in make.conf">
767     SYNC="rsync://192.168.0.1/gentoo-portage"
768     </pre>
769    
770     </body>
771     </section>
772    
773     <section>
774 vapier 1.3 <title>Mail Server</title>
775     <body>
776 neysx 1.23
777 vapier 1.3 <p>
778 neysx 1.23 Sometimes it's nice to run your own Simple Mail Transfer Protocol (SMTP) server
779     on the router. You may have your own reason for wanting to do so, but I run it
780     so that the users see mail as being sent instantly and the work of
781     retrying/routing is left up to the mail server. Some ISPs also don't allow for
782     mail relaying for accounts that aren't part of their network (like Verizon).
783     Also, you can easily throttle the delivery of mail so that large attachments
784     won't seriously lag your connection for half an hour.
785 vapier 1.4 </p>
786    
787     <pre caption="Setting up SMTP">
788     # <i>emerge qmail</i>
789     <comment>make sure the output of `hostname` is correct</comment>
790     # <i>ebuild /var/db/pkg/*-*/qmail-1.03-r*/*.ebuild config</i>
791 vapier 1.30 # <i>iptables -I INPUT -p tcp --dport smtp -i ! ${LAN} -j REJECT</i>
792 vapier 1.4 # <i>ln -s /var/qmail/supervise/qmail-send /service/qmail-send</i>
793 vapier 1.10 # <i>ln -s /var/qmail/supervise/qmail-smtpd /service/qmail-smtpd</i>
794 vapier 1.13 <!--
795 vapier 1.4 # <i>cd /etc/tcprules.d</i>
796     # <i>nano tcp.qmail-smtp</i>
797 vapier 1.13 -->
798     # <i>cd /etc</i>
799     # <i>nano tcp.smtp</i>
800 neysx 1.23 <comment>Add an entry like so to the allow section:</comment>
801     192.168.0.:allow,RELAYCLIENT=""
802 vapier 1.13 <!--
803 vapier 1.4 # <i>tcprules tcp.qmail-qmtp.cdb rules.tmp &lt; tcp.qmail-smtp</i>
804 vapier 1.13 -->
805     # <i>tcprules tcp.smtp.cdb rules.tmp &lt; tcp.smtp</i>
806 vapier 1.4 # <i>rc-update add svscan default</i>
807     # <i>/etc/init.d/svscan start</i>
808     </pre>
809    
810     <p>
811 neysx 1.23 I'm a huge fan of qmail, but you're free to use a different mta :). When you
812     setup e-mail on the hosts in your network, tell them that their SMTP server is
813     192.168.0.1 and everything should be peachy. You might want to visit the <uri
814     link="http://qmail.org/">qmail homepage</uri> for more documentation.
815 vapier 1.3 </p>
816 neysx 1.23
817 vapier 1.3 </body>
818     </section>
819    
820 vapier 1.4 <!--
821 vapier 1.3 <section>
822 vapier 1.4 <title>E-mail Virus Scanning</title>
823 vapier 1.3 <body>
824     <p>
825 vapier 1.4 If you'd like to provide e-mail virus scanning for your users, but
826     don't want to have to install a virus scanner on every single machine,
827     then <c>pop3vscan</c> may just be the thing for you; a transparent
828     Post Office Protocol (POP) scanner.
829 vapier 1.3 </p>
830 vapier 1.4
831     <pre caption="Setting up pop3vscan">
832     TODO
833     </pre>
834    
835 vapier 1.3 </body>
836     </section>
837 vapier 1.4 -->
838 vapier 1.3
839 vapier 1.33 <section>
840     <title>Full DHCP Server</title>
841     <body>
842    
843     <p>
844     Earlier we used dnsmasq to provide DHCP service to all our clients. For most
845     people with a simple small LAN, this is perfect. But you may need something
846     with more features. Thus we turn to a full-featured DHCP server as provided
847     by the <uri link="http://www.isc.org/products/DHCP">ISC</uri> folks.
848     </p>
849    
850     <pre caption="Setting up dhcpd">
851     # <i>emerge dhcp</i>
852     # <i>nano /etc/dhcp/dhcpd.conf</i>
853     <comment>(Here is a sample configuration file:)</comment>
854     authoritative;
855     ddns-update-style interim;
856     subnet 192.168.0.0 netmask 255.255.255.0 {
857     range 192.168.0.100 192.168.0.250;
858     default-lease-time 259200;
859     max-lease-time 518400;
860     option subnet-mask 255.255.255.0;
861     option broadcast-address 192.168.0.255;
862     option routers 192.168.0.1;
863     option domain-name-servers 192.168.0.1;
864     }
865 vapier 1.44 # <i>nano /etc/conf.d/dhcpd</i>
866 vapier 1.33 <comment>(Set IFACE="eth0")</comment>
867 vapier 1.44 # <i>rc-update add dhcpd default</i>
868     # <i>/etc/init.d/dhcpd start</i>
869 vapier 1.33 </pre>
870    
871     <p>
872     This is the minimal setup required to replace the dnsmasq DHCP functionality
873     that we used earlier. Speaking of which, you did remember to disable the DHCP
874     features in dnsmasq didn't you? If not, you should do so now (just comment
875     out the <c>dhcp-range</c> setting in <path>/etc/dnsmasq.conf</path> and restart
876     the service).
877     </p>
878    
879     </body>
880     </section>
881    
882 vapier 1.38 <section>
883     <title>Connect Another LAN (or two or three or ...)</title>
884     <body>
885    
886     <p>
887     Sometimes you have need of connecting the router to another LAN. Maybe you
888     want to hook up a group of friends temporarily, or you're a neat freak and
889     want to section off different groups of computers, or you're just really
890     really bored. Whatever the reasons, extending the router to other LAN
891     networks should be pretty straightforward. In the following examples, I will
892     assume that this new network is connected via a third ethernet card, namely
893     <c>eth2</c>.
894     </p>
895    
896     <p>
897     First you need to configure the interface. Just take the instructions in the
898     <uri link="#doc_chap4_pre1">4.1 code listing</uri> and replace <c>eth0</c>
899     with <c>eth2</c> and <c>192.168.0</c> with <c>192.168.1</c>.
900     </p>
901    
902     <p>
903     Then you need to tweak dnsmasq to service the new interface. Just edit the
904     <path>/etc/conf.d/dnsmasq</path> file again and append <c>-i eth2</c> to
905     DNSMASQ_OPTS; using -i multiple times is OK. Then edit
906     <path>/etc/dnsmasq.conf</path> and add another line like the dhcp-range line
907     in the <uri link="#doc_chap5_pre1">5.1 code listing</uri>, replacing
908     <c>192.168.0</c> with <c>192.168.1</c>. Having multiple dhcp-range lines is
909     OK too.
910     </p>
911    
912     <p>
913     Finally, see the rules in the <uri link="#doc_chap5_pre2">5.2 code
914     listing</uri> and duplicate the rules that have <c>-i ${LAN}</c> in them. You
915     may want to create another variable, say <c>LAN2</c>, to make things easier.
916     </p>
917    
918     </body>
919     </section>
920    
921 vapier 1.4 </chapter>
922    
923     <chapter>
924 vapier 1.30 <title>Troubleshooting</title>
925 vapier 1.27
926     <section>
927     <title>Useful Tools</title>
928     <body>
929    
930     <p>
931     If you're having trouble getting your computers to communicate, you may way to
932     try out the following tools (they can all be found in the <c>net-analyzer</c>
933     portage category):
934     </p>
935    
936     <table>
937     <tr>
938     <th>Utility</th>
939     <th>Description</th>
940     </tr>
941     <tr>
942     <ti>ethereal</ti>
943     <ti>GUI tool to view all raw network data according to filters</ti>
944     </tr>
945     <tr>
946     <ti>tcpdump</ti>
947     <ti>Console tool to dump all raw network data according to filters</ti>
948     </tr>
949     <tr>
950     <ti>iptraf</ti>
951     <ti>ncurses based IP LAN monitor</ti>
952     </tr>
953     <tr>
954     <ti>ettercap</ti>
955     <ti>ncurses based network monitor/control</ti>
956     </tr>
957     </table>
958    
959     </body>
960     </section>
961    
962     <section>
963 vapier 1.31 <title>DHCP Fails To Start</title>
964     <body>
965    
966     <p>
967     When starting the dhcp init.d script for the first time, it may fail to load
968     but neglect to give you any useful info.
969     </p>
970    
971     <pre caption="DHCP Failing Example">
972     # <i>/etc/init.d/dhcp start</i>
973     * Setting ownership on dhcp.leases ... [ ok ]
974     * Starting dhcpd ... [ !! ]
975     </pre>
976    
977     <p>
978 vapier 1.49 The trick is to know where dhcpd is sending its output. Simply browse to
979     <path>/var/log</path> and read the log files. Since the exact log file depends
980 rane 1.46 on the package you are using as a syslog, try running <c>grep -Rl dhcpd
981 vapier 1.49 /var/log</c> to narrow down the possibilities. Chances are you made a typo in
982     your config file. You could also try running <c>dhcpd -d -f</c> (short for
983 rane 1.46 debug / foreground) and debug the error based upon the output.
984 vapier 1.31 </p>
985    
986     </body>
987     </section>
988    
989     <section>
990 vapier 1.27 <title>Incorrect MTU Value</title>
991     <body>
992    
993     <p>
994     If you experience odd errors (such as not being some webpages while others
995     load fine), you may be having Path MTU Discovery trouble. The quick way to
996     test is to run this iptables command:
997     </p>
998    
999     <pre caption="Circumvent MTU issues">
1000     # <i>iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu</i>
1001     </pre>
1002    
1003     <p>
1004     This will affect all new connections, so just refresh the website you're
1005     having problems with in order to test. In case it helps, the standard MTU
1006     value for 100mbit ethernet connections is <c>1500</c> while for PPPoE
1007     connections it is <c>1492</c>. For more info, you should read Chapter 15
1008     of the <uri link="http://lartc.org/howto/">Linux Advanced Routing &amp;
1009     Traffic Control HOWTO</uri>.
1010     </p>
1011    
1012     </body>
1013     </section>
1014    
1015 vapier 1.47 <section>
1016     <title>Unable to connect two machines directly</title>
1017     <body>
1018    
1019     <p>
1020     If (for whatever reason) you want to connect two machines directly together
1021 jkt 1.48 without a hub or switch, a regular ethernet cable will likely not work, unless
1022     you have an Auto MDI/MDI-X (also known as "autosensing") capable network
1023     adapter. You will need a different cable called a crossover cable. This <uri
1024 vapier 1.47 link="http://en.wikipedia.org/wiki/Ethernet_crossover_cable">Wikipedia</uri>
1025     page explains the low level details.
1026     </p>
1027    
1028     </body>
1029     </section>
1030    
1031 vapier 1.27 </chapter>
1032    
1033     <chapter>
1034 vapier 1.4 <title>Final Notes</title>
1035 vapier 1.3 <section>
1036     <body>
1037 neysx 1.23
1038 vapier 1.3 <p>
1039 neysx 1.23 I have no final notes other than if you experience any troubles with the guide,
1040     please contact <mail link="vapier@gentoo.org">me</mail> or file a bug with <uri
1041     link="http://bugs.gentoo.org/">Gentoo's Bugtracking Website</uri>. If you have
1042     some interesting bits you think would enhance this guide, by all means send it
1043     my way for inclusion.
1044 vapier 1.3 </p>
1045 neysx 1.23
1046 vapier 1.1 </body>
1047     </section>
1048     </chapter>
1049     </guide>

  ViewVC Help
Powered by ViewVC 1.1.20