Project

General

Profile

Download (91.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * ipsec.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008 Shrew Soft Inc.
7
 * Copyright (c) 2007-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
require_once("filter.inc");
26
require_once("auth.inc");
27
require_once("certs.inc");
28
require_once("interfaces.inc");
29

    
30
/* IPsec defines */
31
global $ipsec_loglevels;
32
$ipsec_loglevels = array(
33
	"dmn" => gettext("Daemon"),
34
	"mgr" => gettext("SA Manager"),
35
	"ike" => gettext("IKE SA"),
36
	"chd" => gettext("IKE Child SA"),
37
	"job" => gettext("Job Processing"),
38
	"cfg" => gettext("Configuration backend"),
39
	"knl" => gettext("Kernel Interface"),
40
	"net" => gettext("Networking"),
41
	"asn" => gettext("ASN encoding"),
42
	"enc" => gettext("Message encoding"),
43
	"imc" => gettext("Integrity checker"),
44
	"imv" => gettext("Integrity Verifier"),
45
	"pts" => gettext("Platform Trust Service"),
46
	"tls" => gettext("TLS handler"),
47
	"esp" => gettext("IPsec traffic"),
48
	"lib" => gettext("StrongSwan Lib")
49
);
50

    
51
global $ipsec_log_sevs;
52
$ipsec_log_sevs = array(
53
	'-1' => gettext('Silent'),
54
	'0' => gettext('Audit'),
55
	'1' => gettext('Control'),
56
	'2' => gettext('Diag'),
57
	'3' => gettext('Raw'),
58
	'4' => gettext('Highest')
59
);
60

    
61
global $ipsec_log_cats;
62
$ipsec_log_cats = array(
63
	"dmn" => gettext("Daemon"),
64
	"mgr" => gettext("SA Manager"),
65
	"ike" => gettext("IKE SA"),
66
	"chd" => gettext("IKE Child SA"),
67
	"job" => gettext("Job Processing"),
68
	"cfg" => gettext("Configuration backend"),
69
	"knl" => gettext("Kernel Interface"),
70
	"net" => gettext("Networking"),
71
	"asn" => gettext("ASN encoding"),
72
	"enc" => gettext("Message encoding"),
73
	"imc" => gettext("Integrity checker"),
74
	"imv" => gettext("Integrity Verifier"),
75
	"pts" => gettext("Platform Trust Service"),
76
	"tls" => gettext("TLS handler"),
77
	"esp" => gettext("IPsec traffic"),
78
	"lib" => gettext("StrongSwan Lib")
79
);
80

    
81
global $ipsec_identifier_list;
82
$ipsec_identifier_list = array(
83
	// 'ipv4' => array('desc' => gettext('IPv4 address'), 'mobile' => true),
84
	// 'ipv6' => array('desc' => gettext('IPv6 address'), 'mobile' => true),
85
	// 'rfc822' => array('desc' => gettext('RFC822'), 'mobile' => true),
86
	'none' => array('desc' => '', 'mobile' => true),
87
	'email' => array('desc' => gettext('E-mail address'), 'mobile' => true),
88
	'userfqdn' => array('desc' => gettext('User Fully Qualified Domain Name'), 'mobile' => true)
89
	// 'fqdn' => array('desc' => gettext('Fully Qualified Domain Name'), 'mobile' => true),
90
	// 'dns' => array('desc' => gettext('DNS'), 'mobile' => true),
91
	// 'asn1dn' => array('desc' => gettext('ASN.1 Distinguished Name'), 'mobile' => true),
92
	// 'asn1gn' => array('desc' => gettext('ASN.1 GN'), 'mobile' => true),
93
	// 'keyid' => array('desc' => gettext('KeyID'), 'mobile' => true)
94
);
95

    
96
global $my_identifier_list;
97
$my_identifier_list = array(
98
	'myaddress' => array('desc' => gettext('My IP address'), 'mobile' => true),
99
	'address' => array('desc' => gettext('IP address'), 'mobile' => true),
100
	'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
101
	'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
102
	'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
103
	'keyid tag' => array('desc' => gettext('KeyID tag'), 'mobile' => true),
104
	'dyn_dns' => array('desc' => gettext('Dynamic DNS'), 'mobile' => true)
105
);
106

    
107
global $peer_identifier_list;
108
$peer_identifier_list = array(
109
	'any' => array('desc' => gettext('Any'), 'mobile' => true),
110
	'peeraddress' => array('desc' => gettext('Peer IP address'), 'mobile' => false),
111
	'address' => array('desc' => gettext('IP address'), 'mobile' => false),
112
	'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
113
	'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
114
	'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
115
	'keyid tag' => array('desc' =>gettext('KeyID tag'), 'mobile' => true)
116
);
117

    
118
global $ipsec_idhandling;
119
$ipsec_idhandling = array(
120
	'replace' => 'Yes (Replace)', 'no' => 'No', 'never' => 'Never', 'keep' => 'Keep'
121
);
122

    
123
global $p1_ealgos;
124
$p1_ealgos = array(
125
	'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
126
	'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
127
	'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
128
	'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
129
	'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
130
	'3des' => array('name' => '3DES'),
131
	'cast128' => array('name' => 'CAST128')
132
);
133

    
134
global $p2_ealgos;
135
$p2_ealgos = array(
136
	'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
137
	'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
138
	'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
139
	'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
140
	'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
141
	'3des' => array('name' => '3DES'),
142
	'cast128' => array('name' => 'CAST128')
143
);
144

    
145
global $p1_halgos;
146
$p1_halgos = array(
147
	'md5' => 'MD5',
148
	'sha1' => 'SHA1',
149
	'sha256' => 'SHA256',
150
	'sha384' => 'SHA384',
151
	'sha512' => 'SHA512',
152
	'aesxcbc' => 'AES-XCBC'
153
);
154

    
155
global $p1_dhgroups;
156
$p1_dhgroups = array(
157
	1  => '1 (768 bit)',
158
	2  => '2 (1024 bit)',
159
	5  => '5 (1536 bit)',
160
	14 => '14 (2048 bit)',
161
	15 => '15 (3072 bit)',
162
	16 => '16 (4096 bit)',
163
	17 => '17 (6144 bit)',
164
	18 => '18 (8192 bit)',
165
	19 => '19 (nist ecp256)',
166
	20 => '20 (nist ecp384)',
167
	21 => '21 (nist ecp521)',
168
	22 => '22 (1024(sub 160) bit)',
169
	23 => '23 (2048(sub 224) bit)',
170
	24 => '24 (2048(sub 256) bit)',
171
	25 => '25 (nist ecp192)',
172
	26 => '26 (nist ecp224)',
173
	27 => '27 (brainpool ecp224)',
174
	28 => '28 (brainpool ecp256)',
175
	29 => '29 (brainpool ecp384)',
176
	30 => '30 (brainpool ecp512)',
177
	31 => '31 (Elliptic Curve 25519, 256 bit)',
178
	32 => '32 (Elliptic Curve 25519, 448 bit)',
179
);
180

    
181
global $p2_halgos;
182
$p2_halgos = array(
183
	'hmac_md5' => 'MD5',
184
	'hmac_sha1' => 'SHA1',
185
	'hmac_sha256' => 'SHA256',
186
	'hmac_sha384' => 'SHA384',
187
	'hmac_sha512' => 'SHA512',
188
	'aesxcbc' => 'AES-XCBC'
189
);
190

    
191
global $p1_authentication_methods;
192
$p1_authentication_methods = array(
193
	'hybrid_cert_server' => array('name' => gettext('Hybrid Certificate + Xauth'), 'mobile' => true),
194
	'xauth_cert_server' => array('name' => gettext('Mutual Certificate + Xauth'), 'mobile' => true),
195
	'xauth_psk_server' => array('name' => gettext('Mutual PSK + Xauth'), 'mobile' => true),
196
	'eap-tls' => array('name' => gettext('EAP-TLS'), 'mobile' => true),
197
	'eap-radius' => array('name' => gettext('EAP-RADIUS'), 'mobile' => true),
198
	'eap-mschapv2' => array('name' => gettext('EAP-MSChapv2'), 'mobile' => true),
199
	'cert' => array('name' => gettext('Mutual Certificate'), 'mobile' => false),
200
	'pkcs11' => array('name' => gettext('Mutual Certificate (PKCS#11)'), 'mobile' => false),
201
	'pre_shared_key' => array('name' => gettext('Mutual PSK'), 'mobile' => false)
202
);
203

    
204
global $ipsec_preshared_key_type;
205
$ipsec_preshared_key_type = array(
206
	'PSK' => 'PSK',
207
	'EAP' => 'EAP'
208
);
209

    
210
global $ipsec_closeactions;
211
$ipsec_closeactions = array(
212
	'' => gettext('Default'),
213
	'none' => gettext('Close connection and clear SA'),
214
	'start' => gettext('Restart/Reconnect'),
215
	'trap' => gettext('Close connection and reconnect on demand'),
216
);
217

    
218
global $p2_modes;
219
$p2_modes = array(
220
	'tunnel' => gettext('Tunnel IPv4'),
221
	'tunnel6' => gettext('Tunnel IPv6'),
222
	'transport' => gettext('Transport'),
223
	'vti' => gettext('Routed (VTI)')
224
);
225

    
226
global $p2_protos;
227
$p2_protos = array(
228
	'esp' => 'ESP',
229
	'ah' => 'AH'
230
);
231

    
232
global $p2_pfskeygroups;
233
$p2_pfskeygroups = array(
234
	0 => gettext('off'),
235
	1  => gettext('1 (768 bit)'),
236
	2  => gettext('2 (1024 bit)'),
237
	5  => gettext('5 (1536 bit)'),
238
	14 => gettext('14 (2048 bit)'),
239
	15 => gettext('15 (3072 bit)'),
240
	16 => gettext('16 (4096 bit)'),
241
	17 => gettext('17 (6144 bit)'),
242
	18 => gettext('18 (8192 bit)'),
243
	19 => gettext('19 (nist ecp256)'),
244
	20 => gettext('20 (nist ecp384)'),
245
	21 => gettext('21 (nist ecp521)'),
246
	22 => gettext('22 (1024(sub 160) bit)'),
247
	23 => gettext('23 (2048(sub 224) bit)'),
248
	24 => gettext('24 (2048(sub 256) bit)'),
249
	25 => gettext('25 (nist ecp192)'),
250
	26 => gettext('26 (nist ecp224)'),
251
	27 => gettext('27 (brainpool ecp224)'),
252
	28 => gettext('28 (brainpool ecp256)'),
253
	29 => gettext('29 (brainpool ecp384)'),
254
	30 => gettext('30 (brainpool ecp512)'),
255
	31 => gettext('31 (Elliptic Curve 25519, 256 bit)'),
256
	32 => gettext('32 (Elliptic Curve 25519, 448 bit)'),
257
);
258

    
259
function ipsec_enabled() {
260
	global $config;
261

    
262
	if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
263
		return false;
264
	}
265

    
266
	/* Check if we have at least one phase 1 entry. */
267
	if (!isset($config['ipsec']['phase1']) ||
268
	    !is_array($config['ipsec']['phase1']) ||
269
	    empty($config['ipsec']['phase1'])) {
270
		return false;
271
	}
272
	/* Check if at least one phase 1 entry is enabled. */
273
	foreach ($config['ipsec']['phase1'] as $phase1) {
274
		if (!isset($phase1['disabled'])) {
275
			return true;
276
		}
277
	}
278

    
279
	return false;
280
}
281

    
282
/*
283
 * ikeid management functions
284
 */
285

    
286
function ipsec_ikeid_used($ikeid) {
287
	global $config;
288

    
289
	foreach ($config['ipsec']['phase1'] as $ph1ent) {
290
		if ($ikeid == $ph1ent['ikeid']) {
291
			return true;
292
		}
293
	}
294

    
295
	return false;
296
}
297

    
298
function ipsec_ikeid_next() {
299

    
300
	$ikeid = 1;
301
	while (ipsec_ikeid_used($ikeid)) {
302
		$ikeid++;
303
	}
304

    
305
	return $ikeid;
306
}
307

    
308
/*
309
 * Return phase1 local address
310
 */
311
function ipsec_get_phase1_src(& $ph1ent) {
312

    
313
	if ($ph1ent['interface']) {
314
		if (substr($ph1ent['interface'], 0, 4) == "_vip") {
315
			$if = $ph1ent['interface'];
316
		} else {
317
			$if = get_failover_interface($ph1ent['interface']);
318
		}
319
	} else {
320
		$if = "wan";
321
	}
322
	$ip6 = get_interface_ipv6($if);
323
	$ip4 = get_interface_ip($if);
324
	if ($ph1ent['protocol'] == "inet6") {
325
		$interfaceip = $ip6;
326
	} elseif ($ph1ent['protocol'] == "inet") {
327
		$interfaceip = $ip4;
328
	} elseif ($ph1ent['protocol'] == "both") {
329
		$ifips = array();
330
		if (!empty($ip4)) {
331
			$ifips[] = $ip4;
332
		}
333
		if (!empty($ip6)) {
334
			$ifips[] = $ip6;
335
		}
336
		$interfaceip = implode(',', $ifips);
337
	}
338

    
339
	return $interfaceip;
340
}
341

    
342
/*
343
 * Return phase1 local address
344
 */
345
function ipsec_get_phase1_dst(& $ph1ent) {
346
	global $g;
347

    
348
	if (empty($ph1ent['remote-gateway'])) {
349
		return false;
350
	}
351
	$rg = $ph1ent['remote-gateway'];
352
	if (!is_ipaddr($rg)) {
353
		if (!platform_booting()) {
354
			return resolve_retry($rg, $ph1ent['protocol']);
355
		}
356
	}
357
	if (!is_ipaddr($rg)) {
358
		return false;
359
	}
360

    
361
	return $rg;
362
}
363

    
364
/*
365
 * Return phase2 idinfo in cidr format
366
 */
367
function ipsec_idinfo_to_cidr(& $idinfo, $addrbits = false, $mode = "") {
368
	global $config;
369

    
370
	switch ($idinfo['type']) {
371
		case "address":
372
			if ($addrbits) {
373
				if ($mode == "tunnel6") {
374
					return $idinfo['address']."/128";
375
				} elseif (($mode == "vti") && is_ipaddrv4($idinfo['address'])) {
376
					return $idinfo['address']."/30";
377
				} elseif (($mode == "vti") && is_ipaddrv6($idinfo['address'])) {
378
					return $idinfo['address']."/64";
379
				} else {
380
					return $idinfo['address']."/32";
381
				}
382
			} else {
383
				return $idinfo['address'];
384
			}
385
			break; /* NOTREACHED */
386
		case "network":
387
			return "{$idinfo['address']}/{$idinfo['netbits']}";
388
			break; /* NOTREACHED */
389
		case "none":
390
		case "mobile":
391
			return '0.0.0.0/0';
392
			break; /* NOTREACHED */
393
		default:
394
			if (empty($mode) && !empty($idinfo['mode'])) {
395
				$mode = $idinfo['mode'];
396
			}
397

    
398
			if ($mode == "tunnel6") {
399
				$address = get_interface_ipv6($idinfo['type']);
400
				$netbits = get_interface_subnetv6($idinfo['type']);
401
				$address = gen_subnetv6($address, $netbits);
402
				return "{$address}/{$netbits}";
403
			} else {
404
				$address = get_interface_ip($idinfo['type']);
405
				$netbits = get_interface_subnet($idinfo['type']);
406
				$address = gen_subnet($address, $netbits);
407
				return "{$address}/{$netbits}";
408
			}
409
			break; /* NOTREACHED */
410
	}
411
}
412

    
413
/*
414
 * Return phase2 idinfo in address/netmask format
415
 */
