Project

General

Profile

Download (29.8 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
	/* bridged? */
116
	$bridges_total=0;
117
	if ($lancfg['bridge']) {
118
		// mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " delete up");
119
		/* use open/netBSD style bridge */
120
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
121
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up");
122
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}");
123
		
124
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
125
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
126
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up\n");
127
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}\n");
128
		fclose($fd);
129
		
130
		/* lets keep track of the amount of bridges initialized */
131
		$bridges_total++;
132
	}
133
	
134
	/* media */
135
	if ($lancfg['media'] || $lancfg['mediaopt']) {
136
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
137
		if ($lancfg['media'])
138
			$cmd .= " media " . escapeshellarg($lancfg['media']);
139
		if ($lancfg['mediaopt'])
140
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
141
		mwexec($cmd);
142
	}
143

    
144
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
145
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
146

    
147
	if (!$g['booting']) {
148
		/* make new hosts file */
149
		system_hosts_generate();
150

    
151
		/* reconfigure static routes (kernel may have deleted them) */
152
		system_routing_configure();
153

    
154
		/* set the reload filter dity flag */
155
		touch("{$g['tmp_path']}/filter_dirty");
156

    
157
		/* reload IPsec tunnels */
158
		vpn_ipsec_configure();
159

    
160
		/* reload dhcpd (gateway may have changed) */
161
		services_dhcpd_configure();
162

    
163
		/* reload dnsmasq */
164
		services_dnsmasq_configure();
165

    
166
		/* reload webgui */
167
		system_webgui_start();
168

    
169
		/* reload captive portal */
170
		captiveportal_configure();
171
	}
172

    
173
	return 0;
174
}
175

    
176
function interfaces_optional_configure() {
177
	global $config, $g;
178
	global $bridgeconfig;
179

    
180
	/* Reset bridge configuration.	Interfaces will add to it. */
181
	$bridgeconfig = "";
182

    
183
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
184
		interfaces_optional_configure_if($i);
185
	}
186

    
187
	if (!$g['booting']) {
188
		/* reconfigure static routes (kernel may have deleted them) */
189
		system_routing_configure();
190

    
191
		/* reload IPsec tunnels */
192
		vpn_ipsec_configure();
193

    
194
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
195
		services_dhcpd_configure();
196

    
197
		/* restart dnsmasq */
198
		services_dnsmasq_configure();
199

    
200
		/* set the reload filter dity flag */
201
		touch("{$g['tmp_path']}/filter_dirty");				
202
	}
203

    
204
	return 0;
205
}
206

    
207
function interfaces_optional_configure_if($opti) {
208
	global $config, $g;
209
	global $bridgeconfig;
210
	global $bridges_total;
211
	
212
	if(!is_numeric($bridges_total)) $bridges_total=0;
213

    
214
	$optcfg = $config['interfaces']['opt' . $opti];
215

    
216
	if ($g['booting']) {
217
		$optdescr = "";
218
		if ($optcfg['descr'])
219
			$optdescr = " ({$optcfg['descr']})";
220
		print "\tOPT{$opti}{$optdescr}... ";
221
		mute_kernel_msgs();
222
	}
223

    
224
	if (isset($optcfg['enable'])) {
225
		/* wireless configuration? */
226
		if (is_array($optcfg['wireless']))
227
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
228

    
229
		/* MAC spoofing? */
230
		if ($optcfg['spoofmac']) {
231
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
232
				" link " . escapeshellarg($optcfg['spoofmac']));
233
		} else {
234
			$mac = get_interface_mac_address($optcfg['if']);
235
			if($mac == "ff:ff:ff:ff:ff:ff") {
236
				/*   this is not a valid mac address.  generate a
237
				 *   temporary mac address so the machine can get online.
238
				 */
239
				echo "Generating new MAC address.";
240
				$random_mac = generate_random_mac_address();
241
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
242
					" link " . escapeshellarg($random_mac));
243
				$optcfg['spoofmac'] = $random_mac;
244
				write_config();
245
				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");
246
			}
247
		}
248

    
249
		/* media */
250
		if ($optcfg['media'] || $optcfg['mediaopt']) {
251
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
252
			if ($optcfg['media'])
253
				$cmd .= " media " . escapeshellarg($optcfg['media']);
254
			if ($optcfg['mediaopt'])
255
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
256
			mwexec($cmd);
257
		}
