Project

General

Profile

Download (8.31 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_ndp.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2019 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2011 Seth Mos <seth.mos@dds.nl>
8
 * All rights reserved.
9
 *
10
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
##|+PRIV
28
##|*IDENT=page-diagnostics-ndptable
29
##|*NAME=Diagnostics: NDP Table
30
##|*DESCR=Allow access to the 'Diagnostics: NDP Table' page.
31
##|*MATCH=diag_ndp.php*
32
##|-PRIV
33

    
34
@ini_set('zlib.output_compression', 0);
35
@ini_set('implicit_flush', 1);
36
define('NDP_BINARY_PATH', '/usr/sbin/ndp');
37
require_once("guiconfig.inc");
38

    
39
// Delete ndp entry.
40
if (isset($_POST['deleteentry'])) {
41
	$ip = $_POST['deleteentry'];
42
	if (is_ipaddrv6($ip)) {
43
		$commandReturnValue = mwexec(NDP_BINARY_PATH . " -d " . escapeshellarg($ip), true);
44
		$deleteSucceededFlag = ($commandReturnValue == 0);
45
	} else {
46
		$deleteSucceededFlag = false;
47
	}
48

    
49
	$deleteResultMessage = ($deleteSucceededFlag)
50
		? sprintf(gettext("The NDP entry for %s has been deleted."), $ip)
51
		: sprintf(gettext("%s is not a valid IPv6 address or could not be deleted."), $ip);
52
	$deleteResultMessageType = ($deleteSucceededFlag)
53
		? 'success'
54
		: 'alert-warning';
55
}
56

    
57
exec(NDP_BINARY_PATH . " -na", $rawdata);
58

    
59
$i = 0;
60

    
61
/* if list */
62
$ifdescrs = get_configured_interface_with_descr();
63

    
64
foreach ($ifdescrs as $key =>$interface) {
65
	$hwif[$config['interfaces'][$key]['if']] = $interface;
66
}
67

    
68
/*
69
 * Key map for each element in $rawdata
70
 * 0 => Neighbor IP
71
 * 1 => Physical address (MAC)
72
 * 2 => Interface
73
 * 3 => Expiration
74
 * 4 => State
75
 * 5 => Flags
76
 */
77
$data = array();
78
array_shift($rawdata);
79
foreach ($rawdata as $line) {
80
	$elements = preg_split('/[ ]+/', $line);
81

    
82
	$ndpent = array();
83
	$ndpent['ipv6'] = trim($elements[0]);
84
	$ndpent['mac'] = trim($elements[1]);
85
	$ndpent['interface'] = trim($elements[2]);
86
	$ndpent['expiration'] = trim($elements[3]);
87
	$data[] = $ndpent;
88
}
89

    
90
/* FIXME: Not ipv6 compatible dns resolving. PHP needs fixing */
91
function _getHostName($mac, $ip) {
92
	if (is_ipaddr($ip)) {
93
		list($ip, $scope) = explode("%", $ip);
94
		if (gethostbyaddr($ip) <> "" and gethostbyaddr($ip) <> $ip) {
95
			return gethostbyaddr($ip);
96
		} else {
97
			return "";
98
		}
99
	}
100
}
101

    
102
// Resolve hostnames and replace Z_ with "".  The intention
103
// is to sort the list by hostnames, alpha and then the non
104
// resolvable addresses will appear last in the list.
105
foreach ($data as &$entry) {
106
	$dns = trim(_getHostName($entry['mac'], $entry['ipv6']));
107
	if (trim($dns)) {
108
		$entry['dnsresolve'] = "$dns";
109
	} else {
110
		$entry['dnsresolve'] = "Z_ ";
111
	}
112
}
113
unset($entry);
114

    
115
// Sort the data alpha first
116
$data = msort($data, "dnsresolve");
117

    
118
// Load MAC-Manufacturer table
119
$mac_man = load_mac_manufacturer_table();
120

    
121
$pgtitle = array(gettext("Diagnostics"), gettext("NDP Table"));
122
include("head.inc");
123

    
124
// Show message if defined.
125
if (isset($deleteResultMessage, $deleteResultMessageType)) {
126
	print_info_box(htmlentities($deleteResultMessage), $deleteResultMessageType);
127
}
128
?>
129

    
130
<div class="panel panel-default" id="search-panel">
131
	<div class="panel-heading">
132
		<h2 class="panel-title">
133
			<?=gettext('Search')?>
134
			<span class="widget-heading-icon pull-right">
135
				<a data-toggle="collapse" href="#search-panel_panel-body">
136
					<i class="fa fa-plus-circle"></i>
137
				</a>
138
			</span>
139
		</h2>
140
	</div>
141
	<div id="search-panel_panel-body" class="panel-body collapse in">
142
		<div class="form-group">
143
			<label class="col-sm-2 control-label">
144
				<?=gettext("Search term")?>
145
			</label>
146
			<div class="col-sm-5"><input class="form-control" name="searchstr" id="searchstr" type="text"/></div>
147
			<div class="col-sm-2">
148
				<select id="where" class="form-control">
149
					<option value="0"><?=gettext("IPv6 Address")?></option>
150
					<option value="1"><?=gettext("MAC Address")?></option>
151
					<option value="2"><?=gettext("Hostname")?></option>
152
					<option value="3"><?=gettext("Interface")?></option>
153
					<option value="4"><?=gettext("Expiration")?></option>
154
					<option value="5" selected><?=gettext("All")?></option>
155
				</select>
156
			</div>
157
			<div class="col-sm-3">
158
				<a id="btnsearch" title="<?=gettext("Search")?>" class="btn btn-primary btn-sm"><i class="fa fa-search icon-embed-btn"></i><?=gettext("Search")?></a>
159
				<a id="btnclear" title="<?=gettext("Clear")?>" class="btn btn-info btn-sm"><i class="fa fa-undo icon-embed-btn"></i><?=gettext("Clear")?></a>
160
			</div>
161
			<div class="col-sm-10 col-sm-offset-2">
162
				<span class="help-block"><?=gettext('Enter a search string or *nix regular expression to filter entries.')?></span>
163
			</div>
164
		</div>
165
	</div>
166
</div>
167

    
168
<div class="panel panel-default">
169
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('NDP Table')?></h2></div>
170
	<div class="panel-body">
171

    
172
<div class="table-responsive">
173
	<table class="table table-striped table-condensed table-hover sortable-theme-bootstrap" data-sortable>
174
		<thead>
175
			<tr>
176
				<th><?=gettext("IPv6 address")?></th>
177
				<th><?=gettext("MAC address")?></th>
178
				<th><?=gettext("Hostname")?></th>
179
				<th><?=gettext("Interface")?></th>
180
				<th><?=gettext("Expiration")?></th>
181
				<th data-sortable="false"><?=gettext("Actions")?></th>
182
			</tr>
183
	</thead>
184
	<tbody>
185
			<?php foreach ($data as $entry): ?>
186
				<tr>
187
					<td><?=$entry['ipv6']?></td>
188
					<td>
189
						<?php
190
						$mac=trim($entry['mac']);
191
						$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
192
						?>
193
						<?=$mac?>
194

    
195
						<?php if (isset($mac_man[$mac_hi])):?>
196
							(<?=$mac_man[$mac_hi]?>)
197
						<?php endif; ?>
198

    
199
					</td>
200
					<td>
201
						<?=htmlspecialchars(str_replace("Z_ ", "", $entry['dnsresolve']))?>
202
					</td>
203
					<td>
204
						<?php
205
						if (isset($hwif[$entry['interface']])) {
206
							echo $hwif[$entry['interface']];
207
						} else {
208
							echo $entry['interface'];
209
						}
210
						?>
211
					</td>
212
					<td>
213
						<?=$entry['expiration']?>
214
					</td>
215
					<td>
216
						<a class="fa fa-trash" title="<?=gettext('Delete NDP entry')?>"	href="diag_ndp.php?deleteentry=<?=$entry['ipv6']?>" usepost></a>
217
					</td>
218
				</tr>
219
			<?php endforeach; ?>
220
	</tbody>
221
	</table>
222
</div>
223

    
224
	</div>
225
</div>
226

    
227
<script type="text/javascript">
228
//<![CDATA[
229
events.push(function() {
230
	// Make these controls plain buttons
231
	$("#btnsearch").prop('type', 'button');
232
	$("#btnclear").prop('type', 'button');
233

    
234
	// Search for a term in the entry name and/or dn
235
	$("#btnsearch").click(function() {
236
		var searchstr = $('#searchstr').val().toLowerCase();
237
		var table = $("table tbody");
238
		var where = $('#where').val();
239

    
240
		table.find('tr').each(function (i) {
241
			var $tds = $(this).find('td'),
242
				ipaddr   = $tds.eq(0).text().trim().toLowerCase();
243
				macaddr  = $tds.eq(1).text().trim().toLowerCase();
244
				hostname = $tds.eq(2).text().trim().toLowerCase();
245
				iface    = $tds.eq(3).text().trim().toLowerCase(),
246
				stat     = $tds.eq(4).text().trim().toLowerCase();
247

    
248
			regexp = new RegExp(searchstr);
249
			if (searchstr.length > 0) {
250
				if (!(regexp.test(ipaddr)   && ((where == 0) || (where == 5))) &&
251
				    !(regexp.test(macaddr)  && ((where == 1) || (where == 5))) &&
252
				    !(regexp.test(hostname) && ((where == 2) || (where == 5))) &&
253
				    !(regexp.test(iface)    && ((where == 3) || (where == 5))) &&
254
				    !(regexp.test(stat)     && ((where == 4) || (where == 5)))
255
				    ) {
256
					$(this).hide();
257
				} else {
258
					$(this).show();
259
				}
260
			} else {
261
				$(this).show();	// A blank search string shows all
262
			}
263
		});
264
	});
265

    
266
	// Clear the search term and unhide all rows (that were hidden during a previous search)
267
	$("#btnclear").click(function() {
268
		var table = $("table tbody");
269

    
270
		$('#searchstr').val("");
271

    
272
		table.find('tr').each(function (i) {
273
			$(this).show();
274
		});
275
	});
276

    
277
	// Hitting the enter key will do the same as clicking the search button
278
	$("#searchstr").on("keyup", function (event) {
279
		if (event.keyCode == 13) {
280
			$("#btnsearch").get(0).click();
281
		}
282
	});
283

    
284
});
285
//]]>
286
</script>
287

    
288
<?php include("foot.inc");
(21-21/235)