Project

General

Profile

Download (21 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
	if ($g['booting'])
148
		echo "done\n";
149

    
150
	return 0;
151
}
152

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

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

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

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

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

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

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

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

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

    
192
	return 0;
193
}
194

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

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

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

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

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

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

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

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

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

    
242
			//$bridgeconfig .= $optcfg['if'] . ":" . $opti . "," .
243
			//	$config['interfaces'][$optcfg['bridge']]['if'] .
244
			//	":" . $opti;
245
                        
246
                        /* use open/netBSD style bridge */
247
			mwexec("/sbin/ifconfig bridge0 create");
248
                        mwexec("/sbin/brconfig bridge0 add {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']} up ");
249
		} else {
250
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
251
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
252
		}
253
	} else {
254
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
255
			" delete down");
256
	}
257

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

    
261
	return 0;
262
}
263

    
264
function interfaces_carp_configure() {
265
	global $g, $config;
266
	if ($g['booting']) {
267
		echo "Configuring CARP interfaces... \n";
268
	}	
269
	unlink_if_exists("/usr/local/etc/rc.d/carp.sh");
270
	unlink_if_exists("/usr/local/pkg/pf/carp.sh");
271
	unlink_if_exists("/usr/local/pkg/pf/carp_rules.sh");
272
	$carp_instances_counter = 0;
273
	$pfsync_instances_counter = 0;
274
	if($config['installedpackages']['carpsettings']['config'] != "") {
275
		foreach($config['installedpackages']['carpsettings']['config'] as $carp)
276
		if($carp['pfsyncenabled'] != "") {
277
		      $pfsync = 1;
278
		      if($carp['premption'] != "")
279
				mwexec("/sbin/sysctl net.inet.carp.preempt=1");
280
		      if($carp['balancing'] != "")
281
				mwexec("/sbin/sysctl net.inet.arpbalance=1");
282
		      $carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
283
		      mwexec("/sbin/ifconfig pfsync0 create");
284
		      mwexec("/sbin/ifconfig pfsync0 syncdev " . $carp_sync_int);
285
		      mwexec("/sbin/ifconfig pfsync0 syncif " . $carp_sync_int);
286
		      mwexec("/sbin/ifconfig {$carp_sync_int} up");
287
		      mwexec("/sbin/ifconfig pfsync0 up");
288
		      if($g['booting']) {
289
			/* install rules to alllow pfsync to sync up during boot
290
			 * carp interfaces will remain down until the bootup sequence finishes
291
			 */
292
			exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
293
			exec("echo pass quick proto pfsync all keep state >> /tmp/rules.boot");
294
			exec("/sbin/pfctl -f /tmp/rules.boot");
295
		      }
296
		      $pfsync_instances_counter++;
297
		}
298
	}		
299
	if($config['installedpackages']['carp']['config'] != "") {
300
		foreach($config['installedpackages']['carp']['config'] as $carp) {
301
		  /*
302
		   *  create the carp interface
303
		   */
304
		  mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
305
		  $broadcast_address = gen_subnet_max($carp['ipaddress'], $carp['netmask']);
306
		  if($carp['password'] != "") {
307
		    $password = " pass " . $carp['password'];
308
		  }
309
		  mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $carp['ipaddress'] . "/" . $carp['netmask'] . " broadcast " . $broadcast_address . " vhid " . $carp['vhid'] . " advskew " . $carp['advskew'] . $password);
310
		  $carp_instances_counter++;
311
		}
312
	}
313
}
314

    
315
function interfaces_carp_bringup() {
316
	global $g, $config;
317
	/* lets bring the carp interfaces up now */
318
	$carp_ints = find_number_of_created_carp_interfaces();
319
	for($x=0; $x<$carp_ints; $x++)
320
		mwexec("/sbin/ifconfig carp{$carp_instances_counter} up");	
321
}
322

    
323
function interfaces_wireless_configure($if, $wlcfg) {
324
	global $config, $g;
325

    
326
	/* wireless configuration */
327
	$ifcargs = escapeshellarg($if) .
328
		" ssid " . escapeshellarg($wlcfg['ssid']) . " channel " .
329
		escapeshellarg($wlcfg['channel']) . " ";
330

    
331
	if ($wlcfg['stationname'])
332
		$ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
333

    
334
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
335
		$ifcargs .= "wepmode on ";
336

    
337
		$i = 1;
338
		foreach ($wlcfg['wep']['key'] as $wepkey) {
339
			$ifcargs .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
340
			if (isset($wepkey['txkey'])) {
341
				$ifcargs .= "weptxkey {$i} ";
342
			}
343
			$i++;
344
		}
345
	} else {
346
		$ifcargs .= "wepmode off ";
347
	}
