Project

General

Profile

Download (52.9 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 0e16b9ca Scott Ullrich
	Copyright (C) 2004-2006 Scott Ullrich
6 ac3f8318 Espen Johansen
	All rights reserved.
7
8
	function interfaces_wireless_configure is
9
	Copyright (C) 2005 Espen Johansen
10 cfc707f7 Scott Ullrich
	All rights reserved.
11
12
	originally part of m0n0wall (http://m0n0.ch/wall)
13 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
	All rights reserved.
15 cfc707f7 Scott Ullrich
16 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
17
	modification, are permitted provided that the following conditions are met:
18 cfc707f7 Scott Ullrich
19 ac3f8318 Espen Johansen
	1. Redistributions of source code must retain the above copyright notices,
20 5b237745 Scott Ullrich
	   this list of conditions and the following disclaimer.
21 cfc707f7 Scott Ullrich
22 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
23 ac3f8318 Espen Johansen
	   notices, this list of conditions and the following disclaimer in the
24 5b237745 Scott Ullrich
	   documentation and/or other materials provided with the distribution.
25 cfc707f7 Scott Ullrich
26 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
	POSSIBILITY OF SUCH DAMAGE.
36
*/
37
38
/* include all configuration functions */
39
require_once("functions.inc");
40 7387844e Chris Buechler
require_once("globals.inc");
41 5b237745 Scott Ullrich
42
function interfaces_loopback_configure() {
43
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
44 cfc707f7 Scott Ullrich
45 5b237745 Scott Ullrich
	return 0;
46
}
47
48
function interfaces_vlan_configure() {
49 7387844e Chris Buechler
	global $config, $g;
50 cfc707f7 Scott Ullrich
51 5b237745 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
52 cfc707f7 Scott Ullrich
53 5b237745 Scott Ullrich
		/* devices with native VLAN support */
54 7387844e Chris Buechler
		$vlan_native_supp = $g['vlan_native_supp'];
55 cfc707f7 Scott Ullrich
56 5b237745 Scott Ullrich
		/* devices with long frame support */
57 7387844e Chris Buechler
		$vlan_long_frame = $g['vlan_long_frame'];
58 cfc707f7 Scott Ullrich
59 161040eb Scott Ullrich
		/* sweep through and axe old interfaces */
60
		$vlan_count = get_number_of_vlan_interfaces();
61
		for($x=0; $x<$vlan_count; $x++)
62 fb8a9b2f Ermal Luçi
			exec("/sbin/ifconfig vlan{$x} down destroy");
63 161040eb Scott Ullrich
64 5b237745 Scott Ullrich
		$i = 0;
65 cfc707f7 Scott Ullrich
66 5b237745 Scott Ullrich
		foreach ($config['vlans']['vlan'] as $vlan) {
67 cfc707f7 Scott Ullrich
68
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
69
				escapeshellarg($vlan['tag']) . " vlandev " .
70 5b237745 Scott Ullrich
				escapeshellarg($vlan['if']);
71 cfc707f7 Scott Ullrich
72 5b237745 Scott Ullrich
			/* get driver name */
73
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
74
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
75
					break;
76
			}
77
			$drvname = substr($vlan['if'], 0, $j);
78 cfc707f7 Scott Ullrich
79 5b237745 Scott Ullrich
			if (in_array($drvname, $vlan_native_supp))
80
				$cmd .= " link0";
81 7387844e Chris Buechler
			else if (in_array($drvname, $vlan_long_frame))
82 5b237745 Scott Ullrich
				$cmd .= " mtu 1500";
83 cfc707f7 Scott Ullrich
84 5b237745 Scott Ullrich
			mwexec($cmd);
85 cfc707f7 Scott Ullrich
86 3f7d2120 Bill Marquette
			/* invalidate interface cache */
87
			get_interface_arr(true);
88
89 c5af8bf9 Scott Ullrich
			/*   all vlans need to spoof their parent mac address, too.  see
90
			 *   ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33 
91
			 */
92
			foreach($config['interfaces'] as $interfaces) {
93
				if($interfaces['if'] == $vlan['if']) {
94
					if($interfaces['spoofmac']) {
95
						mwexec("/sbin/ifconfig " . escapeshellarg($interfaces['if']) .
96
							" link " . escapeshellarg($interfaces['spoofmac']));
97
					}
98
				}
99
			}
100
101 5b237745 Scott Ullrich
			/* make sure the parent interface is up */
102
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
103 cfc707f7 Scott Ullrich
104 5b237745 Scott Ullrich
			$i++;
105
		}
106
	}
107 cfc707f7 Scott Ullrich
108 5b237745 Scott Ullrich
	return 0;
109
}
110
111
function interfaces_lan_configure() {
112 6ee83b6e Scott Ullrich
	global $config, $g;
113
114
	$bridges_total = get_next_available_bridge_interface();
115 cfc707f7 Scott Ullrich
116 5b237745 Scott Ullrich
	$lancfg = $config['interfaces']['lan'];
117 cfc707f7 Scott Ullrich
118 7cc7e84d Scott Ullrich
	/* if user has removed ip address, clear it*/
119
	if($lancfg['ipaddr'] == "")
120
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
121
122 5b237745 Scott Ullrich
	/* wireless configuration? */
123
	if (is_array($lancfg['wireless']))
124
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
125 cfc707f7 Scott Ullrich
126 5b237745 Scott Ullrich
	/* MAC spoofing? */
127 f36d4bd2 Scott Ullrich
	if ($lancfg['spoofmac']) {
128 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
129 5b237745 Scott Ullrich
			" link " . escapeshellarg($lancfg['spoofmac']));
130 f36d4bd2 Scott Ullrich
	} else {
131
		$mac = get_interface_mac_address($lancfg['if']);
132
		if($mac == "ff:ff:ff:ff:ff:ff") {
133
			/*   this is not a valid mac address.  generate a
134
			 *   temporary mac address so the machine can get online.
135
			 */
136 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
137 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
138
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
139
				" link " . escapeshellarg($random_mac));
140
			$lancfg['spoofmac'] = $random_mac;
141
			write_config();
142 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");
143 f36d4bd2 Scott Ullrich
		}
144 eb772abd Scott Ullrich
	}
145 a4d9f914 Scott Ullrich
146
	/* bridged? */
147 eb772abd Scott Ullrich
148 6065fd77 Scott Ullrich
	if ($lancfg['bridge']) {
149 a4d9f914 Scott Ullrich
		/* use open/netBSD style bridge */
150
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
151 eb772abd Scott Ullrich
152 96812f11 Scott Ullrich
		/* force all bridged interfaces to use same mtu */
153 51535829 Scott Ullrich
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
154 96812f11 Scott Ullrich
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
155
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
156 51535829 Scott Ullrich
157
		/* assign items to a bridge */
158 a591b9cb Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}");
159 eb772abd Scott Ullrich
160 e7aa7560 Scott Ullrich
		if(!is_interface_wireless($lancfg['if']) and
161 eb772abd Scott Ullrich
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if']))
162 3789e4df Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$lancfg['bridge']]['if']} stp {$lancfg['if']}");
163 51535829 Scott Ullrich
164
		/* log commands run for debugging in /tmp/ */
165 6065fd77 Scott Ullrich
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
166 51535829 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
167
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
168 a4d9f914 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
169 b3a66d90 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}\n");
170 12fea2f7 Scott Ullrich
		if(!is_interface_wireless($lancfg['if']) and
171
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if']))		
172
				fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
173 a4d9f914 Scott Ullrich
		fclose($fd);
174 eb772abd Scott Ullrich
175 f70c887f Scott Ullrich
		/* bring up interfaces */
176 77712c1e Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
177
		usleep(100);
178 f70c887f Scott Ullrich
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
179 77712c1e Scott Ullrich
		usleep(5);
180 f70c887f Scott Ullrich
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
181 77712c1e Scott Ullrich
		usleep(5);
182 43715f8b Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
183 eb772abd Scott Ullrich
184 c60e4f51 Scott Ullrich
		$bridges_total++;
185 547a126d Bill Marquette
		/* update cache */
186 3f7d2120 Bill Marquette
		if ($bridges_total != find_number_of_created_bridges())
187
			find_number_of_created_bridges(true);
188 a4d9f914 Scott Ullrich
	}
189 eb772abd Scott Ullrich
190 5b237745 Scott Ullrich
	/* media */
191
	if ($lancfg['media'] || $lancfg['mediaopt']) {
192
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
193
		if ($lancfg['media'])
194
			$cmd .= " media " . escapeshellarg($lancfg['media']);
195
		if ($lancfg['mediaopt'])
196
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
197
		mwexec($cmd);
198
	}
199 cfc707f7 Scott Ullrich
200
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
201 5b237745 Scott Ullrich
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
202 cfc707f7 Scott Ullrich
203 5b237745 Scott Ullrich
	if (!$g['booting']) {
204
		/* make new hosts file */
205
		system_hosts_generate();
206 cfc707f7 Scott Ullrich
207 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
208
		system_routing_configure();
209 cfc707f7 Scott Ullrich
210 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
211 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
212 cfc707f7 Scott Ullrich
213 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
214
		vpn_ipsec_configure();
215 cfc707f7 Scott Ullrich
216 5b237745 Scott Ullrich
		/* reload dhcpd (gateway may have changed) */
217
		services_dhcpd_configure();
218 cfc707f7 Scott Ullrich
219 5b237745 Scott Ullrich
		/* reload dnsmasq */
220
		services_dnsmasq_configure();
221 cfc707f7 Scott Ullrich
222 6a669fb0 Scott Ullrich
		/* reload captive portal */
223
		captiveportal_configure();
224 cfc707f7 Scott Ullrich
225 5b237745 Scott Ullrich
	}
226 cfc707f7 Scott Ullrich
227 5b237745 Scott Ullrich
	return 0;
228
}
229
230
function interfaces_optional_configure() {
231
	global $config, $g;
232
	global $bridgeconfig;
233 cfc707f7 Scott Ullrich
234 5b237745 Scott Ullrich
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
235
		interfaces_optional_configure_if($i);
236
	}
