Project

General

Profile

Download (48.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/*
4
	vpn.inc
5
	Copyright (C) 2004 Scott Ullrich
6
	Copyright (C) 2008 Shrew Soft Inc
7
	Copyright (C) 2008 Ermal Lu�i
8
	All rights reserved.
9

    
10
	originally part of m0n0wall (http://m0n0.ch/wall)
11
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
	All rights reserved.
13

    
14
	Redistribution and use in source and binary forms, with or without
15
	modification, are permitted provided that the following conditions are met:
16

    
17
	1. Redistributions of source code must retain the above copyright notice,
18
	   this list of conditions and the following disclaimer.
19

    
20
	2. Redistributions in binary form must reproduce the above copyright
21
	   notice, this list of conditions and the following disclaimer in the
22
	   documentation and/or other materials provided with the distribution.
23

    
24
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
	POSSIBILITY OF SUCH DAMAGE.
34
*/
35

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
38
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/ipsec	/usr/local/libexec/ipsec/charon /usr/local/libexec/ipsec/starter
39
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/filterdns	/usr/local/sbin/mpd4
40
	pfSense_MODULE:	vpn
41
*/
42

    
43
require_once("ipsec.inc");
44

    
45
function vpn_ipsec_configure_loglevels($forconfig = false)
46
{
47
	global $config, $ipsec_loglevels;
48

    
49
	$cfgtext = array();
50
	foreach ($ipsec_loglevels as $lkey => $ldescr) {
51
		if (!isset($config['ipsec']["ipsec_{$lkey}"]) && !$forconfig)
52
			mwexec("/usr/local/sbin/ipsec stroke loglevel {$lkey} -- -1", false);
53
		else if (is_numeric($config['ipsec']["ipsec_{$lkey}"]) &&
54
		    intval($config['ipsec']["ipsec_{$lkey}"]) >= 0 && intval($config['ipsec']["ipsec_{$lkey}"]) <= 5)
55
			$forconfig ? $cfgtext[] = "${lkey} " . (intval($config['ipsec']["ipsec_{$lkey}"]) - 1) :
56
				mwexec("/usr/local/sbin/ipsec stroke loglevel {$lkey} " . (intval($config['ipsec']["ipsec_{$lkey}"]) - 1) , false);
57
	}
58
	if ($forconfig)
59
		return implode(',', $cfgtext);
60
}
61

    
62
/* include all configuration functions */
63
function vpn_ipsec_convert_to_modp($index)
64
{
65

    
66
	$convertion = "";
67
	switch ($index) {
68
	case '1':
69
		$convertion = "modp768";
70
		break;
71
	case '2':
72
		$convertion = "modp1024";
73
		break;
74
	case '5':
75
		$convertion = "modp1536";
76
		break;
77
	case '14':
78
		$convertion = "modp2048";
79
		break;
80
	case '15':
81
		$convertion = "modp3072";
82
		break;
83
	case '16':      
84
		$convertion = "modp4096";
85
		break;
86
	case '17':
87
		$convertion = "modp6144";
88
		break;
89
	case '18':
90
		$convertion = "modp8192";
91
		break;
92
	}
93

    
94
	return $convertion;
95
}
96

    
97
function vpn_ipsec_configure($ipchg = false)
98
{
99
	global $config, $g, $sa, $sn, $p1_ealgos, $p2_ealgos;
100

    
101
	if ($g['platform'] == 'jail')
102
		return;
103

    
104
	/* get the automatic ping_hosts.sh ready */
105
	unlink_if_exists("{$g['vardb_path']}/ipsecpinghosts");
106
	touch("{$g['vardb_path']}/ipsecpinghosts");
107

    
108
	vpn_ipsec_configure_preferoldsa();
109

    
110
	$syscfg = $config['system'];
111
	$ipseccfg = $config['ipsec'];
112
	if (!isset($ipseccfg['enable'])) {
113
		/* try to stop charon */
114
		mwexec("/usr/local/sbin/ipsec stop");
115
		/* Stop dynamic monitoring */
116
		killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
117

    
118
		/* wait for process to die */
119
		sleep(2);
120

    
121
		/* disallow IPSEC, it is off */
122
		mwexec("/sbin/ifconfig enc0 down");
123
		set_single_sysctl("net.inet.ip.ipsec_in_use", "0");
124

    
125
		return 0;
126
	}
127

    
128
	$a_phase1 = $config['ipsec']['phase1'];
129
	$a_phase2 = $config['ipsec']['phase2'];
130
	$a_client = $config['ipsec']['client'];
131

    
132
	$certpath = "{$g['varetc_path']}/ipsec/ipsec.d/certs";
133
	$capath = "{$g['varetc_path']}/ipsec/ipsec.d/cacerts";
134
	$keypath = "{$g['varetc_path']}/ipsec/ipsec.d/private";
135

    
136
	mwexec("/sbin/ifconfig enc0 up");
137
	set_single_sysctl("net.inet.ip.ipsec_in_use", "1");
138
	/* needed for config files */
139
	if (!is_dir("{$g['varetc_path']}/ipsec"))
140
		mkdir("{$g['varetc_path']}/ipsec");
141
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d"))
142
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d");
143
	if (!is_dir($capath))
144
		mkdir($capath);
145
	if (!is_dir($keypath))
146
		mkdir($keypath);
147
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d/crls"))
148
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d/crls");
149
	if (!is_dir($certpath))
150
		mkdir($certpath);
151
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d/aacerts"))
152
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d/aacerts");
153
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d/acerts"))
154
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d/acerts");
155
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d/ocspcerts"))
156
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d/ocspcerts");
157
	if (!is_dir("{$g['varetc_path']}/ipsec/ipsec.d/reqs"))
158
		mkdir("{$g['varetc_path']}/ipsec/ipsec.d/reqs");
159
	
160

    
161
	if (platform_booting())
162
		echo gettext("Configuring IPsec VPN... ");
163

    
164
	/* fastforwarding is not compatible with ipsec tunnels */
165
	set_single_sysctl("net.inet.ip.fastforwarding", "0");
166

    
167
	/* resolve all local, peer addresses and setup pings */
168
	$ipmap = array();
169
	$rgmap = array();
170
	$filterdns_list = array();
171
	$listeniflist = array();
172
	$aggressive_mode_psk = false;
173
	unset($iflist);
174
	if (is_array($a_phase1) && count($a_phase1)) {
175

    
176
		$ipsecpinghosts = "";
177
		/* step through each phase1 entry */
178
		foreach ($a_phase1 as $ph1ent) {
179
			if (isset($ph1ent['disabled']))
180
				continue;
181

    
182
			if ($ph1ent['mode'] == "aggressive" && ($ph1ent['authentication_method'] == "pre_shared_key" || $ph1ent['authentication_method'] == "xauth_psk_server")) 
183
				$aggressive_mode_psk = true;
184

    
185
			$ikeid = $ph1ent['ikeid'];
186
			$listeniflist = get_real_interface($a_phase1['interface']);
187

    
188
			$ep = ipsec_get_phase1_src($ph1ent);
189
			if (!is_ipaddr($ep))
190
				continue;
191

    
192
			if(!in_array($ep,$ipmap))
193
				$ipmap[] = $ep;
194

    
195
			/* see if this tunnel has a hostname for the remote-gateway. If so,
196
			   try to resolve it now and add it to the list for filterdns */
197

    
198
			if (isset ($ph1ent['mobile']))
199
				continue;
200

    
201
			$rg = $ph1ent['remote-gateway'];
202

    
203
			if (!is_ipaddr($rg)) {
204
				$filterdns_list[] = "{$rg}";
205
				add_hostname_to_watch($rg);
206
				if (!platform_booting())
207
					$rg = resolve_retry($rg);
208
				if (!is_ipaddr($rg))
209
					continue;
210
			}
211
			if(array_search($rg, $rgmap)) {
212
				log_error("The remote gateway {$rg} already exists on another phase 1 entry");
213
				continue;
214
			}
215
			$rgmap[$ph1ent['remote-gateway']] = $rg;
216

    
217
			if (is_array($a_phase2)) {
218
				/* step through each phase2 entry */
219
				foreach ($a_phase2 as $ph2ent) {
220
					if (isset($ph2ent['disabled']))
221
						continue;
222

    
223
					if ($ikeid != $ph2ent['ikeid'])
224
						continue;
225

    
226
					/* add an ipsec pinghosts entry */
227
					if ($ph2ent['pinghost']) {
228
						if (!is_array($iflist))
229
							$iflist = get_configured_interface_list();
230
						$viplist = get_configured_vips_list();
231
						$srcip = null;
232
						$local_subnet = ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']);
233
						if(is_ipaddrv6($ph2ent['pinghost'])) {
234
							foreach ($iflist as $ifent => $ifname) {
235
								$interface_ip = get_interface_ipv6($ifent);
236
								if(!is_ipaddrv6($interface_ip))
237
									continue;
238
								if (ip_in_subnet($interface_ip, $local_subnet)) {
239
									$srcip = $interface_ip;
240
									break;
241
								}
242
							}
243
						} else {
244
							foreach ($iflist as $ifent => $ifname) {
245
								$interface_ip = get_interface_ip($ifent);
246
								if(!is_ipaddrv4($interface_ip))
247
									continue;
248
								if ($local_subnet == "0.0.0.0/0" || ip_in_subnet($interface_ip, $local_subnet)) {
249
									$srcip = $interface_ip;
250
									break;
251
								}
252
							}
253
						}
254
						/* if no valid src IP was found in configured interfaces, try the vips */
255
						if (is_null($srcip)) {
256
							foreach ($viplist as $vip) {
257
								if (ip_in_subnet($vip['ipaddr'], $local_subnet)) {
258
									$srcip = $vip['ipaddr'];
259
									break;
260
								}
261
							}
262
						}
263
						$dstip = $ph2ent['pinghost'];
264
						if(is_ipaddrv6($dstip)) {
265
							$family = "inet6";
266
						} else {
267
							$family = "inet";
268
						}
269
						if (is_ipaddr($srcip))
270
							$ipsecpinghosts[] = "{$srcip}|{$dstip}|3|||||{$family}|\n";
271
					}
272
				}
273
			}
274
		}
275
		@file_put_contents("{$g['vardb_path']}/ipsecpinghosts", $ipsecpinghosts);
276
		unset($ipsecpinghosts);
277
	}
278
	unset($iflist);
279

    
280
	$accept_unencrypted = "";
281
	if (isset($config['ipsec']['acceptunencryptedmainmode']))
282
		$accept_unencrypted = "accept_unencrypted_mainmode_messages = yes";
283

    
284
	$i_dont_care_about_security_and_use_aggressive_mode_psk = "";
285
	if ($aggressive_mode_psk) {
286
		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.");
287
		$i_dont_care_about_security_and_use_aggressive_mode_psk = "i_dont_care_about_security_and_use_aggressive_mode_psk=yes";
288
	} 
289

    
290
	$unity_enabled = 'yes';
291
	if (isset($config['ipsec']['unityplugin']))
292
		$unity_enabled = 'no';
293

    
294
	$strongswan = <<<EOD
295

    
296
# Automatically generated config file - DO NOT MODIFY. Changes will be overwritten. 
297
starter {
298
load_warning = no
299
}
300

    
301
charon {
302
# number of worker threads in charon
303
threads = 16
304
ikesa_table_size = 32
305
ikesa_table_segments = 4
306
init_limit_half_open = 1000
307
install_routes = no
308
{$i_dont_care_about_security_and_use_aggressive_mode_psk}
309
{$accept_unencrypted}
310
cisco_unity = {$unity_enabled}
311

    
312
# And two loggers using syslog. The subsections define the facility to log
313
# to, currently one of: daemon, auth.
314
syslog {
315
	identifier = charon
316
	# default level to the LOG_DAEMON facility
317
	daemon {
318
	}
319
	# very minimalistic IKE auditing logs to LOG_AUTHPRIV
320
	auth {
321
		default = -1
322
		ike = 1
323
		ike_name = yes
324
	}
325
}
326

    
327
EOD;
328

    
329
	$strongswan .= "\tplugins {\n";
330

    
331
	if (is_array($a_client) && isset($a_client['enable'])) {
332
		$strongswan .= "\t\tattr {\n";
333
		if ($a_client['pool_address'] && $a_client['pool_netbits'])
334
			$strongswan .= "\t\t\tsubnet = {$a_client['pool_address']}/{$a_client['pool_netbits']}\n";
335

    
336
		$cfgservers = array();
337
		if (!empty($a_client['dns_server1']))
338
			$cfgservers[] = $a_client['dns_server1'];
339
		if (!empty($a_client['dns_server2']))
340
			$cfgservers[] = $a_client['dns_server2'];
341
		if (!empty($a_client['dns_server3']))
342
			$cfgservers[] = $a_client['dns_server3'];
343
		if (!empty($a_client['dns_server4']))
344
			$cfgservers[] = $a_client['dns_server4'];
345

    
346
		if (!empty($cfgservers))
347
			$strongswan .= "\t\t\tdns = " . implode(",", $cfgservers) . "\n";
348
		unset($cfgservers);
349
		$cfgservers = array();
350
		if (!empty($a_client['wins_server1']))
351
			$cfgservers[] = $a_client['wins_server1'];
352
		if (!empty($a_client['wins_server2']))
353
			$cfgservers[] = $a_client['wins_server2'];
354
		if (!empty($cfgservers))
355
			$strongswan .= "\t\t\tnbns = " . implode(",", $cfgservers) . "\n";
356
		unset($cfgservers);
357

    
358
		if (isset($a_client['net_list'])) {
359
			$net_list = '';
360
			foreach ($a_phase2 as $ph2ent) {
361
				if (isset($ph2ent['disabled']))
362
					continue;
363

    
364
				if (!isset($ph2ent['mobile']))
365
					continue;
366

    
367
				$localid = ipsec_idinfo_to_cidr($ph2ent['localid'], true, $ph2ent['mode']);
368

    
369
				if (!empty($net_list))
370
					$net_list .= ",";
371
				$net_list .= $localid;
372
			}
373

    
374
			if (!empty($net_list)) {
375
				$strongswan .= "\t\t\tsplit-include = {$net_list}\n";
376
				unset($net_list);
377
			}
378
		}
379

    
380
		if (!empty($a_client['dns_domain'])) {
381
			$strongswan .= "\t\t\t# Search domain and default domain\n";
382
			$strongswan .= "\t\t\t28674 = {$a_client['dns_domain']}\n";
383
			if (empty($a_client['dns_split']))
384
				$strongswan .= "\t\t\t28675 = {$a_client['dns_domain']}";
385
			$strongswan .= "\n";
386
		}
387

    
388
		if (!empty($a_client['dns_split'])) {
389
			$strongswan .= "\t\t\t28675 = {$a_client['dns_split']}\n";
390
		}
391

    
392
		if (!empty($a_client['login_banner']))
393
			$strongswan .= "\t\t\t28672 = {$a_client['login_banner']}\n";
394

    
395
		if (isset($a_client['save_passwd']))
396
			$strongswan .= "\t\t\t28673 = 1\n";
397

    
398
		if ($a_client['pfs_group'])
399
			$strongswan .= "\t\t\t28679 = {$a_client['pfs_group']}\n";
400
		$strongswan .= "\t\t}\n";
401

    
402
		if ($a_client['user_source'] != "none") {
403
			$strongswan .= "\t\txauth-generic {\n";
404
			$strongswan .= "\t\t\tscript = /etc/inc/ipsec.auth-user.php\n";
405
			$strongswan .= "\t\t\tauthcfg = ";
406
			$firstsed = 0;
407
			$authcfgs = explode(",", $a_client['user_source']);
408
			foreach ($authcfgs as $authcfg) {
409
				if ($firstsed > 0)
410
					$strongswan .= ",";
411
				if ($authcfg == "system")
412
					$authcfg = "Local Database";
413
				$strongswan .= $authcfg;
414
				$firstsed = 1;
415
			}
416
			$strongswan .= "\n";
417
			$strongswan .= "\t\t}\n";
418
		}
419
	}
420

    
421
	$strongswan .= "\t}\n}\n";
422
	@file_put_contents("{$g['varetc_path']}/ipsec/strongswan.conf", $strongswan);
423
	unset($strongswan);
424

    
425
	/* generate CA certificates files */
426
	if (is_array($config['ca']) && count($config['ca'])) {
427
		foreach ($config['ca'] as $ca) {
428
			if (!isset($ca['crt'])) {
429
				log_error(sprintf(gettext("Error: Invalid certificate info for %s"), $ca['descr']));
430
				continue;
431
			}
432
			$cert = base64_decode($ca['crt']);
433
			$x509cert = openssl_x509_parse(openssl_x509_read($cert));
434
			if (!is_array($x509cert) || !isset($x509cert['hash'])) {
435
				log_error(sprintf(gettext("Error: Invalid certificate hash info for %s"), $ca['descr']));
436
				continue;
437
			}
438
			$fname = "{$capath}/{$x509cert['hash']}.0.crt";
439
			if (!@file_put_contents($fname, $cert)) {
440
				log_error(sprintf(gettext("Error: Cannot write IPsec CA file for %s"), $ca['descr']));
441
				continue;
442
			}
443
			unset($cert);
444
		}
445
	}
446

    
447
	$pskconf = "";
448

    
449
	if (is_array($a_phase1) && count($a_phase1)) {
450
		foreach ($a_phase1 as $ph1ent) {
451

    
452
			if (isset($ph1ent['disabled']))
453
				continue;
454

    
455
			if (strstr($ph1ent['authentication_method'], 'rsa') ||
456
			    in_array($ph1ent['authentication_method'], array('eap-mschapv2', 'eap-tls'))) {
457
				$certline = '';
458

    
459
				$ikeid = $ph1ent['ikeid'];
460
				$cert = lookup_cert($ph1ent['certref']);
461

    
462
				if (!$cert) {
463
					log_error(sprintf(gettext("Error: Invalid phase1 certificate reference for %s"), $ph1ent['name']));
464
					continue;
465
				}
466

    
467
				@chmod($certpath, 0600);
468

    
469
				$ph1keyfile = "{$keypath}/cert-{$ikeid}.key";
470
				if (!file_put_contents($ph1keyfile, base64_decode($cert['prv']))) {
471
					log_error(sprintf(gettext("Error: Cannot write phase1 key file for %s"), $ph1ent['name']));
472
					continue;
473
				}
474
				@chmod($ph1keyfile, 0600);
475

    
476
				$ph1certfile = "{$certpath}/cert-{$ikeid}.crt";
477
				if (!file_put_contents($ph1certfile, base64_decode($cert['crt']))) {
478
					log_error(sprintf(gettext("Error: Cannot write phase1 certificate file for %s"), $ph1ent['name']));
479
					@unlink($ph1keyfile);
480
					continue;
481
				}
482
				@chmod($ph1certfile, 0600);
483

    
484
				/* XXX" Traffic selectors? */
485
				$pskconf .= " : RSA {$ph1keyfile}\n";
486
			} else {
487
				list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
488
				list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
489

    
490
				if (empty($peerid_data))
491
					continue;
492

    
493
				$myid = isset($ph1ent['mobile']) ? trim($myid_data) : "%any";
494
				$peerid = ($peerid_data != "allusers") ? trim($peerid_data) : "";
495
				if (!empty($ph1ent['pre-shared-key'])) {
496
					if ($myid_type == 'fqdn' && !empty($myid_data))
497
						$pskconf .= "@{$myid} {$peerid} : PSK 0s" . base64_encode(trim($ph1ent['pre-shared-key'])) . "\n";
498
					else
499
						$pskconf .= "{$myid} {$peerid} : PSK 0s" . base64_encode(trim($ph1ent['pre-shared-key'])) . "\n";
500
				}
501
			}
502
		}
503
	}
504

    
505
	/* Add user PSKs */
506
	if (is_array($config['system']) && is_array($config['system']['user'])) {
507
		foreach ($config['system']['user'] as $user) {
508
			if (!empty($user['ipsecpsk'])) {
509
				$pskconf .= "%any {$user['name']} : PSK 0s" . base64_encode($user['ipsecpsk']) . "\n";
510
			}
511
		}
512
		unset($user);
513
	}
514

    
515
	/* add PSKs for mobile clients */
516
	if (is_array($ipseccfg['mobilekey'])) {
517
		foreach ($ipseccfg['mobilekey'] as $key) {
518
			if ($key['ident'] == "allusers")
519
				$key['ident'] = '%any';
520
			if (empty($key['type']))
521
				$key['type'] = 'PSK';
522
			$pskconf .= "%any {$key['ident']} : {$key['type']} 0s" . base64_encode($key['pre-shared-key']) . "\n";
523
		}
524
		unset($key);
525
	}
526

    
527
	@file_put_contents("{$g['varetc_path']}/ipsec/ipsec.secrets", $pskconf);
528
	chmod("{$g['varetc_path']}/ipsec/ipsec.secrets", 0600);
529
	unset($pskconf);
530

    
531
	$natfilterrules = false;
532
	/* begin ipsec.conf */
533
	$ipsecconf = "";
534
	$enablecompression = false;
535
	if (is_array($a_phase1) && count($a_phase1))  {
536

    
537
		$ipsecconf .= "# This file is automatically generated. Do not edit\n";
538
		$ipsecconf .= "config setup\n\tuniqueids = yes\n";
539
		$ipsecconf .= "\tcharondebug=\"" . vpn_ipsec_configure_loglevels(true) . "\"\n";
540

    
541
		foreach ($a_phase1 as $ph1ent) {
542
			if (isset($ph1ent['disabled']))
543
				continue;
544

    
545
			if ($ph1ent['mode'] == "aggressive")
546
				$aggressive = "yes";
547
			else
548
				$aggressive = "no";
549

    
550
			$ep = ipsec_get_phase1_src($ph1ent);
551
			if (!$ep)
552
				continue;
553

    
554
			$ikeid = $ph1ent['ikeid'];
555
			$keyexchange = "ikev1";
556
			$passive = "route";
557
			if (!empty($ph1ent['iketype'])) {
558
				if ($ph1ent['iketype'] == "ikev2") {
559
					$keyexchange = "ikev2";
560
					//$passive = "start";
561
				} else if ($ph1ent['iketype'] == "auto")
562
					$keyexchange = "ike";
563
			}
564

    
565
			if (isset($ph1ent['mobile'])) {
566
				$right_spec = "%any";
567
				$passive = 'add';
568
			} else
569
				$right_spec = $ph1ent['remote-gateway'];
570

    
571
			list ($myid_type, $myid_data) = ipsec_find_id($ph1ent, "local");
572
			if ($myid_type == 'fqdn')
573
				$myid_data = "@{$myid_data}";
574
			list ($peerid_type, $peerid_data) = ipsec_find_id($ph1ent, "peer", $rgmap);
575
			if ($peerid_type == 'fqdn')
576
				$peerid_data = "@{$peerid_data}";
577

    
578
			/* Only specify peer ID if we are not dealing with a mobile PSK-only tunnel */
579
			$peerid_spec = '';
580
			if (!isset($ph1ent['mobile']))
581
				$peerid_spec = $peerid_data;
582

    
583
			if (is_array($ph1ent['encryption-algorithm']) && !empty($ph1ent['encryption-algorithm']['name']) && !empty($ph1ent['hash-algorithm'])) {
584
				$ealgosp1 = '';
585
				$ealg_id = $ph1ent['encryption-algorithm']['name'];
586
				$ealg_kl = $ph1ent['encryption-algorithm']['keylen'];
587
				if ($ealg_kl)
588
					$ealgosp1 = "ike = {$ealg_id}{$ealg_kl}-{$ph1ent['hash-algorithm']}";
589
				else
590
					$ealgosp1 = "ike = {$ealg_id}-{$ph1ent['hash-algorithm']}";
591

    
592
				$modp = vpn_ipsec_convert_to_modp($ph1ent['dhgroup']);
593
				if (!empty($modp))
594
					$ealgosp1 .= "-{$modp}";
595

    
596
				$ealgosp1 .= "!";
597
			}
598

    
599
			if ($ph1ent['dpd_delay'] && $ph1ent['dpd_maxfail']) {
600
				if ($passive == "route")
601
					$dpdline = "dpdaction = restart";
602
				else
603
					$dpdline = "dpdaction = clear";
604
				$dpdline .= "\n\tdpddelay = {$ph1ent['dpd_delay']}s";
605
				$dpdtimeout = $ph1ent['dpd_delay'] * ($ph1ent['dpd_maxfail'] + 1);
606
				$dpdline .= "\n\tdpdtimeout = {$dpdtimeout}s";
607
			} else
608
				$dpdline = "dpdaction = none";
609

    
610
			$ikelifeline = '';
611
			if ($ph1ent['lifetime'])
612
				$ikelifeline = "ikelifetime = {$ph1ent['lifetime']}s";
613

    
614
			$rightsourceip = NULL;
615
			if (isset($ph1ent['mobile']) && !empty($a_client['pool_address'])) 
616
				$rightsourceip = "\trightsourceip = {$a_client['pool_address']}/{$a_client['pool_netbits']}\n";
617

    
618
			$authentication = "";
619
			switch ($ph1ent['authentication_method']) {
620
			case 'eap-mschapv2':
621
				if (isset($ph1ent['mobile'])) {
622
					$authentication = "eap_identity=%any\n\t";
623
					$authentication .= "leftauth=pubkey\n\trightauth=eap-mschapv2";
624
					if (!empty($ph1ent['certref']))
625
						$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
626
				}
627
				break;
628
			case 'eap-tls':
629
				if (isset($ph1ent['mobile'])) {
630
					$authentication = "eap_identity=%identity\n\t";
631
					$authentication .= "leftauth=pubkey\n\trightauth=eap-tls";
632
					if (!empty($ph1ent['certref']))
633
						$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
634
				} else {
635
					$authentication = "leftauth=eap-tls\n\trightauth=eap-tls";
636
					if (!empty($ph1ent['certref']))
637
						$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
638
				}
639
				break;
640
			case 'xauth_rsa_server':
641
				$authentication = "leftauth = pubkey\n\trightauth = pubkey";
642
				$authentication .= "\n\trightauth2 = xauth-generic";
643
				if (!empty($ph1ent['certref']))
644
					$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
645
				break;
646
			case 'xauth_psk_server':
647
				$authentication = "leftauth = psk\n\trightauth = psk";
648
				$authentication .= "\n\trightauth2 = xauth-generic";
649
				break;
650
			case 'pre_shared_key':
651
				$authentication = "leftauth = psk\n\trightauth = psk";
652
				break;
653
			case 'rsasig':
654
				$authentication = "leftauth = pubkey\n\trightauth = pubkey";
655
				if (!empty($ph1ent['certref']))
656
					$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
657
				break;
658
			case 'hybrid_rsa_server':
659
				$authentication = "leftauth = xauth-generic\n\trightauth = pubkey";
660
				$authentication .= "\n\trightauth2 = xauth";
661
				if (!empty($ph1ent['certref']))
662
					$authentication .= "\n\tleftcert={$certpath}/cert-{$ph1ent['ikeid']}.crt";
663
				break;
664
			}
665

    
666
			$left_spec = $ep;
667

    
668
			if (isset($ph1ent['reauth_enable']))
669
				$reauth = "reauth = no";
670
			else
671
				$reauth = "reauth = yes";
672
			if (isset($ph1ent['rekey_enable']))
673
				$rekey = "rekey = no";
674
			else
675
				$rekey = "rekey = yes";
676

    
677
			if ($ph1ent['nat_traversal'] == 'off')
678
				$forceencaps = 'forceencaps = no';
679
			else if ($ph1ent['nat_traversal'] == 'force')
680
				$forceencaps = 'forceencaps = yes';
681
			else
682
				$forceencaps = 'forceencaps = no';
683

    
684
			$ipseclifetime = 0;
685
			$rightsubnet_spec = array();
686
			$leftsubnet_spec = array();
687
			$reqids = array();
688
			$ealgoAHsp2arr = array();
689
			$ealgoESPsp2arr = array();
690
		if (is_array($a_phase2) && count($a_phase2)) {
691
			foreach ($a_phase2 as $ph2ent) {
692
				if ($ikeid != $ph2ent['ikeid'])
693
					continue;
694

    
695
				if (isset($ph2ent['disabled']))
696
					continue;
697

    
698
				if (isset($ph2ent['mobile']) && !isset($a_client['enable']))
699
					continue;
700

    
701
				if (($ph2ent['mode'] == 'tunnel') or ($ph2ent['mode'] == 'tunnel6')) {
702
					$tunneltype = "type = tunnel";
703

    
704
					$localid_type = $ph2ent['localid']['type'];
705
					$leftsubnet_data = ipsec_idinfo_to_cidr($ph2ent['localid'], false, $ph2ent['mode']);
706

    
707
					/* Do not print localid in some cases, such as a pure-psk or psk/xauth single phase2 mobile tunnel */
708
					if (($localid_type == "none" || $localid_type == "mobile")
709
					    && isset($ph1ent['mobile']) && (ipsec_get_number_of_phase2($ikeid)==1)) {
710
						$left_spec = '%any';
711
					} else {
712
						if ($localid_type != "address") {
713
							$localid_type = "subnet";
714
						}
715
						// Don't let an empty subnet into config, it can cause parse errors. Ticket #2201.
716
						if (!is_ipaddr($leftsubnet_data) && !is_subnet($leftsubnet_data) && ($leftsubnet_data != "0.0.0.0/0")) {
717
							log_error("Invalid IPsec Phase 2 \"{$ph2ent['descr']}\" - {$ph2ent['localid']['type']} has no subnet.");
718
							continue;
719
						}
720
						if (!empty($ph2ent['natlocalid'])) {
721
							$natleftsubnet_data =  ipsec_idinfo_to_cidr($ph2ent['natlocalid'], false, $ph2ent['mode']);
722
							if ($ph2ent['natlocalid']['type'] != "address") {
723
								if (is_subnet($natleftsubnet_data))
724
									$leftsubnet_data = "{$natleftsubnet_data}|{$leftsubnet_data}";
725
							} else {
726
								if (is_ipaddr($natleftsubnet_data))
727
									$leftsubnet_data = "{$natleftsubnet_data}|{$leftsubnet_data}";
728
							}
729
							$natfilterrules = true;
730
						}
731
					}
732

    
733
					$leftsubnet_spec[] = $leftsubnet_data;
734

    
735
					if (!isset($ph2ent['mobile'])) {
736
						$tmpsubnet = ipsec_idinfo_to_cidr($ph2ent['remoteid'], false, $ph2ent['mode']);
737
						$rightsubnet_spec[] = $tmpsubnet;
738
					} else if (!empty($a_client['pool_address'])) {
739
						$rightsubnet_spec[] = "{$a_client['pool_address']}/{$a_client['pool_netbits']}";
740
					}
741
				} else {
742
					$tunneltype = "type = transport";
743

    
744
					if ((($ph1ent['authentication_method'] == "xauth_psk_server") ||
745
					    ($ph1ent['authentication_method'] == "pre_shared_key")) && isset($ph1ent['mobile'])) {
746
						$left_spec = "%any";
747
					} else {
748
						$tmpsubnet = ipsec_get_phase1_src($ph1ent);
749
						$leftsubnet_spec[] = $tmpsubnet;
750
					}
751

    
752
					if (!isset($ph2ent['mobile'])) {
753
						$rightsubnet_spec[] = $right_spec;
754
					}
755
				}
756

    
757
				if (isset($a_client['pfs_group']))
758
					$ph2ent['pfsgroup'] = $a_client['pfs_group'];
759

    
760
				if ($ph2ent['protocol'] == 'esp') {
761
					if (is_array($ph2ent['encryption-algorithm-option'])) {
762
						foreach ($ph2ent['encryption-algorithm-option'] as $ealg) {
763
							$ealg_id = $ealg['name'];
764
							$ealg_kl = $ealg['keylen'];
765

    
766
							if (!empty($ealg_kl) && $ealg_kl == "auto") {
767
								if (empty($p2_ealgos) || !is_array($p2_ealgos))
768
									require("ipsec.inc");
769
								$key_hi = $p2_ealgos[$ealg_id]['keysel']['hi'];
770
								$key_lo = $p2_ealgos[$ealg_id]['keysel']['lo'];
771
								$key_step = $p2_ealgos[$ealg_id]['keysel']['step'];
772
								/* XXX: in some cases where include ordering is suspect these variables
773
								 * are somehow 0 and we enter this loop forever and timeout after 900
774
								 * seconds wrecking bootup */
775
								if ($key_hi != 0 and $key_lo !=0 and $key_step !=0) {
776
									for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) {
777
										if (!empty($ph2ent['hash-algorithm-option']) && is_array($ph2ent['hash-algorithm-option'])) {
778
											foreach ($ph2ent['hash-algorithm-option'] as $halgo) {
779
												$halgo = str_replace('hmac_', '', $halgo);
780
												$tmpealgo = "{$ealg_id}{$keylen}-{$halgo}";
781
												$modp = vpn_ipsec_convert_to_modp($ph2ent['pfsgroup']);
782
												if (!empty($modp))
783
													$tmpealgo .= "-{$modp}";
784
												$ealgoESPsp2arr[] = $tmpealgo;
785
											}
786
										} else {
787
											$tmpealgo = "{$ealg_id}{$keylen}";
788
											$modp = vpn_ipsec_convert_to_modp($ph2ent['pfsgroup']);
789
											if (!empty($modp))
790
												$tmpealgo .= "-{$modp}";
791
											$ealgoESPsp2arr[] = $tmpealgo;
792
										}
793
									}
794
								}
795
							} else {
796
								if (!empty($ph2ent['hash-algorithm-option']) && is_array($ph2ent['hash-algorithm-option'])) {
797
									foreach ($ph2ent['hash-algorithm-option'] as $halgo) {
798
										$halgo = str_replace('hmac_', '', $halgo);
799
										$tmpealgo = "{$ealg_id}{$ealg_kl}-{$halgo}";
800
										$modp = vpn_ipsec_convert_to_modp($ph2ent['pfsgroup']);
801
										if (!empty($modp))
802
											$tmpealgo .= "-{$modp}";
803
										$ealgoESPsp2arr[] = $tmpealgo;
804
									}
805
								} else {
806
									$tmpealgo = "{$ealg_id}{$ealg_kl}";
807
									$modp = vpn_ipsec_convert_to_modp($ph2ent['pfsgroup']);
808
									if (!empty($modp))
809
										$tmpealgo .= "-{$modp}";
810
									$ealgoESPsp2arr[] = $tmpealgo;
811
								}
812
							}
813
						}
814
					}
815
				} else if ($ph2ent['protocol'] == 'ah') {
816
					if (!empty($ph2ent['hash-algorithm-option']) && is_array($ph2ent['hash-algorithm-option'])) {
817
						$modp = vpn_ipsec_convert_to_modp($ph2ent['pfsgroup']);
818
						foreach ($ph2ent['hash-algorithm-option'] as $tmpAHalgo) {
819
							$tmpAHalgo = str_replace('hmac_', '', $tmpAHalgo);
820
							if (!empty($modp))
821
								$tmpAHalgo = "-{$modp}";
822
							$ealgoAHsp2arr[] = $tmpAHalgo;
823
						}
824
					}
825
				}
826

    
827
				$reqids[] = $ph2ent['reqid'];
828

    
829
				if (!empty($ph2ent['lifetime'])) {
830
					if ($ipseclifetime == 0 || intval($ipseclifetime) > intval($ph2ent['lifetime']))
831
						$ipseclifetime = intval($ph2ent['lifetime']);
832
				}
833

    
834
			}
835
		}
836

    
837
			$ipsecconnect =<<<EOD
838
	fragmentation = yes
839
	keyexchange = {$keyexchange}
840
	{$reauth}
841
	{$forceencaps}
842
	{$rekey}
843
	installpolicy = yes
844
	{$tunneltype}
845
	{$dpdline}
846
	auto = {$passive}
847
	left = {$left_spec}
848
	right = {$right_spec}
849
	leftid = {$myid_data}
850

    
851
EOD;
852

    
853
			if (isset($config['ipsec']['compression'])) {
854
				$ipsecconnect .= "\tcompress = yes\n";
855
				$enablecompression = true;
856
			}
857
			if (!empty($ikelifeline))
858
				$ipsecconnect .= "\t{$ikelifeline}\n";
859
			if ($ipseclifetime > 0)
860
				$ipsecconnect .= "\tlifetime = {$ipseclifetime}s\n";
861
			if (!empty($rightsourceip))
862
				$ipsecconnect .= "{$rightsourceip}";
863
			if (!empty($ealgosp1))
864
				$ipsecconnect .= "\t{$ealgosp1}\n";
865
			if (!empty($ealgoAHsp2arr))
866
				$ipsecconnect .= "\tah = " . join(',', $ealgoAHsp2arr) . "!\n";
867
			if (!empty($ealgoESPsp2arr))
868
				$ipsecconnect .= "\tesp = " . join(',', $ealgoESPsp2arr) . "!\n";
869
			if (!empty($authentication))
870
				$ipsecconnect .= "\t{$authentication}\n";
871
			if (!empty($peerid_spec))
872
				$ipsecconnect .= "\trightid = {$peerid_spec}\n";
873
			if ($keyexchange == 'ikev1')
874
				$ipsecconnect .= "\taggressive = {$aggressive}\n";
875

    
876
			if (!isset($ph1ent['mobile']) && $keyexchange == 'ikev1') {
877
				if (!empty($rightsubnet_spec)) {
878
					$ipsecfin = '';
879
					foreach ($rightsubnet_spec as $idx => $rsubnet) {
880
						$ipsecfin .= "\nconn con{$ph1ent['ikeid']}00{$idx}\n";
881
						if (!empty($reqids[$idx]))
882
							$ipsecfin .= "\treqid = " . $reqids[$idx] . "\n";
883
						$ipsecfin .= $ipsecconnect;
884
						$ipsecfin .= "\trightsubnet = {$rsubnet}\n";
885
						$ipsecfin .= "\tleftsubnet = " . $leftsubnet_spec[$idx] . "\n";
886
					}
887
				} else
888
					log_error("No phase2 specifications for tunnel with REQID = {$ikeid}");
889
			} else {
890
				$ipsecfin = "\nconn con{$ph1ent['ikeid']}\n";
891
				if (!empty($reqids[$idx]))
892
					$ipsecfin .= "\treqid = " . $reqids[0] . "\n";
893
				$ipsecfin .= $ipsecconnect;
894
				if (!empty($rightsubnet_spec)) {
895
					$tempsubnets = array();
896
					foreach ($rightsubnet_spec as $rightsubnet)
897
						$tempsubnets[$rightsubnet] = $rightsubnet;
898
					$ipsecfin .= "\trightsubnet = " . join(",", $tempsubnets) . "\n";
899
					unset($tempsubnets, $rightsubnet);
900
				}
901
				if (!empty($leftsubnet_spec)) {
902
					$tempsubnets = array();
903
					foreach ($leftsubnet_spec as $leftsubnet)
904
						$tempsubnets[$leftsubnet] = $leftsubnet;
905
					$ipsecfin .= "\tleftsubnet = " . join(",", $leftsubnet_spec) . "\n";
906
					unset($tempsubnets, $leftsubnet);
907
				}
908
			}
909
			$ipsecconf .= $ipsecfin;
910
			unset($ipsecfin);
911

    
912
		}
913
	}
914

    
915
	@file_put_contents("{$g['varetc_path']}/ipsec/ipsec.conf", $ipsecconf);
916
	unset($ipsecconf);
917
	/* end ipsec.conf */
918

    
919
	if ($enablecompression === true)
920
		set_single_sysctl('net.inet.ipcomp.ipcomp_enable', 1);
921
	else
922
		set_single_sysctl('net.inet.ipcomp.ipcomp_enable', 0);
923

    
924
	/* mange process */
925
	if (isvalidpid("{$g['varrun_path']}/charon.pid")) {
926
		/* Read secrets */
927
		mwexec("/usr/local/sbin/ipsec rereadall", false);
928
		/* Update configuration changes */
929
		mwexec("/usr/local/sbin/ipsec update", false);
930
	} else {
931
		mwexec("/usr/local/sbin/ipsec start", false); 
932
	}
933

    
934
	if ($natfilterrules == true)
935
		filter_configure();
936
	/* start filterdns, if necessary */
937
	if (count($filterdns_list) > 0) {
938
		$interval = 60;
939
		if (!empty($ipseccfg['dns-interval']) && is_numeric($ipseccfg['dns-interval']))
940
			$interval = $ipseccfg['dns-interval'];
941

    
942
		$hostnames = "";
943
		array_unique($filterdns_list);
944
		foreach ($filterdns_list as $hostname)
945
			$hostnames .= "cmd {$hostname} '/usr/local/sbin/pfSctl -c \"service reload ipsecdns\"'\n";
946
		file_put_contents("{$g['varetc_path']}/ipsec/filterdns-ipsec.hosts", $hostnames);
947
		unset($hostnames);
948

    
949
		if (isvalidpid("{$g['varrun_path']}/filterdns-ipsec.pid"))
950
			sigkillbypid("{$g['varrun_path']}/filterdns-ipsec.pid", "HUP");
951
		else {
952
			mwexec("/usr/local/sbin/filterdns -p {$g['varrun_path']}/filterdns-ipsec.pid -i {$interval} -c {$g['varetc_path']}/ipsec/filterdns-ipsec.hosts -d 1");
953
		}
954
	} else {
955
		killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
956
		@unlink("{$g['varrun_path']}/filterdns-ipsec.pid");
957
	}
958

    
959
	if (platform_booting())
960
		echo "done\n";
961

    
962
	return count($filterdns_list);
963
}
964

    
965
/*
966
 * Forcefully restart IPsec
967
 * This is required for when dynamic interfaces reload
968
 * For all other occasions the normal vpn_ipsec_configure()
969
 * will gracefully reload the settings without restarting
970
 */
971
function vpn_ipsec_force_reload($interface = "") {
972
	global $g, $config;
973

    
974
	$ipseccfg = $config['ipsec'];
975

    
976
	if (!empty($interface) && is_array($ipseccfg['phase1'])) {
977
		$found = false;
978
		foreach ($ipseccfg['phase1'] as $ipsec) {
979
			if (!isset($ipsec['disabled']) && ($ipsec['interface'] == $interface)) {
980
				$found = true;
981
				break;
982
			}
983
		}
984
		if (!$found) {
985
			log_error(sprintf(gettext("Ignoring IPsec reload since there are no tunnels on interface %s"), $interface));
986
			return;
987
		}
988
	}
989

    
990
	/* if ipsec is enabled, start up again */
991
	if (isset($ipseccfg['enable'])) {
992
		log_error(gettext("Forcefully reloading IPsec"));
993
		vpn_ipsec_configure();
994
	}
995
}
996

    
997
/* master setup for vpn (mpd) */
998
function vpn_setup() {
999
	global $g;
1000

    
1001
	if ($g['platform'] == 'jail')
1002
		return;
1003

    
1004
	/* start pptpd */
1005
	vpn_pptpd_configure();
1006

    
1007
	/* start pppoe server */
1008
	vpn_pppoes_configure();
1009

    
1010
	/* setup l2tp */
1011
	vpn_l2tp_configure();
1012
}
1013

    
1014
function vpn_netgraph_support() {
1015
	$iflist = get_configured_interface_list();
1016
	foreach ($iflist as $iface) {
1017
		$realif = get_real_interface($iface);
1018
		/* Get support for netgraph(4) from the nic */
1019
		$ifinfo = pfSense_get_interface_addresses($realif);
1020
		if (!empty($ifinfo) && in_array($ifinfo['iftype'], array("ether", "vlan", "bridge")))
1021
			pfSense_ngctl_attach(".", $realif);
1022
	}
1023
}
1024

    
1025
function vpn_pptpd_configure() {
1026
	global $config, $g;
1027

    
1028
	$syscfg = $config['system'];
1029
	$pptpdcfg = $config['pptpd'];
1030

    
1031
	if (platform_booting()) {
1032
		if (!$pptpdcfg['mode'] || ($pptpdcfg['mode'] == "off"))
1033
			return 0;
1034

    
1035
		if (platform_booting(true))
1036
			echo gettext("Configuring PPTP VPN service... ");
1037
	} else {
1038
		/* kill mpd */
1039
		killbypid("{$g['varrun_path']}/pptp-vpn.pid");
1040

    
1041
		/* wait for process to die */
1042
		sleep(3);
1043

    
1044
		if (is_process_running("mpd -b")) {
1045
			killbypid("{$g['varrun_path']}/pptp-vpn.pid");
1046
			log_error(gettext("Could not kill mpd within 3 seconds.   Trying again."));
1047
		}
1048

    
1049
		/* remove mpd.conf, if it exists */
1050
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.conf");
1051
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.links");
1052
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.secret");
1053
	}
1054

    
1055
	if (empty($pptpdcfg['n_pptp_units'])) {
1056
		log_error("Something wrong in the PPTPd configuration. Preventing starting the daemon because issues would arise.");
1057
		return;
1058
	}
1059

    
1060
	/* make sure pptp-vpn directory exists */
1061
	if (!file_exists("{$g['varetc_path']}/pptp-vpn"))
1062
		mkdir("{$g['varetc_path']}/pptp-vpn");
1063

    
1064
	switch ($pptpdcfg['mode']) {
1065
		case 'server' :
1066
			/* write mpd.conf */
1067
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.conf", "w");
1068
			if (!$fd) {
1069
				printf(gettext("Error: cannot open mpd.conf in vpn_pptpd_configure().") . "\n");
1070
				return 1;
1071
			}
1072

    
1073
			$mpdconf = <<<EOD
1074
pptps:
1075

    
1076
EOD;
1077

    
1078
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1079
				$mpdconf .= "	load pt{$i}\n";
1080
			}
1081

    
1082
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1083

    
1084
				$clientip = long2ip32(ip2long($pptpdcfg['remoteip']) + $i);
1085

    
1086
				$mpdconf .= <<<EOD
1087

    
1088
pt{$i}:
1089
	new -i pptpd{$i} pt{$i} pt{$i}
1090
	set ipcp ranges {$pptpdcfg['localip']}/32 {$clientip}/32
1091
	load pts
1092

    
1093
EOD;
1094
			}
1095

    
1096
			$mpdconf .=<<<EOD
1097

    
1098
pts:
1099
	set iface disable on-demand
1100
	set iface enable proxy-arp
1101
	set iface enable tcpmssfix
1102
	set iface idle 1800
1103
	set iface up-script /usr/local/sbin/vpn-linkup
1104
	set iface down-script /usr/local/sbin/vpn-linkdown
1105
	set bundle enable multilink
1106
	set bundle enable crypt-reqd
1107
	set link yes acfcomp protocomp
1108
	set link no pap chap
1109
	set link enable chap-msv2
1110
	set link mtu 1460
1111
	set link keep-alive 10 60
1112
	set ipcp yes vjcomp
1113
	set bundle enable compression
1114
	set ccp yes mppc
1115
	set ccp yes mpp-e128
1116
	set ccp yes mpp-stateless
1117

    
1118
EOD;
1119

    
1120
			if (!isset ($pptpdcfg['req128'])) {
1121
				$mpdconf .=<<<EOD
1122
	set ccp yes mpp-e40
1123
	set ccp yes mpp-e56
1124

    
1125
EOD;
1126
			}
1127

    
1128
			if  (isset($pptpdcfg["wins"]) && $pptpdcfg['wins'] != "")
1129
				$mpdconf  .=  "	set ipcp nbns {$pptpdcfg['wins']}\n";
1130

    
1131
			if (!empty($pptpdcfg['dns1'])) {
1132
				$mpdconf .= "	set ipcp dns " . $pptpdcfg['dns1'];
1133
				if (!empty($pptpdcfg['dns2']))
1134
					$mpdconf .= " " . $pptpdcfg['dns2'];
1135
				$mpdconf .= "\n";
1136
			} elseif (isset ($config['dnsmasq']['enable'])) {
1137
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1138
				if ($syscfg['dnsserver'][0])
1139
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1140
				$mpdconf .= "\n";
1141
			} elseif (isset($config['unbound']['enable'])) {
1142
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1143
				if ($syscfg['dnsserver'][0])
1144
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1145
				$mpdconf .= "\n";
1146
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1147
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1148
			}
1149

    
1150
			if (isset ($pptpdcfg['radius']['server']['enable'])) {
1151
				$authport = (isset($pptpdcfg['radius']['server']['port']) && strlen($pptpdcfg['radius']['server']['port']) > 1) ? $pptpdcfg['radius']['server']['port'] : 1812;
1152
				$acctport = $authport + 1;
1153
				$mpdconf .=<<<EOD
1154
	set radius server {$pptpdcfg['radius']['server']['ip']} "{$pptpdcfg['radius']['server']['secret']}" {$authport} {$acctport}
1155

    
1156
EOD;
1157
			if (isset ($pptpdcfg['radius']['server2']['enable'])) {
1158
				$authport = (isset($pptpdcfg['radius']['server2']['port']) && strlen($pptpdcfg['radius']['server2']['port']) > 1) ? $pptpdcfg['radius']['server2']['port'] : 1812;
1159
				$acctport = $authport + 1;
1160
				$mpdconf .=<<<EOD
1161
	set radius server {$pptpdcfg['radius']['server2']['ip']} "{$pptpdcfg['radius']['server2']['secret2']}" {$authport} {$acctport}
1162

    
1163
EOD;
1164
			}
1165
			$mpdconf .=<<<EOD
1166
	set radius retries 3
1167
	set radius timeout 10
1168
	set auth enable radius-auth
1169

    
1170
EOD;
1171

    
1172
				if (isset ($pptpdcfg['radius']['accounting'])) {
1173
					$mpdconf .=<<<EOD
1174
	set auth enable radius-acct
1175
	set radius acct-update 300
1176

    
1177
EOD;
1178
				}
1179
			}
1180

    
1181
			fwrite($fd, $mpdconf);
1182
			fclose($fd);
1183
			unset($mpdconf);
1184

    
1185
			/* write mpd.links */
1186
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.links", "w");
1187
			if (!$fd) {
1188
				printf(gettext("Error: cannot open mpd.links in vpn_pptpd_configure().") . "\n");
1189
				return 1;
1190
			}
1191

    
1192
			$mpdlinks = "";
1193

    
1194
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1195
				$mpdlinks .=<<<EOD
1196

    
1197
pt{$i}:
1198
	set link type pptp
1199
	set pptp enable incoming
1200
	set pptp disable originate
1201
	set pptp disable windowing
1202

    
1203
EOD;
1204
			}
