Project

General

Profile

Download (16.5 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-2018 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
/*
32
 * returns:
33
 * -2: error
34
 * -1: no interface found
35
 *  0: interface(s) assigned
36
 *  1: user quit
37
 */
38
function set_networking_interfaces_ports() {
39
	global $noreboot;
40
	global $config;
41
	global $g;
42
	global $fp;
43

    
44
	$fp = fopen('php://stdin', 'r');
45

    
46
	$memory = get_memory();
47
	$physmem = $memory[0];
48
	$realmem = $memory[1];
49

    
50
	if ($physmem < $g['minimum_ram_warning']) {
51
		echo "\n\n\n";
52
		echo gettext("DANGER!  WARNING!  ACHTUNG!") . "\n\n";
53
		printf(gettext('%1$s requires *AT LEAST* %2$s RAM to function correctly.%3$s'), $g['product_name'], $g['minimum_ram_warning_text'], "\n");
54
		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");
55
		echo "\n" . gettext("Press ENTER to continue.") . " ";
56
		fgets($fp);
57
		echo "\n";
58
	}
59

    
60
	$iflist = get_interface_list();
61

    
62
	/* Function flow is based on $key or the lack thereof */
63
	$key = null;
64

    
65
	echo <<<EOD
66

    
67
Valid interfaces are:
68

    
69

    
70
EOD;
71

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

    
91
	echo "\n" . gettext("Do VLANs need to be set up first?");
92
	echo "\n" .
93
		gettext(
94
			"If VLANs will not be used, or only for optional interfaces, it is typical to\n" .
95
			"say no here and use the webConfigurator to configure VLANs later, if required.") .
96
		"\n";
97
	echo "\n" . gettext("Should VLANs be set up now [y|n]?") . " ";
98

    
99
	$key = chop(fgets($fp));
100

    
101
	//Manually assign interfaces
102
	if (in_array($key, array('y', 'Y'))) {
103
		vlan_setup();
104
	}
105

    
106
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
107

    
108
		echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
109
		foreach ($config['vlans']['vlan'] as $vlan) {
110

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

    
114
			$iflist[vlan_interface($vlan)] = array();
115
			$ifsmallist = $ifsmallist . vlan_interface($vlan) . " ";
116
		}
117
	}
118

    
119
	echo <<<EOD
120

    
121
If the names of the interfaces are not known, auto-detection can
122
be used instead. To use auto-detection, please disconnect all
123
interfaces before pressing 'a' to begin the process.
124

    
125
EOD;
126

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

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

    
149
		$lanif = chop(fgets($fp));
150

    
151
		if ($lanif == "exit") {
152
			exit;
153
		}
154

    
155
		if ($lanif == "") {
156
			/* It is OK to have just a WAN, without a LAN so break if the user does not want LAN. */
157
			break;
158
		}
159

    
160
		if ($lanif === "a") {
161
			$lanif = autodetect_interface("LAN", $fp);
162
		} else if (!array_key_exists($lanif, $iflist)) {
163
			printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $lanif, "\n");
164
			unset($lanif);
165
			continue;
166
		}
167
		$ifsmallist = str_replace(" " . $lanif . " ", " ", $ifsmallist);
168
	} while (!$lanif);
169

    
170
	/* optional interfaces */
171
	$i = 0;
172
	$optif = array();
