Project

General

Profile

Download (20.6 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-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

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

    
44
global $ipsec_log_sevs;
45
$ipsec_log_sevs = array(
46
	'-1' => gettext('Silent'),
47
	'0' => gettext('Audit'),
48
	'1' => gettext('Control'),
49
	'2' => gettext('Diag'),
50
	'3' => gettext('Raw'),
51
	'4' => gettext('Highest')
52
);
53

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

    
74
global $my_identifier_list;
75
$my_identifier_list = array(
76
	'myaddress' => array('desc' => gettext('My IP address'), 'mobile' => true),
77
	'address' => array('desc' => gettext('IP address'), 'mobile' => true),
78
	'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
79
	'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
80
	'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
81
	'keyid tag' => array('desc' => gettext('KeyID tag'), 'mobile' => true),
82
	'dyn_dns' => array('desc' => gettext('Dynamic DNS'), 'mobile' => true)
83
);
84

    
85
global $peer_identifier_list;
86
$peer_identifier_list = array(
87
	'any' => array('desc' => gettext('Any'), 'mobile' => true),
88
	'peeraddress' => array('desc' => gettext('Peer IP address'), 'mobile' => false),
89
	'address' => array('desc' => gettext('IP address'), 'mobile' => false),
90
	'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
91
	'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
92
	'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
93
	'keyid tag' => array('desc' =>gettext('KeyID tag'), 'mobile' => true)
94
);
95

    
96
global $ipsec_idhandling;
97
$ipsec_idhandling = array(
98
	'yes' => 'YES', 'no' => 'NO', 'never' => 'NEVER', 'keep' => 'KEEP'
99
);
100

    
101
global $p1_ealgos;
102
$p1_ealgos = array(
103
	'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
104
	'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
105
	'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
106
	'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
107
	'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
108
	'3des' => array('name' => '3DES'),
109
	'cast128' => array('name' => 'CAST128')
110
);
111

    
112
global $p2_ealgos;
113
$p2_ealgos = array(
114
	'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
115
	'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
116
	'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
117
	'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
118
	'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
119
	'3des' => array('name' => '3DES'),
120
	'cast128' => array('name' => 'CAST128')
121
);
122

    
123
global $p1_halgos;
124
$p1_halgos = array(
125
	'md5' => 'MD5',
126
	'sha1' => 'SHA1',
127
	'sha256' => 'SHA256',
128
	'sha384' => 'SHA384',
129
	'sha512' => 'SHA512',
130
	'aesxcbc' => 'AES-XCBC'
131
);
132

    
133
global $p1_dhgroups;
134
$p1_dhgroups = array(
135
	1  => '1 (768 bit)',
136
	2  => '2 (1024 bit)',
137
	5  => '5 (1536 bit)',
138
	14 => '14 (2048 bit)',
139
	15 => '15 (3072 bit)',
140
	16 => '16 (4096 bit)',
141
	17 => '17 (6144 bit)',
142
	18 => '18 (8192 bit)',
143
	19 => '19 (nist ecp256)',
144
	20 => '20 (nist ecp384)',
145
	21 => '21 (nist ecp521)',
146
	22 => '22 (1024(sub 160) bit)',
147
	23 => '23 (2048(sub 224) bit)',
148
	24 => '24 (2048(sub 256) bit)',
149
	28 => '28 (brainpool ecp256)',
150
	29 => '29 (brainpool ecp384)',
151
	30 => '30 (brainpool ecp512)'
152
);
153

    
154
global $p2_halgos;
155
$p2_halgos = array(
156
	'hmac_md5' => 'MD5',
157
	'hmac_sha1' => 'SHA1',
158
	'hmac_sha256' => 'SHA256',
159
	'hmac_sha384' => 'SHA384',
160
	'hmac_sha512' => 'SHA512',
161
	'aesxcbc' => 'AES-XCBC'
162
);
163

    
164
global $p1_authentication_methods;
165
$p1_authentication_methods = array(
166
	'hybrid_rsa_server' => array('name' => gettext('Hybrid RSA + Xauth'), 'mobile' => true),
167
	'xauth_rsa_server' => array('name' => gettext('Mutual RSA + Xauth'), 'mobile' => true),
168
	'xauth_psk_server' => array('name' => gettext('Mutual PSK + Xauth'), 'mobile' => true),
169
	'eap-tls' => array('name' => gettext('EAP-TLS'), 'mobile' => true),
170
	'eap-radius' => array('name' => gettext('EAP-RADIUS'), 'mobile' => true),
171
	'eap-mschapv2' => array('name' => gettext('EAP-MSChapv2'), 'mobile' => true),
172
	'rsasig' => array('name' => gettext('Mutual RSA'), 'mobile' => false),
173
	'pre_shared_key' => array('name' => gettext('Mutual PSK'), 'mobile' => false)
174
);
175

    
176
global $ipsec_preshared_key_type;
177
$ipsec_preshared_key_type = array(
178
	'PSK' => 'PSK',
179
	'EAP' => 'EAP'
180
);
181

    
182
global $p2_modes;
183
$p2_modes = array(
184
	'tunnel' => gettext('Tunnel IPv4'),
185
	'tunnel6' => gettext('Tunnel IPv6'),
186
	'transport' => gettext('Transport')
187
);
188

    
189
global $p2_protos;
190
$p2_protos = array(
191
	'esp' => 'ESP',
192
	'ah' => 'AH'
193
);
194

    
195
global $p2_pfskeygroups;
196
$p2_pfskeygroups = array(
197
	0 => gettext('off'),
198
	1  => gettext('1 (768 bit)'),
199
	2  => gettext('2 (1024 bit)'),
200
	5  => gettext('5 (1536 bit)'),
201
	14 => gettext('14 (2048 bit)'),
202
	15 => gettext('15 (3072 bit)'),
203
	16 => gettext('16 (4096 bit)'),
204
	17 => gettext('17 (6144 bit)'),
205
	18 => gettext('18 (8192 bit)'),
206
	19 => gettext('19 (nist ecp256)'),
207
	20 => gettext('20 (nist ecp384)'),
208
	21 => gettext('21 (nist ecp521)'),
209
	22 => gettext('22 (1024(sub 160) bit)'),
210
	23 => gettext('23 (2048(sub 224) bit)'),
211
	24 => gettext('24 (2048(sub 256) bit)'),
212
	28 => gettext('28 (brainpool ecp256)'),
213
	29 => gettext('29 (brainpool ecp384)'),
214
	30 => gettext('30 (brainpool ecp512)')
215
);
216

    
217
function ipsec_enabled() {
218
	global $config;
219

    
220
	if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
221
		return false;
222
	}
223

    
224
	/* Check if we have at least one phase 1 entry. */
225
	if (!isset($config['ipsec']['phase1']) ||
226
	    !is_array($config['ipsec']['phase1']) ||
227
	    empty($config['ipsec']['phase1'])) {
228
		return false;
229
	}
230
	/* Check if at least one phase 1 entry is enabled. */
231
	foreach ($config['ipsec']['phase1'] as $phase1) {
232
		if (!isset($phase1['disabled'])) {
233
			return true;
234
		}
235
	}
236

    
237
	return false;
238
}
239

    
240
/*
241
 * ikeid management functions
242
 */
