Project

General

Profile

Download (6.83 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3 aaec5634 Renato Botelho
 * diag_traceroute.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * 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 fd9ebcd5 Stephen Beaver
 */
58 93e1b16c Scott Ullrich
59 6b07c15a Matthew Grooms
##|+PRIV
60
##|*IDENT=page-diagnostics-traceroute
61 5230f468 jim-p
##|*NAME=Diagnostics: Traceroute
62 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
63
##|*MATCH=diag_traceroute.php*
64
##|-PRIV
65
66 aceaf18c Phil Davis
require_once("guiconfig.inc");
67 13d193c2 Scott Ullrich
68 643bc7b0 jim-p
$allowautocomplete = true;
69 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
70 b8a66425 Scott Ullrich
include("head.inc");
71 444381d1 sbeaver
72 f5da84f3 jim-p
/* 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 93e1b16c Scott Ullrich
define('MAX_TTL', 64);
76
define('DEFAULT_TTL', 18);
77
78 cec703ec Phil Davis
// Set defaults in case they are not supplied.
79 ed69a496 jim-p
$do_traceroute = false;
80
$host = '';
81
$ttl = DEFAULT_TTL;
82 cec703ec Phil Davis
$ipproto = 'ipv4';
83
$sourceip = 'any';
84 77af9793 sbeaver
85 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
86 93e1b16c Scott Ullrich
	unset($input_errors);
87
88
	/* input validation */
89
	$reqdfields = explode(" ", "host ttl");
90 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
91 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
92 444381d1 sbeaver
93 b8cc74ed sullrich
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
94 ea6be4a7 Erik Fonnesbeck
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
95 93e1b16c Scott Ullrich
	}
96 643bc7b0 jim-p
	$host = trim($_REQUEST['host']);
97
	$ipproto = $_REQUEST['ipproto'];
98 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
99 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
100 5f601060 Phil Davis
	}
101
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
102 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
103 5f601060 Phil Davis
	}
104 93e1b16c Scott Ullrich
105 77af9793 sbeaver
	$sourceip = $_REQUEST['sourceip'];
106
	$ttl = $_REQUEST['ttl'];
107
	$resolve = $_REQUEST['resolve'];
108
	$useicmp = $_REQUEST['useicmp'];
109
110 cec703ec Phil Davis
	if ($_POST && !$input_errors) {
111 ed69a496 jim-p
		$do_traceroute = true;
112
	}
113
114 77af9793 sbeaver
} else {
115
	$resolve = false;
116 444381d1 sbeaver
	$useicmp = false;
117 77af9793 sbeaver
}
118 99e991fd Warren Baker
119 947141fd Phil Davis
if ($input_errors) {
120 77af9793 sbeaver
	print_input_errors($input_errors);
121 947141fd Phil Davis
}
122 444381d1 sbeaver
123 cec703ec Phil Davis
/* 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 f5da84f3 jim-p
		if (empty($n)) {
132
			$n = "-l";
133
		}
134 cec703ec Phil Davis
		$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 37676f4e jim-p
$form = new Form(false);
152 77af9793 sbeaver
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 cec703ec Phil Davis
	$ipproto,
167 77af9793 sbeaver
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
168 c6d73876 NOYB
))->setHelp('Select the protocol to use.');
169 77af9793 sbeaver
170
$section->addInput(new Form_Select(
171
	'sourceip',
172
	'Source Address',
173 cec703ec Phil Davis
	$sourceip,
174 c640d60e Phil Davis
	array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
175 c6d73876 NOYB
))->setHelp('Select source address for the trace.');
176 77af9793 sbeaver
177
$section->addInput(new Form_Select(
178
	'ttl',
179 053324be NOYB
	'Maximum number of hops',
180 77af9793 sbeaver
	$ttl,
181 cd7d902d sbeaver
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
182 c6d73876 NOYB
))->setHelp('Select the maximum number of network hops to trace.');
183 77af9793 sbeaver
184
$section->addInput(new Form_Checkbox(
185
	'resolve',
186
	'Reverse Address Lookup',
187
	'',
188
	$resolve
189 288a2a0f Phil Davis
))->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 77af9793 sbeaver
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 37676f4e jim-p
200
$form->addGlobal(new Form_Button(
201
	'Submit',
202 faab522f Renato Botelho
	'Traceroute',
203 37676f4e jim-p
	null,
204
	'fa-rss'
205
))->addClass('btn-primary');
206
207 77af9793 sbeaver
print $form;
208
209
/* Show the traceroute results */
210 cec703ec Phil Davis
if ($do_traceroute && $result) {
211 444381d1 sbeaver
?>
212
	<div class="panel panel-default">
213 cec703ec Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
214 444381d1 sbeaver
		<div class="panel-body">
215
<?php
216 cec703ec Phil Davis
	print('<pre>' . $result . '</pre>');
217 444381d1 sbeaver
?>
218
		</div>
219
	</div>
220
<?php
221 77af9793 sbeaver
}
222
223
include("foot.inc");
224
?>