1205

    
1206
			fwrite($fd, $mpdlinks);
1207
			fclose($fd);
1208
			unset($mpdlinks);
1209

    
1210
			/* write mpd.secret */
1211
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.secret", "w");
1212
			if (!$fd) {
1213
				printf(gettext("Error: cannot open mpd.secret in vpn_pptpd_configure().") . "\n");
1214
				return 1;
1215
			}
1216

    
1217
			$mpdsecret = "";
1218

    
1219
			if (is_array($pptpdcfg['user'])) {
1220
				foreach ($pptpdcfg['user'] as $user) {
1221
					$pass = str_replace('\\', '\\\\', $user['password']);
1222
					$pass = str_replace('"', '\"', $pass);
1223
					$mpdsecret .= "{$user['name']} \"{$pass}\" {$user['ip']}\n";
1224
				}
1225
			}
1226

    
1227
			fwrite($fd, $mpdsecret);
1228
			fclose($fd);
1229
			unset($mpdsecret);
1230
			chmod("{$g['varetc_path']}/pptp-vpn/mpd.secret", 0600);
1231

    
1232
			vpn_netgraph_support();
1233

    
1234
			/* fire up mpd */
1235
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/pptp-vpn -p {$g['varrun_path']}/pptp-vpn.pid -s pptps pptps");
1236

    
1237
			break;