416
function ipsec_idinfo_to_subnet(& $idinfo, $addrbits = false) {
417
	global $config;
418

    
419
	switch ($idinfo['type']) {
420
		case "address":
421
			if ($addrbits) {
422
				if ($idinfo['mode'] == "tunnel6") {
423
					return $idinfo['address']."/128";
424
				} else {
425
					return $idinfo['address']."/255.255.255.255";
426
				}
427
			} else {
428
				return $idinfo['address'];
429
			}
430
			break; /* NOTREACHED */
431
		case "none":
432
		case "network":
433
			return $idinfo['address']."/".gen_subnet_mask($idinfo['netbits']);
434
			break; /* NOTREACHED */
435
		case "mobile":
436
			return "0.0.0.0/0";
437
			break; /* NOTREACHED */
438
		default:
439
			if ($idinfo['mode'] == "tunnel6") {
440
				$address = get_interface_ipv6($idinfo['type']);
441
				$netbits = get_interface_subnetv6($idinfo['type']);
442
				$address = gen_subnetv6($address, $netbits);
443
				return $address."/".$netbits;
444
			} else {
445
				$address = get_interface_ip($idinfo['type']);
446
				$netbits = get_interface_subnet($idinfo['type']);
447
				$address = gen_subnet($address, $netbits);
448
				return $address."/".$netbits;
449
			}
450
			break; /* NOTREACHED */
451
	}
452
}
453

    
454
/*
455
 *  Return phase2 idinfo in text format
456
 */
457
function ipsec_idinfo_to_text(& $idinfo) {
458
	global $config;
459

    
460
	switch ($idinfo['type']) {
461
		case "address":
462
			return $idinfo['address'];
463
			break; /* NOTREACHED */
464
		case "network":
465
			return $idinfo['address']."/".$idinfo['netbits'];
466
			break; /* NOTREACHED */
467
		case "mobile":
468
			return gettext("Mobile Client");
469
			break; /* NOTREACHED */
470
		case "none":
471
			return gettext("None");
472
			break; /* NOTREACHED */
473
		default:
474
			if (!empty($config['interfaces'][$idinfo['type']])) {
475
				return convert_friendly_interface_to_friendly_descr($idinfo['type']);
476
			} else {
477
				return strtoupper($idinfo['type']);
478
			}
479
			break; /* NOTREACHED */
480
	}
481
}
482

    
483
/*
484
 * Return phase1 association for phase2
485
 */
486
function ipsec_lookup_phase1(& $ph2ent, & $ph1ent) {
487
	global $config;
488

    
489
	if (!is_array($config['ipsec'])) {
490
		return false;
491
	}
492
	if (!is_array($config['ipsec']['phase1'])) {
493
		return false;
494
	}
495
	if (empty($config['ipsec']['phase1'])) {
496
		return false;
497
	}
498

    
499
	foreach ($config['ipsec']['phase1'] as $ph1tmp) {
500
		if ($ph1tmp['ikeid'] == $ph2ent['ikeid']) {
501
			$ph1ent = $ph1tmp;
502
			return $ph1ent;
503
		}
504
	}
505

    
506
	return false;
507
}
508

    
509
/*
510
 * Check phase1 communications status
511
 */
512
function ipsec_phase1_status(&$ipsec_status, $ikeid) {
513

    
514
	foreach ($ipsec_status as $ike) {
515
		if ($ike['id'] == $ikeid) {
516
			if ($ike['status'] == 'established') {
517
				return true;
518
			}
519
		}
520
	}
521

    
522
	return false;
523
}
524

    
525
/*
526
 * Check phase2 communications status
527
 */
528
function ipsec_phase2_status(&$ipsec_status, &$phase2) {
529

    
530
	if (ipsec_lookup_phase1($ph2ent, $ph1ent)) {
531
		return ipsec_phase1_status($ipsec_status, $ph1ent['ikeid']);
532
	}
533

    
534
	return false;
535
}
536

    
537
/*
538
 * Wrapper to call pfSense_ipsec_list_sa() when IPsec is enabled
539
 */
540
function ipsec_list_sa() {
541

    
542
	if (ipsec_enabled()) {
543
		return pfSense_ipsec_list_sa();
544
	}
545

    
546
	return array();
547
}
548

    
549
/*
550
 * Return dump of SPD table
551
 */
552
function ipsec_dump_spd() {
553
	$fd = @popen("/sbin/setkey -DP", "r");
554
	$spd = array();
555
	if ($fd) {
556
		while (!feof($fd)) {
557
			$line = chop(fgets($fd));
558
			if (!$line) {
559
				continue;
560
			}
561
			if ($line == "No SPD entries.") {
562
				break;
563
			}
564
			if ($line[0] != "\t") {
565
				if (is_array($cursp)) {
566
					$spd[] = $cursp;
567
				}
568
				$cursp = array();
569
				$linea = explode(" ", $line);
570
				$cursp['srcid'] = substr($linea[0], 0, strpos($linea[0], "["));
571
				$cursp['dstid'] = substr($linea[1], 0, strpos($linea[1], "["));
572
				$i = 0;
573
			} else if (is_array($cursp)) {
574
				$line = trim($line, "\t\r\n ");
575
				$linea = explode(" ", $line);
576
				switch ($i) {
577
					case 1:
578
						if ($linea[1] == "none")	/* don't show default anti-lockout rule */ {
579
							unset($cursp);
580
						} else {
581
							$cursp['dir'] = $linea[0];
582
						}
583
						break;
584
					case 2:
585
						$upperspec = explode("/", $linea[0]);
586
						$cursp['proto'] = $upperspec[0];
587
						list($cursp['src'], $cursp['dst']) = explode("-", $upperspec[2]);
588
						$cursp['reqid'] = substr($upperspec[3], strpos($upperspec[3], "#")+1);
589
						break;
590
				}
591
			}
592
			$i++;
593
		}
594
		if (is_array($cursp) && count($cursp)) {
595
			$spd[] = $cursp;
596
		}
597
		pclose($fd);
598
	}
599

    
600
	return $spd;
601
}
602

    
603
/*
604
 * Return dump of SAD table
605
 */
606
function ipsec_dump_sad() {
607
	$fd = @popen("/sbin/setkey -D", "r");
608
	$sad = array();
609
	if ($fd) {
610
		while (!feof($fd)) {
611
			$line = chop(fgets($fd));
612
			if (!$line || $line[0] == " ") {
613
				continue;
614
			}
615
			if ($line == "No SAD entries.") {
616
				break;
617
			}
618
			if ($line[0] != "\t") {
619
				if (is_array($cursa)) {
620
					$sad[] = $cursa;
621
				}
622
				$cursa = array();
623
				list($cursa['src'], $cursa['dst']) = explode(" ", $line);
624
			} else {
625
				$line = trim($line, "\t\n\r ");
626
				$linea = explode(" ", $line);
627
				foreach ($linea as $idx => $linee) {
628
					if ($linee == 'esp' || $linee == 'ah' || $linee[0] == '#') {
629
						$cursa['proto'] = $linee;
630
					} else if (substr($linee, 0, 3) == 'spi') {
631
						$cursa['spi'] = substr($linee, strpos($linee, 'x') + 1, -1);
632
					} else if (substr($linee, 0, 5) == 'reqid') {
633
						$cursa['reqid'] = substr($linee, strpos($linee, 'x') + 1, -1);
634
					} else if (substr($linee, 0, 2) == 'E:') {
635
						$cursa['ealgo'] = $linea[$idx + 1];
636
						break;
637
					} else if (substr($linee, 0, 2) == 'A:') {
638
						$cursa['aalgo'] = $linea[$idx + 1];
639
						break;
640
					} else if (substr($linee, 0, 8) == 'current:') {
641
						$cursa['data'] = substr($linea[$idx + 1], 0, strpos($linea[$idx + 1], 'bytes') - 1) . ' B';
642
						break;
643
					}
644
				}
645
			}
646
		}
647
		if (is_array($cursa) && count($cursa)) {
648
			$sad[] = $cursa;
649
		}
650
		pclose($fd);
651
	}
652

    
653
	return $sad;
654
}
655

    
656
/*
657
 * Return dump of mobile user list
658
 */
659
function ipsec_dump_mobile() {
660
	global $g, $config;
661

    
662
	if(!isset($config['ipsec']['client']['enable'])) {
663
		return array();
664
	}
665

    
666
	/* Fetch the pool contents and leases from swanctl. */
667
	$_gb = exec("/usr/local/sbin/swanctl --list-pools --leases 2>/dev/null", $output, $rc);
668

    
669
	if ($rc != 0) {
670
		log_error(gettext("Unable to find IPsec daemon leases file. Could not display mobile user stats!"));
671
		return array();
672
	}
673

    
674
	$response = array(
675
		'pool' => array(),
676
	);
677

    
678
	/* swanctl --list-pools --leases example output, field names can be confirmed by
679
	 * looking at --raw or --pretty output. */
680
	/* mobile-pool          10.7.200.0                          0 / 1 / 254 */
681
	$pool_regex='/^(?P<name>\S+)\s+(?P<base>\S+)\s+(?P<online>\d+) \/ (?P<offline>\d+) \/ (?P<size>\d+)/';
682
	/*   10.7.200.1                     online   'jimp' */
683
	$lease_regex='/\s*(?P<host>[\d\.]+)\s+(?P<status>online|offline)\s+\'(?P<id>.*)\'/';
684
	foreach ($output as $line) {
685
		if (preg_match($pool_regex, $line, $matches)) {
686
			$id++;
687
			$response['pool'][$id] = array(
688
			    'name'   => $matches['name'],
689
			    'base'   => $matches['base'],
690
			    'online' => $matches['online'],
691
			    'offline'  => $matches['offline'],
692
			    'size'   => $matches['size'],
693
			);
694
		} elseif (preg_match($lease_regex, $line, $matches)) {
695
			$response['pool'][$id]['lease'][] = array(
696
			    'host'   => $matches['host'],
697
			    'status' => $matches['status'],
698
			    'id'     => $matches['id']
699
			);
700
		}
701
	}
702

    
703
	unset($_gb, $output, $rc, $pool_regex);
704

    
705
	return $response;
706
}
707

    
708
function ipsec_mobilekey_sort() {
709
	global $config;
710

    
711
	function mobilekeycmp($a, $b) {
712
		return strcmp($a['ident'][0], $b['ident'][0]);
713
	}
714

    
715
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
716
}
717

    
718
function ipsec_get_number_of_phase2($ikeid) {
719
	global $config;
720
	$a_phase2 = $config['ipsec']['phase2'];
721

    
722
	$nbph2 = 0;
723

    
724
	if (is_array($a_phase2) && count($a_phase2)) {
725
		foreach ($a_phase2 as $ph2tmp) {
726
			if ($ph2tmp['ikeid'] == $ikeid) {
727
				$nbph2++;
728
			}
729
		}
730
	}
731

    
732
	return $nbph2;
733
}
734

    
735
function ipsec_get_descr($ikeid) {
736
	global $config;
737

    
738
	if (!isset($config['ipsec']['phase1']) ||
739
	    !is_array($config['ipsec']['phase1'])) {
740
		return '';
741
	}
742

    
743
	foreach ($config['ipsec']['phase1'] as $p1) {
744
		if ($p1['ikeid'] == $ikeid) {
745
			return $p1['descr'];
746
		}
747
	}
748

    
749
	return '';
750
}
751

    
752
function ipsec_get_phase1($ikeid) {
753
		global $config;
754

    
755
		if (!isset($config['ipsec']['phase1']) ||
756
		    !is_array($config['ipsec']['phase1'])) {
757
			return '';
758
		}
759

    
760
		$a_phase1 = $config['ipsec']['phase1'];
761
		foreach ($a_phase1 as $p1) {
762
			if ($p1['ikeid'] == $ikeid) {
763
				return $p1;
764
			}
765
		}
766
		unset($a_phase1);
767
}
768

    
769
function ipsec_fixup_ip($ipaddr) {
770
	if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr)) {
771
		return text_to_compressed_ip6($ipaddr);
772
	} else {
773
		return $ipaddr;
774
	}
775
}
776

    
777
function ipsec_find_id(& $ph1ent, $side = "local", $rgmap = array()) {
778
	if ($side == "local") {
779
		$id_type = $ph1ent['myid_type'];
780
		$id_data = $ph1ent['myid_data'];
781

    
782
		$addr = ipsec_get_phase1_src($ph1ent);
783
		if (!$addr) {
784
			return array();
785
		}
786
		/* When automatically guessing, use the first address. */
787
		$addr = explode(',', $addr);
788
		$addr = $addr[0];
789
	} elseif ($side == "peer") {
790
		$id_type = $ph1ent['peerid_type'];
791
		$id_data = $ph1ent['peerid_data'];
792

    
793
		if (isset($ph1ent['mobile'])) {
794
			$addr = "%any";
795
		} else {
796
			$addr = $ph1ent['remote-gateway'];
797
		}
798
	} else {
799
		return array();
800
	}
801

    
802

    
803
	$thisid_type = $id_type;
804
	switch ($thisid_type) {
805
		case 'myaddress':
806
			$thisid_type = 'address';
807
			$thisid_data = $addr;
808
			break;
809
		case 'dyn_dns':
810
			$thisid_type = 'dns';
811
			$thisid_data = $id_data;
812
			break;
813
		case 'peeraddress':
814
			$thisid_type = 'address';
815
			$thisid_data = $rgmap[$ph1ent['remote-gateway']];
816
			break;
817
		case 'address':
818
			$thisid_data = $id_data;
819
			break;
820
		case 'fqdn':
821
			$thisid_data = "{$id_data}";
822
			break;
823
		case 'keyid tag':
824
			$thisid_type = 'keyid';
825
			$thisid_data = "{$id_data}";
826
			break;
827
		case 'user_fqdn':
828
			$thisid_type = 'userfqdn';
829
			$thisid_data = "{$id_data}";
830
			break;
831
		case 'asn1dn':
832
			$thisid_data = $id_data;
833
			break;
834
	}
835
	return array($thisid_type, $thisid_data);
836
}
837

    
838
/*
839
 * Fixup ID type/data to include prefix when necessary, add quotes, etc.
840
 */
841
function ipsec_fixup_id($type, $data) {
842
	/* List of types to pass through as-is without changes or adding prefix */
843
	$auto_types = array('address');
844
	/* List of types which need the resulting string double quoted. */
845
	$quote_types = array('keyid', 'asn1dn');
846

    
847
	/* If the first character of asn1dn type data is not #, then rely on
848
	 * automatic parsing https://redmine.pfsense.org/issues/4792 */
849
	if (($type == 'asn1dn') && !empty($data) && ($data[0] != '#')) {
850
		$auto_types[] = 'asn1dn';
851
	}
852

    
853
	if ($type == 'any') {
854
		$idstring = "";
855
	} elseif (!in_array($type, $auto_types)) {
856
		$idstring = "{$type}:{$data}";
857
	} else {
858
		$idstring = $data;
859
	}
860

    
861
	if (in_array($type, $quote_types)) {
862
		$idstring = "\"{$idstring}\"";
863
	}
864

    
865
	return $idstring;
866
}
867

    
868
function ipsec_fixup_network($network) {
869
	if (substr($network, -3) == '|/0') {
870
		$result = substr($network, 0, -3);
871
	} else {
872
		$tmp = explode('|', $network);
873
		if (isset($tmp[1])) {
874
			$result = $tmp[1];
875
		} else {
876
			$result = $tmp[0];
877
		}
878
		unset($tmp);
879
	}
880

    
881
	return $result;
882
}
883

    
884
function ipsec_new_reqid() {
885
	global $config;
886

    
887
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
888
		return;
889
	}
890

    
891
	$ipsecreqid = lock('ipsecreqids', LOCK_EX);
892
	$keyids = array();
893
	$keyid = 1;
894
	foreach ($config['ipsec']['phase2'] as $ph2) {
895
		$keyids[$ph2['reqid']] = $ph2['reqid'];
896
	}
897

    
898
	for ($i = 1; $i < 16000; $i++) {
899
		if (!isset($keyids[$i])) {
900
			$keyid = $i;
901
			break;
902
		}
903
	}
904
	unlock($ipsecreqid);
905

    
906
	return $keyid;