237 cfc707f7 Scott Ullrich
238 5b237745 Scott Ullrich
	if (!$g['booting']) {
239
		/* reconfigure static routes (kernel may have deleted them) */
240
		system_routing_configure();
241 cfc707f7 Scott Ullrich
242 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
243
		vpn_ipsec_configure();
244 cfc707f7 Scott Ullrich
245 5b237745 Scott Ullrich
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
246
		services_dhcpd_configure();
247 cfc707f7 Scott Ullrich
248 5b237745 Scott Ullrich
		/* restart dnsmasq */
249
		services_dnsmasq_configure();
250 4d18de6a Scott Ullrich
251 c597d50f Scott Ullrich
		/* reload captive portal */
252
		captiveportal_configure();
253
254 4d18de6a Scott Ullrich
		/* set the reload filter dity flag */
255 eb772abd Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
256 5b237745 Scott Ullrich
	}
257 cfc707f7 Scott Ullrich
258 5b237745 Scott Ullrich
	return 0;
259
}
260
261
function interfaces_optional_configure_if($opti) {
262
	global $config, $g;
263 6008210b Scott Ullrich
	global $bridgeconfig, $debugging;
264 6ee83b6e Scott Ullrich
265
	$bridges_total = get_next_available_bridge_interface();
266 cfc707f7 Scott Ullrich
267 5b237745 Scott Ullrich
	$optcfg = $config['interfaces']['opt' . $opti];
268 cfc707f7 Scott Ullrich
269 5b237745 Scott Ullrich
	if ($g['booting']) {
270
		$optdescr = "";
271
		if ($optcfg['descr'])
272
			$optdescr = " ({$optcfg['descr']})";
273 5c6d0f65 Colin Smith
		print "\tOPT{$opti}{$optdescr}... ";
274 5b237745 Scott Ullrich
	}
275 d04d8d42 Scott Ullrich
	
276
	if(file_exists("/tmp/{$optcfg['if']}_router"))
277
		unlink("/tmp/{$optcfg['if']}_router");
278 cfc707f7 Scott Ullrich
279 5b237745 Scott Ullrich
	if (isset($optcfg['enable'])) {
280 d04d8d42 Scott Ullrich
		if($optcfg['gateway'])
281
			system("echo " . $optcfg['gateway'] . " > /tmp/" . $optcfg['if'] . "_router");
282
283 5b237745 Scott Ullrich
		/* wireless configuration? */
284
		if (is_array($optcfg['wireless']))
285
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
286 cfc707f7 Scott Ullrich
287 860c4e80 Chris Buechler
		/* PPP configuration */
288
		if (isset($optcfg['pointtopoint']))
289
			interfaces_ppp_configure_if($optcfg);
290
291 5b237745 Scott Ullrich
		/* MAC spoofing? */
292 f36d4bd2 Scott Ullrich
		if ($optcfg['spoofmac']) {
293 cfc707f7 Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
294 5b237745 Scott Ullrich
				" link " . escapeshellarg($optcfg['spoofmac']));
295 0ed77c51 Scott Ullrich
		} else {
296
			$mac = get_interface_mac_address($optcfg['if']);
297
			if($mac == "ff:ff:ff:ff:ff:ff") {
298
				/*   this is not a valid mac address.  generate a
299
				 *   temporary mac address so the machine can get online.
300
				 */
301 9315ef83 Scott Ullrich
				echo "Generating new MAC address.";
302 0ed77c51 Scott Ullrich
				$random_mac = generate_random_mac_address();
303
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
304
					" link " . escapeshellarg($random_mac));
305
				$optcfg['spoofmac'] = $random_mac;
306
				write_config();
307
				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");
308
			}
309 f36d4bd2 Scott Ullrich
		}
310 cfc707f7 Scott Ullrich
311 5b237745 Scott Ullrich
		/* media */
312
		if ($optcfg['media'] || $optcfg['mediaopt']) {
313
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
314
			if ($optcfg['media'])
315
				$cmd .= " media " . escapeshellarg($optcfg['media']);
316
			if ($optcfg['mediaopt'])
317
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
318
			mwexec($cmd);
319
		}
320 cfc707f7 Scott Ullrich
321 5b237745 Scott Ullrich
		/* bridged? */
322
		if ($optcfg['bridge']) {
323 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
324 38226b19 Scott Ullrich
                        /* use open/netBSD style bridge */
325 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
326 3f7d2120 Bill Marquette
327
			/* invalidate interface cache */
328
			get_interface_arr(true);
329 eb772abd Scott Ullrich
330 96812f11 Scott Ullrich
			/* force all bridged interfaces to use same mtu */
331 51535829 Scott Ullrich
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
332 96812f11 Scott Ullrich
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
333 eb772abd Scott Ullrich
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");
334 51535829 Scott Ullrich
335
			/* assign items to a bridge */
336 a591b9cb Scott Ullrich
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']}");
337 df792110 Scott Ullrich
338 e7aa7560 Scott Ullrich
			if(!is_interface_wireless($optcfg['if']) and
339 eb772abd Scott Ullrich
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if']))
340 3789e4df Scott Ullrich
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
341 51535829 Scott Ullrich
342
			/* log commands run for debugging in /tmp/ */
343 bc1746b5 Scott Ullrich
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
344 51535829 Scott Ullrich
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
345
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
346 bc1746b5 Scott Ullrich
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
347
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
348 12fea2f7 Scott Ullrich
			if(!is_interface_wireless($optcfg['if']) and
349
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if']))
350
					fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
351 bc1746b5 Scott Ullrich
			fclose($fd);
352 eb772abd Scott Ullrich
353 f70c887f Scott Ullrich
			/* bring up interfaces */
354 fafdfe28 Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} down");
355 77712c1e Scott Ullrich
			usleep(100);
356
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
357
			usleep(5);
358 82a7eb4a Scott Ullrich
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
359 77712c1e Scott Ullrich
			usleep(5);
360 6651e3ec Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} up");
361 eb772abd Scott Ullrich
362 c60e4f51 Scott Ullrich
			$bridges_total++;
363 547a126d Bill Marquette
			/* update cache */
364 3f7d2120 Bill Marquette
			if ($bridges_total != find_number_of_created_bridges())
365
				find_number_of_created_bridges(true);
366 5b237745 Scott Ullrich
		} else {
367 0311dbd5 Scott Ullrich
			/* if user has selected DHCP type then act accordingly */
368 d3ce564b Scott Ullrich
			if($optcfg['ipaddr'] == "dhcp") {
369 1223f922 Scott Ullrich
				interfaces_opt_dhcp_configure("opt{$opti}");
370 eb772abd Scott Ullrich
			} else {
371 0311dbd5 Scott Ullrich
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
372 77317f2b Colin Smith
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
373 0311dbd5 Scott Ullrich
			}
374 5b237745 Scott Ullrich
		}
375
	} else {
376 5a66117a Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
377 5b237745 Scott Ullrich
	}
378
	return 0;
379
}
380
381 860c4e80 Chris Buechler
function interfaces_ppp_configure_if($ifcfg) {
382
	global $config;
383
	
384
	if(file_exists("/var/run/ppp0.pid")) {
385
		$pid = file_get_contents("/var/run/ppp0.pid");
386
		mwexec('kill $pid');
387
	}
388
	
389
	mwexec("/sbin/ifconfig ppp0 down destroy");
390
391
	$peerfile = "lcp-echo-failure 0\n";
392
	$peerfile .= "lcp-echo-interval 0\n";
393
	$peerfile .= "connect /etc/ppp/peers/ppp0-connect-chat\n";
394
	//$peerfile .= "disconnect /etc/ppp/peers/ppp0-disconnect-chat\n";
395
	$peerfile .= "/dev/{$ifcfg['serialport']}\n";
396
	$peerfile .= "crtscts\n";
397
	$peerfile .= "local\n";
398
	$peerfile .= ":{$ifcfg['gateway']}\n";
399
	$peerfile .= "noipdefault\n";
400
	$peerfile .= "ipcp-accept-local\n";
401
	$peerfile .= "novj\n";
402
	$peerfile .= "nobsdcomp\n";
403
	$peerfile .= "novjccomp\n";
404
	$peerfile .= "nopcomp\n";
405
	$peerfile .= "noaccomp\n";
406
	$peerfile .= "noauth\n";
407
	$peerfile .= "persist\n";
408
	$peerfile .= "debug\n";
409
	// KD - test
410
	//$peerfile .= "defaultroute\n";
411
	//$peerfile .= "nodetach\n";
412
	// KD - so I know where to look!
413
	$peerfile .= "# created by /etc/inc/interfaces.inc\n";
414
	file_put_contents("/etc/ppp/peers/ppp0", $peerfile);
415
416
	// Added single quotes to some strings below:
417
	// the \rAT is *always* going to need it
418
	// and the phone number on a GSM connection ends in a # char
419
	// Kevin Dawson, 22 Jan 2008
420
	// Refer Andrew Curtis
421
			
422
	$chatfile = "#!/bin/sh\n";
423
	$chatfile .= "exec chat \\\n";
424
	$chatfile .= "TIMEOUT 5 \\\n";
425
	$chatfile .= "ECHO ON \\\n";
426
	$chatfile .= "ABORT '\\nBUSY\\r' \\\n";
427
	$chatfile .= "ABORT '\\nERROR\\r' \\\n";
428
	$chatfile .= "ABORT '\\nNO ANSWER\\r' \\\n";
429
	$chatfile .= "ABORT '\\nNO CARRIER\\r' \\\n";
430
	$chatfile .= "ABORT '\\nNO DIALTONE\\r' \\\n";
431
	$chatfile .= "ABORT '\\nRINGING\\r\\n\\r\\nRINGING\\r' \\\n";
432
	// KD
433
	$chatfile .= "'' '\\rAT' \\\n";
434
	$chatfile .= "TIMEOUT 12 \\\n";
435
	$chatfile .= "OK ATH \\\n";
436
	$chatfile .= "OK ATE1 \\\n";
437
	$chatfile .= "OK 'AT+CGDCONT=1,\"IP\",\"{$ifcfg['ap']}\"' \\\n";
438
	// KD
439
	$chatfile .= "OK 'ATD{$ifcfg['phone']}' \\\n";
440
	$chatfile .= "TIMEOUT 22 \\\n";
441
	$chatfile .= "CONNECT \"\" \\\n";
442
	$chatfile .= "SAY \"\\nConnected.\"\n";
443
	file_put_contents("/etc/ppp/peers/ppp0-connect-chat", $chatfile);
444
	chmod("/etc/ppp/peers/ppp0-connect-chat", 0755);
445
	mwexec("/sbin/ifconfig ppp0 create");
446
	return 0;
447
}
448
449 9f6b1429 Scott Ullrich
function interfaces_carp_configure() {
450 6008210b Scott Ullrich
	global $g, $config, $debugging;
451 2b9747b9 Scott Ullrich
	$balanacing = "";
452
	$pfsyncinterface = "";
453
	$pfsyncenabled = "";
454 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
455
		$mt = microtime();
456
		echo "interfaces_carp_configure() being called $mt\n";
457
	}
