/[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.3 - (show annotations) (download) (as text)
Thu Jul 22 16:09:18 2004 UTC (9 years, 11 months ago) by vapier
Branch: MAIN
Changes since 1.2: +125 -9 lines
File MIME type: application/xml
start adding extras

1 <?xml version='1.0' encoding='UTF-8'?>
2 <!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/home-router-howto.xml,v 1.2 2004/07/22 14:32:26 vapier Exp $ -->
3 <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
4
5 <guide link="/doc/en/home-router-howto.xml">
6
7 <title>Home Router Guide</title>
8
9 <author title="Author">
10 <mail link="vapier@gentoo.org">Mike Frysinger</mail>
11 </author>
12
13 <abstract>
14 This document details how to turn an old Gentoo machine into a router
15 for connecting your home network to the internet.
16 </abstract>
17
18 <version>1.1</version>
19 <date>July 21, 2004</date>
20
21 <chapter>
22 <title>Introduction</title>
23 <section>
24 <body>
25
26 <p>
27 Building your own router out of old spare parts has many advantages
28 over buying a pre-made canned router by say Linksys. The biggest one by
29 far is control over the connection. The other advantages are left up to
30 your imagination; just about anything can be done in this scenario,
31 it's just a matter of needing it.
32 </p>
33
34 <p>
35 This guide will show you how to setup Network Address Translation (NAT)
36 on the router (kernel and iptables), add and configure common services
37 (Domain Name System (DNS) via dnsmasq, dhcp via dhcpcd, ADSL via
38 rp-pppoe), and conclude with more elaborate and fun things that can be
39 done (port forwarding, traffic shaping, proxies/caching, etc...).
40 </p>
41
42 <p>
43 Before getting started, there's a few basic requirements you must meet.
44 First, you'll need a computer that has at least 2 Network Interface
45 Cards (NICs) in it. Next, you'll need the configuration settings for
46 your internet connection (may include things like
47 IP/DNS/Gateway/username/password). Finally, you'll need a bit of spare
48 time and some Gentoo loving.
49 </p>
50
51 <p>
52 The conventions used in this guide are:
53 </p>
54 <ul>
55 <li>eth0 - NIC connected to the Local Area Network (LAN)</li>
56 <li>eth1 - NIC connected to the Wide Area Network (WAN)</li>
57 <li>LAN utilizes the private 192.168.0.xxx network</li>
58 <li>router is hardcoded to the standard 192.168.0.1 IP</li>
59 <li>router is running Linux 2.4 or 2.6; you're on your own with 2.0/2.2</li>
60 </ul>
61
62 <impo>
63 Due to security precautions, I would highly suggest you shut down any
64 unneeded services on the router until we have a chance to get the
65 firewall up and rolling. To view the currently running services, just
66 run <c>rc-status</c>.
67 </impo>
68
69 </body>
70 </section>
71 </chapter>
72
73 <chapter>
74 <title>Kernel setup (know thyself first)</title>
75 <section>
76 <body>
77
78 <p>
79 Your kernel needs to have the drivers running for both your NICs. To
80 see if your cards are already setup, just run <c>ifconfig</c>. Your
81 output may differ slightly from the following, that's fine. What
82 matters is that the interface shows up at all.
83 </p>
84 <pre caption="Checking NICs">
85 # <i>ifconfig -a</i>
86 eth0 Link encap:Ethernet HWaddr 00:60:F5:07:07:B8
87 BROADCAST MULTICAST MTU:1500 Metric:1
88 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
89 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
90 collisions:0 txqueuelen:1000
91 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
92 Interrupt:11 Base address:0x9800
93
94 eth1 Link encap:Ethernet HWaddr 00:60:F5:07:07:B9
95 BROADCAST MULTICAST MTU:1500 Metric:1
96 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
97 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
98 collisions:0 txqueuelen:1000
99 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
100 Interrupt:10 Base address:0x9400
101 </pre>
102 <p>
103 If you do not see your two cards showing up and you're not sure what
104 kind of cards you have, try running <c>lspci</c>. You can get that from
105 <c>emerge sys-apps/pciutils</c>. Look for "Ethernet controller" in the
106 output. Once you have this information, go into your kernel and add
107 support for the correct drivers.
108 </p>
109
110 <p>
111 The next thing you'll need is support for iptables and NAT (and packet
112 shaping if you want). The following list is split up into required
113 (*), suggested (x), and shaper (s) features. It does not matter whether
114 you build the features into the kernel or as a module so long as when
115 the feature is need, the correct module(s) are loaded (module loading
116 is left to the reader as a fun exercise however).
117 </p>
118 <pre caption="Network Options">
119 <i>Networking options ---&gt;</i>
120 <i> [*] TCP/IP networking</i>
121 <i> [*] IP: advanced router</i>
122 <i> [*] Network packet filtering (replaces ipchains)</i>
123
124 <i> IP: Netfilter Configuration ---&gt;</i>
125 <i> [*] Connection tracking (required for masq/NAT)</i>
126 <i> [x] FTP protocol support</i>
127 <i> [x] IRC protocol support</i>
128 <i> [*] IP tables support (required for filtering/masq/NAT)</i>
129 <i> [*] IP range match support</i>
130 <i> [x] MAC address match support</i>
131 <i> [*] Multiple port match support</i>
132 <i> [*] Packet filtering</i>
133 <i> [*] REJECT target support</i>
134 <i> [x] REDIRECT target support</i>
135 <i> [*] Full NAT</i>
136 <i> [*] MASQUERADE target support</i>
137 <i> [s] Packet mangling</i>
138 <i> [s] MARK target support</i>
139 <i> [x] LOG target support</i>
140
141 <i> QoS and/or fair queueing ---&gt;</i>
142 <i> [s] QoS and/or fair queueing</i>
143 <i> [s] HTB packet scheduler</i>
144 <i> [s] Ingress Qdisc</i>
145 </pre>
146 <note>
147 Somethings may be slightly different in a 2.4 vs 2.6 kernel, but you
148 should be able to figure it out :).
149 </note>
150
151 </body>
152 </section>
153 </chapter>
154
155 <chapter>
156 <title>Hug the WAN (a.k.a. The Internet)</title>
157
158 <section>
159 <title>Intro</title>
160 <body>
161 <p>
162 There are many ways to connect to the internet so I'll just cover the
163 ones I'm familiar with. That leaves us with ADSL (PPPoE) and cable
164 modems (static/dynamic). If there are other methods out there, feel
165 free to write up a little blurb and e-mail me. Feel free to skip any of
166 the following sections in this chapter that don't apply to you. This
167 chapter is just about getting the router connected to the internet via
168 eth1.
169 </p>
170 </body>
171 </section>
172
173 <section>
174 <title>ADSL and PPPoE</title>
175 <body>
176
177 <p>
178 All the fancy PPPoE software has been bundled up into one little nice
179 package nowadays called <uri link="http://www.roaringpenguin.com/">Roaring Penguin</uri>.
180 Simply <c>emerge rp-pppoe</c> and you'll be on your way. Remember how
181 I said you'll need username/password information? Well I wasn't lying
182 so I hope you have it now! Load up <path>/etc/ppp/pppoe.conf</path> in
183 your favorite editor and set it up.
184 </p>
185
186 <pre caption="Setting up eth1">
187 <comment>(Replace 'vla9h924' with your username and 'password' with your password)</comment>
188
189 # <i>nano /etc/ppp/pppoe.conf</i>
190 <comment># Ethernet card connected to ADSL modem
191 ETH=eth1
192 # ADSL user name.
193 USER=vla9h924</comment>
194 # <i>nano /etc/ppp/pap-secrets</i>
195 <comment># client server secret
196 "vla9h924" * "password"</comment>
197 # <i>nano /etc/conf.d/net</i>
198 <comment>Add an entry for ifconfig_eth1 and set it to adsl:
199 ifconfig_eth1=( "adsl" )</comment>
200 # <i>ln -s net.eth0 /etc/init.d/net.eth1</i>
201 # <i>rc-update add net.eth1 default</i>
202 # <i>/etc/init.d/net.eth1 start</i>
203 </pre>
204
205 <warn>
206 When the DSL interface comes up, it will create ppp0. Although your NIC
207 is called eth1, the IP is actually bound to ppp0. From now on, when you
208 see examples that utilize 'eth1', substitute with 'ppp0'.
209 </warn>
210
211 </body>
212 </section>
213
214 <section>
215 <title>Cable and/or dynamic/static IP</title>
216 <body>
217
218 <p>
219 If you have a static IP then you will need the few more details than if
220 you have a dynamic IP. For static users, you will need your IP,
221 gateway, and DNS servers.
222 </p>
223
224 <pre caption="Setting up eth1">
225 <comment>Dynamic IP Users:</comment>
226 # <i>emerge dhcpcd</i>
227 # <i>nano /etc/conf.d/net</i>
228 <comment>You'll need an entry like so:
229 ifconfig_eth1=( "dhcp" )</comment>
230
231 <comment>Static IP Users:</comment>
232 # <i>nano /etc/conf.d/net</i>
233 <comment>You'll need entries like so:
234 ifconfig_eth1=( "66.92.78.102 broadcast 66.92.78.255 netmask 255.255.255.0" )
235 routes_eth1=( "default gw 66.92.78.1" )</comment>
236 # <i>nano /etc/resolv.conf</i>
237 <comment>Add one line per DNS server:
238 nameserver 123.123.123.123</comment>
239
240 <comment>Dynamic and Static Setup:</comment>
241 # <i>ln -s net.eth0 /etc/init.d/net.eth1</i>
242 # <i>rc-update add net.eth1 default</i>
243 # <i>/etc/init.d/net.eth1 start</i>
244 </pre>
245
246 <p>
247 You should be all set to go now.
248 </p>
249
250 </body>
251 </section>
252 </chapter>
253
254 <chapter>
255 <title>Hug the LAN (bring along some friends)</title>
256 <section>
257 <body>
258
259 <p>
260 This step is a breeze compared to the previous one.
261 </p>
262
263 <pre caption="Setting up eth0">
264 # <i>nano /etc/conf.d/net</i>
265 <comment>Add a line like the following:
266 ifconfig_eth0=( "192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0" )</comment>
267 # <i>rc-update add net.eth0 default</i>
268 # <i>/etc/init.d/net.eth0 start</i>
269 </pre>
270
271 </body>
272 </section>
273 </chapter>
274
275 <chapter>
276 <title>LAN Services (because we're nice people)</title>
277
278 <section>
279 <title>DHCP Server</title>
280 <body>
281 <p>
282 I bet it'd be nice if everyone else in your house could just plug
283 their computers into the network and things would just work. No need to
284 remember mind-numbing details or make them stare at confusing
285 configuration screens! Life would be grand eh? Introducing the Dynamic
286 Host Configuration Protocol (DHCP) and why you should care.
287 </p>
288
289 <p>
290 DHCP is exactly what its name implies. It's a protocol that allows you
291 to dynamically configure other hosts automatically. You run a DHCP
292 server on the router (dhcpd), give it all the information about your
293 network (valid IPs, DNS servers, gateways, etc...), and then when the
294 other hosts start up, they run a DHCP client to automatically configure
295 themselves. No fuss, no muss! For even more information, you can
296 always visit <uri link="http://en.wikipedia.org/wiki/DHCP">Wikipedia</uri>.
297 </p>
298
299 <pre caption="Setting up dhcpd">
300 # <i>emerge dhcp</i>
301 # <i>nano /etc/dhcp/dhcpd.conf</i>
302 <comment>Here is a sample configuration file:
303 authoritative;
304 subnet 192.168.0.0 netmask 255.255.255.0 {
305 range 192.168.0.100 192.168.0.250;
306 default-lease-time 259200;
307 max-lease-time 518400;
308 option subnet-mask 255.255.255.0;
309 option broadcast-address 192.168.0.255;
310 option routers 192.168.0.1;
311 option domain-name-servers 192.168.0.1;
312 }
313 </comment>
314 # <i>nano /etc/conf.d/dhcp</i>
315 <comment>Set IFACE="eth0"</comment>
316 # <i>rc-update add dhcp default</i>
317 # <i>/etc/init.d/dhcp start</i>
318 </pre>
319
320 <p>
321 Now your little router is a bona-fide DHCP server! Plugin those
322 computers and watch them work! With Windows systems you should go into
323 the TCP/IP Properties and select the 'Obtain an IP address
324 automatically' and 'Obtain DNS server address automatically' options.
325 Sometimes the changes aren't instantaneous, so you may have to run a
326 command prompt and run <c>ipconfig /release</c> and <c>ipconfig
327 /renew</c>. But enough about Windows, let's get back to our favorite
328 penguin.
329 </p>
330 </body>
331 </section>
332
333 <section>
334 <title>DNS Server</title>
335 <body>
336 <p>
337 When people want to visit a place on the internet, they remember names,
338 not a string of useless numbers. After all, what's easier to remember,
339 ebay.com or 66.135.192.87? This is where the DNS steps in. DNS servers
340 run all over the internet, and whenever someone wants to visit 'ebay.com',
341 these servers turn 'ebay.com' (what we understand) into '66.135.192.87'
342 (what our computers understand). For even more information, you can
343 always visit <uri link="http://en.wikipedia.org/wiki/DNS">Wikipedia</uri>.
344 </p>
345
346 <p>
347 You may have noticed in the previous section that we told the DHCP
348 clients we have a DNS server at 192.168.0.1. You may also remember that
349 192.168.0.1 is our little router that we're making. I don't remember
350 setting up a DNS server ... so let's do so now!
351 </p>
352
353 <pre caption="Setting up dnsmasq">
354 # <i>emerge dnsmasq</i>
355 # <i>nano /etc/conf.d/dnsmasq</i>
356 <comment>Add "-i eth1" to DNSMASQ_OPTS</comment>
357 # <i>rc-update add dnsmasq</i>
358 # <i>/etc/init.d/dnsmasq start</i>
359 </pre>
360
361 <p>
362 Well that was quick, but what did we do? The great thing is, we didn't
363 have to do very much! You're welcome to choose other DNS servers if
364 you're more comfortable with them, but the reason dnsmasq is great is
365 because it was designed to do exactly what we want it for. It's a
366 little DNS caching/forwarding server for local networks. We're not
367 looking to provide our own DNS server here, just offer simple DNS
368 services to everyone else on our LAN.
369 </p>
370
371 </body>
372 </section>
373
374 <section>
375 <title>NAT</title>
376 <body>
377
378 <p>
379 At this point, people on your network can talk to each other and they
380 can look up hostnames via DNS, but they still can't actually connect to
381 the internet. While you may think that's great (more bandwidth for
382 you!), I bet they're not too happy just yet.
383 </p>
384
385 <p>
386 This is where NAT steps in. NAT is a way of connecting multiple computers
387 in a private LAN to the internet when you only have a smaller number of
388 IP addresses availabe to you. Typically you were given 1 IP by your ISP,
389 but you want to let your whole house connect to the internet. NAT is the
390 magic that makes this possible. For even more information, you can
391 always visit <uri link="http://en.wikipedia.org/wiki/NAT">Wikipedia</uri>.
392 </p>
393
394 <note>
395 Before we get started, make sure you have iptables on your system. Although
396 it is automatically installed on most systems, you may not have it. If you
397 don't, just run <c>emerge iptables</c>.
398 </note>
399
400 <pre caption="Setting up iptables">
401 <comment>First we flush our current rules</comment>
402 # <i>iptables -F</i>
403 # <i>iptables -t nat -F</i>
404
405 <comment>Then we lock our services so they only work from the LAN</comment>
406 # <i>iptables -I INPUT 1 -i eth0 -j ACCEPT</i>
407 # <i>iptables -I INPUT 1 -i lo -j ACCEPT</i>
408 # <i>iptables -A INPUT -p UDP --dport bootps -i ! eth0 -j REJECT</i>
409 # <i>iptables -A INPUT -p UDP --dport dns -i ! eth0 -j REJECT</i>
410
411 <comment>Drop TCP / UDP packets to privileged ports</comment>
412 # <i>iptables -A INPUT -p TCP -i ! eth0 -d 0/0 --dport 0:1023 -j DROP</i>
413 # <i>iptables -A INPUT -p UDP -i ! eth0 -d 0/0 --dport 0:1023 -j DROP</i>
414
415 <comment>Finally we add the rules for NAT</comment>
416 # <i>iptables -I FORWARD -i 192.168.0.0/255.255.0.0 -j DROP</i>
417 # <i>iptables -A FORWARD -s 192.168.0.0/255.255.0.0 -j ACCEPT</i>
418 # <i>iptables -A FORWARD -d 192.168.0.0/255.255.0.0 -j ACCEPT</i>
419 # <i>iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE</i>
420 <comment>Tell the kernel that ip forwarding is OK</comment>
421 # <i>echo 1 > /proc/sys/net/ipv4/ip_forward</i>
422 # <i>for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done</i>
423
424 <comment>This is so when we boot we don't have to run the rules by hand</comment>
425 # <i>/etc/init.d/iptables save</i>
426 # <i>rc-update add iptables default</i>
427 </pre>
428
429 <p>
430 Once you've typed out all of that, the rest of your network should now
431 be able to use the internet as if they were directly connected
432 themselves.
433 </p>
434
435 </body>
436 </section>
437 </chapter>
438
439 <chapter>
440 <title>Fun Things (for a rainy day)</title>
441
442 <section>
443 <title>Intro</title>
444 <body>
445 <p>
446 Believe it or not, you're done :). From here on out, I'll cover a bunch
447 of common topics that may interest you. Everything in this chapter is
448 completely optional.
449 </p>
450 </body>
451 </section>
452
453 <section>
454 <title>Port Forwarding</title>
455 <body>
456 <p>
457 Sometimes you would like to be able to host services on a computer behind
458 the router, or just to make your life easier when connecting remotely.
459 Perhaps you want to run a FTP, HTTP, SSH, or VNC server on one or more
460 machines behind your router and be able to connect to them all. The only
461 caveat is that you can only have one service/machine combo per port.
462 For example, there is no practical way to setup three FTP servers behind
463 your router and then try to connect to them all through port 21; only one
464 can be on port 21 while the others would have to be on say port 123 and
465 port 567.
466 </p>
467
468 <p>
469 All the port forwarding rules are of the form <c>iptables -t nat -A PREROUTING
470 [-p protocol] --dport [external port on router] -i eth1 -j DNAT --to [ip/port
471 to forward to]</c>. iptables does not accept hostnames when port forwarding.
472 If you are forwarding an external port to the same port on the internal machine,
473 you can omit the destination port. See the iptables(8) page for more information.
474 </p>
475
476 <pre>
477 <comment>Forward port 2 to ssh on an internal host</comment>
478 # <i>iptables -t nat -A PREROUTING -p tcp --dport 2 -i eth1 -j DNAT --to 192.168.0.2:22</i>
479
480 <comment>FTP forwarding to an internal host</comment>
481 # <i>iptables -t nat -A PREROUTING -p tcp --dport 21 -i eth1 -j DNAT --to 192.168.0.56</i>
482
483 <comment>HTTP forwarding to an internal host</comment>
484 # <i>iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth1 -j DNAT --to 192.168.0.56</i>
485
486 <comment>VNC forwarding for internal hosts</comment>
487 # <i>iptables -t nat -I PREROUTING -p tcp --dport 5900 -i eth1 -j DNAT --to 192.168.0.2</i>
488 # <i>iptables -t nat -I PREROUTING -p tcp --dport 5901 -i eth1 -j DNAT --to 192.168.0.3:5900</i>
489 <comment>If you want to VNC in to 192.168.0.3, then just add ':1' to the router's hostname</comment>
490
491 <comment>Bittorrent forwarding</comment>
492 # <i>iptables -t nat -A PREROUTING -p tcp --dport 6881:6889 -i eth1 -j DNAT --to 192.168.0.2</i>
493 </pre>
494
495 <note>
496 If you have other common / cool examples, please <uri link="mailto:vapier@gentoo.org">e-mail me</uri>.
497 </note>
498 </body>
499 </section>
500
501 <section>
502 <title>Traffic Shaping</title>
503 <body>
504 <p>
505 </p>
506 </body>
507 </section>
508
509 <section>
510 <title>Identd (for IRC)</title>
511 <body>
512 <p>
513 Internet Relay Chat utilizes the ident service pretty heavily. Now that
514 the IRC clients are behind the router, we need a way to host ident for
515 both the router and the clients. One such server has been created
516 called <c>midentd</c>.
517 </p>
518
519 <pre caption="Setting up ident">
520 # <i>emerge midentd</i>
521 # <i>rc-update add midentd default</i>
522 # <i>/etc/init.d/midentd start</i>
523 </pre>
524
525 <p>
526 There are a few other ident servers in portage. Depending on your needs,
527 I would recommend checking out <c>oidentd</c> and <c>fakeidentd</c>.
528 </p>
529 </body>
530 </section>
531
532 <section>
533 <title>Mail Server</title>
534 <body>
535 <p>
536 </p>
537 </body>
538 </section>
539
540 <section>
541 <title>HTTP Proxy</title>
542 <body>
543 <p>
544 </p>
545 </body>
546 </section>
547
548 <section>
549 <title>POP Scanning</title>
550 <body>
551 <p>
552 </p>
553 </body>
554 </section>
555
556 </chapter>
557
558 </guide>

  ViewVC Help
Powered by ViewVC 1.1.20