907
}
908

    
909
function ipsec_get_loglevels() {
910
	global $config, $ipsec_log_cats;
911
	$def_loglevel = '1';
912

    
913
	$levels = array();
914

    
915
	foreach (array_keys($ipsec_log_cats) as $cat) {
916
		if (isset($config['ipsec']['logging'][$cat])) {
917
			$levels[$cat] = $config['ipsec']['logging'][$cat];
918
		} elseif (in_array($cat, array('ike', 'chd', 'cfg'))) {
919
			$levels[$cat] = "2";
920
		} else {
921
			$levels[$cat] = $def_loglevel;
922
		}
923
	}
924
	return $levels;
925
}
926

    
927
function ipsec_vti($ph1ent, $returnaddresses = false, $skipdisabled = true) {
928
	global $config;
929
	if (empty($ph1ent) || !is_array($ph1ent) || !is_array($config['ipsec']['phase2'])) {
930
		return false;
931
	}
932

    
933
	$is_vti = false;
934
	$vtisubnet_spec = array();
935

    
936
	foreach ($config['ipsec']['phase2'] as $ph2ent) {
937
		if ($ph1ent['ikeid'] != $ph2ent['ikeid']) {
938
			continue;
939
		}
940
		if ($ph2ent['mode'] == 'vti') {
941
			if ($returnaddresses) {
942
				$vtisubnet_spec[] = array(
943
					'left' => ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']),
944
					'right' => ipsec_idinfo_to_cidr($ph2ent['remoteid'], false, $ph2ent['mode']),
945
					'descr' => $ph2ent['descr'],
946
				);
947
			}
948
			if (!$skipdisabled && isset($ph2ent['disabled'])) {
949
				continue;
950
			} else {
951
				$is_vti = true;
952
			}
953
		}
954
	}
955
	return ($returnaddresses) ? $vtisubnet_spec : $is_vti;
956
}
957

    
958
/* Called when IPsec is reloaded through rc.newipsecdns and similar, gives
959
 * packages an opportunity to execute custom code when IPsec reloads.
960
 */
961
function ipsec_reload_package_hook() {
962
	global $g, $config;
963

    
964
	init_config_arr(array('installedpackages', 'package'));
965
	foreach ($config['installedpackages']['package'] as $package) {
966
		if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
967
			continue;
968
		}
969

    
970
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], 'packagegui');
971
		if (!empty($pkg_config['include_file']) &&
972
		    file_exists($pkg_config['include_file'])) {
973
			require_once($pkg_config['include_file']);
974
		}
975
		if (empty($pkg_config['ipsec_reload_function'])) {
976
			continue;
977
		}
978
		$pkg_ipsec_reload = $pkg_config['ipsec_reload_function'];
979
		if (!function_exists($pkg_ipsec_reload)) {
980
			continue;
981
		}
982
		$pkg_ipsec_reload();
983
	}
984
}
985

    
986
/****f* certs/ipsec_convert_to_modp
987
 * NAME
988
 *   ipsec_convert_to_modp - Take a DH Group number value and return the
989
 *                           associated name
990
 * INPUTS
991
 *   $index: DH group index number to look up
992
 * RESULT
993
 *   Returns the Diffie Hellman Group keyword associated with the group number.
994
 ******/
995
function ipsec_convert_to_modp($index) {
996
	$modpmap = array(
997
		'1' => 'modp768',
998
		'2' => 'modp1024',
999
		'5' => 'modp1536',
1000
		'14' => 'modp2048',
1001
		'15' => 'modp3072',
1002
		'16' => 'modp4096',
1003
		'17' => 'modp6144',
1004
		'18' => 'modp8192',
1005
		'19' => 'ecp256',
1006
		'20' => 'ecp384',
1007
		'21' => 'ecp521',
1008
		'22' => 'modp1024s160',
1009
		'23' => 'modp2048s224',
1010
		'24' => 'modp2048s256',
1011
		'25' => 'ecp192',
1012
		'26' => 'ecp224',
1013
		'27' => 'ecp224bp',
1014
		'28' => 'ecp256bp',
1015
		'29' => 'ecp384bp',
1016
		'30' => 'ecp512bp',
1017
		'31' => 'curve25519',
1018
		'32' => 'curve448',
1019
	);
1020

    
1021
	return $modpmap[$index];
1022
}
1023

    
1024
/*
1025
 * Forcefully restart IPsec
1026
 * This is required for when dynamic interfaces reload
1027
 * For all other occasions the normal ipsec_configure()
1028
 * will gracefully reload the settings without restarting
1029
 */
1030
function ipsec_force_reload($interface = "") {
1031
	global $g, $config;
1032

    
1033
	if (!ipsec_enabled()) {
1034
		return;
1035
	}
1036

    
1037
	$ipseccfg = $config['ipsec'];
1038

    
1039
	if (!empty($interface) && is_array($ipseccfg['phase1'])) {
1040
		$found = false;
1041
		foreach ($ipseccfg['phase1'] as $ipsec) {
1042
			if (!isset($ipsec['disabled']) && ($ipsec['interface'] == $interface)) {
1043
				$found = true;
1044
				break;
1045
			}
1046
		}
1047
		if (!$found) {
1048
			log_error(sprintf(gettext("Ignoring IPsec reload since there are no tunnels on interface %s"), $interface));
1049
			return;
1050
		}
1051
	}
1052

    
1053
	/* If we get this far then we need to take action. */
1054
	log_error(gettext("Forcefully reloading IPsec"));
1055
	ipsec_configure();
1056
}
1057

    
1058
/****f* ipsec/ipsec_strongswan_confgen
1059
 * NAME
1060
 *   ipsec_strongswan_confgen - Generate strongswan.conf style formatted output
1061
 *                              From a multi-dimensional array.
1062
 * INPUTS
1063
 *   $confarr: An array of key=value pairs or key=array entries for subsections.
1064
 *   $indent : A string of tabs used when indenting lines.
1065
 * RESULT
1066
 *   Returns a string of key=value pairs with proper indenting, and with named
1067
 *   subsections enclosed in braces.
1068
 * NOTES
1069
 *   Values starting with a "#" (comments) or "include " will be printed
1070
 *   directly without their associated key name.
1071
 ******/
1072
function ipsec_strongswan_confgen($confarr, $indent = "") {
1073
	$confstr = "";
1074
	/* Only process the contents if it's an array. */
1075
	if (is_array($confarr)) {
1076
		foreach ($confarr as $key => $value) {
1077
			if (is_array($value)) {
1078
				/* If the array is empty, do not print anything */
1079
				if (empty($value)) {
1080
					continue;
1081
				}
1082
				/* If this is an array, setup the subsection name
1083
				 * and structure, then call this function
1084
				 * recursively to walk the lower array. */
1085
				$confstr .= "{$indent}{$key} {\n";
1086
				$confstr .= ipsec_strongswan_confgen($value, $indent . "\t");
1087
				$confstr .= "{$indent}}\n";
1088
			} else {
1089
				$confstr .= "{$indent}";
1090
				if ((substr($value, 0 , 1) != '#') &&
1091
				    (substr($value, 0 , 8) != 'include ')) {
1092
					/* Not a comment or include, print the key */
1093
					$confstr .= "{$key} = ";
1094
				}
1095
				$confstr .= "{$value}\n";
1096
			}
1097
		}
1098
	}
1099
	return $confstr;
1100
}
1101

    
1102
global $g, $ipsec_swanctl_basedir, $ipsec_swanctl_dirs;
1103
$ipsec_swanctl_basedir = "{$g['varetc_path']}/ipsec";
1104
$ipsec_swanctl_dirs = array(
1105
	'conf'     => "{$ipsec_swanctl_basedir}/conf.d",
1106
	'certpath' => "{$ipsec_swanctl_basedir}/x509",
1107
	'capath'   => "{$ipsec_swanctl_basedir}/x509ca",
1108
	'aapath'   => "{$ipsec_swanctl_basedir}/x509aa",
1109
	'ocsppath' => "{$ipsec_swanctl_basedir}/x509ocsp",
1110
	'crlpath'  => "{$ipsec_swanctl_basedir}/x509crl",
1111
	'acpath'   => "{$ipsec_swanctl_basedir}/x509ac",
1112
	'rsakeys'  => "{$ipsec_swanctl_basedir}/rsa",
1113
	'eckeys'   => "{$ipsec_swanctl_basedir}/ecdsa",
1114
	'keypath'  => "{$ipsec_swanctl_basedir}/private",
1115
	'pubkpath' => "{$ipsec_swanctl_basedir}/pubkey",
1116
	'blisspath' => "{$ipsec_swanctl_basedir}/bliss",
1117
	'pkcs8path' => "{$ipsec_swanctl_basedir}/pkcs8",
1118
	'pkcs12path' => "{$ipsec_swanctl_basedir}/pkcs12",
1119
);
1120

    
1121
/****f* ipsec/ipsec_create_dirs
1122
 * NAME
1123
 *   ipsec_create_dirs - Create default set of strongSwan configuration directories.
1124
 * INPUTS
1125
 *   None
1126
 * RESULT
1127
 *   Cleans up and creates strongSwan configuration directories and links.
1128
 ******/
1129
function ipsec_create_dirs() {
1130
	global $config, $g, $ipsec_swanctl_basedir, $ipsec_swanctl_dirs;
1131

    
1132
	/* Cleanup base paths to ensure old files are not left behind (#5238) */
1133
	if (!empty($ipsec_swanctl_basedir)) {
1134
		@rmdir_recursive($ipsec_swanctl_basedir, false);
1135
	}
1136
	/* Create directory structure */
1137
	array_map('safe_mkdir', array_values($ipsec_swanctl_dirs));
1138

    
1139
	/* Create and link strongSwan config */
1140
	$ssdpath = "{$g['varetc_path']}/ipsec/strongswan.d";
1141
	if (!file_exists($ssdpath) || !is_link($ssdpath)) {
1142
		if (is_dir($ssdpath) && !is_link($ssdpath)) {
1143
			@rmdir_recursive($ssdpath, false);
1144
		} else {
1145
			@unlink($ssdpath);
1146
		}
1147
		@symlink("/usr/local/etc/strongswan.d",
1148
		    $ssdpath);
1149
	}
1150
	if (!file_exists("/usr/local/etc/strongswan.conf") ||
1151
	    !is_link("/usr/local/etc/strongswan.conf")) {
1152
		@unlink("/usr/local/etc/strongswan.conf");
1153
		@symlink("{$g['varetc_path']}/ipsec/strongswan.conf",
1154
		    "/usr/local/etc/strongswan.conf");
1155
	}
1156
}
1157

    
1158
/****f* ipsec/ipsec_setup_gwifs
1159
 * NAME
1160
 *   ipsec_setup_gwifs - Setup IPsec-related interfaces and gateways
1161
 * INPUTS
1162
 *   None
1163
 * RESULT
1164
 *   Sets up VTI interfaces and gateways, populates ipsecpinghosts
1165
 ******/
1166
function ipsec_setup_gwifs() {
1167
	global $g, $a_phase1, $a_phase2, $rgmap, $filterdns_list,
1168
		$aggressive_mode_psk, $mobile_ipsec_auth, $ifacesuse,
1169
		$ipsecpinghostsactive;
1170
	/* resolve all local, peer addresses and setup pings */
1171
	unset($iflist);
1172
	if (is_array($a_phase1) && count($a_phase1)) {
1173
		$ipsecpinghosts = array();
1174
		/* step through each phase1 entry */
1175
		foreach ($a_phase1 as $ph1ent) {
1176
			if (isset($ph1ent['disabled'])) {
1177
				continue;
1178
			}
1179

    
1180
			if (substr($ph1ent['interface'], 0, 4) == "_vip") {
1181
				$vpninterface = get_configured_vip_interface($ph1ent['interface']);
1182
				$ifacesuse[] = get_real_interface($vpninterface);
1183
			} else {
1184
				$vpninterface = get_failover_interface($ph1ent['interface']);
1185
				if (substr($vpninterface, 0, 4) == "_vip") {
1186
					$vpninterface = get_configured_vip_interface($vpninterface);
1187
					$ifacesuse[] = get_real_interface($vpninterface);
1188
				} elseif (!empty($vpninterface)) {
1189
					$ifacesuse[] = $vpninterface;
1190
				}
1191
			}
1192

    
1193
			if ($ph1ent['mode'] == "aggressive" && ($ph1ent['authentication_method'] == "pre_shared_key" || $ph1ent['authentication_method'] == "xauth_psk_server")) {
1194
				$aggressive_mode_psk = true;
1195
			}
1196

    
1197
			$ikeid = $ph1ent['ikeid'];
1198

    
1199
			$local_spec = ipsec_get_phase1_src($ph1ent);
1200
			/* When automatically guessing, use the first address. */
1201
			$local_spec  = explode(',', $local_spec);
1202
			$local_spec  = $local_spec[0];
1203
			if (!is_ipaddr($local_spec)) {
1204
				log_error(sprintf(gettext("IPsec ERROR: Could not find phase 1 source for connection %s. Omitting from configuration file."), $ph1ent['descr']));
1205
				continue;
1206
			}
1207

    
1208
			if (isset($ph1ent['mobile'])) {
1209
				$mobile_ipsec_auth = $ph1ent['authentication_method'];
1210
				continue;
1211
			}
1212

    
1213
			/* see if this tunnel has a hostname for the remote-gateway. If so,
1214
			   try to resolve it now and add it to the list for filterdns */
1215
			$rg = $ph1ent['remote-gateway'];
1216
			if (!is_ipaddr($rg)) {
1217
				$filterdns_list[] = "{$rg}";
1218
				add_hostname_to_watch($rg);
1219
				if (!platform_booting()) {
1220
					$rg = resolve_retry($rg, $ph1ent['protocol']);
1221
				}
1222
				if (!is_ipaddr($rg)) {
1223
					continue;
1224
				}
1225
			}
1226
			if (!isset($ph1ent['gw_duplicates']) && array_search($rg, $rgmap)) {
1227
				log_error(sprintf(gettext("The remote gateway %s already exists on another phase 1 entry"), $rg));
1228
				continue;
1229
			}
1230
			$rgmap[$ph1ent['remote-gateway']] = $rg;
1231

    
1232
			$is_vti = false;
1233
			if (is_array($a_phase2)) {
1234
				/* step through each phase2 entry */
1235
				foreach ($a_phase2 as $ph2ent) {
1236
					if ($ph2ent['mode'] == 'vti') {
1237
						$is_vti = true;
1238
					}
1239
					if (isset($ph2ent['disabled']) || ($ikeid != $ph2ent['ikeid'])) {
1240
						continue;
1241
					}
1242

    
1243
					/* add an ipsec pinghosts entry */
1244
					if ($ph2ent['pinghost']) {
1245
						if (!is_array($iflist)) {
1246
							$iflist = get_configured_interface_list();
1247
						}
1248
						$srcip = null;
1249
						$local_subnet = ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']);
1250
						if (is_ipaddrv6($ph2ent['pinghost'])) {
1251
							foreach ($iflist as $ifent => $ifname) {
1252
								$interface_ip = get_interface_ipv6($ifent);
1253
								if (!is_ipaddrv6($interface_ip)) {
1254
									continue;
1255
								}
1256
								if (ip_in_subnet($interface_ip, $local_subnet)) {
1257
									$srcip = $interface_ip;
1258
									break;
1259
								}
1260
							}
1261
						} else {
1262
							foreach ($iflist as $ifent => $ifname) {
1263
								$interface_ip = get_interface_ip($ifent);
1264
								if (!is_ipaddrv4($interface_ip)) {
1265
									continue;
1266
								}
1267
								if ($local_subnet == "0.0.0.0/0" || ip_in_subnet($interface_ip, $local_subnet)) {
1268
									$srcip = $interface_ip;
1269
									break;
1270
								}
1271
							}
1272
						}
1273
						/* if no valid src IP was found in configured interfaces, try the vips */
1274
						if (is_null($srcip)) {
1275
							$viplist = get_configured_vip_list();
1276
							foreach ($viplist as $vip => $address) {
1277
								if (ip_in_subnet($address, $local_subnet)) {
1278
									$srcip = $address;
1279
									break;
1280
								}
1281
							}
1282
						}
1283
						$dstip = $ph2ent['pinghost'];
1284
						$family = "inet" . (is_ipaddrv6($dstip)) ? "6" : "";
1285
						if (is_ipaddr($srcip)) {
1286
							$ipsecpinghosts[] = "{$srcip}|{$dstip}|3|||||{$family}|\n";
1287
							$ipsecpinghostsactive = true;
1288
						}
1289
					}
1290
				}
1291
			}
1292
			if ($is_vti) {
1293
				interface_ipsec_vti_configure($ph1ent);
1294
			}
1295
		}
1296
		@file_put_contents("{$g['vardb_path']}/ipsecpinghosts", $ipsecpinghosts);
1297
		unset($ipsecpinghosts);
1298
	}
1299
	unset($iflist);
1300
}
1301

    
1302
/****f* ipsec/ipsec_logging_config
1303
 * NAME
1304
 *   ipsec_logging_config - Generate an array containing strongSwan logging
1305
 *                          values.
1306
 * INPUTS
1307
 *   None
1308
 * RESULT
1309
 *   Returns an array which can be merged into the strongswan daemon logging
1310
 *   values so that each logging category is represented, even those not listed
1311
 *   in config.xml.
1312
 ******/
