--- /usr/local/www/system_gateways_edit.php.orig	2014-09-11 10:47:02.000000000 +0200
+++ /usr/local/www/system_gateways_edit.php	2014-09-11 10:55:23.000000000 +0200
@@ -84,6 +84,7 @@ if (isset($id) && $a_gateways[$id]) {
 	$pconfig['monitor_disable'] = isset($a_gateways[$id]['monitor_disable']);
 	$pconfig['descr'] = $a_gateways[$id]['descr'];
 	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
+	$pconfig['srcip'] = $a_gateways[$id]['srcip'];
 }
 
 if (isset($_GET['dup']) && is_numericint($_GET['dup'])) {
@@ -184,6 +185,9 @@ if ($_POST) {
 	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
 		$input_errors[] = gettext("A valid monitor IP address must be specified.");
 	}
+	if (($_POST['srcip'] <> "") && !is_ipaddr($_POST['srcip'])) {
+		$input_errors[] = gettext("A valid source IP address must be specified.");
+	}
 	/* only allow correct IPv4 and IPv6 gateway addresses */
 	if (($_POST['gateway'] <> "") && is_ipaddr($_POST['gateway']) && $_POST['gateway'] != "dynamic") {
 		if(is_ipaddrv6($_POST['gateway']) && ($_POST['ipprotocol'] == "inet")) {
@@ -202,6 +206,15 @@ if ($_POST) {
 			$input_errors[] = gettext("The IPv4 monitor address '{$_POST['monitor']}' can not be used on a IPv6 gateway'.");
 		}
 	}
+	/* only allow correct IPv4 and IPv6 source ip addresses */
+	if (($_POST['srcip'] <> "") && is_ipaddr($_POST['srcip'])) {
+		if(is_ipaddrv6($_POST['srcip']) && ($_POST['ipprotocol'] == "inet")) {
+			$input_errors[] = gettext("The IPv6 source IP address '{$_POST['srcip']}' can not be used on a IPv4 gateway'.");
+		}
+		if(is_ipaddrv4($_POST['srcip']) && ($_POST['ipprotocol'] == "inet6")) {
+			$input_errors[] = gettext("The IPv4 source IP address '{$_POST['srcip']}' can not be used on a IPv6 gateway'.");
+		}
+	}
 
 	if (isset($_POST['name'])) {
 		/* check for overlaps */
@@ -389,6 +402,8 @@ if ($_POST) {
 			$gateway['monitor_disable'] = true;
 		if (is_ipaddr($_POST['monitor']))
 			$gateway['monitor'] = $_POST['monitor'];
+		if (is_ipaddr($_POST['srcip']))
+			$gateway['srcip'] = $_POST['srcip'];
 
 		/* NOTE: If monitor ip is changed need to cleanup the old static route */
 		if ($_POST['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['monitor']) &&
@@ -576,6 +591,20 @@ function monitor_change() {
 				</td>
 			</tr>
 			<tr>
+				<td width="22%" valign="top" class="vncell"><?=gettext("Source IP"); ?></td>
+				<td width="78%" class="vtable">
+					<?php
+						$srcip = htmlspecialchars($pconfig['srcip']);
+					?>
+					<input name="srcip" type="text" id="srcip" value="<?php echo htmlspecialchars($srcip); ?>" size="28" />
+					<strong><?=gettext("Alternative source IP"); ?></strong> <br />
+					<?=gettext("Enter an alternative address here to be used as the source IP to monitor the link. " .
+					"This may be required if there are many IP addresses on an interface to ensure that the correct " .
+					"source IP address is selected to ping the monitor/gateway"); ?>.
+					<br />
+				</td>
+			</tr>
+			<tr>
 				<td width="22%" valign="top" class="vncell"><?=gettext("Advanced");?></td>
 				<td width="78%" class="vtable">
 					<?php $showbutton = (!empty($pconfig['latencylow']) || !empty($pconfig['latencyhigh']) || !empty($pconfig['losslow']) || !empty($pconfig['losshigh']) || (isset($pconfig['weight']) && $pconfig['weight'] > 1) || (isset($pconfig['interval']) && ($pconfig['interval'] > $apinger_default['interval'])) || (isset($pconfig['down']) && !($pconfig['down'] == $apinger_default['down']))); ?>
