Project

General

Profile

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

    
26
##|+PRIV
27
##|*IDENT=page-status-ipsec
28
##|*NAME=Status: IPsec
29
##|*DESCR=Allow access to the 'Status: IPsec' page.
30
##|*MATCH=status_ipsec.php*
31
##|-PRIV
32

    
33
require_once("guiconfig.inc");
34
require_once("ipsec.inc");
35

    
36
global $g;
37

    
38
if (!is_array($config['ipsec']['phase1'])) {
39
	$config['ipsec']['phase1'] = array();
40
}
41

    
42
// If this is just an AJAX call to update the table body, just generate the body and quit
43
if ($_REQUEST['ajax']) {
44
	print_ipsec_body();
45
	exit;
46
}
47

    
48
if ($_GET['act'] == 'connect') {
49
	if (ctype_digit($_GET['ikeid'])) {
50
		$ph1ent = ipsec_get_phase1($_GET['ikeid']);
51
		if (!empty($ph1ent)) {
52
			if (empty($ph1ent['iketype']) || $ph1ent['iketype'] == 'ikev1' || isset($ph1ent['splitconn'])) {
53
				$ph2entries = ipsec_get_number_of_phase2($_GET['ikeid']);
54
				for ($i = 0; $i < $ph2entries; $i++) {
55
					$connid = escapeshellarg("con{$_GET['ikeid']}00{$i}");
56
					mwexec_bg("/usr/local/sbin/ipsec down {$connid}");
57
					mwexec_bg("/usr/local/sbin/ipsec up {$connid}");
58
				}
59
			} else {
60
				mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']));
61
				mwexec_bg("/usr/local/sbin/ipsec up con" . escapeshellarg($_GET['ikeid']));
62
			}
63
		}
64
	}
65
} else if ($_GET['act'] == 'ikedisconnect') {
66
	if (ctype_digit($_GET['ikeid'])) {
67
		if (!empty($_GET['ikesaid']) && ctype_digit($_GET['ikesaid'])) {
68
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']) . "[" . escapeshellarg($_GET['ikesaid']) . "]");
69
		} else {
70
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']));
71
		}
72
	}
73
} else if ($_GET['act'] == 'childdisconnect') {
74
	if (ctype_digit($_GET['ikeid'])) {
75
		if (!empty($_GET['ikesaid']) && ctype_digit($_GET['ikesaid'])) {
76
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_GET['ikeid']) . "{" . escapeshellarg($_GET['ikesaid']) . "}");
77
		}
78
	}