458 6008210b Scott Ullrich
	$carp_instances_counter = 0;
459
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
460 467c2c89 Scott Ullrich
	/* destroy previous interfaces */
461 eb772abd Scott Ullrich
	for($x=0; $x<$total_carp_interfaces_defined; $x++)
462
		mwexec("/sbin/ifconfig carp{$x} delete");
463 b932ef16 Scott Ullrich
	if ($g['booting']) {
464 7d0f4544 Scott Ullrich
		echo "Configuring CARP interfaces...";
465
		mute_kernel_msgs();
466 a5250ebc Scott Ullrich
	}
467 b932ef16 Scott Ullrich
	/* suck in configuration items */
468 16ccd95c Scott Ullrich
	if($config['installedpackages']['carpsettings']) 
469
		if($config['installedpackages']['carpsettings']['config']) {
470 b932ef16 Scott Ullrich
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
471
			$pfsyncenabled = $carp['pfsyncenabled'];
472
			$balanacing = $carp['balancing'];
473
			$pfsyncinterface = $carp['pfsyncinterface'];
474 b42ad736 Scott Ullrich
			$pfsyncpeerip = $carp['pfsyncpeerip'];
475 9f6b1429 Scott Ullrich
		}
476 b932ef16 Scott Ullrich
	} else {
477
		unset($pfsyncinterface);
478
		unset($balanacing);
479
		unset($pfsyncenabled);
480 6008210b Scott Ullrich
	}
481 b932ef16 Scott Ullrich
	if($balanacing) {
482
		mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
483
		mwexec("/sbin/sysctl net.inet.carp.preempt=0");
484
	} else {
485
		mwexec("/sbin/sysctl net.inet.carp.preempt=1");
486
	}
487
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
488
	if($g['booting']) {
489
		/*    install rules to alllow pfsync to sync up during boot
490
		 *    carp interfaces will remain down until the bootup sequence finishes
491
		 */
492
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
493
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
494
		exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
495
		exec("/sbin/pfctl -f /tmp/rules.boot");
496 eb772abd Scott Ullrich
	}
497 b932ef16 Scott Ullrich
	/* setup pfsync interface */
498 b42ad736 Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) {
499
		if($pfsyncpeerip) {
500
			mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up");
501
		} else {
502
			mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
503
		}
504
	} else {
505 b932ef16 Scott Ullrich
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
506 b42ad736 Scott Ullrich
	}
507 f6189feb Scott Ullrich
	$fd = fopen("/tmp/carp.sh", "w");
508 eb772abd Scott Ullrich
	if($config['virtualip']['vip']) {
509 b932ef16 Scott Ullrich
		$viparr = &$config['virtualip']['vip'];
510
		mwexec("/sbin/sysctl net.inet.carp.allow=1");
511
	} else {
512
		$viparr = array();
513
		mwexec("/sbin/sysctl net.inet.carp.allow=0");
514
	}
515 6008210b Scott Ullrich
	foreach ($viparr as $vip) {
516
		if ($vip['mode'] == "carp") {
517 8a573737 Scott Ullrich
			$vip_password = $vip['password'];
518 eb772abd Scott Ullrich
			$vip_password = str_replace(" ", "", $vip_password);
519 bb0e29e8 Scott Ullrich
520
			/* ensure CARP IP really exists prior to loading up */
521
			$found = false;
522
			$iflist = array("lan", "wan");
523
			for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
524
				$iflist['opt' . $i] = 'opt' . $i;
525
			foreach($iflist as $if) {
526 9f5e2a88 Scott Ullrich
				/* ignore down or "disabled" interfaces */
527 af668fad Scott Ullrich
				if($if <> "lan" and $if <> "wan")
528
					if (!isset($config['interfaces'][$if]['enable'])) 
529
						continue;								
530 bb0e29e8 Scott Ullrich
				$ww_subnet_ip = $config['interfaces'][$if]['ipaddr'];
531
				$ww_subnet_bits = $config['interfaces'][$if]['subnet'];
532
				if (ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits))
533
					$found = true;
534
			}
535 ca7a3a5c Scott Ullrich
			if($found == false) {
536
				file_notice("CARP", "Sorry but we could not find a matching real interface subnet for the virtual IP address {$vip['subnet']}.", "Firewall: Virtual IP", "");
537
				continue;
538
			}			
539 b932ef16 Scott Ullrich
			/* create the carp interface and setup */
540 6008210b Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
541 3f7d2120 Bill Marquette
542
			/* invalidate interface cache */
543
			get_interface_arr(true);
544
545 6008210b Scott Ullrich
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
546 eb772abd Scott Ullrich
			if($vip['password'] != "")
547 8a573737 Scott Ullrich
				$password = " pass \"" . $vip_password . "\"";
548 6008210b Scott Ullrich
			if($debugging)
549
				echo "Configuring carp{$carp_instances_counter}.\n";
550 f6189feb Scott Ullrich
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password . "\n");
551 9669a414 Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
552 94b636a6 Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
553 be606e9a Scott Ullrich
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " up\n");
554 b932ef16 Scott Ullrich
			usleep(10);
555 6008210b Scott Ullrich
			$carp_instances_counter++;
556
		}
557 75f2c22c Scott Ullrich
	}
558 f6189feb Scott Ullrich
	fclose($fd);
559 b932ef16 Scott Ullrich
	mwexec("/bin/sh /tmp/carp.sh");
560 3c18fbc9 Scott Ullrich
	if ($g['booting']) {
561 deebaae1 Scott Ullrich
		unmute_kernel_msgs();
562 74dbce1f Scott Ullrich
		echo "done.\n";
563 669e1adb Bill Marquette
	}
564 7b2d4769 Bill Marquette
565
	/* update cache */
566 9e097ada Bill Marquette
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
567 eb772abd Scott Ullrich
		find_number_of_created_carp_interfaces(true);
568 9f6b1429 Scott Ullrich
}
569
570 04c5bd17 Scott Ullrich
function interfaces_carp_bring_up_final() {
571 6008210b Scott Ullrich
	global $config, $g, $debugging;
572
	if(isset($config['system']['developerspew'])) {
573
		$mt = microtime();
574
		echo "interfaces_carp_bring_up_final() being called $mt\n";
575
	}
576 d0556634 Scott Ullrich
	if(!$config['virtualip']['vip'])
577 cb77ff27 Scott Ullrich
		return;
578 04c5bd17 Scott Ullrich
	$viparr = &$config['virtualip']['vip'];
579 6008210b Scott Ullrich
	/* could not locate an array, return */
580 eb772abd Scott Ullrich
	if(!is_array($viparr))
581 5b0272bf Scott Ullrich
		return;
582 6008210b Scott Ullrich
	$carp_instances_counter = 0;
583 25530ad8 Scott Ullrich
	$counter = 0;
584 0c6ee817 Scott Ullrich
	if($g['booting'])
585
		echo "Waiting for final CARP interface bringup...";
586 512fa4d7 Scott Ullrich
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
587 44837e6b Scott Ullrich
	if($g['booting']) {
588
		while($supress > 0) {
589
			sleep(2);
590
			$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
591
			if($counter > 15)
592 9123cef8 Scott Ullrich
				$supress = 0;
593 44837e6b Scott Ullrich
			$counter++;
594
			echo ".";
595
		}
596 2d1e01b0 Scott Ullrich
		echo " done.\n";
597 44837e6b Scott Ullrich
	}
598 04c5bd17 Scott Ullrich
	foreach ($viparr as $vip) {
599 6e42e038 Bill Marquette
		/* bail if this isn't a carp VIP */
600
		if ($vip['mode'] != "carp")
601
			continue;
602
603 6008210b Scott Ullrich
		if($debugging)
604
			echo "Upping interface carp{$carp_instances_counter}.\n";
605
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
606 eb772abd Scott Ullrich
		if($vip['password'] != "")
607 6008210b Scott Ullrich
			$password = " pass " . $vip['password'];
608
		if($debugging)
609
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
610
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
611 5f2ad497 Scott Ullrich
		sleep(1);
612
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
613 04c5bd17 Scott Ullrich
		$carp_instances_counter++;
614
	}
615 0c6ee817 Scott Ullrich
	if($g['booting'])
616 eb772abd Scott Ullrich
		echo " done.\n";
617 04c5bd17 Scott Ullrich
}
618
619 a04de17f Chris Buechler
function interfaces_ipalias_configure() {
620
	global $g, $config, $debugging;
621
	if(isset($config['system']['developerspew'])) {
622
		$mt = microtime();
623
		echo "interfaces_ipalias_configure() being called $mt\n";
624
	}
625
	$viparr = &$config['virtualip']['vip'];
626 6a74c90e Scott Ullrich
	if(is_array($viparr)) {
627
		foreach ($viparr as $vip) {
628
			if ($vip['mode'] == "ipalias") {
629
				$if = $vip['interface'];
630
				mwexec("/sbin/ifconfig " . escapeshellarg($config['interfaces'][$if]['if']) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
631
			}
632 a04de17f Chris Buechler
		}
633
	}
634
}
635
636 5b237745 Scott Ullrich
function interfaces_wireless_configure($if, $wlcfg) {
637 ac3f8318 Espen Johansen
	global $config, $g;
638 eb772abd Scott Ullrich
639 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
640
	 *    since wireless is changing a lot, these series of commands are fragile
641
     *    and will sometimes need to be verified by a operator by executing the command
642
     *    and returning the output of the command to the developers for inspection.  please
643
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
644
	 */
645 eb772abd Scott Ullrich
646 8a958125 Scott Ullrich
	conf_mount_rw();
647 eb772abd Scott Ullrich
648 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
649 eb772abd Scott Ullrich
650 4742e927 Scott Ullrich
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
651
	fwrite($fd_set, "#!/bin/sh\n");
652 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
653 eb772abd Scott Ullrich
654 4742e927 Scott Ullrich
	fwrite($fd_set, "# enable shell debugging\n");
655
	fwrite($fd_set, "set -x\n");
656 eb772abd Scott Ullrich
657 2ac908dd Espen Johansen
	/* set values for /path/program */
658
	$hostapd = "/usr/sbin/hostapd";
659
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
660 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
661
	$killall = "/usr/bin/killall";
662 2ac908dd Espen Johansen
663 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
664 5508cf57 Scott Ullrich
665 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
666 f4c9d138 Scott Ullrich
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
667 5508cf57 Scott Ullrich
668 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
669
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
670
671 ac3f8318 Espen Johansen
	/* set wireless channel value */
672 ea9d29fa Scott Ullrich
	if(isset($wlcfg['channel']))
673 f4c9d138 Scott Ullrich
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
674 2ac908dd Espen Johansen
675 f134033e Scott Ullrich
	/* set Distance value */
676 eb772abd Scott Ullrich
	if($wlcfg['distance'])
677 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
678
679 ac3f8318 Espen Johansen
	/* Set ssid */
680 eb772abd Scott Ullrich
	if($wlcfg['ssid'])
681 191a8175 Scott Ullrich
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
682 eb772abd Scott Ullrich
683 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
684 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "hostap")
685 ac3f8318 Espen Johansen
		$hostapmode = "mediaopt hostap";
686
	else
687
		$hostapmode = "-mediaopt hostap";
688
689
	/* Set wireless adhoc mode */
690 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "adhoc")
691 ac3f8318 Espen Johansen
		$adhocmode = "mediaopt adhoc";
