Blame SOURCES/bz1744224-IPsrcaddr-1-add-destination-and-table-parameters.patch

734564
--- ClusterLabs-resource-agents-e711383f/heartbeat/IPsrcaddr	2019-08-15 16:02:10.055827624 +0200
734564
+++ /home/oalbrigt/src/resource-agents/heartbeat/IPsrcaddr	2019-08-15 15:45:50.690757838 +0200
734564
@@ -1,6 +1,6 @@
734564
 #!/bin/sh
734564
 #
734564
-#	Description:	IPsrcaddr - Preferred source address modification
734564
+#	Description:	IPsrcaddr - Preferred source(/dest) address modification
734564
 #
734564
 #	Author:			John Sutton <john@scl.co.uk>
734564
 #	Support:		users@clusterlabs.org
734564
@@ -11,7 +11,7 @@
734564
 #
734564
 #	This script manages the preferred source address associated with
734564
 #	packets which originate on the localhost and are routed through the
734564
-#	default route.  By default, i.e. without the use of this script or
734564
+#	matching route.  By default, i.e. without the use of this script or
734564
 #	similar, these packets will carry the IP of the primary i.e. the
734564
 #	non-aliased interface.  This can be a nuisance if you need to ensure
734564
 #	that such packets carry the same IP irrespective of which host in
734564
@@ -27,7 +27,7 @@
734564
 #
734564
 #	NOTES:
734564
 #
734564
-#	1) There must be one and not more than 1 default route!  Mainly because
734564
+#	1) There must be one and not more than 1 matching route!  Mainly because
734564
 #	I can't see why you should have more than one.  And if there is more
734564
 #	than one, we would have to box clever to find out which one is to be
734564
 #	modified, or we would have to pass its identity as an argument.
734564
@@ -54,16 +54,25 @@
734564
 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
734564
 
734564
 # Defaults
734564
+OCF_RESKEY_ipaddress_default=""
734564
+OCF_RESKEY_cidr_netmask_default=""
734564
+OCF_RESKEY_destination_default="0.0.0.0/0"
734564
 OCF_RESKEY_proto_default=""
734564
+OCF_RESKEY_table_default=""
734564
 
734564
+: ${OCF_RESKEY_ipaddress=${OCF_RESKEY_ipaddress_default}}
734564
+: ${OCF_RESKEY_cidr_netmask=${OCF_RESKEY_cidr_netmask_default}}
734564
+: ${OCF_RESKEY_destination=${OCF_RESKEY_destination_default}}
734564
 : ${OCF_RESKEY_proto=${OCF_RESKEY_proto_default}}
734564
+: ${OCF_RESKEY_table=${OCF_RESKEY_table_default}}
734564
 #######################################################################
734564
 
734564
 [ -z "$OCF_RESKEY_proto" ] && PROTO="" || PROTO="proto $OCF_RESKEY_proto"
734564
+[ -z "$OCF_RESKEY_table" ] && TABLE="" || TABLE="table $OCF_RESKEY_table"
734564
 
734564
 USAGE="usage: $0 {start|stop|status|monitor|validate-all|meta-data}";
734564
 
734564
-  CMDSHOW="$IP2UTIL route show   to exact 0.0.0.0/0"
734564
+  CMDSHOW="$IP2UTIL route show   $TABLE to exact $OCF_RESKEY_destination"
734564
 CMDCHANGE="$IP2UTIL route change to "
734564
 
734564
 SYSTYPE="`uname -s`"
734564
@@ -91,7 +100,7 @@
734564
 The IP address. 
734564
 </longdesc>
734564
 <shortdesc lang="en">IP address</shortdesc>
734564
-<content type="string" default="" />
734564
+<content type="string" default="${OCF_RESKEY_ipaddress_default}" />
734564
 </parameter>
734564
 
734564
 <parameter name="cidr_netmask">
734564
@@ -100,7 +109,15 @@
734564
 dotted quad notation  255.255.255.0).
734564
 </longdesc>
734564
 <shortdesc lang="en">Netmask</shortdesc>
734564
-<content type="string" default=""/>
734564
+<content type="string" default="${OCF_RESKEY_cidr_netmask_default}"/>
734564
+</parameter>
734564
+
734564
+<parameter name="destination">
734564
+<longdesc lang="en">
734564
+The destination IP/subnet for the route (default: $OCF_RESKEY_destination_default)
734564
+</longdesc>
734564
+<shortdesc lang="en">Destination IP/subnet</shortdesc>
734564
+<content type="string" default="${OCF_RESKEY_destination_default}" />
734564
 </parameter>
734564
 
734564
 <parameter name="proto">
734564
@@ -108,7 +125,17 @@
734564
 Proto to match when finding network. E.g. "kernel".
734564
 </longdesc>
734564
 <shortdesc lang="en">Proto</shortdesc>
734564
-<content type="string" default="" />
734564
+<content type="string" default="${OCF_RESKEY_proto_default}" />
734564
+</parameter>
734564
+
734564
+<parameter name="table">
734564
+<longdesc lang="en">
734564
+Table to modify. E.g. "local".
734564
+
734564
+The table has to have a route matching the "destination" parameter.
734564
+</longdesc>
734564
+<shortdesc lang="en">Table</shortdesc>
734564
+<content type="string" default="${OCF_RESKEY_table_default}" />
734564
 </parameter>
734564
 </parameters>
734564
 
734564
@@ -151,21 +178,22 @@
734564
 export OCF_RESKEY_ip=$OCF_RESKEY_ipaddress