258

    
259
		/* OpenVPN configuration? */
260
 		if (isset($optcfg['ovpn'])) {
261
 			if (strstr($optcfg['if'], "tap"))
262
 				ovpn_link_tap();
263
 		}
264

    
265
		/* bridged? */
266
		if ($optcfg['bridge']) {
267
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
268
                        /* use open/netBSD style bridge */
269
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
270
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up");
271
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}");
272
			
273
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
274
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
275
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
276
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}\n");
277
			fclose($fd);
278
			
279
			/* lets keep track of the amount of bridges initialized */
280
			$bridges_total++;
281
		} else {
282
			/* if user has selected DHCP type then act accordingly */
283
			if($optcfg['ipaddr'] == "dhcp") {
284
				interfaces_opt_dhcp_configure("opt{$opti}");
285
			} else {			
286
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
287
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
288
			}
289
		}
290
	} else {
291
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
292
	}
293

    
294
	return 0;
295
}
296

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

    
371
function interfaces_carp_bringup() {
372
	global $g;
373
	/* lets bring the carp interfaces up now */
374
	if ($g['booting'])
375
		sleep(1);
376
	$carp_ints = find_number_of_created_carp_interfaces();
377
	for($x=0; $x<$carp_ints; $x++)
378
		mwexec("/sbin/ifconfig carp{$x} up");	
379
}
380

    
381
function interfaces_wireless_configure($if, $wlcfg) {
382
        global $config, $g;
383
	
384
	/*   set wireless channel value.  if we're using 0 then
385
	 *   convert the channel to -
386
	 */
387
	$channel = escapeshellarg($wlcfg['channel']);
388
	if($channel == "") 
389
		$channel = "";
390

    
391
        /* wireless configuration */
392
        $ifcargs = escapeshellarg($if) .
393
                " ssid " . escapeshellarg($wlcfg['ssid']) . " channel {$channel} ";
394

    
395
        if ($wlcfg['stationname'])
396
                $ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
397

    
398
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
399
                $ifcargs .= "wepmode on ";
400

    
401
                $i = 1;
402
                foreach ($wlcfg['wep']['key'] as $wepkey) {
403
                        $ifcargs .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
404
                        if (isset($wepkey['txkey'])) {
405
                                $ifcargs .= "weptxkey {$i} ";
406
                        }
407
                        $i++;
408
                }
409
        } else {
410
                $ifcargs .= "wepmode off ";
411
        }
412

    
413
	if (preg_match($g['wireless_regex'], $if)) {
414
                if ($wlcfg['standard'])
415
                        $ifcargs .= "mode {$wlcfg['standard']} ";
416
        }
417

    
418
        switch ($wlcfg['mode']) {
419
                case 'hostap':
420
                        if (preg_match($g['wireless_regex'], $if)) 
421
                                $ifcargs .= "-mediaopt adhoc mediaopt hostap ";
422
                        else if (strstr($if, "wi"))
423
                                $ifcargs .= "-mediaopt ibss mediaopt hostap ";
424
                        break;
425
                case 'ibss':
426
                case 'IBSS':
427
                        if (preg_match($g['wireless_regex'], $if)) 
428
                                $ifcargs .= "-mediaopt hostap mediaopt adhoc ";
429
                        else if (strstr($if, "wi"))
430
                                $ifcargs .= "-mediaopt hostap mediaopt ibss ";
431
                        else if (strstr($if, "an"))
432
                                $ifcargs .= "mediaopt adhoc ";
433
                        break;
434
                case 'bss':
435
                case 'BSS':
436
                        if (preg_match($g['wireless_regex'], $if)) 
437
                                $ifcargs .= "-mediaopt hostap -mediaopt adhoc ";
438
                        else if (strstr($if, "wi"))
439
                                $ifcargs .= "-mediaopt hostap -mediaopt ibss ";
440
                        else if (strstr($if, "an"))
441
                                $ifcargs .= "-mediaopt adhoc ";
442
                        break;
443
        }
444
	
445
	/*   extra options during hostap mode
446
	 */
447
	if($wlcfg['mode'] == "hostap") {
448
		/* handle hide ssid option */
449
		if(isset($wlcfg['hidessid']))
450
			$ifcargs .= "hidessid ";
451
		else
452
			$ifcargs .= "-hidessid ";
453
		/* handle pureg (802.11g) only option */
454
		if(isset($wlcfg['pureg']))
455
			$ifcargs .= "pureg ";
456
		else
457
			$ifcargs .= "-pureg ";
458
	}