348

    
349
	switch ($wlcfg['mode']) {
350
		case 'hostap':
351
			if (strstr($if, "wi"))
352
				$ifcargs .= "-mediaopt ibss mediaopt hostap ";
353
			break;
354
		case 'ibss':
355
		case 'IBSS':
356
			if (strstr($if, "wi"))
357
				$ifcargs .= "-mediaopt hostap mediaopt ibss ";
358
			else if (strstr($if, "an"))
359
				$ifcargs .= "mediaopt adhoc ";
360
			break;
361
		case 'bss':
362
		case 'BSS':
363
			if (strstr($if, "wi"))
364
				$ifcargs .= "-mediaopt hostap -mediaopt ibss ";
365
			else if (strstr($if, "an"))
366
				$ifcargs .= "-mediaopt adhoc ";
367
			break;
368
	}
369

    
370
	$ifcargs .= "up";
371

    
372
	mwexec("/sbin/ifconfig " . $ifcargs);
373

    
374
	return 0;
375
}
376

    
377
function interfaces_wan_configure() {
378
	global $config, $g;
379

    
380
	$wancfg = $config['interfaces']['wan'];
381

    
382
	if ($g['booting'])
383
		echo "Configuring WAN interface... ";
384
	else {
385
		/* kill dhclient */
386
		killbypid("{$g['varrun_path']}/dhclient.pid");
387

    
388
		/* kill PPPoE client (mpd) */
389
		killbypid("{$g['varrun_path']}/mpd.pid");
390

    
391
		/* wait for processes to die */
392
		sleep(2);
393

    
394
		unlink_if_exists("{$g['varetc_path']}/dhclient.conf");
395
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
396
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
397
		unlink_if_exists("{$g['vardb_path']}/wanip");
398
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
399

    
400
	}
401

    
402
	/* remove all addresses first */
403
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
404
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
405

    
406
	/* wireless configuration? */
407
	if (is_array($wancfg['wireless']))
408
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
409

    
410
	if ($wancfg['spoofmac'])
411
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
412
			" link " . escapeshellarg($wancfg['spoofmac']));
413

    
414
	/* media */
415
	if ($wancfg['media'] || $wancfg['mediaopt']) {
416
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
417
		if ($wancfg['media'])
418
			$cmd .= " media " . escapeshellarg($wancfg['media']);
419
		if ($wancfg['mediaopt'])
420
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
421
		mwexec($cmd);
422
	}
423

    
424
	switch ($wancfg['ipaddr']) {
425

    
426
		case 'dhcp':
427
			interfaces_wan_dhcp_configure();
428
			break;
429

    
430
		case 'pppoe':
431
			interfaces_wan_pppoe_configure();
432
			break;
433

    
434
		case 'pptp':
435
			interfaces_wan_pptp_configure();
436
			break;
437

    
438
		case 'bigpond':
439
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
440
			interfaces_wan_dhcp_configure();
441
			break;
442

    
443
		default:
444
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
445
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
446
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
447
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
448
			} else {
449
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
450
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
451
			}
452
			/* install default route */
453
			mwexec("/sbin/route delete default");
454
			mwexec("/sbin/route add default " . escapeshellarg($config['system']['gateway']));
455

    
456
			/* resync ipfilter (done automatically for DHCP/PPPoE/PPTP) */
457
			filter_resync();
458
	}
459

    
460
	if (!$g['booting']) {
461
		/* reconfigure static routes (kernel may have deleted them) */
462
		system_routing_configure();
463

    
464
		/* reload ipfilter */
465
		filter_configure();
466

    
467
		/* reload ipsec tunnels */
468
		vpn_ipsec_configure();
469

    
470
		/* restart ez-ipupdate */
471
		services_dyndns_configure();
472

    
473
		/* force DNS update */
474
		services_dnsupdate_process();
475

    
476
		/* restart dnsmasq */
477
		services_dnsmasq_configure();
478
	}
479

    
480
	if ($g['booting'])
481
		echo "done\n";
482

    
483
	return 0;
