Project

General

Profile

Download (33.5 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 cfc707f7 Scott Ullrich
	Copyright (C) 2004 Scott Ullrich
6
	All rights reserved.
7
8
	originally part of m0n0wall (http://m0n0.ch/wall)
9 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11 cfc707f7 Scott Ullrich
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 cfc707f7 Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 cfc707f7 Scott Ullrich
18 5b237745 Scott Ullrich
	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 cfc707f7 Scott Ullrich
22 5b237745 Scott Ullrich
	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 b1b55ed2 Scott Ullrich
if(!is_numeric($bridges_total)) $bridges_total=0;
38
39 5b237745 Scott Ullrich
function interfaces_loopback_configure() {
40
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
41 cfc707f7 Scott Ullrich
42 5b237745 Scott Ullrich
	return 0;
43
}
44
45
function interfaces_vlan_configure() {
46 669e1adb Bill Marquette
	global $config;
47 cfc707f7 Scott Ullrich
48 5b237745 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
49 cfc707f7 Scott Ullrich
50 5b237745 Scott Ullrich
		/* devices with native VLAN support */
51
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
52 cfc707f7 Scott Ullrich
53 5b237745 Scott Ullrich
		/* devices with long frame support */
54
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
55 cfc707f7 Scott Ullrich
56 5b237745 Scott Ullrich
		$i = 0;
57 cfc707f7 Scott Ullrich
58 5b237745 Scott Ullrich
		foreach ($config['vlans']['vlan'] as $vlan) {
59 cfc707f7 Scott Ullrich
60
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
61
				escapeshellarg($vlan['tag']) . " vlandev " .
62 5b237745 Scott Ullrich
				escapeshellarg($vlan['if']);
63 cfc707f7 Scott Ullrich
64 5b237745 Scott Ullrich
			/* get driver name */
65
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
66
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
67
					break;
68
			}
69
			$drvname = substr($vlan['if'], 0, $j);
70 cfc707f7 Scott Ullrich
71 5b237745 Scott Ullrich
			if (in_array($drvname, $vlan_native_supp))
72
				$cmd .= " link0";
73
			else if (in_array($drvname, $vlan_long_supp))
74
				$cmd .= " mtu 1500";
75 cfc707f7 Scott Ullrich
76 5b237745 Scott Ullrich
			mwexec($cmd);
77 cfc707f7 Scott Ullrich
78 5b237745 Scott Ullrich
			/* make sure the parent interface is up */
79
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
80 cfc707f7 Scott Ullrich
81 5b237745 Scott Ullrich
			$i++;
82
		}
83
	}
84 cfc707f7 Scott Ullrich
85 5b237745 Scott Ullrich
	return 0;
86
}
87
88
function interfaces_lan_configure() {
89 b1b55ed2 Scott Ullrich
	global $config, $g, $bridges_total;
90 cfc707f7 Scott Ullrich
91 5b237745 Scott Ullrich
	$lancfg = $config['interfaces']['lan'];
92 cfc707f7 Scott Ullrich
93 5b237745 Scott Ullrich
	/* wireless configuration? */
94
	if (is_array($lancfg['wireless']))
95
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
96 cfc707f7 Scott Ullrich
97 5b237745 Scott Ullrich
	/* MAC spoofing? */
98 f36d4bd2 Scott Ullrich
	if ($lancfg['spoofmac']) {
99 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
100 5b237745 Scott Ullrich
			" link " . escapeshellarg($lancfg['spoofmac']));
101 f36d4bd2 Scott Ullrich
	} else {
102
		$mac = get_interface_mac_address($lancfg['if']);
103
		if($mac == "ff:ff:ff:ff:ff:ff") {
104
			/*   this is not a valid mac address.  generate a
105
			 *   temporary mac address so the machine can get online.
106
			 */
107 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
108 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
109
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
110
				" link " . escapeshellarg($random_mac));
111
			$lancfg['spoofmac'] = $random_mac;
112
			write_config();
113 da86dd6f Scott Ullrich
			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");
114 f36d4bd2 Scott Ullrich
		}
115
	}	
116 a4d9f914 Scott Ullrich
117
	/* bridged? */
118 b1b55ed2 Scott Ullrich
	
119 6065fd77 Scott Ullrich
	if ($lancfg['bridge']) {
120 a7341542 Scott Ullrich
		// mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " delete up");
121 a4d9f914 Scott Ullrich
		/* use open/netBSD style bridge */
122
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
123 6065fd77 Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up");
124
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}");
125 a4d9f914 Scott Ullrich
		
126 6065fd77 Scott Ullrich
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
127 a4d9f914 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
128 6065fd77 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up\n");
129
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}\n");
130 a4d9f914 Scott Ullrich
		fclose($fd);
131
		
132
		/* lets keep track of the amount of bridges initialized */
133
		$bridges_total++;
134
	}
135 f36d4bd2 Scott Ullrich
	
136 5b237745 Scott Ullrich
	/* media */
137
	if ($lancfg['media'] || $lancfg['mediaopt']) {
138
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
139
		if ($lancfg['media'])
140
			$cmd .= " media " . escapeshellarg($lancfg['media']);
141
		if ($lancfg['mediaopt'])
142
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
143
		mwexec($cmd);
144
	}
145 cfc707f7 Scott Ullrich
146
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
147 5b237745 Scott Ullrich
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
148 cfc707f7 Scott Ullrich
149 5b237745 Scott Ullrich
	if (!$g['booting']) {
150
		/* make new hosts file */
151
		system_hosts_generate();
152 cfc707f7 Scott Ullrich
153 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
154
		system_routing_configure();
155 cfc707f7 Scott Ullrich
156 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
157 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
158 cfc707f7 Scott Ullrich
159 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
160
		vpn_ipsec_configure();
161 cfc707f7 Scott Ullrich
162 5b237745 Scott Ullrich
		/* reload dhcpd (gateway may have changed) */
163
		services_dhcpd_configure();
164 cfc707f7 Scott Ullrich
165 5b237745 Scott Ullrich
		/* reload dnsmasq */
166
		services_dnsmasq_configure();
167 cfc707f7 Scott Ullrich
168 5b237745 Scott Ullrich
		/* reload webgui */
169
		system_webgui_start();
170 cfc707f7 Scott Ullrich
171 5b237745 Scott Ullrich
		/* reload captive portal */
172
		captiveportal_configure();
173
	}