1238

    
1239
		case 'redir' :
1240
			break;
1241
	}
1242

    
1243
	if (platform_booting())
1244
		echo "done\n";
1245

    
1246
	return 0;
1247
}
1248

    
1249
function vpn_pppoes_configure() {
1250
	global $config;
1251

    
1252
	if (is_array($config['pppoes']['pppoe'])) {
1253
		foreach ($config['pppoes']['pppoe'] as $pppoe)
1254
			vpn_pppoe_configure($pppoe);
1255
	}
1256
}
1257

    
1258
function vpn_pppoe_configure(&$pppoecfg) {
1259
	global $config, $g;
1260

    
1261
	$syscfg = $config['system'];
1262

    
1263
	/* create directory if it does not exist */
1264
	if (!is_dir("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn"))
1265
		mkdir("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn");
1266

    
1267
	if (platform_booting()) {
1268
		if (!$pppoecfg['mode'] || ($pppoecfg['mode'] == "off"))
1269
			return 0;
1270

    
1271
		echo gettext("Configuring PPPoE Server service... ");
1272
	} else {
1273
		/* kill mpd */
1274
		killbypid("{$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid");
1275

    
1276
		/* wait for process to die */
1277
		sleep(2);
1278

    
1279
	}
1280

    
1281
	switch ($pppoecfg['mode']) {
1282

    
1283
		case 'server' :
1284

    
1285
			$pppoe_interface = get_real_interface($pppoecfg['interface']);
1286

    
1287
			if ($pppoecfg['paporchap'] == "chap")
1288
				$paporchap = "set link enable chap";
1289
			else
1290
				$paporchap = "set link enable pap";
1291

    
1292
			/* write mpd.conf */
1293
			$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.conf", "w");
1294
			if (!$fd) {
1295
				printf(gettext("Error: cannot open mpd.conf in vpn_pppoe_configure().") . "\n");
1296
				return 1;
1297
			}
1298
			$mpdconf = "\n\n";
1299
			$mpdconf .= "poes:\n";
1300

    
1301
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1302
				$mpdconf .= "	load poes{$pppoecfg['pppoeid']}{$i}\n";
1303
			}
1304

    
1305
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1306

    
1307
				$clientip = long2ip32(ip2long($pppoecfg['remoteip']) + $i);
1308

    
1309
				if (isset($pppoecfg['radius']['radiusissueips']) && isset($pppoecfg['radius']['server']['enable'])) {
1310
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 0.0.0.0/0";
1311
				} else {
1312
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 {$clientip}/32";
1313
				}
1314

    
1315
				$mpdconf .=<<<EOD
1316

    
1317
poes{$pppoecfg['pppoeid']}{$i}:
1318
	new -i poes{$pppoecfg['pppoeid']}{$i} poes{$pppoecfg['pppoeid']}{$i} poes{$pppoecfg['pppoeid']}{$i}
1319
	{$isssue_ip_type}
1320
	load pppoe_standard
1321

    
1322
EOD;
1323
			}