692
	else
693
		$adhocmode = "-mediaopt adhoc";
694
695
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
696
697
	/* handle hide ssid option */
698
	if(isset($wlcfg['hidessid']['enable']))
699
		$hidessid = "hidessid";
700
	else
701
		$hidessid = "-hidessid";
702
703
	/* handle pureg (802.11g) only option */
704
	if(isset($wlcfg['pureg']['enable']))
705
		$pureg = "mode 11g pureg";
706
	else
707
		$pureg = "-pureg";
708
709
	/* enable apbridge option */
710
	if(isset($wlcfg['apbridge']['enable']))
711
		$apbridge = "apbridge";
712
	else
713
		$apbridge = "-apbridge";
714
715
	/* handle turbo option */
716
	if(isset($wlcfg['turbo']['enable']))
717
		$turbo = "mediaopt turbo";
718
	else
719
		$turbo = "-mediaopt turbo";
720
721
	/* handle txpower setting */
722
	if($wlcfg['txpower'] <> "")
723 f4c9d138 Scott Ullrich
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
724 eb772abd Scott Ullrich
725 ac3f8318 Espen Johansen
	/* handle wme option */
726
	if(isset($wlcfg['wme']['enable']))
727
		$wme = "wme";
728
	else
729
		$wme = "-wme";
730 eb772abd Scott Ullrich
731 ac3f8318 Espen Johansen
	/* set up wep if enabled */
732 2f19fa14 Scott Ullrich
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
733 eb772abd Scott Ullrich
		if($wlcfg['wpa']['auth_algs'] == "1")
734 2f19fa14 Scott Ullrich
			$wepset .= "authmode open wepmode on ";
735
		else if($wlcfg['wpa']['auth_algs'] == "2")
736
			$wepset .= "authmode shared wepmode on ";
737
		else if($wlcfg['wpa']['auth_algs'] == "3")
738 eb772abd Scott Ullrich
			$wepset .= "authmode mixed wepmode on ";
739 2f19fa14 Scott Ullrich
		$i = 1;
740
		foreach ($wlcfg['wep']['key'] as $wepkey) {
741
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
742 eb772abd Scott Ullrich
			if (isset($wepkey['txkey']))
743 2f19fa14 Scott Ullrich
				$wepset .= "weptxkey {$i} ";
744
			$i++;
745
		}
746
    } else {
747
    	$wepset .= "authmode open wepmode off ";
748 ac3f8318 Espen Johansen
	}
749
750
	/* generate wpa_supplicant/hostap config if wpa is enabled */
751
752
	switch ($wlcfg['mode']) {
753 b67d192d Scott Ullrich
		case 'bss':
754 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
755
756
				$wpa .= <<<EOD
757 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
758 50ad3b7c Scott Ullrich
ctrl_interface_group=0
759
ap_scan=1
760 2ac908dd Espen Johansen
#fast_reauth=1
761 249558a2 Scott Ullrich
network={
762 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
763
scan_ssid=1
764 2ac908dd Espen Johansen
priority=5
765
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
766 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
767 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
768
group={$wlcfg['wpa']['wpa_pairwise']}
769 50ad3b7c Scott Ullrich
}
770
EOD;
771
772 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
773 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
774
				fclose($fd);
775 8d27a5fe Espen Johansen
776 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_wpasupplicant($if));
777 ac3f8318 Espen Johansen
			}
778
		break;
779 459d6351 Scott Ullrich
780 ac3f8318 Espen Johansen
		case 'hostap':
781
			if (isset($wlcfg['wpa']['enable'])) {
782
				$wpa .= <<<EOD
783 459d6351 Scott Ullrich
interface={$if}
784
driver=bsd
785
logger_syslog=-1
786
logger_syslog_level=0
787
logger_stdout=-1
788
logger_stdout_level=0
789 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
790
ctrl_interface={$g['varrun_path']}/hostapd
791 459d6351 Scott Ullrich
ctrl_interface_group=wheel
792 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
793
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
794 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
795 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
796 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
797
auth_algs={$wlcfg['wpa']['auth_algs']}
798
wpa={$wlcfg['wpa']['wpa_mode']}
799
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
800
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
801 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
802
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
803
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
804 2ac908dd Espen Johansen
wpa_passphrase={$wlcfg['wpa']['passphrase']}
805
ieee8021x={$wlcfg['wpa']['ieee8021x']}
806 53dfd34e Espen Johansen
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
807
#rsn_preauth=1
808
#rsn_preauth_interfaces=eth0
809 459d6351 Scott Ullrich
EOD;
810 2ac908dd Espen Johansen
811 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
812 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
813
				fclose($fd);
814 2ac908dd Espen Johansen
815 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_hostapd($if));
816 ac3f8318 Espen Johansen
			}
817
		break;
818 5508cf57 Scott Ullrich
819 ac3f8318 Espen Johansen
		case 'adhoc':
820 99e72ce8 Scott Ullrich
			fwrite($fd_set, kill_hostapd($if));
821
			fwrite($fd_set, kill_wpasupplicant($if));
822 ac3f8318 Espen Johansen
		break;
823 eb772abd Scott Ullrich
	}
824 ac3f8318 Espen Johansen
825 4742e927 Scott Ullrich
	/*
826
	 *    all variables are set, lets start up everything
827
     */
828 eb772abd Scott Ullrich
829 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
830
	if($distance) {
831 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
832
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
833 78922914 Scott Ullrich
	}
834 eb772abd Scott Ullrich
835 4d857dcf Scott Ullrich
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
836 eb772abd Scott Ullrich
837 4742e927 Scott Ullrich
	$settings = <<<EOD
838
839 2f19fa14 Scott Ullrich
{$ifconfig} {$if} down
840 4742e927 Scott Ullrich
{$ifconfig} {$if} {$hostapmode}
841 490b8b2a Scott Ullrich
{$ifconfig} {$if} {$standard_no_turbo}
842 8a958125 Scott Ullrich
{$ifconfig} {$if} {$channel}
843 4742e927 Scott Ullrich
{$ifconfig} {$if} {$turbo}
844 8a958125 Scott Ullrich
{$ifconfig} {$if} {$ssid}
845 4742e927 Scott Ullrich
{$ifconfig} {$if} {$hidessid}
846 8a958125 Scott Ullrich
{$ifconfig} {$if} {$adhocmode}
847
{$ifconfig} {$if} {$protmode}
848 4742e927 Scott Ullrich
{$ifconfig} {$if} {$pureg}
849
{$ifconfig} {$if} {$apbridge}
850
{$ifconfig} {$if} {$wme}
851
{$ifconfig} {$if} {$wepset}
852
{$ifconfig} {$if} {$txpower}
853
{$ifconfig} {$if} up
854
855
EOD;
856 eb772abd Scott Ullrich
857 4742e927 Scott Ullrich
	/* write out above <<EOD stuff */
858
	fwrite($fd_set, $settings);
859 da1dab20 Scott Ullrich
860 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
861 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "bss")
862 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
863 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "hostap")
864 4742e927 Scott Ullrich
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
865 ac3f8318 Espen Johansen
	}
866 191a8175 Scott Ullrich
867 4742e927 Scott Ullrich
	fclose($fd_set);
868
869 8a958125 Scott Ullrich
	conf_mount_ro();
870
871 4742e927 Scott Ullrich
	/* execute commands now in shell */
872
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
873 99e72ce8 Scott Ullrich
	sleep(2);
