Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1 93e1b16c Scott Ullrich
<?php
2
/*
3
	diag_arp.php
4
*/
5 fd9ebcd5 Stephen Beaver
/* ====================================================================
6 0da0d43e Phil Davis
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7 9da2cf1c Stephen Beaver
 *
8 c9df279d Stephen Beaver
 *  Some or all of this file is based on the m0n0wall project which is
9 9da2cf1c Stephen Beaver
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10 fd9ebcd5 Stephen Beaver
 *
11 0da0d43e Phil Davis
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13 fd9ebcd5 Stephen Beaver
 *
14
 *  1. Redistributions of source code must retain the above copyright notice,
15
 *      this list of conditions and the following disclaimer.
16
 *
17
 *  2. Redistributions in binary form must reproduce the above copyright
18
 *      notice, this list of conditions and the following disclaimer in
19
 *      the documentation and/or other materials provided with the
20 0da0d43e Phil Davis
 *      distribution.
21 fd9ebcd5 Stephen Beaver
 *
22 0da0d43e Phil Davis
 *  3. All advertising materials mentioning features or use of this software
23 fd9ebcd5 Stephen Beaver
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25 0da0d43e Phil Davis
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26 fd9ebcd5 Stephen Beaver
 *
27
 *  4. The names "pfSense" and "pfSense Project" must not be used to
28
 *       endorse or promote products derived from this software without
29
 *       prior written permission. For written permission, please contact
30
 *       coreteam@pfsense.org.
31
 *
32
 *  5. Products derived from this software may not be called "pfSense"
33
 *      nor may "pfSense" appear in their names without prior written
34
 *      permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *  6. Redistributions of any form whatsoever must retain the following
37
 *      acknowledgment:
38
 *
39
 *  "This product includes software developed by the pfSense Project
40
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
41 0da0d43e Phil Davis
 *
42 fd9ebcd5 Stephen Beaver
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *  ====================================================================
56
 *
57
 */