173

    
174
	if ($lanif <> "") {
175
		while (strlen(trim($ifsmallist)) > 0) {
176
			if (!empty($optif[$i])) {
177
				$i++;
178
			}
179
			$io = $i + 1;
180

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

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

    
188
			$optif[$i] = chop(fgets($fp));
189

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

    
211
	/* check for double assignments */
212
	$ifarr = array_merge(array($lanif, $wanif), $optif);
213

    
214
	for ($i = 0; $i < (count($ifarr)-1); $i++) {
215
		for ($j = ($i+1); $j < count($ifarr); $j++) {
216
			if ($ifarr[$i] == $ifarr[$j]) {
217
				echo <<<EOD
218

    
219
Error: The same interface name cannot be assigned twice!
220

    
221
EOD;
222
				fclose($fp);
223
				return (-2);
224
			}
225
		}
226
	}
227

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

    
230
	echo "WAN  -> " . $wanif . "\n";
231
	if ($lanif != "") {
232
		echo "LAN  -> " . $lanif . "\n";
233
	}
234
	for ($i = 0; $i < count($optif); $i++) {
235
		echo "OPT" . ($i+1) . " -> " . $optif[$i] . "\n";
236
	}
237

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

    
241
	if (in_array($key, array('y', 'Y'))) {
242
		if ($lanif) {
243
			if (is_array($config['interfaces']['lan'])) {
244
				$upints = pfSense_interface_listget(IFF_UP);
245
				if (in_array($config['interfaces']['lan']['if'], $upints))
246
					interface_bring_down('lan', true);
247
			}
248
			if (!is_array($config['interfaces']['lan'])) {
249
				$config['interfaces']['lan'] = array();
250
			}
251
			$config['interfaces']['lan']['if'] = $lanif;
252
			$config['interfaces']['lan']['enable'] = true;
253
		} elseif (!platform_booting()) {
254

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

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

    
324
		if (is_array($config['interfaces']['wan'])) {
325
			$upints = pfSense_interface_listget(IFF_UP);
326
			if (in_array($config['interfaces']['wan']['if'], $upints))
327
				interface_bring_down('wan', true);
328
		}
329
		if (!is_array($config['interfaces']['wan'])) {
330
			$config['interfaces']['wan'] = array();
331
		}
332
		$config['interfaces']['wan']['if'] = $wanif;
333
		$config['interfaces']['wan']['enable'] = true;
334
		if (preg_match($g['wireless_regex'], $wanif)) {
335
			if (is_array($config['interfaces']['wan']) &&
336
			    !is_array($config['interfaces']['wan']['wireless'])) {
337
				$config['interfaces']['wan']['wireless'] = array();
338
			}
339
		} else {
340
			if (isset($config['interfaces']['wan'])) {
341
				unset($config['interfaces']['wan']['wireless']);
342
			}
343
		}
344

    
345
		for ($i = 0; $i < count($optif); $i++) {
346
			if (is_array($config['interfaces']['opt' . ($i+1)])) {
347
				$upints = pfSense_interface_listget(IFF_UP);
348
				if (in_array($config['interfaces']['opt' . ($i+1)]['if'], $upints))
349
					interface_bring_down('opt' . ($i+1), true);
350
			}
351
			if (!is_array($config['interfaces']['opt' . ($i+1)])) {
352
				$config['interfaces']['opt' . ($i+1)] = array();
353
			}
354

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

    
357
			/* wireless interface? */
358
			if (preg_match($g['wireless_regex'], $optif[$i])) {
359
				if (!is_array($config['interfaces']['opt' . ($i+1)]['wireless'])) {
360
					$config['interfaces']['opt' . ($i+1)]['wireless'] = array();
361
				}
362
			} else {
363
				unset($config['interfaces']['opt' . ($i+1)]['wireless']);
364
			}
365

    
366
			if (empty($config['interfaces']['opt' . ($i+1)]['descr'])) {
367
				$config['interfaces']['opt' . ($i+1)]['descr'] = "OPT" . ($i+1);
368
				unset($config['interfaces']['opt' . ($i+1)]['enable']);
369
			}
370
		}
371

    
372
		/* remove all other (old) optional interfaces */
373
		for (; isset($config['interfaces']['opt' . ($i+1)]); $i++) {
374
			unset($config['interfaces']['opt' . ($i+1)]);
375
		}
376

    
377
		printf(gettext("%sWriting configuration..."), "\n");
378
		write_config(gettext("Console assignment of interfaces"));
379
		printf(gettext("done.%s"), "\n");
380

    
381
		fclose($fp);
382

    
383
		echo gettext("One moment while the settings are reloading...");
384
		touch("{$g['tmp_path']}/assign_complete");
385

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

    
391
		echo gettext(" done!") . "\n";
392

    
393
		return (0);
394
	}
395
}
396

    
397
function autodetect_interface($ifname, $fp) {
398
	$iflist_prev = get_interface_list("media");
399
	echo <<<EOD
400

    
401
Connect the {$ifname} interface now and make sure that the link is up.
402
Then press ENTER to continue.
403

    
404
EOD;
405
	fgets($fp);
406
	$iflist = get_interface_list("media");
407

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

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

    
417
	return null;
418
}
419

    
420
function interfaces_setup() {
421
	global $iflist, $config, $g, $fp;
422

    
423
	$iflist = get_interface_list();
424
}
425

    
426
function vlan_setup() {
427
	global $iflist, $config, $g, $fp;
428

    
429
	$iflist = get_interface_list();
430

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

    
435
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
436
			return;
437
		}
438
	}
439

    
440
	$config['vlans']['vlan'] = array();
441
	echo "\n";
442

    
443
	$vlanif = 0;
444

    
445
	while (1) {
446
		$vlan = array();
447

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

    
462
		if ($vlan_capable == 0) {
463
			echo gettext("No VLAN capable interfaces detected.") . "\n";
464
			return;
465
		}
466

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

    
470
		if ($vlan['if']) {
471
			if (!array_key_exists($vlan['if'], $iflist) ||
472
			    !is_jumbo_capable($vlan['if'])) {
473
				printf(gettext('%1$sInvalid interface name \'%2$s\'%3$s'), "\n", $vlan['if'], "\n");
474
				continue;
475
			}
476
		} else {
477
			break;
478
		}
479

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

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

    
501
function check_for_alternate_interfaces() {
502
	global $config;
503

    
504
	// If the WAN and/or LAN devices in the factory default config do not exist,
505
	// then look for alternate devices.
506
	// This lets many systems boot a factory default config without being
507
	// forced to do interface assignment on the console.
508

    
509
	$specplatform = system_identify_specific_platform();
510
	$default_device = array();
511

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

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

    
548
	foreach ($other_devices_arr as $ifname => $other_devices) {
549
		if (!does_interface_exist($config['interfaces'][$ifname]['if'])) {
550
			if (does_interface_exist($default_device[$ifname])) {
551
				$config['interfaces'][$ifname]['if'] = $default_device[$ifname];
552
				$interface_assignment_changed = true;
553
			} else {
554
				foreach ($other_devices as $other_device) {
555
					if (does_interface_exist($other_device)) {
556
						$config['interfaces'][$ifname]['if'] = $other_device;
557
						$interface_assignment_changed = true;
558
						break;
559
					}
560
				}
561
			}
562
		}
563
	}
564

    
565
	if ($interface_assignment_changed) {
566
		write_config("Factory default boot detected WAN " . $config['interfaces']['wan']['if'] . " and LAN " . $config['interfaces']['lan']['if']);
567
	}
568
}
569

    
570
?>
(7-7/60)