174 cfc707f7 Scott Ullrich
175 5b237745 Scott Ullrich
	return 0;
176
}
177
178
function interfaces_optional_configure() {
179
	global $config, $g;
180
	global $bridgeconfig;
181 cfc707f7 Scott Ullrich
182 5b237745 Scott Ullrich
	/* Reset bridge configuration.	Interfaces will add to it. */
183
	$bridgeconfig = "";
184 cfc707f7 Scott Ullrich
185 5b237745 Scott Ullrich
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
186
		interfaces_optional_configure_if($i);
187
	}
188 cfc707f7 Scott Ullrich
189 5b237745 Scott Ullrich
	if (!$g['booting']) {
190
		/* reconfigure static routes (kernel may have deleted them) */
191
		system_routing_configure();
192 cfc707f7 Scott Ullrich
193 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
194
		vpn_ipsec_configure();
195 cfc707f7 Scott Ullrich
196 5b237745 Scott Ullrich
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
197
		services_dhcpd_configure();
198 cfc707f7 Scott Ullrich
199 5b237745 Scott Ullrich
		/* restart dnsmasq */
200
		services_dnsmasq_configure();
201 4d18de6a Scott Ullrich
202
		/* set the reload filter dity flag */
203
		touch("{$g['tmp_path']}/filter_dirty");				
204 5b237745 Scott Ullrich
	}
205 cfc707f7 Scott Ullrich
206 5b237745 Scott Ullrich
	return 0;
207
}
208
209
function interfaces_optional_configure_if($opti) {
210
	global $config, $g;
211
	global $bridgeconfig;
212 5a66117a Scott Ullrich
	global $bridges_total;
213 cfc707f7 Scott Ullrich
214 5b237745 Scott Ullrich
	$optcfg = $config['interfaces']['opt' . $opti];
215 cfc707f7 Scott Ullrich
216 5b237745 Scott Ullrich
	if ($g['booting']) {
217
		$optdescr = "";
218
		if ($optcfg['descr'])
219
			$optdescr = " ({$optcfg['descr']})";
220 5c6d0f65 Colin Smith
		print "\tOPT{$opti}{$optdescr}... ";
221 c1627786 Scott Ullrich
		mute_kernel_msgs();
222 5b237745 Scott Ullrich
	}
223 cfc707f7 Scott Ullrich
224 5b237745 Scott Ullrich
	if (isset($optcfg['enable'])) {
225
		/* wireless configuration? */
226
		if (is_array($optcfg['wireless']))
227
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
228 cfc707f7 Scott Ullrich
229 5b237745 Scott Ullrich
		/* MAC spoofing? */
230 f36d4bd2 Scott Ullrich
		if ($optcfg['spoofmac']) {
231 cfc707f7 Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
232 5b237745 Scott Ullrich
				" link " . escapeshellarg($optcfg['spoofmac']));
233 0ed77c51 Scott Ullrich
		} 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 9315ef83 Scott Ullrich
				echo "Generating new MAC address.";
240 0ed77c51 Scott Ullrich
				$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 f36d4bd2 Scott Ullrich
		}
248 cfc707f7 Scott Ullrich
249 5b237745 Scott Ullrich
		/* 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 cfc707f7 Scott Ullrich
259 5b237745 Scott Ullrich
		/* OpenVPN configuration? */
260
 		if (isset($optcfg['ovpn'])) {
261 669e1adb Bill Marquette
 			if (strstr($optcfg['if'], "tap"))
262 5b237745 Scott Ullrich
 				ovpn_link_tap();
263
 		}
264 cfc707f7 Scott Ullrich
265 5b237745 Scott Ullrich
		/* bridged? */
266
		if ($optcfg['bridge']) {
267 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
268 38226b19 Scott Ullrich
                        /* use open/netBSD style bridge */
269 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
270 bc1746b5 Scott Ullrich
                        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 5a66117a Scott Ullrich
			/* lets keep track of the amount of bridges initialized */
280
			$bridges_total++;
281 5b237745 Scott Ullrich
		} else {
282 0311dbd5 Scott Ullrich
			/* if user has selected DHCP type then act accordingly */
283 d3ce564b Scott Ullrich
			if($optcfg['ipaddr'] == "dhcp") {
284 1223f922 Scott Ullrich
				interfaces_opt_dhcp_configure("opt{$opti}");
285 0311dbd5 Scott Ullrich
			} else {			
286
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
287 77317f2b Colin Smith
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
288 0311dbd5 Scott Ullrich
			}
289 5b237745 Scott Ullrich
		}
290
	} else {
291 5a66117a Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
292 5b237745 Scott Ullrich
	}
293 cfc707f7 Scott Ullrich
294 5b237745 Scott Ullrich
	return 0;
295
}
296
297 9f6b1429 Scott Ullrich
function interfaces_carp_configure() {
298
	global $g, $config;
299
	if ($g['booting']) {
300 74dbce1f Scott Ullrich
		echo "Configuring CARP interfaces...";
301 dd2ab8f8 Scott Ullrich
		mute_kernel_msgs();
302 669e1adb Bill Marquette
	}
303 9f6b1429 Scott Ullrich
	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 75f2c22c Scott Ullrich
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
309 1425e067 Bill Marquette
	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 669e1adb Bill Marquette
			}
