Project

General

Profile

Download (16.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * config.console.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * originally part of m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * All rights reserved.
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
require_once("config.inc");
27
require_once("globals.inc");
28
require_once("interfaces.inc");
29
require_once("util.inc");
30

    
31
function set_networking_interfaces_ports() {
32
	global $noreboot;
33
	global $config;
34
	global $g;
35
	global $fp;
36

    
37
	$fp = fopen('php://stdin', 'r');
38

    
39
	$memory = get_memory();
40
	$physmem = $memory[0];
41
	$realmem = $memory[1];
42

    
43
	if ($physmem < $g['minimum_ram_warning']) {
44
		echo "\n\n\n";
45
		echo gettext("DANGER!  WARNING!  ACHTUNG!") . "\n\n";
46
		printf(gettext('%1$s requires *AT LEAST* %2$s RAM to function correctly.%3$s'), $g['product_name'], $g['minimum_ram_warning_text'], "\n");
47
		printf(gettext('Only (%1$s) MB RAM has been detected, with (%2$s) available to %3$s.%4$s'), $realmem, $physmem, $g['product_name'], "\n");
48
		echo "\n" . gettext("Press ENTER to continue.") . " ";
49
		fgets($fp);
50
		echo "\n";
51
	}
52

    
53
	$iflist = get_interface_list();
54

    
55
	/* Function flow is based on $key or the lack thereof */
56
	$key = null;
57

    
58
	echo <<<EOD
59

    
60
Valid interfaces are:
61

    
62

    
63
EOD;
64

    
65
	if (!is_array($iflist)) {
66
		echo gettext("No interfaces found!") . "\n";
67
		$iflist = array();
68
	} else {
69
		// ifsmallist is kept with spaces at the beginning and end to assist with str_replace() operations
70
		$ifsmallist = " ";
71
		foreach ($iflist as $iface => $ifa) {
72
			$friendly = convert_real_interface_to_friendly_interface_name($iface);	
73
			$ifstatus = pfSense_get_interface_addresses($config['interfaces'][$friendly]['if']);
74
			if (is_array($ifstatus) && $ifstatus['linkstateup'])
75
				$status = "  (up)";
76
			else
77
				$status = "(down)";
78
			$ifsmallist = $ifsmallist . $iface. " ";
79
			echo sprintf("%-7s %s %s %s\n", $iface, $ifa['mac'],
80
				$status, substr($ifa['dmesg'], 0, 48));
81
		}
82
	}
83

    
84
	echo "\n" . gettext("Do VLANs need to be set up first?");
85
	echo "\n" .
86
		gettext(
87
			"If VLANs will not be used, or only for optional interfaces, it is typical to\n" .
88
			"say no here and use the webConfigurator to configure VLANs later, if required.") .
89
		"\n";
90
	echo "\n" . gettext("Should VLANs be set up now [y|n]?") . " ";
91

    
92
	$key = chop(fgets($fp));
93

    
94
	//Manually assign interfaces
95
	if (in_array($key, array('y', 'Y'))) {
96
		vlan_setup();
97
	}
98

    
99
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
100

    
101
		echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
102
		foreach ($config['vlans']['vlan'] as $vlan) {
103

    
104
			echo sprintf("% -16s%s\n", vlan_interface($vlan),
105
				"VLAN tag {$vlan['tag']}, parent interface {$vlan['if']}");
106

    
107
			$iflist[vlan_interface($vlan)] = array();
108
			$ifsmallist = $ifsmallist . vlan_interface($vlan) . " ";
109
		}
110
	}
111

    
112
	echo <<<EOD
113

    
114
If the names of the interfaces are not known, auto-detection can
115
be used instead. To use auto-detection, please disconnect all
116
interfaces before pressing 'a' to begin the process.
117

    
118
EOD;
119

    
120
	do {
121
		echo "\n" . gettext("Enter the WAN interface name or 'a' for auto-detection") . " ";
122
		printf(gettext('%1$s(%2$s or a): '), "\n", trim($ifsmallist));
123
		$wanif = chop(fgets($fp));
124
		if ($wanif === "") {
125
			return;
126
		}
127
		if ($wanif === "a") {
128
			$wanif = autodetect_interface("WAN", $fp);
129
		} else if (!array_key_exists($wanif, $iflist)) {
130
			printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $wanif, "\n");
131
			unset($wanif);
132
			continue;
133
		}
134
		$ifsmallist = str_replace(" " . $wanif . " ", " ", $ifsmallist);
135
	} while (!$wanif);