1313
function ipsec_logging_config() {
1314
	global $config, $ipsec_log_cats, $ipsec_log_sevs;
1315
	$logcfg = array();
1316
	$log_levels = ipsec_get_loglevels();
1317
	foreach (array_keys($ipsec_log_cats) as $cat) {
1318
		if (is_numeric($log_levels[$cat]) &&
1319
		    in_array(intval($log_levels[$cat]), array_keys($ipsec_log_sevs), true)) {
1320
			$logcfg[$cat] = $log_levels[$cat];
1321
		}
1322
	}
1323
	return $logcfg;
1324
}
1325

    
1326
/****f* ipsec/ipsec_setup_strongswan
1327
 * NAME
1328
 *   ipsec_setup_strongswan - Generate the strongswan.conf configuration file.
1329
 * INPUTS
1330
 *   None
1331
 * RESULT
1332
 *   Determines strongswan.conf values and crafts the configuration file in the
1333
 *   appropriate format.
1334
 * NOTES
1335
 *   Called from ipsec_configure()
1336
 ******/
1337
function ipsec_setup_strongswan() {
1338
	global $config, $g, $mobile_ipsec_auth, $a_client, $a_phase2, $aggressive_mode_psk, $ifacesuse;
1339

    
1340
	/* strongswan.conf array */
1341
	$ssconf = array();
1342
	$ssconf[] = "# Automatically generated config file - DO NOT MODIFY. Changes will be overwritten.";
1343
	$ssconf['starter'] = array();
1344
	$ssconf['starter']['load_warning'] = "no";
1345

    
1346
	$ssconf['charon'] = array();
1347
	$ssconf['charon'][] = '# number of worker threads in charon';
1348
	$ssconf['charon']['threads'] = '16';
1349
	$ssconf['charon']['ikesa_table_size'] = '32';
1350
	$ssconf['charon']['ikesa_table_segments'] = '4';
1351
	$ssconf['charon']['init_limit_half_open'] = '1000';
1352
	$ssconf['charon']['install_routes'] = 'no';
1353
	$ssconf['charon']['load_modular'] = 'yes';
1354
	$ssconf['charon']['ignore_acquire_ts'] = 'yes';
1355

    
1356
	if ($aggressive_mode_psk) {
1357
		$stronconf = '';
1358
		if (file_exists("{$g['varetc_path']}/ipsec/strongswan.conf")) {
1359
			$stronconf = file_get_contents("{$g['varetc_path']}/ipsec/strongswan.conf");
1360
		}
1361
		log_error("WARNING: Setting i_dont_care_about_security_and_use_aggressive_mode_psk option because a phase 1 is configured using aggressive mode with pre-shared keys. This is not a secure configuration.");
1362
		$restart = (!empty($stronconf) && strpos($stronconf, 'i_dont_care_about_security_and_use_aggressive_mode_psk') === FALSE);
1363
		unset($stronconf);
1364
		$ssconf['charon']['i_dont_care_about_security_and_use_aggressive_mode_psk'] = "yes";
1365
	}
1366

    
1367
	if (isset($config['ipsec']['acceptunencryptedmainmode'])) {
1368
		$ssconf['charon']['accept_unencrypted_mainmode_messages'] = "yes";
1369
	}
1370

    
1371
	if (isset($config['ipsec']['maxexchange'])) {
1372
		$ssconf['charon']['max_ikev1_exchanges'] = $config['ipsec']['maxexchange'];
1373
	}
1374

    
1375
	$unity_enabled = isset($config['ipsec']['unityplugin']) ? 'yes' : 'no';
1376
	$ssconf['charon']['cisco_unity'] = $unity_enabled;
1377

    
1378
	if (isset($config['ipsec']['enableinterfacesuse']) && !empty($ifacesuse)) {
1379
		$ssconf['charon']['interfaces_use'] = implode(',', array_unique($ifacesuse));
1380
	}
1381

    
1382
	if (isset($config['ipsec']['makebeforebreak'])) {
1383
		$ssconf['charon']['make_before_break'] = "yes";
1384
	}
1385

    
1386
	if (isset($config['ipsec']['port'])) {
1387
		$ssconf['charon']['port'] = $config['ipsec']['port'];
1388
	}
1389

    
1390
	if (isset($config['ipsec']['port_nat_t'])) {
1391
		$ssconf['charon']['port_nat_t'] = $config['ipsec']['port_nat_t'];
1392
	}
1393

    
1394
	$ssconf['charon']['syslog'] = array();
1395
	$ssconf['charon']['syslog']['identifier'] = 'charon';
1396
	$ssconf['charon']['syslog'][] = "# log everything under daemon since it ends up in the same place regardless with our syslog.conf";
1397
	$ssconf['charon']['syslog']['daemon'] = array();
1398
	$ssconf['charon']['syslog']['daemon']['ike_name'] = 'yes';
1399
	$ssconf['charon']['syslog']['daemon'] = array_merge($ssconf['charon']['syslog']['daemon'], ipsec_logging_config());
1400
	$ssconf['charon']['syslog'][] = '# disable logging under auth so logs aren\'t duplicated';
1401
	$ssconf['charon']['syslog']['auth'] = array();
1402
	$ssconf['charon']['syslog']['auth']['default'] = -1;
1403

    
1404
	$ssconf['charon']['plugins'] = array();
1405
	$ssconf['charon']['plugins'][] = "# Load defaults";
1406
	$ssconf['charon']['plugins'][] = "include {$g['varetc_path']}/ipsec/strongswan.d/charon/*.conf";
1407
	$ssconf['charon']['plugins']['unity'] = array('load' => $unity_enabled);
1408
	$ssconf['charon']['plugins']['curve25519'] = array('load' => "yes");
1409

    
1410
	$ssconf['charon']['plugins']['pkcs11'] = array();
1411
	$ssconf['charon']['plugins']['pkcs11']['load'] = "yes";
1412
	$ssconf['charon']['plugins']['pkcs11']['modules'] = array();
1413
	$ssconf['charon']['plugins']['pkcs11']['modules']['opensc'] = array();
1414
	$ssconf['charon']['plugins']['pkcs11']['modules']['opensc']['load_certs'] = "yes";
1415
	$ssconf['charon']['plugins']['pkcs11']['modules']['opensc']['path'] = "/usr/local/lib/opensc-pkcs11.so";
1416

    
1417
	/* Find RADIUS servers designated for Mobile IPsec user auth */
1418
	$radius_servers = array();
1419
	foreach (explode(',', $config['ipsec']['client']['user_source']) as $user_source) {
1420
		$auth_server = auth_get_authserver($user_source);
1421
		$nice_user_source = strtolower(preg_replace('/[\s\.]+/', '_', $user_source));
1422
		if ($auth_server && ($auth_server['type'] === 'radius')) {
1423
			$radius_servers[$nice_user_source] = array();
1424
			$radius_servers[$nice_user_source]['address'] = $auth_server['host'];
1425
			$radius_servers[$nice_user_source]['secret'] = "\"{$auth_server['radius_secret']}\"";
1426
			$radius_servers[$nice_user_source]['auth_port'] = empty($auth_server['radius_auth_port']) ? 1812 : $auth_server['radius_auth_port'];;
1427
			$radius_servers[$nice_user_source]['acct_port'] = empty($auth_server['radius_acct_port']) ? 1813 : $auth_server['radius_acct_port'];
1428
		}
1429
	}
1430

    
1431
	/* Generate an eap-radius config section if appropriate */
1432
	if (count($radius_servers) && ($mobile_ipsec_auth === "eap-radius")) {
1433
		$ssconf['charon']['plugins']['eap-radius'] = array();
1434
		$ssconf['charon']['plugins']['eap-radius']['load'] = "2";
1435
		$ssconf['charon']['plugins']['eap-radius']['class_group'] = "yes";
1436
		$ssconf['charon']['plugins']['eap-radius']['eap_start'] = "no";
1437
		/* Activate RADIUS accounting only if it was selected on the IPsec Mobile Clients tab */
1438
		if ($auth_server && isset($auth_server['radius_acct_port']) &&
1439
		    (is_array($a_client) && ($a_client['radiusaccounting'] == "enabled"))){
1440
			$ssconf['charon']['plugins']['eap-radius']['accounting'] = "yes";
1441
			$ssconf['charon']['plugins']['eap-radius']['accounting_close_on_timeout'] = "no";
1442
		}
1443
		$ssconf['charon']['plugins']['eap-radius']['servers'] = $radius_servers;
1444
	}
1445

    
1446
	if (is_array($a_client) && isset($a_client['enable'])) {
1447
		if ($a_client['user_source'] != "none") {
1448
			$ssconf['charon']['plugins']['xauth-generic'] = array();
1449
			$ssconf['charon']['plugins']['xauth-generic']['script'] = "/etc/inc/ipsec.auth-user.php";
1450
			$authcfgs = array();
1451
			foreach (explode(",", $a_client['user_source']) as $authcfg) {
1452
				$authcfgs[] = ($authcfg == "system") ? "Local Database" : $authcfg;
1453
			}
1454
			$ssconf['charon']['plugins']['xauth-generic']['authcfg'] = implode(",", $authcfgs);
1455
		}
1456
	}
1457

    
1458
	@file_put_contents("{$g['varetc_path']}/ipsec/strongswan.conf", ipsec_strongswan_confgen($ssconf));
1459
}
1460

    
1461
/****f* ipsec/ipsec_setup_pools
1462
 * NAME
1463
 *   ipsec_setup_pools - Generate primary mobile pool configuration for swanctl
1464
  * INPUTS
1465
 *   None
1466
 * RESULT
1467
 *   Adds configured mobile pool settings to $scconf
1468
 * NOTES
1469
 *   These values were formerly in strongswan.conf but may now be configured in
1470
 *   pools, making future expansion to support multiple pools possible.
1471
 ******/
1472
function ipsec_setup_pools() {
1473
	global $config, $g, $mobile_ipsec_auth, $a_client, $a_phase2, $scconf;
1474
	if (!is_array($a_client) || !isset($a_client['enable'])) {
1475
		return;
1476
	}
1477
	if (($mobile_ipsec_auth == "eap-radius") && empty($a_client['pool_address']) &&
1478
	    empty($a_client['pool_address_v6'])) {
1479
		return;
1480
	}
1481
	$scconf['mobile-pool'] = array();
1482
	$scconf['pools'] = array();
1483
	$pool_common =& $scconf['mobile-pool'];
1484

    
1485
	$rightdnsservers = array();
1486
	if (!empty($a_client['dns_server1'])) {
1487
		$rightdnsservers[] = $a_client['dns_server1'];
1488
	}
1489
	if (!empty($a_client['dns_server2'])) {
1490
		$rightdnsservers[] = $a_client['dns_server2'];
1491
	}
1492
	if (!empty($a_client['dns_server3'])) {
1493
		$rightdnsservers[] = $a_client['dns_server3'];
1494
	}
1495
	if (!empty($a_client['dns_server4'])) {
1496
		$rightdnsservers[] = $a_client['dns_server4'];
1497
	}
1498
	if (count($rightdnsservers)) {
1499
		$pool_common['dns'] = implode(',', $rightdnsservers);
1500
	}
1501

    
1502
	$cfgservers = array();
1503
	if (!empty($a_client['wins_server1'])) {
1504
		$cfgservers[] = $a_client['wins_server1'];
1505
	}
1506
	if (!empty($a_client['wins_server2'])) {
1507
		$cfgservers[] = $a_client['wins_server2'];
1508
	}
1509
	if (!empty($cfgservers)) {
1510
		$pool_common['nbns'] = implode(",", $cfgservers);
1511
	}
1512
	unset($cfgservers);
1513

    
1514
	$net_list4 = array();
1515
	$net_list6 = array();
1516
	if (isset($a_client['net_list']) && is_array($a_phase2)) {
1517
		foreach ($a_phase2 as $ph2ent) {
1518
			if (isset($ph2ent['disabled']) ||
1519
			    !isset($ph2ent['mobile'])) {
1520
				continue;
1521
			}
1522
			$nlent = ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']);
1523
			if (is_subnetv4($nlent)) {
1524
				$net_list4[] = $nlent;
1525
			} elseif (is_subnetv6($nlent)) {
1526
				$net_list6[] = $nlent;
1527
			}
1528
			unset($nlent);
1529
		}
1530
	}
1531
	if (!empty($a_client['dns_domain'])) {
1532
		$pool_common[] = "# Search domain and default domain";
1533
		$pool_common['28674'] = "\"{$a_client['dns_domain']}\"";
1534
		if (empty($a_client['dns_split'])) {
1535
			$pool_common['28675'] = "\"{$a_client['dns_domain']}\"";
1536
		}
1537
	}
1538

    
1539
	if (!empty($a_client['dns_split'])) {
1540
		$pool_common['28675'] = "\"{$a_client['dns_split']}\"";
1541
	}
1542

    
1543
	if (!empty($a_client['login_banner'])) {
1544
		$pool_common['28672'] = "\"{$a_client['login_banner']}\"";
1545
	}
1546

    
1547
	if (isset($a_client['save_passwd'])) {
1548
		$pool_common['28673'] = "1";
1549
	}
1550

    
1551
	if ($a_client['pfs_group']) {
1552
		$pool_common['28679'] = "\"{$a_client['pfs_group']}\"";
1553
	}
1554

    
1555
	if (!empty($a_client['pool_address'])) {
1556
		$scconf['pools']['mobile-pool-v4 : mobile-pool'] = array();
1557
		$v4pool =& $scconf['pools']['mobile-pool-v4 : mobile-pool'];
1558
		$v4pool['addrs'] = "{$a_client['pool_address']}/{$a_client['pool_netbits']}";
1559
		if (!empty($net_list4)) {
1560
			$v4pool['subnet'] = implode(",", $net_list4);
1561
			$v4pool['split_include'] = implode(",", $net_list4);
1562
			unset($net_list4);
1563
		}
1564
	}
1565
	if (!empty($a_client['pool_address_v6'])) {
1566
		$scconf['pools']['mobile-pool-v6 : mobile-pool'] = array();
1567
		$v6pool =& $scconf['pools']['mobile-pool-v6 : mobile-pool'];
1568
		$v6pool['addrs'] = "{$a_client['pool_address_v6']}/{$a_client['pool_netbits_v6']}";
1569
		if (!empty($net_list6)) {
1570
			$v6pool['subnet'] = implode(",", $net_list6);
1571
			$v6pool['split_include'] = implode(",", $net_list6);
1572
			unset($net_list6);
1573
		}
1574
	}
1575

    
1576
	return;
1577
}
1578

    
1579
/****f* ipsec/ipsec_setup_userpools
1580
 * NAME
1581
 *   ipsec_setup_userpools - Generate per-user custom pool settings for swanctl
1582
  * INPUTS
1583
 *   None
1584
 * RESULT
1585
 *   Adds configured per-user pool settings to $scconf using the primary mobile
1586
 *   pool as a base configuration.
1587
 * NOTES
1588
 *   Given this new flexible format, it is now possible to override any valid
1589
 *   pool setting, so future expansion of per-user settings is possible.
1590
 ******/
