Project

General

Profile

Download (18.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	interfaces.inc
4
	Copyright (C) 2004 Scott Ullrich
5
	All rights reserved.
6

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

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

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

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

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

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

    
36
function interfaces_loopback_configure() {
37
	global $config, $g;
38

    
39
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
40

    
41
	return 0;
42
}
43

    
44
function interfaces_vlan_configure() {
45
	global $config, $g;
46

    
47
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
48

    
49
		/* load the VLAN module */
50
		mwexec("/sbin/kldload if_vlan");
51

    
52
		/* devices with native VLAN support */
53
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
54

    
55
		/* devices with long frame support */
56
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
57

    
58
		$i = 0;
59

    
60
		foreach ($config['vlans']['vlan'] as $vlan) {
61

    
62
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
63
				escapeshellarg($vlan['tag']) . " vlandev " .
64
				escapeshellarg($vlan['if']);
65

    
66
			/* get driver name */
67
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
68
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
69
					break;
70
			}
71
			$drvname = substr($vlan['if'], 0, $j);
72

    
73
			if (in_array($drvname, $vlan_native_supp))
74
				$cmd .= " link0";
75
			else if (in_array($drvname, $vlan_long_supp))
76
				$cmd .= " mtu 1500";
77

    
78
			mwexec($cmd);
79

    
80
			/* make sure the parent interface is up */
81
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
82

    
83
			$i++;
84
		}
85
	}
86

    
87
	return 0;
88
}
89

    
90
function interfaces_lan_configure() {
91
	global $config, $g;
92

    
93
	if ($g['booting'])
94
		echo "Configuring LAN interface... ";
95

    
96
	$lancfg = $config['interfaces']['lan'];
97

    
98
	/* wireless configuration? */
99
	if (is_array($lancfg['wireless']))
100
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
101

    
102
	/* MAC spoofing? */
103
	if ($lancfg['spoofmac'])
104
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
105
			" link " . escapeshellarg($lancfg['spoofmac']));
106

    
107
	/* media */
108
	if ($lancfg['media'] || $lancfg['mediaopt']) {
109
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
110
		if ($lancfg['media'])
111
			$cmd .= " media " . escapeshellarg($lancfg['media']);
112
		if ($lancfg['mediaopt'])
113
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
114
		mwexec($cmd);
115
	}
116

    
117
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
118
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
119

    
120
	if (!$g['booting']) {
121
		/* make new hosts file */
122
		system_hosts_generate();
123

    
124
		/* reconfigure static routes (kernel may have deleted them) */
125
		system_routing_configure();
126

    
127
		/* reload ipfilter (address may have changed) */
128
		filter_configure();
129

    
130
		/* reload IPsec tunnels */
131
		vpn_ipsec_configure();
132

    
133
		/* reload dhcpd (gateway may have changed) */
134
		services_dhcpd_configure();
135

    
136
		/* reload dnsmasq */
137
		services_dnsmasq_configure();
138

    
139
		/* reload webgui */
140
		system_webgui_start();
141

    
142
		/* reload captive portal */
143
		captiveportal_configure();
144
	}
145

    
146
	if ($g['booting'])
147
		echo "done\n";
148

    
149
	return 0;
150
}
151

    
152
function interfaces_optional_configure() {
153
	global $config, $g;
154
	global $bridgeconfig;
155

    
156
	/* Reset bridge configuration.	Interfaces will add to it. */
157
	$bridgeconfig = "";
158

    
159
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
160
		interfaces_optional_configure_if($i);
161
	}
162

    
163
	if ($bridgeconfig) {
164
		/* Set the system bridge configuration and enable bridging. */
165
		mwexec("/sbin/sysctl net.link.ether.bridge_cfg=" . $bridgeconfig);
166

    
167
		if (isset($config['bridge']['filteringbridge']))
168
			mwexec("/sbin/sysctl net.link.ether.bridge.pf=1");
169

    
170
		mwexec("/sbin/sysctl net.link.ether.bridge=1");
171
	} else {
172
		mwexec("/sbin/sysctl net.link.ether.bridge.pf=0");
173
		mwexec("/sbin/sysctl net.link.ether.bridge=0");
174
	}
