2 # iproute2 version, default updown script
4 # Copyright (C) 2003-2004 Nigel Meteringham
5 # Copyright (C) 2003-2004 Tuomo Soini
6 # Copyright (C) 2002-2004 Michael Richardson
7 # Copyright (C) 2005-2007 Andreas Steffen <andreas.steffen@strongswan.org>
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the
11 # Free Software Foundation; either version 2 of the License, or (at your
12 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 # RCSID $Id: _updown.in,v 1.2 2006/04/17 15:06:29 as Exp $
21 # CAUTION: Installing a new version of strongSwan will install a new
22 # copy of this script, wiping out any custom changes you make. If
23 # you need changes, make a copy of this under another name, and customize
24 # that, and use the (left/right)updown parameters in ipsec.conf to make
25 # strongSwan use yours instead of this default one.
27 # things that this script gets (from ipsec_pluto(8) man page)
30 # indicates what version of this interface is being
31 # used. This document describes version 1.1. This
32 # is upwardly compatible with version 1.0.
35 # specifies the name of the operation to be performed
36 # (prepare-host, prepare-client, up-host, up-client,
37 # down-host, or down-client). If the address family
38 # for security gateway to security gateway communica
39 # tions is IPv6, then a suffix of -v6 is added to the
43 # is the name of the connection for which we are
47 # is the next hop to which packets bound for the peer
51 # is the name of the ipsec interface to be used.
54 # is the requid of the ESP policy
57 # is the IP address of our host.
60 # is the ID of our host.
63 # is the IP address / count of our client subnet. If
64 # the client is just the host, this will be the
65 # host's own IP address / max (where max is 32 for
66 # IPv4 and 128 for IPv6).
69 # is the IP address of our client net. If the client
70 # is just the host, this will be the host's own IP
73 # PLUTO_MY_CLIENT_MASK
74 # is the mask for our client net. If the client is
75 # just the host, this will be 255.255.255.255.
78 # if non-empty, then the source address for the route will be
79 # set to this IP address.
82 # is the IP protocol that will be transported.
85 # is the UDP/TCP port to which the IPsec SA is
86 # restricted on our side.
89 # is the IP address of our peer.
92 # is the ID of our peer.
95 # is the CA which issued the cert of our peer.
98 # is the IP address / count of the peer's client sub
99 # net. If the client is just the peer, this will be
100 # the peer's own IP address / max (where max is 32
101 # for IPv4 and 128 for IPv6).
103 # PLUTO_PEER_CLIENT_NET
104 # is the IP address of the peer's client net. If the
105 # client is just the peer, this will be the peer's
108 # PLUTO_PEER_CLIENT_MASK
109 # is the mask for the peer's client net. If the
110 # client is just the peer, this will be
113 # PLUTO_PEER_PROTOCOL
114 # is the IP protocol that will be transported.
117 # is the UDP/TCP port to which the IPsec SA is
118 # restricted on the peer side.
121 # uncomment to log VPN connections
124 # tag put in front of each log entry:
127 # syslog facility and priority used:
128 FAC_PRIO=local0.notice
130 # to create a special vpn logging file, put the following line into
131 # the syslog configuration file /etc/syslog.conf:
133 # local0.notice -/var/log/vpn
135 # in order to use source IP routing the Linux kernel options
136 # CONFIG_IP_ADVANCED_ROUTER and CONFIG_IP_MULTIPLE_TABLES
139 # special routing table for sourceip routes
140 SOURCEIP_ROUTING_TABLE=100
142 # priority of the sourceip routing table
143 SOURCEIP_ROUTING_TABLE_PRIO=100
145 # check interface version
146 case "$PLUTO_VERSION" in
147 1.[0|1]) # Older Pluto?!? Play it safe, script may be using new features.
148 echo "$0: obsolete interface version \`$PLUTO_VERSION'," >&2
149 echo "$0: called by obsolete Pluto?" >&2
153 *) echo "$0: unknown interface version \`$PLUTO_VERSION'" >&2
162 iptables:iptables) # due to (left/right)firewall; for default script only
164 custom:*) # custom parameters (see above CAUTION comment)
166 *) echo "$0: unknown parameters \`$*'" >&2
171 # utility functions for route manipulation
172 # Meddling with this stuff should not be necessary and requires great care.
184 if ! ip -o route get ${PLUTO_MY_SOURCEIP%/*} | grep -q ^local
186 it="ip addr add ${PLUTO_MY_SOURCEIP%/*}/32 dev $PLUTO_INTERFACE"
187 oops="`eval $it 2>&1`"
189 if test " $oops" = " " -a " $st" != " 0"
191 oops="silent error, exit status $st"
193 if test " $oops" != " " -o " $st" != " 0"
195 echo "$0: addsource \`$it' failed ($oops)" >&2
204 if [ -z "$PLUTO_MY_SOURCEIP" ]
206 if [ -f /etc/sysconfig/defaultsource ]
208 . /etc/sysconfig/defaultsource
211 if [ -f /etc/conf.d/defaultsource ]
213 . /etc/conf.d/defaultsource
216 if [ -n "$DEFAULTSOURCE" ]
218 PLUTO_MY_SOURCEIP=$DEFAULTSOURCE
222 if [ -z "$KLIPS" -a -z "$PLUTO_MY_SOURCEIP" ]
224 # leave because no route entry is required
228 parms1="$PLUTO_PEER_CLIENT"
230 if [ -n "$PLUTO_NEXT_HOP" ]
232 parms2="via $PLUTO_NEXT_HOP"
234 parms2="via $PLUTO_PEER"
236 parms2="$parms2 dev $PLUTO_INTERFACE"
239 if [ -n "$PLUTO_MY_SOURCEIP" ]
244 if [ `ip rule list | grep "lookup ${SOURCEIP_ROUTING_TABLE}" | wc -l` -eq 0 ]
246 ip rule add pref ${SOURCEIP_ROUTING_TABLE_PRIO} table ${SOURCEIP_ROUTING_TABLE}
249 parms3="$parms3 src ${PLUTO_MY_SOURCEIP%/*} table ${SOURCEIP_ROUTING_TABLE}"
252 case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
254 # opportunistic encryption work around
255 # need to provide route that eclipses default, without
257 it="ip route $1 0.0.0.0/1 $parms2 $parms3 &&
258 ip route $1 128.0.0.0/1 $parms2 $parms3"
260 *) it="ip route $1 $parms1 $parms2 $parms3"
263 oops="`eval $it 2>&1`"
265 if test " $oops" = " " -a " $st" != " 0"
267 oops="silent error, exit status $st"
269 if test " $oops" != " " -o " $st" != " 0"
271 echo "$0: doroute \`$it' failed ($oops)" >&2
276 # in the presence of KLIPS and ipsecN interfaces do not use IPSEC_POLICY
277 if [ `echo "$PLUTO_INTERFACE" | grep "ipsec"` ]
284 IPSEC_POLICY="-m policy --pol ipsec --proto esp --reqid $PLUTO_REQID"
285 IPSEC_POLICY_IN="$IPSEC_POLICY --dir in"
286 IPSEC_POLICY_OUT="$IPSEC_POLICY --dir out"
289 # are there port numbers?
290 if [ "$PLUTO_MY_PORT" != 0 ]
292 S_MY_PORT="--sport $PLUTO_MY_PORT"
293 D_MY_PORT="--dport $PLUTO_MY_PORT"
295 if [ "$PLUTO_PEER_PORT" != 0 ]
297 S_PEER_PORT="--sport $PLUTO_PEER_PORT"
298 D_PEER_PORT="--dport $PLUTO_PEER_PORT"
302 case "$PLUTO_VERB:$1" in
303 prepare-host:*|prepare-client:*)
304 if [ -z "$KLIPS" -a -z "$PLUTO_MY_SOURCEIP" ]
306 # exit because no route will be added,
307 # so that existing routes can stay
311 # delete possibly-existing route (preliminary to adding a route)
312 case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
314 # need to provide route that eclipses default, without
318 it="ip route delete $parms1 2>&1 ; ip route delete $parms2 2>&1"
319 oops="`ip route delete $parms1 2>&1 ; ip route delete $parms2 2>&1`"
322 parms="$PLUTO_PEER_CLIENT"
323 it="ip route delete $parms 2>&1"
324 oops="`ip route delete $parms 2>&1`"
328 if test " $oops" = " " -a " $status" != " 0"
330 oops="silent error, exit status $status"
333 *'RTNETLINK answers: No such process'*)
334 # This is what route (currently -- not documented!) gives
335 # for "could not find such a route".
340 if test " $oops" != " " -o " $status" != " 0"
342 echo "$0: \`$it' failed ($oops)" >&2
346 route-host:*|route-client:*)
347 # connection to me or my client subnet being routed
350 unroute-host:*|unroute-client:*)
351 # connection to me or my client subnet being unrouted
355 # connection to me coming up
356 # If you are doing a custom version, firewall commands go here.
359 # connection to me going down
360 # If you are doing a custom version, firewall commands go here.
363 # connection to my client subnet coming up
364 # If you are doing a custom version, firewall commands go here.
367 # connection to my client subnet going down
368 # If you are doing a custom version, firewall commands go here.
371 # connection to me, with (left/right)firewall=yes, coming up
372 # This is used only by the default updown script, not by your custom
373 # ones, so do not mess with it; see CAUTION comment up at top.
374 iptables -I INPUT 1 -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
375 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
376 -d $PLUTO_ME $D_MY_PORT $IPSEC_POLICY_IN -j ACCEPT
377 iptables -I OUTPUT 1 -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
378 -s $PLUTO_ME $S_MY_PORT $IPSEC_POLICY_OUT \
379 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT -j ACCEPT
381 # log IPsec host connection setup
384 if [ "$PLUTO_PEER_CLIENT" == "$PLUTO_PEER/32" ]
386 logger -t $TAG -p $FAC_PRIO \
387 "+ `echo -e $PLUTO_PEER_ID` $PLUTO_PEER -- $PLUTO_ME"
389 logger -t $TAG -p $FAC_PRIO \
390 "+ `echo -e $PLUTO_PEER_ID` $PLUTO_PEER_CLIENT == $PLUTO_PEER -- $PLUTO_ME"
395 # connection to me, with (left/right)firewall=yes, going down
396 # This is used only by the default updown script, not by your custom
397 # ones, so do not mess with it; see CAUTION comment up at top.
398 iptables -D INPUT -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
399 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
400 -d $PLUTO_ME $D_MY_PORT $IPSEC_POLICY_IN -j ACCEPT
401 iptables -D OUTPUT -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
402 -s $PLUTO_ME $S_MY_PORT $IPSEC_POLICY_OUT \
403 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT -j ACCEPT
405 # log IPsec host connection teardown
408 if [ "$PLUTO_PEER_CLIENT" == "$PLUTO_PEER/32" ]
410 logger -t $TAG -p $FAC_PRIO -- \
411 "- `echo -e $PLUTO_PEER_ID` $PLUTO_PEER -- $PLUTO_ME"
413 logger -t $TAG -p $FAC_PRIO -- \
414 "- `echo -e $PLUTO_PEER_ID` $PLUTO_PEER_CLIENT == $PLUTO_PEER -- $PLUTO_ME"
419 # connection to client subnet, with (left/right)firewall=yes, coming up
420 # This is used only by the default updown script, not by your custom
421 # ones, so do not mess with it; see CAUTION comment up at top.
422 if [ "$PLUTO_PEER_CLIENT" != "$PLUTO_MY_SOURCEIP/32" ]
424 iptables -I FORWARD 1 -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
425 -s $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $S_MY_PORT \
426 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT \
427 $IPSEC_POLICY_OUT -j ACCEPT
428 iptables -I FORWARD 1 -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
429 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
430 -d $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $D_MY_PORT \
431 $IPSEC_POLICY_IN -j ACCEPT
434 # a virtual IP requires an INPUT and OUTPUT rule on the host
435 # or sometimes host access via the internal IP is needed
436 if [ -n "$PLUTO_MY_SOURCEIP" -o -n "$PLUTO_HOST_ACCESS" ]
438 iptables -I INPUT 1 -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
439 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
440 -d $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $D_MY_PORT \
441 $IPSEC_POLICY_IN -j ACCEPT
442 iptables -I OUTPUT 1 -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
443 -s $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $S_MY_PORT \
444 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT \
445 $IPSEC_POLICY_OUT -j ACCEPT
448 # log IPsec client connection setup
451 if [ "$PLUTO_PEER_CLIENT" == "$PLUTO_PEER/32" ]
453 logger -t $TAG -p $FAC_PRIO \
454 "+ `echo -e $PLUTO_PEER_ID` $PLUTO_PEER -- $PLUTO_ME == $PLUTO_MY_CLIENT"
456 logger -t $TAG -p $FAC_PRIO \
457 "+ `echo -e $PLUTO_PEER_ID` $PLUTO_PEER_CLIENT == $PLUTO_PEER -- $PLUTO_ME == $PLUTO_MY_CLIENT"
461 down-client:iptables)
462 # connection to client subnet, with (left/right)firewall=yes, going down
463 # This is used only by the default updown script, not by your custom
464 # ones, so do not mess with it; see CAUTION comment up at top.
465 if [ "$PLUTO_PEER_CLIENT" != "$PLUTO_MY_SOURCEIP/32" ]
467 iptables -D FORWARD -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
468 -s $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $S_MY_PORT \
469 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT \
470 $IPSEC_POLICY_OUT -j ACCEPT
471 iptables -D FORWARD -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
472 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
473 -d $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $D_MY_PORT \
474 $IPSEC_POLICY_IN -j ACCEPT
477 # a virtual IP requires an INPUT and OUTPUT rule on the host
478 # or sometimes host access via the internal IP is needed
479 if [ -n "$PLUTO_MY_SOURCEIP" -o -n "$PLUTO_HOST_ACCESS" ]
481 iptables -D INPUT -i $PLUTO_INTERFACE -p $PLUTO_MY_PROTOCOL \
482 -s $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $S_PEER_PORT \
483 -d $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $D_MY_PORT \
484 $IPSEC_POLICY_IN -j ACCEPT
485 iptables -D OUTPUT -o $PLUTO_INTERFACE -p $PLUTO_PEER_PROTOCOL \
486 -s $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK $S_MY_PORT \
487 -d $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK $D_PEER_PORT \
488 $IPSEC_POLICY_OUT -j ACCEPT
491 # log IPsec client connection teardown
494 if [ "$PLUTO_PEER_CLIENT" == "$PLUTO_PEER/32" ]
496 logger -t $TAG -p $FAC_PRIO -- \
497 "- `echo -e $PLUTO_PEER_ID` $PLUTO_PEER -- $PLUTO_ME == $PLUTO_MY_CLIENT"
499 logger -t $TAG -p $FAC_PRIO -- \
500 "- `echo -e $PLUTO_PEER_ID` $PLUTO_PEER_CLIENT == $PLUTO_PEER -- $PLUTO_ME == $PLUTO_MY_CLIENT"
507 prepare-host-v6:*|prepare-client-v6:*)
509 route-host-v6:*|route-client-v6:*)
510 # connection to me or my client subnet being routed
513 unroute-host-v6:*|unroute-client-v6:*)
514 # connection to me or my client subnet being unrouted
518 # connection to me coming up
519 # If you are doing a custom version, firewall commands go here.
522 # connection to me going down
523 # If you are doing a custom version, firewall commands go here.
526 # connection to my client subnet coming up
527 # If you are doing a custom version, firewall commands go here.
530 # connection to my client subnet going down
531 # If you are doing a custom version, firewall commands go here.
533 *) echo "$0: unknown verb \`$PLUTO_VERB' or parameter \`$1'" >&2