Modified to include macports method
[wiki] / examples / masquerading-firewall.mdwn
1 [[!meta title="tinc from behind a masquerading firewall"]]
2
3 ## Example: tinc from behind a masquerading firewall
4
5 When running tinc from behind a masquerading firewall (not on the firewall
6 itself), one must be careful to configure the firewall so that it allows the
7 tinc traffic to pass through without altering the source and destination ports.
8 Example firewall rules are included in this example. They are written for
9 iptables (Linux 2.4 firewall code), but commented so that you may apply the
10 same kind of rules to other firewalls. 
11
12 [[!toc levels=2]]
13
14 ### Overview
15
16 [[!img examples/fig-firewall.png]]
17
18 The network setup is as follows:
19
20 * Internal network is 10.20.30.0/24
21 * Firewall IP is 123.234.123.1 on the outside, 10.20.30.1/24 on the inside.
22 * Host running tinc has IP 10.20.30.42
23 * VPN the host wants to connect to has address range 192.168.0.0/16
24 * The host has it's own VPN IP 192.168.10.20 
25
26 ### Configuration of the host running tinc
27
28 >     host# ifconfig
29 >     eth0      Link encap:Ethernet  HWaddr 00:20:30:40:50:60
30 >               inet addr:10.20.30.42  Bcast:10.20.30.255  Mask:255.255.255.0
31 >               UP BROADCAST RUNNING  MTU:1500  Metric:1
32 >               ...
33 >     
34 >     lo        Link encap:Local Loopback
35 >               inet addr:127.0.0.1  Mask:255.0.0.0
36 >               UP LOOPBACK RUNNING  MTU:3856  Metric:1
37 >               ...
38 >     
39 >     vpn       Link encap:Point-to-Point Protocol
40 >               inet addr:192.168.10.20  P-t-P:192.168.10.20  Mask:255.255.0.0
41 >               UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
42 >               ...
43 >     
44 >     host# route
45 >     Kernel IP routing table
46 >     Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
47 >     10.20.30.0      *               255.255.255.0   U     0      0        0 eth0
48 >     192.168.0.0     *               255.255.0.0     U     0      0        0 vpn
49 >     default         10.20.30.1      0.0.0.0         UG    0      0        0 eth0
50 >     
51 >     host# iptables -L -v
52 >     Chain INPUT (policy ACCEPT 1234 packets, 123K bytes)
53 >      pkts bytes target     prot opt in     out     source               destination
54 >     
55 >     Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
56 >      pkts bytes target     prot opt in     out     source               destination
57 >     
58 >     Chain OUTPUT (policy ACCEPT 2161K packets, 364M bytes)
59 >      pkts bytes target     prot opt in     out     source               destination
60 >     
61 >     host# iptables -L -v -t nat
62 >     Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
63 >      pkts bytes target     prot opt in     out     source               destination
64 >     
65 >     Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
66 >      pkts bytes target     prot opt in     out     source               destination
67 >     
68 >     Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
69 >      pkts bytes target     prot opt in     out     source               destination
70
71 ### Configuration of tinc
72
73 >     host# cat /etc/tinc/vpn/tinc.conf
74 >     Name = atwork
75 >     Device = /dev/tun
76 >     ConnectTo = home
77 >     
78 >     host# cat /etc/tinc/vpn/tinc-up
79 >     #!/bin/sh
80 >     
81 >     ifconfig vpn 192.168.10.20 netmask 255.255.0.0
82 >     
83 >     host# ls /etc/tinc/vpn/hosts
84 >     atwork  home
85 >     
86 >     host# cat /etc/tinc/vpn/hosts/atwork
87 >     Address = 123.234.123.1
88 >     Subnet = 192.168.10.20/32
89 >     -----BEGIN RSA PUBLIC KEY-----
90 >     ...
91 >     -----END RSA PUBLIC KEY-----
92 >     
93 >     host# cat /etc/tinc/vpn/hosts/home
94 >     Address = 200.201.202.203
95 >     Subnet = 192.168.1.0/24
96 >     -----BEGIN RSA PUBLIC KEY-----
97 >     ...
98 >     -----END RSA PUBLIC KEY-----
99
100 ### Configuration of the firewall
101
102 >     firewall# ifconfig
103 >     ppp0      Link encap:Point-to-Point Protocol
104 >               inet addr:123.234.123.1  P-t-P:123.234.120.1  Mask:255.255.255.255
105 >               UP POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1
106 >               ...
107 >     
108 >     eth0      Link encap:Ethernet  HWaddr 00:20:13:14:15:16
109 >               inet addr:10.20.30.1  Bcast:10.20.30.255  Mask:255.255.255.0
110 >               UP BROADCAST RUNNING  MTU:1500  Metric:1
111 >               ...
112 >     
113 >     lo        Link encap:Local Loopback
114 >               inet addr:127.0.0.1  Mask:255.0.0.0
115 >               UP LOOPBACK RUNNING  MTU:3856  Metric:1
116 >               ...
117 >     
118 >     firewall# route
119 >     Kernel IP routing table
120 >     Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
121 >     10.20.30.0      *               255.255.255.0   U     0      0        0 eth0
122 >     default         123.234.120.1   0.0.0.0         UG    0      0        0 ppp0
123 >     
124 >     firewall# iptables -L -v
125 >     Chain INPUT (policy ACCEPT 1234 packets, 123K bytes)
126 >      pkts bytes target     prot opt in     out     source               destination
127 >     
128 >     Chain FORWARD (policy DROP 1234 packets, 123K bytes)
129 >      pkts bytes target     prot opt in     out     source               destination
130 >      1234  123K ACCEPT     any  --  ppp0   eth0    anywhere             10.20.30.0/24
131 >      1234  123K ACCEPT     any  --  eth0   ppp0    10.20.30.0/24        anywhere
132 >     
133 >     Chain OUTPUT (policy ACCEPT 2161K packets, 364M bytes)
134 >      pkts bytes target     prot opt in     out     source               destination
135 >     
136 >     firewall# iptables -L -v -t nat
137 >     Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
138 >      pkts bytes target     prot opt in     out     source               destination
139 >      1234  123K DNAT       tcp  --  ppp0   any     anywhere             anywhere           tcp dpt:655 to:10.20.30.42:655
140 >      1234  123K DNAT       udp  --  ppp0   any     anywhere             anywhere           udp dpt:655 to:10.20.30.42:655
141 >     
142 >     Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
143 >      pkts bytes target     prot opt in     out     source               destination
144 >      1234  123K MASQUERADE all  --  eth0   ppp0    anywhere             anywhere
145 >     
146 >     Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
147 >      pkts bytes target     prot opt in     out     source               destination
148 >     
149 >     firewall# cat /etc/init.d/firewall
150 >     #!/bin/sh
151 >     
152 >     echo 1 >/proc/sys/net/ipv4/ip_forward
153 >     
154 >     iptables -P FORWARD DROP
155 >     iptables -F FORWARD
156 >     iptables -A FORWARD -j ACCEPT -i ppp0 -o eth0 -d 10.20.30.0/24
157 >     iptables -A FORWARD -j ACCEPT -i eth0 -o ppp0 -s 10.20.30.0/24
158 >     
159 >     iptables -t nat -F POSTROUTING
160 >     # Next rule prevents masquerading from altering source port of outbound tinc packets
161 >     iptables -t nat -A POSTROUTING -p udp -m udp -sport 655 -j MASQUERADE -o ppp0 --to-ports 655
162 >     iptables -t nat -A POSTROUTING -j MASQUERADE -o ppp0
163 >     
164 >     iptables -t nat -F PREROUTING
165 >     # Next two rules forward incoming tinc packets to the host behind the firewall running tinc
166 >     iptables -t nat -A PREROUTING -j DNAT -i ppp0 -p tcp --dport 655 --to 10.20.30.42:655
167 >     iptables -t nat -A PREROUTING -j DNAT -i ppp0 -p udp --dport 655 --to 10.20.30.42:655