Project

General

Profile

Download (10.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 13d193c2 Scott Ullrich
/*
60
	pfSense_BUILDER_BINARIES:	/bin/cat		/usr/sbin/arp
61
	pfSense_MODULE:	arp
62
*/
63
64 6b07c15a Matthew Grooms
##|+PRIV
65
##|*IDENT=page-diagnostics-arptable
66
##|*NAME=Diagnostics: ARP Table page
67
##|*DESCR=Allow access to the 'Diagnostics: ARP Table' page.
68
##|*MATCH=diag_arp.php*
69
##|-PRIV
70
71 868ba36d Scott Ullrich
@ini_set('zlib.output_compression', 0);
72
@ini_set('implicit_flush', 1);
73 6b07c15a Matthew Grooms
74 93e1b16c Scott Ullrich
require("guiconfig.inc");
75 6090e85b Bill Marquette
76 fc259334 Seth Mos
function leasecmp($a, $b) {
77
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
78
}
79 93e1b16c Scott Ullrich
80 fc259334 Seth Mos
function adjust_gmt($dt) {
81
	$ts = strtotime($dt . " GMT");
82
	return strftime("%Y/%m/%d %H:%M:%S", $ts);
83
}
84 93e1b16c Scott Ullrich
85 fc259334 Seth Mos
function remove_duplicate($array, $field) {
86 5f601060 Phil Davis
	foreach ($array as $sub) {
87 20b9b335 Scott Ullrich
		$cmp[] = $sub[$field];
88 5f601060 Phil Davis
	}
89 fc259334 Seth Mos
	$unique = array_unique($cmp);
90 5f601060 Phil Davis
	foreach ($unique as $k => $rien) {
91 fc259334 Seth Mos
		$new[] = $array[$k];
92 5f601060 Phil Davis
	}
93 fc259334 Seth Mos
	return $new;
94
}
95 93e1b16c Scott Ullrich
96 20b9b335 Scott Ullrich
// Define path to AWK
97 fc259334 Seth Mos
$awk = "/usr/bin/awk";
98 20b9b335 Scott Ullrich
99
// Read in leases file
100
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases";
101
102 fc259334 Seth Mos
/* this pattern sticks comments into a single array item */
103
$cleanpattern = "'{ gsub(\"#.*\", \"\");} { gsub(\";\", \"\"); print;}'";
104 20b9b335 Scott Ullrich
105 fc259334 Seth Mos
/* We then split the leases file by } */
106
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
107 93e1b16c Scott Ullrich
108 5f601060 Phil Davis
/* stuff the leases file in a proper format into an array by line */
109 fc259334 Seth Mos
exec("cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern}", $leases_content);
110
$leases_count = count($leases_content);
111
112
$pools = array();
113
$leases = array();
114
$i = 0;
115
$l = 0;
116
$p = 0;
117
// Put everything together again
118 5f601060 Phil Davis
while ($i < $leases_count) {
119 b45630bf jim-p
	/* split the line by space */
120
	$data = explode(" ", $leases_content[$i]);
121
	/* walk the fields */
122
	$f = 0;
123
	$fcount = count($data);
124
	/* with less then 20 fields there is nothing useful */
125 5f601060 Phil Davis
	if ($fcount < 20) {
126 b45630bf jim-p
		$i++;
127
		continue;
128
	}
129 5f601060 Phil Davis
	while ($f < $fcount) {
130
		switch ($data[$f]) {
131 b45630bf jim-p
			case "failover":
132
				$pools[$p]['name'] = $data[$f+2];
133
				$pools[$p]['mystate'] = $data[$f+7];
134
				$pools[$p]['peerstate'] = $data[$f+14];
135
				$pools[$p]['mydate'] = $data[$f+10];
136
				$pools[$p]['mydate'] .= " " . $data[$f+11];
137
				$pools[$p]['peerdate'] = $data[$f+17];
138
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
139
				$p++;
140
				$i++;
141
				continue 3;
142
			case "lease":
143
				$leases[$l]['ip'] = $data[$f+1];
144
				$leases[$l]['type'] = "dynamic";
145
				$f = $f+2;
146
				break;
147
			case "starts":
148
				$leases[$l]['start'] = $data[$f+2];
149
				$leases[$l]['start'] .= " " . $data[$f+3];
150
				$f = $f+3;
151
				break;
152
			case "ends":
153
				$leases[$l]['end'] = $data[$f+2];
154
				$leases[$l]['end'] .= " " . $data[$f+3];
155
				$f = $f+3;
156
				break;
157
			case "tstp":
158
				$f = $f+3;
159
				break;
160
			case "tsfp":
161
				$f = $f+3;
162
				break;
163
			case "atsfp":
164
				$f = $f+3;
165
				break;
166
			case "cltt":
167
				$f = $f+3;
168
				break;
169
			case "binding":
170 699737d9 Phil Davis
				switch ($data[$f+2]) {
171 b45630bf jim-p
					case "active":
172
						$leases[$l]['act'] = "active";
173
						break;
174
					case "free":
175
						$leases[$l]['act'] = "expired";
176
						$leases[$l]['online'] = "offline";
177
						break;
178
					case "backup":
179
						$leases[$l]['act'] = "reserved";
180
						$leases[$l]['online'] = "offline";
181
						break;
182
				}
183
				$f = $f+1;
184
				break;
185
			case "next":
186
				/* skip the next binding statement */
187
				$f = $f+3;
188
				break;
189 f1273b82 jim-p
			case "rewind":
190
				/* skip the rewind binding statement */
191
				$f = $f+3;
192
				break;
193 b45630bf jim-p
			case "hardware":
194
				$leases[$l]['mac'] = $data[$f+2];
195
				/* check if it's online and the lease is active */
196 5f601060 Phil Davis
				if ($leases[$l]['act'] == "active") {
197 b45630bf jim-p
					$online = exec("/usr/sbin/arp -an |/usr/bin/awk '/{$leases[$l]['ip']}/ {print}'|wc -l");
198
					if ($online == 1) {
199
						$leases[$l]['online'] = 'online';
200
					} else {
201
						$leases[$l]['online'] = 'offline';
202
					}
203
				}
204
				$f = $f+2;
205
				break;
206
			case "client-hostname":
207 5f601060 Phil Davis
				if ($data[$f+1] <> "") {
208 699737d9 Phil Davis
					$leases[$l]['hostname'] = preg_replace('/"/', '', $data[$f+1]);
209 b45630bf jim-p
				} else {
210
					$hostname = gethostbyaddr($leases[$l]['ip']);
211 5f601060 Phil Davis
					if ($hostname <> "") {
212 b45630bf jim-p
						$leases[$l]['hostname'] = $hostname;
213
					}
214
				}
215
				$f = $f+1;
216
				break;
217
			case "uid":
218
				$f = $f+1;
219
				break;
220
		}
221
		$f++;
222
	}
223
	$l++;
224
	$i++;
225 fc259334 Seth Mos
}
226
227
/* remove duplicate items by mac address */
228 5f601060 Phil Davis
if (count($leases) > 0) {
229 699737d9 Phil Davis
	$leases = remove_duplicate($leases, "ip");
230 fc259334 Seth Mos
}
231
232 5f601060 Phil Davis
if (count($pools) > 0) {
233 699737d9 Phil Davis
	$pools = remove_duplicate($pools, "name");
234 b45630bf jim-p
	asort($pools);
235 fc259334 Seth Mos
}
236
237
// Put this in an easy to use form
238
$dhcpmac = array();
239
$dhcpip = array();
240 f8ec8de4 Renato Botelho
241 fc259334 Seth Mos
foreach ($leases as $value) {
242 f8ec8de4 Renato Botelho
	$dhcpmac[$value['mac']] = $value['hostname'];
243
	$dhcpip[$value['ip']] = $value['hostname'];
244 93e1b16c Scott Ullrich
}
245
246 699737d9 Phil Davis
exec("/usr/sbin/arp -an", $rawdata);
247 93e1b16c Scott Ullrich
248 f8ec8de4 Renato Botelho
$i = 0;
249 cbe3ea96 Ermal Luçi
250
/* if list */
251
$ifdescrs = get_configured_interface_with_descr();
252 93e1b16c Scott Ullrich
253 e7237dd0 jim-p
foreach ($ifdescrs as $key => $interface) {
254
	$thisif = convert_friendly_interface_to_real_interface_name($key);
255 5f601060 Phil Davis
	if (!empty($thisif)) {
256 e7237dd0 jim-p
		$hwif[$thisif] = $interface;
257 5f601060 Phil Davis
	}
258 93e1b16c Scott Ullrich
}
259
260
$data = array();
261
foreach ($rawdata as $line) {
262 699737d9 Phil Davis
	$elements = explode(' ', $line);
263 f8ec8de4 Renato Botelho
264 93e1b16c Scott Ullrich
	if ($elements[3] != "(incomplete)") {
265
		$arpent = array();
266 699737d9 Phil Davis
		$arpent['ip'] = trim(str_replace(array('(', ')'), '', $elements[1]));
267 93e1b16c Scott Ullrich
		$arpent['mac'] = trim($elements[3]);
268
		$arpent['interface'] = trim($elements[5]);
269
		$data[] = $arpent;
270
	}
271
}
272
273 699737d9 Phil Davis
function _getHostName($mac, $ip) {
274 93e1b16c Scott Ullrich
	global $dhcpmac, $dhcpip;
275 f8ec8de4 Renato Botelho
276 5f601060 Phil Davis
	if ($dhcpmac[$mac]) {
277 93e1b16c Scott Ullrich
		return $dhcpmac[$mac];
278 5f601060 Phil Davis
	} else if ($dhcpip[$ip]) {
279 93e1b16c Scott Ullrich
		return $dhcpip[$ip];
280 5f601060 Phil Davis
	} else {
281 d31ca336 Renato Botelho
		exec("host -W 1 " . escapeshellarg($ip), $output);
282 699737d9 Phil Davis
		if (preg_match('/.*pointer ([A-Za-z_0-9.-]+)\..*/', $output[0], $matches)) {
283 5f601060 Phil Davis
			if ($matches[1] <> $ip) {
284 7d5b007c Sjon Hortensius
				return $matches[1];
285 5f601060 Phil Davis
			}
286 dd4bded7 Evgeny Yurchenko
		}
287
	}
288
	return "";
289 93e1b16c Scott Ullrich
}
290
291 699737d9 Phil Davis
$pgtitle = array(gettext("Diagnostics"), gettext("ARP Table"));
292 fc259334 Seth Mos
include("head.inc");
293 20b9b335 Scott Ullrich
294 93e1b16c Scott Ullrich
?>
295 20b9b335 Scott Ullrich
296 a4af095c Renato Botelho
<!-- On modern hardware the table will load so fast you may never see this! -->
297 4a993c8f Scott Ullrich
<div id="loading">
298 a4af095c Renato Botelho
	<?= gettext(" Loading, please wait...")?>
299 4a993c8f Scott Ullrich
</div>
300
301
<?php
302
303 868ba36d Scott Ullrich
// Flush buffers out to client so that they see Loading, please wait....
304 5f601060 Phil Davis
for ($i = 0; $i < ob_get_level(); $i++) {
305
	ob_end_flush();
306
}
307 608947a8 Stephen Beaver
308 868ba36d Scott Ullrich
ob_implicit_flush(1);
309
310 20b9b335 Scott Ullrich
// Resolve hostnames and replace Z_ with "".  The intention
311
// is to sort the list by hostnames, alpha and then the non
312
// resolvable addresses will appear last in the list.
313 dd4bded7 Evgeny Yurchenko
$dnsavailable=1;
314 7d5b007c Sjon Hortensius
$dns = trim(_getHostName("", "8.8.8.8"));
315 5f601060 Phil Davis
if ($dns == "") {
316 7d5b007c Sjon Hortensius
	$dns = trim(_getHostName("", "8.8.4.4"));
317 5f601060 Phil Davis
	if ($dns == "") {
318
		$dnsavailable = 0;
319
	}
320 dd4bded7 Evgeny Yurchenko
}
321
322 4a993c8f Scott Ullrich
foreach ($data as &$entry) {
323 5f601060 Phil Davis
	if ($dnsavailable) {
324 dd4bded7 Evgeny Yurchenko
		$dns = trim(_getHostName($entry['mac'], $entry['ip']));
325 5f601060 Phil Davis
	} else {
326 dd4bded7 Evgeny Yurchenko
		$dns="";
327 5f601060 Phil Davis
	}
328
	if (trim($dns)) {
329 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "$dns";
330 5f601060 Phil Davis
	} else {
331 4a993c8f Scott Ullrich
		$entry['dnsresolve'] = "Z_ ";
332 5f601060 Phil Davis
	}
333 4a993c8f Scott Ullrich
}
334 20b9b335 Scott Ullrich
335
// Sort the data alpha first
336 4a993c8f Scott Ullrich
$data = msort($data, "dnsresolve");
337
338 57f2840e Evgeny
// Load MAC-Manufacturer table
339
$mac_man = load_mac_manufacturer_table();
340 4a993c8f Scott Ullrich
?>
341 89f64f0f Sander van Leeuwen
<div class="table-responsive">
342 608947a8 Stephen Beaver
	<table class="sortable-theme-bootstrap table table-striped table-hover" data-sortable>
343 89f64f0f Sander van Leeuwen
		<thead>
344
			<tr>
345
				<th><?= gettext("Interface")?></th>
346
				<th><?= gettext("IP address")?></th>
347
				<th><?= gettext("MAC address")?></th>
348
				<th><?= gettext("Hostname")?></th>
349
			</tr>
350
		</thead>
351
		<tbody>
352
		<?php foreach ($data as $entry): ?>
353
			<tr>
354
				<td><?=$hwif[$entry['interface']]?></td>
355
				<td><?=$entry['ip']?></td>
356
				<td>
357
					<?=trim($entry['mac'])?>
358
				<?php
359
					$mac = trim($entry['mac']);
360
					$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
361
362
					if (isset($mac_man[$mac_hi]))
363
						print '<small>('. $mac_man[$mac_hi] .')</small>';
364
	?>
365
				</td>
366
				<td><?=trim(str_replace("Z_ ", "", $entry['dnsresolve']))?></td>
367
			</tr>
368
		<?php endforeach?>
369
		</tbody>
370
	</table>
371
</div>
372
373 a4af095c Renato Botelho
<script>
374 293ceb87 Colin Fleming
//<![CDATA[
375 a4af095c Renato Botelho
// Clear the "loading" div once the page has loaded"
376 0da0d43e Phil Davis
events.push(function(){
377 a4af095c Renato Botelho
	$('#loading').empty();
378
});
379 0da0d43e Phil Davis
//]]>
380 4a993c8f Scott Ullrich
</script>
381 a4af095c Renato Botelho
382 0da0d43e Phil Davis
<?php
383 a4af095c Renato Botelho
print_info_box(gettext("Local IPv6 peers use ") . '<a href="diag_ndp.php">' . gettext("NDP") . '</a>' . gettext(" instead of ARP"), 'info');
384 93e1b16c Scott Ullrich
385 a4af095c Renato Botelho
include("foot.inc")?>