874 490b8b2a Scott Ullrich
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
875 191a8175 Scott Ullrich
876 ac3f8318 Espen Johansen
	return 0;
877 cfc707f7 Scott Ullrich
878 5b237745 Scott Ullrich
}
879
880 4b2a6180 Scott Ullrich
function kill_hostapd($interface) {
881 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
882 4b2a6180 Scott Ullrich
}
883
884
function kill_wpasupplicant($interface) {
885 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
886 4b2a6180 Scott Ullrich
}
887
888 0311dbd5 Scott Ullrich
function find_dhclient_process($interface) {
889 2305d4c5 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
890
        	$realinterface = filter_translate_type_to_real_interface($interface);
891 89fcabc4 Scott Ullrich
	if($realinterface)
892
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
893 0311dbd5 Scott Ullrich
	return $pid;
894
}
895
896 5b237745 Scott Ullrich
function interfaces_wan_configure() {
897 0831bc86 Scott Ullrich
	global $config, $g, $bridges_total;
898 cfc707f7 Scott Ullrich
899 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
900 cfc707f7 Scott Ullrich
901 d04d8d42 Scott Ullrich
	if(file_exists("/tmp/{$config['interfaces']['wan']['if']}_router")) 
902
		unlink("/tmp/{$config['interfaces']['wan']['if']}_router");
903
904 5c6d0f65 Colin Smith
	if(!$g['booting']) {
905 c1627786 Scott Ullrich
		mute_kernel_msgs();
906 0311dbd5 Scott Ullrich
907
		/* find dhclient process for wan and kill it */
908 2305d4c5 Scott Ullrich
		killbypid(find_dhclient_process("wan"));
909 cfc707f7 Scott Ullrich
910 d7a6517a Scott Ullrich
		/* remove wanup file if it exists */
911
		unlink_if_exists("{$g['tmp_path']}/wanup");
912
913 5b237745 Scott Ullrich
		/* kill PPPoE client (mpd) */
914
		killbypid("{$g['varrun_path']}/mpd.pid");
915 cfc707f7 Scott Ullrich
916 5b237745 Scott Ullrich
		/* wait for processes to die */
917 d7a6517a Scott Ullrich
		sleep(3);
918 cfc707f7 Scott Ullrich
919 0311dbd5 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
920 a23d7248 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
921
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
922
		unlink_if_exists("{$g['vardb_path']}/wanip");
923
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
924 5b237745 Scott Ullrich
	}
925 cfc707f7 Scott Ullrich
926 5b237745 Scott Ullrich
	/* remove all addresses first */
927
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
928
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
929 cfc707f7 Scott Ullrich
930 5b237745 Scott Ullrich
	/* wireless configuration? */
931
	if (is_array($wancfg['wireless']))
932
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
933 cfc707f7 Scott Ullrich
934 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
935 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
936 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
937 f36d4bd2 Scott Ullrich
	}  else {
938
		$mac = get_interface_mac_address($wancfg['if']);
939
		if($mac == "ff:ff:ff:ff:ff:ff") {
940
			/*   this is not a valid mac address.  generate a
941
			 *   temporary mac address so the machine can get online.
942
			 */
943 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
944 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
945
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
946
				" link " . escapeshellarg($random_mac));
947
			$wancfg['spoofmac'] = $random_mac;
948
			write_config();
949 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");
950 f36d4bd2 Scott Ullrich
		}
951
	}
952 cfc707f7 Scott Ullrich
953 5b237745 Scott Ullrich
	/* media */
954
	if ($wancfg['media'] || $wancfg['mediaopt']) {
955
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
956
		if ($wancfg['media'])
957
			$cmd .= " media " . escapeshellarg($wancfg['media']);
958
		if ($wancfg['mediaopt'])
959
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
960
		mwexec($cmd);
961
	}
962 cfc707f7 Scott Ullrich
963 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
964 cfc707f7 Scott Ullrich
965 5b237745 Scott Ullrich
		case 'dhcp':
966
			interfaces_wan_dhcp_configure();
967
			break;
968 cfc707f7 Scott Ullrich
969 5b237745 Scott Ullrich
		case 'pppoe':
970
			interfaces_wan_pppoe_configure();
971
			break;
972 cfc707f7 Scott Ullrich
973 5b237745 Scott Ullrich
		case 'pptp':
974
			interfaces_wan_pptp_configure();
975
			break;
976 cfc707f7 Scott Ullrich
977 5b237745 Scott Ullrich
		case 'bigpond':
978
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
979
			interfaces_wan_dhcp_configure();
980
			break;
981 cfc707f7 Scott Ullrich
982 5b237745 Scott Ullrich
		default:
983 a23d7248 Scott Ullrich
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
984
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
985
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
986
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
987
			} else {
988
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
989
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
990
			}
991 cfc707f7 Scott Ullrich
992 d04d8d42 Scott Ullrich
			if($config['interfaces']['wan']['gateway'])
993
				system("echo " . $config['interfaces']['wan']['gateway'] . " > /tmp/" . $config['interfaces']['wan']['if'] . "_router");
994
995 3c9daf99 Scott Ullrich
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
996
			filter_configure();
997 5b237745 Scott Ullrich
	}
