2 #* reason -- why this script was called, one of: pre-init connect disconnect
3 #* VPNGATEWAY -- vpn gateway address (always present)
4 #* TUNDEV -- tunnel device (always present)
5 #* INTERNAL_IP4_ADDRESS -- address (always present)
6 #* INTERNAL_IP4_NETMASK -- netmask (often unset)
7 #* INTERNAL_IP4_NETMASKLEN -- netmask length (often unset)
8 #* INTERNAL_IP4_NETADDR -- address of network (only present if netmask is set)
9 #* INTERNAL_IP4_DNS -- list of dns serverss
10 #* INTERNAL_IP4_NBNS -- list of wins servers
11 #* CISCO_DEF_DOMAIN -- default domain name
12 #* CISCO_BANNER -- banner from server
13 #* CISCO_SPLIT_INC -- number of networks in split-network-list
14 #* CISCO_SPLIT_INC_%d_ADDR -- network address
15 #* CISCO_SPLIT_INC_%d_MASK -- subnet mask (for example: 255.255.255.0)
16 #* CISCO_SPLIT_INC_%d_MASKLEN -- subnet masklen (for example: 24)
17 #* CISCO_SPLIT_INC_%d_PROTOCOL -- protocol (often just 0)
18 #* CISCO_SPLIT_INC_%d_SPORT -- source port (often just 0)
19 #* CISCO_SPLIT_INC_%d_DPORT -- destination port (often just 0)
23 # Section A: route handling
25 # 1) The 3 values CISCO_SPLIT_INC_%d_PROTOCOL/SPORT/DPORT are currently being ignored
26 # In order to use them, we'll probably need os specific solutions
27 # * Linux: iptables -t mangle -I PREROUTING <conditions> -j ROUTE --oif $TUNDEV
28 # This would be an *alternative* to changing the routes (and thus 2) and 3)
29 # shouldn't be relevant at all)
30 # 2) There are two different functions to set routes: generic routes and the
31 # default route. Why isn't the defaultroute handled via the generic route case?
32 # 3) In the split tunnel case, all routes but the default route might get replaced
33 # without getting restored later. We should explicitely check and save them just
34 # like the defaultroute
35 # 4) Replies to a dhcp-server should never be sent into the tunnel
37 # Section B: Split DNS handling
39 # 1) Maybe dnsmasq can do something like that
40 # 2) Parse dns packets going out via tunnel and redirect them to original dns-server
45 # =========== script (variable) setup ====================================
47 PATH
=/sbin
:/usr
/sbin
:$PATH
51 DEFAULT_ROUTE_FILE
=/var
/run
/vpnc
/defaultroute
52 RESOLV_CONF_BACKUP
=/var
/run
/vpnc
/resolv.conf-backup
53 SCRIPTNAME
=`basename $0`
55 # some systems, eg. Darwin & FreeBSD, prune /var/run on boot
56 if [ ! -d "/var/run/vpnc" ]; then
57 mkdir
-p /var
/run
/vpnc
60 # stupid SunOS: no blubber in /usr/local/bin ... (on stdout)
61 IPROUTE
="`which ip | grep '^/' 2> /dev/null`"
63 if [ "$OS" = "Linux" ]; then
64 ifconfig_syntax_ptp
="pointopoint"
66 route_syntax_del
="del"
67 route_syntax_netmask
="netmask"
69 ifconfig_syntax_ptp
=""
71 route_syntax_del
="delete"
72 route_syntax_netmask
="-netmask"
74 if [ "$OS" = "SunOS" ]; then
75 route_syntax_interface
="-interface"
77 route_syntax_interface
=""
80 if [ -x /sbin
/resolvconf
]; then # Optional tool on Debian, Ubuntu, Gentoo
81 MODIFYRESOLVCONF
=modify_resolvconf_manager
82 RESTORERESOLVCONF
=restore_resolvconf_manager
83 elif [ -x /sbin
/modify_resolvconf
]; then # Mandatory tool on Suse earlier than 11.1
84 MODIFYRESOLVCONF
=modify_resolvconf_suse
85 RESTORERESOLVCONF
=restore_resolvconf_suse
86 else # Generic for any OS
87 MODIFYRESOLVCONF
=modify_resolvconf_generic
88 RESTORERESOLVCONF
=restore_resolvconf_generic
91 # =========== tunnel interface handling ====================================
94 if [ -n "$INTERNAL_IP4_MTU" ]; then
96 elif [ -n "$IPROUTE" ]; then
97 MTU
=$
(($
($IPROUTE route get
"$VPNGATEWAY" |
sed -ne 's/^.*mtu \([[:digit:]]\+\).*$/\1/p') - 88))
102 # Point to point interface require a netmask of 255.255.255.255 on some systems
103 ifconfig
"$TUNDEV" inet
"$INTERNAL_IP4_ADDRESS" $ifconfig_syntax_ptp "$INTERNAL_IP4_ADDRESS" netmask
255.255.255.255 mtu
${MTU} up
105 if [ -n "$INTERNAL_IP4_NETMASK" ]; then
106 set_network_route
$INTERNAL_IP4_NETADDR $INTERNAL_IP4_NETMASK $INTERNAL_IP4_NETMASKLEN
109 # If the netmask is provided, it contains the address _and_ netmask
110 if [ -n "$INTERNAL_IP6_NETMASK" -a -n "$IPROUTE" ]; then
111 $IPROUTE -6 addr add
$INTERNAL_IP6_NETMASK dev
$TUNDEV
112 elif [ -n "$INTERNAL_IP6_ADDRESS" -a -n "$IPROUTE" ]; then
113 $IPROUTE -6 addr add
$INTERNAL_IP6_ADDRESS/128 dev
$TUNDEV
117 destroy_tun_device
() {
119 NetBSD
) # and probably others...
120 ifconfig
"$TUNDEV" destroy
125 # =========== route handling ====================================
127 if [ -n "$IPROUTE" ]; then
128 fix_ip_get_output
() {
129 sed -e 's/ /\n/g' | \
130 sed -ne '1p;/via/{N;p};/dev/{N;p};/src/{N;p};/mtu/{N;p}'
133 set_vpngateway_route
() {
134 $IPROUTE route add
`$IPROUTE route get "$VPNGATEWAY" | fix_ip_get_output`
135 $IPROUTE route flush cache
138 del_vpngateway_route
() {
139 $IPROUTE route
$route_syntax_del "$VPNGATEWAY"
140 $IPROUTE route flush cache
143 set_default_route
() {
144 $IPROUTE route |
grep '^default' | fix_ip_get_output
> "$DEFAULT_ROUTE_FILE"
145 $IPROUTE route replace default dev
"$TUNDEV"
146 $IPROUTE route flush cache
149 set_network_route
() {
153 $IPROUTE route replace
"$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
154 $IPROUTE route flush cache
157 reset_default_route
() {
158 if [ -s "$DEFAULT_ROUTE_FILE" ]; then
159 $IPROUTE route replace
`cat "$DEFAULT_ROUTE_FILE"`
160 $IPROUTE route flush cache
161 rm -f -- "$DEFAULT_ROUTE_FILE"
165 del_network_route
() {
169 $IPROUTE route
$route_syntax_del "$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
170 $IPROUTE route flush cache
173 set_ipv6_default_route
() {
174 # We don't save/restore IPv6 default route; just add a higher-priority one.
175 $IPROUTE -6 route add default dev
"$TUNDEV" metric
1
176 $IPROUTE -6 route flush cache
179 set_ipv6_network_route
() {
182 $IPROUTE -6 route replace
"$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
183 $IPROUTE route flush cache
186 reset_ipv6_default_route
() {
187 $IPROUTE -6 route del default dev
"$TUNDEV"
188 $IPROUTE route flush cache
191 del_ipv6_network_route
() {
194 $IPROUTE -6 route del
"$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
195 $IPROUTE -6 route flush cache
197 else # use route command
199 # isn't -n supposed to give --numeric output?
201 # Get rid of lines containing IPv6 addresses (':')
202 netstat
-r -n |
awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $2; }'
205 set_vpngateway_route
() {
206 route add
-host "$VPNGATEWAY" $route_syntax_gw "`get_default_gw`"
209 del_vpngateway_route
() {
210 vpngw_route
=`netstat -r -n | awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $2; }'`
211 route
$route_syntax_del -host "$VPNGATEWAY" $route_syntax_gw "$vpngw_route"
214 set_default_route
() {
215 DEFAULTGW
="`get_default_gw`"
216 echo "$DEFAULTGW" > "$DEFAULT_ROUTE_FILE"
217 route
$route_syntax_del default
$route_syntax_gw "`get_default_gw`"
218 route add default
$route_syntax_gw "$INTERNAL_IP4_ADDRESS" $route_syntax_interface
221 set_network_route
() {
225 del_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
226 route add
-net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS" $route_syntax_interface
229 reset_default_route
() {
230 if [ -s "$DEFAULT_ROUTE_FILE" ]; then
231 route
$route_syntax_del default
$route_syntax_gw "`get_default_gw`" $route_syntax_interface
232 route add default
$route_syntax_gw `cat "$DEFAULT_ROUTE_FILE"`
233 rm -f -- "$DEFAULT_ROUTE_FILE"
237 del_network_route
() {
239 Linux|NetBSD|Darwin|SunOS
) # and probably others...
240 # routes are deleted automatically on device shutdown
247 route
$route_syntax_del -net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS"
250 set_ipv6_default_route
() {
255 set_ipv6_network_route
() {
260 reset_ipv6_default_route
() {
265 del_ipv6_network_route
() {
272 # =========== resolv.conf handling ====================================
274 # =========== resolv.conf handling for any OS =========================
276 modify_resolvconf_generic
() {
277 grep '^#@VPNC_GENERATED@' /etc
/resolv.conf
> /dev
/null
2>&1 ||
cp -- /etc
/resolv.conf
"$RESOLV_CONF_BACKUP"
278 NEW_RESOLVCONF
="#@VPNC_GENERATED@ -- this file is generated by vpnc
279 # and will be overwritten by vpnc
280 # as long as the above mark is intact"
282 # Remember the original value of CISCO_DEF_DOMAIN we need it later
283 CISCO_DEF_DOMAIN_ORIG
="$CISCO_DEF_DOMAIN"
284 # Don't step on INTERNAL_IP4_DNS value, use a temporary variable
285 INTERNAL_IP4_DNS_TEMP
="$INTERNAL_IP4_DNS"
286 exec 6< "$RESOLV_CONF_BACKUP"
287 while read LINE
<&6 ; do
290 if [ -n "$INTERNAL_IP4_DNS_TEMP" ]; then
291 read ONE_NAMESERVER INTERNAL_IP4_DNS_TEMP
<<-EOF
292 $INTERNAL_IP4_DNS_TEMP
294 LINE
="nameserver $ONE_NAMESERVER"
300 if [ -n "$CISCO_DEF_DOMAIN" ]; then
301 LINE
="$LINE $CISCO_DEF_DOMAIN"
306 if [ -n "$CISCO_DEF_DOMAIN" ]; then
307 LINE
="domain $CISCO_DEF_DOMAIN"
312 NEW_RESOLVCONF
="$NEW_RESOLVCONF
317 for i
in $INTERNAL_IP4_DNS_TEMP ; do
318 NEW_RESOLVCONF
="$NEW_RESOLVCONF
321 if [ -n "$CISCO_DEF_DOMAIN" ]; then
322 NEW_RESOLVCONF
="$NEW_RESOLVCONF
323 search $CISCO_DEF_DOMAIN"
325 echo "$NEW_RESOLVCONF" > /etc
/resolv.conf
327 if [ "$OS" = "Darwin" ]; then
329 # Skip for pre-10.4 systems
332 # 10.4 and later require use of scutil for DNS to work properly
335 if [ -n "$CISCO_SPLIT_INC" ]; then
336 if [ $CISCO_SPLIT_INC -lt 1 ]; then
337 # Must override for correct default route
338 # Cannot use multiple DNS matching in this case
339 OVERRIDE_PRIMARY
='d.add OverridePrimary # 1'
342 # Uncomment the following if/fi pair to use multiple
343 # DNS matching when available. When multiple DNS matching
344 # is present, anything reading the /etc/resolv.conf file
345 # directly will probably not work as intended.
346 #if [ -z "$CISCO_DEF_DOMAIN_ORIG" ]; then
347 # Cannot use multiple DNS matching without a domain
348 OVERRIDE_PRIMARY
='d.add OverridePrimary # 1'
350 scutil
>/dev
/null
2>&1 <<-EOF
353 d.add ServerAddresses * $INTERNAL_IP4_DNS
354 set State:/Network/Service/$TUNDEV/DNS
356 # next line overrides the default gateway and breaks split routing
357 # d.add Router $INTERNAL_IP4_ADDRESS
358 d.add Addresses * $INTERNAL_IP4_ADDRESS
359 d.add SubnetMasks * 255.255.255.255
360 d.add InterfaceName $TUNDEV
362 set State:/Network/Service/$TUNDEV/IPv4
365 if [ -n "$CISCO_DEF_DOMAIN_ORIG" ]; then
366 scutil
>/dev
/null
2>&1 <<-EOF
368 get State:/Network/Service/$TUNDEV/DNS
369 d.add DomainName $CISCO_DEF_DOMAIN_ORIG
370 d.add SearchDomains * $CISCO_DEF_DOMAIN_ORIG
371 d.add SupplementalMatchDomains * $CISCO_DEF_DOMAIN_ORIG
372 set State:/Network/Service/$TUNDEV/DNS
381 restore_resolvconf_generic
() {
382 if [ ! -e "$RESOLV_CONF_BACKUP" ]; then
385 grep '^#@VPNC_GENERATED@' /etc
/resolv.conf
> /dev
/null
2>&1 && cat "$RESOLV_CONF_BACKUP" > /etc
/resolv.conf
386 rm -f -- "$RESOLV_CONF_BACKUP"
388 if [ "$OS" = "Darwin" ]; then
390 # Skip for pre-10.4 systems
393 # 10.4 and later require use of scutil for DNS to work properly
395 scutil
>/dev
/null
2>&1 <<-EOF
397 remove State:/Network/Service/$TUNDEV/IPv4
398 remove State:/Network/Service/$TUNDEV/DNS
405 # === resolv.conf handling via /sbin/modify_resolvconf (Suse) =====================
407 # Suse provides a script that modifies resolv.conf. Use it because it will
408 # restart/reload all other services that care about it (e.g. lwresd).
410 modify_resolvconf_suse
()
412 FULL_SCRIPTNAME
=`readlink -f $0`
414 test -n "$INTERNAL_IP4_DNS" && RESOLV_OPTS
="-n \"$INTERNAL_IP4_DNS\""
415 test -n "$CISCO_DEF_DOMAIN" && RESOLV_OPTS
="$RESOLV_OPTS -d $CISCO_DEF_DOMAIN"
416 test -n "$RESOLV_OPTS" && eval /sbin
/modify_resolvconf modify
-s vpnc
-p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV $RESOLV_OPTS -t \"This
file was created by
$SCRIPTNAME\"
419 # Restore resolv.conf to old contents on Suse
420 restore_resolvconf_suse
()
422 FULL_SCRIPTNAME
=`readlink -f $0`
423 /sbin
/modify_resolvconf restore
-s vpnc
-p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV
426 # === resolv.conf handling via /sbin/resolvconf (Debian, Ubuntu, Gentoo)) =========
428 modify_resolvconf_manager
() {
430 for i
in $INTERNAL_IP4_DNS; do
431 NEW_RESOLVCONF
="$NEW_RESOLVCONF
434 if [ -n "$CISCO_DEF_DOMAIN" ]; then
435 NEW_RESOLVCONF
="$NEW_RESOLVCONF
436 domain $CISCO_DEF_DOMAIN"
438 echo "$NEW_RESOLVCONF" |
/sbin
/resolvconf
-a $TUNDEV
441 restore_resolvconf_manager
() {
442 /sbin
/resolvconf
-d $TUNDEV
445 # ========= Toplevel state handling =======================================
447 kernel_is_2_6_or_above
() {
459 if [ "$OS" = "Linux" ]; then
460 if (exec 6<> /dev
/net
/tun
) > /dev
/null
2>&1 ; then
462 else # can't open /dev/net/tun
463 test -e /proc
/sys
/kernel
/modprobe
&& `cat /proc/sys/kernel/modprobe` tun
2>/dev
/null
464 # fix for broken devfs in kernel 2.6.x
465 if [ "`readlink /dev/net/tun`" = misc
/net
/tun \
466 -a ! -e /dev
/net
/misc
/net
/tun
-a -e /dev
/misc
/net
/tun
] ; then
467 ln -sf /dev
/misc
/net
/tun
/dev
/net
/tun
469 # make sure tun device exists
470 if [ ! -e /dev
/net
/tun
]; then
472 mknod
-m 0640 /dev
/net
/tun c
10 200
474 # workaround for a possible latency caused by udev, sleep max. 10s
475 if kernel_is_2_6_or_above
; then
476 for x
in `seq 100` ; do
477 (exec 6<> /dev
/net
/tun
) > /dev
/null
2>&1 && break;
482 elif [ "$OS" = "FreeBSD" ]; then
483 if [ ! -e /dev
/tun
]; then
486 elif [ "$OS" = "GNU/kFreeBSD" ]; then
487 if [ ! -e /dev
/tun
]; then
490 elif [ "$OS" = "NetBSD" ]; then
492 elif [ "$OS" = "OpenBSD" ]; then
494 elif [ "$OS" = "SunOS" ]; then
496 elif [ "$OS" = "Darwin" ]; then
502 if [ -n "$CISCO_BANNER" ]; then
503 echo "Connect Banner:"
504 echo "$CISCO_BANNER" |
while read LINE
; do echo "|" "$LINE" ; done
510 if [ -n "$CISCO_SPLIT_INC" ]; then
512 while [ $i -lt $CISCO_SPLIT_INC ] ; do
513 eval NETWORK
="\${CISCO_SPLIT_INC_${i}_ADDR}"
514 eval NETMASK
="\${CISCO_SPLIT_INC_${i}_MASK}"
515 eval NETMASKLEN
="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
516 if [ $NETWORK != "0.0.0.0" ]; then
517 set_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
523 for i
in $INTERNAL_IP4_DNS ; do
524 if ! echo "$i" |
grep -q : ; then
525 set_network_route
"$i" "255.255.255.255" "32"
528 elif [ -n "$INTERNAL_IP4_ADDRESS" ]; then
531 if [ -n "$CISCO_IPV6_SPLIT_INC" ]; then
533 while [ $i -lt $CISCO_IPV6_SPLIT_INC ] ; do
534 eval NETWORK
="\${CISCO_IPV6_SPLIT_INC_${i}_ADDR}"
535 eval NETMASKLEN
="\${CISCO_IPV6_SPLIT_INC_${i}_MASKLEN}"
536 if [ $NETMASKLEN -lt 128 ]; then
537 set_ipv6_network_route
"$NETWORK" "$NETMASKLEN"
539 set_ipv6_default_route
543 for i
in $INTERNAL_IP4_DNS ; do
544 if echo "$i" |
grep -q : ; then
545 set_ipv6_network_route
"$i" "128"
548 elif [ -n "$INTERNAL_IP6_NETMASK" -o -n "$INTERNAL_IP6_ADDRESS" ]; then
549 set_ipv6_default_route
552 if [ -n "$INTERNAL_IP4_DNS" ]; then
558 if [ -n "$CISCO_SPLIT_INC" ]; then
560 while [ $i -lt $CISCO_SPLIT_INC ] ; do
561 eval NETWORK
="\${CISCO_SPLIT_INC_${i}_ADDR}"
562 eval NETMASK
="\${CISCO_SPLIT_INC_${i}_MASK}"
563 eval NETMASKLEN
="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
564 if [ $NETWORK != "0.0.0.0" ]; then
565 # FIXME: This doesn't restore previously overwritten
567 del_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
573 for i
in $INTERNAL_IP4_DNS ; do
574 del_network_route
"$i" "255.255.255.255" "32"
579 if [ -n "$CISCO_IPV6_SPLIT_INC" ]; then
581 while [ $i -lt $CISCO_IPV6_SPLIT_INC ] ; do
582 eval NETWORK
="\${CISCO_IPV6_SPLIT_INC_${i}_ADDR}"
583 eval NETMASKLEN
="\${CISCO_IPV6_SPLIT_INC_${i}_MASKLEN}"
584 if [ $NETMASKLEN -eq 128 ]; then
585 del_ipv6_network_route
"$NETWORK" "$NETMASKLEN"
587 reset_ipv6_default_route
591 for i
in $INTERNAL_IP6_DNS ; do
592 del_ipv6_network_route
"$i" "128"
595 reset_ipv6_default_route
600 if [ -n "$INTERNAL_IP4_DNS" ]; then
608 if [ -z "$reason" ]; then
609 echo "this script must be called from vpnc" 1>&2
624 echo "unknown reason '$reason'. Maybe vpnc-script is out of date" 1>&2