Project

General

Profile

Download (19.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
  Copyright (C) 2008 Bill Marquette, Seth Mos
4
  Copyright (C) 2010 Ermal Lu?i
5
  All rights reserved.
6

    
7
  Redistribution and use in source and binary forms, with or without
8
  modification, are permitted provided that the following conditions are met:
9

    
10
  1. Redistributions of source code must retain the above copyright notice,
11
  this list of conditions and the following disclaimer.
12

    
13
  2. Redistributions in binary form must reproduce the above copyright
14
  notice, this list of conditions and the following disclaimer in the
15
  documentation and/or other materials provided with the distribution.
16

    
17
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
  AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
  POSSIBILITY OF SUCH DAMAGE.
27

    
28
	pfSense_BUILDER_BINARIES:	/usr/bin/killall	/sbin/route	/usr/local/sbin/apinger
29
	pfSense_MODULE:	routing
30

    
31
 */
32

    
33
/*
34
 * Creates monitoring configuration file and
35
 * adds apropriate static routes.
36
 */
37
function setup_gateways_monitor() {
38
	global $config, $g;
39

    
40
	$gateways_arr = return_gateways_array();
41
	if (!is_array($gateways_arr)) {
42
		log_error("No gateways to monitor. Apinger will not be run.");
43
		killbypid("{$g['varrun_path']}/apinger.pid");
44
		@unlink("{$g['varrun_path']}/apinger.status");
45
		return;
46
	}
47

    
48
	/* Default settings. Probably should move to globals.inc? */
49
	$a_settings = array();
50
	$a_settings['latencylow'] = "200";
51
	$a_settings['latencyhigh'] = "500";
52
	$a_settings['losslow'] = "10";
53
	$a_settings['losshigh'] = "20";
54

    
55
	$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
56
	$apingerconfig = <<<EOD
57

    
58
# pfSense apinger configuration file. Automatically Generated!
59

    
60
## User and group the pinger should run as
61
user "root"
62
group "wheel"
63

    
64
## Mailer to use (default: "/usr/lib/sendmail -t")
65
#mailer "/var/qmail/bin/qmail-inject" 
66

    
67
## Location of the pid-file (default: "/var/run/apinger.pid")
68
pid_file "{$g['varrun_path']}/apinger.pid"
69

    
70
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
71
#timestamp_format "%Y%m%d%H%M%S"
72

    
73
status {
74
	## File where the status information whould be written to
75
	file "{$g['varrun_path']}/apinger.status"
76
	## Interval between file updates
77
	## when 0 or not set, file is written only when SIGUSR1 is received
78
	interval 5s
79
}
80

    
81
########################################
82
# RRDTool status gathering configuration
83
# Interval between RRD updates
84
rrd interval 60s;
85

    
86
## These parameters can be overriden in a specific alarm configuration
87
alarm default { 
88
	command on "/usr/local/sbin/pfSctl -c 'filter reload'"
89
	command off "/usr/local/sbin/pfSctl -c 'filter reload'"
90
	combine 10s
91
}
92

    
93
## "Down" alarm definition. 
94
## This alarm will be fired when target doesn't respond for 30 seconds.
95
alarm down "down" {
96
	time 10s
97
}
98

    
99
## "Delay" alarm definition. 
100
## This alarm will be fired when responses are delayed more than 200ms
101
## it will be canceled, when the delay drops below 100ms
102
alarm delay "delay" {
103
	delay_low {$a_settings['latencylow']}ms
104
	delay_high {$a_settings['latencyhigh']}ms
105
}
106

    
107
## "Loss" alarm definition. 
108
## This alarm will be fired when packet loss goes over 20%
109
## it will be canceled, when the loss drops below 10%
110
alarm loss "loss" {
111
	percent_low {$a_settings['losslow']}
112
	percent_high {$a_settings['losshigh']}
113
}
114

    
115
target default {
116
	## How often the probe should be sent	
117
	interval 1s
118
	
119
	## How many replies should be used to compute average delay 
120
	## for controlling "delay" alarms
121
	avg_delay_samples 10
122
	
123
	## How many probes should be used to compute average loss
124
	avg_loss_samples 50
125

    
126
	## The delay (in samples) after which loss is computed
127
	## without this delays larger than interval would be treated as loss
128
	avg_loss_delay_samples 20
129

    
130
	## Names of the alarms that may be generated for the target
131
	alarms "down","delay","loss"
132

    
133
	## Location of the RRD
134
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
135
}
136

    
137
EOD;
138

    
139
	foreach($gateways_arr as $name => $gateway) {
140
		/* Do not monitor if such was requested */
141
		if (isset($gateway['monitor_disable']))
142
			continue;
143
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
144
			if (is_ipaddr($gateway['gateway']))
145
				$gateway['monitor'] = $gateway['gateway'];
146
			else /* No chance to get an ip to monitor skip target. */
147
				continue;
148
		}
149

    
150
		/* Interface ip is needed since apinger will bind a socket to it. */
151
		$gwifip = find_interface_ip($gateway['interface'], true);
152
		if (!is_ipaddr($gwifip))
153
			continue; //Skip this target
154

    
155
		$apingercfg = "target \"{$gateway['monitor']}\" {\n";
156
		$apingercfg .= "	description \"{$name}\"\n";
157
		$apingercfg .= "	srcip \"{$gwifip}\"\n";
158
		if (!empty($gateway['interval']) && intval($gateway['interval']) > 1)
159
			$apingercfg .= "	interval " . intval($gateway['interval']) . "s\n";
160
		$alarms = "";
161
		$alarmscfg = "";
162
		$override = false;
163
		if (!empty($gateway['lowloss'])) {
164
			$alarmscfg .= "alarm loss \"{$name}loss\" {\n";
165
			$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
166
       			$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
167
			$alarmscfg .= "}\n";
168
			$alarms .= "\"{$name}loss\"";
169
			$override = true;
170
		} else {
171
			if ($override == true)
172
				$alarms .= ",";
173
			$alarms .= "\"loss\"";
174
			$override = true;
175
		}
176
		if (!empty($gateway['latencylow'])) {
177
			$alarmscfg .= "alarm delay \"{$name}delay\" {\n";
178
			$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
179
			$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
180
			$alarmscfg .= "}\n";
181
			if ($override == true)
182
				$alarms .= ",";
183
			$alarms .= "\"{$name}delay\"";
184
			$override = true;
185
		} else {
186
			if ($override == true)
187
				$alarms .= ",";
188
			$alarms .= "\"delay\"";
189
			$override = true;
190
		}
191
		if (!empty($gateway['down'])) {
192
			$alarmscfg .= "alarm down \"{$name}down\" {\n";
193
			$alarmscfg .= "\ttime {$gateway['down']}s\n";
194
			$alarmscfg .= "}\n";
195
			if ($override == true)
196
				$alarms .= ",";
197
			$alarms .= "\"{$name}down\"";
198
			$override = true;
199
		} else {
200
			if ($override == true)
201
				$alarms .= ",";
202
			$alarms .= "\"down\"";
203
			$override = true;
204
		}
205
		if ($override == true)
206
			$apingercfg .= "\talarms override {$alarms};\n";
207

    
208
		$apingercfg .= "	rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
209
		$apingercfg .= "}\n";
210
		$apingercfg .= "\n";
211
		/*
212
		 * If the gateway is the same as the monitor we do not add a
213
		 * route as this will break the routing table.
214
		 * Add static routes for each gateway with their monitor IP
215
		 * not strictly necessary but is a added level of protection.
216
		 */
217
		if (is_ipaddr($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
218
			log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
219
			mwexec("/sbin/route change -host " . escapeshellarg($gateway['monitor']) .
220
				" " . escapeshellarg($gateway['gateway']), true);
221
		}
222

    
223
		$apingerconfig .= $alarmscfg;
224
		$apingerconfig .= $apingercfg;
225
	}
226
	fwrite($fd, $apingerconfig);
227
	fclose($fd);
228

    
229
	killbypid("{$g['varrun_path']}/apinger.pid");
230
	if (is_dir("{$g['tmp_path']}"))
231
		chmod("{$g['tmp_path']}", 01777);
232
	if (!is_dir("{$g['vardb_path']}/rrd"))
233
		mkdir("{$g['vardb_path']}/rrd", 0775);
234

    
235
	@chown("{$g['vardb_path']}/rrd", "nobody");
236

    
237
	/* start a new apinger process */
238
	@unlink("{$g['varrun_path']}/apinger.status");
239
	sleep(1);
240
	mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
241
	sleep(1);
242
	sigkillbypid("{$g['varrun_path']}/apinger.pid", "USR1");
243

    
244
	return 0;
245
}
246

    
247
/* return the status of the apinger targets as a array */
248
function return_gateways_status($byname = false) {
249
	global $config, $g;
250

    
251
	$apingerstatus = array();
252
	if (file_exists("{$g['varrun_path']}/apinger.status")) {
253
		$apingerstatus = file("{$g['varrun_path']}/apinger.status");
254
	}
255

    
256
	$status = array();
257
	foreach($apingerstatus as $line) {
258
		$info = explode("|", $line);
259
		if ($byname == false)
260
			$target = $info[0];
261
		else
262
			$target = $info[2];
263
		$status[$target]['monitorip'] = $info[0];
264
		$status[$target]['srcip'] = $info[1];
265
		$status[$target]['name'] = $info[2];
266
		$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
267
		$status[$target]['delay'] = empty($info[6]) ? 0 : $info[6];
268
		$status[$target]['loss'] = empty($info[7]) ? "0.0%" : $info[7] . "";
269
		$status[$target]['status'] = trim($info[8]);
270
	}
271

    
272
	/* tack on any gateways that have monitoring disabled */
273
	$gateways_arr = return_gateways_array();
274
	foreach($gateways_arr as $gwitem) {
275
		if(isset($gwitem['monitor_disable'])) {
276
			if(!is_ipaddr($gwitem['monitorip'])) {
277
				$realif = $gwitem['interface'];
278
				$tgtip = get_interface_gateway($realif);
279
				$srcip = find_interface_ip($realif);
280
			} else {
281
				$tgtip = $gwitem['monitorip'];
282
				$srcip = find_interface_ip($realif);
283
			}
284
			if($byname == true)
285
				$target = $gwitem['name'];
286
			else
287
				$target = $tgtip;
288

    
289
			$status[$target]['monitorip'] = $tgtip;
290
			$status[$target]['srcip'] = $srcip;
291
			$status[$target]['name'] = $gwitem['name'];
292
			$status[$target]['lastcheck'] = date('r');
293
			$status[$target]['delay'] = "0.0ms";
294
			$status[$target]['loss'] = "0.0%";
295
			$status[$target]['status'] = "none";
296
		}
297
	}
298
	return($status);
299
}
300

    
301
/* Return all configured gateways on the system */
302
function return_gateways_array($disabled = false) {
303
	global $config, $g;
304

    
305
	$gateways_arr = array();
306

    
307
	$i = 0;
308
	/* Process/add all the configured gateways. */
309
	if (is_array($config['gateways']['gateway_item'])) {
310
		foreach($config['gateways']['gateway_item'] as $gateway) {
311
			if(empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
312
				$gateway['gateway'] = get_interface_gateway($gateway['interface']);
313
				/* no IP address found, set to dynamic */
314
				if(! is_ipaddr($gateway['gateway']))
315
					$gateway['gateway'] = "dynamic";
316
				$gateway['dynamic'] = true;
317
			}
318
			if (isset($gateway['monitor_disable']))
319
				$gateway['monitor_disable'] = true;
320
			else if (empty($gateway['monitor']))
321
				$gateway['monitor'] = $gateway['gateway'];
322

    
323
			$gateway['friendlyiface'] = $gateway['interface'];
324
			$gateway['interface'] = get_real_interface($gateway['interface']);
325
			/* FIXME: Should this be enabled.
326
			 * Some interface like wan might be default but have no info recorded 
327
			 * the config.
328
			if ($gateway['friendlyiface'] == "wan" && !isset($gateway['defaultgw'])) {
329
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw"))
330
					$gateway['defaultgw'] = true;
331
			}
332
			 */
333
			/* include the gateway index as the attribute */
334
			$gateway['attribute'] = $i;
335

    
336
			$gateways_arr[$gateway['name']] = $gateway;
337
			$i++;
338
		}
339
	} 
340

    
341
	/* Loop through all interfaces with a gateway and add it to a array */
342
	if ($disabled == false)
343
		$iflist = get_configured_interface_with_descr();
344
	else
345
		$iflist = get_configured_interface_with_descr(false, true);
346

    
347
	/* Process/add dynamic gateways. */
348
	foreach($iflist as $ifname => $friendly ) {
349
		if(! interface_has_gateway($ifname))
350
			continue;
351

    
352
		if (empty($config['interfaces'][$ifname]))
353
			continue;
354

    
355
		$ifcfg = &$config['interfaces'][$ifname];
356
		if (!empty($ifcfg['ipaddr']) && is_ipaddr($ifcfg['ipaddr']))
357
			continue;
358

    
359
		$gateway = array();
360
		$gateway['dynamic'] = false;
361
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
362
		$gateway['interface'] = get_real_interface($ifname);
363
		$gateway['friendlyiface'] = $ifname;
364
		$gateway['name'] = $friendly;
365
		$gateway['attribute'] = "system";
366
	
367
		if ($gateway['dynamic'] === "default") {
368
			$gateway['defaultgw'] = true;
369
			$gateway['dynamic'] = true;
370
		}
371
		/* Loopback dummy for dynamic interfaces without a IP */
372
		if (!is_ipaddr($gateway['gateway']) && $gateway['dynamic'] == true)
373
			$gateway['gateway'] = "dynamic";
374

    
375
		/* automatically skip known static and dynamic gateways we have a array entry for */
376
		foreach($gateways_arr as $gateway_item) {
377
			if (($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) ||
378
				($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true))
379
					continue 2;
380
		}
381

    
382
		if (is_ipaddr($gateway['gateway']))
383
			$gateway['monitor'] = $gateway['gateway'];
384

    
385
		$gateway['descr'] = "Interface {$friendly} Dynamic Gateway";
386
		$gateways_arr[$friendly] = $gateway;
387
	}
388

    
389
	return($gateways_arr);
390
}
391

    
392
/*
393
 * Return an array with all gateway groups with name as key
394
 * All gateway groups will be processed before returning the array.
395
 */
396
function return_gateway_groups_array() {
397
	global $config, $g;
398

    
399
	/* fetch the current gateways status */
400
	$gateways_status = return_gateways_status(true);
401
	$gateways_arr = return_gateways_array();
402
	$gateway_groups_array = array();
403

    
404
	if (isset($config['system']['gw_switch_default'])) {
405
		/* 
406
		 * NOTE: The code below is meant to replace the default gateway when it goes down.
407
		 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
408
		 */
409
		$upgw = "";
410
		$dfltgwdown = false;
411
		$dfltgwfound = false;
412
		foreach ($gateways_arr as $gwname => $gwsttng) {
413
			if (isset($gwsttng['defaultgw'])) {
414
				$dfltgwfound = true;
415
				$dfltgwname = $gwname;
416
				if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down"))
417
					$dfltgwdown = true;
418
			}
419
			/* Keep a record of the last up gateway */
420
			/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
421
			if (empty($upgw) && (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) && $gwsttng[$gwname]['friendlyiface'] != "lan")
422
				$upgw = $gwname;
423
			if ($dfltgwdown == true && !empty($upgw))
424
				break;
425
		}
426
		if ($dfltgwfound == false) {
427
			$gwname = convert_friendly_interface_to_friendly_descr("wan");
428
			if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down"))
429
				$dfltgwdown = true;
430
		}
431
		if ($dfltgwdown == true && !empty($upgw)) {
432
			if ($gateways_arr[$upgw]['gateway'] == "dynamic")
433
				$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
434
			if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
435
				log_error("Default gateway down setting {$upgw} as default!");
436
				mwexec("/sbin/route change -inet default {$gateways_arr[$upgw]['gateway']}");
437
			}
438
		} else {
439
			$defaultgw = trim(`/sbin/route -n get -inet default | /usr/bin/grep gateway | /usr/bin/sed 's/gateway://g'`, " \n");
440
			if ($defaultgw != $gateways_arr[$dfltgwname]['gateway'])
441
				mwexec("/sbin/route change -inet default {$gateways_arr[$dfltgwname]['gateway']}");
442
		}
443
				
444
		unset($upgw, $dfltgwfound, $dfltgwdown, $gwname, $gwsttng);
445
	}
446

    
447
	if (is_array($config['gateways']['gateway_group'])) {
448
		foreach($config['gateways']['gateway_group'] as $group) {
449
			/* create array with group gateways members seperated by tier */
450
			$tiers = array();
451
			$backupplan = array();
452
			foreach($group['item'] as $item) {
453
				$itemsplit = explode("|", $item);
454
				$tier = $itemsplit[1];
455
				$gwname = $itemsplit[0];
456

    
457
				/* Do it here rather than reiterating again the group in case no member is up. */
458
				$backupplan[$tier][] = $gwname;
459

    
460
				/* check if the gateway is available before adding it to the array */
461
				if (!empty($gateways_status[$gwname])) {
462
					$status = $gateways_status[$gwname];
463
					$gwdown = false;
464
					if (stristr($status['status'], "down")) {
465
						$msg = "MONITOR: {$gwname} is down, removing from routing group";
466
						$gwdown = true;
467
					} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
468
						/* packet loss */
469
						$msg = "MONITOR: {$gwname} has packet loss, removing from routing group";
470
						$gwdown = true;
471
					} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
472
						/* high latency */
473
						$msg = "MONITOR: {$gwname} has high latency, removing from routing group";
474
						$gwdown = true;
475
					}
476
					if ($gwdown == true) {
477
						log_error($msg);
478
						notify_via_growl($msg);
479
						notify_via_smtp($msg);
480
					} else
481
						/* Online add member */
482
						$tiers[$tier][] = $gwname;
483
				} else if (isset($gateways_arr[$gwname]['monitor_disable']))
484
					$tiers[$tier][] = $gwname;
485
			}
486
			$tiers_count = count($tiers);
487
			if($tiers_count == 0) {
488
				/* Oh dear, we have no members! Engage Plan B */
489
				if (!$g['booting']) {
490
					$msg = "Gateways status could not be determined, considering all as up/active.";
491
					log_error($msg);
492
					notify_via_growl($msg);
493
					notify_via_smtp($msg);
494
				}
495
				$tiers = $backupplan;
496
			}
497
			/* sort the tiers array by the tier key */
498
			ksort($tiers);
499

    
500
			/* we do not really foreach the tiers as we stop after the first tier */
501
			foreach($tiers as $tier) {
502
				/* process all gateways in this tier */
503
				foreach($tier as $member) {
504
					/* determine interface gateway */
505
					if (isset($gateways_arr[$member])) {
506
						$gateway = $gateways_arr[$member];
507
						$int = $gateway['interface'];
508
						$gatewayip = "";
509
						if(is_ipaddr($gateway['gateway'])) 
510
							$gatewayip = $gateway['gateway'];
511
						else if ($int <> "")
512
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
513
					
514
						if (($int <> "") && is_ipaddr($gatewayip)) {
515
							$groupmember = array();
516
							$groupmember['int']  = $int;
517
							$groupmember['gwip']  = $gatewayip;
518
							$groupmember['weight']  = isset($gateway['weight']) ? $gateway['weight'] : 1;
519
							$gateway_groups_array[$group['name']][] = $groupmember;
520
						}
521
					}
522
				}
523
				/* we should have the 1st available tier now, exit stage left */
524
				break;
525
			}
526
		}