243

    
244
function ipsec_ikeid_used($ikeid) {
245
	global $config;
246

    
247
	foreach ($config['ipsec']['phase1'] as $ph1ent) {
248
		if ($ikeid == $ph1ent['ikeid']) {
249
			return true;
250
		}
251
	}
252

    
253
	return false;
254
}
255

    
256
function ipsec_ikeid_next() {
257

    
258
	$ikeid = 1;
259
	while (ipsec_ikeid_used($ikeid)) {
260
		$ikeid++;
261
	}
262

    
263
	return $ikeid;
264
}
265

    
266
/*
267
 * Return phase1 local address
268
 */
269
function ipsec_get_phase1_src(& $ph1ent) {
270

    
271
	if ($ph1ent['interface']) {
272
		if (substr($ph1ent['interface'], 0, 4) == "_vip") {
273
			$if = $ph1ent['interface'];
274
		} else {
275
			$if = get_failover_interface($ph1ent['interface']);
276
		}
277
	} else {
278
		$if = "wan";
279
	}
280
	if ($ph1ent['protocol'] == "inet6") {
281
		$interfaceip = get_interface_ipv6($if);
282
	} else {
283
		$interfaceip = get_interface_ip($if);
284
	}
285

    
286
	return $interfaceip;
287
}
288

    
289
/*
290
 * Return phase1 local address
291
 */