998 cfc707f7 Scott Ullrich
999 77712c1e Scott Ullrich
	if ($wancfg['bridge']) {
1000
		/* use open/netBSD style bridge */
1001
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
1002 3f7d2120 Bill Marquette
1003
		/* invalidate interface cache */
1004
		get_interface_arr(true);
1005 eb772abd Scott Ullrich
1006 77712c1e Scott Ullrich
		/* force all bridged interfaces to use same mtu */
1007
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
1008
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
1009
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
1010 eb772abd Scott Ullrich
1011 77712c1e Scott Ullrich
		/* assign items to a bridge */
1012 a591b9cb Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
1013 df792110 Scott Ullrich
1014 e7aa7560 Scott Ullrich
		if(!is_interface_wireless($wancfg['if']) and
1015 eb772abd Scott Ullrich
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1016 3789e4df Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
1017 eb772abd Scott Ullrich
1018 77712c1e Scott Ullrich
		/* log commands run for debugging in /tmp/ */
1019
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
1020
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
1021
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
1022
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
1023 b3a66d90 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1024 12fea2f7 Scott Ullrich
		if(!is_interface_wireless($wancfg['if']) and
1025
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1026
				fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1027 77712c1e Scott Ullrich
		fclose($fd);
1028 eb772abd Scott Ullrich
1029 77712c1e Scott Ullrich
		/* bring up interfaces */
1030
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
1031
		usleep(100);
1032
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
1033
		usleep(5);
1034
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
1035
		usleep(5);
1036
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
1037 eb772abd Scott Ullrich
1038 c60e4f51 Scott Ullrich
		$bridges_total++;
1039 547a126d Bill Marquette
		/* update cache */
1040 3f7d2120 Bill Marquette
		if ($bridges_total != find_number_of_created_bridges())
1041
			find_number_of_created_bridges(true);
1042 77712c1e Scott Ullrich
	}
1043
1044 5b237745 Scott Ullrich
	if (!$g['booting']) {
1045
		/* reconfigure static routes (kernel may have deleted them) */
1046
		system_routing_configure();
1047 cfc707f7 Scott Ullrich
1048 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
1049 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
1050 cfc707f7 Scott Ullrich
1051 5b237745 Scott Ullrich
		/* reload ipsec tunnels */
1052
		vpn_ipsec_configure();
1053 cfc707f7 Scott Ullrich
1054 5b237745 Scott Ullrich
		/* restart ez-ipupdate */
1055
		services_dyndns_configure();
1056 cfc707f7 Scott Ullrich
1057 a23d7248 Scott Ullrich
		/* force DNS update */
1058
		services_dnsupdate_process();
1059
1060 5b237745 Scott Ullrich
		/* restart dnsmasq */
1061
		services_dnsmasq_configure();
1062 eb772abd Scott Ullrich
1063 c597d50f Scott Ullrich
		/* reload captive portal */
1064
		captiveportal_configure();
1065 5b237745 Scott Ullrich
	}
1066 cfc707f7 Scott Ullrich
1067 5e99d81a Scott Ullrich
	mwexec("/sbin/ifconfig {$wancfg['if']} up");
1068
1069 c1627786 Scott Ullrich
	unmute_kernel_msgs();
1070
1071 5b237745 Scott Ullrich
	return 0;
1072
}
1073
1074 0311dbd5 Scott Ullrich
function interfaces_opt_dhcp_configure($interface) {
1075
	global $config, $g;
1076
1077 1223f922 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
1078 45870464 Scott Ullrich
	$optif = $optcfg['if'];
1079 0311dbd5 Scott Ullrich
1080
	/* generate dhclient_wan.conf */
1081 aab78cf6 Scott Ullrich
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
1082 0311dbd5 Scott Ullrich
	if (!$fd) {
1083 aab78cf6 Scott Ullrich
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
1084 0311dbd5 Scott Ullrich
		return 1;
1085
	}
1086
1087 2305d4c5 Scott Ullrich
	if ($optcfg['dhcphostname']) {
1088
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1089
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1090
	} else {
1091
		$dhclientconf_hostname = "";
1092
	}
1093
1094 0311dbd5 Scott Ullrich
 	$dhclientconf = "";
1095
1096 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1097 1db50557 Chris Buechler
timeout 60;
1098 0709cd0b Scott Ullrich
retry 1;
1099
select-timeout 0;
1100
initial-interval 1;
1101 6d76590c Scott Ullrich
interface "{$optif}" {
1102 2305d4c5 Scott Ullrich
	script "/sbin/dhclient-script";
1103
	{$dhclientconf_hostname}
1104 0311dbd5 Scott Ullrich
}
1105
1106
EOD;
1107
1108 bc40d758 Seth Mos
if(is_ipaddr($optcfg['alias-address'])) {
1109
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1110
	$dhclientconf .= <<<EOD
1111
alias {
1112
	interface  "{$optif}";
1113
	fixed-address {$optcfg['alias-address']};
1114
	option subnet-mask {$subnetmask};
1115
}
1116
1117
EOD;
1118
}
1119 0311dbd5 Scott Ullrich
	fwrite($fd, $dhclientconf);
1120
	fclose($fd);
1121 45870464 Scott Ullrich
1122 0f1b5370 Scott Ullrich
        /* bring interface up before starting dhclient */
1123 0311dbd5 Scott Ullrich
        mwexec("/sbin/ifconfig {$optif} up");
1124
1125
        /* fire up dhclient */
1126 2305d4c5 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
1127 0311dbd5 Scott Ullrich
1128
	return 0;
1129
}
1130
1131 8c3e8725 Scott Ullrich
function interfaces_dhcp_configure($interface) {
1132
	global $config, $g;
1133
1134 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
1135
        	$realinterface = filter_translate_type_to_real_interface($interface);
1136 6d76590c Scott Ullrich
1137 8c3e8725 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
1138
1139
	/* generate dhclient_$interface.conf */
1140
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1141
	if (!$fd) {
1142
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
1143
		return 1;
1144
	}
1145
1146 2305d4c5 Scott Ullrich
	if ($optcfg['dhcphostname']) {
1147
		$dhclientconf_hostname =  "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1148
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1149
	} else {
1150
		$dhclientconf_hostname = "";
1151
	}
1152
1153 8c3e8725 Scott Ullrich
 	$dhclientconf = "";
1154
1155 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1156 76d3b9a3 Chris Buechler
timeout 60;
1157 0709cd0b Scott Ullrich
retry 1;
1158
select-timeout 0;
1159
initial-interval 1;
1160 84cec030 Scott Ullrich
interface "{$realinterface}" {
1161 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
1162
	script "/sbin/dhclient-script";
1163 8c3e8725 Scott Ullrich
}
1164
1165
EOD;
1166
1167 bc40d758 Seth Mos
if(is_ipaddr($optcfg['alias-address'])) {
1168
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1169
	$dhclientconf .= <<<EOD
1170
alias {
1171
	interface  "{$optif}";
1172
	fixed-address {$optcfg['alias-address']};
1173
	option subnet-mask {$subnetmask};
1174
}
1175
1176
EOD;
1177
}
1178
1179 8c3e8725 Scott Ullrich
	fwrite($fd, $dhclientconf);
1180
	fclose($fd);
1181 eb772abd Scott Ullrich
1182 8c3e8725 Scott Ullrich
	$optif = $optcfg['if'];
1183 eb772abd Scott Ullrich
1184 8c3e8725 Scott Ullrich
        /* bring wan interface up before starting dhclient */
1185
        mwexec("/sbin/ifconfig {$optif} up");
1186
1187
        /* fire up dhclient */
1188 2305d4c5 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
1189 8c3e8725 Scott Ullrich
1190
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
1191 2305d4c5 Scott Ullrich
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
1192 8c3e8725 Scott Ullrich
	fclose($fout);
1193
1194
	return 0;
1195
}
1196
1197 5b237745 Scott Ullrich
function interfaces_wan_dhcp_configure() {
1198
	global $config, $g;
1199 cfc707f7 Scott Ullrich
1200 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1201
1202 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
1203
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
1204 5b237745 Scott Ullrich
	if (!$fd) {
1205 0311dbd5 Scott Ullrich
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
1206 5b237745 Scott Ullrich
		return 1;
1207
	}
1208 eb772abd Scott Ullrich
1209 2305d4c5 Scott Ullrich
	if ($wancfg['dhcphostname']) {
1210
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1211
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1212
	} else {
1213
		$dhclientconf_hostname = "";
1214
	}
1215
1216 5b237745 Scott Ullrich
 	$dhclientconf = "";
1217 cfc707f7 Scott Ullrich
1218 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1219 d19cc554 Scott Ullrich
interface "{$wancfg['if']}" {
1220 76d3b9a3 Chris Buechler
timeout 60;
1221 ce69a638 Scott Ullrich
retry 1;
1222
select-timeout 0;
1223
initial-interval 1;
1224 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
1225
	script "/sbin/dhclient-script";
1226 5b237745 Scott Ullrich
}
1227
1228
EOD;
1229
1230 bc40d758 Seth Mos
if(is_ipaddr($wancfg['alias-address'])) {
1231
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1232
	$dhclientconf .= <<<EOD
1233
alias {
1234
	interface  "{$wancfg['if']}";
1235
	fixed-address {$wancfg['alias-address']};
1236
	option subnet-mask {$subnetmask};
1237
}
1238
1239
EOD;
1240
}
1241 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
1242
	fclose($fd);
1243 eb772abd Scott Ullrich
1244 0521b82d Scott Ullrich
	$wanif = $wancfg['if'];
1245 eb772abd Scott Ullrich
1246 eacc8c14 Scott Ullrich
        /* bring wan interface up before starting dhclient */
1247 0521b82d Scott Ullrich
        mwexec("/sbin/ifconfig {$wanif} up");
1248 eacc8c14 Scott Ullrich
1249 0521b82d Scott Ullrich
        /* fire up dhclient */
1250 2305d4c5 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1251 cfc707f7 Scott Ullrich
1252 fdca0ea8 Scott Ullrich
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1253 2305d4c5 Scott Ullrich
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1254 0119d2f7 Scott Ullrich
	fclose($fout);
1255
1256 5b237745 Scott Ullrich
	return 0;
1257
}
1258
1259 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_down() {
1260 2305d4c5 Scott Ullrich
	global $config;
1261
	$wancfg = $config['interfaces']['wan'];
1262
	$wanif = $wancfg['if'];
1263
	mwexec("/sbin/ifconfig {$wanif} delete");
1264
	sleep(1);
1265 a23d7248 Scott Ullrich
}
1266
1267 468cee8d Scott Ullrich
function interfaces_dhcp_down($interface) {
1268 2305d4c5 Scott Ullrich
	global $config;
1269
	if(filter_translate_type_to_real_interface($interface) <> "")
1270
		$realinterface = filter_translate_type_to_real_interface($interface);
1271
	mwexec("/sbin/ifconfig {$realinterface} down");
1272
	sleep(1);
1273
	$pid = find_dhclient_process($interface);
1274
	if($pid)
1275
		mwexec("kill {$pid}");
1276 468cee8d Scott Ullrich
}
1277
1278 8c3e8725 Scott Ullrich
function interfaces_dhcp_up($interface) {
1279
	interfaces_dhcp_configure($interface);
1280
	sleep(1);
1281
}
1282
1283 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_up() {
1284
	interfaces_wan_dhcp_configure();
1285 8551d2ef Scott Ullrich
	sleep(1);
1286 a23d7248 Scott Ullrich
}
1287
1288 5b237745 Scott Ullrich
function interfaces_wan_pppoe_configure() {
1289
	global $config, $g;
1290 cfc707f7 Scott Ullrich
1291 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1292
	$pppoecfg = $config['pppoe'];
1293 cfc707f7 Scott Ullrich
1294 5b237745 Scott Ullrich
	/* generate mpd.conf */
1295
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1296
	if (!$fd) {
1297
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1298
		return 1;
1299
	}
1300 cfc707f7 Scott Ullrich
1301 5b237745 Scott Ullrich
	$idle = 0;
1302 cfc707f7 Scott Ullrich
1303 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
1304
		$ondemand = "enable";
1305
		if ($pppoecfg['timeout'])
1306
			$idle = $pppoecfg['timeout'];
1307
	} else {
1308
		$ondemand = "disable";
1309
	}
1310 cfc707f7 Scott Ullrich
1311 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1312 07cae4b2 Scott Ullrich
startup:
1313
pppoeclient:
1314 6dd9b015 Ermal Luçi
	new -i pppoe0 pppoeclient pppoeclient
1315 5b237745 Scott Ullrich
	set iface route default
1316
	set iface {$ondemand} on-demand
1317
	set iface idle {$idle}
1318
	set iface up-script /usr/local/sbin/ppp-linkup
1319 cc936773 Scott Ullrich
1320 5b237745 Scott Ullrich
EOD;
1321 389741e5 Scott Ullrich
1322
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1323
	 *    Create reference in mpd.conf
1324
	 */
1325
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1326
		$mpdconf .= <<<EOD
1327
	set iface down-script /usr/local/sbin/ppp-linkdown
1328
1329
EOD;
1330
	}
1331
1332 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
1333 41404ef1 Scott Ullrich
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1334
			$mpdconf .= <<<EOD
1335
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1336 5b237745 Scott Ullrich
1337
EOD;
1338 41404ef1 Scott Ullrich
		} else {
1339
			$mpdconf .= <<<EOD
1340
	set iface addrs 192.0.2.112 192.0.2.113
1341
1342
EOD;
1343
		}
1344 5b237745 Scott Ullrich
	}
1345 cfc707f7 Scott Ullrich
1346 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1347
	set bundle disable multilink
1348 07cae4b2 Scott Ullrich
	set auth authname "{$pppoecfg['username']}"
1349
	set auth password "{$pppoecfg['password']}"
1350 5b237745 Scott Ullrich
	set link keep-alive 10 60
1351
	set link max-redial 0
1352
	set link no acfcomp protocomp
1353
	set link disable pap chap
1354
	set link accept chap
1355
	set link mtu 1492
1356
	set ipcp yes vjcomp
1357
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1358 a23d7248 Scott Ullrich
1359 07cae4b2 Scott Ullrich
1360
1361 a23d7248 Scott Ullrich
EOD;
1362
1363
	if (isset($config['system']['dnsallowoverride'])) {
1364
		$mpdconf .= <<<EOD
1365 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1366 a23d7248 Scott Ullrich
1367
EOD;
1368
	}
1369 a0ff9696 Scott Ullrich
1370 98e392c5 Scott Ullrich
	if (!isset($config['pppoe']['dnsnosec'])) {
1371
			$mpdconf .= <<<EOD
1372
	set ipcp enable req-sec-dns
1373
1374
EOD;
1375
	}