58 93e1b16c Scott Ullrich
59 6b07c15a Matthew Grooms
##|+PRIV
60
##|*IDENT=page-diagnostics-arptable
61 5230f468 jim-p
##|*NAME=Diagnostics: ARP Table
62 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
63
##|*MATCH=diag_arp.php*
64
##|-PRIV
65
66 868ba36d Scott Ullrich
@ini_set('zlib.output_compression', 0);
67
@ini_set('implicit_flush', 1);
68 6b07c15a Matthew Grooms
69 93e1b16c Scott Ullrich
require("guiconfig.inc");
70 6090e85b Bill Marquette
71 17e3a05a stilez
// delete arp entry
72
if (isset($_GET['deleteentry'])) {
73
	$ip = $_GET['deleteentry'];
74
	if (is_ipaddrv4($ip)) {
75
		$ret = mwexec("arp -d " . $_GET['deleteentry'], true);
76
	} else {
77
		$ret = 1;
78
	}
79
	if ($ret) {
80
		$savemsg = sprintf(gettext("%s is not a valid IPv4 address or could not be deleted."), $ip);
81
		$savemsgtype = 'alert-warning';
82
	} else {
83
		$savemsg = sprintf(gettext("The ARP cache entry for %s has been deleted."), $ip);
84
		$savemsgtype = 'success';
85
	}
86
}
87
88 fc259334 Seth Mos
function leasecmp($a, $b) {
89
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
90
}
91 93e1b16c Scott Ullrich
92 fc259334 Seth Mos
function adjust_gmt($dt) {
93
	$ts = strtotime($dt . " GMT");
94
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
95
}
96 93e1b16c Scott Ullrich
97 fc259334 Seth Mos
function remove_duplicate($array, $field) {
98 5f601060 Phil Davis
	foreach ($array as $sub) {
99 20b9b335 Scott Ullrich
		$cmp[] = $sub[$field];
100 5f601060 Phil Davis
	}
101 fc259334 Seth Mos
	$unique = array_unique($cmp);
102 5f601060 Phil Davis
	foreach ($unique as $k => $rien) {
103 fc259334 Seth Mos
		$new[] = $array[$k];
104 5f601060 Phil Davis
	}
105 fc259334 Seth Mos
	return $new;
106
}
107 93e1b16c Scott Ullrich
108 20b9b335 Scott Ullrich
// Define path to AWK
109 fc259334 Seth Mos
$awk = "/usr/bin/awk";
110 20b9b335 Scott Ullrich
111
// Read in leases file
112
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
113
114 fc259334 Seth Mos
/* this pattern sticks comments into a single array item */
115
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
116 20b9b335 Scott Ullrich
117 fc259334 Seth Mos
/* We then split the leases file by } */
118
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
119 93e1b16c Scott Ullrich
120 5f601060 Phil Davis
/* stuff the leases file in a proper format into an array by line */
121 fc259334 Seth Mos
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
122
$leases_count = count($leases_content);
123
124
$pools = array();
125
$leases = array();
126
$i = 0;
127
$l = 0;
128
$p = 0;
129
// Put everything together again
130 5f601060 Phil Davis
while ($i < $leases_count) {
131 b45630bf jim-p
	/* split the line by space */
132
	$data = explode(" ", $leases_content[$i]);
133
	/* walk the fields */
134
	$f = 0;
135
	$fcount = count($data);
136
	/* with less then 20 fields there is nothing useful */
137 5f601060 Phil Davis
	if ($fcount < 20) {
138 b45630bf jim-p
		$i++;
139
		continue;
140
	}
141 5f601060 Phil Davis
	while ($f < $fcount) {
142
		switch ($data[$f]) {
143 b45630bf jim-p
			case "failover":
144
				$pools[$p]['name'] = $data[$f+2];
145
				$pools[$p]['mystate'] = $data[$f+7];
146
				$pools[$p]['peerstate'] = $data[$f+14];
147
				$pools[$p]['mydate'] = $data[$f+10];
148
				$pools[$p]['mydate'] .= " " . $data[$f+11];
149
				$pools[$p]['peerdate'] = $data[$f+17];
150
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
151
				$p++;
152
				$i++;
153
				continue 3;
154
			case "lease":
155
				$leases[$l]['ip'] = $data[$f+1];
156
				$leases[$l]['type'] = "dynamic";
157
				$f = $f+2;
158
				break;
159
			case "starts":
160
				$leases[$l]['start'] = $data[$f+2];
161
				$leases[$l]['start'] .= " " . $data[$f+3];
162
				$f = $f+3;
163
				break;
164
			case "ends":
165
				$leases[$l]['end'] = $data[$f+2];
166
				$leases[$l]['end'] .= " " . $data[$f+3];
167
				$f = $f+3;
168
				break;
169
			case "tstp":
170
				$f = $f+3;
171
				break;
172
			case "tsfp":
173
				$f = $f+3;
174
				break;
175
			case "atsfp":
176
				$f = $f+3;
177
				break;
178
			case "cltt":
179
				$f = $f+3;
180
				break;
181
			case "binding":
182 699737d9 Phil Davis
				switch ($data[$f+2]) {
183 b45630bf jim-p
					case "active":
184
						$leases[$l]['act'] = "active";
185
						break;
186
					case "free":
187
						$leases[$l]['act'] = "expired";
188
						$leases[$l]['online'] = "offline";
189
						break;
190
					case "backup":
191
						$leases[$l]['act'] = "reserved";
192
						$leases[$l]['online'] = "offline";
193
						break;
194
				}
195
				$f = $f+1;
196
				break;
197
			case "next":
198
				/* skip the next binding statement */
199
				$f = $f+3;
200
				break;
201 f1273b82 jim-p
			case "rewind":
202
				/* skip the rewind binding statement */
203
				$f = $f+3;
204
				break;
205 b45630bf jim-p
			case "hardware":
206
				$leases[$l]['mac'] = $data[$f+2];
207
				/* check if it's online and the lease is active */
208 5f601060 Phil Davis
				if ($leases[$l]['act'] == "active") {
209 b45630bf jim-p
					$online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
210
					if ($online == 1) {
211
						$leases[$l]['online'] = 'online';
212
					} else {
213
						$leases[$l]['online'] = 'offline';
214
					}
215
				}
216
				$f = $f+2;
217
				break;
218
			case "client-hostname":
219 5f601060 Phil Davis
				if ($data[$f+1] <> "") {
220 699737d9 Phil Davis
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
221 b45630bf jim-p
				} else {
222
					$hostname = gethostbyaddr($leases[$l]['ip']);
223 5f601060 Phil Davis
					if ($hostname <> "") {
224 b45630bf jim-p
						$leases[$l]['hostname'] = $hostname;
225
					}
226
				}
227
				$f = $f+1;
228
				break;
229
			case "uid":
230
				$f = $f+1;
231
				break;
232
		}
233
		$f++;
234
	}
235
	$l++;
236
	$i++;
237 fc259334 Seth Mos
}
238
239
/* remove duplicate items by mac address */
240 5f601060 Phil Davis
if (count($leases) > 0) {
241 699737d9 Phil Davis
	$leases = remove_duplicate($leases, "ip");
242 fc259334 Seth Mos
}
243
244 5f601060 Phil Davis
if (count($pools) > 0) {
245 699737d9 Phil Davis
	$pools = remove_duplicate($pools, "name");
246 b45630bf jim-p
	asort($pools);
247 fc259334 Seth Mos
}
248
249
// Put this in an easy to use form
250
$dhcpmac = array();
251
$dhcpip = array();
252 f8ec8de4 Renato Botelho
253 fc259334 Seth Mos
foreach ($leases as $value) {
254 f8ec8de4 Renato Botelho
	$dhcpmac[$value['mac']] = $value['hostname'];
255
	$dhcpip[$value['ip']] = $value['hostname'];
256 93e1b16c Scott Ullrich
}
257
258 699737d9 Phil Davis
exec("/usr/sbin/arp -an", $rawdata);
259 93e1b16c Scott Ullrich
260 f8ec8de4 Renato Botelho
$i = 0;
261 cbe3ea96 Ermal Luçi
262
/* if list */
263
$ifdescrs = get_configured_interface_with_descr();
264 93e1b16c Scott Ullrich
265 e7237dd0 jim-p
foreach ($ifdescrs as $key => $interface) {
266
	$thisif = convert_friendly_interface_to_real_interface_name($key);
267 5f601060 Phil Davis
	if (!empty($thisif)) {
268 e7237dd0 jim-p
		$hwif[$thisif] = $interface;
269 5f601060 Phil Davis
	}
270 93e1b16c Scott Ullrich
}
271
272
$data = array();
273
foreach ($rawdata as $line) {
274 699737d9 Phil Davis
	$elements = explode(' ', $line);
275 f8ec8de4 Renato Botelho
276 93e1b16c Scott Ullrich
	if ($elements[3] != "(incomplete)") {
277
		$arpent = array();
278 699737d9 Phil Davis
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
279 93e1b16c Scott Ullrich
		$arpent['mac'] = trim($elements[3]);
280
		$arpent['interface'] = trim($elements[5]);
281
		$data[] = $arpent;
282
	}
283
}
284
285 699737d9 Phil Davis
function _getHostName($mac, $ip) {
286 93e1b16c Scott Ullrich
	global $dhcpmac, $dhcpip;
287 f8ec8de4 Renato Botelho
288 5f601060 Phil Davis
	if ($dhcpmac[$mac]) {
289 93e1b16c Scott Ullrich
		return $dhcpmac[$mac];
290 5f601060 Phil Davis
	} else if ($dhcpip[$ip]) {
291 93e1b16c Scott Ullrich
		return $dhcpip[$ip];
292 5f601060 Phil Davis
	} else {
293 d31ca336 Renato Botelho
		exec("host -W 1 " . escapeshellarg($ip), $output);
294 699737d9 Phil Davis
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
295 5f601060 Phil Davis
			if ($matches[1] <> $ip) {
296 7d5b007c Sjon Hortensius
				return $matches[1];
297 5f601060 Phil Davis
			}
298 dd4bded7 Evgeny Yurchenko
		}
299
	}