175

    
176
	if (!$g['booting']) {
177
		/* reconfigure static routes (kernel may have deleted them) */
178
		system_routing_configure();
179

    
180
		/* reload ipfilter (address may have changed) */
181
		filter_configure();
182

    
183
		/* reload IPsec tunnels */
184
		vpn_ipsec_configure();
185

    
186
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
187
		services_dhcpd_configure();
188

    
189
		/* restart dnsmasq */
190
		services_dnsmasq_configure();
191
	}
192

    
193
	return 0;
194
}
195

    
196
function interfaces_optional_configure_if($opti) {
197
	global $config, $g;
198
	global $bridgeconfig;
199

    
200
	$optcfg = $config['interfaces']['opt' . $opti];
201

    
202
	if ($g['booting']) {
203
		$optdescr = "";
204
		if ($optcfg['descr'])
205
			$optdescr = " ({$optcfg['descr']})";
206
		echo "Configuring OPT{$opti}{$optdescr} interface... ";
207
	}
208

    
209
	if (isset($optcfg['enable'])) {
210
		/* wireless configuration? */
211
		if (is_array($optcfg['wireless']))
212
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
213

    
214
		/* MAC spoofing? */
215
		if ($optcfg['spoofmac'])
216
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
217
				" link " . escapeshellarg($optcfg['spoofmac']));
218

    
219
		/* media */
220
		if ($optcfg['media'] || $optcfg['mediaopt']) {
221
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
222
			if ($optcfg['media'])
223
				$cmd .= " media " . escapeshellarg($optcfg['media']);
224
			if ($optcfg['mediaopt'])
225
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
226
			mwexec($cmd);
227
		}
228

    
229
		/* OpenVPN configuration? */
230
 		if (isset($optcfg['ovpn'])) {
231
 			if (strstr($if, "tap"))
232
 				ovpn_link_tap();
233
 		}
234

    
235
		/* bridged? */
236
		if ($optcfg['bridge']) {
237
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
238
				" delete up");
239

    
240
			if ($bridgeconfig != "")
241
				$bridgeconfig .= ",";
242

    
243
			$bridgeconfig .= $optcfg['if'] . ":" . $opti . "," .
244
				$config['interfaces'][$optcfg['bridge']]['if'] .
245
				":" . $opti;
246
		} else {
247
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
248
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
249
		}
250
	} else {
251
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
252
			" delete down");
253
	}
254

    
255
	if ($g['booting'])
256
		echo "done\n";
257

    
258
	return 0;
259
}
260

    
261
function interfaces_wireless_configure($if, $wlcfg) {
262
	global $config, $g;
263

    
264
	/* wireless configuration */
265
	$ifcargs = escapeshellarg($if) .
266
		" ssid " . escapeshellarg($wlcfg['ssid']) . " channel " .
267
		escapeshellarg($wlcfg['channel']) . " ";
268

    
269
	if ($wlcfg['stationname'])
270
		$ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
271

    
272
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
273
		$ifcargs .= "wepmode on ";
274

    
275
		$i = 1;
276
		foreach ($wlcfg['wep']['key'] as $wepkey) {
277
			$ifcargs .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
278
			if (isset($wepkey['txkey'])) {
279
				$ifcargs .= "weptxkey {$i} ";
280
			}
281
			$i++;
282
		}
283
	} else {
284
		$ifcargs .= "wepmode off ";
285
	}
286

    
287
	switch ($wlcfg['mode']) {
288
		case 'hostap':
289
			if (strstr($if, "wi"))
290
				$ifcargs .= "-mediaopt ibss mediaopt hostap ";
291
			break;
292
		case 'ibss':
293
		case 'IBSS':
294
			if (strstr($if, "wi"))
295
				$ifcargs .= "-mediaopt hostap mediaopt ibss ";
296
			else if (strstr($if, "an"))
297
				$ifcargs .= "mediaopt adhoc ";
298
			break;
299
		case 'bss':
300
		case 'BSS':
301
			if (strstr($if, "wi"))
302
				$ifcargs .= "-mediaopt hostap -mediaopt ibss ";
303
			else if (strstr($if, "an"))
304
				$ifcargs .= "-mediaopt adhoc ";
305
			break;
306
	}
307

    
308
	$ifcargs .= "up";
309

    
310
	mwexec("/sbin/ifconfig " . $ifcargs);
311

    
312
	return 0;