79
}
80

    
81
// Table body is composed here so that it can be more easily updated via AJAX
82
function print_ipsec_body() {
83
	global $config;
84

    
85
	$a_phase1 = &$config['ipsec']['phase1'];
86
	$status = ipsec_list_sa();
87
	$ipsecconnected = array();
88

    
89
	if (is_array($status)) {
90
		foreach ($status as $ikeid => $ikesa) {
91
			$con_id = substr($ikeid, 3);
92

    
93
			if ($ikesa['version'] == 1) {
94
				$ph1idx = substr($con_id, 0, strrpos(substr($con_id, 0, -1), '00'));
95
				$ipsecconnected[$ph1idx] = $ph1idx;
96
			} else {
97
				$ipsecconnected[$con_id] = $ph1idx = $con_id;
98
			}
99

    
100
			print("<tr>\n");
101
			print("<td>\n");
102
			print(htmlspecialchars(ipsec_get_descr($ph1idx)));
103
			print("</td>\n");
104
			print("<td>\n");
105

    
106
			if (!empty($ikesa['local-id'])) {
107
				if ($ikesa['local-id'] == '%any') {
108
					print(gettext('Any identifier'));
109
				} else {
110
					print(htmlspecialchars($ikesa['local-id']));
111
				}
112
			} else {
113
				print(gettext("Unknown"));
114
			}
115

    
116
			print("</td>\n");
117
			print("<td>\n");
118

    
119
			if (!empty($ikesa['local-host'])) {
120
				print(htmlspecialchars($ikesa['local-host']));
121
			} else {
122
				print(gettext("Unknown"));
123
			}
124

    
125
			/*
126
			 * XXX: local-nat-t was defined by pfSense
127
			 * When strongswan team accepted the change, they changed it to
128
			 * nat-local. Keep both for a while and remove local-nat-t in
129
			 * the future
130
			 */
131
			if (isset($ikesa['local-nat-t']) || isset($ikesa['nat-local'])) {
132
				print(" NAT-T");
133
			}
134

    
135
			print("</td>\n");
136
			print("<td>\n");
137

    
138
			$identity = "";
139
			if (!empty($ikesa['remote-id'])) {
140
				if ($ikesa['remote-id'] == '%any') {
141
					$identity = htmlspecialchars(gettext('Any identifier'));
142
				} else {
143
					$identity = htmlspecialchars($ikesa['remote-id']);
144
				}
145
			}
146

    
147
			if (!empty($ikesa['remote-xauth-id'])) {
148
				echo htmlspecialchars($ikesa['remote-xauth-id']);
149
				echo "<br/>{$identity}";
150
			} elseif (!empty($ikesa['remote-eap-id'])) {
151
				echo htmlspecialchars($ikesa['remote-eap-id']);
152
				echo "<br/>{$identity}";
153
			} else {
154
				if (empty($identity)) {
155
					print(gettext("Unknown"));
156
				} else {
157
					print($identity);
158
				}
159
			}
160

    
161
			print("</td>\n");
162
			print("<td>\n");
163

    
164
			if (!empty($ikesa['remote-host'])) {
165
				print(htmlspecialchars($ikesa['remote-host']));
166
			} else {
167
				print(gettext("Unknown"));
168
			}
169
			/*
170
			 * XXX: remote-nat-t was defined by pfSense
171
			 * When strongswan team accepted the change, they changed it to
172
			 * nat-remote. Keep both for a while and remove remote-nat-t in
173
			 * the future
174
			 */
175
			if (isset($ikesa['remote-nat-t']) || isset($ikesa['nat-remote'])) {
176
				print(" NAT-T");
177
			}
178

    
179
			print("</td>\n");
180
			print("<td>\n");
181
			print("IKEv" . htmlspecialchars($ikesa['version']));
182
			print("<br/>\n");
183

    
184
			if ($ikesa['initiator'] == 'yes') {
185
				print("initiator");
186
			} else {
187
				print("responder");
188
			}
189

    
190
			print("</td>\n");
191
			print("<td>\n");
192
			print(htmlspecialchars($ikesa['reauth-time']) . gettext(" seconds (") . convert_seconds_to_dhms($ikesa['reauth-time']) . ")");
193
			print("</td>\n");
194
			print("<td>\n");
195
			print(htmlspecialchars($ikesa['encr-alg']));
196
			print("<br/>");
197
			print(htmlspecialchars($ikesa['integ-alg']));
198
			print("<br/>");
199
			print(htmlspecialchars($ikesa['prf-alg']));
200
			print("<br/>\n");
201
			print(htmlspecialchars($ikesa['dh-group']));
202
			print("</td>\n");
203
			print("<td>\n");
204

    
205
			if ($ikesa['state'] == 'ESTABLISHED') {
206
				print('<span class="text-success">');
207
			} else {
208
				print('<span>');
209
			}
210

    
211
			print(ucfirst(htmlspecialchars($ikesa['state'])));
212

    
213
			if ($ikesa['state'] == 'ESTABLISHED') {
214
				print("<br/>");
215
				printf(gettext('%1$s seconds (%2$s) ago'), htmlspecialchars($ikesa['established']), convert_seconds_to_dhms($ikesa['established']));
216
			}
217

    
218
			print("</span>");
219
			print("</td>\n");
220
			print("<td>\n");
221

    
222
			if ($ikesa['state'] != 'ESTABLISHED') {
223

    
224
				print('<a href="status_ipsec.php?act=connect&amp;ikeid=' . $con_id . '" class="btn btn-xs btn-success" data-toggle="tooltip" title="' . gettext("Connect VPN"). '" >');
225
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
226
				print(gettext("Connect VPN"));
227
				print("</a>\n");
228

    
229
			} else {
230

    
231
				print('<a href="status_ipsec.php?act=ikedisconnect&amp;ikeid=' . $con_id . '" class="btn btn-xs btn-danger" data-toggle="tooltip" title="' . gettext("Disconnect VPN") . '">');
232
				print('<i class="fa fa-trash icon-embed-btn"></i>');
233
				print(gettext("Disconnect"));
234
				print("</a><br />\n");
235

    
236
			}
237

    
238
			print("</td>\n");
239
			print("</tr>\n");
240
			print("<tr>\n");
241
			print("<td colspan = 10>\n");
242

    
243
			if (is_array($ikesa['child-sas']) && (count($ikesa['child-sas']) > 0)) {
244

    
245
				print('<div>');
246
				print('<a type="button" id="btnchildsa-' . $ikeid .  '" class="btn btn-sm btn-info">');
247
				print('<i class="fa fa-plus-circle icon-embed-btn"></i>');
248
				print(gettext('Show child SA entries'));
249
				print("</a>\n");
250
				print("	</div>\n");
251

    
252
				print('<table class="table table-hover table-condensed" id="childsa-' . $ikeid . '" style="display:none">');
253
				print("<thead>\n");
254
				print('<tr class="bg-info">');
255
				print('<th><?=gettext("Local subnets")?></th>');
256
				print('<th><?=gettext("Local SPI(s)")?></th>');
257
				print('<th><?=gettext("Remote subnets")?></th>');
258
				print('<th><?=gettext("Times")?></th>');
259
				print('<th><?=gettext("Algo")?></th>');
260
				print('<th><?=gettext("Stats")?></th>');
261
				print('<th><!-- Buttons --></th>');
262
				print("</tr\n");
263
				print("</thead>\n");
264
				print("<tbody>\n");
265

    
266
				foreach ($ikesa['child-sas'] as $childid => $childsa) {
267
					print("<tr>");
268
					print("<td>\n");
269

    
270
					if (is_array($childsa['local-ts'])) {
271
						foreach ($childsa['local-ts'] as $lnets) {
272
							print(htmlspecialchars(ipsec_fixup_network($lnets)) . "<br />");
273
						}
274
					} else {
275
						print(gettext("Unknown"));
276
					}
277

    
278
					print("</td>\n");
279
					print("<td>\n");
280

    
281
					if (isset($childsa['spi-in'])) {
282
						print(gettext("Local: ") . htmlspecialchars($childsa['spi-in']));
283
					}
284

    
285
					if (isset($childsa['spi-out'])) {
286
						print('<br/>' . gettext('Remote: ') . htmlspecialchars($childsa['spi-out']));
287
					}
288

    
289
					print("</td>\n");
290
					print("<td>\n");
291

    
292
					if (is_array($childsa['remote-ts'])) {
293
						foreach ($childsa['remote-ts'] as $rnets) {
294
							print(htmlspecialchars(ipsec_fixup_network($rnets)) . '<br />');
295
						}
296
					} else {
297
						print(gettext("Unknown"));
298
					}
299

    
300
					print("</td>\n");
301
					print("<td>\n");
302

    
303
					printf(gettext('Rekey: %1$s seconds (%2$s)'), htmlspecialchars($childsa['rekey-time']), convert_seconds_to_dhms($childsa['rekey-time']));
304
					print('<br/>');
305
					printf(gettext('Life: %1$s seconds (%2$s)'), htmlspecialchars($childsa['life-time']), convert_seconds_to_dhms($childsa['life-time']));
306
					print('<br/>');
307
					printf(gettext('Install: %1$s seconds (%2$s)'), htmlspecialchars($childsa['install-time']), convert_seconds_to_dhms($childsa['install-time']));
308

    
309

    
310
					print("</td>\n");
311
					print("<td>\n");
312

    
313
					print(htmlspecialchars($childsa['encr-alg']) . '<br/>');
314
					print(htmlspecialchars($childsa['integ-alg']) . '<br/>');
315

    
316
					if (!empty($childsa['prf-alg'])) {
317
						print(htmlspecialchars($childsa['prf-alg']) . '<br/>');
318
					}
319

    
320
					if (!empty($childsa['dh-group'])) {
321
						print(htmlspecialchars($childsa['dh-group']) . '<br/>');
322
					}
323

    
324
					if (!empty($childsa['esn'])) {
325
						print(htmlspecialchars($childsa['esn']) . '<br/>');
326
					}
327

    
328
					print(gettext("IPComp: "));
329
					if (!empty($childsa['cpi-in']) || !empty($childsa['cpi-out'])) {
330
						print(htmlspecialchars($childsa['cpi-in']) . " " . htmlspecialchars($childsa['cpi-out']));
331
					} else {
332
						print(gettext('none'));
333
					}
334

    
335
					print("</td>\n");
336
					print("<td>\n");
337

    
338
					print(gettext("Bytes-In: ") . htmlspecialchars(number_format($childsa['bytes-in'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-in'])) . ')<br/>');
339
					print(gettext("Packets-In: ") . htmlspecialchars(number_format($childsa['packets-in'])) . '<br/>');
340
					print(gettext("Bytes-Out: ") . htmlspecialchars(number_format($childsa['bytes-out'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-out'])) . ')<br/>');
341
					print(gettext("Packets-Out: ") . htmlspecialchars(number_format($childsa['packets-out'])) . '<br/>');
342

    
343
					print("</td>\n");
344
					print("<td>\n");
345
					print('<a href="status_ipsec.php?act=childdisconnect&amp;ikeid=' . $con_id . '&amp;ikesaid=' . $childsa['uniqueid'] . '" class="btn btn-xs btn-warning" data-toggle="tooltip" title="' . gettext('Disconnect Child SA') . '">');
346
					print('<i class="fa fa-trash icon-embed-btn"></i>');
347
					print(gettext("Disconnect"));
348
					print("</a>\n");
349
					print("</td>\n");
350
					print("</tr>\n");
351

    
352
				}
353

    
354
				print("</tbody>\n");
355
				print("	</table>\n");
356
				print("</td>\n");
357
				print("</tr>\n");
358

    
359
			}
360

    
361
			unset($con_id);
362
		}
363

    
364
	}
365

    
366
	$rgmap = array();
367
	if (is_array($a_phase1)) {
368
		foreach ($a_phase1 as $ph1ent) {
369
			if (isset($ph1ent['disabled'])) {
370
				continue;
371
			}
372

    
373
			$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
374

    
375
			if ($ipsecconnected[$ph1ent['ikeid']]) {
376
				continue;
377
			}
378

    
379
			print("<tr>\n");
380
			print("<td>\n");
381

    
382
			print(htmlspecialchars($ph1ent['descr']));
383
			print("</td>\n");
384
			print("<td>\n");
385
			list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
386

    
387
			if (empty($myid_data)) {
388
				print(gettext("Unknown"));
389
			} else {
390
				print(htmlspecialchars($myid_data));
391
			}
392

    
393
			print("</td>\n");
394
			print("<td>\n");
395
			$ph1src = ipsec_get_phase1_src($ph1ent);
396

    
397
			if (empty($ph1src)) {
398
				print(gettext("Unknown"));
399
			} else {
400
				print(htmlspecialchars($ph1src));
401
			}
402

    
403
			print("</td>\n");
404
			print("<td>\n");
405

    
406
			list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
407

    
408
			if (empty($peerid_data)) {
409
				print(gettext("Unknown"));
410
			} else {
411
				print(htmlspecialchars($peerid_data));
412
			}
413
			print("			</td>\n");
414
			print("			<td>\n");
415
			$ph1src = ipsec_get_phase1_dst($ph1ent);
416

    
417
			if (empty($ph1src)) {
418
				print(gettext("Unknown"));
419
			} else {
420
				print(htmlspecialchars($ph1src));
421
			}
422

    
423
			print("</td>\n");
424
			print("<td>\n");
425
			print("</td>\n");
426
			print("<td>\n");
427
			print("</td>\n");
428
			print("<td>\n");
429
			print("</td>\n");
430

    
431
			if (isset($ph1ent['mobile'])) {
432

    
433
				print("<td>\n");
434
				print(gettext("Awaiting connections"));
435
				print("</td>\n");
436
				print("<td>\n");
437
				print("</td>\n");
438
				print("</td>\n");
439
			} else {
440

    
441
				print("<td>\n");
442
				print(gettext("Disconnected"));
443
				print("</td>\n");
444
				print("<td>\n");
445
				print('<a href="status_ipsec.php?act=connect&amp;ikeid=' . $ph1ent['ikeid'] . '" class="btn btn-xs btn-success">');
446
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
447
				print(gettext("Connect VPN"));
448
				print("</a>\n");
449
				print("</td>\n");
450

    
451
			}
452
			print("</tr>\n");
453
		}
454
	}
455

    
456
	unset($ipsecconnected, $phase1, $rgmap);
457
}
458

    
459
$pgtitle = array(gettext("Status"), gettext("IPsec"), gettext("Overview"));
460
$pglinks = array("", "@self", "@self");
461
$shortcut_section = "ipsec";
462

    
463
include("head.inc");
464

    
465
$tab_array = array();
466
$tab_array[] = array(gettext("Overview"), true, "status_ipsec.php");
467
$tab_array[] = array(gettext("Leases"), false, "status_ipsec_leases.php");
468
$tab_array[] = array(gettext("SADs"), false, "status_ipsec_sad.php");
469
$tab_array[] = array(gettext("SPDs"), false, "status_ipsec_spd.php");
470
display_top_tabs($tab_array);
471
?>
472

    
473
<div class="panel panel-default">
474
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("IPsec Status");?></h2></div>
475
	<div class="panel-body table-responsive">
476
		<table class="table table-striped table-condensed table-hover sortable-theme-bootstrap" data-sortable>
477
			<thead>
478
				<tr>
479
					<th><?=gettext("Description")?></th>
480
					<th><?=gettext("Local ID")?></th>
481
					<th><?=gettext("Local IP")?></th>
482
					<th><?=gettext("Remote ID")?></th>
483
					<th><?=gettext("Remote IP")?></th>
484
					<th><?=gettext("Role")?></th>
485
					<th><?=gettext("Reauth")?></th>
486
					<th><?=gettext("Algo")?></th>
487
					<th><?=gettext("Status")?></th>
488
					<th></th>
489
				</tr>
490
			</thead>
491
			<tbody id="ipsec-body">
492
				<tr>
493
					<td colspan="10">
494
						<?=print_info_box(gettext("Collecting IPsec status information."), "warning", "")?>
495
					</td>
496
				</tr>
497
			</tbody>
498
		</table>
499
	</div>
500
</div>
501

    
502
<?php
503
unset($status);
504

    
505
if (ipsec_enabled()) {
506
	print('<div class="infoblock">');
507
} else {
508
	print('<div class="infoblock blockopen">');
509
}
510

    
511
print_info_box(sprintf(gettext('IPsec can be configured %1$shere%2$s.'), '<a href="vpn_ipsec.php">', '</a>'), 'info', false);
512
?>
513
</div>
514

    
515
<script type="text/javascript">
516
//<![CDATA[
517

    
518
events.push(function() {
519
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
520
	sa_open = new Array();	// Array in which to keep the child SA show/hide state
521

    
522
	// Fetch the tbody contents from the server
523
	function update_table() {
524
		if (ajax_lock) {
525
			return;
526
		}
527

    
528
		ajax_lock = true;
529

    
530
		ajaxRequest = $.ajax(
531
			{
532
				url: "/status_ipsec.php",
533
				type: "post",
534
				data: {
535
					ajax: 	"ajax"
536
				}
537
			}
538
		);
539

    
540
		// Deal with the results of the above ajax call
541
		ajaxRequest.done(function (response, textStatus, jqXHR) {
542

    
543
			if (!response) {
544
				response = '<tr><td colspan="10"><?=print_info_box(gettext("No IPsec status information available."), "warning", "")?></td></tr>';
545
			}
546

    
547
			$('#ipsec-body').html(response);
548
			ajax_lock = false;
549

    
550
			// Update "Show child SA" handlers
551
			$('[id^=btnchildsa-]').click(function () {
552
				show_childsa($(this).prop("id").replace( /^\D+/g, ''));
553
			});
554

    
555
			// Check the sa_open array for child SAs that have been opened
556
			$('[id^=childsa-con]').each(function(idx) {
557
				sa_idx = $(this).prop("id").replace( /^\D+/g, '');
558

    
559
				if (sa_open[sa_idx]) {
560
					show_childsa(sa_idx);
561
				}
562
			});
563

    
564
			// and do it again
565
			setTimeout(update_table, 5000);
566
		});
567
	}
568

    
569
	function show_childsa(said) {
570
		sa_open[said] = true;
571
		$('#childsa-con' + said).show();
572
		$('#btnchildsa-con' + said).hide();
573
	}
574

    
575
	// Populate the tbody on page load
576
	update_table();
577
});
578
//]]>
579
</script>
580

    
581
<?php
582
include("foot.inc"); ?>
(162-162/223)