Project

General

Profile

Download (27 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
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
39

    
40
	return 0;
41
}
42

    
43
function interfaces_vlan_configure() {
44
	global $config;
45

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

    
48
		/* devices with native VLAN support */
49
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
50

    
51
		/* devices with long frame support */
52
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
53

    
54
		$i = 0;
55

    
56
		foreach ($config['vlans']['vlan'] as $vlan) {
57

    
58
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
59
				escapeshellarg($vlan['tag']) . " vlandev " .
60
				escapeshellarg($vlan['if']);
61

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

    
69
			if (in_array($drvname, $vlan_native_supp))
70
				$cmd .= " link0";
71
			else if (in_array($drvname, $vlan_long_supp))
72
				$cmd .= " mtu 1500";
73

    
74
			mwexec($cmd);
75

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

    
79
			$i++;
80
		}
81
	}
82

    
83
	return 0;
84
}
85

    
86
function interfaces_lan_configure() {
87
	global $config, $g;
88

    
89
	$lancfg = $config['interfaces']['lan'];
90

    
91
	/* wireless configuration? */
92
	if (is_array($lancfg['wireless']))
93
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
94

    
95
	/* MAC spoofing? */
96
	if ($lancfg['spoofmac']) {
97
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
98
			" link " . escapeshellarg($lancfg['spoofmac']));
99
	} else {
100
		$mac = get_interface_mac_address($lancfg['if']);
101
		if($mac == "ff:ff:ff:ff:ff:ff") {
102
			/*   this is not a valid mac address.  generate a
103
			 *   temporary mac address so the machine can get online.
104
			 */
105
			echo "Generating new MAC address.";
106
			$random_mac = generate_random_mac_address();
107
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
108
				" link " . escapeshellarg($random_mac));
109
			$lancfg['spoofmac'] = $random_mac;
110
			write_config();
111
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$lancfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
112
		}
113
	}	
114
	
115
	/* media */
116
	if ($lancfg['media'] || $lancfg['mediaopt']) {
117
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
118
		if ($lancfg['media'])
119
			$cmd .= " media " . escapeshellarg($lancfg['media']);
120
		if ($lancfg['mediaopt'])
121
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
122
		mwexec($cmd);
123
	}
124

    
125
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
126
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
127

    
128
	if (!$g['booting']) {
129
		/* make new hosts file */
130
		system_hosts_generate();
131

    
132
		/* reconfigure static routes (kernel may have deleted them) */
133
		system_routing_configure();
134

    
135
		/* set the reload filter dity flag */
136
		touch("{$g['tmp_path']}/filter_dirty");
137

    
138
		/* reload IPsec tunnels */
139
		vpn_ipsec_configure();
140

    
141
		/* reload dhcpd (gateway may have changed) */
142
		services_dhcpd_configure();
143

    
144
		/* reload dnsmasq */
145
		services_dnsmasq_configure();
146

    
147
		/* reload webgui */
148
		system_webgui_start();
149

    
150
		/* reload captive portal */
151
		captiveportal_configure();
152
	}
153

    
154
	return 0;