334 9f6b1429 Scott Ullrich
		}
335 1425e067 Bill Marquette
		$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 669e1adb Bill Marquette
			}
357 9f6b1429 Scott Ullrich
		}
358 23bfcd9f Scott Ullrich
	} else {
359
		/* hush little pfsync, don't say a word.  GeekGod's gonna
360
		   buy you a mocking bird. */
361
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
362
	}	
363 75f2c22c Scott Ullrich
	/* remove any dangling carp references */
364
	for($x=$carp_instances_counter; $x<$total_carp_interfaces_defined; $x++) {
365
		mwexec("/sbin/ifconfig carp{$x} down");
366
		mwexec("/sbin/ifconfig carp{$x} destroy");
367
	}
368 dd2ab8f8 Scott Ullrich
	unmute_kernel_msgs();
369 deebaae1 Scott Ullrich
	if ($g['booting']) {
370
		unmute_kernel_msgs();
371 74dbce1f Scott Ullrich
		echo "done.\n";
372 669e1adb Bill Marquette
	}
373 9f6b1429 Scott Ullrich
}
374
375 93e251a4 Scott Ullrich
function interfaces_carp_bringup() {
376 669e1adb Bill Marquette
	global $g;
377 93e251a4 Scott Ullrich
	/* lets bring the carp interfaces up now */
378 c6e604d8 Scott Ullrich
	if ($g['booting'])
379 8551d2ef Scott Ullrich
		sleep(1);
380 93e251a4 Scott Ullrich
	$carp_ints = find_number_of_created_carp_interfaces();
381
	for($x=0; $x<$carp_ints; $x++)
382 669e1adb Bill Marquette
		mwexec("/sbin/ifconfig carp{$x} up");	
383 93e251a4 Scott Ullrich
}
384
385 5b237745 Scott Ullrich
function interfaces_wireless_configure($if, $wlcfg) {
386 5508cf57 Scott Ullrich
        global $config, $g;
387 15e67907 Scott Ullrich
	
388
	/*   set wireless channel value.  if we're using 0 then
389
	 *   convert the channel to -
390
	 */
391
	$channel = escapeshellarg($wlcfg['channel']);
392
	if($channel == "") 
393
		$channel = "";
394 5508cf57 Scott Ullrich
395
        /* wireless configuration */
396
        $ifcargs = escapeshellarg($if) .
397 15e67907 Scott Ullrich
                " ssid " . escapeshellarg($wlcfg['ssid']) . " channel {$channel} ";
398 5508cf57 Scott Ullrich
399
        if ($wlcfg['stationname'])
400
                $ifcargs .= "stationname " . escapeshellarg($wlcfg['stationname']) . " ";
401 249558a2 Scott Ullrich
	
402
	if(!$wlcfg['mode']) {
403
		if (isset($wlcfg['wpa']['enable'])) {
404 50ad3b7c Scott Ullrich
		
405
	$wpa .= <<<EOD
406
	
407
ctrl_interface_group=0
408
eapol_version=1
409
ap_scan=1
410
fast_reauth=1
411
412 249558a2 Scott Ullrich
network={
413 50ad3b7c Scott Ullrich
	ssid="{$wlcfg['ssid']}"
414
	key_mgmt="{$wlcfg['wpapsk']}"
415
	psk="{$wlcfg['passphrase']}"
416 249558a2 Scott Ullrich
	
417 50ad3b7c Scott Ullrich
}
418
419
EOD;
420
421
		$fd = fopen("{$g['etc_path']}/ifconfig_{$if}", "w");
422
		fwrite($fd, "{$wpa}");
423
		fclose($fd);
424
		mwexec_bg("wpa_supplicant -i {$if} -c /etc/wpa_supplicant.conf");
425
426 249558a2 Scott Ullrich
		}
427 50ad3b7c Scott Ullrich
	}
428 5508cf57 Scott Ullrich
429
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
430
                $ifcargs .= "wepmode on ";
431
432
                $i = 1;
433
                foreach ($wlcfg['wep']['key'] as $wepkey) {
434
                        $ifcargs .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
435
                        if (isset($wepkey['txkey'])) {
436
                                $ifcargs .= "weptxkey {$i} ";
437
                        }
438
                        $i++;
439
                }
440
        } else {
441
                $ifcargs .= "wepmode off ";
442
        }
443
444 425f3e67 Scott Ullrich
	if(isset($wlcfg['pureg'])) {
445
		$ifcargs .= "mode 11g ";
446
	} else {
447
		if (preg_match($g['wireless_regex'], $if)) {
448
			if ($wlcfg['standard'])
449
				$ifcargs .= "mode {$wlcfg['standard']} ";
450
		}
451
	}
