Project

General

Profile

Download (17.5 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-2018 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 ($_POST['act'] == 'connect') {
49
	if (ctype_digit($_POST['ikeid'])) {
50
		$ph1ent = ipsec_get_phase1($_POST['ikeid']);
51
		if (!empty($ph1ent)) {
52
			if (empty($ph1ent['iketype']) || $ph1ent['iketype'] == 'ikev1' || isset($ph1ent['splitconn'])) {
53
				$ph2entries = ipsec_get_number_of_phase2($_POST['ikeid']);
54
				for ($i = 0; $i < $ph2entries; $i++) {
55
					$connid = escapeshellarg("con{$_POST['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($_POST['ikeid']));
61
				mwexec_bg("/usr/local/sbin/ipsec up con" . escapeshellarg($_POST['ikeid']));
62
			}
63
		}
64
	}
65
} else if ($_POST['act'] == 'ikedisconnect') {
66

    
67
	if (ctype_digit($_POST['ikeid'])) {
68
		if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
69
			mwexec_bg("/usr/local/sbin/ipsec down " ."'" . "con" . escapeshellarg($_POST['ikeid']) . "[" . escapeshellarg($_POST['ikesaid']) . "]" . "'");
70
		} else {
71
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_POST['ikeid']));
72
		}
73
	}
74
} else if ($_POST['act'] == 'childdisconnect') {
75
	//pull out number from id
76
	$id_val = filter_var($_POST['ikeid'], FILTER_SANITIZE_NUMBER_INT);
77

    
78
	if (ctype_digit($id_val)) {
79
		if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
80
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($id_val) . "{" . escapeshellarg($_POST['ikesaid']) . "}");
81
		}
82
	}