155
}
156

    
157
function interfaces_optional_configure() {
158
	global $config, $g;
159
	global $bridgeconfig;
160

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

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

    
168
	if (!$g['booting']) {
169
		/* reconfigure static routes (kernel may have deleted them) */
170
		system_routing_configure();
171

    
172
		/* set the reload filter dity flag */
173
		touch("{$g['tmp_path']}/filter_dirty");
174

    
175
		/* reload IPsec tunnels */
176
		vpn_ipsec_configure();
177

    
178
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
179
		services_dhcpd_configure();
180

    
181
		/* restart dnsmasq */
182
		services_dnsmasq_configure();
183
	}
184

    
185
	return 0;
186
}
187

    
188
function interfaces_optional_configure_if($opti) {
189
	global $config, $g;
190
	global $bridgeconfig;
191
	global $bridges_total;
192
	
193
	if(!is_numeric($bridges_total)) $bridges_total=0;
194

    
195
	$optcfg = $config['interfaces']['opt' . $opti];
196

    
197
	if ($g['booting']) {
198
		$optdescr = "";
199
		if ($optcfg['descr'])
200
			$optdescr = " ({$optcfg['descr']})";
201
		print "\tOPT{$opti}{$optdescr}... ";
202
		mute_kernel_msgs();
203
	}
204

    
205
	if (isset($optcfg['enable'])) {
206
		/* wireless configuration? */
207
		if (is_array($optcfg['wireless']))
208
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
209

    
210
		/* MAC spoofing? */
211
		if ($optcfg['spoofmac']) {
212
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
213
				" link " . escapeshellarg($optcfg['spoofmac']));
214
		} else {
215
			$mac = get_interface_mac_address($optcfg['if']);
216
			if($mac == "ff:ff:ff:ff:ff:ff") {
217
				/*   this is not a valid mac address.  generate a
218
				 *   temporary mac address so the machine can get online.
219
				 */
220
				echo "Generating new MAC address.";
221
				$random_mac = generate_random_mac_address();
222
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
223
					" link " . escapeshellarg($random_mac));
224
				$optcfg['spoofmac'] = $random_mac;
225
				write_config();
226
				file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$optcfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
227
			}
228
		}
229

    
230
		/* media */
231
		if ($optcfg['media'] || $optcfg['mediaopt']) {
232
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
233
			if ($optcfg['media'])
234
				$cmd .= " media " . escapeshellarg($optcfg['media']);
235
			if ($optcfg['mediaopt'])
236
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
237
			mwexec($cmd);
238
		}
239

    
240
		/* OpenVPN configuration? */
241
 		if (isset($optcfg['ovpn'])) {
242
 			if (strstr($optcfg['if'], "tap"))
243
 				ovpn_link_tap();
244
 		}
245

    
246
		/* bridged? */
247
		if ($optcfg['bridge']) {
248
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
249
                        /* use open/netBSD style bridge */
250
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
251
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up");
252
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}");
253
			
254
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
255
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
256
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
257
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}\n");
258
			fclose($fd);
259
			
260
			/* lets keep track of the amount of bridges initialized */
261
			$bridges_total++;
262
		} else {
263
			/* if user has selected DHCP type then act accordingly */
264
			if($optcfg['type'] == 'DHCP') {
265
				interfaces_opt_dhcp_configure($opti);
266
			} else {			
267
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
268
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
269
			}
270
		}
271
	} else {
272
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
273
	}
274

    
275
	if ($g['booting']) {
276
		unmute_kernel_msgs();
277
		print "done.\n";
278
	}
279

    
280
	return 0;
281
}
282

    
283
function interfaces_carp_configure() {
284
	global $g, $config;
285
	if ($g['booting']) {
286
		echo "Configuring CARP interfaces...";
287
		mute_kernel_msgs();
288
	}
289
	unlink_if_exists("/usr/local/etc/rc.d/carp.sh");
290
	unlink_if_exists("/usr/local/pkg/pf/carp.sh");
291
	unlink_if_exists("/usr/local/pkg/pf/carp_rules.sh");
292
	$carp_instances_counter = 0;
293
	$pfsync_instances_counter = 0;
294
	if (is_array($config['virtualip']['vip'])) {
295
		if(is_array($config['installedpackages']['carpsettings']['config'])) {
296
			foreach($config['installedpackages']['carpsettings']['config'] as $carp)
297
			if($carp['pfsyncenabled'] != "") {
298
				if($carp['premption'] != "")
299
				mwexec("/sbin/sysctl net.inet.carp.preempt=1");
300
				if($carp['balancing'] != "")
301
				mwexec("/sbin/sysctl net.inet.arpbalance=1");
302
				$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
303
				mwexec("/sbin/ifconfig pfsync0 create");
304
				mwexec("/sbin/ifconfig pfsync0 syncdev " . $carp_sync_int);
305
				mwexec("/sbin/ifconfig pfsync0 syncif " . $carp_sync_int);
306
				mwexec("/sbin/ifconfig {$carp_sync_int} up");
307
				mwexec("/sbin/ifconfig pfsync0 up");
308
				if($g['booting']) {
309
					/* install rules to alllow pfsync to sync up during boot
310
					* carp interfaces will remain down until the bootup sequence finishes
311
					*/
312
					exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
313
					exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
314
					exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
315
					exec("/sbin/pfctl -f /tmp/rules.boot");
316
				}
317
				$pfsync_instances_counter++;
318
			}
319
		}
320
		$viparr = &$config['virtualip']['vip'];
321
		foreach ($viparr as $vip) {
322
			if ($vip['mode'] == "carp") {
323
				/*
324
				*  create the carp interface
325
				*/
326
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
327
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " down");
328
				$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
329
				if($vip['password'] != "") {
330
					$password = " pass " . $vip['password'];
331
				}
332
				/* XXX: billm - carpdev not in our build?
333
				$carpdev = "";
334
				if(isset($vip['interface']) && ($vip['interface'] != "AUTO" && $vip['interface'] != "")) {
335
					$ci = filter_opt_interface_to_real($vip['interface']);
336
					$carpdev = " carpdev {$ci} ";
337
				}
338
				*/
339
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
340
				$carp_instances_counter++;
341
			}
342
		}
343
	}