734564
 
734564
 srca_read() {
734564
-	# Capture the default route - doublequotes prevent word splitting...
734564
-	DEFROUTE="`$CMDSHOW`" || errorexit "command '$CMDSHOW' failed"
734564
-
734564
-	# ... so we can make sure there is only 1 default route
734564
-	[ 1 -eq `echo "$DEFROUTE" | wc -l` ] || \
734564
-		errorexit "more than 1 default route exists"
734564
+	# Capture matching route - doublequotes prevent word splitting...
734564
+	ROUTE="`$CMDSHOW`" || errorexit "command '$CMDSHOW' failed"
734564
 
734564
-	# But there might still be no default route
734564
-	[ -z "$DEFROUTE" ] && errorexit "no default route exists"
734564
+	# ... so we can make sure there is only 1 matching route
734564
+	[ 1 -eq `echo "$ROUTE" | wc -l` ] || \
734564
+		errorexit "more than 1 matching route exists"
734564
+
734564
+	# But there might still be no matching route
734564
+	[ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] && [ -z "$ROUTE" ] && \
734564
+		 ! ocf_is_probe && errorexit "no matching route exists"
734564
 
734564
 	# Sed out the source ip address if it exists
734564
-	SRCIP=`echo $DEFROUTE | sed -n "s/$MATCHROUTE/\3/p"`
734564
+	SRCIP=`echo $ROUTE | sed -n "s/$MATCHROUTE/\3/p"`
734564
 
734564
 	# and what remains after stripping out the source ip address clause
734564
-	ROUTE_WO_SRC=`echo $DEFROUTE | sed "s/$MATCHROUTE/\1\5/"`
734564
+	ROUTE_WO_SRC=`echo $ROUTE | sed "s/$MATCHROUTE/\1\5/"`
734564
 
734564
 	[ -z "$SRCIP" ] && return 1
734564
 	[ $SRCIP = $1 ] && return 0
734564
@@ -185,11 +213,13 @@
734564
 		rc=$OCF_SUCCESS
734564
 		ocf_log info "The ip route has been already set.($NETWORK, $INTERFACE, $ROUTE_WO_SRC)"
734564
 	else
734564
-		$IP2UTIL route replace $NETWORK dev $INTERFACE src $1 || \
734564
-			errorexit "command 'ip route replace $NETWORK dev $INTERFACE src $1' failed"
734564
+		$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE src $1 || \
734564
+			errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE src $1' failed"
734564
 
734564
-		$CMDCHANGE $ROUTE_WO_SRC src $1 || \
734564
-			errorexit "command '$CMDCHANGE $ROUTE_WO_SRC src $1' failed"
734564
+		if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
734564
+			$CMDCHANGE $ROUTE_WO_SRC src $1 || \
734564
+				errorexit "command '$CMDCHANGE $ROUTE_WO_SRC src $1' failed"
734564
+		fi
734564
 		rc=$?
734564
 	fi
734564
 
734564
@@ -201,7 +231,7 @@
734564
 #	If one exists but it's not the same as the one specified, that's
734564
 #	an error.  Maybe that's the wrong behaviour because if this fails
734564
 #	then when IPaddr releases the associated interface (if there is one)
734564
-#	your default route will also get dropped ;-(
734564
+#	your matching route will also get dropped ;-(
734564
 #	The exit code should conform to LSB exit codes.
734564
 #
734564
 
734564
@@ -217,11 +247,13 @@
734564
 	  
734564
 	[ $rc = 2 ] && errorexit "The address you specified to stop does not match the preferred source address"
734564
 
734564
-	$IP2UTIL route replace $NETWORK dev $INTERFACE || \
734564
-		errorexit "command 'ip route replace $NETWORK dev $INTERFACE' failed"
734564
+	$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE || \
734564
+		errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE' failed"
734564
 
734564
-	$CMDCHANGE $ROUTE_WO_SRC || \
734564
-		errorexit "command '$CMDCHANGE $ROUTE_WO_SRC' failed"
734564
+	if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
734564
+		$CMDCHANGE $ROUTE_WO_SRC || \
734564
+			errorexit "command '$CMDCHANGE $ROUTE_WO_SRC' failed"
734564
+	fi
734564
 
734564
 	return $?
734564
 }
734564
@@ -406,6 +438,10 @@
734564
 		return $OCF_ERR_CONFIGURED
734564
 	fi
734564
 
734564
+	if ! echo "$OCF_RESKEY_destination" | grep -q "/"; then
734564
+		return $OCF_ERR_CONFIGURED
734564
+	fi
734564
+
734564
 
734564
 	if ! [ "x$SYSTYPE" = "xLinux" ]; then
734564
 		# checks after this point are only relevant for linux.
734564
@@ -486,7 +522,11 @@
734564
 }
734564
 
734564
 INTERFACE=`echo $findif_out | awk '{print $1}'`
734564
-NETWORK=`$IP2UTIL route list dev $INTERFACE scope link $PROTO match $ipaddress|grep -m 1 -o '^[^ ]*'`
734564
+if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
734564
+	NETWORK=`$IP2UTIL route list dev $INTERFACE scope link $PROTO match $ipaddress|grep -m 1 -o '^[^ ]*'`
734564
+else
734564
+	NETWORK="$OCF_RESKEY_destination"
734564
+fi
734564
 
734564
 case $1 in
734564
 	start)		srca_start $ipaddress