Project

General

Profile

Download (6.83 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_traceroute.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Paul Taylor (paultaylor@winndixie.com)
8
 * All rights reserved.
9
 *
10
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms, with or without
15
 * modification, are permitted provided that the following conditions are met:
16
 *
17
 * 1. Redistributions of source code must retain the above copyright notice,
18
 *    this list of conditions and the following disclaimer.
19
 *
20
 * 2. Redistributions in binary form must reproduce the above copyright
21
 *    notice, this list of conditions and the following disclaimer in
22
 *    the documentation and/or other materials provided with the
23
 *    distribution.
24
 *
25
 * 3. All advertising materials mentioning features or use of this software
26
 *    must display the following acknowledgment:
27
 *    "This product includes software developed by the pfSense Project
28
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
29
 *
30
 * 4. The names "pfSense" and "pfSense Project" must not be used to
31
 *    endorse or promote products derived from this software without
32
 *    prior written permission. For written permission, please contact
33
 *    coreteam@pfsense.org.
34
 *
35
 * 5. Products derived from this software may not be called "pfSense"
36
 *    nor may "pfSense" appear in their names without prior written
37
 *    permission of the Electric Sheep Fencing, LLC.
38
 *
39
 * 6. Redistributions of any form whatsoever must retain the following
40
 *    acknowledgment:
41
 *
42
 * "This product includes software developed by the pfSense Project
43
 * for use in the pfSense software distribution (http://www.pfsense.org/).
44
 *
45
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
46
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
49
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56
 * OF THE POSSIBILITY OF SUCH DAMAGE.
57
 */
58

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-traceroute
61
##|*NAME=Diagnostics: Traceroute
62
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
63
##|*MATCH=diag_traceroute.php*
64
##|-PRIV
65

    
66
require_once("guiconfig.inc");
67

    
68
$allowautocomplete = true;
69
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
70
include("head.inc");
71

    
72
/* Max TTL of both traceroute and traceroute6 is 255, but in practice more than
73
   64 hops would most likely time out in the GUI. If a user requires a
74
   traceroute that long, they can use the CLI. */
75
define('MAX_TTL', 64);
76
define('DEFAULT_TTL', 18);
77

    
78
// Set defaults in case they are not supplied.
79
$do_traceroute = false;
80
$host = '';
81
$ttl = DEFAULT_TTL;
82
$ipproto = 'ipv4';
83
$sourceip = 'any';
84

    
85
if ($_POST || $_REQUEST['host']) {
86
	unset($input_errors);
87

    
88
	/* input validation */
89
	$reqdfields = explode(" ", "host ttl");
90
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
91
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
92

    
93
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
94
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
95
	}
96
	$host = trim($_REQUEST['host']);
97
	$ipproto = $_REQUEST['ipproto'];
98
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
99
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
100
	}
101
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
102
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
103
	}
104

    
105
	$sourceip = $_REQUEST['sourceip'];
106
	$ttl = $_REQUEST['ttl'];
107
	$resolve = $_REQUEST['resolve'];
108
	$useicmp = $_REQUEST['useicmp'];
109

    
110
	if ($_POST && !$input_errors) {
111
		$do_traceroute = true;
112
	}
113

    
114
} else {
115
	$resolve = false;
116
	$useicmp = false;
117
}
118

    
119
if ($input_errors) {
120
	print_input_errors($input_errors);
121
}
122

    
123
/* Do the traceroute and show any error */
124
if ($do_traceroute) {
125
	$useicmpparam = isset($useicmp) ? "-I" : "";
126
	$n = isset($resolve) ? "" : "-n";
127

    
128
	$command = "/usr/sbin/traceroute";
129
	if ($ipproto == "ipv6") {
130
		$command .= "6";
131
		if (empty($n)) {
132
			$n = "-l";
133
		}
134
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
135
	} else {
136
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
137
	}
138

    
139
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
140
		$srcip = "-s " . escapeshellarg($ifaddr);
141
	}
142

    
143
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmpparam} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
144
	$result = shell_exec($cmd);
145

    
146
	if (!$result) {
147
		print_info_box(sprintf(gettext('Error: %s could not be traced/resolved'), $host));
148
	}
149
}
150

    
151
$form = new Form(false);
152

    
153
$section = new Form_Section('Traceroute');
154

    
155
$section->addInput(new Form_Input(
156
	'host',
157
	'Hostname',
158
	'text',
159
	$host,
160
	['placeholder' => 'Hostname to trace.']
161
));
162

    
163
$section->addInput(new Form_Select(
164
	'ipproto',
165
	'IP Protocol',
166
	$ipproto,
167
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
168
))->setHelp('Select the protocol to use.');
169

    
170
$section->addInput(new Form_Select(
171
	'sourceip',
172
	'Source Address',
173
	$sourceip,
174
	array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
175
))->setHelp('Select source address for the trace.');
176

    
177
$section->addInput(new Form_Select(
178
	'ttl',
179
	'Maximum number of hops',
180
	$ttl,
181
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
182
))->setHelp('Select the maximum number of network hops to trace.');
183

    
184
$section->addInput(new Form_Checkbox(
185
	'resolve',
186
	'Reverse Address Lookup',
187
	'',
188
	$resolve
189
))->setHelp('When checked, traceroute will attempt to perform a PTR lookup to locate hostnames for hops along the path. This will slow down the process as it has to wait for DNS replies.');
190

    
191
$section->addInput(new Form_Checkbox(
192
	'useicmp',
193
	gettext("Use ICMP"),
194
	'',
195
	$useicmp
196
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
197

    
198
$form->add($section);
199

    
200
$form->addGlobal(new Form_Button(
201
	'Submit',
202
	'Traceroute',
203
	null,
204
	'fa-rss'
205
))->addClass('btn-primary');
206

    
207
print $form;
208

    
209
/* Show the traceroute results */
210
if ($do_traceroute && $result) {
211
?>
212
	<div class="panel panel-default">
213
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
214
		<div class="panel-body">
215
<?php
216
	print('<pre>' . $result . '</pre>');
217
?>
218
		</div>
219
	</div>
220
<?php
221
}
222

    
223
include("foot.inc");
224
?>
(32-32/227)