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-2018 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 $ipsec_identifier_list;
|
75
|
$ipsec_identifier_list = array(
|
76
|
// 'ipv4' => array('desc' => gettext('IPv4 address'), 'mobile' => true),
|
77
|
// 'ipv6' => array('desc' => gettext('IPv6 address'), 'mobile' => true),
|
78
|
// 'rfc822' => array('desc' => gettext('RFC822'), 'mobile' => true),
|
79
|
'none' => array('desc' => '', 'mobile' => true),
|
80
|
'email' => array('desc' => gettext('E-mail address'), 'mobile' => true),
|
81
|
'userfqdn' => array('desc' => gettext('User Fully Qualified Domain Name'), 'mobile' => true)
|
82
|
// 'fqdn' => array('desc' => gettext('Fully Qualified Domain Name'), 'mobile' => true),
|
83
|
// 'dns' => array('desc' => gettext('DNS'), 'mobile' => true),
|
84
|
// 'asn1dn' => array('desc' => gettext('ASN.1 Distinguished Name'), 'mobile' => true),
|
85
|
// 'asn1gn' => array('desc' => gettext('ASN.1 GN'), 'mobile' => true),
|
86
|
// 'keyid' => array('desc' => gettext('KeyID'), 'mobile' => true)
|
87
|
);
|
88
|
|
89
|
global $my_identifier_list;
|
90
|
$my_identifier_list = array(
|
91
|
'myaddress' => array('desc' => gettext('My IP address'), 'mobile' => true),
|
92
|
'address' => array('desc' => gettext('IP address'), 'mobile' => true),
|
93
|
'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
|
94
|
'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
|
95
|
'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
|
96
|
'keyid tag' => array('desc' => gettext('KeyID tag'), 'mobile' => true),
|
97
|
'dyn_dns' => array('desc' => gettext('Dynamic DNS'), 'mobile' => true)
|
98
|
);
|
99
|
|
100
|
global $peer_identifier_list;
|
101
|
$peer_identifier_list = array(
|
102
|
'any' => array('desc' => gettext('Any'), 'mobile' => true),
|
103
|
'peeraddress' => array('desc' => gettext('Peer IP address'), 'mobile' => false),
|
104
|
'address' => array('desc' => gettext('IP address'), 'mobile' => false),
|
105
|
'fqdn' => array('desc' => gettext('Distinguished name'), 'mobile' => true),
|
106
|
'user_fqdn' => array('desc' => gettext('User distinguished name'), 'mobile' => true),
|
107
|
'asn1dn' => array('desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true),
|
108
|
'keyid tag' => array('desc' =>gettext('KeyID tag'), 'mobile' => true)
|
109
|
);
|
110
|
|
111
|
global $ipsec_idhandling;
|
112
|
$ipsec_idhandling = array(
|
113
|
'yes' => 'YES', 'no' => 'NO', 'never' => 'NEVER', 'keep' => 'KEEP'
|
114
|
);
|
115
|
|
116
|
global $p1_ealgos;
|
117
|
$p1_ealgos = array(
|
118
|
'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
|
119
|
'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
120
|
'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
121
|
'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
122
|
'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
|
123
|
'3des' => array('name' => '3DES'),
|
124
|
'cast128' => array('name' => 'CAST128')
|
125
|
);
|
126
|
|
127
|
global $p2_ealgos;
|
128
|
$p2_ealgos = array(
|
129
|
'aes' => array('name' => 'AES', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
|
130
|
'aes128gcm' => array('name' => 'AES128-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
131
|
'aes192gcm' => array('name' => 'AES192-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
132
|
'aes256gcm' => array('name' => 'AES256-GCM', 'keysel' => array('lo' => 64, 'hi' => 128, 'step' => 32)),
|
133
|
'blowfish' => array('name' => 'Blowfish', 'keysel' => array('lo' => 128, 'hi' => 256, 'step' => 64)),
|
134
|
'3des' => array('name' => '3DES'),
|
135
|
'cast128' => array('name' => 'CAST128')
|
136
|
);
|
137
|
|
138
|
global $p1_halgos;
|
139
|
$p1_halgos = array(
|
140
|
'md5' => 'MD5',
|
141
|
'sha1' => 'SHA1',
|
142
|
'sha256' => 'SHA256',
|
143
|
'sha384' => 'SHA384',
|
144
|
'sha512' => 'SHA512',
|
145
|
'aesxcbc' => 'AES-XCBC'
|
146
|
);
|
147
|
|
148
|
global $p1_dhgroups;
|
149
|
$p1_dhgroups = array(
|
150
|
1 => '1 (768 bit)',
|
151
|
2 => '2 (1024 bit)',
|
152
|
5 => '5 (1536 bit)',
|
153
|
14 => '14 (2048 bit)',
|
154
|
15 => '15 (3072 bit)',
|
155
|
16 => '16 (4096 bit)',
|
156
|
17 => '17 (6144 bit)',
|
157
|
18 => '18 (8192 bit)',
|
158
|
19 => '19 (nist ecp256)',
|
159
|
20 => '20 (nist ecp384)',
|
160
|
21 => '21 (nist ecp521)',
|
161
|
22 => '22 (1024(sub 160) bit)',
|
162
|
23 => '23 (2048(sub 224) bit)',
|
163
|
24 => '24 (2048(sub 256) bit)',
|
164
|
28 => '28 (brainpool ecp256)',
|
165
|
29 => '29 (brainpool ecp384)',
|
166
|
30 => '30 (brainpool ecp512)'
|
167
|
);
|
168
|
|
169
|
global $p2_halgos;
|
170
|
$p2_halgos = array(
|
171
|
'hmac_md5' => 'MD5',
|
172
|
'hmac_sha1' => 'SHA1',
|
173
|
'hmac_sha256' => 'SHA256',
|
174
|
'hmac_sha384' => 'SHA384',
|
175
|
'hmac_sha512' => 'SHA512',
|
176
|
'aesxcbc' => 'AES-XCBC'
|
177
|
);
|
178
|
|
179
|
global $p1_authentication_methods;
|
180
|
$p1_authentication_methods = array(
|
181
|
'hybrid_rsa_server' => array('name' => gettext('Hybrid RSA + Xauth'), 'mobile' => true),
|
182
|
'xauth_rsa_server' => array('name' => gettext('Mutual RSA + Xauth'), 'mobile' => true),
|
183
|
'xauth_psk_server' => array('name' => gettext('Mutual PSK + Xauth'), 'mobile' => true),
|
184
|
'eap-tls' => array('name' => gettext('EAP-TLS'), 'mobile' => true),
|
185
|
'eap-radius' => array('name' => gettext('EAP-RADIUS'), 'mobile' => true),
|
186
|
'eap-mschapv2' => array('name' => gettext('EAP-MSChapv2'), 'mobile' => true),
|
187
|
'rsasig' => array('name' => gettext('Mutual RSA'), 'mobile' => false),
|
188
|
'pre_shared_key' => array('name' => gettext('Mutual PSK'), 'mobile' => false)
|
189
|
);
|
190
|
|
191
|
global $ipsec_preshared_key_type;
|
192
|
$ipsec_preshared_key_type = array(
|
193
|
'PSK' => 'PSK',
|
194
|
'EAP' => 'EAP'
|
195
|
);
|
196
|
|
197
|
global $p2_modes;
|
198
|
$p2_modes = array(
|
199
|
'tunnel' => gettext('Tunnel IPv4'),
|
200
|
'tunnel6' => gettext('Tunnel IPv6'),
|
201
|
'transport' => gettext('Transport'),
|
202
|
'vti' => gettext('Routed (VTI)')
|
203
|
);
|
204
|
|
205
|
global $p2_protos;
|
206
|
$p2_protos = array(
|
207
|
'esp' => 'ESP',
|
208
|
'ah' => 'AH'
|
209
|
);
|
210
|
|
211
|
global $p2_pfskeygroups;
|
212
|
$p2_pfskeygroups = array(
|
213
|
0 => gettext('off'),
|
214
|
1 => gettext('1 (768 bit)'),
|
215
|
2 => gettext('2 (1024 bit)'),
|
216
|
5 => gettext('5 (1536 bit)'),
|
217
|
14 => gettext('14 (2048 bit)'),
|
218
|
15 => gettext('15 (3072 bit)'),
|
219
|
16 => gettext('16 (4096 bit)'),
|
220
|
17 => gettext('17 (6144 bit)'),
|
221
|
18 => gettext('18 (8192 bit)'),
|
222
|
19 => gettext('19 (nist ecp256)'),
|
223
|
20 => gettext('20 (nist ecp384)'),
|
224
|
21 => gettext('21 (nist ecp521)'),
|
225
|
22 => gettext('22 (1024(sub 160) bit)'),
|
226
|
23 => gettext('23 (2048(sub 224) bit)'),
|
227
|
24 => gettext('24 (2048(sub 256) bit)'),
|
228
|
28 => gettext('28 (brainpool ecp256)'),
|
229
|
29 => gettext('29 (brainpool ecp384)'),
|
230
|
30 => gettext('30 (brainpool ecp512)')
|
231
|
);
|
232
|
|
233
|
function ipsec_enabled() {
|
234
|
global $config;
|
235
|
|
236
|
if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
|
237
|
return false;
|
238
|
}
|
239
|
|
240
|
/* Check if we have at least one phase 1 entry. */
|
241
|
if (!isset($config['ipsec']['phase1']) ||
|
242
|
!is_array($config['ipsec']['phase1']) ||
|
243
|
empty($config['ipsec']['phase1'])) {
|
244
|
return false;
|
245
|
}
|
246
|
/* Check if at least one phase 1 entry is enabled. */
|
247
|
foreach ($config['ipsec']['phase1'] as $phase1) {
|
248
|
if (!isset($phase1['disabled'])) {
|
249
|
return true;
|
250
|
}
|
251
|
}
|
252
|
|
253
|
return false;
|
254
|
}
|
255
|
|
256
|
/*
|
257
|
* ikeid management functions
|
258
|
*/
|
259
|
|
260
|
function ipsec_ikeid_used($ikeid) {
|
261
|
global $config;
|
262
|
|
263
|
foreach ($config['ipsec']['phase1'] as $ph1ent) {
|
264
|
if ($ikeid == $ph1ent['ikeid']) {
|
265
|
return true;
|
266
|
}
|
267
|
}
|
268
|
|
269
|
return false;
|
270
|
}
|
271
|
|
272
|
function ipsec_ikeid_next() {
|
273
|
|
274
|
$ikeid = 1;
|
275
|
while (ipsec_ikeid_used($ikeid)) {
|
276
|
$ikeid++;
|
277
|
}
|
278
|
|
279
|
return $ikeid;
|
280
|
}
|
281
|
|
282
|
/*
|
283
|
* Return phase1 local address
|
284
|
*/
|
285
|
function ipsec_get_phase1_src(& $ph1ent) {
|
286
|
|
287
|
if ($ph1ent['interface']) {
|
288
|
if (substr($ph1ent['interface'], 0, 4) == "_vip") {
|
289
|
$if = $ph1ent['interface'];
|
290
|
} else {
|
291
|
$if = get_failover_interface($ph1ent['interface']);
|
292
|
}
|
293
|
} else {
|
294
|
$if = "wan";
|
295
|
}
|
296
|
$ip6 = get_interface_ipv6($if);
|
297
|
$ip4 = get_interface_ip($if);
|
298
|
if ($ph1ent['protocol'] == "inet6") {
|
299
|
$interfaceip = $ip6;
|
300
|
} elseif ($ph1ent['protocol'] == "inet") {
|
301
|
$interfaceip = $ip4;
|
302
|
} elseif ($ph1ent['protocol'] == "both") {
|
303
|
$ifips = array();
|
304
|
if (!empty($ip4)) {
|
305
|
$ifips[] = $ip4;
|
306
|
}
|
307
|
if (!empty($ip6)) {
|
308
|
$ifips[] = $ip6;
|
309
|
}
|
310
|
$interfaceip = implode(',', $ifips);
|
311
|
}
|
312
|
|
313
|
return $interfaceip;
|
314
|
}
|
315
|
|
316
|
/*
|
317
|
* Return phase1 local address
|
318
|
*/
|
319
|
function ipsec_get_phase1_dst(& $ph1ent) {
|
320
|
global $g;
|
321
|
|
322
|
if (empty($ph1ent['remote-gateway'])) {
|
323
|
return false;
|
324
|
}
|
325
|
$rg = $ph1ent['remote-gateway'];
|
326
|
if (!is_ipaddr($rg)) {
|
327
|
if (!platform_booting()) {
|
328
|
return resolve_retry($rg);
|
329
|
}
|
330
|
}
|
331
|
if (!is_ipaddr($rg)) {
|
332
|
return false;
|
333
|
}
|
334
|
|
335
|
return $rg;
|
336
|
}
|
337
|
|
338
|
/*
|
339
|
* Return phase2 idinfo in cidr format
|
340
|
*/
|
341
|
function ipsec_idinfo_to_cidr(& $idinfo, $addrbits = false, $mode = "") {
|
342
|
global $config;
|
343
|
|
344
|
switch ($idinfo['type']) {
|
345
|
case "address":
|
346
|
if ($addrbits) {
|
347
|
if ($mode == "tunnel6") {
|
348
|
return $idinfo['address']."/128";
|
349
|
} else {
|
350
|
return $idinfo['address']."/32";
|
351
|
}
|
352
|
} else {
|
353
|
return $idinfo['address'];
|
354
|
}
|
355
|
break; /* NOTREACHED */
|
356
|
case "network":
|
357
|
return "{$idinfo['address']}/{$idinfo['netbits']}";
|
358
|
break; /* NOTREACHED */
|
359
|
case "none":
|
360
|
case "mobile":
|
361
|
return '0.0.0.0/0';
|
362
|
break; /* NOTREACHED */
|
363
|
default:
|
364
|
if (empty($mode) && !empty($idinfo['mode'])) {
|
365
|
$mode = $idinfo['mode'];
|
366
|
}
|
367
|
|
368
|
if ($mode == "tunnel6") {
|
369
|
$address = get_interface_ipv6($idinfo['type']);
|
370
|
$netbits = get_interface_subnetv6($idinfo['type']);
|
371
|
$address = gen_subnetv6($address, $netbits);
|
372
|
return "{$address}/{$netbits}";
|
373
|
} else {
|
374
|
$address = get_interface_ip($idinfo['type']);
|
375
|
$netbits = get_interface_subnet($idinfo['type']);
|
376
|
$address = gen_subnet($address, $netbits);
|
377
|
return "{$address}/{$netbits}";
|
378
|
}
|
379
|
break; /* NOTREACHED */
|
380
|
}
|
381
|
}
|
382
|
|
383
|
/*
|
384
|
* Return phase2 idinfo in address/netmask format
|
385
|
*/
|
386
|
function ipsec_idinfo_to_subnet(& $idinfo, $addrbits = false) {
|
387
|
global $config;
|
388
|
|
389
|
switch ($idinfo['type']) {
|
390
|
case "address":
|
391
|
if ($addrbits) {
|
392
|
if ($idinfo['mode'] == "tunnel6") {
|
393
|
return $idinfo['address']."/128";
|
394
|
} else {
|
395
|
return $idinfo['address']."/255.255.255.255";
|
396
|
}
|
397
|
} else {
|
398
|
return $idinfo['address'];
|
399
|
}
|
400
|
break; /* NOTREACHED */
|
401
|
case "none":
|
402
|
case "network":
|
403
|
return $idinfo['address']."/".gen_subnet_mask($idinfo['netbits']);
|
404
|
break; /* NOTREACHED */
|
405
|
case "mobile":
|
406
|
return "0.0.0.0/0";
|
407
|
break; /* NOTREACHED */
|
408
|
default:
|
409
|
if ($idinfo['mode'] == "tunnel6") {
|
410
|
$address = get_interface_ipv6($idinfo['type']);
|
411
|
$netbits = get_interface_subnetv6($idinfo['type']);
|
412
|
$address = gen_subnetv6($address, $netbits);
|
413
|
return $address."/".$netbits;
|
414
|
} else {
|
415
|
$address = get_interface_ip($idinfo['type']);
|
416
|
$netbits = get_interface_subnet($idinfo['type']);
|
417
|
$address = gen_subnet($address, $netbits);
|
418
|
return $address."/".$netbits;
|
419
|
}
|
420
|
break; /* NOTREACHED */
|
421
|
}
|
422
|
}
|
423
|
|
424
|
/*
|
425
|
* Return phase2 idinfo in text format
|
426
|
*/
|
427
|
function ipsec_idinfo_to_text(& $idinfo) {
|
428
|
global $config;
|
429
|
|
430
|
switch ($idinfo['type']) {
|
431
|
case "address":
|
432
|
return $idinfo['address'];
|
433
|
break; /* NOTREACHED */
|
434
|
case "network":
|
435
|
return $idinfo['address']."/".$idinfo['netbits'];
|
436
|
break; /* NOTREACHED */
|
437
|
case "mobile":
|
438
|
return gettext("Mobile Client");
|
439
|
break; /* NOTREACHED */
|
440
|
case "none":
|
441
|
return gettext("None");
|
442
|
break; /* NOTREACHED */
|
443
|
default:
|
444
|
if (!empty($config['interfaces'][$idinfo['type']])) {
|
445
|
return convert_friendly_interface_to_friendly_descr($idinfo['type']);
|
446
|
} else {
|
447
|
return strtoupper($idinfo['type']);
|
448
|
}
|
449
|
break; /* NOTREACHED */
|
450
|
}
|
451
|
}
|
452
|
|
453
|
/*
|
454
|
* Return phase1 association for phase2
|
455
|
*/
|
456
|
function ipsec_lookup_phase1(& $ph2ent, & $ph1ent) {
|
457
|
global $config;
|
458
|
|
459
|
if (!is_array($config['ipsec'])) {
|
460
|
return false;
|
461
|
}
|
462
|
if (!is_array($config['ipsec']['phase1'])) {
|
463
|
return false;
|
464
|
}
|
465
|
if (empty($config['ipsec']['phase1'])) {
|
466
|
return false;
|
467
|
}
|
468
|
|
469
|
foreach ($config['ipsec']['phase1'] as $ph1tmp) {
|
470
|
if ($ph1tmp['ikeid'] == $ph2ent['ikeid']) {
|
471
|
$ph1ent = $ph1tmp;
|
472
|
return $ph1ent;
|
473
|
}
|
474
|
}
|
475
|
|
476
|
return false;
|
477
|
}
|
478
|
|
479
|
/*
|
480
|
* Check phase1 communications status
|
481
|
*/
|
482
|
function ipsec_phase1_status(&$ipsec_status, $ikeid) {
|
483
|
|
484
|
foreach ($ipsec_status as $ike) {
|
485
|
if ($ike['id'] == $ikeid) {
|
486
|
if ($ike['status'] == 'established') {
|
487
|
return true;
|
488
|
}
|
489
|
}
|
490
|
}
|
491
|
|
492
|
return false;
|
493
|
}
|
494
|
|
495
|
/*
|
496
|
* Check phase2 communications status
|
497
|
*/
|
498
|
function ipsec_phase2_status(&$ipsec_status, &$phase2) {
|
499
|
|
500
|
if (ipsec_lookup_phase1($ph2ent, $ph1ent)) {
|
501
|
return ipsec_phase1_status($ipsec_status, $ph1ent['ikeid']);
|
502
|
}
|
503
|
|
504
|
return false;
|
505
|
}
|
506
|
|
507
|
/*
|
508
|
* Wrapper to call pfSense_ipsec_list_sa() when IPsec is enabled
|
509
|
*/
|
510
|
function ipsec_list_sa() {
|
511
|
|
512
|
if (ipsec_enabled()) {
|
513
|
return pfSense_ipsec_list_sa();
|
514
|
}
|
515
|
|
516
|
return array();
|
517
|
}
|
518
|
|
519
|
/*
|
520
|
* Return dump of SPD table
|
521
|
*/
|
522
|
function ipsec_dump_spd() {
|
523
|
$fd = @popen("/sbin/setkey -DP", "r");
|
524
|
$spd = array();
|
525
|
if ($fd) {
|
526
|
while (!feof($fd)) {
|
527
|
$line = chop(fgets($fd));
|
528
|
if (!$line) {
|
529
|
continue;
|
530
|
}
|
531
|
if ($line == "No SPD entries.") {
|
532
|
break;
|
533
|
}
|
534
|
if ($line[0] != "\t") {
|
535
|
if (is_array($cursp)) {
|
536
|
$spd[] = $cursp;
|
537
|
}
|
538
|
$cursp = array();
|
539
|
$linea = explode(" ", $line);
|
540
|
$cursp['srcid'] = substr($linea[0], 0, strpos($linea[0], "["));
|
541
|
$cursp['dstid'] = substr($linea[1], 0, strpos($linea[1], "["));
|
542
|
$i = 0;
|
543
|
} else if (is_array($cursp)) {
|
544
|
$line = trim($line, "\t\r\n ");
|
545
|
$linea = explode(" ", $line);
|
546
|
switch ($i) {
|
547
|
case 1:
|
548
|
if ($linea[1] == "none") /* don't show default anti-lockout rule */ {
|
549
|
unset($cursp);
|
550
|
} else {
|
551
|
$cursp['dir'] = $linea[0];
|
552
|
}
|
553
|
break;
|
554
|
case 2:
|
555
|
$upperspec = explode("/", $linea[0]);
|
556
|
$cursp['proto'] = $upperspec[0];
|
557
|
list($cursp['src'], $cursp['dst']) = explode("-", $upperspec[2]);
|
558
|
$cursp['reqid'] = substr($upperspec[3], strpos($upperspec[3], "#")+1);
|
559
|
break;
|
560
|
}
|
561
|
}
|
562
|
$i++;
|
563
|
}
|
564
|
if (is_array($cursp) && count($cursp)) {
|
565
|
$spd[] = $cursp;
|
566
|
}
|
567
|
pclose($fd);
|
568
|
}
|
569
|
|
570
|
return $spd;
|
571
|
}
|
572
|
|
573
|
/*
|
574
|
* Return dump of SAD table
|
575
|
*/
|
576
|
function ipsec_dump_sad() {
|
577
|
$fd = @popen("/sbin/setkey -D", "r");
|
578
|
$sad = array();
|
579
|
if ($fd) {
|
580
|
while (!feof($fd)) {
|
581
|
$line = chop(fgets($fd));
|
582
|
if (!$line || $line[0] == " ") {
|
583
|
continue;
|
584
|
}
|
585
|
if ($line == "No SAD entries.") {
|
586
|
break;
|
587
|
}
|
588
|
if ($line[0] != "\t") {
|
589
|
if (is_array($cursa)) {
|
590
|
$sad[] = $cursa;
|
591
|
}
|
592
|
$cursa = array();
|
593
|
list($cursa['src'], $cursa['dst']) = explode(" ", $line);
|
594
|
} else {
|
595
|
$line = trim($line, "\t\n\r ");
|
596
|
$linea = explode(" ", $line);
|
597
|
foreach ($linea as $idx => $linee) {
|
598
|
if ($linee == 'esp' || $linee == 'ah' || $linee[0] == '#') {
|
599
|
$cursa['proto'] = $linee;
|
600
|
} else if (substr($linee, 0, 3) == 'spi') {
|
601
|
$cursa['spi'] = substr($linee, strpos($linee, 'x') + 1, -1);
|
602
|
} else if (substr($linee, 0, 5) == 'reqid') {
|
603
|
$cursa['reqid'] = substr($linee, strpos($linee, 'x') + 1, -1);
|
604
|
} else if (substr($linee, 0, 2) == 'E:') {
|
605
|
$cursa['ealgo'] = $linea[$idx + 1];
|
606
|
break;
|
607
|
} else if (substr($linee, 0, 2) == 'A:') {
|
608
|
$cursa['aalgo'] = $linea[$idx + 1];
|
609
|
break;
|
610
|
} else if (substr($linee, 0, 8) == 'current:') {
|
611
|
$cursa['data'] = substr($linea[$idx + 1], 0, strpos($linea[$idx + 1], 'bytes') - 1) . ' B';
|
612
|
break;
|
613
|
}
|
614
|
}
|
615
|
}
|
616
|
}
|
617
|
if (is_array($cursa) && count($cursa)) {
|
618
|
$sad[] = $cursa;
|
619
|
}
|
620
|
pclose($fd);
|
621
|
}
|
622
|
|
623
|
return $sad;
|
624
|
}
|
625
|
|
626
|
/*
|
627
|
* Return dump of mobile user list
|
628
|
*/
|
629
|
function ipsec_dump_mobile() {
|
630
|
global $g, $config;
|
631
|
|
632
|
if(!isset($config['ipsec']['client']['enable'])) {
|
633
|
return array();
|
634
|
}
|
635
|
|
636
|
$_gb = exec("/usr/local/sbin/ipsec leases 2>/dev/null", $output, $rc);
|
637
|
|
638
|
if ($rc != 0) {
|
639
|
log_error(gettext("Unable to find IPsec daemon leases file. Could not display mobile user stats!"));
|
640
|
return array();
|
641
|
}
|
642
|
|
643
|
$response = array();
|
644
|
$id = -1;
|
645
|
|
646
|
/* Leases in pool '10.7.200.0/24', usage: 1/254, 1 online */
|
647
|
$lease_regex='/^Leases *in *pool *\'(?P<name>.+)\', *usage: *(?P<usage>\d+)\/(?P<size>\d+), *(?P<online>\d+) *online/';
|
648
|
/* 10.7.200.1 online 'jimp' */
|
649
|
$pool_regex='/\s*(?P<host>[\d\.]+)\s+(?P<status>online|offline)\s+\'(?P<id>.*)\'/';
|
650
|
/* no matching leases found */
|
651
|
$nopool_regex='/no *matching *leases *found/';
|
652
|
|
653
|
$lease=false;
|
654
|
foreach ($output as $line) {
|
655
|
if (preg_match($lease_regex, $line, $matches)) {
|
656
|
$id++;
|
657
|
$response['pool'][$id] = array(
|
658
|
'name' => $matches['name'],
|
659
|
'usage' => $matches['usage'],
|
660
|
'size' => $matches['size'],
|
661
|
'online' => $matches['online'],
|
662
|
);
|
663
|
$lease=true;
|
664
|
} else if ($lease) {
|
665
|
if (preg_match($nopool_regex, $line)) {
|
666
|
$response['pool'][$id]['lease'][] = array();
|
667
|
$lease=false;
|
668
|
} else if (preg_match($pool_regex, $line, $matches)) {
|
669
|
$response['pool'][$id]['lease'][] = array(
|
670
|
'host' => $matches['host'],
|
671
|
'status' => $matches['status'],
|
672
|
'id' => $matches['id']
|
673
|
);
|
674
|
}
|
675
|
}
|
676
|
}
|
677
|
|
678
|
unset($_gb, $lease, $output, $rc, $id, $lease_regex, $pool_regex,
|
679
|
$nopool_regex);
|
680
|
|
681
|
return $response;
|
682
|
}
|
683
|
|
684
|
function ipsec_mobilekey_sort() {
|
685
|
global $config;
|
686
|
|
687
|
function mobilekeycmp($a, $b) {
|
688
|
return strcmp($a['ident'][0], $b['ident'][0]);
|
689
|
}
|
690
|
|
691
|
usort($config['ipsec']['mobilekey'], "mobilekeycmp");
|
692
|
}
|
693
|
|
694
|
function ipsec_get_number_of_phase2($ikeid) {
|
695
|
global $config;
|
696
|
$a_phase2 = $config['ipsec']['phase2'];
|
697
|
|
698
|
$nbph2 = 0;
|
699
|
|
700
|
if (is_array($a_phase2) && count($a_phase2)) {
|
701
|
foreach ($a_phase2 as $ph2tmp) {
|
702
|
if ($ph2tmp['ikeid'] == $ikeid) {
|
703
|
$nbph2++;
|
704
|
}
|
705
|
}
|
706
|
}
|
707
|
|
708
|
return $nbph2;
|
709
|
}
|
710
|
|
711
|
function ipsec_get_descr($ikeid) {
|
712
|
global $config;
|
713
|
|
714
|
if (!isset($config['ipsec']['phase1']) ||
|
715
|
!is_array($config['ipsec']['phase1'])) {
|
716
|
return '';
|
717
|
}
|
718
|
|
719
|
foreach ($config['ipsec']['phase1'] as $p1) {
|
720
|
if ($p1['ikeid'] == $ikeid) {
|
721
|
return $p1['descr'];
|
722
|
}
|
723
|
}
|
724
|
|
725
|
return '';
|
726
|
}
|
727
|
|
728
|
function ipsec_get_phase1($ikeid) {
|
729
|
global $config;
|
730
|
|
731
|
if (!isset($config['ipsec']['phase1']) ||
|
732
|
!is_array($config['ipsec']['phase1'])) {
|
733
|
return '';
|
734
|
}
|
735
|
|
736
|
$a_phase1 = $config['ipsec']['phase1'];
|
737
|
foreach ($a_phase1 as $p1) {
|
738
|
if ($p1['ikeid'] == $ikeid) {
|
739
|
return $p1;
|
740
|
}
|
741
|
}
|
742
|
unset($a_phase1);
|
743
|
}
|
744
|
|
745
|
function ipsec_fixup_ip($ipaddr) {
|
746
|
if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr)) {
|
747
|
return text_to_compressed_ip6($ipaddr);
|
748
|
} else {
|
749
|
return $ipaddr;
|
750
|
}
|
751
|
}
|
752
|
|
753
|
function ipsec_find_id(& $ph1ent, $side = "local", $rgmap = array()) {
|
754
|
if ($side == "local") {
|
755
|
$id_type = $ph1ent['myid_type'];
|
756
|
$id_data = $ph1ent['myid_data'];
|
757
|
|
758
|
$addr = ipsec_get_phase1_src($ph1ent);
|
759
|
if (!$addr) {
|
760
|
return array();
|
761
|
}
|
762
|
/* When automatically guessing, use the first address. */
|
763
|
$addr = explode(',', $addr);
|
764
|
$addr = $addr[0];
|
765
|
} elseif ($side == "peer") {
|
766
|
$id_type = $ph1ent['peerid_type'];
|
767
|
$id_data = $ph1ent['peerid_data'];
|
768
|
|
769
|
if (isset($ph1ent['mobile'])) {
|
770
|
$addr = "%any";
|
771
|
} else {
|
772
|
$addr = $ph1ent['remote-gateway'];
|
773
|
}
|
774
|
} else {
|
775
|
return array();
|
776
|
}
|
777
|
|
778
|
|
779
|
$thisid_type = $id_type;
|
780
|
switch ($thisid_type) {
|
781
|
case 'myaddress':
|
782
|
$thisid_type = 'address';
|
783
|
$thisid_data = $addr;
|
784
|
break;
|
785
|
case 'dyn_dns':
|
786
|
$thisid_type = 'dns';
|
787
|
$thisid_data = $id_data;
|
788
|
break;
|
789
|
case 'peeraddress':
|
790
|
$thisid_type = 'address';
|
791
|
$thisid_data = $rgmap[$ph1ent['remote-gateway']];
|
792
|
break;
|
793
|
case 'address':
|
794
|
$thisid_data = $id_data;
|
795
|
break;
|
796
|
case 'fqdn':
|
797
|
$thisid_data = "{$id_data}";
|
798
|
break;
|
799
|
case 'keyid tag':
|
800
|
$thisid_type = 'keyid';
|
801
|
$thisid_data = "{$id_data}";
|
802
|
break;
|
803
|
case 'user_fqdn':
|
804
|
$thisid_type = 'userfqdn';
|
805
|
$thisid_data = "{$id_data}";
|
806
|
break;
|
807
|
case 'asn1dn':
|
808
|
$thisid_data = $id_data;
|
809
|
break;
|
810
|
}
|
811
|
return array($thisid_type, $thisid_data);
|
812
|
}
|
813
|
|
814
|
function ipsec_fixup_network($network) {
|
815
|
if (substr($network, -3) == '|/0') {
|
816
|
$result = substr($network, 0, -3);
|
817
|
} else {
|
818
|
$tmp = explode('|', $network);
|
819
|
if (isset($tmp[1])) {
|
820
|
$result = $tmp[1];
|
821
|
} else {
|
822
|
$result = $tmp[0];
|
823
|
}
|
824
|
unset($tmp);
|
825
|
}
|
826
|
|
827
|
return $result;
|
828
|
}
|
829
|
|
830
|
function ipsec_new_reqid() {
|
831
|
global $config;
|
832
|
|
833
|
if (!is_array($config['ipsec']) || !is_array($config['ipsec']['phase2'])) {
|
834
|
return;
|
835
|
}
|
836
|
|
837
|
$ipsecreqid = lock('ipsecreqids', LOCK_EX);
|
838
|
$keyids = array();
|
839
|
$keyid = 1;
|
840
|
foreach ($config['ipsec']['phase2'] as $ph2) {
|
841
|
$keyids[$ph2['reqid']] = $ph2['reqid'];
|
842
|
}
|
843
|
|
844
|
for ($i = 1; $i < 16000; $i++) {
|
845
|
if (!isset($keyids[$i])) {
|
846
|
$keyid = $i;
|
847
|
break;
|
848
|
}
|
849
|
}
|
850
|
unlock($ipsecreqid);
|
851
|
|
852
|
return $keyid;
|
853
|
}
|
854
|
|
855
|
function ipsec_get_loglevels() {
|
856
|
global $config, $ipsec_log_cats;
|
857
|
$def_loglevel = '1';
|
858
|
|
859
|
$levels = array();
|
860
|
|
861
|
foreach (array_keys($ipsec_log_cats) as $cat) {
|
862
|
if (isset($config['ipsec']['logging'][$cat])) {
|
863
|
$levels[$cat] = $config['ipsec']['logging'][$cat];
|
864
|
} elseif (in_array($cat, array('ike', 'chd', 'cfg'))) {
|
865
|
$levels[$cat] = "2";
|
866
|
} else {
|
867
|
$levels[$cat] = $def_loglevel;
|
868
|
}
|
869
|
}
|
870
|
return $levels;
|
871
|
}
|
872
|
|
873
|
function ipsec_vti($ph1ent, $returnaddresses = false) {
|
874
|
global $config;
|
875
|
if (empty($ph1ent) || !is_array($ph1ent) || !is_array($config['ipsec']['phase2'])) {
|
876
|
return false;
|
877
|
}
|
878
|
|
879
|
$is_vti = false;
|
880
|
$vtisubnet_spec = array();
|
881
|
|
882
|
foreach ($config['ipsec']['phase2'] as $ph2ent) {
|
883
|
if ($ph1ent['ikeid'] != $ph2ent['ikeid']) {
|
884
|
continue;
|
885
|
}
|
886
|
if ($ph2ent['mode'] == 'vti') {
|
887
|
if ($returnaddresses) {
|
888
|
$vtisubnet_spec[] = array(
|
889
|
'left' => ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']),
|
890
|
'right' => ipsec_idinfo_to_cidr($ph2ent['remoteid'], false, $ph2ent['mode']),
|
891
|
'descr' => $ph2ent['descr'],
|
892
|
);
|
893
|
}
|
894
|
$is_vti = true;
|
895
|
}
|
896
|
}
|
897
|
return ($returnaddresses) ? $vtisubnet_spec : $is_vti;
|
898
|
}
|
899
|
|
900
|
?>
|