344
	unmute_kernel_msgs();
345
	if ($g['booting']) {
346
		unmute_kernel_msgs();
347
		echo "done.\n";
348
	}
349
}
350

    
351
function interfaces_carp_bringup() {
352
	global $g;
353
	/* lets bring the carp interfaces up now */
354
	if ($g['booting'])
355
		sleep(1);
356
	$carp_ints = find_number_of_created_carp_interfaces();
357
	for($x=0; $x<$carp_ints; $x++)
358
		mwexec("/sbin/ifconfig carp{$x} up");	
359
}
360

    
361
function interfaces_wireless_configure($if, $wlcfg) {
362
        global $config, $g;
363
	
364
	/*   set wireless channel value.  if we're using 0 then
365
	 *   convert the channel to -
366
	 */
367
	$channel = escapeshellarg($wlcfg['channel']);
368
	if($channel == "") 
369
		$channel = "";
370

    
371
        /* wireless configuration */
372
        $ifcargs = escapeshellarg($if) .
373
                " ssid " . escapeshellarg($wlcfg['ssid']) . " channel {$channel} ";
374

    
375
        if ($wlcfg['stationname'])
376
                $ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
377

    
378
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
379
                $ifcargs .= "wepmode on ";
380

    
381
                $i = 1;
382
                foreach ($wlcfg['wep']['key'] as $wepkey) {
383
                        $ifcargs .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
384
                        if (isset($wepkey['txkey'])) {
385
                                $ifcargs .= "weptxkey {$i} ";
386
                        }
387
                        $i++;
388
                }
389
        } else {
390
                $ifcargs .= "wepmode off ";
391
        }
392

    
393
        if (strstr($if, "ath")) {
394
                if ($wlcfg['standard'])
395
                        $ifcargs .= "mode {$wlcfg['standard']} ";
396
        }
397

    
398
        switch ($wlcfg['mode']) {
399
                case 'hostap':
400
                        if (strstr($if, "ath"))
401
                                $ifcargs .= "-mediaopt adhoc mediaopt hostap ";
402
                        else if (strstr($if, "wi"))
403
                                $ifcargs .= "-mediaopt ibss mediaopt hostap ";
404
                        break;
405
                case 'ibss':
406
                case 'IBSS':
407
                        if (strstr($if, "ath"))
408
                                $ifcargs .= "-mediaopt hostap mediaopt adhoc ";
409
                        else if (strstr($if, "wi"))
410
                                $ifcargs .= "-mediaopt hostap mediaopt ibss ";
411
                        else if (strstr($if, "an"))
412
                                $ifcargs .= "mediaopt adhoc ";
413
                        break;
414
                case 'bss':
415
                case 'BSS':
416
                        if (strstr($if, "ath"))
417
                                $ifcargs .= "-mediaopt hostap -mediaopt adhoc ";
418
                        else if (strstr($if, "wi"))
419
                                $ifcargs .= "-mediaopt hostap -mediaopt ibss ";
420
                        else if (strstr($if, "an"))
421
                                $ifcargs .= "-mediaopt adhoc ";
422
                        break;
423
        }