1324

    
1325
			$mpdconf .=<<<EOD
1326

    
1327
pppoe_standard:
1328
	set bundle no multilink
1329
	set bundle enable compression
1330
	set auth max-logins 1
1331
	set iface up-script /usr/local/sbin/vpn-linkup
1332
	set iface down-script /usr/local/sbin/vpn-linkdown
1333
	set iface idle 0
1334
	set iface disable on-demand
1335
	set iface disable proxy-arp
1336
	set iface enable tcpmssfix
1337
	set iface mtu 1500
1338
	set link no pap chap
1339
	{$paporchap}
1340
	set link keep-alive 60 180
1341
	set ipcp yes vjcomp
1342
	set ipcp no vjcomp
1343
	set link max-redial -1
1344
	set link mtu 1492
1345
	set link mru 1492
1346
	set ccp yes mpp-e40
1347
	set ccp yes mpp-e128
1348
	set ccp yes mpp-stateless
1349
	set link latency 1
1350
	#set ipcp dns 10.10.1.3
1351
	#set bundle accept encryption
1352

    
1353
EOD;
1354

    
1355
			if (!empty($pppoecfg['dns1'])) {
1356
				$mpdconf .= "	set ipcp dns " . $pppoecfg['dns1'];
1357
				if (!empty($pppoecfg['dns2']))
1358
					$mpdconf .= " " . $pppoecfg['dns2'];
1359
				$mpdconf .= "\n";
1360
			} elseif (isset ($config['dnsmasq']['enable'])) {
1361
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1362
				if ($syscfg['dnsserver'][0])
1363
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1364
				$mpdconf .= "\n";
1365
			} elseif (isset ($config['unbound']['enable'])) {
1366
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1367
				if ($syscfg['dnsserver'][0])
1368
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1369
				$mpdconf .= "\n";
1370
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1371
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1372
			}