136

    
137
	do {
138
		printf(gettext('%1$sEnter the LAN interface name or \'a\' for auto-detection %2$s' .
139
			'NOTE: this enables full Firewalling/NAT mode.%3$s' .
140
			'(%4$s a or nothing if finished):%5$s'), "\n", "\n", "\n", trim($ifsmallist), " ");
141

    
142
		$lanif = chop(fgets($fp));
143

    
144
		if ($lanif == "exit") {
145
			exit;
146
		}
147

    
148
		if ($lanif == "") {
149
			/* It is OK to have just a WAN, without a LAN so break if the user does not want LAN. */
150
			break;
151
		}
152

    
153
		if ($lanif === "a") {
154
			$lanif = autodetect_interface("LAN", $fp);
155
		} else if (!array_key_exists($lanif, $iflist)) {
156
			printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $lanif, "\n");
157
			unset($lanif);
158
			continue;
159
		}
160
		$ifsmallist = str_replace(" " . $lanif . " ", " ", $ifsmallist);
161
	} while (!$lanif);
162

    
163
	/* optional interfaces */
164
	$i = 0;
165
	$optif = array();
166

    
167
	if ($lanif <> "") {
168
		while (strlen(trim($ifsmallist)) > 0) {
169
			if (!empty($optif[$i])) {
170
				$i++;
171
			}
172
			$io = $i + 1;
173

    
174
			if ($config['interfaces']['opt' . $io]['descr']) {
175
				printf(gettext('%1$sOptional interface %2$s description found: %3$s'), "\n", $io, $config['interfaces']['opt' . $io]['descr']);
176
			}
177

    
178
			printf(gettext('%1$sEnter the Optional %2$s interface name or \'a\' for auto-detection%3$s' .
179
				'(%4$s a or nothing if finished):%5$s'), "\n", $io, "\n", trim($ifsmallist), " ");
180

    
181
			$optif[$i] = chop(fgets($fp));
182

    
183
			if ($optif[$i]) {
184
				if ($optif[$i] === "a") {
185
					$ad = autodetect_interface(gettext("Optional") . " " . $io, $fp);
186
					if ($ad) {
187
						$optif[$i] = $ad;
188
					} else {
189
						unset($optif[$i]);
190
					}
191
				} else if (!array_key_exists($optif[$i], $iflist)) {
192
					printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $optif[$i], "\n");
193
					unset($optif[$i]);
194
					continue;
195
				}
196
				$ifsmallist = str_replace(" " . $optif[$i] . " ", " ", $ifsmallist);
197
			} else {
198
				unset($optif[$i]);
199
				break;
200
			}
201
		}
202
	}
203

    
204
	/* check for double assignments */
205
	$ifarr = array_merge(array($lanif, $wanif), $optif);
206

    
207
	for ($i = 0; $i < (count($ifarr)-1); $i++) {
208
		for ($j = ($i+1); $j < count($ifarr); $j++) {
209
			if ($ifarr[$i] == $ifarr[$j]) {
210
				echo <<<EOD
211

    
212
Error: The same interface name cannot be assigned twice!
213

    
214
EOD;
215
				fclose($fp);
216
				return;
217
			}
218
		}
219
	}
220

    
221
	echo "\n" . gettext("The interfaces will be assigned as follows:") . "\n\n";
222

    
223
	echo "WAN  -> " . $wanif . "\n";
224
	if ($lanif != "") {
225
		echo "LAN  -> " . $lanif . "\n";
226
	}
227
	for ($i = 0; $i < count($optif); $i++) {
228
		echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
229
	}
230

    
231
	echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
232
	$key = chop(fgets($fp));