313
}
314

    
315
function interfaces_wan_configure() {
316
	global $config, $g;
317

    
318
	$wancfg = $config['interfaces']['wan'];
319

    
320
	if ($g['booting'])
321
		echo "Configuring WAN interface... ";
322
	else {
323
		/* kill dhclient */
324
		killbypid("{$g['varrun_path']}/dhclient.pid");
325

    
326
		/* kill PPPoE client (mpd) */
327
		killbypid("{$g['varrun_path']}/mpd.pid");
328

    
329
		/* wait for processes to die */
330
		sleep(2);
331

    
332
		unlink_if_exists("{$g['varetc_path']}/dhclient.conf");
333
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
334
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
335
		unlink_if_exists("{$g['vardb_path']}/wanip");
336
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
337

    
338
	}
339

    
340
	/* remove all addresses first */
341
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
342
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
343

    
344
	/* wireless configuration? */
345
	if (is_array($wancfg['wireless']))
346
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
347

    
348
	if ($wancfg['spoofmac'])
349
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
350
			" link " . escapeshellarg($wancfg['spoofmac']));
351

    
352
	/* media */
353
	if ($wancfg['media'] || $wancfg['mediaopt']) {
354
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
355
		if ($wancfg['media'])
356
			$cmd .= " media " . escapeshellarg($wancfg['media']);
357
		if ($wancfg['mediaopt'])
358
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
359
		mwexec($cmd);
360
	}
361

    
362
	switch ($wancfg['ipaddr']) {
363

    
364
		case 'dhcp':
365
			interfaces_wan_dhcp_configure();
366
			break;
367

    
368
		case 'pppoe':
369
			interfaces_wan_pppoe_configure();
370
			break;
371

    
372
		case 'pptp':
373
			interfaces_wan_pptp_configure();
374
			break;
375

    
376
		case 'bigpond':
377
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
378
			interfaces_wan_dhcp_configure();
379
			break;
380

    
381
		default:
382
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
383
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
384
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
385
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
386
			} else {
387
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
388
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
389
			}
390
			/* install default route */
391
			mwexec("/sbin/route delete default");
392
			mwexec("/sbin/route add default " . escapeshellarg($wancfg['gateway']));
393

    
394
			/* resync ipfilter (done automatically for DHCP/PPPoE/PPTP) */
395
			filter_resync();
396
	}
397

    
398
	if (!$g['booting']) {
399
		/* reconfigure static routes (kernel may have deleted them) */
400
		system_routing_configure();
401

    
402
		/* reload ipfilter */
403
		filter_configure();
404

    
405
		/* reload ipsec tunnels */
406
		vpn_ipsec_configure();
407

    
408
		/* restart ez-ipupdate */
409
		services_dyndns_configure();
410

    
411
		/* force DNS update */
412
		services_dnsupdate_process();
413

    
414
		/* restart dnsmasq */
415
		services_dnsmasq_configure();
416
	}
417

    
418
	if ($g['booting'])
419
		echo "done\n";
420

    
421
	return 0;