83
}
84

    
85
// Table body is composed here so that it can be more easily updated via AJAX
86
function print_ipsec_body() {
87
	global $config;
88
	$a_phase1 = &$config['ipsec']['phase1'];
89
	$status = ipsec_list_sa();
90
	$ipsecconnected = array();
91
	if (is_array($status)) {
92
		foreach ($status as $ikeid => $ikesa) {
93
			//check which array format
94
			if(isset($ikesa['con-id'])){
95
				$con_id = substr($ikesa['con-id'],3);
96
			}else{
97
				$con_id = filter_var($ikeid, FILTER_SANITIZE_NUMBER_INT);
98
			}
99
			if ($ikesa['version'] == 1) {
100
				$ph1idx = substr($con_id, 0, strrpos(substr($con_id, 0, -1), '00'));
101
				$ipsecconnected[$ph1idx] = $ph1idx;
102
			} else {
103
				if (!ipsec_ikeid_used($con_id)) {
104
					// probably a v2 with split connection then
105
					$ph1idx = substr($con_id, 0, strrpos(substr($con_id, 0, -1), '00'));
106
					$ipsecconnected[$ph1idx] = $ph1idx;
107
				} else {
108
					$ipsecconnected[$con_id] = $ph1idx = $con_id;
109
				}
110
			}
111

    
112
			print("<tr>\n");
113
			print("<td>\n");
114
			if (is_array($a_phase1) && htmlspecialchars(ipsec_get_descr($ph1idx)) == "") {
115
				foreach ($a_phase1 as $ph1) {
116
					if($con_id == $ph1['ikeid'] && isset($ph1['mobile']) ){
117
						print(htmlspecialchars($ph1['descr']));
118
						break;
119
					}
120
				}
121
			}
122
			print(htmlspecialchars(ipsec_get_descr($ph1idx)));
123
			print("</td>\n");
124
			print("<td>\n");
125

    
126
			if (!empty($ikesa['local-id'])) {
127
				if ($ikesa['local-id'] == '%any') {
128
					print(gettext('Any identifier'));
129
				} else {
130
					print(htmlspecialchars($ikesa['local-id']));
131
				}
132
			} else {
133
				print(gettext("Unknown"));
134
			}
135

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

    
139
			if (!empty($ikesa['local-host'])) {
140
				print(htmlspecialchars($ikesa['local-host']));
141
			} else {
142
				print(gettext("Unknown"));
143
			}
144

    
145
			/*
146
			 * XXX: local-nat-t was defined by pfSense
147
			 * When strongswan team accepted the change, they changed it to
148
			 * nat-local. Keep both for a while and remove local-nat-t in
149
			 * the future
150
			 */
151
			if (isset($ikesa['local-nat-t']) || isset($ikesa['nat-local'])) {
152
				print(" NAT-T");
153
			}
154

    
155
			print("</td>\n");
156
			print("<td>\n");
157

    
158
			$identity = "";
159
			if (!empty($ikesa['remote-id'])) {
160
				if ($ikesa['remote-id'] == '%any') {
161
					$identity = htmlspecialchars(gettext('Any identifier'));
162
				} else {
163
					$identity = htmlspecialchars($ikesa['remote-id']);
164
				}
165
			}
166

    
167
			if (!empty($ikesa['remote-xauth-id'])) {
168
				echo htmlspecialchars($ikesa['remote-xauth-id']);
169
				echo "<br/>{$identity}";
170
			} elseif (!empty($ikesa['remote-eap-id'])) {
171
				echo htmlspecialchars($ikesa['remote-eap-id']);
172
				echo "<br/>{$identity}";
173
			} else {
174
				if (empty($identity)) {
175
					print(gettext("Unknown"));
176
				} else {
177
					print($identity);
178
				}
179
			}
180

    
181
			print("</td>\n");
182
			print("<td>\n");
183

    
184
			if (!empty($ikesa['remote-host'])) {
185
				print(htmlspecialchars($ikesa['remote-host']));
186
			} else {
187
				print(gettext("Unknown"));
188
			}
189
			/*
190
			 * XXX: remote-nat-t was defined by pfSense
191
			 * When strongswan team accepted the change, they changed it to
192
			 * nat-remote. Keep both for a while and remove remote-nat-t in
193
			 * the future
194
			 */
195
			if (isset($ikesa['remote-nat-t']) || isset($ikesa['nat-remote'])) {
196
				print(" NAT-T");
197
			}
198

    
199
			print("</td>\n");
200
			print("<td>\n");
201
			print("IKEv" . htmlspecialchars($ikesa['version']));
202
			print("<br/>\n");
203

    
204
			if ($ikesa['initiator'] == 'yes') {
205
				print("initiator");
206
			} else {
207
				print("responder");
208
			}
209

    
210
			print("</td>\n");
211
			print("<td>\n");
212
			print(htmlspecialchars($ikesa['reauth-time']) . gettext(" seconds (") . convert_seconds_to_dhms($ikesa['reauth-time']) . ")");
213
			print("</td>\n");
214
			print("<td>\n");
215
			print(htmlspecialchars($ikesa['encr-alg']));
216
			print("<br/>");
217
			print(htmlspecialchars($ikesa['integ-alg']));
218
			print("<br/>");
219
			print(htmlspecialchars($ikesa['prf-alg']));
220
			print("<br/>\n");
221
			print(htmlspecialchars($ikesa['dh-group']));
222
			print("</td>\n");
223
			print("<td>\n");
224

    
225
			if ($ikesa['state'] == 'ESTABLISHED') {
226
				print('<span class="text-success">');
227
			} else {
228
				print('<span>');
229
			}
230

    
231
			print(ucfirst(htmlspecialchars($ikesa['state'])));
232

    
233
			if ($ikesa['state'] == 'ESTABLISHED') {
234
				print("<br/>");
235
				printf(gettext('%1$s seconds (%2$s) ago'), htmlspecialchars($ikesa['established']), convert_seconds_to_dhms($ikesa['established']));
236
			}
237

    
238
			print("</span>");
239
			print("</td>\n");
240
			print("<td>\n");
241

    
242
			if ($ikesa['state'] != 'ESTABLISHED') {
243

    
244
				print('<a href="status_ipsec.php?act=connect&amp;ikeid=' . $con_id . '&amp;ikesaid=' .$ikesa['uniqueid'] . '" class="btn btn-xs btn-success" data-toggle="tooltip" title="' . gettext("Connect VPN"). '" usepost>');
245
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
246
				print(gettext("Connect VPN"));
247
				print("</a>\n");
248

    
249
			} else {
250

    
251
				print('<a href="status_ipsec.php?act=ikedisconnect&amp;ikeid=' . $con_id . '&amp;ikesaid=' .$ikesa['uniqueid'] . '"class="btn btn-xs btn-danger" data-toggle="tooltip" title="' . gettext("Disconnect VPN") . '" usepost>');
252
				print('<i class="fa fa-trash icon-embed-btn"></i>');
253
				print(gettext("Disconnect"));
254
				print("</a><br />\n");
255

    
256
			}
257

    
258
			print("</td>\n");
259
			print("</tr>\n");
260
			print("<tr>\n");
261
			print("<td colspan = 10>\n");
262

    
263
			if (is_array($ikesa['child-sas']) && (count($ikesa['child-sas']) > 0)) {
264
				$child_key = "";
265
				foreach ($ikesa['child-sas'] as $key => $val){
266
					$child_key = $key;
267
					break;
268
				}
269

    
270
				print('<div>');
271
				print('<a type="button" id="btnchildsa-'. $child_key .  '" class="btn btn-sm btn-info">');
272
				print('<i class="fa fa-plus-circle icon-embed-btn"></i>');
273
				print(gettext('Show child SA entries'));
274
				print("</a>\n");
275
				print("	</div>\n");
276

    
277
				print('<table class="table table-hover table-condensed" id="childsa-'.$child_key . '" style="display:none">');
278
				print("<thead>\n");
279
				print('<tr class="bg-info">');
280
				print('<th><?=gettext("Local subnets")?></th>');
281
				print('<th><?=gettext("Local SPI(s)")?></th>');
282
				print('<th><?=gettext("Remote subnets")?></th>');
283
				print('<th><?=gettext("Times")?></th>');
284
				print('<th><?=gettext("Algo")?></th>');
285
				print('<th><?=gettext("Stats")?></th>');
286
				print('<th><!-- Buttons --></th>');
287
				print("</tr\n");
288
				print("</thead>\n");
289
				print("<tbody>\n");
290

    
291
				foreach ($ikesa['child-sas'] as $childid => $childsa) {
292
					print("<tr>");
293
					print("<td>\n");
294

    
295
					if (is_array($childsa['local-ts'])) {
296
						foreach ($childsa['local-ts'] as $lnets) {
297
							print(htmlspecialchars(ipsec_fixup_network($lnets)) . "<br />");
298
						}
299
					} else {
300
						print(gettext("Unknown"));
301
					}
302

    
303
					print("</td>\n");
304
					print("<td>\n");
305

    
306
					if (isset($childsa['spi-in'])) {
307
						print(gettext("Local: ") . htmlspecialchars($childsa['spi-in']));
308
					}
309

    
310
					if (isset($childsa['spi-out'])) {
311
						print('<br/>' . gettext('Remote: ') . htmlspecialchars($childsa['spi-out']));
312
					}
313

    
314
					print("</td>\n");
315
					print("<td>\n");
316

    
317
					if (is_array($childsa['remote-ts'])) {
318
						foreach ($childsa['remote-ts'] as $rnets) {
319
							print(htmlspecialchars(ipsec_fixup_network($rnets)) . '<br />');
320
						}
321
					} else {
322
						print(gettext("Unknown"));
323
					}
324

    
325
					print("</td>\n");
326
					print("<td>\n");
327

    
328
					printf(gettext('Rekey: %1$s seconds (%2$s)'), htmlspecialchars($childsa['rekey-time']), convert_seconds_to_dhms($childsa['rekey-time']));
329
					print('<br/>');
330
					printf(gettext('Life: %1$s seconds (%2$s)'), htmlspecialchars($childsa['life-time']), convert_seconds_to_dhms($childsa['life-time']));
331
					print('<br/>');
332
					printf(gettext('Install: %1$s seconds (%2$s)'), htmlspecialchars($childsa['install-time']), convert_seconds_to_dhms($childsa['install-time']));
333

    
334

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

    
338
					print(htmlspecialchars($childsa['encr-alg']) . '<br/>');
339
					print(htmlspecialchars($childsa['integ-alg']) . '<br/>');
340

    
341
					if (!empty($childsa['prf-alg'])) {
342
						print(htmlspecialchars($childsa['prf-alg']) . '<br/>');
343
					}
344

    
345
					if (!empty($childsa['dh-group'])) {
346
						print(htmlspecialchars($childsa['dh-group']) . '<br/>');
347
					}
348

    
349
					if (!empty($childsa['esn'])) {
350
						print(htmlspecialchars($childsa['esn']) . '<br/>');
351
					}
352

    
353
					print(gettext("IPComp: "));
354
					if (!empty($childsa['cpi-in']) || !empty($childsa['cpi-out'])) {
355
						print(htmlspecialchars($childsa['cpi-in']) . " " . htmlspecialchars($childsa['cpi-out']));
356
					} else {
357
						print(gettext('none'));
358
					}
359

    
360
					print("</td>\n");
361
					print("<td>\n");
362

    
363
					print(gettext("Bytes-In: ") . htmlspecialchars(number_format($childsa['bytes-in'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-in'])) . ')<br/>');
364
					print(gettext("Packets-In: ") . htmlspecialchars(number_format($childsa['packets-in'])) . '<br/>');
365
					print(gettext("Bytes-Out: ") . htmlspecialchars(number_format($childsa['bytes-out'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-out'])) . ')<br/>');
366
					print(gettext("Packets-Out: ") . htmlspecialchars(number_format($childsa['packets-out'])) . '<br/>');
367

    
368
					print("</td>\n");
369
					print("<td>\n");
370
					print('<a href="status_ipsec.php?act=childdisconnect&amp;ikeid=' . $childsa['name'] . '&amp;ikesaid=' . $childsa['uniqueid'] . '" class="btn btn-xs btn-warning" data-toggle="tooltip" title="' . gettext('Disconnect Child SA') . '" usepost>');
371
					print('<i class="fa fa-trash icon-embed-btn"></i>');
372
					print(gettext("Disconnect"));
373
					print("</a>\n");
374
					print("</td>\n");
375
					print("</tr>\n");
376

    
377
				}
378

    
379
				print("</tbody>\n");
380
				print("	</table>\n");
381
				print("</td>\n");
382
				print("</tr>\n");
383

    
384
			}
385

    
386
			unset($con_id);
387
		}
388

    
389
	}
390

    
391
	$rgmap = array();
392
	if (is_array($a_phase1)) {
393
		foreach ($a_phase1 as $ph1ent) {
394
			if (isset($ph1ent['disabled'])) {
395
				continue;
396
			}
397

    
398
			$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
399

    
400
			if ($ipsecconnected[$ph1ent['ikeid']]) {
401
				continue;
402
			}
403

    
404
			print("<tr>\n");
405
			print("<td>\n");
406

    
407
			print(htmlspecialchars($ph1ent['descr']));
408
			print("</td>\n");
409
			print("<td>\n");
410
			list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
411

    
412
			if (empty($myid_data)) {
413
				print(gettext("Unknown"));
414
			} else {
415
				print(htmlspecialchars($myid_data));
416
			}
417

    
418
			print("</td>\n");
419
			print("<td>\n");
420
			$ph1src = ipsec_get_phase1_src($ph1ent);
421

    
422
			if (empty($ph1src)) {
423
				print(gettext("Unknown"));
424
			} else {
425
				print(htmlspecialchars($ph1src));
426
			}
427

    
428
			print("</td>\n");
429
			print("<td>\n");
430

    
431
			list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
432

    
433
			if (empty($peerid_data)) {
434
				print(gettext("Unknown"));
435
			} else {
436
				print(htmlspecialchars($peerid_data));
437
			}
438
			print("			</td>\n");
439
			print("			<td>\n");
440
			$ph1src = ipsec_get_phase1_dst($ph1ent);
441

    
442
			if (empty($ph1src)) {
443
				print(gettext("Unknown"));
444
			} else {
445
				print(htmlspecialchars($ph1src));
446
			}
447

    
448
			print("</td>\n");
449
			print("<td>\n");
450
			print("</td>\n");
451
			print("<td>\n");
452
			print("</td>\n");
453
			print("<td>\n");
454
			print("</td>\n");
455

    
456
			if (isset($ph1ent['mobile'])) {
457

    
458
				print("<td>\n");
459
				print(gettext("Awaiting connections"));
460
				print("</td>\n");
461
				print("<td>\n");
462
				print("</td>\n");
463
				print("</td>\n");
464
			} else {
465

    
466
				print("<td>\n");
467
				print(gettext("Disconnected"));
468
				print("</td>\n");
469
				print("<td>\n");
470
				print('<a href="status_ipsec.php?act=connect&amp;ikeid=' . $ph1ent['ikeid'] . '" class="btn btn-xs btn-success" usepost>');
471
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
472
				print(gettext("Connect VPN"));
473
				print("</a>\n");
474
				print("</td>\n");
475

    
476
			}
477
			print("</tr>\n");
478
		}
479
	}
480

    
481
	unset($ipsecconnected, $phase1, $rgmap);
482
}
483

    
484
$pgtitle = array(gettext("Status"), gettext("IPsec"), gettext("Overview"));
485
$pglinks = array("", "@self", "@self");
486
$shortcut_section = "ipsec";
487

    
488
include("head.inc");
489

    
490
$tab_array = array();
491
$tab_array[] = array(gettext("Overview"), true, "status_ipsec.php");
492
$tab_array[] = array(gettext("Leases"), false, "status_ipsec_leases.php");
493
$tab_array[] = array(gettext("SADs"), false, "status_ipsec_sad.php");
494
$tab_array[] = array(gettext("SPDs"), false, "status_ipsec_spd.php");
495
display_top_tabs($tab_array);
496
?>
497

    
498
<div class="panel panel-default">
499
	<div class="panel-heading"><h2 class="panel-title"><?=gettext("IPsec Status");?></h2></div>
500
	<div class="panel-body table-responsive">
501
		<table class="table table-striped table-condensed table-hover sortable-theme-bootstrap" data-sortable>
502
			<thead>
503
				<tr>
504
					<th><?=gettext("Description")?></th>
505
					<th><?=gettext("Local ID")?></th>
506
					<th><?=gettext("Local IP")?></th>
507
					<th><?=gettext("Remote ID")?></th>
508
					<th><?=gettext("Remote IP")?></th>
509
					<th><?=gettext("Role")?></th>
510
					<th><?=gettext("Reauth")?></th>
511
					<th><?=gettext("Algo")?></th>
512
					<th><?=gettext("Status")?></th>
513
					<th></th>
514
				</tr>
515
			</thead>
516
			<tbody id="ipsec-body">
517
				<tr>
518
					<td colspan="10">
519
						<?=print_info_box(gettext("Collecting IPsec status information."), "warning", "")?>
520
					</td>
521
				</tr>
522
			</tbody>
523
		</table>
524
	</div>
525
</div>
526

    
527
<?php
528
unset($status);
529

    
530
if (ipsec_enabled()) {
531
	print('<div class="infoblock">');
532
} else {
533
	print('<div class="infoblock blockopen">');
534
}
535

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

    
540
<script type="text/javascript">
541
//<![CDATA[
542

    
543
events.push(function() {
544
	ajax_lock = false;		// Mutex so we don't make a call until the previous call is finished
545
	sa_open = new Array();	// Array in which to keep the child SA show/hide state
546
	tryCount = 3;
547
	// Fetch the tbody contents from the server
548
	function update_table() {
549
		if (ajax_lock) {
550
			return;
551
		}
552

    
553
		ajax_lock = true;
554

    
555
		ajaxRequest = $.ajax(
556
			{
557
				url: "/status_ipsec.php",
558
				type: "post",
559
				data: {
560
					ajax: 	"ajax"
561
				},
562
				error: function(xhr, textStatus, errorThrown){
563
					//alert("error.... retrying");
564
					if (tryCount > 0){
565
						tryCount --;
566
						ajax_lock = false;
567
						update_table();
568
					}
569
					return;
570
				}
571
			}
572
		);
573

    
574
		// Deal with the results of the above ajax call
575
		ajaxRequest.done(function (response, textStatus, jqXHR) {
576
			if(textStatus === "success"){
577
				tryCount =3;
578
			}
579
			if (!response) {
580
				response = '<tr><td colspan="10"><?=print_info_box(addslashes(gettext("No IPsec status information available.")), "warning", "")?></td></tr>';
581
			}
582

    
583
			$('#ipsec-body').html(response);
584
			ajax_lock = false;
585

    
586
			// Update "Show child SA" handlers
587
			$('[id^=btnchildsa-]').click(function () {
588
				show_childsa($(this).prop("id").replace( /^\D+/g, ''));
589
			});
590

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

    
595
				if (sa_open[sa_idx]) {
596
					show_childsa(sa_idx);
597
				}
598
			});
599

    
600
			// re-attached the GET to POST handler
601
			interceptGET();
602

    
603
			// and do it again
604
			setTimeout(update_table, 5000);
605
		});
606
	}
607

    
608
	function show_childsa(said) {
609
		sa_open[said] = true;
610
		$('#childsa-con' + said).show();
611
		$('#btnchildsa-con' + said).hide();
612
	}
613

    
614
	// Populate the tbody on page load
615
	update_table();
616
});
617
//]]>
618
</script>
619

    
620
<?php
621
include("foot.inc"); ?>
(169-169/232)