292
function ipsec_get_phase1_dst(& $ph1ent) {
293
	global $g;
294

    
295
	if (empty($ph1ent['remote-gateway'])) {
296
		return false;
297
	}
298
	$rg = $ph1ent['remote-gateway'];
299
	if (!is_ipaddr($rg)) {
300
		if (!platform_booting()) {
301
			return resolve_retry($rg);
302
		}
303
	}
304
	if (!is_ipaddr($rg)) {
305
		return false;
306
	}
307

    
308
	return $rg;
309
}
310

    
311
/*
312
 * Return phase2 idinfo in cidr format
313
 */
314
function ipsec_idinfo_to_cidr(& $idinfo, $addrbits = false, $mode = "") {
315
	global $config;
316

    
317
	switch ($idinfo['type']) {
318
		case "address":
319
			if ($addrbits) {
320
				if ($mode == "tunnel6") {
321
					return $idinfo['address']."/128";
322
				} else {
323
					return $idinfo['address']."/32";
324
				}
325
			} else {
326
				return $idinfo['address'];
327
			}
328
			break; /* NOTREACHED */
329
		case "network":
330
			return "{$idinfo['address']}/{$idinfo['netbits']}";
331
			break; /* NOTREACHED */
332
		case "none":
333
		case "mobile":
334
			return '0.0.0.0/0';
335
			break; /* NOTREACHED */
336
		default:
337
			if (empty($mode) && !empty($idinfo['mode'])) {
338
				$mode = $idinfo['mode'];
339
			}
340

    
341
			if ($mode == "tunnel6") {
342
				$address = get_interface_ipv6($idinfo['type']);
343
				$netbits = get_interface_subnetv6($idinfo['type']);
344
				$address = gen_subnetv6($address, $netbits);
345
				return "{$address}/{$netbits}";
346
			} else {
347
				$address = get_interface_ip($idinfo['type']);
348
				$netbits = get_interface_subnet($idinfo['type']);
349
				$address = gen_subnet($address, $netbits);
350
				return "{$address}/{$netbits}";
351
			}
352
			break; /* NOTREACHED */
353
	}
354
}
355

    
356
/*
357
 * Return phase2 idinfo in address/netmask format
358
 */
359
function ipsec_idinfo_to_subnet(& $idinfo, $addrbits = false) {
360
	global $config;
361

    
362
	switch ($idinfo['type']) {
363
		case "address":
364
			if ($addrbits) {
365
				if ($idinfo['mode'] == "tunnel6") {
366
					return $idinfo['address']."/128";
367
				} else {
368
					return $idinfo['address']."/255.255.255.255";
369
				}
370
			} else {
371
				return $idinfo['address'];
372
			}
373
			break; /* NOTREACHED */
374
		case "none":
375
		case "network":
376
			return $idinfo['address']."/".gen_subnet_mask($idinfo['netbits']);
377
			break; /* NOTREACHED */
378
		case "mobile":
379
			return "0.0.0.0/0";
380
			break; /* NOTREACHED */
381
		default:
382
			if ($idinfo['mode'] == "tunnel6") {
383
				$address = get_interface_ipv6($idinfo['type']);
384
				$netbits = get_interface_subnetv6($idinfo['type']);
385
				$address = gen_subnetv6($address, $netbits);
386
				return $address."/".$netbits;
387
			} else {
388
				$address = get_interface_ip($idinfo['type']);
389
				$netbits = get_interface_subnet($idinfo['type']);
390
				$address = gen_subnet($address, $netbits);
391
				return $address."/".$netbits;
392
			}
393
			break; /* NOTREACHED */
394
	}
395
}
396

    
397
/*
398
 *  Return phase2 idinfo in text format
399
 */