1591
function ipsec_setup_userpools() {
1592
	global $config, $scconf;
1593
	$a_mobilekey = $config['ipsec']['mobilekey'];
1594

    
1595
	/* Do not waste time if we do not have all the necessary information. */
1596
	if (!is_array($a_mobilekey) ||
1597
	    empty($a_mobilekey) ||
1598
	    !is_array($scconf['connections']) ||
1599
	    !is_array($scconf['con-mobile-defaults']) ||
1600
	    !is_array($scconf['pools']) ||
1601
	    !is_array($scconf['mobile-pool'])) {
1602
		return;
1603
	}
1604

    
1605
	$suffix = 1;
1606
	foreach ($a_mobilekey as $mkent) {
1607
		if (($mkent['type'] != "EAP") ||
1608
		    !isset($mkent['ident_type']) ||
1609
		    !isset($mkent['pool_address']) ||
1610
		    !isset($mkent['pool_netbits']) ||
1611
		    (strlen($mkent['pool_address']) < 1) ||
1612
		    !is_ipaddr($mkent['pool_address'])) {
1613
			continue;
1614
		}
1615
		/* The name of just this pool */
1616
		$upbase = "mobile-userpool-{$suffix}";
1617
		/* The full connection name including a reference to the primary
1618
		 * mobile connection */
1619
		$upconn = "con-{$upbase} : con-mobile-defaults";
1620
		/* The full pool name including a reference to the primary
1621
		 * mobile pool */
1622
		$upname = "{$upbase} : mobile-pool";
1623

    
1624
		$scconf['connections'][$upconn] = array();
1625
		$scconf['pools'][$upname] = array();
1626

    
1627
		$clientid = ($mkent['ident_type'] == "none") ? "\"{$mkent['ident']}\"" : "{$mkent['ident_type']}:{$mkent['ident']}";
1628
		$clienteapid = ($ph1ent['authentication_method'] == "eap-mschapv2") ? $clientid : '%any';
1629

    
1630
		/* Craft a cloned connection with the ID information to match */
1631
		$scconf['connections'][$upconn]['remote'] = array();
1632
		$scconf['connections'][$upconn]['remote']['id'] = $clientid;
1633
		$scconf['connections'][$upconn]['remote']['eap_id'] = $clienteapid;
1634
		$scconf['connections'][$upconn]['pools'] = $upbase;
1635

    
1636
		/* Craft a cloned pool with pool settings to override for this user */
1637
		$scconf['pools'][$upname]['addrs'] = "{$mkent['pool_address']}/{$mkent['pool_netbits']}";
1638
		if (isset($mkent['dns_address']) && strlen($mkent['dns_address']) > 0 && is_ipaddr($mkent['dns_address'])) {
1639
			$scconf['pools'][$upname]['dns'] = $mkent['dns_address'];
1640
		}
1641
		$suffix++;
1642
	}
1643
	return;
1644
}
1645

    
1646
/****f* ipsec/ipsec_setup_bypass
1647
 * NAME
1648
 *   ipsec_setup_bypass - Generate a bypass connection for LAN-LAN and custom rules traffic
1649
 * INPUTS
1650
 *   None
1651
 * RESULT
1652
 *   Sets up a bypass connection to prevent local traffic from being caught by
1653
 *   IPsec policies.
1654
 ******/
1655
function ipsec_setup_bypass() {
1656
	global $config, $scconf;
1657

    
1658
	$scconf['connections']['bypass'] = array();
1659
	/* Prevents the connection from being considered for remote peers */
1660
	$scconf['connections']['bypass']['remote_addrs'] = "127.0.0.1";
1661
	$scconf['connections']['bypass']['children'] = array();
1662

    
1663
	/* Locate the LAN IPv4 and IPv6 subnets */
1664
	$bypassnets = array();
1665
	if ($config['interfaces']['lan']) {
1666
		$lanip = get_interface_ip("lan");
1667
		if (!empty($lanip) && is_ipaddrv4($lanip)) {
1668
			$lansn = get_interface_subnet("lan");
1669
			$lansa = gen_subnetv4($lanip, $lansn);
1670
			if (!empty($lansa) && !empty($lansn)) {
1671
				$bypassnets[] = "{$lansa}/{$lansn}";
1672
			}
1673
		}
1674
		$lanip6 = get_interface_ipv6("lan");
1675
		if (!empty($lanip6) && is_ipaddrv6($lanip6)) {
1676
			$lansn6 = get_interface_subnetv6("lan");
1677
			$lansa6 = gen_subnetv6($lanip6, $lansn6);
1678
			if (!empty($lansa6) && !empty($lansn6)) {
1679
				$bypassnets[] = "{$lansa6}/{$lansn6}";
1680
			}
1681
		}
1682
	}
1683
	/* If we have viable targets, setup a bypass LAN connection */
1684
	if (!empty($bypassnets) && !isset($config['ipsec']['noshuntlaninterfaces'])) {
1685
		$bypass = implode(',', $bypassnets);
1686
		$scconf['connections']['bypass']['children']['bypasslan'] = array();
1687
		$scconf['connections']['bypass']['children']['bypasslan']['local_ts'] = $bypass;
1688
		$scconf['connections']['bypass']['children']['bypasslan']['remote_ts'] = $bypass;
1689
		$scconf['connections']['bypass']['children']['bypasslan']['mode'] = "pass";
1690
		$scconf['connections']['bypass']['children']['bypasslan']['start_action'] = "trap";
1691
	}
1692

    
1693
	/* Setup custom bypass rules */
1694
	if ((isset($config['ipsec']['ipsecbypass']) && $config['ipsec']['bypassrules']) &&
1695
	    is_array($config['ipsec']['bypassrules'])) {
1696
		foreach ($config['ipsec']['bypassrules']['rule'] as $id => $rule) {
1697
			$scconf['connections']['bypass']['children']["rule{$id}"] = array();
1698
			$scconf['connections']['bypass']['children']["rule{$id}"]["local_ts"] = $rule['source'] .
1699
			       	"/" . $rule['srcmask'];
1700
			$scconf['connections']['bypass']['children']["rule{$id}"]["remote_ts"] = $rule['destination'] .
1701
			       	"/" . $rule['dstmask'];
1702
			$scconf['connections']['bypass']['children']["rule{$id}"]["mode"] = "pass";
1703
			$scconf['connections']['bypass']['children']["rule{$id}"]["start_action"] = "trap";
1704
		}
1705
	}
1706

    
1707
	return;
1708
}
1709

    
1710
/****f* ipsec/ipsec_setup_routes
1711
 * NAME
1712
 *   ipsec_setup_routes - Setup OS routing table static routes for remote peers.
1713
 *                        This ensures that IPsec connections are routed out of
1714
 *                        the expected interface on egress.
1715
 * INPUTS
1716
 *   $interface : The interface upon which routes will be configured.
1717
 *   $family    : The address family ('inet' or 'inet6')
1718
 *   $sourcehost: The local source address used for initiating connections.
1719
 *   $duplicates: If the ipsec tunnel allows duplicates remote peers
1720
 * RESULT
1721
 *   Static routes in the OS routing table for IPsec peers
1722
 ******/
1723
function ipsec_setup_routes($interface, $family, $sourcehost, $duplicates) {
1724
	if (substr($interface, 0, 4) == "_vip") {
1725
		$vpninterface = get_configured_vip_interface($interface);
1726
		if (substr($vpninterface, 0, 4) == "_vip") {
1727
			// vips are nested if its a ipalias with a carp parent
1728
			$vpninterface = get_configured_vip_interface($vpninterface);
1729
		}
1730
		$ifacesuse = get_real_interface($vpninterface);
1731
	} else {
1732
		$ifacesuse = get_failover_interface($interface);
1733
		if (substr($ifacesuse, 0, 4) == "_vip") {
1734
			$vpninterface = get_configured_vip_interface($ifacesuse);
1735
			$ifacesuse = get_real_interface($vpninterface);
1736
		} else {
1737
			$vpninterface = convert_real_interface_to_friendly_interface_name($ifacesuse);
1738
		}
1739
	}
1740
	if ($family == 'inet' && !empty($ifacesuse) &&
1741
	    interface_has_gateway($vpninterface)) {
1742
		$gatewayip = get_interface_gateway($vpninterface);
1743
		$interfaceip = get_interface_ip($vpninterface);
1744
		$subnet_bits = get_interface_subnet($vpninterface);
1745
		$subnet_ip = gen_subnetv4($interfaceip, $subnet_bits);
1746
		/*
1747
		 * if the remote gateway is in the local subnet, then don't add
1748
		 * a route
1749
		 */
1750
		if (is_ipaddrv4($sourcehost) &&
1751
		    !ip_in_subnet($sourcehost, "{$subnet_ip}/{$subnet_bits}") &&
1752
		    is_ipaddrv4($gatewayip) && !$duplicates) {
1753
			route_add_or_change($sourcehost, $gatewayip);
1754
		}
1755
	} else if ($family == 'inet6' && !empty($ifacesuse) &&
1756
	    interface_has_gatewayv6($vpninterface)) {
1757
		$gatewayip = get_interface_gateway_v6($vpninterface);
1758
		$interfaceip = get_interface_ipv6($vpninterface);
1759
		$subnet_bits = get_interface_subnetv6($vpninterface);
1760
		$subnet_ip = gen_subnetv6($interfaceip, $subnet_bits);
1761
		/*
1762
		 * if the remote gateway is in the local subnet, then don't add
1763
		 * a route
1764
		 */
1765
		if (is_ipaddrv6($sourcehost) &&
1766
		    !ip_in_subnet($sourcehost, "{$subnet_ip}/{$subnet_bits}") &&
1767
		    is_ipaddrv6($gatewayip) && !$duplicates) {
1768
			route_add_or_change($sourcehost, $gatewayip);
1769
		}
1770
	}
1771
	return $ifacesuse;
1772
}
1773

    
1774
/****f* ipsec/ipsec_setup_authentication
1775
 * NAME
1776
 *   ipsec_setup_authentication - Generate an array with local/remote
1777
 *                                authentication information for a given IPsec
1778
 *                                Phase 1.
1779
 * INPUTS
1780
 *   $ph1ent: An IPsec Phase 1 configuration
1781
 *   $conn  : A swanctl connection array corresponding to the IPsec Phase 1.
1782
 * RESULT
1783
 *   Populates $conn with local and remote arrays containing authentication
1784
 *   details.
1785
 ******/
1786
function ipsec_setup_authentication(& $ph1ent, & $conn) {
1787
	global $rgmap, $ipsec_swanctl_dirs, $config;
1788
	$local = array();
1789
	$remote = array();
1790
	$remote2 = array();
1791

    
1792
	list($myid_type, $myid_data) = ipsec_find_id($ph1ent, 'local');
1793
	$localid = ipsec_fixup_id($myid_type, $myid_data);
1794
	if (!empty($localid)) {
1795
		$local['id'] = $localid;
1796
	}
1797

    
1798
	// Only specify peer ID if we are not dealing with mobile PSK
1799
	if (!(isset($ph1ent['mobile']) &&
1800
	    in_array($ph1ent['authentication_method'], array("pre_shared_key", "xauth_psk_server")))) {
1801
		list ($remoteid_type, $remoteid_data) = ipsec_find_id($ph1ent, 'peer', $rgmap);
1802
		$remoteid = ipsec_fixup_id($remoteid_type, $remoteid_data);
1803
	}
1804
	if (!empty($remoteid)) {
1805
		$remote['id'] = $remoteid;
1806
	}
1807

    
1808
	if (!empty($ph1ent['caref'])) {
1809
		$ca = lookup_ca($ph1ent['caref']);
1810
		if ($ca) {
1811
			/* Get hash value to use for filename */
1812
			$ca_details = openssl_x509_parse(base64_decode($ca['crt']));
1813
			$cafn = "{$ipsec_swanctl_dirs['capath']}/{$ca_details['hash']}.0";
1814
		}
1815
	}
1816

    
1817
	$authentication = "";
1818
	switch ($ph1ent['authentication_method']) {
1819
		case 'eap-mschapv2':
1820
			if (isset($ph1ent['mobile'])) {
1821
				$local['auth'] = "pubkey";
1822
				$remote['eap_id'] = "%any";
1823
				$remote['auth'] = "eap-mschapv2";
1824
			}
1825
			break;
1826
		case 'eap-tls':
1827
			if (isset($ph1ent['mobile'])) {
1828
				$local['auth'] = "pubkey";
1829
				$remote['eap_id'] = "%any";
1830
			} else {
1831
				$local['auth'] = "eap-tls";
1832
			}
1833
			$remote['auth'] = "eap-tls";
1834
			break;
1835
		case 'eap-radius':
1836
			if (isset($ph1ent['mobile'])) {
1837
				$local['auth'] = "pubkey";
1838
				$remote['eap_id'] = "%any";
1839
			} else {
1840
				$local['auth'] = "eap-radius";
1841
			}
1842
			if (($config['ipsec']['client']['group_source'] == 'enabled') &&
1843
			    !empty($config['ipsec']['client']['auth_groups'])) {
1844
				$remote['groups'] = $config['ipsec']['client']['auth_groups'];
1845
			}
1846
			$remote['auth'] = "eap-radius";
1847
			break;
1848
		case 'xauth_cert_server':
1849
			$local['auth'] = "pubkey";
1850
			$remote['auth'] = "pubkey";
1851
			$remote2['auth'] = "xauth-generic";
1852
			break;
1853
		case 'xauth_psk_server':
1854
			$local['auth'] = "psk";
1855
			$remote['auth'] = "psk";
1856
			$remote2['auth'] = "xauth-generic";
1857
			break;
1858
		case 'pre_shared_key':
1859
			$local['auth'] = "psk";
1860
			$remote['auth'] = "psk";
1861
			break;
1862
		case 'cert':
1863
		case 'pkcs11':
1864
			$local['auth'] = "pubkey";
1865
			$remote['auth'] = "pubkey";
1866
			break;
1867
		case 'hybrid_cert_server':
1868
			$local['auth'] = "pubkey";
1869
			$remote['auth'] = "xauth-generic";
1870
			break;
1871
	}
1872
	if (in_array($ph1ent['authentication_method'], array('eap-mschapv2', 'eap-tls', 'eap-radius', 'xauth_cert_server', 'cert', 'hybrid_cert_server')) &&
1873
	    !empty($ph1ent['certref'])) {
1874
		$local['cert'] = array('file' => "{$ipsec_swanctl_dirs['certpath']}/cert-{$ph1ent['ikeid']}.crt");
1875
		$conn['send_cert'] = "always";
1876
	}
1877
	if ($ph1ent['authentication_method'] == 'pkcs11') {
1878
		$local['cert'] = array('handle' => "{$ph1ent['pkcs11certref']}");
1879
		$conn['send_cert'] = "always";
1880
	}
1881
	if (in_array($ph1ent['authentication_method'], array('eap-tls', 'xauth_cert_server', 'cert', 'pkcs11')) &&
1882
	    isset($cafn)) {
1883
		$remote['cacerts'] = $cafn;
1884
		if (isset($config['ipsec']['strictcrlpolicy'])) {
1885
			$remote['revocation'] = "strict";
1886
		}
1887
	}
1888

    
1889
	$conn['local'] = $local;
1890
	/* If there is data for a second remote auth round, setup two remote
1891
	 * arrays with unique names, otherwise setup a single remote. */
1892
	if (empty($remote2)) {
1893
		$conn['remote'] = $remote;
1894
	} else {
1895
		$conn['remote-1'] = $remote;
1896
		$conn['remote-2'] = $remote2;
1897
	}
1898
}
1899

    
1900
/****f* ipsec/ipsec_setup_proposal_algo
1901
 * NAME
1902
 *   ipsec_setup_proposal_algo - Form a single proposal algorithm string
1903
 * INPUTS
1904
 *   $ealg_id: Encryption algorithm ID
1905
 *   $keylen : Key length for the encryption algorithm
1906
 *   $halgo  : Hash algorithm
1907
 *   $modp   : DH Group number
1908
 * RESULT
1909
 *   Returns a string using the available information to form a single proposal
1910
 *   algorithm definition.
1911
 * NOTES
1912
 *   Values left empty will not be added to the string. Some combinations may
1913
 *   require one or more parts to be omitted.
1914
 ******/
1915
function ipsec_setup_proposal_algo($ealg_id, $keylen, $halgo, $prfalgo, $modp) {
1916
	$palgo = "";
1917

    
1918
	/* Add the encryption algorithm (if present) */
1919
	if (!empty($ealg_id)) {
1920
		$palgo .= "{$ealg_id}";
1921
	}
1922

    
1923
	/* Add the key length (if present) */
1924
	if (!empty($keylen)) {
1925
		$palgo .= "{$keylen}";
1926
	}
1927

    
1928
	/* Add the hash algorithm (if present) */
1929
	if (!empty($halgo)) {
1930
		/* If there is some content in the propsal already, add a
1931
		 * separator */
1932
		if (!empty($palgo)) {
1933
			$palgo .= "-";
1934
		}
1935
		$halgo = str_replace('hmac_', '', $halgo);
1936
		$palgo .= "{$halgo}";
1937
	}
1938

    
1939
	if (!empty($prfalgo)) {
1940
		$palgo .= "-prf{$prfalgo}";
1941
	}
1942

    
1943
	/* Convert the DH group to its keyword and add (if present) */
1944
	$modp = ipsec_convert_to_modp($modp);
1945
	if (!empty($modp)) {
1946
		$palgo .= "-{$modp}";
1947
	}
1948

    
1949
	return $palgo;
1950
}
1951

    
1952
/****f* ipsec/ipsec_setup_proposal_entry
1953
 * NAME
1954
 *   ipsec_setup_proposal_entry - Generate a full proposal string for an IPsec
1955
 *                                Phase 2 configuration.
1956
 * INPUTS
1957
 *   $ph2ent  : An IPsec Phase 2 configuration
1958
 *   $algo_arr: An array in which all proposal algorithms are collected
1959
 *   $ealg_id : Encryption algorithm ID
1960
 *   $keylen  : Key length for the encryption algorithm
1961
 * RESULT
1962
 *   Returns a string containing all proposal elements, and merges the entries
1963
 *   to $algo_arr as well.
1964
 ******/
