Project

General

Profile

« Previous | Next » 

Revision 860a83e4

Added by Ermal LUÇI about 14 years ago

Switch back to the default gateway configured when possible when gateway switching is active

View differences:

etc/inc/gwlb.inc
368 368
	$gateway_groups_array = array();
369 369

  
370 370
	if (isset($config['system']['gw_switch_default'])) {
371
	/* 
372
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
373
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
374
	 */
375
	$upgw = "";
376
	$dfltgwdown = false;
377
	$dfltgwfound = false;
378
	foreach ($gateways_arr as $gwname => $gwsttng) {
379
		if (isset($gwsttng['defaultgw'])) {
380
			$dfltgwfound = true;
371
		/* 
372
		 * NOTE: The code below is meant to replace the default gateway when it goes down.
373
		 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
374
		 */
375
		$upgw = "";
376
		$dfltgwdown = false;
377
		$dfltgwfound = false;
378
		foreach ($gateways_arr as $gwname => $gwsttng) {
379
			if (isset($gwsttng['defaultgw'])) {
380
				$dfltgwfound = true;
381
				$dfltgwname = $gwname;
382
				if (stristr($gateways_status[$gwname]['status'], "down"))
383
					$dfltgwdown = true;
384
			}
385
			/* Keep a record of the last up gateway */
386
			if (empty($upgw) && !stristr($gateways_status[$gwname]['status'], "down"))
387
				$upgw = $gwname;
388
			if ($dfltgwdown == true && !empty($upgw))
389
				break;
390
		}
391
		if ($dfltgwfound == false) {
392
			$gwname = convert_friendly_interface_to_friendly_descr("wan");
381 393
			if (stristr($gateways_status[$gwname]['status'], "down"))
382 394
				$dfltgwdown = true;
383 395
		}
384
		/* Keep a record of the last up gateway */
385
		if (empty($upgw) && !stristr($gateways_status[$gwname]['status'], "down"))
386
			$upgw = $gwname;
387
		if ($dfltgwdown == true && !empty($upgw))
388
			break;
389
	}
390
	if ($dfltgwfound == false) {
391
		$gwname = convert_friendly_interface_to_friendly_descr("wan");
392
		if (stristr($gateways_status[$gwname]['status'], "down"))
393
			$dfltgwdown = true;
394
	}
395
	if ($dfltgwdown == true && !empty($upgw)) {
396
		if ($gateways_arr[$upgw]['gateway'] == "dynamic")
397
			$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
398
		if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
399
			log_error("Default gateway down setting {$upgw} as default!");
400
			mwexec("/sbin/route delete -inet default; /sbin/route add -inet default {$gateways_arr[$upgw]['gateway']}");
396
		if ($dfltgwdown == true && !empty($upgw)) {
397
			if ($gateways_arr[$upgw]['gateway'] == "dynamic")
398
				$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
399
			if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
400
				log_error("Default gateway down setting {$upgw} as default!");
401
				mwexec("/sbin/route delete -inet default; /sbin/route add -inet default {$gateways_arr[$upgw]['gateway']}");
402
			}
403
		} else {
404
			$defaultgw = trim(`/sbin/route -n get -inet default | /usr/bin/grep gateway | /usr/bin/sed 's/gateway://g'`, " \n");
405
			if ($defaultgw != $gateways_arr[$dfltgwname]['gateway'])
406
				mwexec("/sbin/route delete -inet default; /sbin/route add -inet default {$gateways_arr[$dfltgwname]['gateway']}");
401 407
		}
402
	}
403
	unset($upgw, $dfltgwfound, $dfltgwdown, $gwname, $gwsttng);
408
				
409
		unset($upgw, $dfltgwfound, $dfltgwdown, $gwname, $gwsttng);
404 410
	}
405 411

  
406 412
	if (is_array($config['gateways']['gateway_group'])) {

Also available in: Unified diff