422
}
423

    
424
function interfaces_wan_dhcp_configure() {
425
	global $config, $g;
426

    
427
	$wancfg = $config['interfaces']['wan'];
428

    
429
	/* generate dhclient.conf */
430
	$fd = fopen("{$g['varetc_path']}/dhclient.conf", "w");
431
	if (!$fd) {
432
		printf("Error: cannot open dhclient.conf in interfaces_wan_dhcp_configure().\n");
433
		return 1;
434
	}
435

    
436
 	$dhclientconf = "";
437

    
438
 	if ($wancfg['dhcphostname']) {
439
		$dhclientconf .= <<<EOD
440
send dhcp-client-identifier "{$wancfg['dhcphostname']}";
441
interface "{$wancfg['if']}" {
442
	send host-name "{$wancfg['dhcphostname']}";
443
}
444

    
445
EOD;
446
	}
447

    
448
	fwrite($fd, $dhclientconf);
449
	fclose($fd);
450

    
451
	/* fire up dhclient - don't wait for the lease (-nw) */
452
	mwexec("/sbin/dhclient -nw -cf {$g['varetc_path']}/dhclient.conf " .
453
		escapeshellarg($wancfg['if']) . " &");
454

    
455
	return 0;
456
}
457

    
458
function interfaces_wan_dhcp_down() {
459
	mwexec("/sbin/dhclient -r");
460
	sleep(3);
461
}
462

    
463
function interfaces_wan_dhcp_up() {
464
	interfaces_wan_dhcp_configure();
465
	sleep(3);
466
}
467

    
468
function interfaces_wan_pppoe_configure() {
469
	global $config, $g;
470

    
471
	$wancfg = $config['interfaces']['wan'];
472
	$pppoecfg = $config['pppoe'];
473

    
474
	/* generate mpd.conf */
475
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
476
	if (!$fd) {
477
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
478
		return 1;
479
	}
480

    
481
	$idle = 0;
482

    
483
	if (isset($pppoecfg['ondemand'])) {
484
		$ondemand = "enable";
485
		if ($pppoecfg['timeout'])
486
			$idle = $pppoecfg['timeout'];
487
	} else {
488
		$ondemand = "disable";
489
	}
490

    
491
	$mpdconf = <<<EOD
492
pppoe:
493
	new -i ng0 pppoe pppoe
494
	set iface route default
495
	set iface {$ondemand} on-demand
496
	set iface idle {$idle}
497
	set iface up-script /usr/local/sbin/ppp-linkup
498

    
499
EOD;
500

    
501
	if (isset($pppoecfg['ondemand'])) {
502
		$mpdconf .= <<<EOD
503
	set iface addrs 10.0.0.1 10.0.0.2
504

    
505
EOD;
506
	}
507

    
508
	$mpdconf .= <<<EOD
509
	set bundle disable multilink
510
	set bundle authname "{$pppoecfg['username']}"
511
	set bundle password "{$pppoecfg['password']}"
512
	set link keep-alive 10 60
513
	set link max-redial 0
514
	set link no acfcomp protocomp
515
	set link disable pap chap
516
	set link accept chap
517
	set link mtu 1492
518
	set ipcp yes vjcomp
519
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
520

    
521
EOD;
522

    
523
	if (isset($config['system']['dnsallowoverride'])) {
524
		$mpdconf .= <<<EOD
525
	set ipcp enable req-pri-dns
526
	set ipcp enable req-sec-dns
527

    
528
EOD;
529
	}
530

    
531
	$mpdconf .= <<<EOD
532
	open iface
533

    
534
EOD;
535

    
536
	fwrite($fd, $mpdconf);
537
	fclose($fd);
538

    
539
	/* generate mpd.links */
540
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
541
	if (!$fd) {
542
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
543
		return 1;
544
	}
545

    
546
	$mpdconf = <<<EOD
547
pppoe:
548
	set link type pppoe
549
	set pppoe iface {$wancfg['if']}
550
	set pppoe service "{$pppoecfg['provider']}"
551
	set pppoe enable originate
552
	set pppoe disable incoming
553

    
554
EOD;
555

    
556
	fwrite($fd, $mpdconf);
557
	fclose($fd);
558

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

    
562
	return 0;
563
}
564

    
565
function interfaces_wan_pppoe_down() {
566
	global $g;
567
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
568
	sleep(3);
569
}
570

    
571
function interfaces_wan_pppoe_up() {
572
	global $g;
573
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
574
	sleep(3);
575
}
576

    
577
function interfaces_wan_pptp_configure() {
578
	global $config, $g;
579

    
580
	$wancfg = $config['interfaces']['wan'];
581
	$pptpcfg = $config['pptp'];
582

    
583
	/* generate mpd.conf */
584
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
585
	if (!$fd) {
586
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
587
		return 1;
588
	}
589

    
590
	$idle = 0;
591

    
592
	if (isset($pptpcfg['ondemand'])) {
593
		$ondemand = "enable";
594
		if ($pptpcfg['timeout'])
595
			$idle = $pptpcfg['timeout'];
596
	} else {
597
		$ondemand = "disable";
598
	}
599

    
600
	$mpdconf = <<<EOD
601
pptp:
602
	new -i ng0 pptp pptp
603
	set iface route default
604
	set iface {$ondemand} on-demand
605
	set iface idle {$idle}
606
	set iface up-script /usr/local/sbin/ppp-linkup
607

    
608
EOD;
609

    
610
	if (isset($pptpcfg['ondemand'])) {
611
		$mpdconf .= <<<EOD
612
	set iface addrs 10.0.0.1 10.0.0.2
613

    
614
EOD;
615
	}
616

    
617
	$mpdconf .= <<<EOD
618
	set bundle disable multilink
619
	set bundle authname "{$pptpcfg['username']}"
620
	set bundle password "{$pptpcfg['password']}"
621
	set link keep-alive 10 60
622
	set link max-redial 0
623
	set link no acfcomp protocomp
624
	set link disable pap chap
625
	set link accept chap
626
	set ipcp no vjcomp
627
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
628

    
629
EOD;
630

    
631
	if (isset($config['system']['dnsallowoverride'])) {
632
		$mpdconf .= <<<EOD
633
	set ipcp enable req-pri-dns
634
	set ipcp enable req-sec-dns
635

    
636
EOD;
637
	}
638

    
639
	$mpdconf .= <<<EOD
640
	open
641

    
642
EOD;
643

    
644
	fwrite($fd, $mpdconf);
645
	fclose($fd);
646

    
647
	/* generate mpd.links */
648
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
649
	if (!$fd) {
650
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
651
		return 1;
652
	}
653

    
654
	$mpdconf = <<<EOD
655
pptp:
656
	set link type pptp
657
	set pptp enable originate outcall
658
	set pptp disable windowing
659
	set pptp self {$pptpcfg['local']}
660
	set pptp peer {$pptpcfg['remote']}
661

    
662
EOD;
663

    
664
	fwrite($fd, $mpdconf);
665
	fclose($fd);
666

    
667
	/* configure interface */
668
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
669
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
670

    
671
	/* fire up mpd */
672
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pptp");
673

    
674
	return 0;
675
}
676

    
677
function interfaces_wan_pptp_down() {
678
	global $g;
679
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
680
	sleep(3);
681
}
682

    
683
function interfaces_wan_pptp_up() {
684
	global $g;
685
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
686
	sleep(3);
687
}
688

    
689
function interfaces_wan_bigpond_configure($curwanip) {
690
	global $config, $g;
691

    
692
	$bpcfg = $config['bigpond'];
693

    
694
	if (!$curwanip) {
695
		/* IP address not configured yet, exit */
696
		return 0;
697
	}
698

    
699
	/* kill bpalogin */
700
	killbyname("bpalogin");
701

    
702
	/* wait a moment */
703
	sleep(1);
704

    
705
	/* get the default domain */
706
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
707
	if ($nfd) {
708
		$defaultdomain = trim(fgets($nfd));
709
		fclose($nfd);
710
	}
711

    
712
	/* generate bpalogin.conf */
713
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
714
	if (!$fd) {
715
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
716
		return 1;
717
	}
718

    
719
	if (!$bpcfg['authserver'])
720
		$bpcfg['authserver'] = "dce-server";
721
	if (!$bpcfg['authdomain'])
722
		$bpcfg['authdomain'] = $defaultdomain;
723

    
724
	$bpconf = <<<EOD
725
username {$bpcfg['username']}
726
password {$bpcfg['password']}
727
authserver {$bpcfg['authserver']}
728
authdomain {$bpcfg['authdomain']}
729
localport 5050
730

    
731
EOD;
732

    
733
	if ($bpcfg['minheartbeatinterval'])
734
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
735

    
736
	fwrite($fd, $bpconf);
737
	fclose($fd);
738

    
739
	/* fire up bpalogin */
740
	mwexec("/usr/local/sbin/bpalogin -c {$g['varetc_path']}/bpalogin.conf");
741

    
742
	return 0;
743
}
744

    
745
function get_real_wan_interface() {
746
	global $config, $g;
747

    
748
	$wancfg = $config['interfaces']['wan'];
749

    
750
	$wanif = $wancfg['if'];
751
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
752
		$wanif = $g['pppoe_interface'];
753
	}
754

    
755
	return $wanif;
756
}
757

    
758
function get_current_wan_address() {
759
	global $config, $g;
760

    
761
	$wancfg = $config['interfaces']['wan'];
762

    
763
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
764
		/* dynamic WAN IP address, find out which one */
765
		$wanif = get_real_wan_interface();
766

    
767
		/* get interface info with netstat */
768
		exec("/usr/bin/netstat -nWI " . escapeshellarg($wanif) . " -f inet", $ifinfo);
769

    
770
		if (isset($ifinfo[1])) {
771
			$aif = preg_split("/\s+/", $ifinfo[1]);
772
			$curwanip = chop($aif[3]);
773

    
774
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
775
				return $curwanip;
776
		}
777

    
778
		return null;
779
	} else {
780
		/* static WAN IP address */
781
		return $wancfg['ipaddr'];
782
	}
783
}
784

    
785
?>
(6-6/14)