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