400
function ipsec_idinfo_to_text(& $idinfo) {
401
	global $config;
402

    
403
	switch ($idinfo['type']) {
404
		case "address":
405
			return $idinfo['address'];
406
			break; /* NOTREACHED */
407
		case "network":
408
			return $idinfo['address']."/".$idinfo['netbits'];
409
			break; /* NOTREACHED */
410
		case "mobile":
411
			return gettext("Mobile Client");
412
			break; /* NOTREACHED */
413
		case "none":
414
			return gettext("None");
415
			break; /* NOTREACHED */
416
		default:
417
			if (!empty($config['interfaces'][$idinfo['type']])) {
418
				return convert_friendly_interface_to_friendly_descr($idinfo['type']);
419
			} else {
420
				return strtoupper($idinfo['type']);
421
			}
422
			break; /* NOTREACHED */
423
	}
424
}
425

    
426
/*
427
 * Return phase1 association for phase2
428
 */
429
function ipsec_lookup_phase1(& $ph2ent, & $ph1ent) {
430
	global $config;
431

    
432
	if (!is_array($config['ipsec'])) {
433
		return false;
434
	}
435
	if (!is_array($config['ipsec']['phase1'])) {
436
		return false;
437
	}
438
	if (empty($config['ipsec']['phase1'])) {
439
		return false;
440
	}
441

    
442
	foreach ($config['ipsec']['phase1'] as $ph1tmp) {
443
		if ($ph1tmp['ikeid'] == $ph2ent['ikeid']) {
444
			$ph1ent = $ph1tmp;
445
			return $ph1ent;
446
		}
447
	}
448

    
449
	return false;
450
}
451

    
452
/*
453
 * Check phase1 communications status
454
 */
455
function ipsec_phase1_status(&$ipsec_status, $ikeid) {
456

    
457
	foreach ($ipsec_status as $ike) {
458
		if ($ike['id'] == $ikeid) {
459
			if ($ike['status'] == 'established') {
460
				return true;
461
			}
462
		}
463
	}
464

    
465
	return false;
466
}
467

    
468
/*
469
 * Check phase2 communications status
470
 */
471
function ipsec_phase2_status(&$ipsec_status, &$phase2) {
472

    
473
	if (ipsec_lookup_phase1($ph2ent, $ph1ent)) {
474
		return ipsec_phase1_status($ipsec_status, $ph1ent['ikeid']);
475
	}
476

    
477
	return false;
478
}
479

    
480
/*
481
 * Wrapper to call pfSense_ipsec_list_sa() when IPsec is enabled
482
 */
483
function ipsec_list_sa() {
484

    
485
	if (ipsec_enabled()) {
486
		return pfSense_ipsec_list_sa();
487
	}
488

    
489
	return array();
490
}
491

    
492
/*
493
 * Return dump of SPD table
494
 */
