Project

General

Profile

Download (18.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	interfaces.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
function interfaces_loopback_configure() {
38
	global $config, $g;
39

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

    
42
	return 0;
43
}
44

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

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

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

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

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

    
59
		$i = 0;
60

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

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

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

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

    
79
			mwexec($cmd);
80

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

    
84
			$i++;
85
		}
86
	}
87

    
88
	return 0;
89
}
90

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
147
	enable_hardware_offloading($lancfg['if']);
148

    
149
	if ($g['booting'])
150
		echo "done\n";
151

    
152
	return 0;
153
}
154

    
155
function interfaces_optional_configure() {
156
	global $config, $g;
157
	global $bridgeconfig;
158

    
159
	/* Reset bridge configuration.	Interfaces will add to it. */
160
	$bridgeconfig = "";
161

    
162
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
163
		interfaces_optional_configure_if($i);
164
	}
165

    
166
	if ($bridgeconfig) {
167
		/* Set the system bridge configuration and enable bridging. */
168
		mwexec("/sbin/sysctl net.link.ether.bridge_cfg=" . $bridgeconfig);
169
		mwexec("/sbin/sysctl net.link.ether.bridge.enable=1");
170
		if (isset($config['bridge']['filteringbridge']))
171
			mwexec("/sbin/sysctl net.link.ether.bridge.pf=1");
172
	} else {
173
		mwexec("/sbin/sysctl net.link.ether.bridge.pf=0");
174
		mwexec("/sbin/sysctl net.link.ether.bridge.enable=0");
175
	}
176

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

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

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

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

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

    
194
	return 0;
195
}
196

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

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

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

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

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

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

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

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

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

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

    
256
	enable_hardware_offloading(escapeshellarg($optcfg['if']));
257

    
258
	if ($g['booting'])
259
		echo "done\n";
260

    
261
	return 0;
262
}
263

    
264
function interfaces_wireless_configure($if, $wlcfg) {
265
	global $config, $g;
266

    
267
	/* wireless configuration */
268
	$ifcargs = escapeshellarg($if) .
269
		" ssid " . escapeshellarg($wlcfg['ssid']) . " channel " .
270
		escapeshellarg($wlcfg['channel']) . " ";
271

    
272
	if ($wlcfg['stationname'])
273
		$ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
274

    
275
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
276
		$ifcargs .= "wepmode on ";
277

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

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

    
311
	$ifcargs .= "up";
312

    
313
	mwexec("/sbin/ifconfig " . $ifcargs);
314

    
315
	return 0;
316
}
317

    
318
function interfaces_wan_configure() {
319
	global $config, $g;
320

    
321
	$wancfg = $config['interfaces']['wan'];
322

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

    
329
		/* kill PPPoE client (mpd) */
330
		killbypid("{$g['varrun_path']}/mpd.pid");
331

    
332
		/* wait for processes to die */
333
		sleep(2);
334

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

    
341
	}
342

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

    
347
	/* wireless configuration? */
348
	if (is_array($wancfg['wireless']))
349
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
350

    
351
	if ($wancfg['spoofmac'])
352
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
353
			" link " . escapeshellarg($wancfg['spoofmac']));
354

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

    
365
	switch ($wancfg['ipaddr']) {
366

    
367
		case 'dhcp':
368
			interfaces_wan_dhcp_configure();
369
			break;
370

    
371
		case 'pppoe':
372
			interfaces_wan_pppoe_configure();
373
			break;
374

    
375
		case 'pptp':
376
			interfaces_wan_pptp_configure();
377
			break;
378

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

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

    
397
			/* resync ipfilter (done automatically for DHCP/PPPoE/PPTP) */
398
			filter_resync();
399
	}
400

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

    
405
		/* reload ipfilter */
406
		filter_configure();
407

    
408
		/* reload ipsec tunnels */
409
		vpn_ipsec_configure();
410

    
411
		/* restart ez-ipupdate */
412
		services_dyndns_configure();
413

    
414
		/* force DNS update */
415
		services_dnsupdate_process();
416

    
417
		/* restart dnsmasq */
418
		services_dnsmasq_configure();
419
	}
420

    
421
	enable_hardware_offloading($wancfg['if']);
422

    
423
	if ($g['booting'])
424
		echo "done\n";
425

    
426
	return 0;
