Project

General

Profile

Download (6.83 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_traceroute.php
4
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 0da0d43e Phil Davis
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 fd9ebcd5 Stephen Beaver
 *  Copyright (c)  2005 Paul Taylor (paultaylor@winndixie.com) and Manuel Kasper <mk@neon1.net>
8 9da2cf1c Stephen Beaver
 *
9 c9df279d Stephen Beaver
 *  Some or all of this file is based on the m0n0wall project which is
10 9da2cf1c Stephen Beaver
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
11 fd9ebcd5 Stephen Beaver
 *
12 0da0d43e Phil Davis
 *  Redistribution and use in source and binary forms, with or without modification,
13
 *  are permitted provided that the following conditions are met:
14 fd9ebcd5 Stephen Beaver
 *
15
 *  1. Redistributions of source code must retain the above copyright notice,
16
 *      this list of conditions and the following disclaimer.
17
 *
18
 *  2. Redistributions in binary form must reproduce the above copyright
19
 *      notice, this list of conditions and the following disclaimer in
20
 *      the documentation and/or other materials provided with the
21 0da0d43e Phil Davis
 *      distribution.
22 fd9ebcd5 Stephen Beaver
 *
23 0da0d43e Phil Davis
 *  3. All advertising materials mentioning features or use of this software
24 fd9ebcd5 Stephen Beaver
 *      must display the following acknowledgment:
25
 *      "This product includes software developed by the pfSense Project
26 0da0d43e Phil Davis
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
27 fd9ebcd5 Stephen Beaver
 *
28
 *  4. The names "pfSense" and "pfSense Project" must not be used to
29
 *       endorse or promote products derived from this software without
30
 *       prior written permission. For written permission, please contact
31
 *       coreteam@pfsense.org.
32
 *
33
 *  5. Products derived from this software may not be called "pfSense"
34
 *      nor may "pfSense" appear in their names without prior written
35
 *      permission of the Electric Sheep Fencing, LLC.
36
 *
37
 *  6. Redistributions of any form whatsoever must retain the following
38
 *      acknowledgment:
39
 *
40
 *  "This product includes software developed by the pfSense Project
41
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
42 0da0d43e Phil Davis
 *
43 fd9ebcd5 Stephen Beaver
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
44
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
47
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
55
 *
56
 *  ====================================================================
57
 *
58
 */
59 93e1b16c Scott Ullrich
60 6b07c15a Matthew Grooms
##|+PRIV
61
##|*IDENT=page-diagnostics-traceroute
62 5230f468 jim-p
##|*NAME=Diagnostics: Traceroute
63 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: Traceroute' page.
64
##|*MATCH=diag_traceroute.php*
65
##|-PRIV
66
67 b8a66425 Scott Ullrich
require("guiconfig.inc");
68 13d193c2 Scott Ullrich
69 643bc7b0 jim-p
$allowautocomplete = true;
70 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("Traceroute"));
71 b8a66425 Scott Ullrich
include("head.inc");
72 444381d1 sbeaver
73 93e1b16c Scott Ullrich
define('MAX_TTL', 64);
74
define('DEFAULT_TTL', 18);
75
76 ed69a496 jim-p
$do_traceroute = false;
77
$host = '';
78
$ttl = DEFAULT_TTL;
79 77af9793 sbeaver
$pconfig['ttl'] = DEFAULT_TTL;
80
$pconfig['ipproto'] = 'IPv4';
81
$pconfig['sourceip'] = 'Any';
82
83
function create_sourceaddresslist() {
84
	$list = array('any' => 'Any');
85
86
	$sourceips = get_possible_traffic_source_addresses(true);
87
88 947141fd Phil Davis
	foreach ($sourceips as $sipvalue => $sipname) {
89 0da0d43e Phil Davis
		$list[$sipvalue] = $sipname;
90 947141fd Phil Davis
	}
91 77af9793 sbeaver
92
	return($list);
93
}
94
95 b8cc74ed sullrich
if ($_POST || $_REQUEST['host']) {
96 93e1b16c Scott Ullrich
	unset($input_errors);
97
	unset($do_traceroute);
98
99
	/* input validation */
100
	$reqdfields = explode(" ", "host ttl");
101 699737d9 Phil Davis
	$reqdfieldsn = array(gettext("Host"), gettext("ttl"));
102 1e9b4611 Renato Botelho
	do_input_validation($_REQUEST, $reqdfields, $reqdfieldsn, $input_errors);
103 444381d1 sbeaver
104 b8cc74ed sullrich
	if (($_REQUEST['ttl'] < 1) || ($_REQUEST['ttl'] > MAX_TTL)) {
105 ea6be4a7 Erik Fonnesbeck
		$input_errors[] = sprintf(gettext("Maximum number of hops must be between 1 and %s"), MAX_TTL);
106 93e1b16c Scott Ullrich
	}
107 643bc7b0 jim-p
	$host = trim($_REQUEST['host']);
108
	$ipproto = $_REQUEST['ipproto'];
109 5f601060 Phil Davis
	if (($ipproto == "ipv4") && is_ipaddrv6($host)) {
110 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv4, the target host must be an IPv4 address or hostname.");
111 5f601060 Phil Davis
	}
112
	if (($ipproto == "ipv6") && is_ipaddrv4($host)) {
113 643bc7b0 jim-p
		$input_errors[] = gettext("When using IPv6, the target host must be an IPv6 address or hostname.");
114 5f601060 Phil Davis
	}
115 93e1b16c Scott Ullrich
116 ed69a496 jim-p
	if (!$input_errors) {
117 444381d1 sbeaver
		$host = $_REQUEST['host'];
118 ed69a496 jim-p
	}
119 444381d1 sbeaver
120 77af9793 sbeaver
	$sourceip = $_REQUEST['sourceip'];
121
	$ttl = $_REQUEST['ttl'];
122
	$resolve = $_REQUEST['resolve'];
123
	$useicmp = $_REQUEST['useicmp'];
124
125 ed69a496 jim-p
	if ($_POST) {
126
		$do_traceroute = true;
127
	}
128
129 77af9793 sbeaver
} else {
130
	$resolve = false;
131 444381d1 sbeaver
	$useicmp = false;
132 77af9793 sbeaver
}
133 99e991fd Warren Baker
134 947141fd Phil Davis
if ($input_errors) {
135 77af9793 sbeaver
	print_input_errors($input_errors);
136 947141fd Phil Davis
}
137 444381d1 sbeaver
138 38e06c66 Sjon Hortensius
$form = new Form('Traceroute');
139 77af9793 sbeaver
140
$section = new Form_Section('Traceroute');
141
142
$section->addInput(new Form_Input(
143
	'host',
144
	'Hostname',
145
	'text',
146
	$host,
147
	['placeholder' => 'Hostname to trace.']
148
));
149
150
$section->addInput(new Form_Select(
151
	'ipproto',
152
	'IP Protocol',
153 0da0d43e Phil Davis
	$pconfig['ipproto'],
154 77af9793 sbeaver
	array('ipv4' => 'IPv4', 'ipv6' => 'IPv6')
155
))->setHelp('Select the protocol to use');
156
157
$section->addInput(new Form_Select(
158
	'sourceip',
159
	'Source Address',
160 0da0d43e Phil Davis
	$pconfig['sourceip'],
161 77af9793 sbeaver
	create_sourceaddresslist()
162
))->setHelp('Select source address for the trace');
163
164
$section->addInput(new Form_Select(
165
	'ttl',
166
	'Maximum nuber of hops',
167
	$ttl,
168 cd7d902d sbeaver
	array_combine(range(1, MAX_TTL), range(1, MAX_TTL))
169 77af9793 sbeaver
))->setHelp('Select the maximum number of network hops to trace');
170
171
$section->addInput(new Form_Checkbox(
172
	'resolve',
173
	'Reverse Address Lookup',
174
	'',
175
	$resolve
176 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.');
177 77af9793 sbeaver
178
$section->addInput(new Form_Checkbox(
179
	'useicmp',
180
	gettext("Use ICMP"),
181
	'',
182
	$useicmp
183
))->setHelp('By default, traceroute uses UDP but that may be blocked by some routers. Check this box to use ICMP instead, which may succeed. ');
184
185
$form->add($section);
186
print $form;
187
188
/* Show the traceroute results */
189
if (!$input_errors && $do_traceroute) {
190
191 643bc7b0 jim-p
	$useicmp = isset($_REQUEST['useicmp']) ? "-I" : "";
192
	$n = isset($resolve) ? "" : "-n";
193
194 288a2a0f Phil Davis
	$command = "/usr/sbin/traceroute";
195
	if ($ipproto == "ipv6") {
196
		$command .= "6";
197
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ipv6($sourceip);
198
	} else {
199
		$ifaddr = is_ipaddr($sourceip) ? $sourceip : get_interface_ip($sourceip);
200
	}
201
202
	if ($ifaddr && (is_ipaddr($host) || is_hostname($host))) {
203
		$srcip = "-s " . escapeshellarg($ifaddr);
204
	}
205 643bc7b0 jim-p
206 f09c5600 jim-p
	$cmd = "{$command} {$n} {$srcip} -w 2 {$useicmp} -m " . escapeshellarg($ttl) . " " . escapeshellarg($host);
207 444381d1 sbeaver
?>
208
	<div class="panel panel-default">
209 f17594c7 Sjon Hortensius
		<div class="panel-heading"><h2 class="panel-title">Results</h2></div>
210 444381d1 sbeaver
		<div class="panel-body">
211
<?php
212 947141fd Phil Davis
		if ($result = shell_exec($cmd)) {
213 444381d1 sbeaver
			print(nl2br($result));
214 947141fd Phil Davis
		} else {
215 444381d1 sbeaver
			print('Error: ' . $host . ' ' . gettext("could not be traced/resolved"));
216 947141fd Phil Davis
		}
217 444381d1 sbeaver
?>
218
		</div>
219
	</div>
220
<?php
221 77af9793 sbeaver
}
222
223
include("foot.inc");
224
?>