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['tmp_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['tmp_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['tmp_path']}/apinger.status");
239
	sleep(1);
240
	mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
241

    
242
	return 0;
243
}
244

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

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

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

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

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

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

    
303
	$gateways_arr = array();
304

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

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

    
334
			$gateways_arr[$gateway['name']] = $gateway;
335
			$i++;
336
		}
337
	} 
338

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

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

    
350
		if (empty($config['interfaces'][$ifname]))
351
			continue;
352

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

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

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

    
380
		if (is_ipaddr($gateway['gateway']))
381
			$gateway['monitor'] = $gateway['gateway'];
382

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

    
387
	return($gateways_arr);
388
}
389

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

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

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

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

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

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

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

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

    
545
function lookup_gateway_ip_by_name($name) {
546

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

    
553
	return false;
554
}
555

    
556
function lookup_gateway_monitor_ip_by_name($name) {
557

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

    
564
		return $gateway['monitor'];
565
        }
566

    
567
        return (false);
568
}
569

    
570
function lookup_gateway_interface_by_name($name) {
571

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

    
578
        return (false);
579
}
580

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

    
584
        $gw = NULL;
585

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

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

    
609
        /* return gateway */
610
        return ($gw);
611
}
612

    
613
?>
(24-24/62)