PageRenderTime 2540ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/programs/_updown.mast/_updown.mast.in

https://gitlab.com/envieidoc/Openswan
Autoconf | 598 lines | 323 code | 54 blank | 221 comment | 57 complexity | 4d10644517f01d907aeca7d674a8708d MD5 | raw file
  1. #! /bin/sh
  2. # iproute2 version, default updown script
  3. #
  4. # Copyright (C) 2003-2004 Nigel Metheringham
  5. # Copyright (C) 2002-2007 Michael Richardson <mcr@xelerance.com>
  6. # Copyright (C) 2003-2011 Tuomo Soini <tis@foobar.fi>
  7. # Copyright (C) 2008 Paul Wouters <paul@xelerance.com>
  8. #
  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>.
  13. #
  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
  17. # for more details.
  18. # CAUTION: Installing a new version of Openswan will install a new
  19. # copy of this script, wiping out any custom changes you make. If
  20. # you need changes, make a copy of this under another name, and customize
  21. # that, and use the (left/right)updown= parameters in ipsec.conf to make
  22. # Openswan use yours instead of this default one.
  23. test $IPSEC_INIT_SCRIPT_DEBUG && set -v -x
  24. LC_ALL=C export LC_ALL
  25. # things that this script gets (from ipsec_pluto(8) man page)
  26. #
  27. #
  28. # PLUTO_VERSION
  29. # indicates what version of this interface is being
  30. # used. This document describes version 1.1. This
  31. # is upwardly compatible with version 1.0.
  32. #
  33. # PLUTO_VERB
  34. # specifies the name of the operation to be performed
  35. # (prepare-host, prepare-client, up-host, up-client,
  36. # down-host, or down-client). If the address family
  37. # for security gateway to security gateway communications
  38. # is IPv6, then a suffix of -v6 is added to the
  39. # verb.
  40. #
  41. # PLUTO_CONNECTION
  42. # is the name of the connection for which we are
  43. # routing.
  44. #
  45. # PLUTO_CONN_POLICY
  46. # the policy of the connection, as in:
  47. # RSASIG+ENCRYPT+TUNNEL+PFS+DONTREKEY+OPPORTUNISTIC+failureDROP+lKOD+rKOD
  48. #
  49. # PLUTO_NEXT_HOP
  50. # is the next hop to which packets bound for the peer
  51. # must be sent.
  52. #
  53. # PLUTO_INTERFACE
  54. # is the name of the ipsec interface to be used.
  55. #
  56. # PLUTO_ME
  57. # is the IP address of our host.
  58. #
  59. # PLUTO_METRIC
  60. # is the metric to set for the route
  61. #
  62. # PLUTO_MTU
  63. # is the mtu to set for the route
  64. #
  65. # PLUTO_MY_CLIENT
  66. # is the IP address / count of our client subnet. If
  67. # the client is just the host, this will be the
  68. # host's own IP address / max (where max is 32 for
  69. # IPv4 and 128 for IPv6).
  70. #
  71. # PLUTO_MY_CLIENT_NET
  72. # is the IP address of our client net. If the client
  73. # is just the host, this will be the host's own IP
  74. # address.
  75. #
  76. # PLUTO_MY_CLIENT_MASK
  77. # is the mask for our client net. If the client is
  78. # just the host, this will be 255.255.255.255.
  79. #
  80. # PLUTO_MY_SOURCEIP
  81. # if non-empty, then the source address for the route will be
  82. # set to this IP address.
  83. #
  84. # PLUTO_MY_PROTOCOL
  85. # is the protocol for this connection. Useful for
  86. # firewalling.
  87. #
  88. # PLUTO_MY_PORT
  89. # is the port. Useful for firewalling.
  90. #
  91. # PLUTO_PEER
  92. # is the IP address of our peer.
  93. #
  94. # PLUTO_PEER_CLIENT
  95. # is the IP address / count of the peer's client subnet.
  96. # If the client is just the peer, this will be
  97. # the peer's own IP address / max (where max is 32
  98. # for IPv4 and 128 for IPv6).
  99. #
  100. # PLUTO_PEER_CLIENT_NET
  101. # is the IP address of the peer's client net. If the
  102. # client is just the peer, this will be the peer's
  103. # own IP address.
  104. #
  105. # PLUTO_PEER_CLIENT_MASK
  106. # is the mask for the peer's client net. If the
  107. # client is just the peer, this will be
  108. # 255.255.255.255.
  109. #
  110. # PLUTO_PEER_PROTOCOL
  111. # is the protocol set for remote end with port
  112. # selector.
  113. #
  114. # PLUTO_PEER_PORT
  115. # is the peer's port. Useful for firewalling.
  116. #
  117. # PLUTO_CONNECTION_TYPE
  118. #
  119. # PLUTO_CONN_ADDRFAMILY
  120. # is the family type, "ipv4" or "ipv6"
  121. #
  122. # PLUTO_STACK
  123. # IPsec stack used by pluto (eg protostack= values)
  124. #
  125. # PLUTO_NM_CONFIGURED
  126. # is NetworkManager used for resolv.conf update
  127. #
  128. # PLUTO_SAREF_TRACKING
  129. # If we need to manipulate any iptables for SAref tracking
  130. #
  131. # for debugging of the script
  132. #exec >/tmp/_updown.m$$
  133. #exec 2>&1
  134. #set -x
  135. # Import default _updown configs from the /etc/[sysconfig|default]pluto_updown file
  136. #
  137. # Two variables can be set in this file:
  138. #
  139. # DEFAULTSOURCE
  140. # is the default value for PLUTO_MY_SOURCEIP
  141. #
  142. # IPROUTEARGS
  143. # is the extra argument list for ip route command
  144. #
  145. # IPRULEARGS
  146. # is the extra argument list for ip rule command
  147. #
  148. # rpm based systems
  149. if [ -f /etc/sysconfig/pluto_updown ]
  150. then
  151. . /etc/sysconfig/pluto_updown
  152. # deb based systems
  153. elif [ -f /etc/default/pluto_updown ]
  154. then
  155. . /etc/default/pluto_updown
  156. fi
  157. use_comment=true
  158. # check interface version
  159. case "$PLUTO_VERSION" in
  160. 1.*) # Older Pluto?!? Play it safe, script may be using new features.
  161. echo "$0: obsolete interface version \`$PLUTO_VERSION'," >&2
  162. echo "$0: called by obsolete Pluto?" >&2
  163. exit 2
  164. ;;
  165. 2.*) ;;
  166. *) echo "$0: unknown interface version \`$PLUTO_VERSION'" >&2
  167. exit 2
  168. ;;
  169. esac
  170. # check parameter(s)
  171. case "$1:$*" in
  172. ':') # no parameters
  173. ;;
  174. custom:*) # custom parameters (see above CAUTION comment)
  175. ;;
  176. *) echo "$0: unknown parameters \`$*'" >&2
  177. exit 2
  178. ;;
  179. esac
  180. # function to see if iptables has an IPSEC chain already, and if not,
  181. # it will create one and insert it into the OUTPUT and FORWARD chains.
  182. checkipsec() {
  183. if [ -z "$PLUTO_SAREF_TRACKING" -o "$PLUTO_SAREF_TRACKING" = "no" ]; then
  184. echo "SAref table initialisation left to third party"
  185. else if [ -z "$PLUTO_CONN_ADDRFAMILY" -o "$PLUTO_CONN_ADDRFAMILY" = "ipv6" ]; then
  186. echo "SAref not activated for IPv6"
  187. else
  188. # make sure we have an IPSEC chain
  189. if ! ( iptables -n -t mangle -L IPSEC >/dev/null 2>&1) ; then
  190. iptables -t mangle -N IPSEC
  191. fi
  192. # we also need to have a NEW_IPSEC_CONN chain
  193. if ! ( iptables -n -t mangle -L NEW_IPSEC_CONN >/dev/null 2>&1) ; then
  194. iptables -t mangle -N NEW_IPSEC_CONN
  195. fi
  196. # initialize the IPSEC chain if needed
  197. if [ "$PLUTO_SAREF_TRACKING" = "conntrack" ]; then
  198. if [ "$(iptables -n -t mangle -L IPSEC | wc -l )" != 6 ] ; then
  199. iptables -t mangle -F IPSEC
  200. iptables -t mangle -A IPSEC -j CONNMARK --restore-mark
  201. iptables -t mangle -A IPSEC -m mark --mark 0x80000000/0x80000000 -j RETURN
  202. iptables -t mangle -A IPSEC -j NEW_IPSEC_CONN
  203. iptables -t mangle -A IPSEC -j CONNMARK --save-mark
  204. fi
  205. else
  206. if [ "$(iptables -n -t mangle -L IPSEC | wc -l )" != 3 ] ; then
  207. iptables -t mangle -F IPSEC
  208. iptables -t mangle -A IPSEC -j NEW_IPSEC_CONN
  209. fi
  210. fi
  211. # next, setup routing rules and tables:
  212. # we pick table 50, cause proto50=ESP
  213. if ! ( ip rule show | grep -qF 'from all fwmark 0x80000000/0x80000000 lookup 50' ) ; then
  214. # note "fwmarkmask" is an (obsolete) Openswan patch to "ip" command.
  215. # note2: iproute2-2.6.22-070710 supports mask via /mask notation instead
  216. # ip rule add fwmark 0x80000000 fwmarkmask 0x80000000 table 50
  217. ip rule add from all fwmark 0x80000000/0x80000000 lookup 50
  218. # This rule makes sure that packets originating from mast0 (ones that came
  219. # out of a tunnel) go directly to the main table. This makes sure that
  220. # rp_filter can find the right reverse path route.
  221. ip rule add from all iif $PLUTO_INTERFACE lookup main
  222. fi
  223. # the default route goes over the mast interface
  224. if ! ( ip route show table 50 | grep -qF "default dev $PLUTO_INTERFACE" ) ; then
  225. ip route add default dev $PLUTO_INTERFACE table 50
  226. fi
  227. # now look for -j IPSEC in OUTPUT chains.
  228. if ! (iptables -n -t mangle -L OUTPUT | grep -q '^IPSEC') ; then
  229. iptables -t mangle -I OUTPUT 1 -j IPSEC
  230. iptables -t mangle -I OUTPUT 1 -p udp --sport 500 -j ACCEPT
  231. iptables -t mangle -I OUTPUT 1 -p udp --dport 500 -j ACCEPT
  232. iptables -t mangle -I OUTPUT 1 -p udp --sport 4500 -j ACCEPT
  233. iptables -t mangle -I OUTPUT 1 -p udp --dport 4500 -j ACCEPT
  234. fi
  235. # now look for -j IPSEC in PREROUTING chains.
  236. if ! (iptables -n -t mangle -L PREROUTING | grep -q '^IPSEC') ; then
  237. iptables -t mangle -I PREROUTING 1 -j IPSEC
  238. fi
  239. if [ -w /proc/sys/net/ipv4/conf/$PLUTO_INTERFACE/src_valid_mark ] ; then
  240. # finally make sure that the top bit of source vmark for mast0 is set
  241. vmark=$(cat /proc/sys/net/ipv4/conf/$PLUTO_INTERFACE/src_valid_mark)
  242. vmark=$(( $vmark | 0x80000000 ))
  243. echo "$vmark" > /proc/sys/net/ipv4/conf/$PLUTO_INTERFACE/src_valid_mark
  244. else
  245. # In case that we don't have the means to get rp_filter to cooperate
  246. # with KLIPS nfmark based routing scheme, we disable rp_filter.
  247. for n in /proc/sys/net/ipv4/conf/*/rp_filter ; do
  248. echo 0 > $n
  249. done
  250. fi
  251. fi
  252. fi
  253. }
  254. # utility functions for route manipulation
  255. # called to add appropriate "erout'ing"
  256. uproute() {
  257. checkipsec
  258. doipsecrule add
  259. ip route flush cache
  260. }
  261. # called to remove any routing
  262. downroute() {
  263. checkipsec
  264. doipsecrule delete
  265. ip route flush cache
  266. }
  267. # called XXXX
  268. uprule() {
  269. doipsecrule delete
  270. doipsecrule add
  271. ip route flush cache
  272. }
  273. # called XXXX
  274. downrule() {
  275. doipsecrule delete
  276. }
  277. updateresolvconf() {
  278. if [ -n "$PLUTO_CISCO_DNS_INFO" ]; then
  279. if [ -n "`pidof unbound`" -a -n "$PLUTO_CISCO_DOMAIN_INFO" ]; then
  280. echo "updating local nameserver for $PLUTO_CISCO_DOMAIN_INFO with $PLUTO_CISCO_DNS_INFO"
  281. /usr/sbin/unbound-control forward_add $PLUTO_CISCO_DOMAIN_INFO $PLUTO_CISCO_DNS_INFO
  282. /usr/sbin/unbound-control flush_zone $PLUTO_CISCO_DOMAIN_INFO
  283. return
  284. fi
  285. fi
  286. if [ -z "$PLUTO_NM_CONFIGURED" -o "$PLUTO_NM_CONFIGURED" = 0 ]; then
  287. echo "updating resolvconf"
  288. if [ -e "$OPENSWAN_RESOLV_CONF" ]; then
  289. echo "Backup resolv.conf already exists, so doing nothing"
  290. return 1
  291. fi
  292. if [ ! -e "$ORIG_RESOLV_CONF" ]; then
  293. echo "resolv.conf does not exist, so doing nothing"
  294. return 1
  295. fi
  296. cp -- $ORIG_RESOLV_CONF $OPENSWAN_RESOLV_CONF
  297. RESOLVE_CONF="#Generated by Openswan (IPSec)"
  298. if [ -n "$PLUTO_CISCO_DOMAIN_INFO" ]; then
  299. if grep 'domain' $ORIG_RESOLV_CONF > /dev/null 2>&1
  300. then
  301. RESOLVE_CONF="$RESOLVE_CONF\ndomain $PLUTO_CISCO_DOMAIN_INFO\nsearch $PLUTO_CISCO_DOMAIN_INFO"
  302. else
  303. RESOLVE_CONF="$RESOLVE_CONF\nsearch $PLUTO_CISCO_DOMAIN_INFO"
  304. fi
  305. fi
  306. if [ -n "$PLUTO_CISCO_DNS_INFO" ]; then
  307. for i in $PLUTO_CISCO_DNS_INFO; do
  308. RESOLVE_CONF="$RESOLVE_CONF\nnameserver $i"
  309. done
  310. fi
  311. rm -f -- $ORIG_RESOLV_CONF
  312. printf "$RESOLVE_CONF" > $ORIG_RESOLV_CONF
  313. return $?
  314. else
  315. echo "Updating resolv.conf is controlled by Network Manager"
  316. return 0
  317. fi
  318. }
  319. restoreresolvconf() {
  320. if [ -n "`pidof unbound`" ]; then
  321. if [ -n "$PLUTO_CISCO_DNS_INFO" ]; then
  322. echo "flushing local nameserver of $PLUTO_CISCO_DOMAIN_INFO"
  323. /usr/sbin/unbound-control forward_remove $PLUTO_CISCO_DOMAIN_INFO
  324. /usr/sbin/unbound-control flush_zone $PLUTO_CISCO_DOMAIN_INFO
  325. fi
  326. return
  327. fi
  328. if [ -z "$PLUTO_NM_CONFIGURED" -o "$PLUTO_NM_CONFIGURED" = 0 ]; then
  329. echo "restoring resolvconf"
  330. if [ ! -e "$OPENSWAN_RESOLV_CONF" ]; then
  331. echo "Problem in restoring the resolv.conf, as there is no backup file"
  332. return 2
  333. fi
  334. if grep 'Openswan' $ORIG_RESOLV_CONF > /dev/null 2>&1
  335. then
  336. cp -- "$OPENSWAN_RESOLV_CONF" $ORIG_RESOLV_CONF
  337. else
  338. echo "Current resolv.conf is not generated by Openswan, so doing nothing"
  339. fi
  340. rm -f -- "$OPENSWAN_RESOLV_CONF"
  341. return 0
  342. else
  343. # Here disconnect signal is sent to NetworkManager
  344. # whenever an already established connection is being terminated.
  345. unset openswan_reason
  346. unset PLUTO_CISCO_DOMAIN_INFO
  347. unset PLUTO_CISCO_DNS_INFO
  348. unset PLUTO_PEER_BANNER
  349. unset PLUTO_MY_SOURCEIP
  350. unset PLUTO_PEER
  351. echo "Restoring resolv.conf is controlled by Network Manager"
  352. disconnectNM
  353. fi
  354. }
  355. disconnectNM() {
  356. # This will be called whenever a connection fails to establish
  357. # due to a state (either phase 1, xauth phase, or phase 2) fails.
  358. # This will send a singal to NetworkManager over dbus so that NM
  359. # can clear up coonnections.
  360. openswan_reason=disconnect
  361. export openswan_reason
  362. echo "sending disconnect signal to NetworkManager"
  363. /usr/libexec/nm-openswan-service-helper
  364. return 0
  365. }
  366. addsource() {
  367. st=0
  368. # check if given sourceip is local and add as alias if not
  369. if ! ip -o route get ${PLUTO_MY_SOURCEIP%/*} | grep -q ^local; then
  370. it="ip addr add ${PLUTO_MY_SOURCEIP%/*}/32 dev ${PLUTO_INTERFACE%:*}"
  371. oops="`eval $it 2>&1`"
  372. st=$?
  373. if [ " $oops" = " " -a " $st" != " 0" ]; then
  374. oops="silent error, exit status $st"
  375. fi
  376. case "$oops" in
  377. 'RTNETLINK answers: File exists'*)
  378. # should not happen, but ... ignore if the
  379. # address was already assigned on interface
  380. oops=""
  381. st=0
  382. ;;
  383. esac
  384. if [ " $oops" != " " -o " $st" != " 0" ]; then
  385. echo "$0: addsource \`$it' failed ($oops)" >&2
  386. fi
  387. fi
  388. return $st
  389. }
  390. # WARNING: changesource might not work as expected with mast
  391. changesource() {
  392. st=0
  393. parms="$PLUTO_PEER_CLIENT"
  394. parms2="dev $PLUTO_INTERFACE"
  395. parms3="src ${PLUTO_MY_SOURCEIP%/*}"
  396. if [ -n "$IPROUTETABLE" ]
  397. then
  398. parms3="$parms3 table $IPROUTETABLE"
  399. fi
  400. cmd=add
  401. if ! ip -o route get ${PLUTO_MY_SOURCEIP%/*} | grep mast0
  402. then
  403. cmd=change
  404. fi
  405. it="ip route $cmd $parms $parms2 $parms3"
  406. case "$PLUTO_PEER_CLIENT" in
  407. "0.0.0.0/0"|"::/0")
  408. # opportunistic encryption work around
  409. it=
  410. ;;
  411. esac
  412. oops="`eval $it 2>&1`"
  413. st=$?
  414. if test " $oops" = " " -a " $st" != " 0"
  415. then
  416. oops="silent error, exit status $st"
  417. fi
  418. if test " $oops" != " " -o " $st" != " 0"
  419. then
  420. echo "$0: changesource \`$it' failed ($oops)" >&2
  421. fi
  422. return $st
  423. }
  424. # the purpose of this command is to add an entry to the NEW_IPSEC_CONN chain
  425. # in the iptables system. It grabs the right packets and does a --set-mark
  426. # on them. An advanced routing rule then directs the packets to the
  427. # appropriate device with the right mark.
  428. #
  429. # This is not be done for OE packets --- they are supposed to get a
  430. # new netfilter module instead.
  431. doipsecrule() {
  432. if [ -z "$PLUTO_SAREF_TRACKING" -o "$PLUTO_SAREF_TRACKING" = "no" ]; then
  433. echo "SAref tracking left to third party"
  434. else if [ -z "$PLUTO_CONN_ADDRFAMILY" -o "$PLUTO_CONN_ADDRFAMILY" = "ipv6" ]; then
  435. echo "SAref not activated for IPv6"
  436. else
  437. saref=$PLUTO_PEER_REF
  438. nf_saref=$(printf "0x%x0000" $(( $saref | 0x8000 )))
  439. srcnet=$PLUTO_MY_CLIENT_NET/$PLUTO_MY_CLIENT_MASK
  440. dstnet=$PLUTO_PEER_CLIENT_NET/$PLUTO_PEER_CLIENT_MASK
  441. #echo SAref: $saref '->' $nf_saref
  442. rulespec="--src $srcnet --dst $dstnet -m mark --mark 0/0x80000000 -j MARK --set-mark $nf_saref"
  443. if $use_comment ; then
  444. # WARNING: you should only edit use_comment while you have no established conns
  445. rulespec="$rulespec -m comment --comment '$PLUTO_CONNECTION'"
  446. fi
  447. case $1 in
  448. add)
  449. it="iptables -t mangle -I NEW_IPSEC_CONN 1 $rulespec"
  450. ;;
  451. delete)
  452. it="iptables -t mangle -D NEW_IPSEC_CONN $rulespec"
  453. ;;
  454. esac
  455. oops="`set +x; eval $it 2>&1`"
  456. st=$?
  457. if test " $oops" = " " -a " $st" != " 0"
  458. then
  459. oops="silent error, exit status $st"
  460. fi
  461. if test " $oops" != " " -o " $st" != " 0"
  462. then
  463. echo "$0: doroute \`$it' failed ($oops)" >&2
  464. fi
  465. return $st
  466. fi
  467. fi
  468. }
  469. # the big choice
  470. case "$PLUTO_VERB:$1" in
  471. spdadd-client:*|spdadd-host:*)
  472. checkipsec;
  473. doipsecrule add;;
  474. spddel-client:*|spddel-host:*)
  475. checkipsec
  476. doipsecrule delete;;
  477. prepare-host:*|prepare-client:*)
  478. # set up a "%trap" equivalent (we don't know how do this yet!)
  479. ;;
  480. route-host:*|route-client:*)
  481. # connection to me or my client subnet being routed
  482. # setup of %TRAP equivalent
  483. ;;
  484. unroute-host:*|unroute-client:*)
  485. # connection to me or my client subnet being unrouted
  486. # remove %TRAP equivalent
  487. ;;
  488. up-host:*)
  489. # If you are doing a custom version, firewall commands go here.
  490. ;;
  491. down-host:*)
  492. # If you are doing a custom version, firewall commands go here.
  493. ;;
  494. up-client:)
  495. # If you are doing a custom version, firewall commands go here.
  496. ;;
  497. down-client:)
  498. # If you are doing a custom version, firewall commands go here.
  499. ;;
  500. updateresolvconf-host|updateresolvconf-client)
  501. # updating resolv.conf using DNS info obtained from the server
  502. updateresolvconf
  503. ;;
  504. restoreresolvconf-host|restoreresolvconf-client)
  505. # restoring resolv.conf
  506. restoreresolvconf
  507. ;;
  508. disconnectNM-host|disconnectNM-client)
  509. # sending disconnect signal to NM, as something went wrong.
  510. disconnectNM
  511. ;;
  512. #
  513. # IPv6
  514. #
  515. prepare-host-v6:*|prepare-client-v6:*)
  516. ;;
  517. route-host-v6:*|route-client-v6:*)
  518. # connection to me or my client subnet being routed
  519. #uproute_v6
  520. ;;
  521. unroute-host-v6:*|unroute-client-v6:*)
  522. # connection to me or my client subnet being unrouted
  523. #downroute_v6
  524. ;;
  525. up-host-v6:*)
  526. # connection to me coming up
  527. # If you are doing a custom version, firewall commands go here.
  528. ;;
  529. down-host-v6:*)
  530. # connection to me going down
  531. # If you are doing a custom version, firewall commands go here.
  532. ;;
  533. up-client-v6:)
  534. # connection to my client subnet coming up
  535. # If you are doing a custom version, firewall commands go here.
  536. ;;
  537. down-client-v6:)
  538. # connection to my client subnet going down
  539. # If you are doing a custom version, firewall commands go here.
  540. ;;
  541. *) echo "$0: unknown verb \`$PLUTO_VERB' or parameter \`$1'" >&2
  542. exit 1
  543. ;;
  544. esac