Project

General

Profile

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

    
3
/*
4
	vpn.inc
5
	Copyright (C) 2004 Scott Ullrich
6
	All rights reserved.
7

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

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

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

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

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

    
34
/* include all configuration functions */
35
require_once ("functions.inc");
36

    
37
/* master setup for vpn (mpd) */
38
function vpn_setup() {
39
	/* start pptpd */
40
	vpn_pptpd_configure();
41

    
42
	/* start pppoe server */
43
	vpn_pppoe_configure();
44

    
45
	/* setup l2tp */
46
	vpn_l2tp_configure();
47
}
48

    
49
function vpn_ipsec_failover_configure() {
50
	global $config, $g;
51

    
52
	$sasyncd_text = "";
53

    
54
	if ($config['installedpackages']['sasyncd'] <> "")
55
		foreach ($config['installedpackages']['sasyncd']['config'] as $sasyncd) {
56
			$enabled = isset ($sasyncd['enable']);
57
			if (!$enabled)
58
				return;
59
			if ($sasyncd['peerip'] <> "")
60
				$sasyncd_text .= "peer {$sasyncd['peerip']}\n";
61
			if ($sasyncd['interface'])
62
				$sasyncd_text .= "carp interface {$sasyncd['interface']}\n";
63
			if ($sasyncd['sharedkey'] <> "")
64
				$sasyncd_text .= "sharedkey {$sasyncd['sharedkey']}\n";
65
			if ($sasyncd['mode'] <> "")
66
				$sasyncd_text .= "mode {$sasyncd['mode']}\n";
67
			if ($sasyncd['listenon'] <> "")
68
				$sasyncd_text .= "listen on {$sasyncd['listenon']}\n";
69
			if ($sasyncd['flushmodesync'] <> "")
70
				$sasyncd_text .= "flushmode sync {$sasyncd['flushmodesync']}\n";
71
		}
72

    
73
	$fd = fopen("{$g['varetc_path']}/sasyncd.conf", "w");
74
	fwrite($fd, $sasyncd_text);
75
	fclose($fd);
76
	chmod("{$g['varetc_path']}/sasyncd.conf", 0600);
77

    
78
	mwexec("killall sasyncd");
79

    
80
	/* launch sasyncd, oh wise one */
81
	mwexec_bg("/usr/local/sbin/sasyncd -d -v -v -v");
82
}
83

    
84
function find_last_gif_device() {
85
	$last_gif_found = -1;
86
	$regs = "";
87
	if (!($fp = popen("/sbin/ifconfig -l", "r")))
88
		return -1;
89
	$ifconfig_data = fread($fp, 4096);
90
	pclose($fp);
91
	$ifconfig_array = split(" ", $ifconfig_data);
92
	foreach ($ifconfig_array as $ifconfig) {
93
		ereg("gif(.)", $ifconfig, $regs);
94
		if ($regs[0] && $regs[0] > $last_gif_found) {
95
			$last_gif_found = $regs[1];
96
		}
97
	}
98
	return $last_gif_found;
99
}
100

    
101
function vpn_ipsec_configure($ipchg = false) {
102
	global $config, $g, $sa, $sn;
103

    
104
	mwexec("/sbin/ifconfig enc0 up");
105

    
106
	/* get the automatic /etc/ping_hosts.sh ready */
107
	unlink_if_exists("/var/db/ipsecpinghosts");
108
	touch("/var/db/ipsecpinghosts");
109

    
110
	if ($g['booting'] == true) {
111
		/* determine if we should load the via padlock module */
112
		$dmesg_boot = `/usr/bin/grep CPU {$g['varlog_path']}/dmesg.boot`;
113
		if (stristr($dmesg_boot, "ACE") == true) {
114
			//echo "Enabling [VIA Padlock] ...";
115
			//mwexec("/sbin/kldload padlock");
116
			//mwexec("/sbin/sysctl net.inet.ipsec.crypto_support=1");
117
			//mwexec("/sbin/setkey -F");
118
			//mwexec("/sbin/setkey -FP");
119
			//echo " done.\n";
120
		}
121
	}
122

    
123
	if(isset($config['ipsec']['preferredoldsa'])) {
124
		mwexec("/sbin/sysctl net.key.preferred_oldsa=0");
125
	} else {
126
		mwexec("/sbin/sysctl -w net.key.preferred_oldsa=-30");
127
	}
128

    
129
	$number_of_gifs = find_last_gif_device();
130
	for ($x = 0; $x < $number_of_gifs; $x++) {
131
		mwexec("/sbin/ifconfig gif" . $x . " delete");
132
	}
133

    
134
	$curwanip = get_current_wan_address();
135

    
136
	$syscfg = $config['system'];
137
	$ipseccfg = $config['ipsec'];
138
	$lancfg = $config['interfaces']['lan'];
139
	$lanip = $lancfg['ipaddr'];
140
	$lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']);
141
	$lansn = $lancfg['subnet'];
142

    
143

    
144
	if (!isset($ipseccfg['enable'])) {
145
		mwexec("/sbin/ifconfig enc0 down");
146
		mwexec("/sbin/ifconfig enc0 destroy");
147

    
148
		/* kill racoon */
149
		mwexec("/usr/bin/killall racoon");
150
		killbypid("{$g['varrun_path']}/dnswatch-ipsec.pid");
151
		
152
		/* wait for racoon process to die */
153
		sleep(2);
154

    
155
		/* send a SIGKILL to be sure */
156
		sigkillbypid("{$g['varrun_path']}/racoon.pid", "KILL");
157

    
158
		/* flush SPD and SAD */
159
		mwexec("/sbin/setkey -FP");
160
		mwexec("/sbin/setkey -F");
161

    
162
		return true;
163
	}
164

    
165
	if ($g['booting']) {
166
		echo "Configuring IPsec VPN... ";
167
	}
168

    
169
	if (isset ($ipseccfg['enable'])) {
170
		/* fastforwarding is not compatible with ipsec tunnels */
171
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=0");
172

    
173
		if (!$curwanip) {
174
			/* IP address not configured yet, exit */
175
			if ($g['booting'])
176
				echo "done\n";
177
			return 0;
178
		}
179

    
180
		if ((is_array($ipseccfg['tunnel']) && count($ipseccfg['tunnel'])) ||
181
		  isset ($ipseccfg['mobileclients']['enable'])) {
182
		  
183
			$dnswatch_list = array();
184
			$rgmap = array();
185
		  
186
			if (is_array($ipseccfg['tunnel']) && count($ipseccfg['tunnel'])) {
187
				/* generate spd.conf */
188
				$fd = fopen("{$g['varetc_path']}/spd.conf", "w");
189
				if (!$fd) {
190
					printf("Error: cannot open spd.conf in vpn_ipsec_configure().\n");
191
					return 1;
192
				}
193

    
194
				$spdconf = "";
195

    
196
				$spdconf .= "spdadd {$lansa}/{$lansn} {$lanip}/32 any -P in none;\n";
197
				$spdconf .= "spdadd {$lanip}/32 {$lansa}/{$lansn} any -P out none;\n";
198

    
199
				foreach ($ipseccfg['tunnel'] as $tunnel) {
200
					if (isset ($tunnel['disabled']))
201
						continue;
202

    
203
				   /* see if this tunnel has a hostname for the remote-gateway, and if so,
204
				      try to resolve it now and add it to the list for dnswatch */
205
				   if (!is_ipaddr($tunnel['remote-gateway'])) {
206
				           $dnswatch_list[] = $tunnel['remote-gateway'];
207
				           $rgip = resolve_retry($tunnel['remote-gateway']);
208
				
209
				           if (!$rgip)
210
				                   continue;
211
				
212
				   } else {
213
				           $rgip = $tunnel['remote-gateway'];
214
				   }
215
				   $rgmap[$tunnel['remote-gateway']] = $rgip;
216

    
217
					$ep = vpn_endpoint_determine($tunnel, $curwanip);
218
					if (!$ep)
219
						continue;
220

    
221
					vpn_localnet_determine($tunnel['local-subnet'], $sa, $sn);
222

    
223
					if (is_domain($tunnel['remote-gateway'])) {
224
						$tmp = gethostbyname($tunnel['remote-gateway']);
225
						if ($tmp)
226
							$tunnel['remote-gateway'] = $tmp;
227
					}
228

    
229
					/* add entry to host pinger */
230
					if ($tunnel['pinghost']) {
231
						$pfd = fopen("/var/db/ipsecpinghosts", "a");
232
						$iflist = array("lan" => "lan", "wan" => "wan");
233
			          	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
234
			          		$iflist['opt' . $i] = "opt{$i}";
235
			            foreach ($iflist as $ifent => $ifname) {
236
			            	$interface_ip = find_interface_ip($config['interfaces'][$ifname]['if']);
237
			            	if (ip_in_subnet($interface_ip, $sa . "/" . $sn))
238
			                	$srcip = find_interface_ip($config['interfaces'][$ifname]['if']);
239
			            }
240
						$dstip = $tunnel['pinghost'];
241
						fwrite($pfd, "$srcip|$dstip|3\n");
242
						fclose($pfd);
243
					}
244

    
245
					if (isset ($tunnel['creategif'])) {
246
						$number_of_gifs = find_last_gif_device();
247
						$number_of_gifs++;
248
						$curwanip = get_current_wan_address();
249
						if ($config['installedpackages']['sasyncd']['config'] <> "")
250
							foreach ($config['installedpackages']['sasyncd']['config'] as $sasyncd) {
251
								if ($sasyncd['ip'] <> "")
252
									$curwanip = $sasyncd['ip'];
253
							}
254
						mwexec("/sbin/ifconfig gif" . $number_of_gifs . " tunnel" . $curwanip . " " . $tunnel['remote-gateway']);
255
						mwexec("/sbin/ifconfig gif" . $number_of_gifs . " {$lansa}/{$lansn} {$lanip}/32");
256
					}
257

    
258
					$spdconf .= "spdadd {$sa}/{$sn} " .
259
					  "{$tunnel['remote-subnet']} any -P out ipsec " .
260
					  "{$tunnel['p2']['protocol']}/tunnel/{$ep}-" .
261
					  "{$rgip}/unique;\n";
262

    
263
					$spdconf .= "spdadd {$tunnel['remote-subnet']} " .
264
					  "{$sa}/{$sn} any -P in ipsec " .
265
					  "{$tunnel['p2']['protocol']}/tunnel/{$rgip}-" .
266
					  "{$ep}/unique;\n";
267
				}
268

    
269
				fwrite($fd, $spdconf);
270
				fclose($fd);
271
			}
272

    
273
			/* generate racoon.conf */
274
			$fd = fopen("{$g['varetc_path']}/racoon.conf", "w");
275
			if (!$fd) {
276
				printf("Error: cannot open racoon.conf in vpn_ipsec_configure().\n");
277
				return 1;
278
			}
279

    
280
			$racoonconf = "";
281

    
282
			$racoonconf .= "path pre_shared_key \"{$g['varetc_path']}/psk.txt\";\n\n";
283
			$racoonconf .= "path certificate  \"{$g['varetc_path']}\";\n\n";
284

    
285
			/* generate CA certificates files */
286
			$cacertnum = 0;
287
			if (is_array($ipseccfg['cacert']) && count($ipseccfg['cacert']))
288
				foreach ($ipseccfg['cacert'] as $cacert) {
289
					++ $cacertnum;
290
					if (isset ($cacert['cert'])) {
291
						$cert = base64_decode($cacert['cert']);
292
						$x509cert = openssl_x509_parse(openssl_x509_read($cert));
293
						if (is_array($x509cert) && isset ($x509cert['hash'])) {
294
							$fd1 = fopen("{$g['varetc_path']}/{$x509cert['hash']}.0", "w");
295
							if (!$fd1) {
296
								printf("Error: cannot open {$x509cert['hash']}.0 in vpn.\n");
297
								return 1;
298
							}
299
							chmod("{$g['varetc_path']}/{$x509cert['hash']}.0", 0600);
300
							fwrite($fd1, $cert);
301
							fclose($fd1);
302
						}
303
					}
304
				}
305

    
306
			$tunnelnumber = 0;
307
			if (is_array($ipseccfg['tunnel']) && count($ipseccfg['tunnel']))
308
				foreach ($ipseccfg['tunnel'] as $tunnel) {
309

    
310
					++ $tunnelnumber;
311

    
312
					if (isset ($tunnel['disabled']))
313
						continue;
314

    
315

    
316
					  $rgip = $rgmap[$tunnel['remote-gateway']];
317
					   if (!$rgip)
318
					           continue;
319

    
320
					$ep = vpn_endpoint_determine($tunnel, $curwanip);
321
					if (!$ep)
322
						continue;
323

    
324
					vpn_localnet_determine($tunnel['local-subnet'], $sa, $sn);
325

    
326
					if (isset ($tunnel['p1']['myident']['myaddress'])) {
327
						$myidentt = "address";
328
						$myident = $ep;
329
					} elseif (isset ($tunnel['p1']['myident']['address'])) {
330
						$myidentt = "address";
331
						$myident = $tunnel['p1']['myident']['address'];
332
					} elseif (isset ($tunnel['p1']['myident']['fqdn'])) {
333
						$myidentt = "fqdn";
334
						$myident = $tunnel['p1']['myident']['fqdn'];
335
					} elseif (isset ($tunnel['p1']['myident']['ufqdn'])) {
336
						$myidentt = "user_fqdn";
337
						$myident = $tunnel['p1']['myident']['ufqdn'];
338
					} else if (isset($tunnel['p1']['myident']['asn1dn'])) {
339
						$myidentt = "asn1dn";
340
						$myident = $tunnel['p1']['myident']['asn1dn'];					
341
					} else if (isset($tunnel['p1']['myident']['asn1dn'])) {
342
						$myidentt = "asn1dn";
343
						$myident = $tunnel['p1']['myident']['asn1dn'];						
344
					} elseif (isset ($tunnel['p1']['myident']['dyn_dns'])) {
345
						$myidentt = "dyn_dns";
346
						$myident = gethostbyname($tunnel['p1']['myident']['dyn_dns']);
347
					}
348

    
349
					if (!($myidentt == "asn1dn" && $myident == "")) {
350
						$myident = " \"{$myident}\"";
351
					}
352

    
353
					$nattline = '';
354
					if (isset($tunnel['natt'])) {
355
						$nattline = "nat_traversal on;";
356
					}
357

    
358
					if (isset ($tunnel['p1']['authentication_method'])) {
359
						$authmethod = $tunnel['p1']['authentication_method'];
360
					} else {
361
						$authmethod = 'pre_shared_key';
362
					}
363

    
364
					$certline = '';
365

    
366
					if ($authmethod == 'rsasig') {
367
						if ($tunnel['p1']['cert'] && $tunnel['p1']['private-key']) {
368
							$cert = base64_decode($tunnel['p1']['cert']);
369
							$private_key = base64_decode($tunnel['p1']['private-key']);
370
						} else {
371
							/* null certificate/key */
372
							$cert = '';
373
							$private_key = '';
374
						}
375

    
376
						if ($tunnel['p1']['peercert'])
377
							$peercert = base64_decode($tunnel['p1']['peercert']);
378
						else
379
							$peercert = '';
380

    
381
						$fd1 = fopen("{$g['varetc_path']}/server{$tunnelnumber}-signed.pem", "w");
382
						if (!$fd1) {
383
							printf("Error: cannot open server{$tunnelnumber}-signed.pem in vpn.\n");
384
							return 1;
385
						}
386
						chmod("{$g['varetc_path']}/server{$tunnelnumber}-signed.pem", 0600);
387
						fwrite($fd1, $cert);
388
						fclose($fd1);
389

    
390
						$fd1 = fopen("{$g['varetc_path']}/server{$tunnelnumber}-key.pem", "w");
391
						if (!$fd1) {
392
							printf("Error: cannot open server{$tunnelnumber}-key.pem in vpn.\n");
393
							return 1;
394
						}
395
						chmod("{$g['varetc_path']}/server{$tunnelnumber}-key.pem", 0600);
396
						fwrite($fd1, $private_key);
397
						fclose($fd1);
398

    
399
						$certline = "certificate_type x509 \"server{$tunnelnumber}-signed.pem\" \"server{$tunnelnumber}-key.pem\";";
400

    
401
						if ($peercert != '') {
402
							$fd1 = fopen("{$g['varetc_path']}/peer{$tunnelnumber}-signed.pem", "w");
403
							if (!$fd1) {
404
								printf("Error: cannot open server{$tunnelnumber}-signed.pem in vpn.\n");
405
								return 1;
406
							}
407
							chmod("{$g['varetc_path']}/peer{$tunnelnumber}-signed.pem", 0600);
408
							fwrite($fd1, $peercert);
409
							fclose($fd1);
410
							$certline .=<<<EOD
411

    
412
	peers_certfile "peer{$tunnelnumber}-signed.pem";
413
EOD;
414
						}
415
					}
416
					$myidentifier = $myidentt;
417
					if (!empty($myident)) 
418
						$myidentifier .= ' "' . $myident . '"';					
419
					$racoonconf .=<<<EOD
420
remote {$tunnel['remote-gateway']} {
421
	exchange_mode {$tunnel['p1']['mode']};
422
	my_identifier {$myidentt}{$myident};
423
	{$nattline}
424
	{$certline}
425
	peers_identifier address {$rgip};
426
	initial_contact on;
427
	dpd_delay 120;                   # DPD poll every 120 seconds
428
	ike_frag on;
429
	support_proxy on;
430
	proposal_check obey;
431

    
432
	proposal \{
433
		encryption_algorithm {$tunnel['p1']['encryption-algorithm']};
434
		hash_algorithm {$tunnel['p1']['hash-algorithm']};
435
		authentication_method {$authmethod};
436
		dh_group {$tunnel['p1']['dhgroup']};
437

    
438
EOD;
439
					if ($tunnel['p1']['lifetime'])
440
						$racoonconf .= "		lifetime time {$tunnel['p1']['lifetime']} secs;\n";
441

    
442
					$racoonconf .= "	}\n";
443

    
444
					if ($tunnel['p1']['lifetime'])
445
						$racoonconf .= "	lifetime time {$tunnel['p1']['lifetime']} secs;\n";
446

    
447
					$racoonconf .= "}\n\n";
448

    
449
					$p2ealgos = join(",", $tunnel['p2']['encryption-algorithm-option']);
450
					$p2halgos = join(",", $tunnel['p2']['hash-algorithm-option']);
451

    
452
					$racoonconf .=<<<EOD
453
sainfo address {$sa}/{$sn} any address {$tunnel['remote-subnet']} any \{
454
	encryption_algorithm {$p2ealgos};
455
	authentication_algorithm {$p2halgos};
456
	compression_algorithm deflate;
457

    
458
EOD;
459

    
460
					if ($tunnel['p2']['pfsgroup'])
461
						$racoonconf .= "	pfs_group {$tunnel['p2']['pfsgroup']};\n";
462

    
463
					if ($tunnel['p2']['lifetime'])
464
						$racoonconf .= "	lifetime time {$tunnel['p2']['lifetime']} secs;\n";
465

    
466
					$racoonconf .= "}\n\n";
467
				}
468

    
469
			/* mobile clients? */
470
			if (isset ($ipseccfg['mobileclients']['enable'])) {
471

    
472
				$tunnel = $ipseccfg['mobileclients'];
473

    
474
				if (isset ($tunnel['p1']['myident']['myaddress'])) {
475
					$myidentt = "address";
476
					$myident = $curwanip;
477
				} else
478
					if (isset ($tunnel['p1']['myident']['address'])) {
479
						$myidentt = "address";
480
						$myident = $tunnel['p1']['myident']['address'];
481
					} else
482
						if (isset ($tunnel['p1']['myident']['fqdn'])) {
483
							$myidentt = "fqdn";
484
							$myident = $tunnel['p1']['myident']['fqdn'];
485
						} else
486
							if (isset ($tunnel['p1']['myident']['ufqdn'])) {
487
								$myidentt = "user_fqdn";
488
								$myident = $tunnel['p1']['myident']['ufqdn'];
489
							}
490

    
491
				if (isset ($tunnel['p1']['authentication_method'])) {
492
					$authmethod = $tunnel['p1']['authentication_method'];
493
				} else {
494
					$authmethod = 'pre_shared_key';
495
				}
496

    
497
				$certline = '';
498
				if ($authmethod == 'rsasig') {
499
					if ($tunnel['p1']['cert'] && $tunnel['p1']['private-key']) {
500
						$cert = base64_decode($tunnel['p1']['cert']);
501
						$private_key = base64_decode($tunnel['p1']['private-key']);
502
					} else {
503
						/* null certificate/key */
504
						$cert = '';
505
						$private_key = '';
506
					}
507

    
508
					if ($tunnel['p1']['peercert'])
509
						$peercert = base64_decode($tunnel['p1']['peercert']);
510
					else
511
						$peercert = '';
512

    
513
					$fd1 = fopen("{$g['varetc_path']}/server-mobile{$tunnelnumber}-signed.pem", "w");
514
					if (!$fd1) {
515
						printf("Error: cannot open server-mobile{$tunnelnumber}-signed.pem in vpn.\n");
516
						return 1;
517
					}
518
					chmod("{$g['varetc_path']}/server-mobile{$tunnelnumber}-signed.pem", 0600);
519
					fwrite($fd1, $cert);
520
					fclose($fd1);
521

    
522
					$fd1 = fopen("{$g['varetc_path']}/server-mobile{$tunnelnumber}-key.pem", "w");
523
					if (!$fd1) {
524
						printf("Error: cannot open server-mobile{$tunnelnumber}-key.pem in vpn.\n");
525
						return 1;
526
					}
527
					chmod("{$g['varetc_path']}/server-mobile{$tunnelnumber}-key.pem", 0600);
528
					fwrite($fd1, $private_key);
529
					fclose($fd1);
530

    
531
					$certline = "certificate_type x509 \"server-mobile{$tunnelnumber}-signed.pem\" \"server-mobile{$tunnelnumber}-key.pem\";";
532
				}
533
				$racoonconf .=<<<EOD
534
remote anonymous \{
535
	exchange_mode {$tunnel['p1']['mode']};
536
	my_identifier {$myidentt}{$myident};
537
	{$certline}
538
	initial_contact on;
539
	dpd_delay 120;                   # DPD poll every 120 seconds
540
	ike_frag on;
541
	passive on;
542
	generate_policy on;
543
	support_proxy on;
544
	proposal_check obey;
545

    
546
	proposal \{
547
		encryption_algorithm {$tunnel['p1']['encryption-algorithm']};
548
		hash_algorithm {$tunnel['p1']['hash-algorithm']};
549
		authentication_method {$authmethod};
550
		dh_group {$tunnel['p1']['dhgroup']};
551

    
552
EOD;
553
				if ($tunnel['p1']['lifetime'])
554
					$racoonconf .= "		lifetime time {$tunnel['p1']['lifetime']} secs;\n";
555

    
556
				$racoonconf .= "	}\n";
557

    
558
				if ($tunnel['p1']['lifetime'])
559
					$racoonconf .= "	lifetime time {$tunnel['p1']['lifetime']} secs;\n";
560

    
561
				$racoonconf .= "}\n\n";
562

    
563
				$p2ealgos = join(",", $tunnel['p2']['encryption-algorithm-option']);
564
				$p2halgos = join(",", $tunnel['p2']['hash-algorithm-option']);
565

    
566
				$racoonconf .=<<<EOD
567
sainfo anonymous \{
568
	encryption_algorithm {$p2ealgos};
569
	authentication_algorithm {$p2halgos};
570
	compression_algorithm deflate;
571

    
572
EOD;
573

    
574
				if ($tunnel['p2']['pfsgroup'])
575
					$racoonconf .= "	pfs_group {$tunnel['p2']['pfsgroup']};\n";
576

    
577
				if ($tunnel['p2']['lifetime'])
578
					$racoonconf .= "	lifetime time {$tunnel['p2']['lifetime']} secs;\n";
579

    
580
				$racoonconf .= "}\n\n";
581
			}
582

    
583
			fwrite($fd, $racoonconf);
584
			fclose($fd);
585

    
586
			/* generate psk.txt */
587
			$fd = fopen("{$g['varetc_path']}/psk.txt", "w");
588
			if (!$fd) {
589
				printf("Error: cannot open psk.txt in vpn_ipsec_configure().\n");
590
				return 1;
591
			}
592

    
593
			$pskconf = "";
594

    
595
			if (is_array($ipseccfg['tunnel'])) {
596
				foreach ($ipseccfg['tunnel'] as $tunnel) {
597
					if (isset ($tunnel['disabled']))
598
						continue;
599
                   $rgip = $rgmap[$tunnel['remote-gateway']];
600
                   if (!$rgip)
601
                           continue;
602
                   $pskconf .= "{$rgip}     {$tunnel['p1']['pre-shared-key']}\n";
603
				}
604
			}
605

    
606
			/* add PSKs for mobile clients */
607
			if (is_array($ipseccfg['mobilekey'])) {
608
				foreach ($ipseccfg['mobilekey'] as $key) {
609
					$pskconf .= "{$key['ident']}	{$key['pre-shared-key']}\n";
610
				}
611
			}
612

    
613
			fwrite($fd, $pskconf);
614
			fclose($fd);
615
			chmod("{$g['varetc_path']}/psk.txt", 0600);
616

    
617

    
618
			if(is_process_running("racoon")) {
619
				/* We are already online, reload */
620
				mwexec("/usr/bin/killall -HUP racoon");
621
				/* flush SPD entries */
622
				mwexec("/sbin/setkey -FP");
623
				mwexec("/sbin/setkey -F");
624
				/* load SPD */
625
				mwexec("/sbin/setkey -f {$g['varetc_path']}/spd.conf");
626
				sleep(1);
627
				/* We are already online, reload */
628
				mwexec("/usr/bin/killall -HUP racoon");
629
				sleep(1);
630
				mwexec("/usr/bin/killall -HUP racoon");
631
			} else {
632
				/* start racoon */
633
				mwexec("/usr/local/sbin/racoon -f {$g['varetc_path']}/racoon.conf");
634
				/* flush SA + SPD  entries*/
635
				mwexec("/sbin/setkey -FP");
636
				mwexec("/sbin/setkey -F");
637
				/* load SPD */
638
				mwexec("/sbin/setkey -f {$g['varetc_path']}/spd.conf");
639
				sleep(1);
640
				/* We are already online, reload */
641
				mwexec("/usr/bin/killall -HUP racoon");
642
				sleep(1);
643
				mwexec("/usr/bin/killall -HUP racoon");
644

    
645
				/* start dnswatch, if necessary */
646
				if (count($dnswatch_list) > 0) {
647
					$interval = 60;
648
					if ($ipseccfg['dns-interval']) {
649
						$interval = $ipseccfg['dns-interval'];
650
					}
651
			
652
					$hostnames = "";
653
					foreach ($dnswatch_list as $dns) {
654
						$hostnames .= " " . escapeshellarg($dns);
655
					}
656
					mwexec("/usr/local/bin/dnswatch {$g['varrun_path']}/dnswatch-ipsec.pid $interval " .
657
					escapeshellarg("/etc/rc.newipsecdns") . $hostnames);
658
				}
659
			}
660

    
661
			if (is_array($ipseccfg['tunnel'])) {
662
				foreach ($ipseccfg['tunnel'] as $tunnel) {
663
					if (isset ($tunnel['auto'])) {
664
						$remotehost = substr($tunnel['remote-subnet'], 0, strpos($tunnel['remote-subnet'], "/"));
665
						$srchost = vpn_endpoint_determine($tunnel, $curwanip);
666
						if ($srchost)
667
							mwexec_bg("/sbin/ping -c 10 -S {$srchost} {$remotehost}");
668
					}
669
				}
670
			}
671
		}
672
	}
673

    
674
	vpn_ipsec_failover_configure();
675

    
676
	if (!$g['booting']) {
677
		/* reload the filter */
678
		touch("{$g["tmp_path"]}/filter_dirty");
679
	}
680

    
681
	if ($g['booting'])
682
		echo "done\n";
683

    
684
	return 0;
685
}
686

    
687
function vpn_pptpd_configure() {
688
	global $config, $g;
689

    
690
	$syscfg = $config['system'];
691
	$pptpdcfg = $config['pptpd'];
692

    
693
	$starting_ng = get_number_of_wan_netgraph_interfaces_needed();
694

    
695
	if ($g['booting']) {
696
		if (!$pptpdcfg['mode'] || ($pptpdcfg['mode'] == "off"))
697
			return 0;
698

    
699
		echo "Configuring PPTP VPN service... ";
700
	} else {
701
		/* kill mpd */
702
		killbypid("{$g['varrun_path']}/mpd-vpn.pid");
703

    
704
		/* wait for process to die */
705
		sleep(3);
706

    
707
		if (is_process_running("mpd -b")) {
708
			killbypid("{$g['varrun_path']}/mpd-vpn.pid");
709
			log_error("Could not kill mpd within 3 seconds.   Trying again.");
710
		}
711

    
712
		/* remove mpd.conf, if it exists */
713
		unlink_if_exists("{$g['varetc_path']}/mpd-vpn/mpd.conf");
714
		unlink_if_exists("{$g['varetc_path']}/mpd-vpn/mpd.links");
715
		unlink_if_exists("{$g['varetc_path']}/mpd-vpn/mpd.secret");
716
	}
717

    
718
	/* make sure mpd-vpn directory exists */
719
	if (!file_exists("{$g['varetc_path']}/mpd-vpn"))
720
		mkdir("{$g['varetc_path']}/mpd-vpn");
721

    
722
	switch ($pptpdcfg['mode']) {
723
		case 'server' :
724
			/* write mpd.conf */
725
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.conf", "w");
726
			if (!$fd) {
727
				printf("Error: cannot open mpd.conf in vpn_pptpd_configure().\n");
728
				return 1;
729
			}
730

    
731
			$mpdconf =<<<EOD
732
pptpd:
733

    
734
EOD;
735

    
736
			for ($i = 0; $i < $g['n_pptp_units']; $i++) {
737
				$mpdconf .= "	load pt{$i}\n";
738
			}
739

    
740
			for ($i = 0; $i < $g['n_pptp_units']; $i++) {
741

    
742
				$clientip = long2ip(ip2long($pptpdcfg['remoteip']) + $i);
743
				$ngif = "ng" . ($i + $starting_ng);
744

    
745
				if(isset($pptpdcfg['radius']['radiusissueips']) && isset($pptpdcfg['radius']['enable'])) {
746
					$isssue_ip_type = "set ipcp ranges {$pptpdcfg['localip']}/32 0.0.0.0/0";
747
					$isssue_ip_type .="\n\tset ipcp yes radius-ip";
748
				} else {
749
					$isssue_ip_type = "set ipcp ranges {$pptpdcfg['localip']}/32 {$clientip}/32";
750
				}
751

    
752
				$mpdconf .=<<<EOD
753

    
754
pt{$i}:
755
	new -i {$ngif} pt{$i} pt{$i}
756
	{$isssue_ip_type}
757
	load pts
758

    
759
EOD;
760
			}
761

    
762
			$mpdconf .=<<<EOD
763

    
764
pts:
765
	set iface disable on-demand
766
	set iface enable proxy-arp
767
	set iface enable tcpmssfix
768
	set iface idle 1800
769
	set iface up-script /usr/local/sbin/vpn-linkup
770
	set iface down-script /usr/local/sbin/vpn-linkdown
771
	set bundle enable multilink
772
	set bundle enable crypt-reqd
773
	set link yes acfcomp protocomp
774
	set link no pap chap
775
	set link enable chap-msv2
776
	set link mtu 1460
777
	set link keep-alive 10 60
778
	set ipcp yes vjcomp
779
	set bundle enable compression
780
	set ccp yes mppc
781
	set ccp yes mpp-e128
782
	set ccp yes mpp-stateless
783

    
784
EOD;
785

    
786
			if (!isset ($pptpdcfg['req128'])) {
787
				$mpdconf .=<<<EOD
788
	set ccp yes mpp-e40
789
	set ccp yes mpp-e56
790

    
791
EOD;
792
			}
793

    
794
			if  (isset($pptpdcfg["wins"]))
795
				$mpdconf  .=  "	set ipcp nbns {$pptpdcfg['wins']}\n";
796
			if (is_array($pptpdcfg['dnsserver']) && ($pptpdcfg['dnsserver'][0])) {
797
				$mpdconf .= "	set ipcp dns " . join(" ", $pptpdcfg['dnsserver']) . "\n";
798
			} else
799
				if (isset ($config['dnsmasq']['enable'])) {
800
					$mpdconf .= "	set ipcp dns " . $config['interfaces']['lan']['ipaddr'];
801
					if ($syscfg['dnsserver'][0])
802
						$mpdconf .= " " . $syscfg['dnsserver'][0];
803
					$mpdconf .= "\n";
804
				} else
805
					if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
806
						$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
807
					}
808

    
809
			if (isset ($pptpdcfg['radius']['enable'])) {
810
				$authport = isset($pptpdcfg['radius']['port']) ? $pptpdcfg['radius']['port'] : 1812;
811
				$acctport = $authport + 1;
812
				$mpdconf .=<<<EOD
813
	set radius server {$pptpdcfg['radius']['server']} "{$pptpdcfg['radius']['secret']}" {$authport} {$acctport}
814
	set radius retries 3
815
	set radius timeout 10
816
	set bundle enable radius-auth
817
	set bundle disable radius-fallback
818

    
819
EOD;
820

    
821
				if (isset ($pptpdcfg['radius']['accounting'])) {
822
					$mpdconf .=<<<EOD
823
	set bundle enable radius-acct
824
	set radius acct-update 300
825

    
826
EOD;
827
				}
828
			}
829

    
830
			fwrite($fd, $mpdconf);
831
			fclose($fd);
832

    
833
			/* write mpd.links */
834
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.links", "w");
835
			if (!$fd) {
836
				printf("Error: cannot open mpd.links in vpn_pptpd_configure().\n");
837
				return 1;
838
			}
839

    
840
			$mpdlinks = "";
841

    
842
			for ($i = 0; $i < $g['n_pptp_units']; $i++) {
843
				$mpdlinks .=<<<EOD
844

    
845
pt{$i}:
846
	set link type pptp
847
	set pptp enable incoming
848
	set pptp disable originate
849
	set pptp disable windowing
850
	set pptp self 127.0.0.1
851

    
852
EOD;
853
			}
854

    
855
			fwrite($fd, $mpdlinks);
856
			fclose($fd);
857

    
858
			/* write mpd.secret */
859
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.secret", "w");
860
			if (!$fd) {
861
				printf("Error: cannot open mpd.secret in vpn_pptpd_configure().\n");
862
				return 1;
863
			}
864

    
865
			$mpdsecret = "";
866

    
867
			if (is_array($pptpdcfg['user'])) {
868
				foreach ($pptpdcfg['user'] as $user)
869
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
870
			}
871

    
872
			fwrite($fd, $mpdsecret);
873
			fclose($fd);
874
			chmod("{$g['varetc_path']}/mpd-vpn/mpd.secret", 0600);
875

    
876
			/* fire up mpd */
877
			mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']}/mpd-vpn -p {$g['varrun_path']}/mpd-vpn.pid -f mpd.conf pptpd");
878

    
879
			break;
880

    
881
		case 'redir' :
882
			break;
883
	}
884

    
885
	if (!$g['booting']) {
886
		/* reload the filter */
887
		filter_configure();
888
	}
889

    
890
	if ($g['booting'])
891
		echo "done\n";
892

    
893
	return 0;
894
}
895

    
896
function vpn_localnet_determine($adr, & $sa, & $sn) {
897
	global $config, $g;
898

    
899
	if (isset ($adr)) {
900
		if ($adr['network']) {
901
			switch ($adr['network']) {
902
				case 'lan' :
903
					$sn = $config['interfaces']['lan']['subnet'];
904
					$sa = gen_subnet($config['interfaces']['lan']['ipaddr'], $sn);
905
					break;
906
			}
907
		} else
908
			if ($adr['address']) {
909
				list ($sa, $sn) = explode("/", $adr['address']);
910
				if (is_null($sn))
911
					$sn = 32;
912
			}
913
	} else {
914
		$sn = $config['interfaces']['lan']['subnet'];
915
		$sa = gen_subnet($config['interfaces']['lan']['ipaddr'], $sn);
916
	}
917
}
918

    
919
function vpn_endpoint_determine($tunnel, $curwanip) {
920

    
921
	global $g, $config;
922

    
923
	if ((!$tunnel['interface']) || ($tunnel['interface'] == "wan")) {
924
		if ($curwanip)
925
			return $curwanip;
926
		else
927
			return null;
928
	} elseif ($tunnel['interface'] == "lan") {
929
		return $config['interfaces']['lan']['ipaddr'];
930
	} else {
931
		$oc = $config['interfaces'][$tunnel['interface']];
932
		/* carp ips, etc */
933
		$ip = find_interface_ip($tunnel['interface']);
934
		if($ip) 
935
			return $ip;
936
			
937
		if (isset ($oc['enable']) && $oc['if']) {
938
			return $oc['ipaddr'];
939
		}
940
	}
941

    
942
	return null;
943
}
944

    
945
function vpn_pppoe_configure() {
946
	global $config, $g;
947

    
948
	$syscfg = $config['system'];
949
	$pppoecfg = $config['pppoe'];
950

    
951
	$starting_ng = get_number_of_wan_netgraph_interfaces_needed();
952

    
953
	/* create directory if it does not exist */
954
	if (!is_dir("{$g['varetc_path']}/mpd-vpn"))
955
		mkdir("{$g['varetc_path']}/mpd-vpn");
956

    
957
	if ($g['booting']) {
958
		if (!$pppoecfg['mode'] || ($pppoecfg['mode'] == "off"))
959
			return 0;
960

    
961
		echo "Configuring PPPoE VPN service... ";
962
	} else {
963
		/* kill mpd */
964
		killbypid("{$g['varrun_path']}/mpd-vpn.pid");
965

    
966
		/* wait for process to die */
967
		sleep(2);
968

    
969
	}
970

    
971
	/* make sure mpd-vpn directory exists */
972
	if (!file_exists("{$g['varetc_path']}/mpd-vpn"))
973
		mkdir("{$g['varetc_path']}/mpd-vpn");
974

    
975
	switch ($pppoecfg['mode']) {
976

    
977
		case 'server' :
978

    
979
			$pppoe_interface = filter_translate_type_to_real_interface($pppoecfg['interface']);
980

    
981
			if ($pppoecfg['paporchap'] == "chap")
982
				$paporchap = "set link enable chap";
983
			else
984
				$paporchap = "set link enable pap";
985

    
986
			/* write mpd.conf */
987
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.conf", "a");
988
			if (!$fd) {
989
				printf("Error: cannot open mpd.conf in vpn_pppoe_configure().\n");
990
				return 1;
991
			}
992
			$mpdconf = "\n\n";
993
			$mpdconf .=<<<EOD
994
pppoe:
995

    
996
EOD;
997

    
998
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
999
				$mpdconf .= "	load pppoe{$i}\n";
1000
			}
1001

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

    
1004
				$clientip = long2ip(ip2long($pppoecfg['remoteip']) + $i);
1005
				$ngif = "ng" . ($i + $starting_ng);
1006

    
1007
				if (isset ($pppoecfg['radius']['radiusissueips']) && isset ($pppoecfg['radius']['enable'])) {
1008
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 0.0.0.0/0";
1009
					$isssue_ip_type .= "\n\tset ipcp yes radius-ip";
1010
				} else {
1011
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 {$clientip}/32";
1012
				}
1013

    
1014
				$mpdconf .=<<<EOD
1015

    
1016
pppoe{$i}:
1017
	new -i {$ngif} pppoe{$i} pppoe{$i}
1018
	{$isssue_ip_type}
1019
	load pppoe_standart
1020

    
1021
EOD;
1022
			}
1023

    
1024
			$mpdconf .=<<<EOD
1025

    
1026
pppoe_standart:
1027
	set link type pppoe
1028
	set pppoe iface {$pppoe_interface}
1029
	set pppoe service "*"
1030
	set pppoe disable originate
1031
	set pppoe enable incoming
1032
	set bundle no multilink
1033
	set bundle enable compression
1034
	set bundle max-logins 1
1035
	set iface idle 0
1036
	set iface disable on-demand
1037
	set iface disable proxy-arp
1038
	set iface enable tcpmssfix
1039
	set iface mtu 1500
1040
	set link no pap chap
1041
	{$paporchap}
1042
	set link keep-alive 60 180
1043
	set ipcp yes vjcomp
1044
	set ipcp no vjcomp
1045
	set link max-redial -1
1046
	set link mtu 1492
1047
	set link mru 1492
1048
	set ccp yes mpp-e40
1049
	set ccp yes mpp-e128
1050
	set ccp yes mpp-stateless
1051
	set link latency 1
1052
	#set ipcp dns 10.10.1.3
1053
	#set bundle accept encryption
1054

    
1055
EOD;
1056

    
1057
			if (isset ($config['dnsmasq']['enable'])) {
1058
				$mpdconf .= "	set ipcp dns " . $config['interfaces']['lan']['ipaddr'];
1059
				if ($syscfg['dnsserver'][0])
1060
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1061
				$mpdconf .= "\n";
1062
			} else
1063
				if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1064
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1065
				}
1066

    
1067
			if (isset ($pppoecfg['radius']['enable'])) {
1068
				$mpdconf .=<<<EOD
1069
	set radius server {$pppoecfg['radius']['server']} "{$pppoecfg['radius']['secret']}"
1070
	set ipcp radius-ip
1071
	set radius retries 3
1072
	set radius timeout 10
1073
	set bundle enable radius-auth
1074
	set bundle disable radius-fallback
1075

    
1076
EOD;
1077

    
1078
				if (isset ($pppoecfg['radius']['accounting'])) {
1079
					$mpdconf .=<<<EOD
1080
	set bundle enable radius-acct
1081

    
1082
EOD;
1083
				}
1084
			}
1085

    
1086
			fwrite($fd, $mpdconf);
1087
			fclose($fd);
1088

    
1089
			/* write mpd.links */
1090
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.links", "a");
1091
			if (!$fd) {
1092
				printf("Error: cannot open mpd.links in vpn_pppoe_configure().\n");
1093
				return 1;
1094
			}
1095

    
1096
			$mpdlinks = "";
1097

    
1098
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1099
				$mpdlinks .=<<<EOD
1100

    
1101
pppoe:
1102
	set link type pppoe
1103
	set pppoe iface {$pppoe_interface}
1104

    
1105
EOD;
1106
			}
1107

    
1108
			fwrite($fd, $mpdlinks);
1109
			fclose($fd);
1110

    
1111
			/* write mpd.secret */
1112
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.secret", "a");
1113
			if (!$fd) {
1114
				printf("Error: cannot open mpd.secret in vpn_pppoe_configure().\n");
1115
				return 1;
1116
			}
1117

    
1118
			$mpdsecret = "\n\n";
1119

    
1120
			if (is_array($pppoecfg['user'])) {
1121
				foreach ($pppoecfg['user'] as $user)
1122
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
1123
			}
1124

    
1125
			fwrite($fd, $mpdsecret);
1126
			fclose($fd);
1127
			chmod("{$g['varetc_path']}/mpd-vpn/mpd.secret", 0600);
1128

    
1129
			/* fire up mpd */
1130
			mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']}/mpd-vpn -p {$g['varrun_path']}/mpd-vpn.pid pppoe");
1131

    
1132
			break;
1133

    
1134
		case 'redir' :
1135
			break;
1136
	}
1137

    
1138
	touch("{$g["tmp_path"]}/filter_dirty");
1139

    
1140
	if ($g['booting'])
1141
		echo "done\n";
1142

    
1143
	return 0;
1144
}
1145

    
1146
function vpn_l2tp_configure() {
1147
	global $config, $g;
1148

    
1149
	$syscfg = $config['system'];
1150
	$l2tpcfg = $config['l2tp'];
1151

    
1152
	mwexec("/sbin/kldload /boot/kernel/ng_l2tp.ko");
1153

    
1154
	$starting_ng = get_number_of_wan_netgraph_interfaces_needed();
1155

    
1156
	/* create directory if it does not exist */
1157
	if (!is_dir("{$g['varetc_path']}/mpd-vpn"))
1158
		mkdir("{$g['varetc_path']}/mpd-vpn");
1159

    
1160
	if ($g['booting']) {
1161
		if (!$l2tpcfg['mode'] || ($l2tpcfg['mode'] == "off"))
1162
			return 0;
1163

    
1164
		echo "Configuring l2tp VPN service... ";
1165
	} else {
1166
		/* kill mpd */
1167
		killbypid("{$g['varrun_path']}/mpd-vpn.pid");
1168

    
1169
		/* wait for process to die */
1170
		sleep(2);
1171

    
1172
	}
1173

    
1174
	/* make sure mpd-vpn directory exists */
1175
	if (!file_exists("{$g['varetc_path']}/mpd-vpn"))
1176
		mkdir("{$g['varetc_path']}/mpd-vpn");
1177

    
1178
	switch ($l2tpcfg['mode']) {
1179

    
1180
		case 'server' :
1181

    
1182
			$l2tp_interface = filter_translate_type_to_real_interface($l2tpcfg['interface']);
1183

    
1184
			if ($l2tpcfg['paporchap'] == "chap")
1185
				$paporchap = "set link enable chap";
1186
			else
1187
				$paporchap = "set link enable pap";
1188

    
1189
			/* write mpd.conf */
1190
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.conf", "a");
1191
			if (!$fd) {
1192
				printf("Error: cannot open mpd.conf in vpn_l2tp_configure().\n");
1193
				return 1;
1194
			}
1195
			$mpdconf = "\n\n";
1196
			$mpdconf .=<<<EOD
1197
l2tp:
1198

    
1199
EOD;
1200

    
1201
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1202
				$mpdconf .= "	load l2tp{$i}\n";
1203
			}
1204

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

    
1207
				$clientip = long2ip(ip2long($l2tpcfg['remoteip']) + $i);
1208
				$ngif = "ng" . ($i + $starting_ng);
1209

    
1210
				if (isset ($l2tpcfg['radius']['radiusissueips']) && isset ($l2tpcfg['radius']['enable'])) {
1211
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 0.0.0.0/0";
1212
					$isssue_ip_type .= "\n\tset ipcp yes radius-ip";
1213
				} else {
1214
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 {$clientip}/32";
1215
				}
1216

    
1217
				$mpdconf .=<<<EOD
1218

    
1219
l2tp{$i}:
1220
	new -i {$ngif} l2tp{$i} l2tp{$i}
1221
	{$isssue_ip_type}
1222
	load l2tp_standard
1223

    
1224
EOD;
1225
			}
1226

    
1227
			$mpdconf .=<<<EOD
1228

    
1229
l2tp_standard:
1230
        set bundle disable multilink
1231
        set bundle enable compression
1232
        set bundle yes crypt-reqd
1233
        set ipcp yes vjcomp
1234
        # set ipcp ranges 131.188.69.161/32 131.188.69.170/28
1235
        set ccp yes mppc
1236
        set iface disable on-demand
1237
        set iface enable proxy-arp
1238
        set link yes acfcomp protocomp
1239
        set link no pap chap
1240
        set link enable chap
1241
        set link keep-alive 10 180
1242

    
1243
EOD;
1244

    
1245
			if (isset ($config['dnsmasq']['enable'])) {
1246
				$mpdconf .= "	set ipcp dns " . $config['interfaces']['lan']['ipaddr'];
1247
				if ($syscfg['dnsserver'][0])
1248
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1249
				$mpdconf .= "\n";
1250
			} else
1251
				if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1252
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1253
				}