233

    
234
	if (in_array($key, array('y', 'Y'))) {
235
		if ($lanif) {
236
			if (is_array($config['interfaces']['lan'])) {
237
				$upints = pfSense_interface_listget(IFF_UP);
238
				if (in_array($config['interfaces']['lan']['if'], $upints))
239
					interface_bring_down('lan', true);
240
			}
241
			if (!is_array($config['interfaces']['lan'])) {
242
				$config['interfaces']['lan'] = array();
243
			}
244
			$config['interfaces']['lan']['if'] = $lanif;
245
			$config['interfaces']['lan']['enable'] = true;
246
		} elseif (!platform_booting()) {
247

    
248
			echo "\n" . gettext("You have chosen to remove the LAN interface.") . "\n";
249
			echo "\n" . gettext("Would you like to remove the LAN IP address and \nunload the interface now [y|n]?") . " ";
250

    
251
			if (strcasecmp(chop(fgets($fp)), "y") == 0) {
252
				if (isset($config['interfaces']['lan']) && $config['interfaces']['lan']['if']) {
253
					mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
254
				}
255
			}
256
			if (isset($config['interfaces']['lan'])) {
257
				unset($config['interfaces']['lan']);
258
			}
259
			if (isset($config['dhcpd']['lan'])) {
260
				unset($config['dhcpd']['lan']);
261
			}
262
			if (isset($config['dhcpdv6']['lan'])) {
263
				unset($config['dhcpdv6']['lan']);
264
			}
265
			if (isset($config['interfaces']['lan']['if'])) {
266
				unset($config['interfaces']['lan']['if']);
267
			}
268
			if (isset($config['interfaces']['wan']['blockpriv'])) {
269
				unset($config['interfaces']['wan']['blockpriv']);
270
			}
271
			if (isset($config['shaper'])) {
272
				unset($config['shaper']);
273
			}
274
			if (isset($config['ezshaper'])) {
275
				unset($config['ezshaper']);
276
			}
277
			if (isset($config['nat'])) {
278
				unset($config['nat']);
279
			}
280
		} else {
281
			if (isset($config['interfaces']['lan']['if'])) {
282
				mwexec("/sbin/ifconfig " . $config['interfaces']['lan']['if'] . " delete");
283
			}
284
			if (isset($config['interfaces']['lan'])) {
285
				unset($config['interfaces']['lan']);
286
			}
287
			if (isset($config['dhcpd']['lan'])) {
288
				unset($config['dhcpd']['lan']);
289
			}
290
			if (isset($config['interfaces']['lan']['if'])) {
291
				unset($config['interfaces']['lan']['if']);
292
			}
293
			if (isset($config['interfaces']['wan']['blockpriv'])) {
294
				unset($config['interfaces']['wan']['blockpriv']);
295
			}
296
			if (isset($config['shaper'])) {
297
				unset($config['shaper']);
298
			}
299
			if (isset($config['ezshaper'])) {
300
				unset($config['ezshaper']);
301
			}
302
			if (isset($config['nat'])) {
303
				unset($config['nat']);
304
			}
305
		}
306
		if (preg_match($g['wireless_regex'], $lanif)) {
307
			if (is_array($config['interfaces']['lan']) &&
308
			    !is_array($config['interfaces']['lan']['wireless'])) {
309
				$config['interfaces']['lan']['wireless'] = array();
310
			}
311
		} else {
312
			if (isset($config['interfaces']['lan'])) {
313
				unset($config['interfaces']['lan']['wireless']);
314
			}
315
		}
316

    
317
		if (is_array($config['interfaces']['wan'])) {
318
			$upints = pfSense_interface_listget(IFF_UP);
319
			if (in_array($config['interfaces']['wan']['if'], $upints))
320
				interface_bring_down('wan', true);
321
		}
322
		if (!is_array($config['interfaces']['wan'])) {
323
			$config['interfaces']['wan'] = array();
324
		}
325
		$config['interfaces']['wan']['if'] = $wanif;
326
		$config['interfaces']['wan']['enable'] = true;
327
		if (preg_match($g['wireless_regex'], $wanif)) {
328
			if (is_array($config['interfaces']['wan']) &&
329
			    !is_array($config['interfaces']['wan']['wireless'])) {
330
				$config['interfaces']['wan']['wireless'] = array();
331
			}
332
		} else {
333
			if (isset($config['interfaces']['wan'])) {
334
				unset($config['interfaces']['wan']['wireless']);
335
			}
336
		}
337

    
338
		for ($i = 0; $i < count($optif); $i++) {
339
			if (is_array($config['interfaces']['opt' . ($i+1)])) {
340
				$upints = pfSense_interface_listget(IFF_UP);
341
				if (in_array($config['interfaces']['opt' . ($i+1)]['if'], $upints))
342
					interface_bring_down('opt' . ($i+1), true);
343
			}
344
			if (!is_array($config['interfaces']['opt' . ($i+1)])) {
345
				$config['interfaces']['opt' . ($i+1)] = array();
346
			}
347

    
348
			$config['interfaces']['opt' . ($i+1)]['if'] = $optif[$i];
349

    
350
			/* wireless interface? */
351
			if (preg_match($g['wireless_regex'], $optif[$i])) {
352
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless'])) {
353
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
354
				}
355
			} else {
356
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
357
			}
358

    
359
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
360
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
361
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
362
			}