1965
function ipsec_setup_proposal_entry(& $ph2ent, & $algo_arr, $ealg_id, $keylen) {
1966
	global $config, $p2_ealgos;
1967
	$proposal = array();
1968

    
1969
	/* If multiple hash algorithms are present, loop through and add them all. */
1970
	if (!empty($ph2ent['hash-algorithm-option']) && is_array($ph2ent['hash-algorithm-option']) 
1971
	    && !strpos($ealg_id, "gcm")) {
1972
		foreach ($ph2ent['hash-algorithm-option'] as $halgo) {
1973
			$proposal[] = ipsec_setup_proposal_algo($ealg_id, $keylen, $halgo, false, $ph2ent['pfsgroup']);
1974
		}
1975
	} else {
1976
		$proposal[] = ipsec_setup_proposal_algo($ealg_id, $keylen, false, false, $ph2ent['pfsgroup']);
1977
	}
1978

    
1979
	/* Add to master list */
1980
	$algo_arr = array_merge($algo_arr, $proposal);
1981
	return implode(',', $proposal);
1982
}
1983

    
1984
/****f* ipsec/ipsec_setup_vtireq
1985
 * NAME
1986
 *   ipsec_setup_vtireq - Setup a VTI type IPsec request
1987
 * INPUTS
1988
 *   $child                : A swanctl child array
1989
 *   $ipsec_vti_cleanup_ifs: An array of VTI interface names for later cleanup
1990
 *   $ikeid                : The IKE ID of this child
1991
 *   $idx                  : The index of this child (for split connections/IKEv1)
1992
 * RESULT
1993
 *   Sets up VTI-specific values for a request.
1994
 ******/
1995
function ipsec_setup_vtireq(& $child, & $ipsec_vti_cleanup_ifs, $ikeid, $idx = 0) {
1996
	$child['reqid'] = get_ipsecifnum($ikeid, $idx);
1997
	/* This interface will be a valid IPsec interface, so remove it from the cleanup list. */
1998
	$ipsec_vti_cleanup_ifs = array_diff($ipsec_vti_cleanup_ifs, array("ipsec{$child['reqid']}"));
1999
	$child['local_ts'] .= ",0.0.0.0/0";
2000
	$child['remote_ts'] .= ",0.0.0.0/0";
2001
}
2002

    
2003
/****f* ipsec/ipsec_setup_tunnels
2004
 * NAME
2005
 *   ipsec_setup_tunnels - Configure all P1/P2 entries as swanctl connections
2006
 * INPUTS
2007
 *   None
2008
 * RESULT
2009
 *   Sets up a swanctl array for all connections in the configuration along with
2010
 *   their children, authentication, etc.
2011
 ******/
2012
function ipsec_setup_tunnels() {
2013
	global $aggressive_mode_psk, $a_client, $config,
2014
		$filterdns_list, $g, $ifacesuse, $ipsec_idhandling, $ipsec_log_cats,
2015
		$ipsec_log_sevs, $ipsec_swanctl_basedir, $ipsec_swanctl_dirs,
2016
		$ipseccfg, $mobile_ipsec_auth, $natfilterrules, $p1_ealgos,
2017
		$p2_ealgos, $rgmap, $sa, $sn, $scconf, $conn, $tunnels,
2018
		$ipsec_vti_cleanup_ifs, $conn_defaults;
2019

    
2020
	foreach ($tunnels as $ph1ent) {
2021
		/* Skip disabled entries */
2022
		if (isset($ph1ent['disabled'])) {
2023
			continue;
2024
		}
2025
		/* If the local source is invalid, skip this entry. */
2026
		$local_spec = ipsec_get_phase1_src($ph1ent);
2027
		if (!$local_spec) {
2028
			continue;
2029
		}
2030

    
2031
		/* Determine the name of this connection, either con-mobile for
2032
		 * mobile clients, or a name based on the IKE ID otherwise. */
2033
		if (isset($ph1ent['mobile'])) {
2034
			$cname = "con-mobile";
2035
			/* Start with common default values */
2036
			$scconf["{$cname}-defaults"] = $conn_defaults;
2037
			/* Array reference to make things easier */
2038
			$conn =& $scconf["{$cname}-defaults"];
2039
			$scconf['connections']["{$cname} : {$cname}-defaults"] = array("# Stub to load con-mobile-defaults");
2040
		} else {
2041
			if (get_ipsecifnum($ph1ent['ikeid'], 0)) {
2042
				$cname = "con" . get_ipsecifnum($ph1ent['ikeid'], 0);
2043
			} else {
2044
				$cname = "con{$ph1ent['ikeid']}00000";
2045
			}
2046
			/* Start with common default values */
2047
			$scconf['connections'][$cname] = $conn_defaults;
2048
			/* Array reference to make things easier */
2049
			$conn =& $scconf['connections'][$cname];
2050
		}
2051

    
2052
		/* Common parameters for all children */
2053
		$child_params = array();
2054
		if (!empty($ph1ent['closeaction'])) {
2055
			$child_params['close_action'] = $ph1ent['closeaction'];
2056
		}
2057

    
2058
		$ikeid = $ph1ent['ikeid'];
2059

    
2060
		/* "trap" adds policies to start a tunnel when interesting
2061
		 * traffic is observed by the host. */
2062
		$start_action = "trap";
2063

    
2064
		/* Set the IKE version appropriately (empty = IKEv1) */
2065
		switch ($ph1ent['iketype']) {
2066
			case 'auto':
2067
				$ikeversion = 0;
2068
				break;
2069
			case 'ikev2':
2070
				$ikeversion = 2;
2071
				break;
2072
			case 'ikev1':
2073
			default:
2074
				$ikeversion = 1;
2075
				break;
2076
		}
2077
		$conn['version'] = $ikeversion;
2078

    
2079
		/* For IKEv1 or auto, setup aggressive mode if configured */
2080
		if ($ikeversion != 2){
2081
			$conn['aggressive'] = ($ph1ent['mode'] == "aggressive") ? "yes" : "no";
2082
		}
2083

    
2084
		if (isset($ph1ent['mobile'])) {
2085
			/* Mobile tunnels allow 'any' as a peer */
2086
			$remote_spec = "0.0.0.0/0,::/0";
2087
			/* Mobile tunnels cannot start automatically */
2088
			$start_action = 'none';
2089
		} else {
2090
			$remote_spec = $ph1ent['remote-gateway'];
2091
			$sourcehost = (is_ipaddr($remote_spec)) ? $remote_spec : $rgmap[$remote_spec];
2092
			$ifacesuse = ipsec_setup_routes($ph1ent['interface'], $ph1ent['protocol'], $sourcehost, isset($ph1ent['gw_duplicates']));
2093
		}
2094

    
2095
		/* Setup IKE proposals */
2096
		if (is_array($ph1ent['encryption']['item'])) {
2097
			$ciphers = array();
2098
			foreach($ph1ent['encryption']['item'] as $p1enc) {
2099
				if (!is_array($p1enc['encryption-algorithm']) ||
2100
						empty($p1enc['encryption-algorithm']['name']) ||
2101
						empty($p1enc['hash-algorithm'])) {
2102
					continue;
2103
				}
2104
				if ($ph1ent['prfselect_enable'] != 'yes') {
2105
					$p1enc['prf-algorithm'] = false;
2106
				}
2107
				$ciphers[] = ipsec_setup_proposal_algo($p1enc['encryption-algorithm']['name'],
2108
									$p1enc['encryption-algorithm']['keylen'],
2109
									$p1enc['hash-algorithm'],
2110
									$p1enc['prf-algorithm'],
2111
									$p1enc['dhgroup']);
2112
			}
2113
			$conn['proposals'] = implode(",", $ciphers);
2114
		}
2115

    
2116
		/* Configure DPD values. The DPD action is a per-child parameter,
2117
		 * not per-connection like the delay and timeout. */
2118
		if ($ph1ent['dpd_delay'] && $ph1ent['dpd_maxfail']) {
2119
			if ($start_action == "trap") {
2120
				$child_params['dpd_action'] = "trap";
2121
			} else {
2122
				$child_params['dpd_action'] = "clear";
2123
			}
2124
			$conn['dpd_delay'] = "{$ph1ent['dpd_delay']}s";
2125
			$conn['dpd_timeout'] =  $ph1ent['dpd_delay'] * ($ph1ent['dpd_maxfail'] + 1) . "s";
2126

    
2127
			/* Adjust dpd_action if the close_action is explicitly set */
2128
			if (!empty($child_params['close_action'])) {
2129
				switch ($ph1ent['closeaction']) {
2130
					case 'trap':
2131
						$child_params['dpd_action'] = 'trap';
2132
						break;
2133
					case 'start':
2134
						$child_params['dpd_action'] = 'restart';
2135
						break;
2136
					case 'none':
2137
					default:
2138
						$child_params['dpd_action'] = 'clear';
2139
				}
2140
			}
2141
		} else {
2142
			$child_params['dpd_action'] = "clear";
2143
		}
2144

    
2145
		/* Rekey (IKEv2 or Auto only, not supported by IKEv1) */
2146
		if (($ikeversion == 0) || ($ikeversion == 2)) {
2147
			$conn['rekey_time'] = ipsec_get_rekey_time($ph1ent) . "s";
2148
		}
2149

    
2150
		/* Reauth (Any) */
2151
		$conn['reauth_time'] = ipsec_get_reauth_time($ph1ent) . "s";
2152

    
2153
		/* Over Time */
2154
		$conn['over_time'] = ipsec_get_over_time($ph1ent) . "s";
2155

    
2156
		/* Rand Time */
2157
		$conn['rand_time'] = ipsec_get_rand_time($ph1ent) . "s";
2158

    
2159
		/* NAT Traversal */
2160
		$conn['encap'] = ($ph1ent['nat_traversal'] == 'force') ? "yes" : "no";
2161

    
2162
		/* MOBIKE support */
2163
		$conn['mobike'] = ($ph1ent['mobike'] == 'on') ? "yes" : "no";
2164

    
2165
		/* TFC Padding */
2166
		if (isset($ph1ent['tfc_enable'])) {
2167
			$conn['tfc_padding'] = (isset($ph1ent['tfc_bytes']) && is_numericint($ph1ent['tfc_bytes'])) ? $ph1ent['tfc_bytes'] : "mtu";
2168
		}
2169

    
2170
		/* Custom Ports */
2171
		if (isset($ph1ent['ikeport'])) {
2172
			/* For a connection without encapsulation, do not set local_port */
2173
			$conn['remote_port'] = $ph1ent['ikeport'];
2174
		} elseif (isset($ph1ent['nattport'])) {
2175
			/* For an encapsulated connection,
2176
			 * set local_port to the server NAT-T port value or default (4500) */
2177
			$conn['local_port'] = isset($config['ipsec']['port_nat_t']) ? $config['ipsec']['port_nat_t'] : 4500;
2178
			$conn['remote_port'] = $ph1ent['nattport'];
2179
		}
2180

    
2181
		/* Arrays for P2s/children */
2182
		$children = array();
2183
		$remote_ts_spec = array();
2184
		$local_ts_spec = array();
2185
		$reqids = array();
2186
		$has_vti = false;
2187
		$ealgoAHsp2arr = array();
2188
		$ealgoESPsp2arr = array();
2189
		$suffix = 0;
2190

    
2191
		foreach ($ph1ent['p2'] as $ph2ent) {
2192
			/* If this entry is disabled, or cannot be configured, skip. */
2193
			if (isset($ph2ent['disabled']) ||
2194
			    (isset($ph2ent['mobile']) && !isset($a_client['enable']))) {
2195
				continue;
2196
			}
2197
			$child = array();
2198
			$local_ts = "";
2199
			$remote_ts = "";
2200
			if (($ph2ent['mode'] == 'tunnel') or ($ph2ent['mode'] == 'tunnel6')) {
2201
				/* Normal tunnel child config */
2202
				$child['mode'] = "tunnel";
2203
				$child['policies'] = "yes";
2204

    
2205
				$localid_type = $ph2ent['localid']['type'];
2206
				$localsubnet_data = ipsec_idinfo_to_cidr($ph2ent['localid'], false, $ph2ent['mode']);
2207

    
2208
				/* Do not print localid in some cases, such as a pure-psk or psk/xauth single phase2 mobile tunnel */
2209
				if (($localid_type == "none" || $localid_type == "mobile") &&
2210
				    isset($ph1ent['mobile']) && (ipsec_get_number_of_phase2($ikeid) == 1)) {
2211
					$local_spec = '0.0.0.0/0,::/0';
2212
				} else {
2213
					if ($localid_type != "address") {
2214
						$localid_type = "subnet";
2215
					}
2216
					// Don't let an empty subnet into config, it can cause parse errors. Ticket #2201.
2217
					if (!is_ipaddr($localsubnet_data) && !is_subnet($localsubnet_data) && ($localsubnet_data != "0.0.0.0/0")) {
2218
						log_error("Invalid IPsec Phase 2 \"{$ph2ent['descr']}\" - {$ph2ent['localid']['type']} has no subnet.");
2219
						continue;
2220
					}
2221
					if (!empty($ph2ent['natlocalid'])) {
2222
						$natlocalsubnet_data = ipsec_idinfo_to_cidr($ph2ent['natlocalid'], false, $ph2ent['mode']);
2223
						if ($ph2ent['natlocalid']['type'] != "address") {
2224
							if (is_subnet($natlocalsubnet_data)) {
2225
								$localsubnet_data = "{$natlocalsubnet_data}|{$localsubnet_data}";
2226
							}
2227
						} else {
2228
							if (is_ipaddr($natlocalsubnet_data)) {
2229
								$localsubnet_data = "{$natlocalsubnet_data}|{$localsubnet_data}";
2230
							}
2231
						}
2232
						$natfilterrules = true;
2233
					}
2234
				}
2235

    
2236
				$local_ts = $localsubnet_data;
2237

    
2238
				if (!isset($ph2ent['mobile'])) {
2239
					$remote_ts = ipsec_idinfo_to_cidr($ph2ent['remoteid'], false, $ph2ent['mode']);
2240
				} else if (!empty($a_client['pool_address'])) {
2241
					$remote_ts = "{$a_client['pool_address']}/{$a_client['pool_netbits']}";
2242
				}
2243

    
2244
			} elseif ($ph2ent['mode'] == 'vti') {
2245
				/* VTI-specific child config */
2246
				$child['policies'] = "no";
2247
				/* VTI cannot use trap policies, so start automatically instead */
2248
				$start_action = 'start';
2249
				if ($child_params['dpd_action'] == "trap") {
2250
					$child_params['dpd_action'] = "restart";
2251
				}
2252
				$localid_type = $ph2ent['localid']['type'];
2253
				$localsubnet_data = ipsec_idinfo_to_cidr($ph2ent['localid'], false, $ph2ent['mode']);
2254
				$local_ts = $localsubnet_data;
2255
				$remote_ts = ipsec_idinfo_to_cidr($ph2ent['remoteid'], false, $ph2ent['mode']);
2256
				$has_vti = true;
2257
			} else {
2258
				/* Transport mode child config */
2259
				$child['mode'] = "transport";
2260
				$child['policies'] = "yes";
2261

    
2262
				if ((($ph1ent['authentication_method'] == "xauth_psk_server") ||
2263
				    ($ph1ent['authentication_method'] == "pre_shared_key")) &&
2264
				    isset($ph1ent['mobile'])) {
2265
					$local_spec = "0.0.0.0/0,::/0";
2266
				} else {
2267
					$local_ts = ipsec_get_phase1_src($ph1ent);
2268
				}
2269

    
2270
				if (!isset($ph2ent['mobile'])) {
2271
					$remote_ts = $remote_spec;
2272
				}
2273
			}
2274

    
2275
			if (!empty($local_ts)) {
2276
				$local_ts_spec[] = $local_ts;
2277
			}
2278
			if (!empty($remote_ts)) {
2279
				$remote_ts_spec[] = $remote_ts;
2280
			}
2281

    
2282
			/* If a PFS group is configured on the Mobile Clients tab,
2283
			 * and this is a mobile P2, use it here. */
2284
			if (isset($a_client['pfs_group']) && isset($ph2ent['mobile'])) {
2285
				$ph2ent['pfsgroup'] = $a_client['pfs_group'];
2286
			}
2287

    
2288
			$reqids[] = $ph2ent['reqid'];
2289

    
2290
			if (!empty($ph2ent['lifetime'])) {
2291
				$child['life_time'] = ipsec_get_life_time($ph2ent) . "s";
2292
				/* Rekey at 90% of lifetime */
2293
				$child['rekey_time'] = ipsec_get_rekey_time($ph2ent) . "s";
2294
				/* Random rekey fuzz time */
2295
				$child['rand_time'] = ipsec_get_rand_time($ph2ent) . "s";
2296
			}
2297

    
2298
			/* If we are to act only as a responder, disable the start action */
2299
			$child['start_action'] = isset($ph1ent['responderonly']) ? 'none' : $start_action;
2300

    
2301
			/* Setup child SA proposals */
2302
			$proposal = array();
2303
			$ph2ent_ealgos = array();
2304
			$ph2ent_ealgos_aead = array();
2305
			if ($ph2ent['protocol'] == 'esp') {
2306
				if (is_array($ph2ent['encryption-algorithm-option'])) {
2307
					foreach ($ph2ent['encryption-algorithm-option'] as $ealg) {
2308
						if (strpos($ealg['name'], "gcm")) {
2309
							/* put AEAD ciphers on top, 
2310
							*  see https://redmine.pfsense.org/issues/11078 */
2311
							$ph2ent_ealgos_aead[] = $ealg;
2312
						} else {
2313
							$ph2ent_ealgos[] = $ealg;
2314
						}
2315
					}
2316
					$ph2ent_ealgos = array_merge(array_reverse($ph2ent_ealgos_aead), $ph2ent_ealgos);
2317
					foreach ($ph2ent_ealgos as $ealg) {
2318
						$ealg_id = $ealg['name'];
2319
						$ealg_kl = $ealg['keylen'];
2320

    
2321
						if (!empty($ealg_kl) && $ealg_kl == "auto") {
2322
							if (empty($p2_ealgos) || !is_array($p2_ealgos)) {
2323
								require_once("ipsec.inc");
2324
							}
2325
							$key_hi = $p2_ealgos[$ealg_id]['keysel']['hi'];
2326
							$key_lo = $p2_ealgos[$ealg_id]['keysel']['lo'];
2327
							$key_step = $p2_ealgos[$ealg_id]['keysel']['step'];
2328
							/* XXX: in some cases where include ordering is suspect these variables
2329
							 * are somehow 0 and we enter this loop forever and timeout after 900
2330
							 * seconds wrecking bootup */
2331
							if ($key_hi != 0 and $key_lo != 0 and $key_step != 0) {
2332
								for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) {
2333
									$proposal[] = ipsec_setup_proposal_entry($ph2ent, $ealgoESPsp2arr, $ealg_id, $keylen);
2334
								}
2335
							}
2336
						} else {
2337
							$proposal[] = ipsec_setup_proposal_entry($ph2ent, $ealgoESPsp2arr, $ealg_id, $ealg_kl);
2338
						}
2339
					}
2340
				}
2341
			} else if ($ph2ent['protocol'] == 'ah') {
2342
				$proposal[] = ipsec_setup_proposal_entry($ph2ent, $ealgoAHsp2arr, '', '');
2343
			}
2344

    
2345
			/* Not mobile, and IKEv1 or Split Connections active */
2346
			if (!isset($ph1ent['mobile']) && (($ikeversion == 1) || isset($ph1ent['splitconn']))) {
2347
				if (!empty($remote_ts)) {
2348
					/* Setup child sub-connections using unique names with a suffix */
2349
					if (get_ipsecifnum($ph1ent['ikeid'], $idx)) {
2350
						$subconnum = get_ipsecifnum($ph1ent['ikeid'], $idx);
2351
					} else {
2352
						$subconnum = "{$ph1ent['ikeid']}00000";
2353
					}
2354

    
2355
					$subconname = "con" . ($subconnum + $suffix);
2356
					$children[$subconname] = $child;
2357
					$children[$subconname]['local_ts'] = $local_ts;
2358
					$children[$subconname]['remote_ts'] = $remote_ts;
2359
					if ($has_vti) {
2360
						ipsec_setup_vtireq($children[$subconname], $ipsec_vti_cleanup_ifs, $ph1ent['ikeid'], $suffix);
2361
					}
2362
					if (!empty($ph2ent['protocol']) && !empty($proposal)) {
2363
						$children[$subconname][$ph2ent['protocol'] . '_proposals'] = implode(',', $proposal);
2364
					}
2365
				} else {
2366
					log_error(sprintf(gettext("No phase2 specifications for tunnel with REQID = %s"), $ikeid));
2367
				}
2368
			} else {
2369
				/* TODO: Fix this to nicely merge all P2 params for single child case */
2370
				if (!is_array($children[$cname])) {
2371
					$children[$cname] = array();
2372
				}
2373
				$children[$cname] = array_merge($children[$cname], $child);
2374
			}
2375
			$suffix++;
2376
		}
2377

    
2378
		$conn['local_addrs'] = $local_spec;
2379
		$conn['remote_addrs'] = $remote_spec;
2380

    
2381
		$pools = array();
2382
		if (isset($ph1ent['mobile'])) {
2383
			if (($ph1ent['authentication_method'] == 'eap-radius') && 
2384
			    empty($a_client['pool_address']) && empty($a_client['pool_address_v6'])) {
2385
				$pools[] = "radius";
2386
			} else {
2387
				if (!empty($a_client['pool_address'])) {
2388
					$pools[] = "mobile-pool-v4";
2389
				}
2390
				if (!empty($a_client['pool_address_v6'])) {
2391
					$pools[] = "mobile-pool-v6";
2392
				}
2393
				if (isset($a_client['radius_ip_priority_enable'])) {
2394
					$pools[] .= "radius";
2395
				}
2396
			}
2397
		}
2398
		if (!empty($pools)) {
2399
			$conn['pools'] = implode(', ', $pools);
2400
		}
2401

    
2402
		/* For IKEv2 without Split Connections, setup combined sets of
2403
		 * local/remote traffic selectors and propsals */
2404
		if (!(!isset($ph1ent['mobile']) && (($ikeversion == 1) || isset($ph1ent['splitconn'])))) {
2405
			if (!isset($ph1ent['mobile']) && !empty($remote_ts_spec)) {
2406
				$children[$cname]['remote_ts'] = implode(",", $remote_ts_spec);
2407
			}
2408
			if (!empty($local_ts_spec)) {
2409
				$children[$cname]['local_ts'] = implode(",", $local_ts_spec);
2410
			}
2411
			if ($has_vti) {
2412
				ipsec_setup_vtireq($children[$cname], $ipsec_vti_cleanup_ifs, $ph1ent['ikeid']);
2413
			}
2414
			if (!empty($ealgoAHsp2arr)) {
2415
				$children[$cname]['ah_proposals'] = implode(',', array_unique($ealgoAHsp2arr));
2416
			}
2417
			if (!empty($ealgoESPsp2arr)) {
2418
				$children[$cname]['esp_proposals'] = implode(',', array_unique($ealgoESPsp2arr));
2419
			}
2420
		}
2421

    
2422
		/* Setup connection authentication */
2423
		ipsec_setup_authentication($ph1ent, $conn);
2424

    
2425
		/* Add children to this connection, including default child parameters */
2426
		if (count($children)) {
2427
			foreach($children as $name => $child) {
2428
				$conn['children'][$name] = array_merge($child_params, $child);
2429
			}
2430
		}
2431

    
2432
	}