1376
	
1377 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1378 07cae4b2 Scott Ullrich
	open
1379 5b237745 Scott Ullrich
1380
EOD;
1381
1382
	fwrite($fd, $mpdconf);
1383
	fclose($fd);
1384 cfc707f7 Scott Ullrich
1385 5b237745 Scott Ullrich
	/* generate mpd.links */
1386
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1387
	if (!$fd) {
1388
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1389
		return 1;
1390
	}
1391 cfc707f7 Scott Ullrich
1392 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1393 07cae4b2 Scott Ullrich
pppoeclient:
1394 5b237745 Scott Ullrich
	set link type pppoe
1395
	set pppoe iface {$wancfg['if']}
1396
	set pppoe service "{$pppoecfg['provider']}"
1397
	set pppoe enable originate
1398
	set pppoe disable incoming
1399
1400
EOD;
1401
1402
	fwrite($fd, $mpdconf);
1403
	fclose($fd);
1404 eb772abd Scott Ullrich
1405 07cae4b2 Scott Ullrich
	if(file_exists("{$g['varrun_path']}/mpdpppoe.pid") and $g['booting']) {
1406 d7a6517a Scott Ullrich
		/* if we are booting and mpd has already been started then don't start again. */
1407
	} else {
1408
		/* if mpd is active, lets take it down */
1409 07cae4b2 Scott Ullrich
		if(file_exists("{$g['varrun_path']}/mpdpppoe.pid")) {
1410
			killbypid("{$g['varrun_path']}/mpdpppoe.pid");
1411 d7a6517a Scott Ullrich
			sleep(3);
1412
		}
1413 eb772abd Scott Ullrich
		/* fire up mpd */
1414 07cae4b2 Scott Ullrich
		mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpdpppoe.pid pppoeclient");
1415 ec11a1ad Scott Ullrich
	}
1416
1417 a205d904 Scott Ullrich
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1418
	for ($count = 0; $count < 30; $count++) {
1419
		if(file_exists("{$g['tmp_path']}/wanup")) {
1420
			break;
1421
		}
1422
		sleep(1);
1423
	}
1424 d7a6517a Scott Ullrich
1425 a205d904 Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/wanup");
1426 e1c8cdf5 Scott Ullrich
1427 5b237745 Scott Ullrich
	return 0;
1428
}
1429
1430 58af5941 Scott Ullrich
function interfaces_wan_pppoe_restart() {
1431
	interfaces_wan_pppoe_down();
1432
	sleep(1);
1433
	interfaces_wan_pppoe_up();
1434
}
1435
1436 a23d7248 Scott Ullrich
function interfaces_wan_pppoe_down() {
1437
	global $g;
1438 07cae4b2 Scott Ullrich
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR2");
1439 8551d2ef Scott Ullrich
	sleep(1);
1440 a23d7248 Scott Ullrich
}
1441
1442
function interfaces_wan_pppoe_up() {
1443
	global $g;
1444 07cae4b2 Scott Ullrich
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR1");
1445 8551d2ef Scott Ullrich
	sleep(1);
1446 a23d7248 Scott Ullrich
}
1447
1448 5b237745 Scott Ullrich
function interfaces_wan_pptp_configure() {
1449
	global $config, $g;
1450 cfc707f7 Scott Ullrich
1451 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1452
	$pptpcfg = $config['pptp'];
1453 cfc707f7 Scott Ullrich
1454 5b237745 Scott Ullrich
	/* generate mpd.conf */
1455
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1456
	if (!$fd) {
1457
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1458
		return 1;
1459
	}
1460 cfc707f7 Scott Ullrich
1461 5b237745 Scott Ullrich
	$idle = 0;
1462 cfc707f7 Scott Ullrich
1463 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
1464
		$ondemand = "enable";
1465
		if ($pptpcfg['timeout'])
1466
			$idle = $pptpcfg['timeout'];
1467
	} else {
1468
		$ondemand = "disable";
1469
	}
1470 cfc707f7 Scott Ullrich
1471 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1472
pptp:
1473 6dd9b015 Ermal Luçi
	new -i pptp0 pptp pptp
1474 5b237745 Scott Ullrich
	set iface route default
1475
	set iface {$ondemand} on-demand
1476
	set iface idle {$idle}
1477
	set iface up-script /usr/local/sbin/ppp-linkup
1478
1479
EOD;
1480 cfc707f7 Scott Ullrich
1481 389741e5 Scott Ullrich
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1482
	 *   Create reference in mpd.conf
1483
	 */
1484
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1485
		$mpdconf .= <<<EOD
1486
	set iface down-script /usr/local/sbin/ppp-linkdown
1487
1488
EOD;
1489
	}
1490
1491 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
1492
		$mpdconf .= <<<EOD
1493 a23d7248 Scott Ullrich
	set iface addrs 10.0.0.1 10.0.0.2
1494 5b237745 Scott Ullrich
1495
EOD;
1496
	}
1497 cfc707f7 Scott Ullrich
1498 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1499
	set bundle disable multilink
1500
	set bundle authname "{$pptpcfg['username']}"
1501
	set bundle password "{$pptpcfg['password']}"
1502
	set link keep-alive 10 60
1503
	set link max-redial 0
1504
	set link no acfcomp protocomp
1505
	set link disable pap chap
1506
	set link accept chap
1507
	set ipcp no vjcomp
1508
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1509 a23d7248 Scott Ullrich
1510
EOD;
1511
	if (isset($config['system']['dnsallowoverride'])) {
1512
		$mpdconf .= <<<EOD
1513 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1514 a23d7248 Scott Ullrich
1515
EOD;
1516
	}
1517 a0ff9696 Scott Ullrich
1518 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1519 5b237745 Scott Ullrich
	open
1520
1521
EOD;
1522
1523
	fwrite($fd, $mpdconf);
1524
	fclose($fd);
1525 cfc707f7 Scott Ullrich
1526 5b237745 Scott Ullrich
	/* generate mpd.links */
1527
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1528
	if (!$fd) {
1529
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1530
		return 1;
1531
	}
1532 cfc707f7 Scott Ullrich
1533 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1534
pptp:
1535
	set link type pptp
1536
	set pptp enable originate outcall
1537
	set pptp disable windowing
1538
	set pptp self {$pptpcfg['local']}
1539
	set pptp peer {$pptpcfg['remote']}
1540
1541
EOD;
1542
1543
	fwrite($fd, $mpdconf);
1544
	fclose($fd);
1545 cfc707f7 Scott Ullrich
1546 5b237745 Scott Ullrich
	/* configure interface */
1547 cfc707f7 Scott Ullrich
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1548 5b237745 Scott Ullrich
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1549 cfc707f7 Scott Ullrich
1550 5b237745 Scott Ullrich
	/* fire up mpd */
1551
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pptp");
1552 cfc707f7 Scott Ullrich
1553 5b237745 Scott Ullrich
	return 0;
1554
}
1555
1556 58af5941 Scott Ullrich
function interfaces_wan_pptp_restart() {
1557
	interfaces_wan_pptp_down();
1558
	sleep(1);
1559
	interfaces_wan_pptp_up();
1560
}
1561
1562 a23d7248 Scott Ullrich
function interfaces_wan_pptp_down() {
1563
	global $g;
1564
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1565 8551d2ef Scott Ullrich
	sleep(1);
1566 a23d7248 Scott Ullrich
}
1567
1568
function interfaces_wan_pptp_up() {
1569
	global $g;
1570
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1571 8551d2ef Scott Ullrich
	sleep(1);
1572 a23d7248 Scott Ullrich
}
1573
1574 5b237745 Scott Ullrich
function interfaces_wan_bigpond_configure($curwanip) {
1575
	global $config, $g;
1576 cfc707f7 Scott Ullrich
1577 5b237745 Scott Ullrich
	$bpcfg = $config['bigpond'];
1578 cfc707f7 Scott Ullrich
1579 5b237745 Scott Ullrich
	if (!$curwanip) {
1580
		/* IP address not configured yet, exit */
1581
		return 0;
1582
	}
1583 cfc707f7 Scott Ullrich
1584 5b237745 Scott Ullrich
	/* kill bpalogin */
1585
	killbyname("bpalogin");
1586 cfc707f7 Scott Ullrich
1587 5b237745 Scott Ullrich
	/* wait a moment */
1588
	sleep(1);
1589 cfc707f7 Scott Ullrich
1590 5b237745 Scott Ullrich
	/* get the default domain */
1591
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1592
	if ($nfd) {
1593
		$defaultdomain = trim(fgets($nfd));
1594
		fclose($nfd);
1595
	}
1596 cfc707f7 Scott Ullrich
1597 5b237745 Scott Ullrich
	/* generate bpalogin.conf */
1598
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1599
	if (!$fd) {
1600
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1601
		return 1;
1602
	}
1603 cfc707f7 Scott Ullrich
1604 5b237745 Scott Ullrich
	if (!$bpcfg['authserver'])
1605
		$bpcfg['authserver'] = "dce-server";
1606
	if (!$bpcfg['authdomain'])
1607
		$bpcfg['authdomain'] = $defaultdomain;
1608 cfc707f7 Scott Ullrich
1609 5b237745 Scott Ullrich
	$bpconf = <<<EOD
1610
username {$bpcfg['username']}
1611
password {$bpcfg['password']}
1612
authserver {$bpcfg['authserver']}
1613
authdomain {$bpcfg['authdomain']}
1614
localport 5050
1615
1616
EOD;
1617
1618
	if ($bpcfg['minheartbeatinterval'])
1619
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1620
1621
	fwrite($fd, $bpconf);
1622
	fclose($fd);
1623 cfc707f7 Scott Ullrich
1624 5b237745 Scott Ullrich
	/* fire up bpalogin */
1625
	mwexec("/usr/local/sbin/bpalogin -c {$g['varetc_path']}/bpalogin.conf");
1626 cfc707f7 Scott Ullrich
1627 5b237745 Scott Ullrich
	return 0;
1628
}
1629
1630
function get_real_wan_interface() {
1631
	global $config, $g;
1632 cfc707f7 Scott Ullrich
1633 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1634 cfc707f7 Scott Ullrich
1635 5b237745 Scott Ullrich
	$wanif = $wancfg['if'];
1636 6dd9b015 Ermal Luçi
	if ($wancfg['ipaddr'] == "pppoe") 
1637
		$wanif = "pppoe0";
1638
	if ($wancfg['ipaddr'] == "pptp") 
1639
		$wanif = "pptp0";
1640 cfc707f7 Scott Ullrich
1641 5b237745 Scott Ullrich
	return $wanif;
1642
}
1643
1644 1675c73f Scott Ullrich
function get_current_wan_address($interface = "wan") {
1645 5b237745 Scott Ullrich
	global $config, $g;
1646 cfc707f7 Scott Ullrich
1647 1675c73f Scott Ullrich
	$wancfg = $config['interfaces'][$interface];
1648 cfc707f7 Scott Ullrich
1649 a8a7325e Scott Ullrich
	$interface = filter_translate_type_to_real_interface($interface);
1650 767a716e Scott Ullrich
	$ifinfo = "";
1651 bc76c771 Scott Ullrich
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1652
		/* get interface info with netstat */
1653
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1654
1655
		if (isset($ifinfo[1])) {
1656
			$aif = preg_split("/\s+/", $ifinfo[1]);
1657
			$curwanip = chop($aif[3]);
1658
1659
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1660
				return $curwanip;
1661
		}
1662
1663 eb772abd Scott Ullrich
		return null;
1664 bc76c771 Scott Ullrich
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1665 5b237745 Scott Ullrich
		/* dynamic WAN IP address, find out which one */
1666
		$wanif = get_real_wan_interface();
1667 cfc707f7 Scott Ullrich
1668 5b237745 Scott Ullrich
		/* get interface info with netstat */
1669
		exec("/usr/bin/netstat -nWI " . escapeshellarg($wanif) . " -f inet", $ifinfo);
1670 cfc707f7 Scott Ullrich
1671 5b237745 Scott Ullrich
		if (isset($ifinfo[1])) {
1672
			$aif = preg_split("/\s+/", $ifinfo[1]);
1673
			$curwanip = chop($aif[3]);
1674 cfc707f7 Scott Ullrich
1675 5b237745 Scott Ullrich
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1676
				return $curwanip;
1677
		}
1678 cfc707f7 Scott Ullrich
1679 5b237745 Scott Ullrich
		return null;
1680
	} else {
1681
		/* static WAN IP address */
1682
		return $wancfg['ipaddr'];
1683
	}
