811adb
From 50a596bfb977b18902dc62b99145bbd1a087690a Mon Sep 17 00:00:00 2001
811adb
From: Oyvind Albrigtsen <oalbrigt@redhat.com>
811adb
Date: Tue, 1 Mar 2022 11:06:07 +0100
811adb
Subject: [PATCH] IPsrcaddr: fixes
811adb
811adb
- use findif.sh to detect secondary interfaces
811adb
- get metric and proto to update the correct route/update it correctly
811adb
- match route using interface to fail when trying to update secondary
811adb
  interfaces without specifying destination (would update default route
811adb
  before)
811adb
- also use PRIMARY_IP/OPTS during stop-action for default routes (to get
811adb
  back to the exact routes we started with)
811adb
- dont fail during stop-action if route doesnt exist
811adb
- use [[:blank:]] for WS to follow POSIX standard (suggested by nrwahl)
811adb
---
811adb
 heartbeat/IPsrcaddr | 35 +++++++++++++++++++----------------
811adb
 1 file changed, 19 insertions(+), 16 deletions(-)
811adb
811adb
diff --git a/heartbeat/IPsrcaddr b/heartbeat/IPsrcaddr
811adb
index c82adc0e9..7dbf65ff5 100755
811adb
--- a/heartbeat/IPsrcaddr
811adb
+++ b/heartbeat/IPsrcaddr
811adb
@@ -52,6 +52,7 @@
811adb
 # Initialization:
811adb
 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
811adb
 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
811adb
+. ${OCF_FUNCTIONS_DIR}/findif.sh
811adb
 
811adb
 # Defaults
811adb
 OCF_RESKEY_ipaddress_default=""
811adb
@@ -181,19 +182,21 @@ errorexit() {
811adb
 #
811adb
 #	where the src clause "src Y.Y.Y.Y" may or may not be present
811adb
 
811adb
-WS="[`echo -en ' \t'`]"
811adb
+WS="[[:blank:]]"
811adb
 OCTET="[0-9]\{1,3\}"
811adb
 IPADDR="\($OCTET\.\)\{3\}$OCTET"
811adb
 SRCCLAUSE="src$WS$WS*\($IPADDR\)"
811adb
 MATCHROUTE="\(.*${WS}\)\($SRCCLAUSE\)\($WS.*\|$\)"
811adb
-FINDIF=$HA_BIN/findif
811adb
+METRICCLAUSE=".*\(metric$WS[^ ]\+\)"
811adb
+PROTOCLAUSE=".*\(proto$WS[^ ]\+\)"
811adb
+FINDIF=findif
811adb
 
811adb
 # findif needs that to be set
811adb
 export OCF_RESKEY_ip=$OCF_RESKEY_ipaddress
811adb
 
811adb
 srca_read() {
811adb
 	# Capture matching route - doublequotes prevent word splitting...
811adb
-	ROUTE="`$CMDSHOW 2> /dev/null`" || errorexit "command '$CMDSHOW' failed"
811adb
+	ROUTE="`$CMDSHOW dev $INTERFACE 2> /dev/null`" || errorexit "command '$CMDSHOW' failed"
811adb
 
811adb
 	# ... so we can make sure there is only 1 matching route
811adb
 	[ 1 -eq `echo "$ROUTE" | wc -l` ] || \
811adb
@@ -201,7 +204,7 @@ srca_read() {
811adb
 
811adb
 	# But there might still be no matching route
811adb
 	[ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] && [ -z "$ROUTE" ] && \
811adb
-		 ! ocf_is_probe && errorexit "no matching route exists"
811adb
+		 ! ocf_is_probe && [ "$__OCF_ACTION" != stop ] && errorexit "no matching route exists"
811adb
 
811adb
 	# Sed out the source ip address if it exists
811adb
 	SRCIP=`echo $ROUTE | sed -n "s/$MATCHROUTE/\3/p"`
811adb
@@ -232,8 +235,8 @@ srca_start() {
811adb
 		rc=$OCF_SUCCESS
811adb
 		ocf_log info "The ip route has been already set.($NETWORK, $INTERFACE, $ROUTE_WO_SRC)"
811adb
 	else
811adb
-		$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE src $1 || \
811adb
-			errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE src $1' failed"
811adb
+		$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE $PROTO src $1 $METRIC || \
811adb
+			errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE $PROTO src $1 $METRIC' failed"
811adb
 
811adb
 		if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
811adb
 			$CMDCHANGE $ROUTE_WO_SRC src $1 || \
811adb
@@ -266,14 +269,11 @@ srca_stop() {
811adb
 	  
811adb
 	[ $rc = 2 ] && errorexit "The address you specified to stop does not match the preferred source address"
811adb
 
811adb
-	OPTS=""
811adb
-	if [ "$OCF_RESKEY_destination" != "0.0.0.0/0" ] ;then
811adb
-		PRIMARY_IP="$($IP2UTIL -4 -o addr show dev $INTERFACE primary | awk '{split($4,a,"/");print a[1]}')"
811adb
-		OPTS="proto kernel scope host src $PRIMARY_IP"
811adb
-	fi
811adb
+	PRIMARY_IP="$($IP2UTIL -4 -o addr show dev $INTERFACE primary | awk '{split($4,a,"/");print a[1]}')"
811adb
+	OPTS="proto kernel scope link src $PRIMARY_IP"
811adb
 
811adb
-	$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE $OPTS || \
811adb
-		errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE $OPTS' failed"
811adb
+	$IP2UTIL route replace $TABLE $NETWORK dev $INTERFACE $OPTS $METRIC || \
811adb
+		errorexit "command 'ip route replace $TABLE $NETWORK dev $INTERFACE $OPTS $METRIC' failed"
811adb
 
811adb
 	if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
811adb
 		$CMDCHANGE $ROUTE_WO_SRC || \
811adb
@@ -539,16 +539,19 @@ if [ $rc -ne $OCF_SUCCESS ]; then
811adb
 	esac
811adb
 fi
811adb
 
811adb
-findif_out=`$FINDIF -C`
811adb
+findif_out=`$FINDIF`
811adb
 rc=$?
811adb
 [ $rc -ne 0 ] && {
811adb
-	ocf_exit_reason "[$FINDIF -C] failed"
811adb
+	ocf_exit_reason "[$FINDIF] failed"
811adb
 	exit $rc
811adb
 }
811adb
 
811adb
 INTERFACE=`echo $findif_out | awk '{print $1}'`
811adb
+LISTROUTE=`$IP2UTIL route list dev $INTERFACE scope link $PROTO match $ipaddress`
811adb
+METRIC=`echo $LISTROUTE | sed -n "s/$METRICCLAUSE/\1/p"`
811adb
+[ -z "$PROTO" ] && PROTO=`echo $LISTROUTE | sed -n "s/$PROTOCLAUSE/\1/p"`
811adb
 if [ "$OCF_RESKEY_destination" = "0.0.0.0/0" ] ;then
811adb
-	NETWORK=`$IP2UTIL route list dev $INTERFACE scope link $PROTO match $ipaddress|grep -m 1 -o '^[^ ]*'`
811adb
+	NETWORK=`echo $LISTROUTE | grep -m 1 -o '^[^ ]*'`
811adb
 
811adb
 	if [ -z "$NETWORK" ]; then
811adb
 		err_str="command '$IP2UTIL route list dev $INTERFACE scope link $PROTO"