summaryrefslogtreecommitdiff
path: root/ipsec-updown-ipmasq
blob: 53093a8668fac8b2aea133436f6523c9c889c98a (plain)
  1. #! /bin/sh
  2. # default updown script
  3. # Copyright (C) 2000, 2001 D. Hugh Redelmeier, Henry Spencer
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by the
  7. # Free Software Foundation; either version 2 of the License, or (at your
  8. # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. # This script is a derivative of the one by Hugh Redelmeier and Henry
  15. # Spencer. It uses ipmasq as the firewallscript, and should be used
  16. # together with some modifications to ipmasq.
  17. #
  18. # It is modified by Jonas Smedegaard <jonas@jones.dk>, and Juri Jensen
  19. # <juri@xenux.dk>.
  20. #
  21. # Features:
  22. #
  23. # * Dynamic creation of firewall rules to RW connections
  24. # * Setup of proper source address makes it possible to ping from the
  25. # SGW itself to a remote subnet, without a separate tunnel. Remember
  26. # to change the reference of a 10.0.x.x network below to the IP range
  27. # you're using!
  28. #
  29. # RCSID $Id: ipsec-updown-ipmasq,v 1.3 2006-07-16 12:34:00 jonas Exp $
  30. # CAUTION: Installing a new version of FreeS/WAN will install a new
  31. # copy of this script, wiping out any custom changes you make. If
  32. # you need changes, make a copy of this under another name, and customize
  33. # that, and use the (left/right)updown parameters in ipsec.conf to make
  34. # FreeS/WAN use yours instead of this default one.
  35. # check interface version
  36. case "$PLUTO_VERSION" in
  37. 1.[0]) # Older Pluto?!? Play it safe, script may be using new features.
  38. echo "$0: obsolete interface version \`$PLUTO_VERSION'," >&2
  39. echo "$0: called by obsolete Pluto?" >&2
  40. exit 2
  41. ;;
  42. 1.*) ;;
  43. *) echo "$0: unknown interface version \`$PLUTO_VERSION'" >&2
  44. exit 2
  45. ;;
  46. esac
  47. # check parameter(s)
  48. case "$1:$*" in
  49. ':') # no parameters
  50. ;;
  51. ipfwadm:ipfwadm) # due to (left/right)firewall; for default script only
  52. ;;
  53. custom:*) # custom parameters (see above CAUTION comment)
  54. ;;
  55. *) echo "$0: unknown parameters \`$*'" >&2
  56. exit 2
  57. ;;
  58. esac
  59. # utility functions for route manipulation
  60. # Meddling with this stuff should not be necessary and requires great care.
  61. uproute() {
  62. doroute add
  63. }
  64. downroute() {
  65. doroute del
  66. }
  67. doroute() {
  68. parms="-net $PLUTO_PEER_CLIENT_NET netmask $PLUTO_PEER_CLIENT_MASK"
  69. parms2="dev $PLUTO_INTERFACE gw $PLUTO_NEXT_HOP"
  70. case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
  71. "0.0.0.0/0.0.0.0")
  72. # horrible kludge for obscure routing bug with opportunistic
  73. it="route $1 -net 0.0.0.0 netmask 128.0.0.0 $parms2 &&"
  74. it="$it route $1 -net 128.0.0.0 netmask 128.0.0.0 $parms2"
  75. route $1 -net 0.0.0.0 netmask 128.0.0.0 $parms2 &&
  76. route $1 -net 128.0.0.0 netmask 128.0.0.0 $parms2
  77. ;;
  78. *) it="route $1 $parms $parms2"
  79. route $1 $parms $parms2
  80. ;;
  81. esac
  82. st=$?
  83. src="`ifconfig | egrep "^[[:space:]]*inet addr:10\.0\." | cut -f2 -d: | cut -f1 -d' ' | head -n 1`"
  84. if test "$src" ; then
  85. ip ro ls | egrep "^10\.0\..* dev ipsec" | egrep -v " src " |
  86. while read ; do
  87. ip ro change $REPLY src $src
  88. done
  89. fi
  90. if test $st -ne 0
  91. then
  92. # route has already given its own cryptic message
  93. echo "$0: \`$it' failed" >&2
  94. if test " $1 $st" = " add 7"
  95. then
  96. # another totally undocumented interface -- 7 and
  97. # "SIOCADDRT: Network is unreachable" means that
  98. # the gateway isn't reachable.
  99. echo "$0: (incorrect or missing nexthop setting??)" >&2
  100. fi
  101. fi
  102. return $st
  103. }
  104. # the big choice
  105. case "$PLUTO_VERB:$1" in
  106. prepare-host:*|prepare-client:*)
  107. # delete possibly-existing route (preliminary to adding a route)
  108. case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
  109. "0.0.0.0/0.0.0.0")
  110. # horrible kludge for obscure routing bug with opportunistic
  111. parms1="-net 0.0.0.0 netmask 128.0.0.0"
  112. parms2="-net 128.0.0.0 netmask 128.0.0.0"
  113. it="route del $parms1 2>&1 ; route del $parms2 2>&1"
  114. oops="`route del $parms1 2>&1 ; route del $parms2 2>&1`"
  115. ;;
  116. *)
  117. parms="-net $PLUTO_PEER_CLIENT_NET netmask $PLUTO_PEER_CLIENT_MASK"
  118. it="route del $parms 2>&1"
  119. oops="`route del $parms 2>&1`"
  120. ;;
  121. esac
  122. status="$?"
  123. if test " $oops" = " " -a " $status" != " 0"
  124. then
  125. oops="silent error, exit status $status"
  126. fi
  127. case "$oops" in
  128. 'SIOCDELRT: No such process'*)
  129. # This is what route (currently -- not documented!) gives
  130. # for "could not find such a route".
  131. oops=
  132. status=0
  133. ;;
  134. esac
  135. if test " $oops" != " " -o " $status" != " 0"
  136. then
  137. echo "$0: \`$it' failed ($oops)" >&2
  138. fi
  139. exit $status
  140. ;;
  141. route-host:*|route-client:*)
  142. # connection to me or my client subnet being routed
  143. uproute
  144. ;;
  145. unroute-host:*|unroute-client:*)
  146. # connection to me or my client subnet being unrouted
  147. downroute
  148. ;;
  149. up-host:*)
  150. # connection to me coming up
  151. # If you are doing a custom version, firewall commands go here.
  152. /usr/sbin/ipmasq
  153. ;;
  154. down-host:*)
  155. # connection to me going down
  156. # If you are doing a custom version, firewall commands go here.
  157. /usr/sbin/ipmasq
  158. ;;
  159. up-client:)
  160. # connection to my client subnet coming up
  161. # If you are doing a custom version, firewall commands go here.
  162. /usr/sbin/ipmasq
  163. ;;
  164. down-client:)
  165. # connection to my client subnet going down
  166. # If you are doing a custom version, firewall commands go here.
  167. /usr/sbin/ipmasq
  168. ;;
  169. up-client:ipfwadm)
  170. # connection to client subnet, with (left/right)firewall=yes, coming up
  171. # This is used only by the default updown script, not by your custom
  172. # ones, so do not mess with it; see CAUTION comment up at top.
  173. ipfwadm -F -i accept -b -S $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK \
  174. -D $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK
  175. ;;
  176. down-client:ipfwadm)
  177. # connection to client subnet, with (left/right)firewall=yes, going down
  178. # This is used only by the default updown script, not by your custom
  179. # ones, so do not mess with it; see CAUTION comment up at top.
  180. ipfwadm -F -d accept -b -S $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK \
  181. -D $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK
  182. ;;
  183. *) echo "$0: unknown verb \`$PLUTO_VERB' or parameter \`$1'" >&2
  184. exit 1
  185. ;;
  186. esac