452 5508cf57 Scott Ullrich
453
        switch ($wlcfg['mode']) {
454
                case 'hostap':
455 9b6be82e Scott Ullrich
                        if (preg_match($g['wireless_regex'], $if)) 
456 5508cf57 Scott Ullrich
                                $ifcargs .= "-mediaopt adhoc mediaopt hostap ";
457
                        else if (strstr($if, "wi"))
458
                                $ifcargs .= "-mediaopt ibss mediaopt hostap ";
459 459d6351 Scott Ullrich
				if (isset($wlcfg['wpa']['enable'])) {
460
$wpa .= <<<EOD
461
462
interface={$if}
463
driver=bsd
464
logger_syslog=-1
465
logger_syslog_level=0
466
logger_stdout=-1
467
logger_stdout_level=0
468
debug=4
469
dump_file=/tmp/hostapd.dump
470
ctrl_interface=/var/run/hostapd
471
ctrl_interface_group=wheel
472
ssid={$wlcfg['ssid']}
473
macaddr_acl=1
474
auth_algs=3
475
eapol_key_index_workaround=0
476
wpa=1
477
wpa_key_mgmt={$wlcfg['wpapsk']}
478
wpa_pairwise=CCMP
479
wpa_group_rekey=60
480
wpa_gmk_rekey=3600
481
wpa_passphrase={$wlcfg['passphrase']}
482
483
EOD;
484
					$fd = fopen("{$g['etc_path']}/hostapd_{$if}.conf", "w");
485
					fwrite($fd, "{$wpa}");
486
					fclose($fd);
487 f9a5831e Scott Ullrich
					if(is_process_running("hostapd")) {
488
						mwexec("/usr/bin/killall -HUP hostapd");
489
					} else {
490
						mwexec("/usr/sbin/hostapd -B {$g['etc_path']}/hostapd_{$if}.conf");
491
					}
492 459d6351 Scott Ullrich
				}				
493
				
494 5508cf57 Scott Ullrich
                        break;
495
                case 'ibss':
496
                case 'IBSS':
497 9b6be82e Scott Ullrich
                        if (preg_match($g['wireless_regex'], $if)) 
498 5508cf57 Scott Ullrich
                                $ifcargs .= "-mediaopt hostap mediaopt adhoc ";
499
                        else if (strstr($if, "wi"))
500
                                $ifcargs .= "-mediaopt hostap mediaopt ibss ";
501
                        else if (strstr($if, "an"))
502
                                $ifcargs .= "mediaopt adhoc ";
503
                        break;
504
                case 'bss':
505
                case 'BSS':
506 9b6be82e Scott Ullrich
                        if (preg_match($g['wireless_regex'], $if)) 
507 5508cf57 Scott Ullrich
                                $ifcargs .= "-mediaopt hostap -mediaopt adhoc ";
508
                        else if (strstr($if, "wi"))
509
                                $ifcargs .= "-mediaopt hostap -mediaopt ibss ";
510
                        else if (strstr($if, "an"))
511
                                $ifcargs .= "-mediaopt adhoc ";
512
                        break;
513
        }
514 15e67907 Scott Ullrich
	
515 975326c5 Scott Ullrich
	/*   extra options during hostap mode
516 15e67907 Scott Ullrich
	 */
517
	if($wlcfg['mode'] == "hostap") {
518 975326c5 Scott Ullrich
		/* handle hide ssid option */
519
		if(isset($wlcfg['hidessid']))
520 15e67907 Scott Ullrich
			$ifcargs .= "hidessid ";
521
		else
522
			$ifcargs .= "-hidessid ";
523 975326c5 Scott Ullrich
		/* handle pureg (802.11g) only option */
524
		if(isset($wlcfg['pureg']))
525
			$ifcargs .= "pureg ";
526
		else
527
			$ifcargs .= "-pureg ";
528 15e67907 Scott Ullrich
	}
529 5508cf57 Scott Ullrich
530
        $ifcargs .= "up";
531
532
        mwexec("/sbin/ifconfig " . $ifcargs);
533
534 f169d6ca Scott Ullrich
	$fd = fopen("{$g['tmp_path']}/ifconfig_wireless", "w");
535 dc951404 Scott Ullrich
	fwrite($fd, "/sbin/ifconfig {$ifcargs}");
536
	fclose($fd);
537 50cb1fb0 Scott Ullrich
	
538
	if($wlcfg['txpower'] <> "")
539
		mwexec("/sbin/ifconfig {$ifcargs} txpower {$wlcfg['txpower']}");
540
	
541 80ce93c6 Scott Ullrich
	if(isset($wlcfg['useolsr']))
542
		setup_wireless_olsr(escapeshellarg($if));
543
	
544 5508cf57 Scott Ullrich
        return 0;
545 cfc707f7 Scott Ullrich
546 5b237745 Scott Ullrich
}
547
548 0311dbd5 Scott Ullrich
function find_dhclient_process($interface) {
549 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
550
        	$realinterface = filter_translate_type_to_real_interface($interface);
551
	$pid = `ps ax | grep "[d]hclient" | grep {$realinterface} | awk -F" " '{print $1}'`;
552 0311dbd5 Scott Ullrich
	return $pid;
553
}
554
555 5b237745 Scott Ullrich
function interfaces_wan_configure() {
556
	global $config, $g;
557 cfc707f7 Scott Ullrich
558 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
559 cfc707f7 Scott Ullrich
560 5c6d0f65 Colin Smith
	if(!$g['booting']) {
561 c1627786 Scott Ullrich
		mute_kernel_msgs();
562 0311dbd5 Scott Ullrich
563
		/* find dhclient process for wan and kill it */
564
		killbypid(find_dhclient_process("wan"));
565 cfc707f7 Scott Ullrich
566 5b237745 Scott Ullrich
		/* kill PPPoE client (mpd) */
567
		killbypid("{$g['varrun_path']}/mpd.pid");
568 cfc707f7 Scott Ullrich
569 5b237745 Scott Ullrich
		/* wait for processes to die */
570 0311dbd5 Scott Ullrich
		sleep(1);
571 cfc707f7 Scott Ullrich
572 0311dbd5 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
573 a23d7248 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
574
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
575
		unlink_if_exists("{$g['vardb_path']}/wanip");
576
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
577 5b237745 Scott Ullrich
	}
578 cfc707f7 Scott Ullrich
579 5b237745 Scott Ullrich
	/* remove all addresses first */
580
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
581
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
582 cfc707f7 Scott Ullrich
583 5b237745 Scott Ullrich
	/* wireless configuration? */
584
	if (is_array($wancfg['wireless']))
585
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
586 cfc707f7 Scott Ullrich
587 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
588 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
589 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
590 f36d4bd2 Scott Ullrich
	}  else {
591
		$mac = get_interface_mac_address($wancfg['if']);
592
		if($mac == "ff:ff:ff:ff:ff:ff") {
593
			/*   this is not a valid mac address.  generate a
594
			 *   temporary mac address so the machine can get online.
595
			 */
596 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
597 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
598
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
599
				" link " . escapeshellarg($random_mac));
600
			$wancfg['spoofmac'] = $random_mac;
601
			write_config();
602 da86dd6f Scott Ullrich
			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");
603 f36d4bd2 Scott Ullrich
		}