1684
}
1685
1686 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
1687
 * NAME
1688
 *   is_altq_capable - Test if interface is capable of using ALTQ
1689
 * INPUTS
1690
 *   $int            - string containing interface name
1691
 * RESULT
1692
 *   boolean         - true or false
1693
 ******/
1694
1695
function is_altq_capable($int) {
1696
        /* Per:
1697
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1698
         * Only the following drivers have ALTQ support
1699
         */
1700
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1701 4b31e652 Scott Ullrich
		"em", "fxp", "hme", "lnc", "le", "nve", "re", "rl", "ndis", "sf", "sis", "sk",
1702 a262450f Scott Ullrich
		"tun", "vr", "wi", "xl", "vlan", "ste");
1703 a57b119e Bill Marquette
1704
        $int_family = preg_split("/[0-9]+/", $int);
1705
1706
        if (in_array($int_family[0], $capable))
1707
                return true;
1708
        else
1709
                return false;
1710
}
1711
1712 015bb1cc Scott Ullrich
function get_number_of_bridged_interfaces() {
1713 ce33da32 Scott Ullrich
	$bridges_total = 0;
1714
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1715
	foreach($bridges as $bridge) {
1716 767a716e Scott Ullrich
		$match_array = "";
1717 ce33da32 Scott Ullrich
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1718 7370613f Scott Ullrich
		if($match_array[1][0] <> "") {
1719
			if($match_array[1][0] > $bridges_total)
1720
				$bridges_total = $match_array[1][0];
1721
		}
1722 ce33da32 Scott Ullrich
	}
1723
	return "{$bridges_total}";
1724 015bb1cc Scott Ullrich
}
1725
1726 161040eb Scott Ullrich
function get_number_of_vlan_interfaces() {
1727
        $vlans_total = 0;
1728
        $vlans = split("\n", `/sbin/ifconfig -a | /usr/bin/grep vlan | grep flags`);
1729
        foreach($vlans as $bridge) {
1730
                $match_array = "";
1731
                preg_match_all("/vlan(.*):/",$bridge,$match_array);
1732
                if($match_array[1][0] <> "") {
1733
                        if($match_array[1][0] > $vlans_total)
1734
                                $vlans_total = $match_array[1][0];
1735
                }
1736
        }
1737
        return "{$vlans_total}";
1738
}
1739
1740 860c4e80 Chris Buechler
function get_number_of_ppp_interfaces() {
1741
        $ppps_total = 0;
1742
        $ppps = split("\n", `/sbin/ifconfig -a | /usr/bin/grep ppp | grep flags`);
1743
        foreach($ppps as $bridge) {
1744
                $match_array = "";
1745
                preg_match_all("/ppp(.*):/",$bridge,$match_array);
1746
                if($match_array[1][0] <> "") {
1747
                        if($match_array[1][0] > $ppps_total)
1748
                                $ppps_total = $match_array[1][0];
1749
                }
1750
        }
1751
        return "{$ppps_total}";
1752
}
1753
1754 015bb1cc Scott Ullrich
function get_next_available_bridge_interface() {
1755 38a3e74e Scott Ullrich
	$bridges_total = get_number_of_bridged_interfaces();
1756 0d429e43 Scott Ullrich
	$interfaces = `/sbin/ifconfig -l`;
1757 26eb3795 Scott Ullrich
	$x=0;
1758 f78d2412 Scott Ullrich
	for($x=0; $x<$bridges_total; $x++) {
1759 0d429e43 Scott Ullrich
		if(!stristr($interfaces, "bridge{$x}")) {
1760
			return "{$x}";
1761 dba4049f Scott Ullrich
		}
1762 015bb1cc Scott Ullrich
	}
1763 0d429e43 Scott Ullrich
	return "{$x}";
1764 015bb1cc Scott Ullrich
}
1765
1766 ba0e11c7 Scott Ullrich
function destroy_bridge($bridge_num) {
1767 e2f56f0d Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1768 f9118d49 Scott Ullrich
	sleep(1);
1769 ba0e11c7 Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1770 0d429e43 Scott Ullrich
	sleep(1);
1771 ba0e11c7 Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1772 f9118d49 Scott Ullrich
	sleep(1);
1773 ba0e11c7 Scott Ullrich
	return;
1774
}
1775
1776
function discover_bridge($interface1, $interface2) {
1777 7370613f Scott Ullrich
	if(!$interface1) return;
1778
	if(!$interface2) return;
1779 ba0e11c7 Scott Ullrich
	$total_bridges = get_number_of_bridged_interfaces();
1780 51e4bb37 Scott Ullrich
	$total_bridges++;
1781 4f9a78d9 Scott Ullrich
	$interfaces = `/sbin/ifconfig -l`;
1782 26eb3795 Scott Ullrich
	$x=0;
1783 ba0e11c7 Scott Ullrich
	for($x=0; $x<$total_bridges; $x++) {
1784 4f9a78d9 Scott Ullrich
		$bridge_text = "NA";
1785 eb772abd Scott Ullrich
		if(!stristr($interfaces, "bridge{$x}"))
1786 7370613f Scott Ullrich
			continue;
1787
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1788 51e4bb37 Scott Ullrich
		if(stristr($bridge_text, $interface1))
1789
			if(stristr($bridge_text, $interface2))
1790
				return $x;
1791 ba0e11c7 Scott Ullrich
	}
1792 0d429e43 Scott Ullrich
	return "-1";
1793 ba0e11c7 Scott Ullrich
}
1794 a57b119e Bill Marquette
1795 d8c67d69 Scott Ullrich
function get_wireless_modes($interface)
1796
{
1797
	/* return wireless modes and channels */
1798
	if(is_interface_wireless($interface)) {
1799
		$wi = 1;
1800
		$ifconfig = "/sbin/ifconfig";
1801
		$awk = "/usr/bin/awk";
1802
		$chan_list = "$ifconfig $interface list chan";
1803 4066776d Scott Ullrich
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1804 d8c67d69 Scott Ullrich
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1805
1806 4b0e71db Scott Ullrich
		$interface_channels = "";
1807 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1808
		$interface_channel_count = count($interface_channels);
1809
1810
		$c = 0;
1811
		while ($c < $interface_channel_count)
1812
		{
1813
			$channel_line = explode(",", $interface_channels["$c"]);
1814
			$wireless_mode = trim($channel_line[0]);
1815
			$wireless_channel = trim($channel_line[1]);
1816 4066776d Scott Ullrich
			if(trim($wireless_mode) != "") {
1817
				/* if we only have 11g also set 11b channels */
1818
				if($wireless_mode == "11g") {
1819
					$wireless_modes["11b"] = array();
1820
				}
1821
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1822
			}
1823 d8c67d69 Scott Ullrich
			$c++;
1824
		}
1825
	}
1826 4066776d Scott Ullrich
	return($wireless_modes);
1827 d8c67d69 Scott Ullrich
}
1828
1829 b7ec2b9e Scott Ullrich
function get_interface_mac($interface) {
1830
1831
        /* build interface list with netstat */
1832 84e5047d Scott Ullrich
        $linkinfo = "";
1833 b7ec2b9e Scott Ullrich
        exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1834
        array_shift($linkinfo);
1835
        $alink = preg_split("/\s+/", $linkinfo[0]);
1836
        $mac = chop($alink[3]);
1837
        return $mac;
1838
}
1839
1840 6dd9b015 Ermal Luçi
?>