527
	}
528
	return ($gateway_groups_array);
529
}
530

    
531
/* Update DHCP WAN Interface ip address in gateway group item */
532
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
533
	global $config, $g;
534
	foreach($config['gateways']['gateway_item'] as & $gw) {	
535
		if($gw['interface'] == $interface) {
536
			$current_gw = get_interface_gateway($interface);
537
			if($gw['gateway'] <> $current_gw) {
538
				$gw['gateway'] = $current_gw;
539
				$changed = true;
540
			}
541
		}
542
	}
543
	if($changed && $current_gw)
544
		write_config("Updating gateway group gateway for $interface - new gateway is $current_gw");
545
}
546

    
547
function lookup_gateway_ip_by_name($name) {
548

    
549
	$gateways_arr = return_gateways_array();
550
        foreach ($gateways_arr as $gname => $gw) {
551
                if ($gw['name'] === $name || $gname === $name)
552
                        return $gw['gateway'];
553
        }
554

    
555
	return false;
556
}
557

    
558
function lookup_gateway_monitor_ip_by_name($name) {
559

    
560
        $gateways_arr = return_gateways_array();
561
	if (!empty($gateways_arr[$name])) {
562
		$gateway = $gateways_arr[$name];
563
		if(!is_ipaddr($gateway['monitor']))
564
			return $gateway['gateway'];
565

    
566
		return $gateway['monitor'];
567
        }
568

    
569
        return (false);
570
}
571

    
572
function lookup_gateway_interface_by_name($name) {
573

    
574
        $gateways_arr = return_gateways_array();
575
	if (!empty($gateways_arr[$name])) {
576
		$interfacegw = $gateway['interface'];
577
		return ($interfacegw);
578
        }
579

    
580
        return (false);
581
}
582

    
583
function get_interface_gateway($interface, &$dynamic = false) {
584
        global $config, $g;
585

    
586
        $gw = NULL;
587

    
588
        $gwcfg = $config['interfaces'][$interface];
589
        if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
590
               	foreach($config['gateways']['gateway_item'] as $gateway) {
591
                        if ($gateway['name'] == $gwcfg['gateway']) {
592
                                $gw = $gateway['gateway'];
593
				break;
594
			}
595
                }
596
	}
597

    
598
        // for dynamic interfaces we handle them through the $interface_router file.
599
        if (!is_ipaddr($gw) && !is_ipaddr($gwcfg['ipaddr'])) {
600
                $realif = get_real_interface($interface);
601
                if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
602
                        $gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
603
					$dynamic = true;
604
                }
605
                if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw"))
606
					$dynamic = "default";
607
			
608
				
609
        }
610

    
611
        /* return gateway */
612
        return ($gw);
613
}
614

    
615
?>
(23-23/61)