459

    
460
        $ifcargs .= "up";
461

    
462
        mwexec("/sbin/ifconfig " . $ifcargs);
463

    
464
	$fd = fopen("{$g['tmp_path']}/ifconfig_wireless", "w");
465
	fwrite($fd, "/sbin/ifconfig {$ifcargs}");
466
	fclose($fd);
467
	
468
	if($wlcfg['txpower'] <> "")
469
		mwexec("/sbin/ifconfig {$ifcargs} txpower {$wlcfg['txpower']}");
470
	
471
	if(isset($wlcfg['useolsr']))
472
		setup_wireless_olsr(escapeshellarg($if));
473
	
474
        return 0;
475

    
476
}
477

    
478
function find_dhclient_process($interface) {
479
	$pid = `ps ax | grep dhclient | grep {$interface} | cut -f" " -d2`;
480
	return $pid;
481
}
482

    
483
function interfaces_wan_configure() {
484
	global $config, $g;
485

    
486
	$wancfg = $config['interfaces']['wan'];
487

    
488
	if(!$g['booting']) {
489
		mute_kernel_msgs();
490

    
491
		/* find dhclient process for wan and kill it */
492
		killbypid(find_dhclient_process("wan"));
493

    
494
		/* kill PPPoE client (mpd) */
495
		killbypid("{$g['varrun_path']}/mpd.pid");
496

    
497
		/* wait for processes to die */
498
		sleep(1);
499

    
500
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
501
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
502
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
503
		unlink_if_exists("{$g['vardb_path']}/wanip");
504
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
505
	}
506

    
507
	/* remove all addresses first */
508
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
509
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
510

    
511
	/* wireless configuration? */
512
	if (is_array($wancfg['wireless']))
513
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
514

    
515
	if ($wancfg['spoofmac']) {
516
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
517
			" link " . escapeshellarg($wancfg['spoofmac']));
518
	}  else {
519
		$mac = get_interface_mac_address($wancfg['if']);
520
		if($mac == "ff:ff:ff:ff:ff:ff") {
521
			/*   this is not a valid mac address.  generate a
522
			 *   temporary mac address so the machine can get online.
523
			 */
524
			echo "Generating new MAC address.";
525
			$random_mac = generate_random_mac_address();
526
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
527
				" link " . escapeshellarg($random_mac));
528
			$wancfg['spoofmac'] = $random_mac;
529
			write_config();
530
			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");
531
		}
532
	}
533

    
534
	/* media */
535
	if ($wancfg['media'] || $wancfg['mediaopt']) {
536
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
537
		if ($wancfg['media'])
538
			$cmd .= " media " . escapeshellarg($wancfg['media']);
539
		if ($wancfg['mediaopt'])
540
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
541
		mwexec($cmd);
542
	}
543

    
544
	switch ($wancfg['ipaddr']) {
545

    
546
		case 'dhcp':
547
			interfaces_wan_dhcp_configure();
548
			break;
549

    
550
		case 'pppoe':
551
			interfaces_wan_pppoe_configure();
552
			break;
553

    
554
		case 'pptp':
555
			interfaces_wan_pptp_configure();
556
			break;
557

    
558
		case 'bigpond':
559
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
560
			interfaces_wan_dhcp_configure();
561
			break;
562

    
563
		default:
564
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
565
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
566
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
567
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
568
			} else {
569
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
570
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
571
			}
572
			/* install default route */
573
			mwexec("/sbin/route delete default");
574
			mwexec("/sbin/route add default " . escapeshellarg($config['system']['gateway']));
575

    
576
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
577
			filter_configure();
578
	}
579

    
580
	if (!$g['booting']) {
581
		/* reconfigure static routes (kernel may have deleted them) */
582
		system_routing_configure();
583

    
584
		/* set the reload filter dity flag */
585
		touch("{$g['tmp_path']}/filter_dirty");
586

    
587
		/* reload ipsec tunnels */
588
		vpn_ipsec_configure();
589

    
590
		/* restart ez-ipupdate */
591
		services_dyndns_configure();
592

    
593
		/* force DNS update */
594
		services_dnsupdate_process();
595

    
596
		/* restart dnsmasq */
597
		services_dnsmasq_configure();
598
	}