300
	return "";
301 93e1b16c Scott Ullrich
}
302
303 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
304 fc259334 Seth Mos
include("head.inc");
305 20b9b335 Scott Ullrich
306 17e3a05a stilez
// Handle save msg if defined
307
if ($savemsg) {
308
	print_info_box(htmlentities($savemsg), $savemsgtype);
309
}
310 93e1b16c Scott Ullrich
?>
311 20b9b335 Scott Ullrich
312 a4af095c Renato Botelho
<!-- On modern hardware the table will load so fast you may never see this! -->
313 4a993c8f Scott Ullrich
<div id="loading">
314 a4af095c Renato Botelho
	<?= gettext(" Loading, please wait...")?>
315 4a993c8f Scott Ullrich
</div>
316
317
<?php
318
319 868ba36d Scott Ullrich
// Flush buffers out to client so that they see Loading, please wait....
320 5f601060 Phil Davis
for ($i = 0; $i < ob_get_level(); $i++) {
321
	ob_end_flush();
322
}
323 608947a8 Stephen Beaver
324 868ba36d Scott Ullrich
ob_implicit_flush(1);
325
326 20b9b335 Scott Ullrich
// Resolve hostnames and replace Z_ with "".  The intention
327
// is to sort the list by hostnames, alpha and then the non
328
// resolvable addresses will appear last in the list.
329 dd4bded7 Evgeny Yurchenko
$dnsavailable=1;
330 7d5b007c Sjon Hortensius
$dns = trim(_getHostName("", "8.8.8.8"));
331 5f601060 Phil Davis
if ($dns == "") {
332 7d5b007c Sjon Hortensius
	$dns = trim(_getHostName("", "8.8.4.4"));
333 5f601060 Phil Davis
	if ($dns == "") {
334
		$dnsavailable = 0;
335
	}
336 dd4bded7 Evgeny Yurchenko
}
337
338 4a993c8f Scott Ullrich
foreach ($data as &$entry) {
339 5f601060 Phil Davis
	if ($dnsavailable) {
340 dd4bded7 Evgeny Yurchenko
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
341 5f601060 Phil Davis
	} else {
342 dd4bded7 Evgeny Yurchenko
		$dns="";
343 5f601060 Phil Davis
	}
344
	if (trim($dns)) {
345 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "$dns";
346 5f601060 Phil Davis
	} else {
347 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "Z_ ";
348 5f601060 Phil Davis
	}
349 4a993c8f Scott Ullrich
}
350 8ed2d200 NewEraCracker
unset($entry);
351 20b9b335 Scott Ullrich
352
// Sort the data alpha first
353 4a993c8f Scott Ullrich
$data = msort($data, "dnsresolve");
354
355 57f2840e Evgeny
// Load MAC-Manufacturer table
356
$mac_man = load_mac_manufacturer_table();
357 4a993c8f Scott Ullrich
?>
358 ac950976 Colin Fleming
<div class="panel panel-default">
359
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('ARP Table')?></h2></div>
360
	<div class="panel-body">
