Project

General

Profile

Download (5.17 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3 c5d81585 Renato Botelho
 * diag_traceroute.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 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 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17 c5d81585 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
19 c5d81585 Renato Botelho
 *
20 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25 fd9ebcd5 Stephen Beaver
 */
26 93e1b16c Scott Ullrich
27 6b07c15a Matthew Grooms
##|+PRIV
28
##|*IDENT=page-diagnostics-traceroute
29 5230f468 jim-p
##|*NAME=Diagnostics: Traceroute
30 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
31
##|*MATCH=diag_traceroute.php*
32
##|-PRIV
33
34 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
35 13d193c2 Scott Ullrich
36 643bc7b0 jim-p
$allowautocomplete = true;
37 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
38 b8a66425 Scott Ullrich
include("head.inc");
39 444381d1 sbeaver
40 2afb45ba jim-p
/* Max TTL of both traceroute and traceroute6 is 255, but in practice more than
41
   64 hops would most likely time out in the GUI. If a user requires a
42
   traceroute that long, they can use the CLI. */
43 93e1b16c Scott Ullrich
define('MAX_TTL', 64);
44
define('DEFAULT_TTL', 18);
45
46 cec703ec Phil Davis
// Set defaults in case they are not supplied.
47 ed69a496 jim-p
$do_traceroute = false;
48
$host = '';
49
$ttl = DEFAULT_TTL;
50 cec703ec Phil Davis
$ipproto = 'ipv4';
51
$sourceip = 'any';
52 77af9793 sbeaver
53 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
54 93e1b16c Scott Ullrich
	unset($input_errors);
55
56
	/* input validation */
57
	$reqdfields = explode(" ", "host ttl");
58 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
59 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
60 444381d1 sbeaver
61 b8cc74ed sullrich
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
62 ea6be4a7 Erik Fonnesbeck
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
63 93e1b16c Scott Ullrich
	}
64 643bc7b0 jim-p
	$host = trim($_REQUEST['host']);
65
	$ipproto = $_REQUEST['ipproto'];
66 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
67 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
68 5f601060 Phil Davis
	}
69
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
70 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
71 5f601060 Phil Davis
	}
72 93e1b16c Scott Ullrich
73 77af9793 sbeaver
	$sourceip = $_REQUEST['sourceip'];
74
	$ttl = $_REQUEST['ttl'];
75
	$resolve = $_REQUEST['resolve'];
76
	$useicmp = $_REQUEST['useicmp'];
77
78 cec703ec Phil Davis
	if ($_POST && !$input_errors) {
79 ed69a496 jim-p
		$do_traceroute = true;
80
	}
81
82 77af9793 sbeaver
} else {
83
	$resolve = false;
84 444381d1 sbeaver
	$useicmp = false;
85 77af9793 sbeaver
}
86 99e991fd Warren Baker
87 947141fd Phil Davis
if ($input_errors) {
88 77af9793 sbeaver
	print_input_errors($input_errors);
89 947141fd Phil Davis
}
90 444381d1 sbeaver
91 cec703ec Phil Davis
/* Do the traceroute and show any error */
92
if ($do_traceroute) {
93
	$useicmpparam = isset($useicmp) ? "-I" : "";
94
	$n = isset($resolve) ? "" : "-n";
95
96
	$command = "/usr/sbin/traceroute";
97
	if ($ipproto == "ipv6") {
98
		$command .= "6";
99 2afb45ba jim-p
		if (empty($n)) {
100
			$n = "-l";
101
		}
102 cec703ec Phil Davis
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
103
	} else {
104
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
105
	}
106
107
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
108
		$srcip = "-s " . escapeshellarg($ifaddr);
109
	}
110
111
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmpparam} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
112
	$result = shell_exec($cmd);
113
114
	if (!$result) {
115
		print_info_box(sprintf(gettext('Error: %s could not be traced/resolved'), $host));
116
	}
117
}
118
119 37676f4e jim-p
$form = new Form(false);
120 77af9793 sbeaver
121
$section = new Form_Section('Traceroute');
122
123
$section->addInput(new Form_Input(
124
	'host',
125 3e2028f4 Phil Davis
	'*Hostname',
126 77af9793 sbeaver
	'text',
127
	$host,
128
	['placeholder' => 'Hostname to trace.']
129
));
130
131
$section->addInput(new Form_Select(
132
	'ipproto',
133 fe54f091 Phil Davis
	'*IP Protocol',
134 cec703ec Phil Davis
	$ipproto,
135 77af9793 sbeaver
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
136 3728bbe5 NOYB
))->setHelp('Select the protocol to use.');
137 77af9793 sbeaver
138
$section->addInput(new Form_Select(
139
	'sourceip',
140 fe54f091 Phil Davis
	'*Source Address',
141 cec703ec Phil Davis
	$sourceip,
142 c640d60e Phil Davis
	array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
143 3728bbe5 NOYB
))->setHelp('Select source address for the trace.');
144 77af9793 sbeaver
145
$section->addInput(new Form_Select(
146
	'ttl',
147 7a5e4999 NOYB
	'Maximum number of hops',
148 77af9793 sbeaver
	$ttl,
149 cd7d902d sbeaver
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
150 3728bbe5 NOYB
))->setHelp('Select the maximum number of network hops to trace.');
151 77af9793 sbeaver
152
$section->addInput(new Form_Checkbox(
153
	'resolve',
154
	'Reverse Address Lookup',
155
	'',
156
	$resolve
157 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.');
158 77af9793 sbeaver
159
$section->addInput(new Form_Checkbox(
160
	'useicmp',
161
	gettext("Use ICMP"),
162
	'',
163
	$useicmp
164
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
165
166
$form->add($section);
167 37676f4e jim-p
168
$form->addGlobal(new Form_Button(
169
	'Submit',
170 faab522f Renato Botelho
	'Traceroute',
171 37676f4e jim-p
	null,
172
	'fa-rss'
173
))->addClass('btn-primary');
174
175 77af9793 sbeaver
print $form;
176
177
/* Show the traceroute results */
178 cec703ec Phil Davis
if ($do_traceroute && $result) {
179 444381d1 sbeaver
?>
180
	<div class="panel panel-default">
181 cec703ec Phil Davis
		<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
182 444381d1 sbeaver
		<div class="panel-body">
183
<?php
184 cec703ec Phil Davis
	print('<pre>' . $result . '</pre>');
185 444381d1 sbeaver
?>
186
		</div>
187
	</div>
188
<?php
189 77af9793 sbeaver
}
190
191
include("foot.inc");
192
?>