Project

General

Profile

Download (6.59 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
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
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 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 93e1b16c Scott Ullrich
define('MAX_TTL', 64);
73
define('DEFAULT_TTL', 18);
74
75 cec703ec Phil Davis
// Set defaults in case they are not supplied.
76 ed69a496 jim-p
$do_traceroute = false;
77
$host = '';
78
$ttl = DEFAULT_TTL;
79 cec703ec Phil Davis
$ipproto = 'ipv4';
80
$sourceip = 'any';
81 77af9793 sbeaver
82 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
83 93e1b16c Scott Ullrich
	unset($input_errors);
84
85
	/* input validation */
86
	$reqdfields = explode(" ", "host ttl");
87 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
88 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
89 444381d1 sbeaver
90 b8cc74ed sullrich
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
91 ea6be4a7 Erik Fonnesbeck
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
92 93e1b16c Scott Ullrich
	}
93 643bc7b0 jim-p
	$host = trim($_REQUEST['host']);
94
	$ipproto = $_REQUEST['ipproto'];
95 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
96 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
97 5f601060 Phil Davis
	}
98
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
99 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
100 5f601060 Phil Davis
	}
101 93e1b16c Scott Ullrich
102 77af9793 sbeaver
	$sourceip = $_REQUEST['sourceip'];
103
	$ttl = $_REQUEST['ttl'];
104
	$resolve = $_REQUEST['resolve'];
105
	$useicmp = $_REQUEST['useicmp'];
106
107 cec703ec Phil Davis
	if ($_POST && !$input_errors) {
108 ed69a496 jim-p
		$do_traceroute = true;
109
	}
110
111 77af9793 sbeaver
} else {
112
	$resolve = false;
113 444381d1 sbeaver
	$useicmp = false;
114 77af9793 sbeaver
}
115 99e991fd Warren Baker
116 947141fd Phil Davis
if ($input_errors) {
117 77af9793 sbeaver
	print_input_errors($input_errors);
118 947141fd Phil Davis
}
119 444381d1 sbeaver
120 cec703ec Phil Davis
/* Do the traceroute and show any error */
121
if ($do_traceroute) {
122
	$useicmpparam = isset($useicmp) ? "-I" : "";
123
	$n = isset($resolve) ? "" : "-n";
124
125
	$command = "/usr/sbin/traceroute";
126
	if ($ipproto == "ipv6") {
127
		$command .= "6";
128
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
129
	} else {
130
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
131
	}
132
133
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
134
		$srcip = "-s " . escapeshellarg($ifaddr);
135
	}
136
137
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmpparam} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
138
	$result = shell_exec($cmd);
139
140
	if (!$result) {
141
		print_info_box(sprintf(gettext('Error: %s could not be traced/resolved'), $host));
142
	}
143
}
144
145 37676f4e jim-p
$form = new Form(false);
146 77af9793 sbeaver
147
$section = new Form_Section('Traceroute');
148
149
$section->addInput(new Form_Input(
150
	'host',
151
	'Hostname',
152
	'text',
153
	$host,
154
	['placeholder' => 'Hostname to trace.']
155
));
156
157
$section->addInput(new Form_Select(
158
	'ipproto',
159
	'IP Protocol',
160 cec703ec Phil Davis
	$ipproto,
161 77af9793 sbeaver
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
162 c6d73876 NOYB
))->setHelp('Select the protocol to use.');
163 77af9793 sbeaver
164
$section->addInput(new Form_Select(
165
	'sourceip',
166
	'Source Address',
167 cec703ec Phil Davis
	$sourceip,
168 c640d60e Phil Davis
	array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
169 c6d73876 NOYB
))->setHelp('Select source address for the trace.');
170 77af9793 sbeaver
171
$section->addInput(new Form_Select(
172
	'ttl',
173 053324be NOYB
	'Maximum number of hops',
174 77af9793 sbeaver
	$ttl,
175 cd7d902d sbeaver
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
176 c6d73876 NOYB
))->setHelp('Select the maximum number of network hops to trace.');
177 77af9793 sbeaver
178
$section->addInput(new Form_Checkbox(
179
	'resolve',
180
	'Reverse Address Lookup',
181
	'',
182
	$resolve
183 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.');
184 77af9793 sbeaver
185
$section->addInput(new Form_Checkbox(
186
	'useicmp',
187
	gettext("Use ICMP"),
188
	'',
189
	$useicmp
190
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
191
192
$form->add($section);
193 37676f4e jim-p
194
$form->addGlobal(new Form_Button(
195
	'Submit',
196 faab522f Renato Botelho
	'Traceroute',
197 37676f4e jim-p
	null,
198
	'fa-rss'
199
))->addClass('btn-primary');
200
201 77af9793 sbeaver
print $form;
202
203
/* Show the traceroute results */
204 cec703ec Phil Davis
if ($do_traceroute && $result) {
205 444381d1 sbeaver
?>
206
	<div class="panel panel-default">
207 cec703ec Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
208 444381d1 sbeaver
		<div class="panel-body">
209
<?php
210 cec703ec Phil Davis
	print('<pre>' . $result . '</pre>');
211 444381d1 sbeaver
?>
212
		</div>
213
	</div>
214
<?php
215 77af9793 sbeaver
}
216
217
include("foot.inc");
218
?>