1373

    
1374
			if (isset ($pppoecfg['radius']['server']['enable'])) {
1375
				$radiusport = "";
1376
				$radiusacctport = "";
1377
				if (isset($pppoecfg['radius']['server']['port']))
1378
					$radiusport = $pppoecfg['radius']['server']['port'];
1379
				if (isset($pppoecfg['radius']['server']['acctport']))
1380
					$radiusacctport = $pppoecfg['radius']['server']['acctport'];
1381
				$mpdconf .=<<<EOD
1382
	set radius server {$pppoecfg['radius']['server']['ip']} "{$pppoecfg['radius']['server']['secret']}" {$radiusport} {$radiusacctport}
1383
	set radius retries 3
1384
	set radius timeout 10
1385
	set auth enable radius-auth
1386

    
1387
EOD;
1388

    
1389
				if (isset ($pppoecfg['radius']['accounting'])) {
1390
					$mpdconf .=<<<EOD
1391
	set auth enable radius-acct
1392

    
1393
EOD;
1394
				}
1395
			}
1396

    
1397
			fwrite($fd, $mpdconf);
1398
			fclose($fd);
1399
			unset($mpdconf);
1400

    
1401
			/* write mpd.links */
1402
			$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.links", "w");
1403
			if (!$fd) {
1404
				printf(gettext("Error: cannot open mpd.links in vpn_pppoe_configure().") . "\n");
1405
				return 1;
1406
			}
