Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_arp.php
4
*/
5
/* ====================================================================
6
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *  Some or all of this file is based on the m0n0wall project which is
9
 *  Copyright (c)  2004 Manuel Kasper (BSD 2 clause)
10
 *
11
 *  Redistribution and use in source and binary forms, with or without modification,
12
 *  are permitted provided that the following conditions are met:
13
 *
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
 *      distribution.
21
 *
22
 *  3. All advertising materials mentioning features or use of this software
23
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25
 *       for use in the pfSense software distribution. (http://www.pfsense.org/).
26
 *
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
 *
42
 *  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

    
59
##|+PRIV
60
##|*IDENT=page-diagnostics-arptable
61
##|*NAME=Diagnostics: ARP Table
62
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
63
##|*MATCH=diag_arp.php*
64
##|-PRIV
65

    
66
@ini_set('zlib.output_compression', 0);
67
@ini_set('implicit_flush', 1);
68

    
69
require_once("guiconfig.inc");
70

    
71
// 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
function leasecmp($a, $b) {
89
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
90
}
91

    
92
function adjust_gmt($dt) {
93
	$ts = strtotime($dt . " GMT");
94
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
95
}
96

    
97
function remove_duplicate($array, $field) {
98
	foreach ($array as $sub) {
99
		$cmp[] = $sub[$field];
100
	}
101
	$unique = array_unique($cmp);
102
	foreach ($unique as $k => $rien) {
103
		$new[] = $array[$k];
104
	}
105
	return $new;
106
}
107

    
108
// Define path to AWK
109
$awk = "/usr/bin/awk";
110

    
111
// Read in leases file
112
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
113

    
114
/* this pattern sticks comments into a single array item */
115
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
116

    
117
/* We then split the leases file by } */
118
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
119

    
120
/* stuff the leases file in a proper format into an array by line */
121
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
while ($i < $leases_count) {
131
	/* 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
	if ($fcount < 20) {
138
		$i++;
139
		continue;
140
	}
141
	while ($f < $fcount) {
142
		switch ($data[$f]) {
143
			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
				switch ($data[$f+2]) {
183
					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
			case "rewind":
202
				/* skip the rewind binding statement */
203
				$f = $f+3;
204
				break;
205
			case "hardware":
206
				$leases[$l]['mac'] = $data[$f+2];
207
				/* check if it's online and the lease is active */
208
				if ($leases[$l]['act'] == "active") {
209
					$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
				if ($data[$f+1] <> "") {
220
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
221
				} else {
222
					$hostname = gethostbyaddr($leases[$l]['ip']);
223
					if ($hostname <> "") {
224
						$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
}
238

    
239
/* remove duplicate items by mac address */
240
if (count($leases) > 0) {
241
	$leases = remove_duplicate($leases, "ip");
242
}
243

    
244
if (count($pools) > 0) {
245
	$pools = remove_duplicate($pools, "name");
246
	asort($pools);
247
}
248

    
249
// Put this in an easy to use form
250
$dhcpmac = array();
251
$dhcpip = array();
252

    
253
foreach ($leases as $value) {
254
	$dhcpmac[$value['mac']] = $value['hostname'];
255
	$dhcpip[$value['ip']] = $value['hostname'];
256
}
257

    
258
exec("/usr/sbin/arp -an", $rawdata);
259

    
260
$i = 0;
261

    
262
/* if list */
263
$ifdescrs = get_configured_interface_with_descr();
264

    
265
foreach ($ifdescrs as $key => $interface) {
266
	$thisif = convert_friendly_interface_to_real_interface_name($key);
267
	if (!empty($thisif)) {
268
		$hwif[$thisif] = $interface;
269
	}
270
}
271

    
272
$data = array();
273
foreach ($rawdata as $line) {
274
	$elements = explode(' ', $line);
275

    
276
	if ($elements[3] != "(incomplete)") {
277
		$arpent = array();
278
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
279
		$arpent['mac'] = trim($elements[3]);
280
		$arpent['interface'] = trim($elements[5]);
281
		$data[] = $arpent;
282
	}
283
}
284

    
285
function _getHostName($mac, $ip) {
286
	global $dhcpmac, $dhcpip;
287

    
288
	if ($dhcpmac[$mac]) {
289
		return $dhcpmac[$mac];
290
	} else if ($dhcpip[$ip]) {
291
		return $dhcpip[$ip];
292
	} else {
293
		exec("host -W 1 " . escapeshellarg($ip), $output);
294
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
295
			if ($matches[1] <> $ip) {
296
				return $matches[1];
297
			}
298
		}
299
	}
300
	return "";
301
}
302

    
303
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
304
include("head.inc");
305

    
306
// Handle save msg if defined
307
if ($savemsg) {
308
	print_info_box(htmlentities($savemsg), $savemsgtype);
309
}
310
?>
311

    
312
<!-- On modern hardware the table will load so fast you may never see this! -->
313
<div id="loading">
314
	<?= gettext(" Loading, please wait...")?>
315
</div>
316

    
317
<?php
318

    
319
// Flush buffers out to client so that they see Loading, please wait....
320
for ($i = 0; $i < ob_get_level(); $i++) {
321
	ob_end_flush();
322
}
323

    
324
ob_implicit_flush(1);
325

    
326
// 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
$dnsavailable=1;
330
$dns = trim(_getHostName("", "8.8.8.8"));
331
if ($dns == "") {
332
	$dns = trim(_getHostName("", "8.8.4.4"));
333
	if ($dns == "") {
334
		$dnsavailable = 0;
335
	}
336
}
337

    
338
foreach ($data as &$entry) {
339
	if ($dnsavailable) {
340
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
341
	} else {
342
		$dns="";
343
	}
344
	if (trim($dns)) {
345
		$entry['dnsresolve'] = "$dns";
346
	} else {
347
		$entry['dnsresolve'] = "Z_ ";
348
	}
349
}
350
unset($entry);
351

    
352
// Sort the data alpha first
353
$data = msort($data, "dnsresolve");
354

    
355
// Load MAC-Manufacturer table
356
$mac_man = load_mac_manufacturer_table();
357
?>
358
<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
<div class="table-responsive">
363
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
364
		<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
				<th data-sortable="false"><?=gettext("Actions")?></th>
371
			</tr>
372
		</thead>
373
		<tbody>
374

    
375
<?php
376
		foreach ($data as $entry): ?>
377
			<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
					if (isset($mac_man[$mac_hi])) {
387
						print '<small>('. $mac_man[$mac_hi] .')</small>';
388
					}
389
	?>
390
				</td>
391
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
392
				<td>
393
					<a class="fa fa-trash" title="<?=gettext('Delete arp cache entry')?>"	href="diag_arp.php?deleteentry=<?=$entry['ip']?>"></a>
394
				</td>
395
			</tr>
396
		<?php endforeach?>
397
		</tbody>
398
	</table>
399
</div>
400

    
401
	</div>
402
</div>
403

    
404
<script type="text/javascript">
405
//<![CDATA[
406
// Clear the "loading" div once the page has loaded"
407
events.push(function() {
408
	$('#loading').empty();
409
});
410
//]]>
411
</script>
412

    
413
<div class="infoblock blockopen">
414
<?php
415
print_info_box(gettext("Local IPv6 peers use ") . '<a href="diag_ndp.php">' . gettext("NDP") . '</a>' . gettext(" instead of ARP."), 'info', false);
416
?>
417
</div>
418

    
419
<?php
420
include("foot.inc");
421
?>
(5-5/225)