495
function ipsec_dump_spd() {
496
	$fd = @popen("/sbin/setkey -DP", "r");
497
	$spd = array();
498
	if ($fd) {
499
		while (!feof($fd)) {
500
			$line = chop(fgets($fd));
501
			if (!$line) {
502
				continue;
503
			}
504
			if ($line == "No SPD entries.") {
505
				break;
506
			}
507
			if ($line[0] != "\t") {
508
				if (is_array($cursp)) {
509
					$spd[] = $cursp;
510
				}
511
				$cursp = array();
512
				$linea = explode(" ", $line);
513
				$cursp['srcid'] = substr($linea[0], 0, strpos($linea[0], "["));
514
				$cursp['dstid'] = substr($linea[1], 0, strpos($linea[1], "["));
515
				$i = 0;
516
			} else if (is_array($cursp)) {
517
				$line = trim($line, "\t\r\n ");
518
				$linea = explode(" ", $line);
519
				switch ($i) {
520
					case 1:
521
						if ($linea[1] == "none")	/* don't show default anti-lockout rule */ {
522
							unset($cursp);
523
						} else {
524
							$cursp['dir'] = $linea[0];
525
						}
526
						break;
527
					case 2:
528
						$upperspec = explode("/", $linea[0]);
529
						$cursp['proto'] = $upperspec[0];
530
						list($cursp['src'], $cursp['dst']) = explode("-", $upperspec[2]);
531
						$cursp['reqid'] = substr($upperspec[3], strpos($upperspec[3], "#")+1);
532
						break;
533
				}
534
			}
535
			$i++;
536
		}
537
		if (is_array($cursp) && count($cursp)) {
538
			$spd[] = $cursp;
539
		}
540
		pclose($fd);
541
	}
542

    
543
	return $spd;
544
}
545

    
546
/*
547
 * Return dump of SAD table
548
 */
549
function ipsec_dump_sad() {
550
	$fd = @popen("/sbin/setkey -D", "r");
551
	$sad = array();
552
	if ($fd) {
553
		while (!feof($fd)) {
554
			$line = chop(fgets($fd));
555
			if (!$line || $line[0] == " ") {
556
				continue;
557
			}
558
			if ($line == "No SAD entries.") {
559
				break;
560
			}
561
			if ($line[0] != "\t") {
562
				if (is_array($cursa)) {
563
					$sad[] = $cursa;
564
				}
565
				$cursa = array();
566
				list($cursa['src'], $cursa['dst']) = explode(" ", $line);
567
			} else {
568
				$line = trim($line, "\t\n\r ");
569
				$linea = explode(" ", $line);
570
				foreach ($linea as $idx => $linee) {
571
					if ($linee == 'esp' || $linee == 'ah' || $linee[0] == '#') {
572
						$cursa['proto'] = $linee;
573
					} else if (substr($linee, 0, 3) == 'spi') {
574
						$cursa['spi'] = substr($linee, strpos($linee, 'x') + 1, -1);
575
					} else if (substr($linee, 0, 5) == 'reqid') {
576
						$cursa['reqid'] = substr($linee, strpos($linee, 'x') + 1, -1);
577
					} else if (substr($linee, 0, 2) == 'E:') {
578
						$cursa['ealgo'] = $linea[$idx + 1];
579
						break;
580
					} else if (substr($linee, 0, 2) == 'A:') {
581
						$cursa['aalgo'] = $linea[$idx + 1];
582
						break;
583
					} else if (substr($linee, 0, 8) == 'current:') {
584
						$cursa['data'] = substr($linea[$idx + 1], 0, strpos($linea[$idx + 1], 'bytes') - 1) . ' B';
585
						break;
586
					}
587
				}
588
			}
589
		}
590
		if (is_array($cursa) && count($cursa)) {
591
			$sad[] = $cursa;
592
		}
593
		pclose($fd);
594
	}
595

    
596
	return $sad;
597
}
598

    
599
/*
600
 * Return dump of mobile user list
601
 */
