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['dhcpdv6']['lan'])) {
298
				unset($config['dhcpdv6']['lan']);
299
			}
300
			if (isset($config['interfaces']['lan']['if'])) {
301
				unset($config['interfaces']['lan']['if']);
302
			}
303
			if (isset($config['interfaces']['wan']['blockpriv'])) {
304
				unset($config['interfaces']['wan']['blockpriv']);
305
			}
306
			if (isset($config['shaper'])) {
307
				unset($config['shaper']);
308
			}
309
			if (isset($config['ezshaper'])) {
310
				unset($config['ezshaper']);
311
			}
312
			if (isset($config['nat'])) {
313
				unset($config['nat']);
314
			}
315
		}
316
		if (preg_match($g['wireless_regex'], $lanif)) {
317
			if (is_array($config['interfaces']['lan']) &&
318
			    !is_array($config['interfaces']['lan']['wireless'])) {
319
				$config['interfaces']['lan']['wireless'] = array();
320
			}
321
		} else {
322
			if (isset($config['interfaces']['lan'])) {
323
				unset($config['interfaces']['lan']['wireless']);
324
			}
325
		}
326

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

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

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

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

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

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

    
380
		printf(gettext("%sWriting configuration..."), "\n");
381
		write_config(gettext("Console assignment of interfaces"));
382
		printf(gettext("done.%s"), "\n");
383

    
384
		fclose($fp);
385

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

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

    
394
		echo gettext(" done!") . "\n";
395

    
396
		return (0);
397
	}
398
}
399

    
400
function autodetect_interface($ifname, $fp) {
401
	$iflist_prev = get_interface_list("media");
402
	echo <<<EOD
403

    
404
Connect the {$ifname} interface now and make sure that the link is up.
405
Then press ENTER to continue.
406

    
407
EOD;
408
	fgets($fp);
409
	$iflist = get_interface_list("media");
410

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

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

    
420
	return null;
421
}
422

    
423
function interfaces_setup() {
424
	global $iflist, $config, $g, $fp;
425

    
426
	$iflist = get_interface_list();
427
}
428

    
429
function vlan_setup() {
430
	global $iflist, $config, $g, $fp;
431

    
432
	$iflist = get_interface_list();
433

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

    
438
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
439
			return;
440
		}
441
	}
442

    
443
	$config['vlans']['vlan'] = array();
444
	echo "\n";
445

    
446
	$vlanif = 0;
447

    
448
	while (1) {
449
		$vlan = array();
450

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

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

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

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

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

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

    
504
function check_for_alternate_interfaces() {
505
	global $config;
506

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

    
512
	$specplatform = system_identify_specific_platform();
513
	$default_device = array();
514

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

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

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

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

    
573
?>
(8-8/60)