Project

General

Profile

Download (17.6 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'])){
39
	$config['ipsec'] = array();
40
}
41

    
42
if (!is_array($config['ipsec']['phase1'])) {
43
	$config['ipsec']['phase1'] = array();
44
}
45

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

    
52
if ($_POST['act'] == 'connect') {
53
	if (ctype_digit($_POST['ikeid'])) {
54
		$ph1ent = ipsec_get_phase1($_POST['ikeid']);
55
		if (!empty($ph1ent)) {
56
			if (empty($ph1ent['iketype']) || $ph1ent['iketype'] == 'ikev1' || isset($ph1ent['splitconn'])) {
57
				$ph2entries = ipsec_get_number_of_phase2($_POST['ikeid']);
58
				for ($i = 0; $i < $ph2entries; $i++) {
59
					$connid = escapeshellarg("con{$_POST['ikeid']}00{$i}");
60
					mwexec_bg("/usr/local/sbin/ipsec down {$connid}");
61
					mwexec_bg("/usr/local/sbin/ipsec up {$connid}");
62
				}
63
			} else {
64
				mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_POST['ikeid']));
65
				mwexec_bg("/usr/local/sbin/ipsec up con" . escapeshellarg($_POST['ikeid']));
66
			}
67
		}
68
	}
69
} else if ($_POST['act'] == 'ikedisconnect') {
70

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

    
82
	if (ctype_digit($id_val)) {
83
		if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
84
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($id_val) . "{" . escapeshellarg($_POST['ikesaid']) . "}");
85
		}
86
	}
87
}
88

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

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

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

    
140
			print("</td>\n");
141
			print("<td>\n");
142

    
143
			if (!empty($ikesa['local-host'])) {
144
				print(htmlspecialchars($ikesa['local-host']));
145
			} else {
146
				print(gettext("Unknown"));
147
			}
148

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

    
159
			print("</td>\n");
160
			print("<td>\n");
161

    
162
			$identity = "";
163
			if (!empty($ikesa['remote-id'])) {
164
				if ($ikesa['remote-id'] == '%any') {
165
					$identity = htmlspecialchars(gettext('Any identifier'));
166
				} else {
167
					$identity = htmlspecialchars($ikesa['remote-id']);
168
				}
169
			}
170

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

    
185
			print("</td>\n");
186
			print("<td>\n");
187

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

    
203
			print("</td>\n");
204
			print("<td>\n");
205
			print("IKEv" . htmlspecialchars($ikesa['version']));
206
			print("<br/>\n");
207

    
208
			if ($ikesa['initiator'] == 'yes') {
209
				print("initiator");
210
			} else {
211
				print("responder");
212
			}
213

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

    
229
			if ($ikesa['state'] == 'ESTABLISHED') {
230
				print('<span class="text-success">');
231
			} else {
232
				print('<span>');
233
			}
234

    
235
			print(ucfirst(htmlspecialchars($ikesa['state'])));
236

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

    
242
			print("</span>");
243
			print("</td>\n");
244
			print("<td>\n");
245

    
246
			if ($ikesa['state'] != 'ESTABLISHED') {
247

    
248
				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>');
249
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
250
				print(gettext("Connect VPN"));
251
				print("</a>\n");
252

    
253
			} else {
254

    
255
				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>');
256
				print('<i class="fa fa-trash icon-embed-btn"></i>');
257
				print(gettext("Disconnect"));
258
				print("</a><br />\n");
259

    
260
			}
261

    
262
			print("</td>\n");
263
			print("</tr>\n");
264
			print("<tr>\n");
265
			print("<td colspan = 10>\n");