2433

    
2434
	return;
2435
}
2436

    
2437
/****f* ipsec/ipsec_setup_secrets
2438
 * NAME
2439
 *   ipsec_setup_secrets - Setup swanctl authentication secrets
2440
 * INPUTS
2441
 *   None
2442
 * RESULT
2443
 *   Returns a swanctl array containing secrets (PSKs, certs, etc) and writes out
2444
 *   necessary CA, CRL, and certificate data.
2445
 ******/
2446
function ipsec_setup_secrets() {
2447
	global $config, $a_phase1, $ipsec_swanctl_dirs, $ipseccfg, $rgmap, $scconf;
2448

    
2449
	$suffix = 0;
2450

    
2451
	/* write out CRL files */
2452
	if (is_array($config['crl']) && count($config['crl'])) {
2453
		foreach ($config['crl'] as $crl) {
2454
			if (!isset($crl['text'])) {
2455
				log_error(sprintf(gettext("Warning: Missing CRL data for %s"), $crl['descr']));
2456
				continue;
2457
			}
2458
			$fpath = "{$ipsec_swanctl_dirs['crlpath']}/{$crl['refid']}.crl";
2459
			if (!@file_put_contents($fpath, base64_decode($crl['text']))) {
2460
				log_error(sprintf(gettext("Error: Cannot write IPsec CRL file for %s"), $crl['descr']));
2461
				continue;
2462
			}
2463
		}
2464
	}
2465

    
2466
	$vpncas = array();
2467
	if (is_array($a_phase1) && count($a_phase1)) {
2468
		foreach ($a_phase1 as $ph1ent) {
2469
			if (isset($ph1ent['disabled'])) {
2470
				continue;
2471
			}
2472

    
2473
			if (strstr($ph1ent['authentication_method'], 'cert') ||
2474
			    in_array($ph1ent['authentication_method'], array('eap-mschapv2', 'eap-tls', 'eap-radius'))) {
2475
				/* Write certificate and private key, point to private key */
2476
				$certline = '';
2477

    
2478
				$ikeid = $ph1ent['ikeid'];
2479
				$cert = lookup_cert($ph1ent['certref']);
2480

    
2481
				if (!$cert) {
2482
					log_error(sprintf(gettext("Error: Invalid phase1 certificate reference for %s"), $ph1ent['name']));
2483
					continue;
2484
				}
2485

    
2486
				/* add signing CA cert chain of server cert
2487
				 * to the list of CAs to write
2488
				 */
2489
				$cachain = ca_chain_array($cert);
2490
				if ($cachain && is_array($cachain)) {
2491
					foreach ($cachain as $cacrt) {
2492
						$vpncas[$cacrt['refid']] = $cacrt;
2493
					}
2494
				}
2495

    
2496
				@chmod($ipsec_swanctl_dirs['certpath'], 0600);
2497

    
2498
				$ph1keyfile = "{$ipsec_swanctl_dirs['keypath']}/cert-{$ikeid}.key";
2499
				if (!file_put_contents($ph1keyfile, base64_decode($cert['prv']))) {
2500
					log_error(sprintf(gettext("Error: Cannot write phase1 key file for %s"), $ph1ent['name']));
2501
					continue;
2502
				}
2503
				@chmod($ph1keyfile, 0600);
2504

    
2505
				$ph1certfile = "{$ipsec_swanctl_dirs['certpath']}/cert-{$ikeid}.crt";
2506
				if (!file_put_contents($ph1certfile, base64_decode($cert['crt']))) {
2507
					log_error(sprintf(gettext("Error: Cannot write phase1 certificate file for %s"), $ph1ent['name']));
2508
					@unlink($ph1keyfile);
2509
					continue;
2510
				}
2511
				@chmod($ph1certfile, 0600);
2512

    
2513
				$scconf['secrets']['private-' . $suffix++] = array('file' => $ph1keyfile);
2514
			} else if (strstr($ph1ent['authentication_method'], 'pkcs11')) {
2515
				$p11_id = array();
2516
				$output = shell_exec('/usr/local/bin/pkcs15-tool -c');
2517
				preg_match_all('/ID\s+: (.*)/', $output, $p11_id);
2518
				if (!empty($ph1ent['pkcs11certref']) && in_array($ph1ent['pkcs11certref'], $p11_id[1])) {
2519
					$scconf['secrets']['token-' . $suffix++] = array(
2520
						'handle' => $ph1ent['pkcs11certref'],
2521
						'pin' => $ph1ent['pkcs11pin'],
2522
					);
2523
				} else {
2524
					log_error(sprintf(gettext("Error: Invalid phase1 PKCS#11 certificate reference or PKCS#11 is not present for %s"), $ph1ent['name']));
2525
					continue;
2526
				}
2527
			} else {
2528
				/* Setup pre-shared keys */
2529
				list($myid_type, $myid_data) = ipsec_find_id($ph1ent, 'local');
2530
				list($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, 'peer', $rgmap);
2531
				$myid = trim($myid_data);
2532

    
2533
				if (empty($peerid_data)) {
2534
					continue;
2535
				}
2536

    
2537
				$myid = isset($ph1ent['mobile']) ? ipsec_fixup_id($myid_type, trim($myid_data)) : "%any";
2538
				$peerid = ($peerid_data != 'allusers') ? ipsec_fixup_id($peerid_type, trim($peerid_data)) : '';
2539

    
2540
				if (!empty($ph1ent['pre-shared-key'])) {
2541
					$scconf['secrets']['ike-' . $suffix++] = array(
2542
						'secret' => '0s' . base64_encode(trim($ph1ent['pre-shared-key'])),
2543
						'id-0' => $myid,
2544
						'id-1' => $peerid,
2545
					);
2546
					if (isset($ph1ent['mobile'])) {
2547
						$scconf['secrets']['ike-' . $suffix++] = array(
2548
							'secret' => '0s' . base64_encode(trim($ph1ent['pre-shared-key'])),
2549
							'id-0' => $myid,
2550
							'id-1' => '%any',
2551
						);
2552
						$scconf['secrets']['ike-' . $suffix++] = array(
2553
							'secret' => '0s' . base64_encode(trim($ph1ent['pre-shared-key'])),
2554
						);
2555
					}
2556
				}
2557
			}
2558

    
2559
			/* if the client authenticates with a cert add the
2560
			 * client cert CA chain to the list of CAs to write
2561
			 */
2562
			if (in_array($ph1ent['authentication_method'],
2563
			    array('cert', 'eap-tls', 'xauth_cert_server', 'pkcs11'))) {
2564
				if (!empty($ph1ent['caref']) && !array_key_exists($ph1ent['caref'], $vpncas)) {
2565
					$thisca = lookup_ca($ph1ent['caref']);
2566
					$vpncas[$ph1ent['caref']] = $thisca;
2567
					/* follow chain up to root */
2568
					$cachain = ca_chain_array($thisca);
2569
					if ($cachain and is_array($cachain)) {
2570
						foreach ($cachain as $cacrt) {
2571
							$vpncas[$cacrt['refid']] = $cacrt;
2572
						}
2573
					}
2574
				}
2575
			}
2576
		}
2577
	}
2578

    
2579
	/* Write the required CAs */
2580
	foreach ($vpncas as $carefid => $cadata) {
2581
		$cacrt = base64_decode($cadata['crt']);
2582
		$cacrtattrs = openssl_x509_parse($cacrt);
2583
		if (!is_array($cacrtattrs) || !isset($cacrtattrs['hash'])) {
2584
			log_error(sprintf(gettext("Error: Invalid certificate hash info for %s"), $cadata['descr']));
2585
			continue;
2586
		}
2587
		$cafilename = "{$ipsec_swanctl_dirs['capath']}/{$cacrtattrs['hash']}.0";
2588
		if (!@file_put_contents($cafilename, $cacrt)) {
2589
				log_error(sprintf(gettext("Error: Cannot write IPsec CA file for %s"), $cadata['descr']));
2590
				continue;
2591
		}
2592
	}
2593

    
2594
	/* Add user PSKs */
2595
	if (is_array($config['system']) && is_array($config['system']['user'])) {
2596
		foreach ($config['system']['user'] as $user) {
2597
			if (!empty($user['ipsecpsk'])) {
2598
				$scconf['secrets']['ike-' . $suffix++] = array(
2599
					'secret' => '0s' . base64_encode(trim($user['ipsecpsk'])),
2600
					'id-0' => $myid,
2601
					'id-1' => $user['name'],
2602
				);
2603
			}
2604
		}
2605
		unset($user);
2606
	}
2607

    
2608
	/* add PSKs for mobile clients */
2609
	if (is_array($ipseccfg['mobilekey'])) {
2610
		foreach ($ipseccfg['mobilekey'] as $key) {
2611
			if (($key['ident'] == 'allusers') ||
2612
			    ($key['ident'] == 'any')) {
2613
				$key['ident'] = '%any';
2614
			}
2615
			$userkeyprefix = (strtolower($key['type']) == 'eap') ? 'eap' : 'ike';
2616
			$scconf['secrets'][$userkeyprefix . '-' . $suffix++] = array(
2617
				'secret' => '0s' . base64_encode(trim($key['pre-shared-key'])),
2618
				'id-0' => $key['ident'],
2619
			);
2620
		}
2621
		unset($key);
2622
	}
2623
	return;
2624
}
2625

    
2626
/****f* ipsec/ipsec_configure
2627
 * NAME
2628
 *   ipsec_configure - Configure IPsec
2629
 * INPUTS
2630
 *   $restart: Boolean (default false), whether or not to restart the IPsec
2631
 *             daemons.
2632
 * RESULT
2633
 *   IPsec-related configuration files are written, daemon is started or stopped
2634
 *   appropriately.
2635
 ******/