1407

    
1408
			$mpdlinks = "";
1409

    
1410
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1411
				$mpdlinks .=<<<EOD
1412

    
1413
poes{$pppoecfg['pppoeid']}{$i}:
1414
	set phys type pppoe
1415
	set pppoe iface {$pppoe_interface}
1416
	set pppoe service "*"
1417
	set pppoe disable originate
1418
	set pppoe enable incoming
1419

    
1420
EOD;
1421
			}
1422

    
1423
			fwrite($fd, $mpdlinks);
1424
			fclose($fd);
1425
			unset($mpdlinks);
1426

    
1427
			if ($pppoecfg['username']) {
1428
				/* write mpd.secret */
1429
				$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", "w");
1430
				if (!$fd) {
1431
					printf(gettext("Error: cannot open mpd.secret in vpn_pppoe_configure().") . "\n");
1432
					return 1;
1433
				}
1434

    
1435
				$mpdsecret = "\n\n";
1436

    
1437
				if (!empty($pppoecfg['username'])) {
1438
					$item = explode(" ", $pppoecfg['username']);
1439
					foreach($item as $userdata) {
1440
						$data = explode(":", $userdata);
1441
						$mpdsecret .= "{$data[0]} \"" . base64_decode($data[1]) . "\" {$data[2]}\n";
1442
					}
1443
				}
1444

    
1445
				fwrite($fd, $mpdsecret);
1446
				fclose($fd);
1447
				unset($mpdsecret);
1448
				chmod("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", 0600);
1449
			}