363
		}
364

    
365
		/* remove all other (old) optional interfaces */
366
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++) {
367
			unset($config['interfaces']['opt' . ($i+1)]);
368
		}
369

    
370
		printf(gettext("%sWriting configuration..."), "\n");
371
		write_config(gettext("Console assignment of interfaces"));
372
		printf(gettext("done.%s"), "\n");
373

    
374
		fclose($fp);
375

    
376
		if (platform_booting()) {
377
			return;
378
		}
379

    
380
		echo gettext("One moment while the settings are reloading...");
381
		echo gettext(" done!") . "\n";
382

    
383
		touch("{$g['tmp_path']}/assign_complete");
384

    
385
		if (file_exists("/conf/trigger_initial_wizard")) {
386
			// Let the system know that the interface assign part of initial setup has been done.
387
			touch("{$g['conf_path']}/assign_complete");
388
		}
389
	}
390
}
391

    
392
function autodetect_interface($ifname, $fp) {
393
	$iflist_prev = get_interface_list("media");
394
	echo <<<EOD
395

    
396
Connect the {$ifname} interface now and make sure that the link is up.
397
Then press ENTER to continue.
398

    
399
EOD;
400
	fgets($fp);
401
	$iflist = get_interface_list("media");
402

    
403
	foreach ($iflist_prev as $ifn => $ifa) {
404
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
405
			printf(gettext('Detected link-up on interface %1$s.%2$s'), $ifn, "\n");
406
			return $ifn;
407
		}
408
	}
409

    
410
	printf(gettext("No link-up detected.%s"), "\n");
411

    
412
	return null;
413
}
414

    
415
function interfaces_setup() {
416
	global $iflist, $config, $g, $fp;
417

    
418
	$iflist = get_interface_list();
419
}
420

    
421
function vlan_setup() {
422
	global $iflist, $config, $g, $fp;
423

    
424
	$iflist = get_interface_list();
425

    
426
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
427
		echo "\n" . gettext("WARNING: all existing VLANs will be cleared if you proceed!") . "\n";
428
		echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
429

    
430
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
431
			return;
432
		}
433
	}
434

    
435
	$config['vlans']['vlan'] = array();
436
	echo "\n";
437

    
438
	$vlanif = 0;
439

    
440
	while (1) {
441
		$vlan = array();
442

    
443
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
444
		if (!is_array($iflist)) {
445
			echo gettext("No interfaces found!") . "\n";
446
		} else {
447
			$vlan_capable = 0;
448
			foreach ($iflist as $iface => $ifa) {
449
				if (is_jumbo_capable($iface)) {
450
					echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
451
						$ifa['up'] ? "   (up)" : "");
452
					$vlan_capable++;
453
				}
454
			}
455
		}
456

    
457
		if ($vlan_capable == 0) {
458
			echo gettext("No VLAN capable interfaces detected.") . "\n";
459
			return;
460
		}