602
function ipsec_dump_mobile() {
603
	global $g, $config;
604

    
605
	if(!isset($config['ipsec']['client']['enable'])) {
606
		return array();
607
	}
608

    
609
	$_gb = exec("/usr/local/sbin/ipsec leases 2>/dev/null", $output, $rc);
610

    
611
	if ($rc != 0) {
612
		log_error(gettext("Unable to find IPsec daemon leases file. Could not display mobile user stats!"));
613
		return array();
614
	}
615

    
616
	$response = array();
617
	$id = -1;
618

    
619
	/* Leases in pool '10.7.200.0/24', usage: 1/254, 1 online */
620
	$lease_regex='/^Leases *in *pool *\'(?P<name>.+)\', *usage: *(?P<usage>\d+)\/(?P<size>\d+), *(?P<online>\d+) *online/';
621
	/* 10.7.200.1   online   'jimp' */
622
	$pool_regex='/\s*(?P<host>[\d\.]+)\s+(?P<status>online|offline)\s+\'(?P<id>.*)\'/';
623
	/* no matching leases found */
624
	$nopool_regex='/no *matching *leases *found/';
625

    
626
	$lease=false;
627
	foreach ($output as $line) {
628
		if (preg_match($lease_regex, $line, $matches)) {
629
			$id++;
630
			$response['pool'][$id] = array(
631
			    'name'   => $matches['name'],
632
			    'usage'  => $matches['usage'],
633
			    'size'   => $matches['size'],
634
			    'online' => $matches['online'],
635
			);
636
			$lease=true;
637
		} else if ($lease) {
638
			if (preg_match($nopool_regex, $line)) {
639
				$response['pool'][$id]['lease'][] = array();
640
				$lease=false;
641
			} else if (preg_match($pool_regex, $line, $matches)) {
642
				$response['pool'][$id]['lease'][] = array(
643
				    'host'   => $matches['host'],
644
				    'status' => $matches['status'],
645
				    'id'     => $matches['id']
646
				);
647
			}
648
		}
649
	}
650

    
651
	unset($_gb, $lease, $output, $rc, $id, $lease_regex, $pool_regex,
652
	    $nopool_regex);
653

    
654
	return $response;
655
}
656

    
657
function ipsec_mobilekey_sort() {
658
	global $config;
659

    
660
	function mobilekeycmp($a, $b) {
661
		return strcmp($a['ident'][0], $b['ident'][0]);
662
	}
663

    
664
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
665
}
666

    
667
function ipsec_get_number_of_phase2($ikeid) {
668
	global $config;
669
	$a_phase2 = $config['ipsec']['phase2'];
670

    
671
	$nbph2 = 0;
672

    
673
	if (is_array($a_phase2) && count($a_phase2)) {
674
		foreach ($a_phase2 as $ph2tmp) {
675
			if ($ph2tmp['ikeid'] == $ikeid) {
676
				$nbph2++;
677
			}
678
		}
679
	}
680

    
681
	return $nbph2;
682
}
683

    
684
function ipsec_get_descr($ikeid) {
685
	global $config;
686

    
687
	if (!isset($config['ipsec']['phase1']) ||
688
	    !is_array($config['ipsec']['phase1'])) {
689
		return '';
690
	}
691

    
692
	foreach ($config['ipsec']['phase1'] as $p1) {
693
		if ($p1['ikeid'] == $ikeid) {
694
			return $p1['descr'];
695
		}
696
	}
697

    
698
	return '';
699
}
700

    
701
function ipsec_get_phase1($ikeid) {
702
		global $config;
703

    
704
		if (!isset($config['ipsec']['phase1']) ||
705
		    !is_array($config['ipsec']['phase1'])) {
706
			return '';
707
		}
708

    
709
		$a_phase1 = $config['ipsec']['phase1'];
710
		foreach ($a_phase1 as $p1) {
711
			if ($p1['ikeid'] == $ikeid) {
712
				return $p1;
713
			}
714
		}
715
		unset($a_phase1);
716
}
717

    
718
function ipsec_fixup_ip($ipaddr) {
719
	if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr)) {
720
		return Net_IPv6::compress(Net_IPv6::uncompress($ipaddr));
721
	} else {
722
		return $ipaddr;
723
	}