604
	}
605 cfc707f7 Scott Ullrich
606 5b237745 Scott Ullrich
	/* media */
607
	if ($wancfg['media'] || $wancfg['mediaopt']) {
608
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
609
		if ($wancfg['media'])
610
			$cmd .= " media " . escapeshellarg($wancfg['media']);
611
		if ($wancfg['mediaopt'])
612
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
613
		mwexec($cmd);
614
	}
615 cfc707f7 Scott Ullrich
616 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
617 cfc707f7 Scott Ullrich
618 5b237745 Scott Ullrich
		case 'dhcp':
619
			interfaces_wan_dhcp_configure();
620
			break;
621 cfc707f7 Scott Ullrich
622 5b237745 Scott Ullrich
		case 'pppoe':
623
			interfaces_wan_pppoe_configure();
624
			break;
625 cfc707f7 Scott Ullrich
626 5b237745 Scott Ullrich
		case 'pptp':
627
			interfaces_wan_pptp_configure();
628
			break;
629 cfc707f7 Scott Ullrich
630 5b237745 Scott Ullrich
		case 'bigpond':
631
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
632
			interfaces_wan_dhcp_configure();
633
			break;
634 cfc707f7 Scott Ullrich
635 5b237745 Scott Ullrich
		default:
636 a23d7248 Scott Ullrich
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
637
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
638
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
639
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
640
			} else {
641
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
642
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
643
			}
644 5b237745 Scott Ullrich
			/* install default route */
645
			mwexec("/sbin/route delete default");
646 88f66e13 Bill Marquette
			mwexec("/sbin/route add default " . escapeshellarg($config['system']['gateway']));
647 cfc707f7 Scott Ullrich
648 3c9daf99 Scott Ullrich
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
649
			filter_configure();
650 5b237745 Scott Ullrich
	}
651 cfc707f7 Scott Ullrich
652 5b237745 Scott Ullrich
	if (!$g['booting']) {
653
		/* reconfigure static routes (kernel may have deleted them) */
654
		system_routing_configure();
655 cfc707f7 Scott Ullrich
656 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
657 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
658 cfc707f7 Scott Ullrich
659 5b237745 Scott Ullrich
		/* reload ipsec tunnels */
660
		vpn_ipsec_configure();
661 cfc707f7 Scott Ullrich
662 5b237745 Scott Ullrich
		/* restart ez-ipupdate */
663
		services_dyndns_configure();
664 cfc707f7 Scott Ullrich
665 a23d7248 Scott Ullrich
		/* force DNS update */
666
		services_dnsupdate_process();
667
668 5b237745 Scott Ullrich
		/* restart dnsmasq */
669
		services_dnsmasq_configure();
670
	}
671 cfc707f7 Scott Ullrich
672 c1627786 Scott Ullrich
	unmute_kernel_msgs();
673
674 5b237745 Scott Ullrich
	return 0;
