/[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.38 - (show annotations) (download) (as text)
Mon Dec 12 04:36:21 2005 UTC (8 years, 7 months ago) by vapier
Branch: MAIN
Changes since 1.37: +42 -3 lines
File MIME type: application/xml
add some notes on adding more LANs

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

  ViewVC Help
Powered by ViewVC 1.1.20