Project

General

Profile

Download (16.9 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 ($_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
	if (ctype_digit($_POST['ikeid'])) {
67
		if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
68
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_POST['ikeid']) . "[" . escapeshellarg($_POST['ikesaid']) . "]");
69
		} else {
70
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_POST['ikeid']));
71
		}
72
	}
73
} else if ($_POST['act'] == 'childdisconnect') {
74
	if (ctype_digit($_POST['ikeid'])) {
75
		if (!empty($_POST['ikesaid']) && ctype_digit($_POST['ikesaid'])) {
76
			mwexec_bg("/usr/local/sbin/ipsec down con" . escapeshellarg($_POST['ikeid']) . "{" . escapeshellarg($_POST['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
	if (is_array($status)) {
89
		foreach ($status as $ikeid => $ikesa) {
90
			$con_id = substr($ikesa['con-id'],3);
91

    
92
			if ($ikesa['version'] == 1) {
93
				$ph1idx = substr($con_id, 0, strrpos(substr($con_id, 0, -1), '00'));
94
				$ipsecconnected[$ph1idx] = $ph1idx;
95
			} else {
96
				if (!ipsec_ikeid_used($con_id)) {
97
					// probably a v2 with split connection then
98
					$ph1idx = substr($con_id, 0, strrpos(substr($con_id, 0, -1), '00'));
99
					$ipsecconnected[$ph1idx] = $ph1idx;
100
				} else {
101
					$ipsecconnected[$con_id] = $ph1idx = $con_id;
102
				}
103
			}
104

    
105
			print("<tr>\n");
106
			print("<td>\n");
107
			print(htmlspecialchars(ipsec_get_descr($ph1idx)));
108
			print("</td>\n");
109
			print("<td>\n");
110

    
111
			if (!empty($ikesa['local-id'])) {
112
				if ($ikesa['local-id'] == '%any') {
113
					print(gettext('Any identifier'));
114
				} else {
115
					print(htmlspecialchars($ikesa['local-id']));
116
				}
117
			} else {
118
				print(gettext("Unknown"));
119
			}
120

    
121
			print("</td>\n");
122
			print("<td>\n");
123

    
124
			if (!empty($ikesa['local-host'])) {
125
				print(htmlspecialchars($ikesa['local-host']));
126
			} else {
127
				print(gettext("Unknown"));
128
			}
129

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

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

    
143
			$identity = "";
144
			if (!empty($ikesa['remote-id'])) {
145
				if ($ikesa['remote-id'] == '%any') {
146
					$identity = htmlspecialchars(gettext('Any identifier'));
147
				} else {
148
					$identity = htmlspecialchars($ikesa['remote-id']);
149
				}
150
			}
151

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

    
166
			print("</td>\n");
167
			print("<td>\n");
168

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

    
184
			print("</td>\n");
185
			print("<td>\n");
186
			print("IKEv" . htmlspecialchars($ikesa['version']));
187
			print("<br/>\n");
188

    
189
			if ($ikesa['initiator'] == 'yes') {
190
				print("initiator");
191
			} else {
192
				print("responder");
193
			}
194

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

    
210
			if ($ikesa['state'] == 'ESTABLISHED') {
211
				print('<span class="text-success">');
212
			} else {
213
				print('<span>');
214
			}
215

    
216
			print(ucfirst(htmlspecialchars($ikesa['state'])));
217

    
218
			if ($ikesa['state'] == 'ESTABLISHED') {
219
				print("<br/>");
220
				printf(gettext('%1$s seconds (%2$s) ago'), htmlspecialchars($ikesa['established']), convert_seconds_to_dhms($ikesa['established']));
221
			}
222

    
223
			print("</span>");
224
			print("</td>\n");
225
			print("<td>\n");
226

    
227
			if ($ikesa['state'] != 'ESTABLISHED') {
228

    
229
				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>');
230
				print('<i class="fa fa-sign-in icon-embed-btn"></i>');
231
				print(gettext("Connect VPN"));
232
				print("</a>\n");
233

    
234
			} else {
235

    
236
				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>');
237
				print('<i class="fa fa-trash icon-embed-btn"></i>');
238
				print(gettext("Disconnect"));
239
				print("</a><br />\n");
240

    
241
			}
242

    
243
			print("</td>\n");
244
			print("</tr>\n");
245
			print("<tr>\n");
246
			print("<td colspan = 10>\n");
247

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

    
250
				print('<div>');
251
				print('<a type="button" id="btnchildsa-con'. $con_id .'-'. $ikesa['uniqueid'] .  '" class="btn btn-sm btn-info">');
252
				print('<i class="fa fa-plus-circle icon-embed-btn"></i>');
253
				print(gettext('Show child SA entries'));
254
				print("</a>\n");
255
				print("	</div>\n");
256

    
257
				print('<table class="table table-hover table-condensed" id="childsa-con'.$con_id.'-' . $ikesa['uniqueid'] . '" style="display:none">');
258
				print("<thead>\n");
259
				print('<tr class="bg-info">');
260
				print('<th><?=gettext("Local subnets")?></th>');
261
				print('<th><?=gettext("Local SPI(s)")?></th>');
262
				print('<th><?=gettext("Remote subnets")?></th>');
263
				print('<th><?=gettext("Times")?></th>');
264
				print('<th><?=gettext("Algo")?></th>');
265
				print('<th><?=gettext("Stats")?></th>');
266
				print('<th><!-- Buttons --></th>');
267
				print("</tr\n");
268
				print("</thead>\n");
269
				print("<tbody>\n");
270

    
271
				foreach ($ikesa['child-sas'] as $childid => $childsa) {
272
					print("<tr>");
273
					print("<td>\n");
274

    
275
					if (is_array($childsa['local-ts'])) {
276
						foreach ($childsa['local-ts'] as $lnets) {
277
							print(htmlspecialchars(ipsec_fixup_network($lnets)) . "<br />");
278
						}
279
					} else {
280
						print(gettext("Unknown"));
281
					}
282

    
283
					print("</td>\n");
284
					print("<td>\n");
285

    
286
					if (isset($childsa['spi-in'])) {
287
						print(gettext("Local: ") . htmlspecialchars($childsa['spi-in']));
288
					}
289

    
290
					if (isset($childsa['spi-out'])) {
291
						print('<br/>' . gettext('Remote: ') . htmlspecialchars($childsa['spi-out']));
292
					}
293

    
294
					print("</td>\n");
295
					print("<td>\n");
296

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

    
305
					print("</td>\n");
306
					print("<td>\n");
307

    
308
					printf(gettext('Rekey: %1$s seconds (%2$s)'), htmlspecialchars($childsa['rekey-time']), convert_seconds_to_dhms($childsa['rekey-time']));
309
					print('<br/>');
310
					printf(gettext('Life: %1$s seconds (%2$s)'), htmlspecialchars($childsa['life-time']), convert_seconds_to_dhms($childsa['life-time']));
311
					print('<br/>');
312
					printf(gettext('Install: %1$s seconds (%2$s)'), htmlspecialchars($childsa['install-time']), convert_seconds_to_dhms($childsa['install-time']));
313

    
314

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

    
318
					print(htmlspecialchars($childsa['encr-alg']) . '<br/>');
319
					print(htmlspecialchars($childsa['integ-alg']) . '<br/>');
320

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

    
325
					if (!empty($childsa['dh-group'])) {
326
						print(htmlspecialchars($childsa['dh-group']) . '<br/>');
327
					}
328

    
329
					if (!empty($childsa['esn'])) {
330
						print(htmlspecialchars($childsa['esn']) . '<br/>');
331
					}
332

    
333
					print(gettext("IPComp: "));
334
					if (!empty($childsa['cpi-in']) || !empty($childsa['cpi-out'])) {
335
						print(htmlspecialchars($childsa['cpi-in']) . " " . htmlspecialchars($childsa['cpi-out']));
336
					} else {
337
						print(gettext('none'));
338
					}
339

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

    
343
					print(gettext("Bytes-In: ") . htmlspecialchars(number_format($childsa['bytes-in'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-in'])) . ')<br/>');
344
					print(gettext("Packets-In: ") . htmlspecialchars(number_format($childsa['packets-in'])) . '<br/>');
345
					print(gettext("Bytes-Out: ") . htmlspecialchars(number_format($childsa['bytes-out'])) . ' (' . htmlspecialchars(format_bytes($childsa['bytes-out'])) . ')<br/>');
346
					print(gettext("Packets-Out: ") . htmlspecialchars(number_format($childsa['packets-out'])) . '<br/>');
347

    
348
					print("</td>\n");
349
					print("<td>\n");
350
					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') . '" usepost>');
351
					print('<i class="fa fa-trash icon-embed-btn"></i>');
352
					print(gettext("Disconnect"));
353
					print("</a>\n");
354
					print("</td>\n");
355
					print("</tr>\n");
356

    
357
				}
358

    
359
				print("</tbody>\n");
360
				print("	</table>\n");
361
				print("</td>\n");
362
				print("</tr>\n");
363

    
364
			}
365

    
366
			unset($con_id);
367
		}
368

    
369
	}
370

    
371
	$rgmap = array();
372
	if (is_array($a_phase1)) {
373
		foreach ($a_phase1 as $ph1ent) {
374
			if (isset($ph1ent['disabled'])) {
375
				continue;
376
			}
377

    
378
			$rgmap[$ph1ent['remote-gateway']] = $ph1ent['remote-gateway'];
379

    
380
			if ($ipsecconnected[$ph1ent['ikeid']]) {
381
				continue;
382
			}
383

    
384
			print("<tr>\n");
385
			print("<td>\n");
386

    
387
			print(htmlspecialchars($ph1ent['descr']));
388
			print("</td>\n");
389
			print("<td>\n");
390
			list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
391

    
392
			if (empty($myid_data)) {
393
				print(gettext("Unknown"));
394
			} else {
395
				print(htmlspecialchars($myid_data));
396
			}
397

    
398
			print("</td>\n");
399
			print("<td>\n");
400
			$ph1src = ipsec_get_phase1_src($ph1ent);
401

    
402
			if (empty($ph1src)) {
403
				print(gettext("Unknown"));
404
			} else {
405
				print(htmlspecialchars($ph1src));
406
			}
407

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

    
411
			list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
412

    
413
			if (empty($peerid_data)) {
414
				print(gettext("Unknown"));
415
			} else {
416
				print(htmlspecialchars($peerid_data));
417
			}
418
			print("			</td>\n");
419
			print("			<td>\n");
420
			$ph1src = ipsec_get_phase1_dst($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
			print("</td>\n");
431
			print("<td>\n");
432
			print("</td>\n");
433
			print("<td>\n");
434
			print("</td>\n");
435

    
436
			if (isset($ph1ent['mobile'])) {
437

    
438
				print("<td>\n");
439
				print(gettext("Awaiting connections"));
440
				print("</td>\n");
441
				print("<td>\n");
442
				print("</td>\n");
443
				print("</td>\n");
444
			} else {
445

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

    
456
			}
457
			print("</tr>\n");
458
		}
459
	}
460

    
461
	unset($ipsecconnected, $phase1, $rgmap);
462
}
463

    
464
$pgtitle = array(gettext("Status"), gettext("IPsec"), gettext("Overview"));
465
$pglinks = array("", "@self", "@self");
466
$shortcut_section = "ipsec";
467

    
468
include("head.inc");
469

    
470
$tab_array = array();
471
$tab_array[] = array(gettext("Overview"), true, "status_ipsec.php");
472
$tab_array[] = array(gettext("Leases"), false, "status_ipsec_leases.php");
473
$tab_array[] = array(gettext("SADs"), false, "status_ipsec_sad.php");
474
$tab_array[] = array(gettext("SPDs"), false, "status_ipsec_spd.php");
475
display_top_tabs($tab_array);
476
?>
477

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

    
507
<?php
508
unset($status);
509

    
510
if (ipsec_enabled()) {
511
	print('<div class="infoblock">');
512
} else {
513
	print('<div class="infoblock blockopen">');
514
}
515

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

    
520
<script type="text/javascript">
521
//<![CDATA[
522

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

    
527
	// Fetch the tbody contents from the server
528
	function update_table() {
529
		if (ajax_lock) {
530
			return;
531
		}
532

    
533
		ajax_lock = true;
534

    
535
		ajaxRequest = $.ajax(
536
			{
537
				url: "/status_ipsec.php",
538
				type: "post",
539
				data: {
540
					ajax: 	"ajax"
541
				},
542
				tryCount : 0,
543
				retryLimit :3,
544
				error: function(xhr, textStatus, errorThrown){
545
					//alert("error.... retrying");
546
					if (this.tryCount <= this.retryLimit){
547
						ajax_lock = false;
548
						update_table();
549
					}
550
					return;
551
				}
552
			}
553
		);
554

    
555
		// Deal with the results of the above ajax call
556
		ajaxRequest.done(function (response, textStatus, jqXHR) {
557

    
558
			if (!response) {
559
				response = '<tr><td colspan="10"><?=print_info_box(gettext("No IPsec status information available."), "warning", "")?></td></tr>';
560
			}
561

    
562
			$('#ipsec-body').html(response);
563
			ajax_lock = false;
564

    
565
			// Update "Show child SA" handlers
566
			$('[id^=btnchildsa-]').click(function () {
567
				show_childsa($(this).prop("id").replace( /^\D+/g, ''));
568
			});
569

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

    
574
				if (sa_open[sa_idx]) {
575
					show_childsa(sa_idx);
576
				}
577
			});
578

    
579
			// re-attached the GET to POST handler
580
			interceptGET();
581

    
582
			// and do it again
583
			setTimeout(update_table, 5000);
584
		});
585
	}
586

    
587
	function show_childsa(said) {
588
		sa_open[said] = true;
589
		$('#childsa-con' + said).show();
590
		$('#btnchildsa-con' + said).hide();
591
	}
592

    
593
	// Populate the tbody on page load
594
	update_table();
595
});
596
//]]>
597
</script>
598

    
599
<?php
600
include("foot.inc"); ?>
(169-169/230)