675
}
676
677 0311dbd5 Scott Ullrich
function interfaces_opt_dhcp_configure($interface) {
678
	global $config, $g;
679
680 1223f922 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
681 45870464 Scott Ullrich
	$optif = $optcfg['if'];
682 0311dbd5 Scott Ullrich
683
	/* generate dhclient_wan.conf */
684 aab78cf6 Scott Ullrich
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
685 0311dbd5 Scott Ullrich
	if (!$fd) {
686 aab78cf6 Scott Ullrich
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
687 0311dbd5 Scott Ullrich
		return 1;
688
	}
689
690 6d76590c Scott Ullrich
	if ($optcfg['dhcphostname']) {
691
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
692
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
693
	} else {
694
		$dhclientconf_hostname = "";
695
	}
696
697 0311dbd5 Scott Ullrich
 	$dhclientconf = "";
698
699 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
700
interface "{$optif}" {
701 0311dbd5 Scott Ullrich
	send host-name "{$optcfg['dhcphostname']}";
702 6d302eba Scott Ullrich
	script "/sbin/dhclient-script";
703 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
704 0311dbd5 Scott Ullrich
}
705
706
EOD;
707
708
	fwrite($fd, $dhclientconf);
709
	fclose($fd);
710 45870464 Scott Ullrich
711 0f1b5370 Scott Ullrich
        /* bring interface up before starting dhclient */
712 0311dbd5 Scott Ullrich
        mwexec("/sbin/ifconfig {$optif} up");
713
714
        /* fire up dhclient */
715 8270a5ca Scott Ullrich
        mwexec_bg("/sbin/dhclient -d -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
716 0311dbd5 Scott Ullrich
717
	return 0;
718
}
719
720 8c3e8725 Scott Ullrich
function interfaces_dhcp_configure($interface) {
721
	global $config, $g;
722
723 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
724
        	$realinterface = filter_translate_type_to_real_interface($interface);
725 6d76590c Scott Ullrich
726 8c3e8725 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
727
728
	/* generate dhclient_$interface.conf */
729
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
730
	if (!$fd) {
731
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
732
		return 1;
733
	}
734
735 6d76590c Scott Ullrich
	if ($optcfg['dhcphostname']) {
736
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
737
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
738
	} else {
739
		$dhclientconf_hostname = "";
740
	}
741
742 8c3e8725 Scott Ullrich
 	$dhclientconf = "";
743
744 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
745 84cec030 Scott Ullrich
interface "{$realinterface}" {
746 8c3e8725 Scott Ullrich
	script "/sbin/dhclient-script";
747 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
748 8c3e8725 Scott Ullrich
}
749
750
EOD;
751
752
	fwrite($fd, $dhclientconf);
753
	fclose($fd);
754
	
755
	$optif = $optcfg['if'];
756
	
757
        /* bring wan interface up before starting dhclient */
758
        mwexec("/sbin/ifconfig {$optif} up");
759
760
        /* fire up dhclient */
761 8270a5ca Scott Ullrich
        mwexec_bg("/sbin/dhclient -d -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
762 8c3e8725 Scott Ullrich
763
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
764 8270a5ca Scott Ullrich
	fwrite($fout, "/sbin/dhclient -d -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
765 8c3e8725 Scott Ullrich
	fclose($fout);
766
767
	return 0;
768
}
769
770 5b237745 Scott Ullrich
function interfaces_wan_dhcp_configure() {
771
	global $config, $g;
772 cfc707f7 Scott Ullrich
773 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
774
775 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
776
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
777 5b237745 Scott Ullrich
	if (!$fd) {
778 0311dbd5 Scott Ullrich
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
779 5b237745 Scott Ullrich
		return 1;
780
	}
781 6d76590c Scott Ullrich
	
782
	if ($wancfg['dhcphostname']) {
783
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
784
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
785
	} else {
786
		$dhclientconf_hostname = "";
787
	}
788 cfc707f7 Scott Ullrich
789 5b237745 Scott Ullrich
 	$dhclientconf = "";
790 cfc707f7 Scott Ullrich
791 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
792 d19cc554 Scott Ullrich
interface "{$wancfg['if']}" {
793 6d302eba Scott Ullrich
	script "/sbin/dhclient-script";
794 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
795 5b237745 Scott Ullrich
}
796
797
EOD;
798
799
	fwrite($fd, $dhclientconf);
800
	fclose($fd);
801 209309ac Scott Ullrich
	
802 0521b82d Scott Ullrich
	$wanif = $wancfg['if'];
803
	
804 eacc8c14 Scott Ullrich
        /* bring wan interface up before starting dhclient */
805 0521b82d Scott Ullrich
        mwexec("/sbin/ifconfig {$wanif} up");
806 eacc8c14 Scott Ullrich
807 0521b82d Scott Ullrich
        /* fire up dhclient */
808 8270a5ca Scott Ullrich
        mwexec_bg("/sbin/dhclient -d -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
809 cfc707f7 Scott Ullrich
810 fdca0ea8 Scott Ullrich
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
811 8270a5ca Scott Ullrich
	fwrite($fout, "/sbin/dhclient -d -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
812 0119d2f7 Scott Ullrich
	fclose($fout);
813
814 5b237745 Scott Ullrich
	return 0;
815
}
816
817 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_down() {
818 0aba287e Scott Ullrich
	global $config;
819
	$wancfg = $config['interfaces']['wan'];
820
	$wanif = $wancfg['if'];
821
	mwexec("/sbin/ifconfig {$wanif} delete");
822 8551d2ef Scott Ullrich
	sleep(1);
823 a23d7248 Scott Ullrich
}
824
825 468cee8d Scott Ullrich
function interfaces_dhcp_down($interface) {
826
	global $config;
827 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
828
		$realinterface = filter_translate_type_to_real_interface($interface);
829
	mwexec("/sbin/ifconfig {$realinterface} down");
830 0f1b5370 Scott Ullrich
	sleep(1);
831 84cec030 Scott Ullrich
	$pid = find_dhclient_process($interface);
832
	if($pid)
833
		mwexec("kill {$pid}");
834 468cee8d Scott Ullrich
}
835
836 8c3e8725 Scott Ullrich
function interfaces_dhcp_up($interface) {
837
	interfaces_dhcp_configure($interface);
838
	sleep(1);
839
}
840
841 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_up() {
842
	interfaces_wan_dhcp_configure();
843 8551d2ef Scott Ullrich
	sleep(1);
844 a23d7248 Scott Ullrich
}
845
846 5b237745 Scott Ullrich
function interfaces_wan_pppoe_configure() {
847
	global $config, $g;
848 cfc707f7 Scott Ullrich
849 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
850
	$pppoecfg = $config['pppoe'];
851 cfc707f7 Scott Ullrich
852 5b237745 Scott Ullrich
	/* generate mpd.conf */
853
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
854
	if (!$fd) {
855
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
856
		return 1;
857
	}
858 cfc707f7 Scott Ullrich
859 5b237745 Scott Ullrich
	$idle = 0;
860 cfc707f7 Scott Ullrich
861 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
862
		$ondemand = "enable";
863
		if ($pppoecfg['timeout'])
864
			$idle = $pppoecfg['timeout'];
865
	} else {
866
		$ondemand = "disable";
867
	}
868 cfc707f7 Scott Ullrich
869 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
870
pppoe:
871
	new -i ng0 pppoe pppoe
872
	set iface route default
873
	set iface {$ondemand} on-demand
874
	set iface idle {$idle}
875
	set iface up-script /usr/local/sbin/ppp-linkup
876
877
EOD;
878 cfc707f7 Scott Ullrich
879 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
880
		$mpdconf .= <<<EOD
881
	set iface addrs 10.0.0.1 10.0.0.2
882
883
EOD;
884
	}
885 cfc707f7 Scott Ullrich
886 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
887
	set bundle disable multilink
888
	set bundle authname "{$pppoecfg['username']}"
889
	set bundle password "{$pppoecfg['password']}"
890
	set link keep-alive 10 60
891
	set link max-redial 0
892
	set link no acfcomp protocomp
893
	set link disable pap chap
894
	set link accept chap
895
	set link mtu 1492
896
	set ipcp yes vjcomp
897
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
898 a23d7248 Scott Ullrich
899
EOD;
900
901
	if (isset($config['system']['dnsallowoverride'])) {
902
		$mpdconf .= <<<EOD
903 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
904 a23d7248 Scott Ullrich
905
EOD;
906
	}
907 a0ff9696 Scott Ullrich
908 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
909 5b237745 Scott Ullrich
	open iface
910
911
EOD;
912
913
	fwrite($fd, $mpdconf);
914
	fclose($fd);
915 cfc707f7 Scott Ullrich
916 5b237745 Scott Ullrich
	/* generate mpd.links */
917
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
918
	if (!$fd) {
919
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
920
		return 1;
921
	}
922 cfc707f7 Scott Ullrich
923 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
924
pppoe:
925
	set link type pppoe
926
	set pppoe iface {$wancfg['if']}
927
	set pppoe service "{$pppoecfg['provider']}"
928
	set pppoe enable originate
929
	set pppoe disable incoming
930
931
EOD;
932
933
	fwrite($fd, $mpdconf);
934
	fclose($fd);
935 cfc707f7 Scott Ullrich
936 ec11a1ad Scott Ullrich
	/* if mpd is active, lets take it down */
937
	if(file_exists("{$g['varrun_path']}/mpd.pid")) {
938
		killbypid(file_get_contents("{$g['varrun_path']}/mpd.pid"));
939
		sleep(3);
940
	}
941
942 5b237745 Scott Ullrich
	/* fire up mpd */
943
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
944 cfc707f7 Scott Ullrich
945 5b237745 Scott Ullrich
	return 0;
946
}
947
948 a23d7248 Scott Ullrich
function interfaces_wan_pppoe_down() {
949
	global $g;
950
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
951 8551d2ef Scott Ullrich
	sleep(1);
952 a23d7248 Scott Ullrich
}
953
954
function interfaces_wan_pppoe_up() {
955
	global $g;
956
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
957 8551d2ef Scott Ullrich
	sleep(1);
958 a23d7248 Scott Ullrich
}
959
960 5b237745 Scott Ullrich
function interfaces_wan_pptp_configure() {
961
	global $config, $g;
962 cfc707f7 Scott Ullrich
963 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
964
	$pptpcfg = $config['pptp'];
965 cfc707f7 Scott Ullrich
966 5b237745 Scott Ullrich
	/* generate mpd.conf */
967
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
968
	if (!$fd) {
969
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
970
		return 1;
971
	}
972 cfc707f7 Scott Ullrich
973 5b237745 Scott Ullrich
	$idle = 0;
974 cfc707f7 Scott Ullrich
975 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
976
		$ondemand = "enable";
977
		if ($pptpcfg['timeout'])
978
			$idle = $pptpcfg['timeout'];
979
	} else {
980
		$ondemand = "disable";
981
	}
982 cfc707f7 Scott Ullrich
983 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
984
pptp:
985
	new -i ng0 pptp pptp
986
	set iface route default
987
	set iface {$ondemand} on-demand
988
	set iface idle {$idle}
989
	set iface up-script /usr/local/sbin/ppp-linkup
990
991
EOD;
992 cfc707f7 Scott Ullrich
993 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
994
		$mpdconf .= <<<EOD
995 a23d7248 Scott Ullrich
	set iface addrs 10.0.0.1 10.0.0.2
996 5b237745 Scott Ullrich
997
EOD;
998
	}
999 cfc707f7 Scott Ullrich
1000 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1001
	set bundle disable multilink
1002
	set bundle authname "{$pptpcfg['username']}"
1003
	set bundle password "{$pptpcfg['password']}"
1004
	set link keep-alive 10 60
1005
	set link max-redial 0
1006
	set link no acfcomp protocomp
1007
	set link disable pap chap
1008
	set link accept chap
1009
	set ipcp no vjcomp
1010
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1011 a23d7248 Scott Ullrich
1012
EOD;
1013 a0ff9696 Scott Ullrich
1014 a23d7248 Scott Ullrich
	if (isset($config['system']['dnsallowoverride'])) {
1015
		$mpdconf .= <<<EOD
1016 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1017 a23d7248 Scott Ullrich
1018
EOD;
1019
	}
1020 a0ff9696 Scott Ullrich
1021 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1022 5b237745 Scott Ullrich
	open
1023
1024
EOD;
1025
1026
	fwrite($fd, $mpdconf);
1027
	fclose($fd);
1028 cfc707f7 Scott Ullrich
1029 5b237745 Scott Ullrich
	/* generate mpd.links */
1030
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1031
	if (!$fd) {
1032
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1033
		return 1;
1034
	}
1035 cfc707f7 Scott Ullrich
1036 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1037
pptp:
1038
	set link type pptp
1039
	set pptp enable originate outcall
1040
	set pptp disable windowing
1041
	set pptp self {$pptpcfg['local']}
1042
	set pptp peer {$pptpcfg['remote']}
1043
1044
EOD;
1045
1046
	fwrite($fd, $mpdconf);
1047
	fclose($fd);
1048 cfc707f7 Scott Ullrich
1049 5b237745 Scott Ullrich
	/* configure interface */
1050 cfc707f7 Scott Ullrich
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1051 5b237745 Scott Ullrich
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1052 cfc707f7 Scott Ullrich
1053 5b237745 Scott Ullrich
	/* fire up mpd */
1054
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pptp");
1055 cfc707f7 Scott Ullrich
1056 5b237745 Scott Ullrich
	return 0;
1057
}
1058
1059 a23d7248 Scott Ullrich
function interfaces_wan_pptp_down() {
1060
	global $g;
1061
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1062 8551d2ef Scott Ullrich
	sleep(1);
1063 a23d7248 Scott Ullrich
}
1064
1065
function interfaces_wan_pptp_up() {
1066
	global $g;
1067
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1068 8551d2ef Scott Ullrich
	sleep(1);
1069 a23d7248 Scott Ullrich
}
1070
1071 5b237745 Scott Ullrich
function interfaces_wan_bigpond_configure($curwanip) {
1072
	global $config, $g;
1073 cfc707f7 Scott Ullrich
1074 5b237745 Scott Ullrich
	$bpcfg = $config['bigpond'];
1075 cfc707f7 Scott Ullrich
1076 5b237745 Scott Ullrich
	if (!$curwanip) {
1077
		/* IP address not configured yet, exit */
1078
		return 0;
1079
	}
1080 cfc707f7 Scott Ullrich
1081 5b237745 Scott Ullrich
	/* kill bpalogin */
1082
	killbyname("bpalogin");
1083 cfc707f7 Scott Ullrich
1084 5b237745 Scott Ullrich
	/* wait a moment */
1085
	sleep(1);
1086 cfc707f7 Scott Ullrich
1087 5b237745 Scott Ullrich
	/* get the default domain */
1088
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1089
	if ($nfd) {
1090
		$defaultdomain = trim(fgets($nfd));
1091
		fclose($nfd);
1092
	}
1093 cfc707f7 Scott Ullrich
1094 5b237745 Scott Ullrich
	/* generate bpalogin.conf */
1095
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1096
	if (!$fd) {
1097
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1098
		return 1;
1099
	}
1100 cfc707f7 Scott Ullrich
1101 5b237745 Scott Ullrich
	if (!$bpcfg['authserver'])
1102
		$bpcfg['authserver'] = "dce-server";
1103
	if (!$bpcfg['authdomain'])
1104
		$bpcfg['authdomain'] = $defaultdomain;
1105 cfc707f7 Scott Ullrich
1106 5b237745 Scott Ullrich
	$bpconf = <<<EOD
1107
username {$bpcfg['username']}
1108
password {$bpcfg['password']}
1109
authserver {$bpcfg['authserver']}
1110
authdomain {$bpcfg['authdomain']}
1111
localport 5050
1112
1113
EOD;
1114
1115
	if ($bpcfg['minheartbeatinterval'])
1116
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1117
1118
	fwrite($fd, $bpconf);
1119
	fclose($fd);
1120 cfc707f7 Scott Ullrich
1121 5b237745 Scott Ullrich
	/* fire up bpalogin */
1122
	mwexec("/usr/local/sbin/bpalogin -c {$g['varetc_path']}/bpalogin.conf");
1123 cfc707f7 Scott Ullrich
1124 5b237745 Scott Ullrich
	return 0;
1125
}
1126
1127
function get_real_wan_interface() {
1128
	global $config, $g;
1129 cfc707f7 Scott Ullrich
1130 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1131 cfc707f7 Scott Ullrich
1132 5b237745 Scott Ullrich
	$wanif = $wancfg['if'];
1133
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1134
		$wanif = $g['pppoe_interface'];
1135
	}
1136 cfc707f7 Scott Ullrich
1137 5b237745 Scott Ullrich
	return $wanif;
1138
}
1139
1140
function get_current_wan_address() {
1141
	global $config, $g;
1142 cfc707f7 Scott Ullrich
1143 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1144 cfc707f7 Scott Ullrich
1145 5b237745 Scott Ullrich
	if (in_array($wancfg['ipaddr'], array('pppoe','dhcp','pptp','bigpond'))) {
1146
		/* dynamic WAN IP address, find out which one */
1147
		$wanif = get_real_wan_interface();
1148 cfc707f7 Scott Ullrich
1149 5b237745 Scott Ullrich
		/* get interface info with netstat */
1150
		exec("/usr/bin/netstat -nWI " . escapeshellarg($wanif) . " -f inet", $ifinfo);
1151 cfc707f7 Scott Ullrich
1152 5b237745 Scott Ullrich
		if (isset($ifinfo[1])) {
1153
			$aif = preg_split("/\s+/", $ifinfo[1]);
1154
			$curwanip = chop($aif[3]);
1155 cfc707f7 Scott Ullrich
1156 5b237745 Scott Ullrich
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1157
				return $curwanip;
1158
		}
1159 cfc707f7 Scott Ullrich
1160 5b237745 Scott Ullrich
		return null;
1161
	} else {
1162
		/* static WAN IP address */
1163
		return $wancfg['ipaddr'];
1164
	}
1165
}
1166
1167 081065c1 Bill Marquette
/****f* interfaces/is_jumbo_capable
1168
 * NAME
1169
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
1170
 * INPUTS
1171
 *   $int             - string containing interface name
1172
 * RESULT
1173
 *   boolean          - true or false
1174
 ******/