266

    
267
			if (is_array($ikesa['child-sas']) && (count($ikesa['child-sas']) > 0)) {
268
				$child_key = "";
269
				foreach ($ikesa['child-sas'] as $key => $val){
270
					$child_key = $key;
271
					break;
272
				}
273

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

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

    
295
				foreach ($ikesa['child-sas'] as $childid => $childsa) {
296
					print("<tr>");
297
					print("<td>\n");
298

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

    
307
					print("</td>\n");
308
					print("<td>\n");
309

    
310
					if (isset($childsa['spi-in'])) {
311
						print(gettext("Local: ") . htmlspecialchars($childsa['spi-in']));
312
					}
313

    
314
					if (isset($childsa['spi-out'])) {
315
						print('<br/>' . gettext('Remote: ') . htmlspecialchars($childsa['spi-out']));
316
					}
317

    
318
					print("</td>\n");
319
					print("<td>\n");
320

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

    
329
					print("</td>\n");
330
					print("<td>\n");
331

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

    
338

    
339
					print("</td>\n");
340
					print("<td>\n");
341

    
342
					print(htmlspecialchars($childsa['encr-alg']) . '<br/>');
343
					print(htmlspecialchars($childsa['integ-alg']) . '<br/>');
344

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

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

    
353
					if (!empty($childsa['esn'])) {
354
						print(htmlspecialchars($childsa['esn']) . '<br/>');
355
					}
356

    
357
					print(gettext("IPComp: "));
358
					if (!empty($childsa['cpi-in']) || !empty($childsa['cpi-out'])) {
359
						print(htmlspecialchars($childsa['cpi-in']) . " " . htmlspecialchars($childsa['cpi-out']));
360
					} else {
361
						print(gettext('none'));
362
					}
363

    
364
					print("</td>\n");
365
					print("<td>\n");
366

    
367
					print(gettext("Bytes-In: ") . htmlspecialchars(number_format($childsa['bytes-in'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-in'])) . ')<br/>');
368
					print(gettext("Packets-In: ") . htmlspecialchars(number_format($childsa['packets-in'])) . '<br/>');
369
					print(gettext("Bytes-Out: ") . htmlspecialchars(number_format($childsa['bytes-out'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-out'])) . ')<br/>');
370
					print(gettext("Packets-Out: ") . htmlspecialchars(number_format($childsa['packets-out'])) . '<br/>');
371

    
372
					print("</td>\n");
373
					print("<td>\n");
374
					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>');
375
					print('<i class="fa fa-trash icon-embed-btn"></i>');
376
					print(gettext("Disconnect"));
377
					print("</a>\n");
378
					print("</td>\n");
379
					print("</tr>\n");
380

    
381
				}
382

    
383
				print("</tbody>\n");
384
				print("	</table>\n");
385
				print("</td>\n");
386
				print("</tr>\n");
387

    
388
			}
389

    
390
			unset($con_id);
391
		}
392

    
393
	}
394

    
395
	$rgmap = array();
396
	if (is_array($a_phase1)) {
397
		foreach ($a_phase1 as $ph1ent) {
398
			if (isset($ph1ent['disabled'])) {
399
				continue;
400
			}
401

    
402
			$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
403

    
404
			if ($ipsecconnected[$ph1ent['ikeid']]) {
405
				continue;
406
			}
407

    
408
			print("<tr>\n");
409
			print("<td>\n");
410

    
411
			print(htmlspecialchars($ph1ent['descr']));
412
			print("</td>\n");
413
			print("<td>\n");
414
			list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
415

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

    
422
			print("</td>\n");
423
			print("<td>\n");
424
			$ph1src = ipsec_get_phase1_src($ph1ent);
425

    
426
			if (empty($ph1src)) {
427
				print(gettext("Unknown"));
428
			} else {
429
				print(htmlspecialchars($ph1src));
430
			}
431

    
432
			print("</td>\n");
433
			print("<td>\n");
434

    
435
			list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
436

    
437
			if (empty($peerid_data)) {
438
				print(gettext("Unknown"));
439
			} else {
440
				print(htmlspecialchars($peerid_data));
441
			}
442
			print("			</td>\n");
443
			print("			<td>\n");
444
			$ph1src = ipsec_get_phase1_dst($ph1ent);
445

    
446
			if (empty($ph1src)) {
447
				print(gettext("Unknown"));
448
			} else {
449
				print(htmlspecialchars($ph1src));
450
			}
451

    
452
			print("</td>\n");
453
			print("<td>\n");
454
			print("</td>\n");
455
			print("<td>\n");
456
			print("</td>\n");
457
			print("<td>\n");
458
			print("</td>\n");
459

    
460
			if (isset($ph1ent['mobile'])) {
461

    
462
				print("<td>\n");
463
				print(gettext("Awaiting connections"));
464
				print("</td>\n");
465
				print("<td>\n");
466
				print("</td>\n");
467
				print("</td>\n");
468
			} else {
469

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

    
480
			}
481
			print("</tr>\n");
482
		}
483
	}
484

    
485
	unset($ipsecconnected, $phase1, $rgmap);
486
}
487

    
488
$pgtitle = array(gettext("Status"), gettext("IPsec"), gettext("Overview"));
489
$pglinks = array("", "@self", "@self");
490
$shortcut_section = "ipsec";
491

    
492
include("head.inc");
493

    
494
$tab_array = array();
495
$tab_array[] = array(gettext("Overview"), true, "status_ipsec.php");
496
$tab_array[] = array(gettext("Leases"), false, "status_ipsec_leases.php");
497
$tab_array[] = array(gettext("SADs"), false, "status_ipsec_sad.php");
498
$tab_array[] = array(gettext("SPDs"), false, "status_ipsec_spd.php");
499
display_top_tabs($tab_array);
500
?>
501

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

    
531
<?php
532
unset($status);
533

    
534
if (ipsec_enabled()) {
535
	print('<div class="infoblock">');
536
} else {
537
	print('<div class="infoblock blockopen">');
538
}
539

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

    
544
<script type="text/javascript">
545
//<![CDATA[
546

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

    
557
		ajax_lock = true;
558

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

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

    
587
			$('#ipsec-body').html(response);
588
			ajax_lock = false;
589

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

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

    
599
				if (sa_open[sa_idx]) {
600
					show_childsa(sa_idx);
601
				}
602
			});
603

    
604
			// re-attached the GET to POST handler
605
			interceptGET();
606

    
607
			// and do it again
608
			setTimeout(update_table, 5000);
609
		});
610
	}
611

    
612
	function show_childsa(said) {
613
		sa_open[said] = true;
614
		$('#childsa-con' + said).show();
615
		$('#btnchildsa-con' + said).hide();
616
	}
617

    
618
	// Populate the tbody on page load
619
	update_table();
620
});
621
//]]>
622
</script>
623

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