summaryrefslogtreecommitdiff
path: root/ipsec-updown-ipmasq
blob: ae77c9ed40c527a38ef4be23e2e5884df03b66c3 (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. #
  15. # RCSID $Id: ipsec-updown-ipmasq,v 1.1 2002-05-30 20:52:38 jrisch Exp $
  16. # CAUTION: Installing a new version of FreeS/WAN will install a new
  17. # copy of this script, wiping out any custom changes you make. If
  18. # you need changes, make a copy of this under another name, and customize
  19. # that, and use the (left/right)updown parameters in ipsec.conf to make
  20. # FreeS/WAN use yours instead of this default one.
  21. # check interface version
  22. case "$PLUTO_VERSION" in
  23. 1.[0]) # Older Pluto?!? Play it safe, script may be using new features.
  24. echo "$0: obsolete interface version \`$PLUTO_VERSION'," >&2
  25. echo "$0: called by obsolete Pluto?" >&2
  26. exit 2
  27. ;;
  28. 1.*) ;;
  29. *) echo "$0: unknown interface version \`$PLUTO_VERSION'" >&2
  30. exit 2
  31. ;;
  32. esac
  33. # check parameter(s)
  34. case "$1:$*" in
  35. ':') # no parameters
  36. ;;
  37. ipfwadm:ipfwadm) # due to (left/right)firewall; for default script only
  38. ;;
  39. custom:*) # custom parameters (see above CAUTION comment)
  40. ;;
  41. *) echo "$0: unknown parameters \`$*'" >&2
  42. exit 2
  43. ;;
  44. esac
  45. # utility functions for route manipulation
  46. # Meddling with this stuff should not be necessary and requires great care.
  47. uproute() {
  48. doroute add
  49. }
  50. downroute() {
  51. doroute del
  52. }
  53. doroute() {
  54. parms="-net $PLUTO_PEER_CLIENT_NET netmask $PLUTO_PEER_CLIENT_MASK"
  55. parms2="dev $PLUTO_INTERFACE gw $PLUTO_NEXT_HOP"
  56. case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
  57. "0.0.0.0/0.0.0.0")
  58. # horrible kludge for obscure routing bug with opportunistic
  59. it="route $1 -net 0.0.0.0 netmask 128.0.0.0 $parms2 &&"
  60. it="$it route $1 -net 128.0.0.0 netmask 128.0.0.0 $parms2"
  61. route $1 -net 0.0.0.0 netmask 128.0.0.0 $parms2 &&
  62. route $1 -net 128.0.0.0 netmask 128.0.0.0 $parms2
  63. ;;
  64. *) it="route $1 $parms $parms2"
  65. route $1 $parms $parms2
  66. ;;
  67. esac
  68. st=$?
  69. if test $st -ne 0
  70. then
  71. # route has already given its own cryptic message
  72. echo "$0: \`$it' failed" >&2
  73. if test " $1 $st" = " add 7"
  74. then
  75. # another totally undocumented interface -- 7 and
  76. # "SIOCADDRT: Network is unreachable" means that
  77. # the gateway isn't reachable.
  78. echo "$0: (incorrect or missing nexthop setting??)" >&2
  79. fi
  80. fi
  81. return $st
  82. }
  83. # the big choice
  84. case "$PLUTO_VERB:$1" in
  85. prepare-host:*|prepare-client:*)
  86. # delete possibly-existing route (preliminary to adding a route)
  87. case "$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK" in
  88. "0.0.0.0/0.0.0.0")
  89. # horrible kludge for obscure routing bug with opportunistic
  90. parms1="-net 0.0.0.0 netmask 128.0.0.0"
  91. parms2="-net 128.0.0.0 netmask 128.0.0.0"
  92. it="route del $parms1 2>&1 ; route del $parms2 2>&1"
  93. oops="`route del $parms1 2>&1 ; route del $parms2 2>&1`"
  94. ;;
  95. *)
  96. parms="-net $PLUTO_PEER_CLIENT_NET netmask $PLUTO_PEER_CLIENT_MASK"
  97. it="route del $parms 2>&1"
  98. oops="`route del $parms 2>&1`"
  99. ;;
  100. esac
  101. status="$?"
  102. if test " $oops" = " " -a " $status" != " 0"
  103. then
  104. oops="silent error, exit status $status"
  105. fi
  106. case "$oops" in
  107. 'SIOCDELRT: No such process'*)
  108. # This is what route (currently -- not documented!) gives
  109. # for "could not find such a route".
  110. oops=
  111. status=0
  112. ;;
  113. esac
  114. if test " $oops" != " " -o " $status" != " 0"
  115. then
  116. echo "$0: \`$it' failed ($oops)" >&2
  117. fi
  118. exit $status
  119. ;;
  120. route-host:*|route-client:*)
  121. # connection to me or my client subnet being routed
  122. uproute
  123. ;;
  124. unroute-host:*|unroute-client:*)
  125. # connection to me or my client subnet being unrouted
  126. downroute
  127. ;;
  128. up-host:*)
  129. # connection to me coming up
  130. # If you are doing a custom version, firewall commands go here.
  131. /usr/sbin/ipmasq
  132. ;;
  133. down-host:*)
  134. # connection to me going down
  135. # If you are doing a custom version, firewall commands go here.
  136. /usr/sbin/ipmasq
  137. ;;
  138. up-client:)
  139. # connection to my client subnet coming up
  140. # If you are doing a custom version, firewall commands go here.
  141. /usr/sbin/ipmasq
  142. ;;
  143. down-client:)
  144. # connection to my client subnet going down
  145. # If you are doing a custom version, firewall commands go here.
  146. /usr/sbin/ipmasq
  147. ;;
  148. up-client:ipfwadm)
  149. # connection to client subnet, with (left/right)firewall=yes, coming up
  150. # This is used only by the default updown script, not by your custom
  151. # ones, so do not mess with it; see CAUTION comment up at top.
  152. ipfwadm -F -i accept -b -S $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK \
  153. -D $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK
  154. ;;
  155. down-client:ipfwadm)
  156. # connection to client subnet, with (left/right)firewall=yes, going down
  157. # This is used only by the default updown script, not by your custom
  158. # ones, so do not mess with it; see CAUTION comment up at top.
  159. ipfwadm -F -d accept -b -S $PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK \
  160. -D $PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK
  161. ;;
  162. *) echo "$0: unknown verb \`$PLUTO_VERB' or parameter \`$1'" >&2
  163. exit 1
  164. ;;
  165. esac