1175
1176
function is_jumbo_capable($int) {
1177
	/* Per:
1178
	 * http://www.freebsd.org/cgi/man.cgi?query=vlan&manpath=FreeBSD+6.0-current&format=html
1179
	 * Only the following drivers support large frames
1180
	 */
1181 a57b119e Bill Marquette
	$capable = array("bfe", "dc", "de", "fxp", "hme", "rl", "sis", "ste",
1182
		"tl", "tx", "xl");
1183 081065c1 Bill Marquette
	
1184
	$int_family = preg_split("/[0-9]+/", $int);
1185
1186
	if (in_array($int_family[0], $capable))
1187
		return true;
1188
	else
1189
		return false;
1190
}
1191
1192 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
1193
 * NAME
1194
 *   is_altq_capable - Test if interface is capable of using ALTQ
1195
 * INPUTS
1196
 *   $int            - string containing interface name
1197
 * RESULT
1198
 *   boolean         - true or false
1199
 ******/
1200
1201
function is_altq_capable($int) {
1202
        /* Per:
1203
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1204
         * Only the following drivers have ALTQ support
1205
         */
1206
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1207
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1208
		"tun", "vr", "wi", "xl");
1209
1210
        $int_family = preg_split("/[0-9]+/", $int);
1211
1212
        if (in_array($int_family[0], $capable))
1213
                return true;
1214
        else
1215
                return false;
1216
}
1217
1218
1219 84cec030 Scott Ullrich
?>