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 FULL_SCRIPTNAME
=@PREFIX@
/sbin
/vpnc
54 SCRIPTNAME
=`basename $FULL_SCRIPTNAME`
56 # some systems, eg. Darwin & FreeBSD, prune /var/run on boot
57 if [ ! -d "/var/run/vpnc" ]; then
58 mkdir
-p /var
/run
/vpnc
61 # stupid SunOS: no blubber in /usr/local/bin ... (on stdout)
62 IPROUTE
="`which ip | grep '^/' 2> /dev/null`"
64 if [ "$OS" = "Linux" ]; then
65 ifconfig_syntax_ptp
="pointopoint"
67 route_syntax_del
="del"
68 route_syntax_netmask
="netmask"
70 ifconfig_syntax_ptp
=""
72 route_syntax_del
="delete"
73 route_syntax_netmask
="-netmask"
76 if [ -x /sbin
/resolvconf
]; then # Optional tool on Debian, Ubuntu, Gentoo
77 MODIFYRESOLVCONF
=modify_resolvconf_manager
78 RESTORERESOLVCONF
=restore_resolvconf_manager
79 elif [ -x /sbin
/modify_resolvconf
]; then # Mandatory tool on Suse earlier than 11.1
80 MODIFYRESOLVCONF
=modify_resolvconf_suse
81 RESTORERESOLVCONF
=restore_resolvconf_suse
82 else # Generic for any OS
83 MODIFYRESOLVCONF
=modify_resolvconf_generic
84 RESTORERESOLVCONF
=restore_resolvconf_generic
87 # =========== tunnel interface handling ====================================
90 if [ -n "$INTERNAL_IP4_MTU" ]; then
92 elif [ -n "$IPROUTE" ]; then
93 DEV
=$
($IPROUTE route |
grep ^default |
sed 's/^.* dev \([[:alnum:]-]\+\).*$/\1/')
94 MTU
=$
(($
($IPROUTE link show
"$DEV" |
grep mtu |
sed 's/^.* mtu \([[:digit:]]\+\).*$/\1/') - 88))
99 # Point to point interface require a netmask of 255.255.255.255 on some systems
100 ifconfig
"$TUNDEV" inet
"$INTERNAL_IP4_ADDRESS" $ifconfig_syntax_ptp "$INTERNAL_IP4_ADDRESS" netmask
255.255.255.255 mtu
${MTU} up
102 if [ -n "$INTERNAL_IP4_NETMASK" ]; then
103 set_network_route
$INTERNAL_IP4_NETADDR $INTERNAL_IP4_NETMASK $INTERNAL_IP4_NETMASKLEN
107 destroy_tun_device
() {
109 NetBSD
) # and probably others...
110 ifconfig
"$TUNDEV" destroy
115 # =========== route handling ====================================
117 if [ -n "$IPROUTE" ]; then
118 fix_ip_get_output
() {
119 sed 's/cache//;s/metric \?[0-9]\+ [0-9]\+//g;s/hoplimit [0-9]\+//g'
122 set_vpngateway_route
() {
123 $IPROUTE route add
`$IPROUTE route get "$VPNGATEWAY" | fix_ip_get_output`
124 $IPROUTE route flush cache
127 del_vpngateway_route
() {
128 $IPROUTE route
$route_syntax_del "$VPNGATEWAY"
129 $IPROUTE route flush cache
132 set_default_route
() {
133 $IPROUTE route |
grep '^default' | fix_ip_get_output
> "$DEFAULT_ROUTE_FILE"
134 $IPROUTE route replace default dev
"$TUNDEV"
135 $IPROUTE route flush cache
138 set_network_route
() {
142 $IPROUTE route replace
"$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
143 $IPROUTE route flush cache
146 reset_default_route
() {
147 if [ -s "$DEFAULT_ROUTE_FILE" ]; then
148 $IPROUTE route replace
`cat "$DEFAULT_ROUTE_FILE"`
149 $IPROUTE route flush cache
150 rm -f -- "$DEFAULT_ROUTE_FILE"
154 del_network_route
() {
158 $IPROUTE route
$route_syntax_del "$NETWORK/$NETMASKLEN" dev
"$TUNDEV"
159 $IPROUTE route flush cache
161 else # use route command
163 # isn't -n supposed to give --numeric output?
165 # Get rid of lines containing IPv6 addresses (':')
166 netstat
-r -n |
awk '/:/ { next; } /^(default|0\.0\.0\.0)/ { print $2; }'
169 set_vpngateway_route
() {
170 route add
-host "$VPNGATEWAY" $route_syntax_gw "`get_default_gw`"
173 del_vpngateway_route
() {
174 route
$route_syntax_del -host "$VPNGATEWAY"
177 set_default_route
() {
178 DEFAULTGW
="`get_default_gw`"
179 echo "$DEFAULTGW" > "$DEFAULT_ROUTE_FILE"
180 route
$route_syntax_del default
181 route add default
$route_syntax_gw "$INTERNAL_IP4_ADDRESS"
184 set_network_route
() {
188 del_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
189 route add
-net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS"
192 reset_default_route
() {
193 if [ -s "$DEFAULT_ROUTE_FILE" ]; then
194 route
$route_syntax_del default
195 route add default
$route_syntax_gw `cat "$DEFAULT_ROUTE_FILE"`
196 rm -f -- "$DEFAULT_ROUTE_FILE"
200 del_network_route
() {
202 Linux|NetBSD|Darwin
) # and probably others...
203 # routes are deleted automatically on device shutdown
210 route
$route_syntax_del -net "$NETWORK" $route_syntax_netmask "$NETMASK" $route_syntax_gw "$INTERNAL_IP4_ADDRESS"
214 # =========== resolv.conf handling ====================================
216 # =========== resolv.conf handling for any OS =========================
218 modify_resolvconf_generic
() {
219 grep '^#@VPNC_GENERATED@' /etc
/resolv.conf
> /dev
/null
2>&1 ||
cp -- /etc
/resolv.conf
"$RESOLV_CONF_BACKUP"
220 NEW_RESOLVCONF
="#@VPNC_GENERATED@ -- this file is generated by vpnc
221 # and will be overwritten by vpnc
222 # as long as the above mark is intact"
224 # Remember the original value of CISCO_DEF_DOMAIN we need it later
225 CISCO_DEF_DOMAIN_ORIG
="$CISCO_DEF_DOMAIN"
226 # Don't step on INTERNAL_IP4_DNS value, use a temporary variable
227 INTERNAL_IP4_DNS_TEMP
="$INTERNAL_IP4_DNS"
228 exec 6< "$RESOLV_CONF_BACKUP"
229 while read LINE
<&6 ; do
232 if [ -n "$INTERNAL_IP4_DNS_TEMP" ]; then
233 read ONE_NAMESERVER INTERNAL_IP4_DNS_TEMP
<<-EOF
234 $INTERNAL_IP4_DNS_TEMP
236 LINE
="nameserver $ONE_NAMESERVER"
242 if [ -n "$CISCO_DEF_DOMAIN" ]; then
243 LINE
="$LINE $CISCO_DEF_DOMAIN"
248 if [ -n "$CISCO_DEF_DOMAIN" ]; then
249 LINE
="domain $CISCO_DEF_DOMAIN"
254 NEW_RESOLVCONF
="$NEW_RESOLVCONF
259 for i
in $INTERNAL_IP4_DNS_TEMP ; do
260 NEW_RESOLVCONF
="$NEW_RESOLVCONF
263 if [ -n "$CISCO_DEF_DOMAIN" ]; then
264 NEW_RESOLVCONF
="$NEW_RESOLVCONF
265 search $CISCO_DEF_DOMAIN"
267 echo "$NEW_RESOLVCONF" > /etc
/resolv.conf
269 if [ "$OS" = "Darwin" ]; then
271 # Skip for pre-10.4 systems
274 # 10.4 and later require use of scutil for DNS to work properly
277 if [ -n "$CISCO_SPLIT_INC" ]; then
278 if [ $CISCO_SPLIT_INC -lt 1 ]; then
279 # Must override for correct default route
280 # Cannot use multiple DNS matching in this case
281 OVERRIDE_PRIMARY
='d.add OverridePrimary # 1'
284 # Uncomment the following if/fi pair to use multiple
285 # DNS matching when available. When multiple DNS matching
286 # is present, anything reading the /etc/resolv.conf file
287 # directly will probably not work as intended.
288 #if [ -z "$CISCO_DEF_DOMAIN_ORIG" ]; then
289 # Cannot use multiple DNS matching without a domain
290 OVERRIDE_PRIMARY
='d.add OverridePrimary # 1'
292 scutil
>/dev
/null
2>&1 <<-EOF
295 d.add ServerAddresses * $INTERNAL_IP4_DNS
296 set State:/Network/Service/$TUNDEV/DNS
298 # next line overrides the default gateway and breaks split routing
299 # d.add Router $INTERNAL_IP4_ADDRESS
300 d.add Addresses * $INTERNAL_IP4_ADDRESS
301 d.add SubnetMasks * 255.255.255.255
302 d.add InterfaceName $TUNDEV
304 set State:/Network/Service/$TUNDEV/IPv4
307 if [ -n "$CISCO_DEF_DOMAIN_ORIG" ]; then
308 scutil
>/dev
/null
2>&1 <<-EOF
310 get State:/Network/Service/$TUNDEV/DNS
311 d.add DomainName $CISCO_DEF_DOMAIN_ORIG
312 d.add SearchDomains * $CISCO_DEF_DOMAIN_ORIG
313 d.add SupplementalMatchDomains * $CISCO_DEF_DOMAIN_ORIG
314 set State:/Network/Service/$TUNDEV/DNS
323 restore_resolvconf_generic
() {
324 if [ ! -e "$RESOLV_CONF_BACKUP" ]; then
327 grep '^#@VPNC_GENERATED@' /etc
/resolv.conf
> /dev
/null
2>&1 && cat "$RESOLV_CONF_BACKUP" > /etc
/resolv.conf
328 rm -f -- "$RESOLV_CONF_BACKUP"
330 if [ "$OS" = "Darwin" ]; then
332 # Skip for pre-10.4 systems
335 # 10.4 and later require use of scutil for DNS to work properly
337 scutil
>/dev
/null
2>&1 <<-EOF
339 remove State:/Network/Service/$TUNDEV/IPv4
340 remove State:/Network/Service/$TUNDEV/DNS
347 # === resolv.conf handling via /sbin/modify_resolvconf (Suse) =====================
349 # Suse provides a script that modifies resolv.conf. Use it because it will
350 # restart/reload all other services that care about it (e.g. lwresd).
352 modify_resolvconf_suse
()
355 test -n "$INTERNAL_IP4_DNS" && RESOLV_OPTS
="-n \"$INTERNAL_IP4_DNS\""
356 test -n "$CISCO_DEF_DOMAIN" && RESOLV_OPTS
="$RESOLV_OPTS -d $CISCO_DEF_DOMAIN"
357 test -n "$RESOLV_OPTS" && eval /sbin
/modify_resolvconf modify
-s $SCRIPTNAME -p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV $RESOLV_OPTS -t \"This
file was created by
$SCRIPTNAME\"
360 # Restore resolv.conf to old contents on Suse
361 restore_resolvconf_suse
()
363 /sbin
/modify_resolvconf restore
-s vpnc
-p $SCRIPTNAME -f $FULL_SCRIPTNAME -e $TUNDEV
366 # === resolv.conf handling via /sbin/resolvconf (Debian, Ubuntu, Gentoo)) =========
368 modify_resolvconf_manager
() {
370 for i
in $INTERNAL_IP4_DNS; do
371 NEW_RESOLVCONF
="$NEW_RESOLVCONF
374 if [ -n "$CISCO_DEF_DOMAIN" ]; then
375 NEW_RESOLVCONF
="$NEW_RESOLVCONF
376 domain $CISCO_DEF_DOMAIN"
378 echo "$NEW_RESOLVCONF" |
/sbin
/resolvconf
-a $TUNDEV
381 restore_resolvconf_manager
() {
382 /sbin
/resolvconf
-d $TUNDEV
385 # ========= Toplevel state handling =======================================
387 kernel_is_2_6_or_above
() {
399 if [ "$OS" = "Linux" ]; then
400 if (exec 6<> /dev
/net
/tun
) > /dev
/null
2>&1 ; then
402 else # can't open /dev/net/tun
403 test -e /proc
/sys
/kernel
/modprobe
&& `cat /proc/sys/kernel/modprobe` tun
2>/dev
/null
404 # fix for broken devfs in kernel 2.6.x
405 if [ "`readlink /dev/net/tun`" = misc
/net
/tun \
406 -a ! -e /dev
/net
/misc
/net
/tun
-a -e /dev
/misc
/net
/tun
] ; then
407 ln -sf /dev
/misc
/net
/tun
/dev
/net
/tun
409 # make sure tun device exists
410 if [ ! -e /dev
/net
/tun
]; then
412 mknod
-m 0640 /dev
/net
/tun c
10 200
414 # workaround for a possible latency caused by udev, sleep max. 10s
415 if kernel_is_2_6_or_above
; then
416 for x
in `seq 100` ; do
417 (exec 6<> /dev
/net
/tun
) > /dev
/null
2>&1 && break;
422 elif [ "$OS" = "FreeBSD" ]; then
423 if [ ! -e /dev
/tun
]; then
426 elif [ "$OS" = "GNU/kFreeBSD" ]; then
427 if [ ! -e /dev
/tun
]; then
430 elif [ "$OS" = "NetBSD" ]; then
432 elif [ "$OS" = "OpenBSD" ]; then
434 elif [ "$OS" = "SunOS" ]; then
436 elif [ "$OS" = "Darwin" ]; then
442 if [ -n "$CISCO_BANNER" ]; then
443 echo "Connect Banner:"
444 echo "$CISCO_BANNER" |
while read LINE
; do echo "|" "$LINE" ; done
450 if [ -n "$CISCO_SPLIT_INC" ]; then
452 while [ $i -lt $CISCO_SPLIT_INC ] ; do
453 eval NETWORK
="\${CISCO_SPLIT_INC_${i}_ADDR}"
454 eval NETMASK
="\${CISCO_SPLIT_INC_${i}_MASK}"
455 eval NETMASKLEN
="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
456 if [ $NETWORK != "0.0.0.0" ]; then
457 set_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
463 for i
in $INTERNAL_IP4_DNS ; do
464 set_network_route
"$i" "255.255.255.255" "32"
470 if [ -n "$INTERNAL_IP4_DNS" ]; then
476 if [ -n "$CISCO_SPLIT_INC" ]; then
478 while [ $i -lt $CISCO_SPLIT_INC ] ; do
479 eval NETWORK
="\${CISCO_SPLIT_INC_${i}_ADDR}"
480 eval NETMASK
="\${CISCO_SPLIT_INC_${i}_MASK}"
481 eval NETMASKLEN
="\${CISCO_SPLIT_INC_${i}_MASKLEN}"
482 if [ $NETWORK != "0.0.0.0" ]; then
483 # FIXME: This doesn't restore previously overwritten
485 del_network_route
"$NETWORK" "$NETMASK" "$NETMASKLEN"
491 for i
in $INTERNAL_IP4_DNS ; do
492 del_network_route
"$i" "255.255.255.255" "32"
500 if [ -n "$INTERNAL_IP4_DNS" ]; then
508 if [ -z "$reason" ]; then
509 echo "this script must be called from vpnc" 1>&2
524 echo "unknown reason '$reason'. Maybe vpnc-script is out of date" 1>&2