1450

    
1451
			/* Check if previous instance is still up */
1452
			while (file_exists("{$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid") && isvalidpid("{$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid"))
1453
				killbypid("{$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid");
1454

    
1455
			/* Get support for netgraph(4) from the nic */
1456
			pfSense_ngctl_attach(".", $pppoe_interface);
1457
			/* fire up mpd */
1458
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn -p {$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid -s poes poes");
1459

    
1460
			break;
1461
	}
1462

    
1463
	if (platform_booting())
1464
		echo gettext("done") . "\n";
1465

    
1466
	return 0;
1467
}
1468

    
1469
function vpn_l2tp_configure() {
1470
	global $config, $g;
1471

    
1472
	$syscfg = $config['system'];
1473
	$l2tpcfg = $config['l2tp'];
1474

    
1475
	/* create directory if it does not exist */
1476
	if (!is_dir("{$g['varetc_path']}/l2tp-vpn"))
1477
		mkdir("{$g['varetc_path']}/l2tp-vpn");
1478

    
1479
	if (platform_booting()) {
1480
		if (!$l2tpcfg['mode'] || ($l2tpcfg['mode'] == "off"))
1481
			return 0;
1482

    
1483
		echo gettext("Configuring l2tp VPN service... ");
1484
	} else {
1485
		/* kill mpd */
1486
		killbypid("{$g['varrun_path']}/l2tp-vpn.pid");
1487

    
1488
		/* wait for process to die */
1489
		sleep(8);
1490

    
1491
	}
1492

    
1493
	/* make sure l2tp-vpn directory exists */
1494
	if (!file_exists("{$g['varetc_path']}/l2tp-vpn"))
1495
		mkdir("{$g['varetc_path']}/l2tp-vpn");
1496

    
1497
	switch ($l2tpcfg['mode']) {
1498

    
1499
		case 'server' :
1500
			if ($l2tpcfg['paporchap'] == "chap")
1501
				$paporchap = "set link enable chap";
1502
			else
1503
				$paporchap = "set link enable pap";
1504

    
1505
			/* write mpd.conf */
1506
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.conf", "w");
1507
			if (!$fd) {
1508
				printf(gettext("Error: cannot open mpd.conf in vpn_l2tp_configure().") . "\n");
1509
				return 1;
1510
			}
1511
			$mpdconf = "\n\n";
1512
			$mpdconf .=<<<EOD
1513
l2tps:
1514

    
1515
EOD;
1516

    
1517
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1518
				$mpdconf .= "	load l2tp{$i}\n";
1519
			}
1520

    
1521
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1522

    
1523
				$clientip = long2ip32(ip2long($l2tpcfg['remoteip']) + $i);
1524

    
1525
				if (isset ($l2tpcfg['radius']['radiusissueips']) && isset ($l2tpcfg['radius']['enable'])) {
1526
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 0.0.0.0/0";
1527
				} else {
1528
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 {$clientip}/32";
1529
				}
1530

    
1531
				$mpdconf .=<<<EOD
1532

    
1533
l2tp{$i}:
1534
	new -i l2tp{$i} l2tp{$i} l2tp{$i}
1535
	{$isssue_ip_type}
1536
	load l2tp_standard
1537

    
1538
EOD;
1539
			}
