OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / doc / src / user_examples.html
1 <html>
2 <head>
3 <title>FreeS/WAN examples</title>
4 <meta name="keywords" content="Linux, IPsec, VPN, security, FreeSWAN, examples">
5
6 <!--
7
8 Written by Sandy Harris for the Linux FreeS/WAN project
9 Freely distributable under the GNU General Public License
10
11 More information at www.freeswan.org
12 Feedback to users@lists.freeswan.org
13
14 CVS information:
15 RCS ID:          $Id: user_examples.html,v 1.3 2001/12/29 17:06:10 sandy Exp $
16 Last changed:    $Date: 2001/12/29 17:06:10 $
17 Revision number: $Revision: 1.3 $
18
19 CVS revision numbers do not correspond to FreeS/WAN release numbers.
20 -->
21 </head>
22
23 <body>
24
25 <h1><a name="user.examples">FreeS/WAN script examples</a></h1>
26
27 This file is intended to hold a collection of user-written example
28 scripts or configuration files for use with FreeS/WAN.
29 <p>
30 So far it has only one entry.
31
32 <h2><a name="poltorak">Poltorak's Firewall script</a></h2>
33
34 <pre>
35 From: Poltorak Serguei &lt;poltorak@dataforce.net&gt;
36 Subject: [Users] Using FreeS/WAN
37 Date: Tue, 16 Oct 2001
38
39 Hello.
40
41 I'm using FreeS/WAN IPsec for half a year. I learned a lot of things about
42 it and I think it would be interesting for someone to see the result of my
43 experiments and usage of FreeS/WAN. If you find a mistake in this
44 file, please e-mail me. And excuse me for my english... I'm learning.. :)
45
46 I'll talk about vary simple configuration:
47
48 addresses prefix = 192.168
49
50     lan1          sgw1     .0.0/24 (Internet)       sgw2            lan2
51   .1.0/24---[ .1.1 ; .0.1 ]===================[ .0.10 ; . 2.10 ]---.2.0/24
52
53
54 We need to let lan1 see lan2 across Internet like it is behind sgw1. The
55 same for lan2. And we need to do IPX bridge for Novel Clients and NDS
56 synchronization.
57
58 my config:
59 ------------------- ipsec.conf -------------------
60 conn lan1-lan2
61         type=tunnel
62         compress=yes
63         #-------------------
64         left=192.168.0.1
65         leftsubnet=192.168.1.0/24
66         #-------------------
67         right=192.168.0.10
68         rightsubnet=192.168.2.0/24
69         #-------------------
70         auth=esp
71         authby=secret
72 --------------- end of ipsec.conf ----------------
73
74 ping .2.x from .1.y   (y != 1)
75 It works?? Fine. Let's continue...
76
77 Why y != 1 ?? Because kernel of sgw1 have 2 IP addresses and it will choose
78 the first IP (which is used to go to Internet) .0.1 and the packet won't go
79 through IPsec tunnel :(  But if do ping on .1.1 kernel will respond from
80 that address (.1.1) and the packet will be tunneled. The same problem occurred then
81 .2.x sends a packet to .1.2 which is down at the moment. What happens? .1.1
82 sends ARP requesting .1.2... after 3 tries it send to .2.x an destunreach,
83 but from his "natural" IP or .0.1 . So the error message won't be delivered!
84 It's a big problem...
85
86 Resolution... One can manipulate with ipsec0 or ipsec0:0 to solve the
87 problem (if ipsec0 has .1.1 kernel will send packets correctly), but there
88 are powerful and elegant iproute2 :) We simply need to change source address
89 of packet that goes to other secure lan. This is done with
90
91 ip route replace 192.168.2.0/24 via 192.168.0.10 dev ipsec0 src 192.168.1.1
92
93 Cool!! Now it works!!
94
95 The second step. We want install firewall on sgw1 and sgw2. Encryption of 
96 traffic without security isn't a good idea. I don't use {left|right}firewall, 
97 because I'm running firewall from init scripts.
98
99 We want IPsec data between lan1-lan2, some ICMP errors (destination
100 unreachable, TTL exceeded, parameter problem and source quench), replying on 
101 pings from both lans and Internet, ipxtunnel data for IPX and of course SSH
102 between sgw1 and sgw2 and from/to one specified host.
103
104 I'm using ipchains. With iptables there are some changes.
105
106 ---------------- rc.firewall ---------------------
107 #!/bin/sh
108 #
109 # Firewall for IPsec lan1-lan2
110 #
111
112 IPC=/sbin/ipchains
113 ANY=0.0.0.0/0
114
115 # left
116 SGW1_EXT=192.168.0.1
117 SGW1_INT=192.168.1.1
118 LAN1=192.168.1.0/24
119
120 # right
121 SGW2_EXT=192.168.0.10
122 SGW2_INT=192.168.2.10
123 LAN2=192.168.2.0/24
124
125 # SSH from and to this host
126 SSH_PEER_HOST=_SOME_HOST_
127
128 # this is for left. exchange these values for right.
129 MY_EXT=$SGW1_EXT
130 MY_INT=$SGW1_INT
131 PEER_EXT=$SGW2_EXT
132 PEER_INT=$SGW2_INT
133 INT_IF=eth1
134 EXT_IF=eth0
135 IPSEC_IF=ipsec0
136 MY_LAN=$LAN1
137 PEER_LAN=$LAN2
138
139 $IPC -F
140 $IPC -P input DENY
141 $IPC -P forward DENY
142 $IPC -P output DENY
143
144 # Loopback traffic
145 $IPC -A input -i lo -j ACCEPT
146 $IPC -A output -i lo -j ACCEPT
147
148 # for IPsec SGW1-SGW2
149 ## IKE
150 $IPC -A input -p udp -s $PEER_EXT 500 -d $MY_EXT 500 -i $EXT_IF -j ACCEPT
151 $IPC -A output -p udp -s $MY_EXT 500 -d $PEER_EXT 500 -i $EXT_IF -j ACCEPT
152 ## ESP
153 $IPC -A input -p 50 -s $PEER_EXT -d $MY_EXT -i $EXT_IF -j ACCEPT
154 ### we don't need this line ### $IPC -A output -p 50 -s $MY_EXT -d $PEER_EXT -i $EXT_IF -j ACCEPT
155 ## forward LAN1-LAN2
156 $IPC -A forward -s $MY_LAN -d $PEER_LAN -i $IPSEC_IF -j ACCEPT
157 $IPC -A forward -s $PEER_LAN -d $MY_LAN -i $INT_IF -j ACCEPT
158 $IPC -A output -s $PEER_LAN -d $MY_LAN -i $INT_IF -j ACCEPT
159 $IPC -A input -s $PEER_LAN -d $MY_LAN -i $IPSEC_IF -j ACCEPT
160 $IPC -A input -s $MY_LAN -d $PEER_LAN -i $INT_IF -j ACCEPT
161 $IPC -A output -s $MY_LAN -d $PEER_LAN -i $IPSEC_IF -j ACCEPT
162
163 # ICMP
164 #
165 ## Dest unreachable
166 ### from/to Internet
167 $IPC -A input -p icmp --icmp-type destination-unreachable -s $ANY -d $MY_EXT -i $EXT_IF -j ACCEPT
168 $IPC -A output -p icmp --icmp-type destination-unreachable -s $MY_EXT -d $ANY -i $EXT_IF -j ACCEPT
169 ### from/to Lan
170 $IPC -A input -p icmp --icmp-type destination-unreachable -s $ANY -d $MY_INT -i $INT_IF -j ACCEPT
171 $IPC -A output -p icmp --icmp-type destination-unreachable -s $MY_INT -d $ANY -i $INT_IF -j ACCEPT
172 ### from/to Peer Lan
173 $IPC -A input -p icmp --icmp-type destination-unreachable -s $ANY -d $MY_INT -i $IPSEC_IF -j ACCEPT
174 $IPC -A output -p icmp --icmp-type destination-unreachable -s $MY_INT -d $ANY -i $IPSEC_IF -j ACCEPT
175 #
176 ## Source quench
177 ### from/to Internet
178 $IPC -A input -p icmp --icmp-type source-quench -s $ANY -d $MY_EXT -i $EXT_IF -j ACCEPT
179 $IPC -A output -p icmp --icmp-type source-quench -s $MY_EXT -d $ANY -i $EXT_IF -j ACCEPT
180 ### from/to Lan
181 $IPC -A input -p icmp --icmp-type source-quench -s $ANY -d $MY_INT -i $INT_IF -j ACCEPT
182 $IPC -A output -p icmp --icmp-type source-quench -s $MY_INT -d $ANY -i $INT_IF -j ACCEPT
183 ### from/to Peer Lan
184 $IPC -A input -p icmp --icmp-type source-quench -s $ANY -d $MY_INT -i $IPSEC_IF -j ACCEPT
185 $IPC -A output -p icmp --icmp-type source-quench -s $MY_INT -d $ANY -i $IPSEC_IF -j ACCEPT
186 #
187 ## Parameter problem
188 ### from/to Internet
189 $IPC -A input -p icmp --icmp-type parameter-problem -s $ANY -d $MY_EXT -i $EXT_IF -j ACCEPT
190 $IPC -A output -p icmp --icmp-type parameter-problem -s $MY_EXT -d $ANY -i $EXT_IF -j ACCEPT
191 ### from/to Lan
192 $IPC -A input -p icmp --icmp-type parameter-problem -s $ANY -d $MY_INT -i $INT_IF -j ACCEPT
193 $IPC -A output -p icmp --icmp-type parameter-problem -s $MY_INT -d $ANY -i $INT_IF -j ACCEPT
194 ### from/to Peer Lan
195 $IPC -A input -p icmp --icmp-type parameter-problem -s $ANY -d $MY_INT -i $IPSEC_IF -j ACCEPT
196 $IPC -A output -p icmp --icmp-type parameter-problem -s $MY_INT -d $ANY -i $IPSEC_IF -j ACCEPT
197 #
198 ## Time To Live exceeded
199 ### from/to Internet
200 $IPC -A input -p icmp --icmp-type time-exceeded -s $ANY -d $MY_EXT -i $EXT_IF -j ACCEPT
201 $IPC -A output -p icmp --icmp-type time-exceeded -s $MY_EXT -d $ANY -i $EXT_IF -j ACCEPT
202 ### to Lan
203 $IPC -A input -p icmp --icmp-type time-exceeded -s $ANY -d $MY_INT -i $INT_IF -j ACCEPT
204 $IPC -A output -p icmp --icmp-type time-exceeded -s $MY_INT -d $ANY -i $INT_IF -j ACCEPT
205 ### to Peer Lan
206 $IPC -A input -p icmp --icmp-type time-exceeded -s $ANY -d $MY_INT -i $IPSEC_IF -j ACCEPT
207 $IPC -A output -p icmp --icmp-type time-exceeded -s $MY_INT -d $ANY -i $IPSEC_IF -j ACCEPT
208
209 # ICMP PINGs
210 ## from Internet
211 $IPC -A input -p icmp -s $ANY -d $MY_EXT --icmp-type echo-request  -i $EXT_IF -j ACCEPT
212 $IPC -A output -p icmp -s $MY_EXT -d $ANY --icmp-type echo-reply  -i $EXT_IF -j ACCEPT
213 ## from LAN
214 $IPC -A input -p icmp -s $ANY -d $MY_INT --icmp-type echo-request -i $INT_IF -j ACCEPT
215 $IPC -A output -p icmp -s $MY_INT -d $ANY --icmp-type echo-reply  -i $INT_IF -j ACCEPT
216 ## from Peer LAN
217 $IPC -A input -p icmp -s $ANY -d $MY_INT --icmp-type echo-request -i $IPSEC_IF -j ACCEPT
218 $IPC -A output -p icmp -s $MY_INT -d $ANY --icmp-type echo-reply  -i $IPSEC_IF -j ACCEPT
219
220 # SSH
221 ## from SSH_PEER_HOST
222 $IPC -A input -p tcp -s $SSH_PEER_HOST -d $MY_EXT 22 -i $EXT_IF -j ACCEPT
223 $IPC -A output -p tcp \! -y -s $MY_EXT 22 -d $SSH_PEER_HOST -i $EXT_IF -j ACCEPT
224 ## to SSH_PEER_HOST
225 $IPC -A input -p tcp \! -y -s $SSH_PEER_HOST 22 -d $MY_EXT -i $EXT_IF -j ACCEPT
226 $IPC -A output -p tcp -s $MY_EXT -d $SSH_PEER_HOST 22 -i $EXT_IF -j ACCEPT
227 ## from PEER
228 $IPC -A input -p tcp -s $PEER_EXT -d $MY_EXT 22 -i $EXT_IF -j ACCEPT
229 $IPC -A output -p tcp \! -y -s $MY_EXT 22 -d $PEER_EXT -i $EXT_IF -j ACCEPT
230 ## to PEER
231 $IPC -A input -p tcp \! -y -s $PEER_EXT 22 -d $MY_EXT -i $EXT_IF -j ACCEPT
232 $IPC -A output -p tcp -s $MY_EXT -d $PEER_EXT 22 -i $EXT_IF -j ACCEPT
233
234 # ipxtunnel
235 $IPC -A input -p udp -s $PEER_INT 2005 -d $MY_INT 2005 -i $IPSEC_IF -j ACCEPT
236 $IPC -A output -p udp -s $MY_INT 2005 -d $PEER_INT 2005 -i $IPSEC_IF -j ACCEPT
237
238 ---------------- end of rc.firewall ----------------------
239
240 To understand this we need to look on this scheme:
241
242            ++-----------------------&lt;----------------------------+
243            || ipsec0                                             |
244            \/                                                    |
245  eth0  +--------+    /---------/ yes  /---------/ yes +-----------------------+
246 ------&gt;| INPUT  |--&gt;/ ?local? /-----&gt;/ ?IPsec? /-----&gt;| decrypt & decapsulate |
247  eth1  +--------+  /---------/      /---------/       +-----------------------+
248                        || no            || no
249                        \/               \/
250                   +----------+      +---------+        +-------+
251                   | routing  |      |  local  |        | local |
252                   | decision |      | deliver |        | send  |
253                   +----------+      +---------+        +-------+
254                        ||                                 ||
255                        \/                                 \/
256                    +---------+                       +----------+
257                    | forward |                       | routing  |
258                    +---------+                       | decision |
259                        ||                            +----------+
260                        ||                                  ||
261                        ++----------------&lt;-----------------++
262                        ||
263                        \/
264                    +--------+ eth0
265                    | OUTPUT | eth1
266                    +--------+ ipsec0
267                        ||
268                        \/
269                    /---------/ yes +-----------------------+
270                   / ?IPsec? /-----&gt;| encrypt & encapsulate |
271                  /---------/       +-----------------------+
272                       || no                    ||
273                       ||                       ||
274                       ||                       \/   eth0, eth1
275                       ++-----------------------++--------------&gt;
276
277 This explain how a packet traverse TCP/IP stack in IPsec capable kernel.
278
279 FIX ME, please, if there are any errors
280
281 Test the new firewall now.
282
283
284 Now about IPX. I tried 3 programs for tunneling IPX: tipxd, SIB and ipxtunnel
285
286 tipxd didn't send packets.. :(
287 SIB and ipxtunnel worked fine :)
288 With ipxtunnel there was a little problem. In sources there are an error.
289
290 --------------------- in main.c ------------------------
291 &lt;       bytes += p.len;
292 ---
293 &gt;       bytes += len;
294 --------------------------------------------------------
295
296 After this FIX everything goes right...
297
298 ------------------- /etc/ipxtunnel.conf ----------------
299 port    2005
300 remote  192.168.101.97    2005
301 interface eth1
302 --------------- end of /etc/ipxtunnel.conf -------------
303
304 I use IPX tunnel between .1.1 and .2.10 so we don't need to encrypt nor
305 authenticate encapsulated IPX packets, it is done with IPsec.
306
307 If you don't wont to use iproute2 to change source IP you need to use SIB
308 (it is able to bind local address) or establish tunnel between .0.1 and
309 .0.10 (external IPs, you need to do encryption in the program, but it isn't
310 strong).
311
312 For now I'm using ipxtunnel.
313
314 I think that's all for the moment. If there are any error, please e-mail me: 
315 poltorak@df.ru . It would be cool if someone puts the scheme of TCP/IP in
316 kernel and firewall example on FreeS/WAN's manual pages.
317
318 PoltoS
319 </pre>
320
321 </body>
322 </html>