424
	
425
	/*   extra options during hostap mode
426
	 */
427
	if($wlcfg['mode'] == "hostap") {
428
		/* handle hide ssid option */
429
		if(isset($wlcfg['hidessid']))
430
			$ifcargs .= "hidessid ";
431
		else
432
			$ifcargs .= "-hidessid ";
433
		/* handle pureg (802.11g) only option */
434
		if(isset($wlcfg['pureg']))
435
			$ifcargs .= "pureg ";
436
		else
437
			$ifcargs .= "-pureg ";
438
	}
439

    
440
        $ifcargs .= "up";
441

    
442
        mwexec("/sbin/ifconfig " . $ifcargs);
443

    
444
	$fd = fopen("{$g['tmp_path']}/ifconfig_wireless", "w");
445
	fwrite($fd, "/sbin/ifconfig {$ifcargs}");
446
	fclose($fd);
447
	
448
	if($wlcfg['txpower'] <> "")
449
		mwexec("/sbin/ifconfig {$ifcargs} txpower {$wlcfg['txpower']}");
450
	
451
        return 0;
452

    
453
}
454

    
455
function find_dhclient_process($interface) {
456
	$pid = `ps ax | grep dhclient | grep {$interface} | cut -f" " -d2`;
457
	return $pid;
458
}
459

    
460
function interfaces_wan_configure() {
461
	global $config, $g;
462

    
463
	$wancfg = $config['interfaces']['wan'];
464

    
465
	if(!$g['booting']) {
466
		mute_kernel_msgs();
467

    
468
		/* find dhclient process for wan and kill it */
469
		killbypid(find_dhclient_process("wan"));
470

    
471
		/* kill PPPoE client (mpd) */
472
		killbypid("{$g['varrun_path']}/mpd.pid");
473

    
474
		/* wait for processes to die */
475
		sleep(1);
476

    
477
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
478
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
479
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
480
		unlink_if_exists("{$g['vardb_path']}/wanip");
481
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
482
	}
483

    
484
	/* remove all addresses first */
485
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
486
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
487

    
488
	/* wireless configuration? */
489
	if (is_array($wancfg['wireless']))
490
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
491

    
492
	if ($wancfg['spoofmac']) {
493
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
494
			" link " . escapeshellarg($wancfg['spoofmac']));
495
	}  else {
496
		$mac = get_interface_mac_address($wancfg['if']);
497
		if($mac == "ff:ff:ff:ff:ff:ff") {
498
			/*   this is not a valid mac address.  generate a
499
			 *   temporary mac address so the machine can get online.
500
			 */
501
			echo "Generating new MAC address.";
502
			$random_mac = generate_random_mac_address();
503
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
504
				" link " . escapeshellarg($random_mac));
505
			$wancfg['spoofmac'] = $random_mac;
506
			write_config();
507
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$wancfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
508
		}
509
	}
510

    
511
	/* media */
512
	if ($wancfg['media'] || $wancfg['mediaopt']) {
513
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
514
		if ($wancfg['media'])
515
			$cmd .= " media " . escapeshellarg($wancfg['media']);
516
		if ($wancfg['mediaopt'])
517
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
518
		mwexec($cmd);
519
	}
520

    
521
	switch ($wancfg['ipaddr']) {
522

    
523
		case 'dhcp':
524
			interfaces_wan_dhcp_configure();
525
			break;
526

    
527
		case 'pppoe':
528
			interfaces_wan_pppoe_configure();
529
			break;
530

    
531
		case 'pptp':
532
			interfaces_wan_pptp_configure();
533
			break;
534

    
535
		case 'bigpond':
536
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
537
			interfaces_wan_dhcp_configure();
538
			break;
539

    
540
		default:
541
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
542
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
543
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
544
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
545
			} else {
546
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
547
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
548
			}