599

    
600
	unmute_kernel_msgs();
601

    
602
	return 0;
603
}
604

    
605
function interfaces_opt_dhcp_configure($interface) {
606
	global $config, $g;
607

    
608
	$optcfg = $config['interfaces'][$interface];
609
	$optif = $optcfg['if'];
610

    
611
	/* generate dhclient_wan.conf */
612
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
613
	if (!$fd) {
614
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
615
		return 1;
616
	}
617

    
618
 	$dhclientconf = "";
619

    
620
 	if ($optcfg['dhcphostname']) {
621
		$dhclientconf .= <<<EOD
622
	interface "{$optif}" {
623
	send dhcp-client-identifier "{$optcfg['dhcphostname']}";
624
	send host-name "{$optcfg['dhcphostname']}";
625
	script "/sbin/dhclient-script";
626
}
627

    
628
EOD;
629
	}
630

    
631
	fwrite($fd, $dhclientconf);
632
	fclose($fd);
633

    
634
        /* bring interface up before starting dhclient */
635
        mwexec("/sbin/ifconfig {$optif} up");
636

    
637
        /* fire up dhclient */
638
        mwexec("/sbin/dhclient -b -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
639

    
640
	return 0;
641
}
642

    
643
function interfaces_dhcp_configure($interface) {
644
	global $config, $g;
645

    
646
	$optcfg = $config['interfaces'][$interface];
647

    
648
	/* generate dhclient_$interface.conf */
649
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
650
	if (!$fd) {
651
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
652
		return 1;
653
	}
654

    
655
 	$dhclientconf = "";
656

    
657
 	if ($optcfg['dhcphostname']) {
658
		$dhclientconf .= <<<EOD
659
interface "{$optcfg['if']}" {
660
	send dhcp-client-identifier "{$optcfg['dhcphostname']}";
661
	send host-name "{$optcfg['dhcphostname']}";
662
	script "/sbin/dhclient-script";
663
}
664

    
665
EOD;
666
	}
667

    
668
	fwrite($fd, $dhclientconf);
669
	fclose($fd);
670
	
671
	$optif = $optcfg['if'];
672
	
673
        /* bring wan interface up before starting dhclient */
674
        mwexec("/sbin/ifconfig {$optif} up");
675

    
676
        /* fire up dhclient */
677
        mwexec("/sbin/dhclient -b -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
678

    
679
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
680
	fwrite($fout, "/sbin/dhclient -b -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
681
	fclose($fout);
682

    
683
	return 0;
684
}
685

    
686
function interfaces_wan_dhcp_configure() {
687
	global $config, $g;
688

    
689
	$wancfg = $config['interfaces']['wan'];
690

    
691
	/* generate dhclient_wan.conf */
692
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
693
	if (!$fd) {
694
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
695
		return 1;
696
	}
697

    
698
 	$dhclientconf = "";
699

    
700
 	if ($wancfg['dhcphostname']) {
701
		$dhclientconf .= <<<EOD
702
interface "{$wancfg['if']}" {
703
	send dhcp-client-identifier "{$wancfg['dhcphostname']}";
704
	send host-name "{$wancfg['dhcphostname']}";
705
	script "/sbin/dhclient-script";
706
}
707

    
708
EOD;
709
	}
710

    
711
	fwrite($fd, $dhclientconf);
712
	fclose($fd);
713
	
714
	$wanif = $wancfg['if'];
715
	
716
        /* bring wan interface up before starting dhclient */
717
        mwexec("/sbin/ifconfig {$wanif} up");
718

    
719
        /* fire up dhclient */
720
        mwexec("/sbin/dhclient -b -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
721

    
722
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
723
	fwrite($fout, "/sbin/dhclient -b -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
724
	fclose($fout);
725

    
726
	return 0;
727
}
728

    
729
function interfaces_wan_dhcp_down() {
730
	global $config;
731
	$wancfg = $config['interfaces']['wan'];
732
	$wanif = $wancfg['if'];
733
	mwexec("/sbin/ifconfig {$wanif} delete");
734
	sleep(1);
735
}
736

    
737
function interfaces_dhcp_down($interface) {
738
	global $config;
739
	$wancfg = $config['interfaces']['wan'];
740
	$wanif = $wancfg['if'];
741
	mwexec("/sbin/ifconfig {$interface} down");
742
	sleep(1);
743
	$pid_dhclient = `ps awux | grep dhclient | grep {$interface}`;
744
	if($pid_dhclient)
745
		mwexec("kill {$pid_dhclient}");
746
}
747

    
748
function interfaces_dhcp_up($interface) {
749
	interfaces_dhcp_configure($interface);
750
	sleep(1);
751
}
752

    
753
function interfaces_wan_dhcp_up() {
754
	interfaces_wan_dhcp_configure();
755
	sleep(1);
756
}
757

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

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

    
764
	/* generate mpd.conf */
765
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
766
	if (!$fd) {
767
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
768
		return 1;
769
	}
770

    
771
	$idle = 0;
772

    
773
	if (isset($pppoecfg['ondemand'])) {
774
		$ondemand = "enable";
775
		if ($pppoecfg['timeout'])
776
			$idle = $pppoecfg['timeout'];
777
	} else {
778
		$ondemand = "disable";
779
	}
780

    
781
	$mpdconf = <<<EOD
782
pppoe:
783
	new -i ng0 pppoe pppoe
784
	set iface route default
785
	set iface {$ondemand} on-demand
786
	set iface idle {$idle}
787
	set iface up-script /usr/local/sbin/ppp-linkup
788

    
789
EOD;
790

    
791
	if (isset($pppoecfg['ondemand'])) {
792
		$mpdconf .= <<<EOD
793
	set iface addrs 10.0.0.1 10.0.0.2
794

    
795
EOD;
796
	}
797

    
798
	$mpdconf .= <<<EOD
799
	set bundle disable multilink
800
	set bundle authname "{$pppoecfg['username']}"
801
	set bundle password "{$pppoecfg['password']}"
802
	set link keep-alive 10 60
803
	set link max-redial 0
804
	set link no acfcomp protocomp
805
	set link disable pap chap
806
	set link accept chap
807
	set link mtu 1492
808
	set ipcp yes vjcomp
809
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
810

    
811
EOD;
812

    
813
	if (isset($config['system']['dnsallowoverride'])) {
814
		$mpdconf .= <<<EOD
815
	set ipcp enable req-pri-dns
816
	set ipcp enable req-sec-dns
817

    
818
EOD;
819
	}
820

    
821
	$mpdconf .= <<<EOD
822
	open iface
823

    
824
EOD;
825

    
826
	fwrite($fd, $mpdconf);
827
	fclose($fd);
828

    
829
	/* generate mpd.links */
830
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
831
	if (!$fd) {
832
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
833
		return 1;
834
	}
835

    
836
	$mpdconf = <<<EOD
837
pppoe:
838
	set link type pppoe
839
	set pppoe iface {$wancfg['if']}
840
	set pppoe service "{$pppoecfg['provider']}"
841
	set pppoe enable originate
842
	set pppoe disable incoming
843

    
844
EOD;
845

    
846
	fwrite($fd, $mpdconf);
847
	fclose($fd);
848

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

    
852
	return 0;
853
}
854

    
855
function interfaces_wan_pppoe_down() {
856
	global $g;
857
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
858
	sleep(1);
859
}
860

    
861
function interfaces_wan_pppoe_up() {
862
	global $g;
863
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
864
	sleep(1);
865
}
866

    
867
function interfaces_wan_pptp_configure() {
868
	global $config, $g;
869

    
870
	$wancfg = $config['interfaces']['wan'];
871
	$pptpcfg = $config['pptp'];
872

    
873
	/* generate mpd.conf */
874
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
875
	if (!$fd) {
876
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
877
		return 1;
878
	}
879

    
880
	$idle = 0;
881

    
882
	if (isset($pptpcfg['ondemand'])) {
883
		$ondemand = "enable";
884
		if ($pptpcfg['timeout'])
885
			$idle = $pptpcfg['timeout'];
886
	} else {
887
		$ondemand = "disable";
888
	}
889

    
890
	$mpdconf = <<<EOD
891
pptp:
892
	new -i ng0 pptp pptp
893
	set iface route default
894
	set iface {$ondemand} on-demand
895
	set iface idle {$idle}
896
	set iface up-script /usr/local/sbin/ppp-linkup
897

    
898
EOD;
899

    
900
	if (isset($pptpcfg['ondemand'])) {
901
		$mpdconf .= <<<EOD
902
	set iface addrs 10.0.0.1 10.0.0.2
903

    
904
EOD;
905
	}
906

    
907
	$mpdconf .= <<<EOD
908
	set bundle disable multilink
909
	set bundle authname "{$pptpcfg['username']}"
910
	set bundle password "{$pptpcfg['password']}"
911
	set link keep-alive 10 60
912
	set link max-redial 0
913
	set link no acfcomp protocomp
914
	set link disable pap chap
915
	set link accept chap
916
	set ipcp no vjcomp
917
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
918

    
919
EOD;
920

    
921
	if (isset($config['system']['dnsallowoverride'])) {
922
		$mpdconf .= <<<EOD
923
	set ipcp enable req-pri-dns
924
	set ipcp enable req-sec-dns
925

    
926
EOD;
927
	}
928

    
929
	$mpdconf .= <<<EOD
930
	open
931

    
932
EOD;
933

    
934
	fwrite($fd, $mpdconf);
935
	fclose($fd);
936

    
937
	/* generate mpd.links */
938
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
939
	if (!$fd) {
940
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
941
		return 1;
942
	}
943

    
944
	$mpdconf = <<<EOD
945
pptp:
946
	set link type pptp
947
	set pptp enable originate outcall
948
	set pptp disable windowing
949
	set pptp self {$pptpcfg['local']}
950
	set pptp peer {$pptpcfg['remote']}
951

    
952
EOD;
953

    
954
	fwrite($fd, $mpdconf);
955
	fclose($fd);
956

    
957
	/* configure interface */
958
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
959
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
960

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

    
964
	return 0;
965
}
966

    
967
function interfaces_wan_pptp_down() {
968
	global $g;
969
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
970
	sleep(1);
971
}
972

    
973
function interfaces_wan_pptp_up() {
974
	global $g;
975
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
976
	sleep(1);
977
}
978

    
979
function interfaces_wan_bigpond_configure($curwanip) {
980
	global $config, $g;
981

    
982
	$bpcfg = $config['bigpond'];
983

    
984
	if (!$curwanip) {
985
		/* IP address not configured yet, exit */
986
		return 0;
987
	}
988

    
989
	/* kill bpalogin */
990
	killbyname("bpalogin");
991

    
992
	/* wait a moment */
993
	sleep(1);
994

    
995
	/* get the default domain */
996
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
997
	if ($nfd) {
998
		$defaultdomain = trim(fgets($nfd));
999
		fclose($nfd);
1000
	}
1001

    
1002
	/* generate bpalogin.conf */
1003
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1004
	if (!$fd) {
1005
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1006
		return 1;
1007
	}
1008

    
1009
	if (!$bpcfg['authserver'])
1010
		$bpcfg['authserver'] = "dce-server";
1011
	if (!$bpcfg['authdomain'])
1012
		$bpcfg['authdomain'] = $defaultdomain;
1013

    
1014
	$bpconf = <<<EOD
1015
username {$bpcfg['username']}
1016
password {$bpcfg['password']}
1017
authserver {$bpcfg['authserver']}
1018
authdomain {$bpcfg['authdomain']}
1019
localport 5050
1020

    
1021
EOD;
1022

    
1023
	if ($bpcfg['minheartbeatinterval'])
1024
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1025

    
1026
	fwrite($fd, $bpconf);
1027
	fclose($fd);
1028

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

    
1032
	return 0;
1033
}
1034

    
1035
function get_real_wan_interface() {
1036
	global $config, $g;
1037

    
1038
	$wancfg = $config['interfaces']['wan'];
1039

    
1040
	$wanif = $wancfg['if'];
1041
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1042
		$wanif = $g['pppoe_interface'];
1043
	}
1044

    
1045
	return $wanif;
1046
}
1047

    
1048
function get_current_wan_address() {
1049
	global $config, $g;
1050

    
1051
	$wancfg = $config['interfaces']['wan'];
1052

    
1053
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
1054
		/* dynamic WAN IP address, find out which one */
1055
		$wanif = get_real_wan_interface();
1056

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

    
1060
		if (isset($ifinfo[1])) {
1061
			$aif = preg_split("/\s+/", $ifinfo[1]);
1062
			$curwanip = chop($aif[3]);
1063

    
1064
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1065
				return $curwanip;
1066
		}
1067

    
1068
		return null;
1069
	} else {
1070
		/* static WAN IP address */
1071
		return $wancfg['ipaddr'];
1072
	}
1073
}
1074

    
1075
?>
(8-8/23)