724
}
725

    
726
function ipsec_find_id(& $ph1ent, $side = "local", $rgmap = array()) {
727
	if ($side == "local") {
728
		$id_type = $ph1ent['myid_type'];
729
		$id_data = $ph1ent['myid_data'];
730

    
731
		$addr = ipsec_get_phase1_src($ph1ent);
732
		if (!$addr) {
733
			return array();
734
		}
735
	} elseif ($side == "peer") {
736
		$id_type = $ph1ent['peerid_type'];
737
		$id_data = $ph1ent['peerid_data'];
738

    
739
		if (isset($ph1ent['mobile'])) {
740
			$addr = "%any";
741
		} else {
742
			$addr = $ph1ent['remote-gateway'];
743
		}
744
	} else {
745
		return array();
746
	}
747

    
748

    
749
	$thisid_type = $id_type;
750
	switch ($thisid_type) {
751
		case 'myaddress':
752
			$thisid_type = 'address';
753
			$thisid_data = $addr;
754
			break;
755
		case 'dyn_dns':
756
			$thisid_type = 'dns';
757
			$thisid_data = $id_data;
758
			break;
759
		case 'peeraddress':
760
			$thisid_type = 'address';
761
			$thisid_data = $rgmap[$ph1ent['remote-gateway']];
762
			break;
763
		case 'address':
764
			$thisid_data = $id_data;
765
			break;
766
		case 'fqdn':
767
			$thisid_data = "{$id_data}";
768
			break;
769
		case 'keyid tag':
770
			$thisid_type = 'keyid';
771
			$thisid_data = "{$id_data}";
772
			break;
773
		case 'user_fqdn':
774
			$thisid_type = 'userfqdn';
775
			$thisid_data = "{$id_data}";
776
			break;
777
		case 'asn1dn':
778
			$thisid_data = $id_data;
779
			break;
780
	}
781
	return array($thisid_type, $thisid_data);
782
}
783

    
784
function ipsec_fixup_network($network) {
785
	if (substr($network, -3) == '|/0') {
786
		$result = substr($network, 0, -3);
787
	} else {
788
		$tmp = explode('|', $network);
789
		if (isset($tmp[1])) {
790
			$result = $tmp[1];
791
		} else {
792
			$result = $tmp[0];
793
		}
794
		unset($tmp);
795
	}
796

    
797
	return $result;
798
}
799

    
800
function ipsec_new_reqid() {
801
	global $config;
802

    
803
	if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
804
		return;
805
	}
806

    
807
	$ipsecreqid = lock('ipsecreqids', LOCK_EX);
808
	$keyids = array();
809
	$keyid = 1;
810
	foreach ($config['ipsec']['phase2'] as $ph2) {
811
		$keyids[$ph2['reqid']] = $ph2['reqid'];
812
	}
813

    
814
	for ($i = 1; $i < 16000; $i++) {
815
		if (!isset($keyids[$i])) {
816
			$keyid = $i;
817
			break;
818
		}
819
	}
820
	unlock($ipsecreqid);
821

    
822
	return $keyid;
823
}
824

    
825
function ipsec_get_loglevels() {
826
	global $config, $ipsec_log_cats;
827
	$def_loglevel = '1';
828

    
829
	$levels = array();
830

    
831
	foreach (array_keys($ipsec_log_cats) as $cat) {
832
		if (isset($config['ipsec']['logging'][$cat])) {
833
			$levels[$cat] = $config['ipsec']['logging'][$cat];
834
		} elseif (in_array($cat, array('ike', 'chd', 'cfg'))) {
835
			$levels[$cat] = "2";
836
		} else {
837
			$levels[$cat] = $def_loglevel;
838
		}
839
	}
840
	return $levels;
841
}
842
?>
(21-21/51)