549
			/* install default route */
550
			mwexec("/sbin/route delete default");
551
			mwexec("/sbin/route add default " . escapeshellarg($config['system']['gateway']));
552

    
553
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
554
			filter_configure();
555
	}
556

    
557
	if (!$g['booting']) {
558
		/* reconfigure static routes (kernel may have deleted them) */
559
		system_routing_configure();
560

    
561
		/* set the reload filter dity flag */
562
		touch("{$g['tmp_path']}/filter_dirty");
563

    
564
		/* reload ipsec tunnels */
565
		vpn_ipsec_configure();
566

    
567
		/* restart ez-ipupdate */
568
		services_dyndns_configure();
569

    
570
		/* force DNS update */
571
		services_dnsupdate_process();
572

    
573
		/* restart dnsmasq */
574
		services_dnsmasq_configure();
575
	}
576

    
577
	unmute_kernel_msgs();
578

    
579
	return 0;
580
}
581

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

    
585
	$optcfg = $config['interfaces'][$interface];
586

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

    
594
 	$dhclientconf = "";
595

    
596
 	if ($optcfg['dhcphostname']) {
597
		$dhclientconf .= <<<EOD
598
interface "{$optcfg['if']}" {
599
	send dhcp-client-identifier "{$optcfg['dhcphostname']}";
600
	send host-name "{$optcfg['dhcphostname']}";
601
	script "/etc/dhclient-script";
602
}
603

    
604
EOD;
605
	}
606

    
607
	fwrite($fd, $dhclientconf);
608
	fclose($fd);
609
	
610
	$optif = $optcfg['if'];
611
	
612
        /* bring wan interface up before starting dhclient */
613
        mwexec("/sbin/ifconfig {$optif} up");
614

    
615
        /* fire up dhclient */
616
        mwexec_bg("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$optif}");
617

    
618
	return 0;