461

    
462
		echo "\n" . gettext("Enter the parent interface name for the new VLAN (or nothing if finished):") . " ";
463
		$vlan['if'] = chop(fgets($fp));
464

    
465
		if ($vlan['if']) {
466
			if (!array_key_exists($vlan['if'], $iflist) || 
467
			    !is_jumbo_capable($vlan['if'])) {
468
				printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $vlan['if'], "\n");
469
				continue;
470
			}
471
		} else {
472
			break;
473
		}
474

    
475
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
476
		$vlan['tag'] = chop(fgets($fp));
477
		$vlan['vlanif'] = vlan_interface($vlan);
478
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
479
			printf(gettext('%1$sInvalid VLAN tag \'%2$s\'%3$s'), "\n", $vlan['tag'], "\n");
480
			continue;
481
		}
482

    
483
		if (is_array($config['vlans']['vlan'])) {
484
			foreach ($config['vlans']['vlan'] as $existingvlan) {
485
				if ($vlan['if'] == $existingvlan['if'] && $vlan['tag'] == $existingvlan['tag']) {
486
					printf("\n\n" . gettext("This parent interface and VLAN already created."));
487
					continue 2;
488
				}
489
			}
490
		}
491
		$config['vlans']['vlan'][] = $vlan;
492
		$vlanif++;
493
	}
494
}
495

    
496
function check_for_alternate_interfaces() {
497
	global $config;
498

    
499
	// If the WAN and/or LAN devices in the factory default config do not exist,
500
	// then look for alternate devices.
501
	// This lets many systems boot a factory default config without being
502
	// forced to do interface assignment on the console.
503

    
504
	$specplatform = system_identify_specific_platform();
505
	$default_device = array();
506

    
507
	// If we recognise the platform, then specify the devices directly.
508
	switch ($specplatform['name']) {
509
		case 'alix':
510
			$default_device['wan'] = "vr1";
511
			$default_device['lan'] = "vr0";
512
			break;
513
		case 'APU':
514
			$default_device['wan'] = "re1";
515
			$default_device['lan'] = "re2";
516
			break;
517
		case 'RCC-VE':
518
			/* SG-4860 or SG-8860 */
519
			if (does_interface_exist('igb4')) {
520
				$config['interfaces']['wan']['if'] = 'igb1';
521
				$config['interfaces']['lan']['if'] = 'igb0';
522
			} else {
523
				$config['interfaces']['wan']['if'] = 'igb0';
524
				$config['interfaces']['lan']['if'] = 'igb1';
525
			}
526
			break;
527
		case 'Turbot Dual-E':
528
			$config['interfaces']['wan']['if'] = 'igb0';
529
			$config['interfaces']['lan']['if'] = 'igb1';
530
			break;
531
		default:
532
			$default_device['wan'] = "";
533
			$default_device['lan'] = "";
534
			break;
535
	}
536

    
537
	// Other common device names can be put here and will be looked for
538
	// if the system was not one of the known platforms.
539
	$other_devices_arr['wan'] = array("vr1", "re1", "igb0", "em0");
540
	$other_devices_arr['lan'] = array("vr0", "re2", "igb1", "em1");
541
	$interface_assignment_changed = false;
542

    
543
	foreach ($other_devices_arr as $ifname => $other_devices) {
544
		if (!does_interface_exist($config['interfaces'][$ifname]['if'])) {
545
			if (does_interface_exist($default_device[$ifname])) {
546
				$config['interfaces'][$ifname]['if'] = $default_device[$ifname];
547
				$interface_assignment_changed = true;
548
			} else {
549
				foreach ($other_devices as $other_device) {
550
					if (does_interface_exist($other_device)) {
551
						$config['interfaces'][$ifname]['if'] = $other_device;
552
						$interface_assignment_changed = true;
553
						break;
554
					}
555
				}
556
			}
557
		}
558
	}
559

    
560
	if ($interface_assignment_changed) {
561
		write_config("Factory default boot detected WAN " . $config['interfaces']['wan']['if'] . " and LAN " . $config['interfaces']['lan']['if']);
562
	}
563
}
564

    
565
?>
(7-7/54)