427
}
428

    
429
function interfaces_wan_dhcp_configure() {
430
	global $config, $g;
431

    
432
	$wancfg = $config['interfaces']['wan'];
433

    
434
	/* generate dhclient.conf */
435
	$fd = fopen("{$g['varetc_path']}/dhclient.conf", "w");
436
	if (!$fd) {
437
		printf("Error: cannot open dhclient.conf in interfaces_wan_dhcp_configure().\n");
438
		return 1;
439
	}
440

    
441
 	$dhclientconf = "";
442

    
443
 	if ($wancfg['dhcphostname']) {
444
		$dhclientconf .= <<<EOD
445
send dhcp-client-identifier "{$wancfg['dhcphostname']}";
446
interface "{$wancfg['if']}" {
447
	send host-name "{$wancfg['dhcphostname']}";
448
}
449

    
450
EOD;
451
	}
452

    
453
	fwrite($fd, $dhclientconf);
454
	fclose($fd);
455

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

    
460
	return 0;
461
}
462

    
463
function interfaces_wan_dhcp_down() {
464
	mwexec("/sbin/dhclient -r");
465
	sleep(3);
466
}
467

    
468
function interfaces_wan_dhcp_up() {
469
	interfaces_wan_dhcp_configure();
470
	sleep(3);
471
}
472

    
473
function interfaces_wan_pppoe_configure() {
474
	global $config, $g;
475

    
476
	$wancfg = $config['interfaces']['wan'];
477
	$pppoecfg = $config['pppoe'];
478

    
479
	/* generate mpd.conf */
480
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
481
	if (!$fd) {
482
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
483
		return 1;
484
	}
485

    
486
	$idle = 0;
487

    
488
	if (isset($pppoecfg['ondemand'])) {
489
		$ondemand = "enable";
490
		if ($pppoecfg['timeout'])
491
			$idle = $pppoecfg['timeout'];
492
	} else {
493
		$ondemand = "disable";
494
	}
495

    
496
	$mpdconf = <<<EOD
497
pppoe:
498
	new -i ng0 pppoe pppoe
499
	set iface route default
500
	set iface {$ondemand} on-demand
501
	set iface idle {$idle}
502
	set iface up-script /usr/local/sbin/ppp-linkup
503

    
504
EOD;
505

    
506
	if (isset($pppoecfg['ondemand'])) {
507
		$mpdconf .= <<<EOD
508
	set iface addrs 10.0.0.1 10.0.0.2
509

    
510
EOD;
511
	}
512

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

    
526
EOD;
527

    
528
	if (isset($config['system']['dnsallowoverride'])) {
529
		$mpdconf .= <<<EOD
530
	set ipcp enable req-pri-dns
531
	set ipcp enable req-sec-dns
532

    
533
EOD;
534
	}
535

    
536
	$mpdconf .= <<<EOD
537
	open iface
538

    
539
EOD;
540

    
541
	fwrite($fd, $mpdconf);
542
	fclose($fd);
543

    
544
	/* generate mpd.links */
545
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
546
	if (!$fd) {
547
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
548
		return 1;
549
	}
550

    
551
	$mpdconf = <<<EOD
552
pppoe:
553
	set link type pppoe
554
	set pppoe iface {$wancfg['if']}
555
	set pppoe service "{$pppoecfg['provider']}"
556
	set pppoe enable originate
557
	set pppoe disable incoming
558

    
559
EOD;
560

    
561
	fwrite($fd, $mpdconf);
562
	fclose($fd);
563

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

    
567
	return 0;
568
}
569

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

    
576
function interfaces_wan_pppoe_up() {
577
	global $g;
578
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
579
	sleep(3);
580
}
581

    
582
function interfaces_wan_pptp_configure() {
583
	global $config, $g;
584

    
585
	$wancfg = $config['interfaces']['wan'];
586
	$pptpcfg = $config['pptp'];
587

    
588
	/* generate mpd.conf */
589
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
590
	if (!$fd) {
591
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
592
		return 1;
593
	}
594

    
595
	$idle = 0;
596

    
597
	if (isset($pptpcfg['ondemand'])) {
598
		$ondemand = "enable";
599
		if ($pptpcfg['timeout'])
600
			$idle = $pptpcfg['timeout'];
601
	} else {
602
		$ondemand = "disable";
603
	}
604

    
605
	$mpdconf = <<<EOD
606
pptp:
607
	new -i ng0 pptp pptp
608
	set iface route default
609
	set iface {$ondemand} on-demand
610
	set iface idle {$idle}
611
	set iface up-script /usr/local/sbin/ppp-linkup
612

    
613
EOD;
614

    
615
	if (isset($pptpcfg['ondemand'])) {
616
		$mpdconf .= <<<EOD
617
	set iface addrs 10.0.0.1 10.0.0.2
618

    
619
EOD;
620
	}
621

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

    
634
EOD;
635

    
636
	if (isset($config['system']['dnsallowoverride'])) {
637
		$mpdconf .= <<<EOD
638
	set ipcp enable req-pri-dns
639
	set ipcp enable req-sec-dns
640

    
641
EOD;
642
	}
643

    
644
	$mpdconf .= <<<EOD
645
	open
646

    
647
EOD;
648

    
649
	fwrite($fd, $mpdconf);
650
	fclose($fd);
651

    
652
	/* generate mpd.links */
653
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
654
	if (!$fd) {
655
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
656
		return 1;
657
	}
658

    
659
	$mpdconf = <<<EOD
660
pptp:
661
	set link type pptp
662
	set pptp enable originate outcall
663
	set pptp disable windowing
664
	set pptp self {$pptpcfg['local']}
665
	set pptp peer {$pptpcfg['remote']}
666

    
667
EOD;
668

    
669
	fwrite($fd, $mpdconf);
670
	fclose($fd);
671

    
672
	/* configure interface */
673
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
674
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
675

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

    
679
	return 0;
680
}
681

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

    
688
function interfaces_wan_pptp_up() {
689
	global $g;
690
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
691
	sleep(3);
692
}
693

    
694
function interfaces_wan_bigpond_configure($curwanip) {
695
	global $config, $g;
696

    
697
	$bpcfg = $config['bigpond'];
698

    
699
	if (!$curwanip) {
700
		/* IP address not configured yet, exit */
701
		return 0;
702
	}
703

    
704
	/* kill bpalogin */
705
	killbyname("bpalogin");
706

    
707
	/* wait a moment */
708
	sleep(1);
709

    
710
	/* get the default domain */
711
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
712
	if ($nfd) {
713
		$defaultdomain = trim(fgets($nfd));
714
		fclose($nfd);
715
	}
716

    
717
	/* generate bpalogin.conf */
718
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
719
	if (!$fd) {
720
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
721
		return 1;
722
	}
723

    
724
	if (!$bpcfg['authserver'])
725
		$bpcfg['authserver'] = "dce-server";
726
	if (!$bpcfg['authdomain'])
727
		$bpcfg['authdomain'] = $defaultdomain;
728

    
729
	$bpconf = <<<EOD
730
username {$bpcfg['username']}
731
password {$bpcfg['password']}
732
authserver {$bpcfg['authserver']}
733
authdomain {$bpcfg['authdomain']}
734
localport 5050
735

    
736
EOD;
737

    
738
	if ($bpcfg['minheartbeatinterval'])
739
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
740

    
741
	fwrite($fd, $bpconf);
742
	fclose($fd);
743

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

    
747
	return 0;
748
}
749

    
750
function get_real_wan_interface() {
751
	global $config, $g;
752

    
753
	$wancfg = $config['interfaces']['wan'];
754

    
755
	$wanif = $wancfg['if'];
756
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
757
		$wanif = $g['pppoe_interface'];
758
	}
759

    
760
	return $wanif;
761
}
762

    
763
function get_current_wan_address() {
764
	global $config, $g;
765

    
766
	$wancfg = $config['interfaces']['wan'];
767

    
768
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
769
		/* dynamic WAN IP address, find out which one */
770
		$wanif = get_real_wan_interface();
771

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

    
775
		if (isset($ifinfo[1])) {
776
			$aif = preg_split("/\s+/", $ifinfo[1]);
777
			$curwanip = chop($aif[3]);
778

    
779
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
780
				return $curwanip;
781
		}
782

    
783
		return null;
784
	} else {
785
		/* static WAN IP address */
786
		return $wancfg['ipaddr'];
787
	}
788
}
789

    
790
?>
(7-7/17)