1254

    
1255
			if (isset ($l2tpcfg['radius']['enable'])) {
1256
				$mpdconf .=<<<EOD
1257
	set radius server {$l2tpcfg['radius']['server']} "{$l2tpcfg['radius']['secret']}"
1258
	set ipcp radius-ip
1259
	set radius retries 3
1260
	set radius timeout 10
1261
	set bundle enable radius-auth
1262
	set bundle disable radius-fallback
1263

    
1264
EOD;
1265

    
1266
				if (isset ($l2tpcfg['radius']['accounting'])) {
1267
					$mpdconf .=<<<EOD
1268
	set bundle enable radius-acct
1269

    
1270
EOD;
1271
				}
1272
			}
1273

    
1274
			fwrite($fd, $mpdconf);
1275
			fclose($fd);
1276

    
1277
			/* write mpd.links */
1278
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.links", "a");
1279
			if (!$fd) {
1280
				printf("Error: cannot open mpd.links in vpn_l2tp_configure().\n");
1281
				return 1;
1282
			}
1283

    
1284
			$mpdlinks = "";
1285

    
1286
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1287
				$mpdlinks .=<<<EOD
1288

    
1289
l2tp:
1290
	set link type l2tp
1291
	set l2tp iface {$l2tp_interface}
1292

    
1293
EOD;
1294
			}