1540

    
1541
			$mpdconf .=<<<EOD
1542

    
1543
l2tp_standard:
1544
	set bundle disable multilink
1545
	set bundle enable compression
1546
	set bundle yes crypt-reqd
1547
	set ipcp yes vjcomp
1548
	# set ipcp ranges 131.188.69.161/32 131.188.69.170/28
1549
	set ccp yes mppc
1550
	set iface disable on-demand
1551
	set iface enable proxy-arp
1552
	set iface up-script /usr/local/sbin/vpn-linkup
1553
	set iface down-script /usr/local/sbin/vpn-linkdown
1554
	set link yes acfcomp protocomp
1555
	set link no pap chap
1556
	set link enable chap
1557
	set link keep-alive 10 180
1558

    
1559
EOD;
1560

    
1561
			if (is_ipaddr($l2tpcfg['wins'])) {
1562
				$mpdconf .= "	set ipcp nbns {$l2tpcfg['wins']}\n";
1563
			}
1564
			if (is_ipaddr($l2tpcfg['dns1'])) {
1565
				$mpdconf .= "	set ipcp dns " . $l2tpcfg['dns1'];
1566
				if (is_ipaddr($l2tpcfg['dns2']))
1567
					$mpdconf .= " " . $l2tpcfg['dns2'];
1568
				$mpdconf .= "\n";
1569
			} elseif (isset ($config['dnsmasq']['enable'])) {
1570
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1571
				if ($syscfg['dnsserver'][0])
1572
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1573
				$mpdconf .= "\n";
1574
			} elseif (isset ($config['unbound']['enable'])) {
1575
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1576
				if ($syscfg['dnsserver'][0])
1577
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1578
				$mpdconf .= "\n";
1579
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1580
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1581
			}
1582

    
1583
			if (isset ($l2tpcfg['radius']['enable'])) {
1584
				$mpdconf .=<<<EOD
1585
	set radius server {$l2tpcfg['radius']['server']} "{$l2tpcfg['radius']['secret']}"
1586
	set radius retries 3
1587
	set radius timeout 10
1588
	set auth enable radius-auth
1589

    
1590
EOD;
1591

    
1592
				if (isset ($l2tpcfg['radius']['accounting'])) {
1593
					$mpdconf .=<<<EOD
1594
	set auth enable radius-acct
1595

    
1596
EOD;
1597
				}
1598
			}
1599

    
1600
			fwrite($fd, $mpdconf);
1601
			fclose($fd);
1602
			unset($mpdconf);
1603

    
1604
			/* write mpd.links */
1605
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.links", "w");
1606
			if (!$fd) {
1607
				printf(gettext("Error: cannot open mpd.links in vpn_l2tp_configure().") . "\n");
1608
				return 1;
1609
			}
1610

    
1611
			$mpdlinks = "";
1612

    
1613
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1614
				$mpdlinks .=<<<EOD
1615

    
1616
l2tp{$i}:
1617
	set link type l2tp
1618
	set l2tp enable incoming
1619
	set l2tp disable originate
1620

    
1621
EOD;
1622
			if (!empty($l2tpcfg['secret']))
1623
					$mpdlinks .= "set l2tp secret {$l2tpcfg['secret']}\n";
1624
			}
1625

    
1626
			fwrite($fd, $mpdlinks);
1627
			fclose($fd);
1628
			unset($mpdlinks);
1629

    
1630
			/* write mpd.secret */
1631
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.secret", "w");
1632
			if (!$fd) {
1633
				printf(gettext("Error: cannot open mpd.secret in vpn_l2tp_configure().") . "\n");
1634
				return 1;
1635
			}
1636

    
1637
			$mpdsecret = "\n\n";
1638

    
1639
			if (is_array($l2tpcfg['user'])) {
1640
				foreach ($l2tpcfg['user'] as $user)
1641
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
1642
			}
1643

    
1644
			fwrite($fd, $mpdsecret);
1645
			fclose($fd);
1646
			unset($mpdsecret);
1647
			chmod("{$g['varetc_path']}/l2tp-vpn/mpd.secret", 0600);
1648

    
1649
			vpn_netgraph_support();
1650

    
1651
			/* fire up mpd */
1652
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/l2tp-vpn -p {$g['varrun_path']}/l2tp-vpn.pid -s l2tps l2tps");
1653

    
1654
			break;
1655

    
1656
		case 'redir' :
1657
			break;
1658
	}
1659

    
1660
	if (platform_booting())
1661
		echo "done\n";
1662

    
1663
	return 0;
1664
}
1665

    
1666
function vpn_ipsec_configure_preferoldsa() {
1667
	global $config;
1668
	if(isset($config['ipsec']['preferoldsa']))
1669
		set_single_sysctl("net.key.preferred_oldsa", "-30");
1670
	else
1671
		set_single_sysctl("net.key.preferred_oldsa", "0");
1672
}
1673

    
1674
?>
(59-59/68)