Project

General

Profile

Download (18.5 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
	enable_hardware_offloading($lancfg['if']);
147

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

    
151
	return 0;
152
}
153

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

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

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

    
165
	if ($bridgeconfig) {
166
		/* Set the system bridge configuration and enable bridging. */
167
		mwexec("/sbin/sysctl net.link.ether.bridge_cfg=" . $bridgeconfig);
168
		mwexec("/sbin/sysctl net.link.ether.bridge.enable=1");
169
		if (isset($config['bridge']['filteringbridge']))
170
			mwexec("/sbin/sysctl net.link.ether.bridge.pf=1");
171
	} else {
172
		mwexec("/sbin/sysctl net.link.ether.bridge.pf=0");
173
		mwexec("/sbin/sysctl net.link.ether.bridge.enable=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
	enable_hardware_offloading(escapeshellarg($optcfg['if']));
256

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

    
260
	return 0;
261
}
262

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

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

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

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

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

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

    
310
	$ifcargs .= "up";
311

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

    
314
	return 0;
315
}
316

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

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

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

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

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

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

    
340
	}
341

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
425
	return 0;
426
}
427

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

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

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

    
440
 	$dhclientconf = "";
441

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

    
449
EOD;
450
	}
451

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

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

    
459
	return 0;
460
}
461

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

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

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

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

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

    
485
	$idle = 0;
486

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

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

    
503
EOD;
504

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

    
509
EOD;
510
	}
511

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

    
525
EOD;
526

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

    
532
EOD;
533
	}
534

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

    
538
EOD;
539

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

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

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

    
558
EOD;
559

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

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

    
566
	return 0;
567
}
568

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

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

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

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

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

    
594
	$idle = 0;
595

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

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

    
612
EOD;
613

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

    
618
EOD;
619
	}
620

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

    
633
EOD;
634

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

    
640
EOD;
641
	}
642

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

    
646
EOD;
647

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

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

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

    
666
EOD;
667

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

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

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

    
678
	return 0;
679
}
680

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

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

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

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

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

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

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

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

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

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

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

    
735
EOD;
736

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

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

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

    
746
	return 0;
747
}
748

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

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

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

    
759
	return $wanif;
760
}
761

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

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

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

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

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

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

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

    
789
?>
(6-6/14)