1
|
<?php
|
2
|
/*
|
3
|
* diag_traceroute.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
|
9
|
* 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
|
* 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
|
*
|
20
|
* http://www.apache.org/licenses/LICENSE-2.0
|
21
|
*
|
22
|
* 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
|
*/
|
28
|
|
29
|
##|+PRIV
|
30
|
##|*IDENT=page-diagnostics-traceroute
|
31
|
##|*NAME=Diagnostics: Traceroute
|
32
|
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
|
33
|
##|*MATCH=diag_traceroute.php*
|
34
|
##|-PRIV
|
35
|
|
36
|
require_once("guiconfig.inc");
|
37
|
|
38
|
$allowautocomplete = true;
|
39
|
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
|
40
|
include("head.inc");
|
41
|
|
42
|
/* 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
|
define('MAX_TTL', 64);
|
46
|
define('DEFAULT_TTL', 18);
|
47
|
|
48
|
// Set defaults in case they are not supplied.
|
49
|
$do_traceroute = false;
|
50
|
$host = '';
|
51
|
$ttl = DEFAULT_TTL;
|
52
|
$ipproto = 'ipv4';
|
53
|
$sourceip = 'any';
|
54
|
|
55
|
if ($_POST || $_REQUEST['host']) {
|
56
|
unset($input_errors);
|
57
|
|
58
|
/* input validation */
|
59
|
$reqdfields = explode(" ", "host ttl");
|
60
|
$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
|
61
|
do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
|
62
|
|
63
|
if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
|
64
|
$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
|
65
|
}
|
66
|
$host = trim($_REQUEST['host']);
|
67
|
$ipproto = $_REQUEST['ipproto'];
|
68
|
if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
|
69
|
$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
|
70
|
}
|
71
|
if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
|
72
|
$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
|
73
|
}
|
74
|
|
75
|
$sourceip = $_REQUEST['sourceip'];
|
76
|
$ttl = $_REQUEST['ttl'];
|
77
|
$resolve = $_REQUEST['resolve'];
|
78
|
$useicmp = $_REQUEST['useicmp'];
|
79
|
|
80
|
if ($_POST && !$input_errors) {
|
81
|
$do_traceroute = true;
|
82
|
}
|
83
|
|
84
|
} else {
|
85
|
$resolve = false;
|
86
|
$useicmp = false;
|
87
|
}
|
88
|
|
89
|
if ($input_errors) {
|
90
|
print_input_errors($input_errors);
|
91
|
}
|
92
|
|
93
|
/* Do the traceroute and show any error */
|
94
|
if ($do_traceroute) {
|
95
|
$useicmpparam = isset($useicmp) ? "-I" : "";
|
96
|
$n = isset($resolve) ? "" : "-n";
|
97
|
|
98
|
$command = "/usr/sbin/traceroute";
|
99
|
if ($ipproto == "ipv6") {
|
100
|
$command .= "6";
|
101
|
if (empty($n)) {
|
102
|
$n = "-l";
|
103
|
}
|
104
|
$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
|
105
|
} else {
|
106
|
$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
|
107
|
}
|
108
|
|
109
|
if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
|
110
|
$srcip = "-s " . escapeshellarg($ifaddr);
|
111
|
}
|
112
|
|
113
|
$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmpparam} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
|
114
|
$result = shell_exec($cmd);
|
115
|
|
116
|
if (!$result) {
|
117
|
print_info_box(sprintf(gettext('Error: %s could not be traced/resolved'), htmlspecialchars($host)));
|
118
|
}
|
119
|
}
|
120
|
|
121
|
$form = new Form(false);
|
122
|
|
123
|
$section = new Form_Section('Traceroute');
|
124
|
|
125
|
$section->addInput(new Form_Input(
|
126
|
'host',
|
127
|
'*Hostname',
|
128
|
'text',
|
129
|
$host,
|
130
|
['placeholder' => 'Hostname to trace.']
|
131
|
));
|
132
|
|
133
|
$section->addInput(new Form_Select(
|
134
|
'ipproto',
|
135
|
'*IP Protocol',
|
136
|
$ipproto,
|
137
|
array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
|
138
|
))->setHelp('Select the protocol to use.');
|
139
|
|
140
|
$section->addInput(new Form_Select(
|
141
|
'sourceip',
|
142
|
'*Source Address',
|
143
|
$sourceip,
|
144
|
array('any' => gettext('Any')) + get_possible_traffic_source_addresses(true)
|
145
|
))->setHelp('Select source address for the trace.');
|
146
|
|
147
|
$section->addInput(new Form_Select(
|
148
|
'ttl',
|
149
|
'Maximum number of hops',
|
150
|
$ttl,
|
151
|
array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
|
152
|
))->setHelp('Select the maximum number of network hops to trace.');
|
153
|
|
154
|
$section->addInput(new Form_Checkbox(
|
155
|
'resolve',
|
156
|
'Reverse Address Lookup',
|
157
|
'',
|
158
|
$resolve
|
159
|
))->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.');
|
160
|
|
161
|
$section->addInput(new Form_Checkbox(
|
162
|
'useicmp',
|
163
|
gettext("Use ICMP"),
|
164
|
'',
|
165
|
$useicmp
|
166
|
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
|
167
|
|
168
|
$form->add($section);
|
169
|
|
170
|
$form->addGlobal(new Form_Button(
|
171
|
'Submit',
|
172
|
'Traceroute',
|
173
|
null,
|
174
|
'fa-rss'
|
175
|
))->addClass('btn-primary');
|
176
|
|
177
|
print $form;
|
178
|
|
179
|
/* Show the traceroute results */
|
180
|
if ($do_traceroute && $result) {
|
181
|
?>
|
182
|
<div class="panel panel-default">
|
183
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Results')?></h2></div>
|
184
|
<div class="panel-body">
|
185
|
<?php
|
186
|
print('<pre>' . $result . '</pre>');
|
187
|
?>
|
188
|
</div>
|
189
|
</div>
|
190
|
<?php
|
191
|
}
|
192
|
|
193
|
include("foot.inc");
|
194
|
?>
|