2636
function ipsec_configure($restart = false) {
2637
	global $aggressive_mode_psk, $a_client, $a_phase1, $a_phase2, $config,
2638
		$filterdns_list, $g, $ifacesuse, $ipsec_idhandling, $ipsec_log_cats,
2639
		$ipsec_log_sevs, $ipsec_swanctl_basedir, $ipsec_swanctl_dirs,
2640
		$ipseccfg, $mobile_ipsec_auth, $natfilterrules, $p1_ealgos,
2641
		$p2_ealgos, $rgmap, $sa, $sn, $scconf, $tunnels, $mobile_configured,
2642
		$ipsec_vti_cleanup_ifs, $conn_defaults, $pool_addrs;
2643

    
2644
	/* service may have been enabled, disabled, or otherwise changed in a
2645
	 *way requiring rule updates */
2646
	filter_configure();
2647

    
2648
	if (!ipsec_enabled()) {
2649
		/* IPsec is disabled */
2650
		/* Stop charon */
2651
		mwexec("/usr/local/sbin/strongswanrc stop");
2652
		/* Stop dynamic monitoring */
2653
		killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
2654
		/* Wait for process to die */
2655
		sleep(2);
2656
		/* Shutdown enc0 interface*/
2657
		mwexec("/sbin/ifconfig enc0 down");
2658
		ipsec_gre_default_mtu(); 
2659
		return 0;
2660
	} else {
2661
		/* Startup enc0 interface */
2662
		mwexec("/sbin/ifconfig enc0 up");
2663
	}
2664

    
2665
	if (platform_booting()) {
2666
		echo gettext("Configuring IPsec VPN... ");
2667
	}
2668

    
2669
	$ipsecstartlock = lock('ipsec', LOCK_EX);
2670

    
2671
	/* Prepare automatic ping_hosts.sh data */
2672
	unlink_if_exists("{$g['vardb_path']}/ipsecpinghosts");
2673
	touch("{$g['vardb_path']}/ipsecpinghosts");
2674
	$ipsecpinghostsactive = false;
2675

    
2676
	/* Populate convenience variables */
2677
	$syscfg = $config['system'];
2678
	init_config_arr(array('ipsec', 'phase1'));
2679
	$ipseccfg = $config['ipsec'];
2680
	$a_phase1 = $config['ipsec']['phase1'];
2681
	init_config_arr(array('ipsec', 'phase2'));
2682
	$a_phase2 = $config['ipsec']['phase2'];
2683
	init_config_arr(array('ipsec', 'client'));
2684
	$a_client = $config['ipsec']['client'];
2685

    
2686
	$mobile_configured = false;
2687

    
2688
	/* Setup a single structured array to process, to avoid repeatedly
2689
	 * looping through the arrays to locate entries later. */
2690
	$tunnels = array();
2691
	foreach ($a_phase1 as $p1) {
2692
		if (empty($p1)) {
2693
			continue;
2694
		}
2695
		if (isset($p1['mobile']) && !isset($p1['disabled'])) {
2696
			$mobile_configured = true;
2697
		}
2698
		$tunnels[$p1['ikeid']] = $p1;
2699
		$tunnels[$p1['ikeid']]['p2'] = array();
2700
	}
2701
	foreach ($a_phase2 as $p2) {
2702
		$tunnels[$p2['ikeid']]['p2'][] = $p2;
2703
	}
2704

    
2705
	$ipsec_vti_cleanup_ifs = array();
2706
	$rgmap = array();
2707
	$filterdns_list = array();
2708
	$aggressive_mode_psk = false;
2709
	$mobile_ipsec_auth = "";
2710
	$ifacesuse = array();
2711
	$natfilterrules = false;
2712

    
2713
	/* Configure asynchronous crypto. See https://redmine.pfsense.org/issues/8772 */
2714
	set_sysctl(array('net.inet.ipsec.async_crypto' => (int) (isset($ipseccfg['async_crypto']) && ($ipseccfg['async_crypto'] == "enabled"))));
2715

    
2716
	/* Build a list of all IPsec interfaces configured on the firewall at the OS level */
2717
	foreach (get_interface_arr() as $thisif) {
2718
		if (substr($thisif, 0, 5) == "ipsec") {
2719
			$ipsec_vti_cleanup_ifs[] = $thisif;
2720
		}
2721
	}
2722

    
2723
	/* Create directory structure for IPsec */
2724
	ipsec_create_dirs();
2725

    
2726
	/* Setup gateways and interfaces */
2727
	ipsec_setup_gwifs();
2728

    
2729
	/* Setup and write strongswan.conf */
2730
	ipsec_setup_strongswan();
2731

    
2732
	/* Start Global Connection default values */
2733
	$conn_defaults = array();
2734
	/* Fragmentation is on for everyone */
2735
	$conn_defaults['fragmentation'] = "yes";
2736
	/* Default to 'replace' for unique IDs (was 'yes' in ipsec.conf previously) */
2737
	$conn_defaults['unique'] = 'replace';
2738
	/* If the configuration has a valid alternate value for unique ID handling,
2739
	 * use it instead. */
2740
	if (!empty($config['ipsec']['uniqueids']) &&
2741
	    array_key_exists($config['ipsec']['uniqueids'], $ipsec_idhandling)) {
2742
		$conn_defaults['unique'] = $config['ipsec']['uniqueids'];
2743
	}
2744
	/* Disable ipcomp for now. redmine #6167
2745
	if (isset($config['ipsec']['compression'])) {
2746
		$conn_defaults['compress'] = "yes";
2747
	}
2748
	set_single_sysctl('net.inet.ipcomp.ipcomp_enable', (isset($config['ipsec']['compression'])) ? 1 : 0);
2749
	*/
2750
	/* End Global Connection Defaults */
2751

    
2752
	/* Start swanctl configuration (scconf) */
2753
	$scconf = array();
2754
	$scconf[] = "# This file is automatically generated. Do not edit";
2755
	$scconf['connections'] = array();
2756
	/* Setup IPsec bypass */
2757
	ipsec_setup_bypass();
2758
	/* Setup connections */
2759
	ipsec_setup_tunnels();
2760
	$scconf['pools'] = array();
2761
	if ($mobile_configured) {
2762
		/* Setup mobile address pools */
2763
		ipsec_setup_pools();
2764
		/* Setup per-user pools */
2765
		ipsec_setup_userpools();
2766
	}
2767
	/* Setup secret data */
2768
	$scconf['secrets'] = array();
2769
	ipsec_setup_secrets();
2770

    
2771
	@file_put_contents("{$g['varetc_path']}/ipsec/swanctl.conf", ipsec_strongswan_confgen($scconf));
2772

    
2773
	/* Clean up unused VTI interfaces */
2774
	foreach ($ipsec_vti_cleanup_ifs as $cleanif) {
2775
		if (does_interface_exist($cleanif)) {
2776
			mwexec("/sbin/ifconfig " . escapeshellarg($cleanif) . " destroy", false);
2777
		}
2778
	}
2779

    
2780
	/* set default MTU to 1400 for GRE over IPsec, othewise to 1476 */
2781
	ipsec_gre_default_mtu(); 
2782

    
2783
	/* Manage process */
2784
	if ($restart === true) {
2785
		mwexec_bg("/usr/local/sbin/strongswanrc restart", false);
2786
	} else {
2787
		if (isvalidpid("{$g['varrun_path']}/charon.pid")) {
2788
			mwexec_bg("/usr/local/sbin/strongswanrc reload", false);
2789
		} else {
2790
			mwexec_bg("/usr/local/sbin/strongswanrc start", false);
2791
		}
2792
	}
2793

    
2794
	// Run ping_hosts.sh once if it's enabled to avoid wait for minicron
2795
	if ($ipsecpinghostsactive) {
2796
		mwexec_bg("/usr/local/bin/ping_hosts.sh");
2797
	}
2798

    
2799
	if ($natfilterrules == true) {
2800
		filter_configure();
2801
	}
2802
	/* start filterdns, if necessary */
2803
	if (count($filterdns_list) > 0) {
2804
		$interval = 60;
2805
		if (!empty($ipseccfg['dns-interval']) && is_numeric($ipseccfg['dns-interval'])) {
2806
			$interval = $ipseccfg['dns-interval'];
2807
		}
2808

    
2809
		$hostnames = "";
2810
		array_unique($filterdns_list);
2811
		foreach ($filterdns_list as $hostname) {
2812
			$hostnames .= "cmd {$hostname} '/usr/local/sbin/pfSctl -c \"service reload ipsecdns\"'\n";
2813
		}
2814
		file_put_contents("{$g['varetc_path']}/ipsec/filterdns-ipsec.hosts", $hostnames);
2815
		unset($hostnames);
2816

    
2817
		if (isvalidpid("{$g['varrun_path']}/filterdns-ipsec.pid")) {
2818
			sigkillbypid("{$g['varrun_path']}/filterdns-ipsec.pid", "HUP");
2819
		} else {
2820
			mwexec_bg("/usr/local/sbin/filterdns -p {$g['varrun_path']}/filterdns-ipsec.pid -i {$interval} -c {$g['varetc_path']}/ipsec/filterdns-ipsec.hosts -d 1");
2821
		}
2822
	} else {
2823
		killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
2824
		@unlink("{$g['varrun_path']}/filterdns-ipsec.pid");
2825
	}
2826

    
2827
	if (platform_booting()) {
2828
		echo "done\n";
2829
	}
2830

    
2831
	unlock($ipsecstartlock);
2832
	return count($filterdns_list);
2833
}
2834

    
2835
function ipsec_gre_default_mtu() {
2836
	global $config;
2837

    
2838
	foreach ($config['interfaces'] as $if => $ifdetail) { 
2839
		if (interface_is_type($ifdetail['if'], 'gre') && !isset($ifdetail['mtu'])) {
2840
			if (is_greipsec($ifdetail['if'])) {
2841
				set_interface_mtu($ifdetail['if'], 1400);
2842
			} else {
2843
				set_interface_mtu($ifdetail['if'], 1476);
2844
			}
2845
		}
2846
	}
2847
}
2848

    
2849
/* Return the larger of derived SA rekey time and reauth time */
2850
function ipsec_get_renewmax($entry) {
2851
	if (empty($entry) || !is_array($entry)) {
2852
		return 0;
2853
	}
2854
	return max(ipsec_get_rekey_time($entry), ipsec_get_reauth_time($entry));
2855
}
2856

    
2857
/* Determine the life time of an SA entry (Hard upper total time limit for SA before it is removed) */
2858
function ipsec_get_life_time($entry) {
2859
	if (empty($entry) || !is_array($entry)) {
2860
		return 0;
2861
	}
2862
	/* Use a hardcoded value if present in the configuration */
2863
	if ($entry['lifetime'] > 0) {
2864
		return $entry['lifetime'];
2865
	}
2866
	/* If rekey or reauth are enabled, attempt to derive a lifetime from one of those */
2867
	$renewmax = ipsec_get_renewmax($entry);
2868
	if ($renewmax > 0) {
2869
		return intval($renewmax / 0.9);
2870
	}
2871
	/* To reach here, rekey_time and lifetime are both 0 which is invalid
2872
	 * Default to 16000 for p1 and 4000 for p2 */
2873
	if ($entry['iketype']) {
2874
		return 16000;
2875
	} else {
2876
		return 4000;
2877
	}
2878
}
2879

    
2880
/* Determine the rekey time of an SA entry (Time at which to rekey IKEv2 or Child SA entries) */
2881
function ipsec_get_rekey_time($entry) {
2882
	if (empty($entry) || !is_array($entry)) {
2883
		return 0;
2884
	}
2885
	/* Use a hardcoded value if present in the configuration */
2886
	if (strlen($entry['rekey_time'])) {
2887
		/* Check via strlen since 0 is a valid value */
2888
		return $entry['rekey_time'];
2889
	} elseif ($entry['lifetime'] > 0) {
2890
		/* If rekey_time is empty and lifetime is non-zero, use 90% lifetime */
2891
		return intval($entry['lifetime'] * 0.9);
2892
	}
2893
	/* To reach here, rekey_time and lifetime are empty
2894
	 * Default to 14400 for p1 and 3600 for p2 */
2895
	if ($entry['iketype']) {
2896
		return 14400;
2897
	} else {
2898
		return 3600;
2899
	}
2900
}
2901

    
2902
/* Determine the reauth time of an SA entry (IKE SA tear-down/reauthenticate) */
2903
function ipsec_get_reauth_time($entry) {
2904
	if (empty($entry) || !is_array($entry)) {
2905
		return 0;
2906
	}
2907
	/* Use a hardcoded value if present in the configuration */
2908
	if (strlen($entry['reauth_time'])) {
2909
		/* Check via strlen since 0 is a valid value */
2910
		return $entry['reauth_time'];
2911
	} elseif ($entry['lifetime'] > 0) {
2912
		/* If reauth_time is empty and lifetime is non-zero,
2913
		 * use 90% lifetime for IKEv1, disable for IKEv2/auto */
2914
		if ($entry['iketype'] == 'ikev1') {
2915
			return intval($entry['lifetime'] * 0.9);
2916
		} else {
2917
			return 0;
2918
		}
2919
	}
2920
	/* To reach here, rekey_time and lifetime are empty
2921
	 * Default to disabled (0) */
2922
	return 0;
2923
}
2924

    
2925
/* Determine the over time of an SA entry (Hard upper IKE SA time limit, relative to rekey/reauth time) */
2926
function ipsec_get_over_time($entry) {
2927
	if (empty($entry) || !is_array($entry)) {
2928
		return 0;
2929
	}
2930
	/* Automatically derive the value for rand_time */
2931
	$lifetime = ipsec_get_life_time($entry);
2932
	$renewmax = ipsec_get_renewmax($entry);
2933
	if (($lifetime > 0) && ($renewmax > 0)) {
2934
		/* If life time and rekey/reauth time both have values, subtract to get rand time */
2935
		return $lifetime - $renewmax;
2936
	} elseif ($lifetime > 0) {
2937
		/* If only life time has a value, use 10% of that */
2938
		return intval($lifetime * 0.1);
2939
	} elseif ($renewmax > 0) {
2940
		/* If only rekey/reauth time has a value, use 10% of that */
2941
		return intval($renewmax * 0.1);
2942
	}
2943
	/* No value can be determined, default to 0 */
2944
	return 0;
2945
}
2946

    
2947
/* Determine the rand time of an SA entry (random value subtracted from renewal time to prevent collisions) */
2948
function ipsec_get_rand_time($entry) {
2949
	if (empty($entry) || !is_array($entry)) {
2950
		return 0;
2951
	}
2952
	/* Use a hardcoded value if present in the configuration */
2953
	if (strlen($entry['rand_time'])) {
2954
		/* Check via strlen since 0 is a valid value */
2955
		return $entry['rand_time'];
2956
	}
2957
	/* Logic to automatically determine rand time is identical to calculating over time */
2958
	return ipsec_get_over_time($entry);
2959
}
2960

    
2961
?>
(26-26/61)
OSZAR »