1295

    
1296
			fwrite($fd, $mpdlinks);
1297
			fclose($fd);
1298

    
1299
			/* write mpd.secret */
1300
			$fd = fopen("{$g['varetc_path']}/mpd-vpn/mpd.secret", "a");
1301
			if (!$fd) {
1302
				printf("Error: cannot open mpd.secret in vpn_l2tp_configure().\n");
1303
				return 1;
1304
			}
1305

    
1306
			$mpdsecret = "\n\n";
1307

    
1308
			if (is_array($l2tpcfg['user'])) {
1309
				foreach ($l2tpcfg['user'] as $user)
1310
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
1311
			}
1312

    
1313
			fwrite($fd, $mpdsecret);
1314
			fclose($fd);
1315
			chmod("{$g['varetc_path']}/mpd-vpn/mpd.secret", 0600);
1316

    
1317
			/* fire up mpd */
1318
			mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']}/mpd-vpn -p {$g['varrun_path']}/mpd-vpn.pid l2tp");
1319

    
1320
			break;
1321

    
1322
		case 'redir' :
1323
			break;
1324
	}
1325

    
1326
	touch("{$g["tmp_path"]}/filter_dirty");
1327

    
1328
	if ($g['booting'])
1329
		echo "done\n";
1330

    
1331
	return 0;
1332
}
1333

    
1334
/* Forcefully restart IPsec
1335
 * This is required for when dynamic interfaces reload
1336
 * For all other occasions the normal vpn_ipsec_configure()
1337
 * will gracefully reload the settings without restarting
1338
 */
1339
function vpn_ipsec_force_reload() {
1340
	global $config;
1341
	global $g;
1342

    
1343
	$ipseccfg = $config['ipsec'];
1344

    
1345
	/* kill racoon */
1346
	mwexec("/usr/bin/killall racoon");
1347

    
1348
	/* wait for process to die */
1349
	sleep(4);
1350

    
1351
	/* send a SIGKILL to be sure */
1352
	sigkillbypid("{$g['varrun_path']}/racoon.pid", "KILL");
1353

    
1354
	/* wait for flushing to finish */
1355
	sleep(1);
1356

    
1357
	/* if ipsec is enabled, start up again */
1358
	if (isset($ipseccfg['enable'])) {
1359
		log_error("Forcefully reloading IPsec racoon daemon");
1360
		vpn_ipsec_configure();
1361
	}
1362

    
1363
}
1364

    
1365
?>
(24-24/29)