DragonFly 3.2 supports the Areca ARC-1882 RAID controller family
[ikiwiki.git] / docs / handbook / handbook-network-natd.mdwn
1 \r
2 \r
3 ## 19.13 Network Address Translation \r
4 \r
5 ***Contributed by Chern Lee. ***\r
6 \r
7 ### 19.13.1 Overview \r
8 \r
9 DragonFly's Network Address Translation daemon, commonly known as [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) is a daemon that accepts incoming raw IP packets, changes the source to the local machine and re-injects these packets back into the outgoing IP packet stream. [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=natd&section=8) does this by changing the source IP address and port such that when data is received back, it is able to determine the original location of the data and forward it back to its original requester.\r
10 \r
11 The most common use of NAT is to perform what is commonly known as Internet Connection Sharing.\r
12 \r
13 ### 19.13.2 Setup \r
14 \r
15 Due to the diminishing IP space in IPv4, and the increased number of users on high-speed consumer lines such as cable or DSL, people are increasingly in need of an Internet Connection Sharing solution. The ability to connect several computers online through one connection and IP address makes [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) a reasonable choice.\r
16 \r
17 Most commonly, a user has a machine connected to a cable or DSL line with one IP address and wishes to use this one connected computer to provide Internet access to several more over a LAN.\r
18 \r
19 To do this, the DragonFly machine on the Internet must act as a gateway. This gateway machine must have two NICs--one for connecting to the Internet router, the other connecting to a LAN. All the machines on the LAN are connected through a hub or switch.\r
20 \r
21 advanced-networking/natd.png\r
22 \r
23 A setup like this is commonly used to share an Internet connection. One of the LAN machines is connected to the Internet. The rest of the machines access the Internet through that ***gateway*** machine.\r
24 \r
25 ### 19.13.3 Configuration \r
26 \r
27 The following options must be in the kernel configuration file:\r
28 \r
29     \r
30     options IPFIREWALL\r
31     options IPDIVERT\r
32 \r
33 \r
34 Additionally, at choice, the following may also be suitable:\r
35 \r
36     \r
37     options IPFIREWALL_DEFAULT_TO_ACCEPT\r
38     options IPFIREWALL_VERBOSE\r
39 \r
40 \r
41 The following must be in `/etc/rc.conf`:\r
42 \r
43     \r
44     gateway_enable="YES"\r
45     firewall_enable="YES"\r
46     firewall_type="OPEN"\r
47     natd_enable="YES"\r
48     natd_interface="fxp0"\r
49     natd_flags=""\r
50 \r
51 \r
52 [[!table  data="""
53 | gateway_enable#"YES" | Sets up the machine to act as a gateway. Running `sysctl net.inet.ip.forwarding1` would have the same effect. 
54  firewall_enable="YES" | Enables the firewall rules in `/etc/rc.firewall` at boot. 
55  firewall_type="OPEN" | This specifies a predefined firewall ruleset that allows anything in. See `/etc/rc.firewall` for additional types. 
56  natd_interface="fxp0" | Indicates which interface to forward packets through (the interface connected to the Internet). 
57  natd_flags#"" | Any additional configuration options passed to [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?commandnatd&section=8) on boot. |\r
58 """]]\r
59 Having the previous options defined in `/etc/rc.conf` would run `natd -interface fxp0` at boot. This can also be run manually.\r
60 \r
61  **Note:** It is also possible to use a configuration file for [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) when there are too many options to pass. In this case, the configuration file must be defined by adding the following line to `/etc/rc.conf`:\r
62 \r
63     \r
64     natd_flags="-f /etc/natd.conf"\r
65 \r
66 \r
67 The `/etc/natd.conf` file will contain a list of configuration options, one per line. For example the next section case would use the following file:\r
68 \r
69     \r
70     redirect_port tcp 192.168.0.2:6667 6667\r
71     redirect_port tcp 192.168.0.3:80 80\r
72 \r
73 \r
74 For more information about the configuration file, consult the [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) manual page about the `-f` option.\r
75 \r
76 Each machine and interface behind the LAN should be assigned IP address numbers in the private network space as defined by [RFC 1918](ftp://ftp.isi.edu/in-notes/rfc1918.txt) and have a default gateway of the  **natd**  machine's internal IP address.\r
77 \r
78 For example, client `A` and `B` behind the LAN have IP addresses of `192.168.0.2` and `192.168.0.3`, while the natd machine's LAN interface has an IP address of `192.168.0.1`. Client `A` and `B`'s default gateway must be set to that of the  **natd**  machine, `192.168.0.1`. The  **natd**  machine's external, or Internet interface does not require any special modification for [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) to work.\r
79 \r
80 ### 19.13.4 Port Redirection \r
81 \r
82 The drawback with [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) is that the LAN clients are not accessible from the Internet. Clients on the LAN can make outgoing connections to the world but cannot receive incoming ones. This presents a problem if trying to run Internet services on one of the LAN client machines. A simple way around this is to redirect selected Internet ports on the  **natd**  machine to a LAN client.\r
83 \r
84 For example, an IRC server runs on client `A`, and a web server runs on client `B`. For this to work properly, connections received on ports 6667 (IRC) and 80 (web) must be redirected to the respective machines.\r
85 \r
86 The `-redirect_port` must be passed to [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) with the proper options. The syntax is as follows:\r
87 \r
88     \r
89          -redirect_port proto targetIP:targetPORT[-targetPORT]\r
90                      [aliasIP:]aliasPORT[-aliasPORT]\r
91                      [remoteIP[:remotePORT[-remotePORT]]]\r
92 \r
93 \r
94 In the above example, the argument should be:\r
95 \r
96     \r
97         -redirect_port tcp 192.168.0.2:6667 6667\r
98         -redirect_port tcp 192.168.0.3:80 80\r
99 \r
100 \r
101 This will redirect the proper ***tcp*** ports to the LAN client machines.\r
102 \r
103 The `-redirect_port` argument can be used to indicate port ranges over individual ports. For example, `***tcp 192.168.0.2:2000-3000 2000-3000***` would redirect all connections received on ports 2000 to 3000 to ports 2000 to 3000 on client `A`.\r
104 \r
105 These options can be used when directly running [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8), placed within the `natd_flags=""` option in `/etc/rc.conf`, or passed via a configuration file.\r
106 \r
107 For further configuration options, consult [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8)\r
108 \r
109 ### 19.13.5 Address Redirection \r
110 \r
111 Address redirection is useful if several IP addresses are available, yet they must be on one machine. With this, [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#natd&section8) can assign each LAN client its own external IP address. [natd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=natd&section=8) then rewrites outgoing packets from the LAN clients with the proper external IP address and redirects all traffic incoming on that particular IP address back to the specific LAN client. This is also known as static NAT. For example, the IP addresses `128.1.1.1`, `128.1.1.2`, and `128.1.1.3` belong to the  **natd**  gateway machine. `128.1.1.1` can be used as the  **natd**  gateway machine's external IP address, while `128.1.1.2` and `128.1.1.3` are forwarded back to LAN clients `A` and `B`.\r
112 \r
113 The `-redirect_address` syntax is as follows:\r
114 \r
115     \r
116     -redirect_address localIP publicIP\r
117 \r
118 \r
119 [[!table  data="""
120 | localIP | The internal IP address of the LAN client. 
121  publicIP | The external IP address corresponding to the LAN client. |\r
122 """]]\r
123 In the example, this argument would read:\r
124 \r
125     \r
126     -redirect_address 192.168.0.2 128.1.1.2\r
127     -redirect_address 192.168.0.3 128.1.1.3\r
128 \r
129 \r
130 Like `-redirect_port`, these arguments are also placed within the `natd_flags=""` option of `/etc/rc.conf`, or passed via a configuration file. With address redirection, there is no need for port redirection since all data received on a particular IP address is redirected.\r
131 \r
132 The external IP addresses on the  **natd**  machine must be active and aliased to the external interface. Look at [rc.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command#rc.conf&section5) to do so.\r
133 \r
134 \r
135 \r
136 CategoryHandbook\r
137 CategoryHandbook-advancednetworking\r