361
362 89f64f0f Sander van Leeuwen
<div class="table-responsive">
363 608947a8 Stephen Beaver
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
364 89f64f0f Sander van Leeuwen
		<thead>
365
			<tr>
366
				<th><?= gettext("Interface")?></th>
367
				<th><?= gettext("IP address")?></th>
368
				<th><?= gettext("MAC address")?></th>
369
				<th><?= gettext("Hostname")?></th>
370 17e3a05a stilez
				<th data-sortable="false"><?=gettext("Actions")?></th>
371 89f64f0f Sander van Leeuwen
			</tr>
372
		</thead>
373
		<tbody>
374 947141fd Phil Davis
375 82afb104 Stephen Beaver
<?php
376
		foreach ($data as $entry): ?>
377 89f64f0f Sander van Leeuwen
			<tr>
378
				<td><?=$hwif[$entry['interface']]?></td>
379
				<td><?=$entry['ip']?></td>
380
				<td>
381
					<?=trim($entry['mac'])?>
382
				<?php
383
					$mac = trim($entry['mac']);
384
					$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
385
386 cba48c64 Colin Fleming
					if (isset($mac_man[$mac_hi])) {
387 89f64f0f Sander van Leeuwen
						print '<small>('. $mac_man[$mac_hi] .')</small>';
388 cba48c64 Colin Fleming
					}
389 89f64f0f Sander van Leeuwen
	?>
390
				</td>
391
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
392 17e3a05a stilez
				<td>
393
					<a class="fa fa-trash" title="<?=gettext('Delete arp cache entry')?>"	href="diag_arp.php?deleteentry=<?=$entry['ip']?>"></a>
394
				</td>
395 89f64f0f Sander van Leeuwen
			</tr>
396
		<?php endforeach?>
397
		</tbody>
398
	</table>
399
</div>
400
401 ac950976 Colin Fleming
	</div>
402
</div>
403
404 cba48c64 Colin Fleming
<script type="text/javascript">
405 293ceb87 Colin Fleming
//<![CDATA[
406 a4af095c Renato Botelho
// Clear the "loading" div once the page has loaded"
407 947141fd Phil Davis
events.push(function() {
408 a4af095c Renato Botelho
	$('#loading').empty();
409
});
410 0da0d43e Phil Davis
//]]>
411 4a993c8f Scott Ullrich
</script>
412 a4af095c Renato Botelho
413 c40962e9 Phil Davis
<div class="infoblock blockopen">
414 0da0d43e Phil Davis
<?php
415 c6d73876 NOYB
print_info_box(gettext("Local IPv6 peers use ") . '<a href="diag_ndp.php">' . gettext("NDP") . '</a>' . gettext(" instead of ARP."), 'info', false);
416 c40962e9 Phil Davis
?>
417
</div>
418 93e1b16c Scott Ullrich
419 c40962e9 Phil Davis
<?php
420
include("foot.inc");
421
?>