619
}
620

    
621
function interfaces_wan_dhcp_configure() {
622
	global $config, $g;
623

    
624
	$wancfg = $config['interfaces']['wan'];
625

    
626
	/* generate dhclient_wan.conf */
627
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
628
	if (!$fd) {
629
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
630
		return 1;
631
	}
632

    
633
 	$dhclientconf = "";
634

    
635
 	if ($wancfg['dhcphostname']) {
636
		$dhclientconf .= <<<EOD
637
interface "{$wancfg['if']}" {
638
	send dhcp-client-identifier "{$wancfg['dhcphostname']}";
639
	send host-name "{$wancfg['dhcphostname']}";
640
	script "/etc/dhclient-script";
641
}
642

    
643
EOD;
644
	}
645

    
646
	fwrite($fd, $dhclientconf);
647
	fclose($fd);
648
	
649
	$wanif = $wancfg['if'];
650
	
651
        /* bring wan interface up before starting dhclient */
652
        mwexec("/sbin/ifconfig {$wanif} up");
653

    
654
        /* fire up dhclient */
655
        mwexec_bg("/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
656

    
657
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
658
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
659
	fclose($fout);
660

    
661
	return 0;
662
}
663

    
664
function interfaces_wan_dhcp_down() {
665
	global $config;
666
	$wancfg = $config['interfaces']['wan'];
667
	$wanif = $wancfg['if'];
668
	mwexec("/sbin/dhclient -r");
669
	mwexec("/sbin/ifconfig {$wanif} delete");
670
	sleep(1);
671
}
672

    
673
function interfaces_wan_dhcp_up() {
674
	interfaces_wan_dhcp_configure();
675
	sleep(1);
676
}
677

    
678
function interfaces_wan_pppoe_configure() {
679
	global $config, $g;
680

    
681
	$wancfg = $config['interfaces']['wan'];
682
	$pppoecfg = $config['pppoe'];
683

    
684
	/* generate mpd.conf */
685
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
686
	if (!$fd) {
687
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
688
		return 1;
689
	}
690

    
691
	$idle = 0;
692

    
693
	if (isset($pppoecfg['ondemand'])) {
694
		$ondemand = "enable";
695
		if ($pppoecfg['timeout'])
696
			$idle = $pppoecfg['timeout'];
697
	} else {
698
		$ondemand = "disable";
699
	}
700

    
701
	$mpdconf = <<<EOD
702
pppoe:
703
	new -i ng0 pppoe pppoe
704
	set iface route default
705
	set iface {$ondemand} on-demand
706
	set iface idle {$idle}
707
	set iface up-script /usr/local/sbin/ppp-linkup
708

    
709
EOD;
710

    
711
	if (isset($pppoecfg['ondemand'])) {
712
		$mpdconf .= <<<EOD
713
	set iface addrs 10.0.0.1 10.0.0.2
714

    
715
EOD;
716
	}
717

    
718
	$mpdconf .= <<<EOD
719
	set bundle disable multilink
720
	set bundle authname "{$pppoecfg['username']}"
721
	set bundle password "{$pppoecfg['password']}"
722
	set link keep-alive 10 60
723
	set link max-redial 0
724
	set link no acfcomp protocomp
725
	set link disable pap chap
726
	set link accept chap
727
	set link mtu 1492
728
	set ipcp yes vjcomp
729
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
730

    
731
EOD;
732

    
733
	if (isset($config['system']['dnsallowoverride'])) {
734
		$mpdconf .= <<<EOD
735
	set ipcp enable req-pri-dns
736
	set ipcp enable req-sec-dns
737

    
738
EOD;
739
	}
740

    
741
	$mpdconf .= <<<EOD
742
	open iface
743

    
744
EOD;
745

    
746
	fwrite($fd, $mpdconf);
747
	fclose($fd);
748

    
749
	/* generate mpd.links */
750
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
751
	if (!$fd) {
752
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
753
		return 1;
754
	}
755

    
756
	$mpdconf = <<<EOD
757
pppoe:
758
	set link type pppoe
759
	set pppoe iface {$wancfg['if']}
760
	set pppoe service "{$pppoecfg['provider']}"
761
	set pppoe enable originate
762
	set pppoe disable incoming
763

    
764
EOD;
765

    
766
	fwrite($fd, $mpdconf);
767
	fclose($fd);
768

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

    
772
	return 0;
773
}
774

    
775
function interfaces_wan_pppoe_down() {
776
	global $g;
777
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
778
	sleep(1);
779
}
780

    
781
function interfaces_wan_pppoe_up() {
782
	global $g;
783
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
784
	sleep(1);
785
}
786

    
787
function interfaces_wan_pptp_configure() {
788
	global $config, $g;
789

    
790
	$wancfg = $config['interfaces']['wan'];
791
	$pptpcfg = $config['pptp'];
792

    
793
	/* generate mpd.conf */
794
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
795
	if (!$fd) {
796
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
797
		return 1;
798
	}
799

    
800
	$idle = 0;
801

    
802
	if (isset($pptpcfg['ondemand'])) {
803
		$ondemand = "enable";
804
		if ($pptpcfg['timeout'])
805
			$idle = $pptpcfg['timeout'];
806
	} else {
807
		$ondemand = "disable";
808
	}
809

    
810
	$mpdconf = <<<EOD
811
pptp:
812
	new -i ng0 pptp pptp
813
	set iface route default
814
	set iface {$ondemand} on-demand
815
	set iface idle {$idle}
816
	set iface up-script /usr/local/sbin/ppp-linkup
817

    
818
EOD;
819

    
820
	if (isset($pptpcfg['ondemand'])) {
821
		$mpdconf .= <<<EOD
822
	set iface addrs 10.0.0.1 10.0.0.2
823

    
824
EOD;
825
	}
826

    
827
	$mpdconf .= <<<EOD
828
	set bundle disable multilink
829
	set bundle authname "{$pptpcfg['username']}"
830
	set bundle password "{$pptpcfg['password']}"
831
	set link keep-alive 10 60
832
	set link max-redial 0
833
	set link no acfcomp protocomp
834
	set link disable pap chap
835
	set link accept chap
836
	set ipcp no vjcomp
837
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
838

    
839
EOD;
840

    
841
	if (isset($config['system']['dnsallowoverride'])) {
842
		$mpdconf .= <<<EOD
843
	set ipcp enable req-pri-dns
844
	set ipcp enable req-sec-dns
845

    
846
EOD;
847
	}
848

    
849
	$mpdconf .= <<<EOD
850
	open
851

    
852
EOD;
853

    
854
	fwrite($fd, $mpdconf);
855
	fclose($fd);
856

    
857
	/* generate mpd.links */
858
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
859
	if (!$fd) {
860
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
861
		return 1;
862
	}
863

    
864
	$mpdconf = <<<EOD
865
pptp:
866
	set link type pptp
867
	set pptp enable originate outcall
868
	set pptp disable windowing
869
	set pptp self {$pptpcfg['local']}
870
	set pptp peer {$pptpcfg['remote']}
871

    
872
EOD;
873

    
874
	fwrite($fd, $mpdconf);
875
	fclose($fd);
876

    
877
	/* configure interface */
878
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
879
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
880

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

    
884
	return 0;
885
}
886

    
887
function interfaces_wan_pptp_down() {
888
	global $g;
889
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
890
	sleep(1);
891
}
892

    
893
function interfaces_wan_pptp_up() {
894
	global $g;
895
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
896
	sleep(1);
897
}
898

    
899
function interfaces_wan_bigpond_configure($curwanip) {
900
	global $config, $g;
901

    
902
	$bpcfg = $config['bigpond'];
903

    
904
	if (!$curwanip) {
905
		/* IP address not configured yet, exit */
906
		return 0;
907
	}
908

    
909
	/* kill bpalogin */
910
	killbyname("bpalogin");
911

    
912
	/* wait a moment */
913
	sleep(1);
914

    
915
	/* get the default domain */
916
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
917
	if ($nfd) {
918
		$defaultdomain = trim(fgets($nfd));
919
		fclose($nfd);
920
	}
921

    
922
	/* generate bpalogin.conf */
923
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
924
	if (!$fd) {
925
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
926
		return 1;
927
	}
928

    
929
	if (!$bpcfg['authserver'])
930
		$bpcfg['authserver'] = "dce-server";
931
	if (!$bpcfg['authdomain'])
932
		$bpcfg['authdomain'] = $defaultdomain;
933

    
934
	$bpconf = <<<EOD
935
username {$bpcfg['username']}
936
password {$bpcfg['password']}
937
authserver {$bpcfg['authserver']}
938
authdomain {$bpcfg['authdomain']}
939
localport 5050
940

    
941
EOD;
942

    
943
	if ($bpcfg['minheartbeatinterval'])
944
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
945

    
946
	fwrite($fd, $bpconf);
947
	fclose($fd);
948

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

    
952
	return 0;
953
}
954

    
955
function get_real_wan_interface() {
956
	global $config, $g;
957

    
958
	$wancfg = $config['interfaces']['wan'];
959

    
960
	$wanif = $wancfg['if'];
961
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
962
		$wanif = $g['pppoe_interface'];
963
	}
964

    
965
	return $wanif;
966
}
967

    
968
function get_current_wan_address() {
969
	global $config, $g;
970

    
971
	$wancfg = $config['interfaces']['wan'];
972

    
973
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
974
		/* dynamic WAN IP address, find out which one */
975
		$wanif = get_real_wan_interface();
976

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

    
980
		if (isset($ifinfo[1])) {
981
			$aif = preg_split("/\s+/", $ifinfo[1]);
982
			$curwanip = chop($aif[3]);
983

    
984
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
985
				return $curwanip;
986
		}
987

    
988
		return null;
989
	} else {
990
		/* static WAN IP address */
991
		return $wancfg['ipaddr'];
992
	}
993
}
994

    
995
?>
(8-8/22)