484
}
485

    
486
function interfaces_wan_dhcp_configure() {
487
	global $config, $g;
488

    
489
	$wancfg = $config['interfaces']['wan'];
490

    
491
	/* generate dhclient.conf */
492
	$fd = fopen("{$g['varetc_path']}/dhclient.conf", "w");
493
	if (!$fd) {
494
		printf("Error: cannot open dhclient.conf in interfaces_wan_dhcp_configure().\n");
495
		return 1;
496
	}
497

    
498
 	$dhclientconf = "";
499

    
500
 	if ($wancfg['dhcphostname']) {
501
		$dhclientconf .= <<<EOD
502
send dhcp-client-identifier "{$wancfg['dhcphostname']}";
503
interface "{$wancfg['if']}" {
504
	send host-name "{$wancfg['dhcphostname']}";
505
}
506

    
507
EOD;
508
	}
509

    
510
	fwrite($fd, $dhclientconf);
511
	fclose($fd);
512

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

    
517
	return 0;
518
}
519

    
520
function interfaces_wan_dhcp_down() {
521
	mwexec("/sbin/dhclient -r");
522
	sleep(3);
523
}
524

    
525
function interfaces_wan_dhcp_up() {
526
	interfaces_wan_dhcp_configure();
527
	sleep(3);
528
}
529

    
530
function interfaces_wan_pppoe_configure() {
531
	global $config, $g;
532

    
533
	$wancfg = $config['interfaces']['wan'];
534
	$pppoecfg = $config['pppoe'];
535

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

    
543
	$idle = 0;
544

    
545
	if (isset($pppoecfg['ondemand'])) {
546
		$ondemand = "enable";
547
		if ($pppoecfg['timeout'])
548
			$idle = $pppoecfg['timeout'];
549
	} else {
550
		$ondemand = "disable";
551
	}
552

    
553
	$mpdconf = <<<EOD
554
pppoe:
555
	new -i ng0 pppoe pppoe
556
	set iface route default
557
	set iface {$ondemand} on-demand
558
	set iface idle {$idle}
559
	set iface up-script /usr/local/sbin/ppp-linkup
560

    
561
EOD;
562

    
563
	if (isset($pppoecfg['ondemand'])) {
564
		$mpdconf .= <<<EOD
565
	set iface addrs 10.0.0.1 10.0.0.2
566

    
567
EOD;
568
	}
569

    
570
	$mpdconf .= <<<EOD
571
	set bundle disable multilink
572
	set bundle authname "{$pppoecfg['username']}"
573
	set bundle password "{$pppoecfg['password']}"
574
	set link keep-alive 10 60
575
	set link max-redial 0
576
	set link no acfcomp protocomp
577
	set link disable pap chap
578
	set link accept chap
579
	set link mtu 1492
580
	set ipcp yes vjcomp
581
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
582

    
583
EOD;
584

    
585
	if (isset($config['system']['dnsallowoverride'])) {
586
		$mpdconf .= <<<EOD
587
	set ipcp enable req-pri-dns
588
	set ipcp enable req-sec-dns
589

    
590
EOD;
591
	}
592

    
593
	$mpdconf .= <<<EOD
594
	open iface
595

    
596
EOD;
597

    
598
	fwrite($fd, $mpdconf);
599
	fclose($fd);
600

    
601
	/* generate mpd.links */
602
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
603
	if (!$fd) {
604
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
605
		return 1;
606
	}
607

    
608
	$mpdconf = <<<EOD
609
pppoe:
610
	set link type pppoe
611
	set pppoe iface {$wancfg['if']}
612
	set pppoe service "{$pppoecfg['provider']}"
613
	set pppoe enable originate
614
	set pppoe disable incoming
615

    
616
EOD;
617

    
618
	fwrite($fd, $mpdconf);
619
	fclose($fd);
620

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

    
624
	return 0;
625
}
626

    
627
function interfaces_wan_pppoe_down() {
628
	global $g;
629
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
630
	sleep(3);
631
}
632

    
633
function interfaces_wan_pppoe_up() {
634
	global $g;
635
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
636
	sleep(3);
637
}
638

    
639
function interfaces_wan_pptp_configure() {
640
	global $config, $g;
641

    
642
	$wancfg = $config['interfaces']['wan'];
643
	$pptpcfg = $config['pptp'];
644

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

    
652
	$idle = 0;
653

    
654
	if (isset($pptpcfg['ondemand'])) {
655
		$ondemand = "enable";
656
		if ($pptpcfg['timeout'])
657
			$idle = $pptpcfg['timeout'];
658
	} else {
659
		$ondemand = "disable";
660
	}
661

    
662
	$mpdconf = <<<EOD
663
pptp:
664
	new -i ng0 pptp pptp
665
	set iface route default
666
	set iface {$ondemand} on-demand
667
	set iface idle {$idle}
668
	set iface up-script /usr/local/sbin/ppp-linkup
669

    
670
EOD;
671

    
672
	if (isset($pptpcfg['ondemand'])) {
673
		$mpdconf .= <<<EOD
674
	set iface addrs 10.0.0.1 10.0.0.2
675

    
676
EOD;
677
	}
678

    
679
	$mpdconf .= <<<EOD
680
	set bundle disable multilink
681
	set bundle authname "{$pptpcfg['username']}"
682
	set bundle password "{$pptpcfg['password']}"
683
	set link keep-alive 10 60
684
	set link max-redial 0
685
	set link no acfcomp protocomp
686
	set link disable pap chap
687
	set link accept chap
688
	set ipcp no vjcomp
689
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
690

    
691
EOD;
692

    
693
	if (isset($config['system']['dnsallowoverride'])) {
694
		$mpdconf .= <<<EOD
695
	set ipcp enable req-pri-dns
696
	set ipcp enable req-sec-dns
697

    
698
EOD;
699
	}
700

    
701
	$mpdconf .= <<<EOD
702
	open
703

    
704
EOD;
705

    
706
	fwrite($fd, $mpdconf);
707
	fclose($fd);
708

    
709
	/* generate mpd.links */
710
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
711
	if (!$fd) {
712
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
713
		return 1;
714
	}
715

    
716
	$mpdconf = <<<EOD
717
pptp:
718
	set link type pptp
719
	set pptp enable originate outcall
720
	set pptp disable windowing
721
	set pptp self {$pptpcfg['local']}
722
	set pptp peer {$pptpcfg['remote']}
723

    
724
EOD;
725

    
726
	fwrite($fd, $mpdconf);
727
	fclose($fd);
728

    
729
	/* configure interface */
730
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
731
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
732

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

    
736
	return 0;
737
}
738

    
739
function interfaces_wan_pptp_down() {
740
	global $g;
741
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
742
	sleep(3);
743
}
744

    
745
function interfaces_wan_pptp_up() {
746
	global $g;
747
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
748
	sleep(3);
749
}
750

    
751
function interfaces_wan_bigpond_configure($curwanip) {
752
	global $config, $g;
753

    
754
	$bpcfg = $config['bigpond'];
755

    
756
	if (!$curwanip) {
757
		/* IP address not configured yet, exit */
758
		return 0;
759
	}
760

    
761
	/* kill bpalogin */
762
	killbyname("bpalogin");
763

    
764
	/* wait a moment */
765
	sleep(1);
766

    
767
	/* get the default domain */
768
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
769
	if ($nfd) {
770
		$defaultdomain = trim(fgets($nfd));
771
		fclose($nfd);
772
	}
773

    
774
	/* generate bpalogin.conf */
775
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
776
	if (!$fd) {
777
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
778
		return 1;
779
	}
780

    
781
	if (!$bpcfg['authserver'])
782
		$bpcfg['authserver'] = "dce-server";
783
	if (!$bpcfg['authdomain'])
784
		$bpcfg['authdomain'] = $defaultdomain;
785

    
786
	$bpconf = <<<EOD
787
username {$bpcfg['username']}
788
password {$bpcfg['password']}
789
authserver {$bpcfg['authserver']}
790
authdomain {$bpcfg['authdomain']}
791
localport 5050
792

    
793
EOD;
794

    
795
	if ($bpcfg['minheartbeatinterval'])
796
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
797

    
798
	fwrite($fd, $bpconf);
799
	fclose($fd);
800

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

    
804
	return 0;
805
}
806

    
807
function get_real_wan_interface() {
808
	global $config, $g;
809

    
810
	$wancfg = $config['interfaces']['wan'];
811

    
812
	$wanif = $wancfg['if'];
813
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
814
		$wanif = $g['pppoe_interface'];
815
	}
816

    
817
	return $wanif;
818
}
819

    
820
function get_current_wan_address() {
821
	global $config, $g;
822

    
823
	$wancfg = $config['interfaces']['wan'];
824

    
825
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
826
		/* dynamic WAN IP address, find out which one */
827
		$wanif = get_real_wan_interface();
828

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

    
832
		if (isset($ifinfo[1])) {
833
			$aif = preg_split("/\s+/", $ifinfo[1]);
834
			$curwanip = chop($aif[3]);
835

    
836
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
837
				return $curwanip;
838
		}
839

    
840
		return null;
841
	} else {
842
		/* static WAN IP address */
843
		return $wancfg['ipaddr'];
844
	}
845
}
846

    
847
?>
(7-7/18)