Project

General

Profile

Download (28.9 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-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
require_once("config.inc");
29
require_once("globals.inc");
30
require_once("interfaces.inc");
31
require_once("util.inc");
32

    
33
/*
34
 * returns:
35
 * -2: error
36
 * -1: no interface found
37
 *  0: interface(s) assigned
38
 *  1: user quit
39
 */
40
function set_networking_interfaces_ports() {
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_get('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_get('product_label'), g_get('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_get('product_label'), "\n");
55
		echo "\n" . gettext("Press ENTER to continue.") . " ";
56
		fgets($fp);
57
		echo "\n";
58
	}
59

    
60
	$iflist = get_interface_list('active', 'physical', true);
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 = (!empty($friendly)) ? get_interface_addresses(config_get_path("interfaces/{$friendly}/if", "")) : null;
81
			if (isset($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
	$vlans = config_get_path('vlans/vlan', []);
107
	if (is_array($vlans) && count($vlans)) {
108
		echo "\n\n" . gettext("VLAN interfaces:") . "\n\n";
109
		foreach ($vlans as $vlan) {
110
			echo sprintf("% -16s%s\n", vlan_interface($vlan),
111
				"VLAN tag {$vlan['tag']}, parent interface {$vlan['if']}");
112
			$iflist[vlan_interface($vlan)] = array();
113
			$ifsmallist = $ifsmallist . vlan_interface($vlan) . " ";
114
		}
115
	}
116

    
117
	echo <<<EOD
118

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

    
123
EOD;
124

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

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

    
147
		$lanif = chop(fgets($fp));
148

    
149
		if ($lanif == "exit") {
150
			exit;
151
		}
152

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

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

    
168
	/* optional interfaces */
169
	$i = 0;
170
	$optif = array();
171

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

    
179
			$if_descr_config = config_get_path("interfaces/opt{$io}/descr");
180
			if ($if_descr_config) {
181
				printf(gettext('%1$sOptional interface %2$s description found: %3$s'), "\n", $io, $if_descr_config);
182
			}
183

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

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

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

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

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

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

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

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

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

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

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

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

    
259
			$if_lan_config = config_get_path('interfaces/lan');
260
			if (strcasecmp(chop(fgets($fp)), "y") == 0) {
261
				if (isset($if_lan_config) && $if_lan_config['if']) {
262
					mwexec("/sbin/ifconfig {$if_lan_config['if']} delete");
263
				}
264
			}
265
			if (isset($if_lan_config)) {
266
				config_del_path('interfaces/lan');
267
			}
268

    
269
			config_del_path('dhcpd/lan');
270
			config_del_path('dhcpdv6/lan');
271
			config_del_path('interfaces/wan/blockpriv');
272
			config_del_path('shaper');
273
			config_del_path('ezshaper');
274
			config_del_path('nat');
275
		} else {
276
			$if_lan_config = config_get_path('interfaces/lan');
277
			if (isset($if_lan_config['if'])) {
278
				mwexec("/sbin/ifconfig {$if_lan_config['if']} delete");
279
			}
280
			if (isset($if_lan_config)) {
281
				config_del_path('interfaces/lan');
282
			}
283
			config_del_path('dhcpd/lan');
284
			config_del_path('dhcpdv6/lan');
285
			config_del_path('interfaces/wan/blockpriv');
286
			config_del_path('shaper');
287
			config_del_path('ezshaper');
288
			config_del_path('nat');
289
		}
290
		$if_lan_config = config_get_path('interfaces/lan');
291
		if (preg_match(g_get('wireless_regex'), $lanif)) {
292
			if (is_array($if_lan_config) &&
293
			    !is_array($if_lan_config['wireless'])) {
294
				config_set_path('interfaces/lan/wireless', []);
295
			}
296
		} else {
297
			if (isset($if_lan_config)) {
298
				config_del_path('interfaces/lan/wireless');
299
			}
300
		}
301

    
302
		
303
		if (is_array(config_get_path('interfaces/wan'))) {
304
			$upints = pfSense_interface_listget(IFF_UP);
305
			if (in_array(config_get_path('interfaces/wan/if'), $upints))
306
				interface_bring_down('wan', true);
307
		}
308

    
309
		$if_wan_config = config_get_path('interfaces/wan', []);
310
		$if_wan_config['if'] = $wanif;
311
		$if_wan_config['enable'] = true;
312
		if (preg_match(g_get('wireless_regex'), $wanif)) {
313
			if (is_array($if_wan_config) &&
314
			    !is_array($if_wan_config['wireless'])) {
315
				$if_wan_config['wireless'] = array();
316
			}
317
		} else {
318
			if (isset($if_wan_config)) {
319
				unset($if_wan_config['wireless']);
320
			}
321
		}
322
		config_set_path('interfaces/wan', $if_wan_config);
323

    
324
		$if_opt_counter = 0;
325
		for (; $if_opt_counter < count($optif); $if_opt_counter++) {
326
			$if_opt_name = 'opt' . ($if_opt_counter+1);
327
			if (is_array(config_get_path("interfaces/{$if_opt_name}"))) {
328
				$upints = pfSense_interface_listget(IFF_UP);
329
				if (in_array(config_get_path("interfaces/{$if_opt_name}/if"), $upints))
330
					interface_bring_down($if_opt_name, true);
331
			}
332
			$if_opt_config = config_get_path("interfaces/{$if_opt_name}", []);
333
			$if_opt_config['if'] = $optif[$if_opt_counter];
334

    
335
			/* wireless interface? */
336
			if (preg_match(g_get('wireless_regex'), $optif[$i])) {
337
				if (!is_array($if_opt_config['wireless'])) {
338
					$if_opt_config['wireless'] = array();
339
				}
340
			} else {
341
				unset($if_opt_config['wireless']);
342
			}
343

    
344
			if (empty($if_opt_config['descr'])) {
345
				$if_opt_config['descr'] = strtoupper($if_opt_name);
346
			}
347
			config_set_path("interfaces/{$if_opt_name}", $if_opt_config);
348
		}
349

    
350
		/* remove all other (old) optional interfaces */
351
		$if_config = config_get_path('interfaces', []);
352
		for (; isset($if_config['opt' . ($if_opt_counter+1)]); $if_opt_counter++) {
353
			unset($if_config['opt' . ($if_opt_counter+1)]);
354
		}
355
		config_set_path('interfaces', $if_config);
356

    
357
		printf(gettext("%sWriting configuration..."), "\n");
358
		write_config(gettext("Console assignment of interfaces"));
359
		printf(gettext("done.%s"), "\n");
360

    
361
		fclose($fp);
362

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

    
366
		if (file_exists("{$g['conf_path']}/trigger_initial_wizard")) {
367
			// Let the system know that the interface assign part of initial setup has been done.
368
			touch("{$g['conf_path']}/assign_complete");
369
		}
370

    
371
		echo gettext(" done!") . "\n";
372
		return (0);
373
	}
374
	return (1);
375
}
376

    
377
function autodetect_interface($ifname, $fp) {
378
	$iflist_prev = get_interface_list("media");
379
	echo <<<EOD
380

    
381
Connect the {$ifname} interface now and make sure that the link is up.
382
Then press ENTER to continue.
383

    
384
EOD;
385
	fgets($fp);
386
	$iflist = get_interface_list("media");
387

    
388
	foreach ($iflist_prev as $ifn => $ifa) {
389
		if (!$ifa['up'] && $iflist[$ifn]['up']) {
390
			printf(gettext('Detected link-up on interface %1$s.%2$s'), $ifn, "\n");
391
			return $ifn;
392
		}
393
	}
394

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

    
397
	return null;
398
}
399

    
400
function interfaces_setup() {
401
	global $iflist;
402

    
403
	$iflist = get_interface_list();
404
}
405

    
406
function vlan_setup() {
407
	global $iflist, $fp;
408

    
409
	$iflist = get_interface_list();
410

    
411
	$vlancfg = config_get_path('vlans/vlan');
412
	if (is_array($vlancfg) && count($vlancfg)) {
413
		echo "\n" . gettext("WARNING: all existing VLANs will be cleared if you proceed!") . "\n";
414
		echo "\n" . gettext("Do you want to proceed [y|n]?") . " ";
415

    
416
		if (strcasecmp(chop(fgets($fp)), "y") != 0) {
417
			return;
418
		}
419
	}
420

    
421
	echo "\n";
422

    
423
	$vlanif = 0;
424

    
425
	while (1) {
426
		$vlan = array();
427

    
428
		echo "\n\n" . gettext("VLAN Capable interfaces:") . "\n\n";
429
		if (!is_array($iflist)) {
430
			echo gettext("No interfaces found!") . "\n";
431
		} else {
432
			$vlan_capable = 0;
433
			foreach ($iflist as $iface => $ifa) {
434
				echo sprintf("% -8s%s%s\n", $iface, $ifa['mac'],
435
				    $ifa['up'] ? "   (up)" : "");
436
				$vlan_capable++;
437
			}
438
		}
439

    
440
		if ($vlan_capable == 0) {
441
			echo gettext("No VLAN capable interfaces detected.") . "\n";
442
			return;
443
		}
444

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

    
448
		if ($vlan['if']) {
449
			if (!array_key_exists($vlan['if'], $iflist)) {
450
				printf(gettext(
451
				    '%1$sInvalid interface name \'%2$s\'%3$s'),
452
				    "\n", $vlan['if'], "\n");
453
				continue;
454
			}
455
		} else {
456
			break;
457
		}
458

    
459
		echo gettext("Enter the VLAN tag (1-4094):") . " ";
460
		$vlan['tag'] = chop(fgets($fp));
461
		$vlan['vlanif'] = vlan_interface($vlan);
462
		if (!is_numericint($vlan['tag']) || ($vlan['tag'] < 1) || ($vlan['tag'] > 4094)) {
463
			printf(gettext('%1$sInvalid VLAN tag \'%2$s\'%3$s'), "\n", $vlan['tag'], "\n");
464
			continue;
465
		}
466

    
467
		foreach (config_get_path('vlans/vlan', []) as $existingvlan) {
468
			if ($vlan['if'] == $existingvlan['if'] && $vlan['tag'] == $existingvlan['tag']) {
469
				printf("\n\n" . gettext("This parent interface and VLAN already created."));
470
				continue 2;
471
			}
472
		}
473
		config_set_path('vlans/vlan/', $vlan);
474
		$vlanif++;
475
	}
476
}
477

    
478
function installer_add_gateway($if, $gateway) {
479

    
480
	if (!isset($if) || strlen($if) == 0 ||
481
	    !isset($gateway) || strlen($gateway) == 0) {
482
		return (-1);
483
	}
484

    
485
	/* Delete the gateway first, if already present. */
486
	$gws = config_get_path('gateways/gateway_item', []);
487
	$gws = array_filter($gws, function($gw) use ($gateway, $if) {
488
		if ($gw['gateway'] == $gateway && $gw['interface'] == $if) {
489
			return (false);
490
		}
491
		return (true);
492
	});
493
	config_set_path('gateways/gateway_item', $gws);
494

    
495
	/* Add the interface gateway. */
496
	$gwname = strtoupper($if) . "GW";
497
	$gw = [
498
	    'interface'			=> $if,
499
	    'gateway'			=> $gateway,
500
	    'ipprotocol'		=> 'inet',
501
	    'name'			=> $gwname,
502
	    'descr'			=> "",
503
	    'weight'			=> "",
504
	    'gw_down_kill_states'	=> "",
505
	];
506
	config_set_path('gateways/gateway_item/', $gw);
507
	config_set_path('gateways/defaultgw4', $gwname);
508
	config_set_path("interfaces/{$if}/gateway", $gwname);
509

    
510
	return (0);
511
}
512

    
513
function installer_add_pppoe($dev, $if, $user, $pass, $svc, $svc_null) {
514

    
515
	$newid = 0;
516

    
517
	/* Remove any existing PPPoE settings on the interface. */
518
	$ppps = config_get_path('ppps/ppp', []);
519
	$ppps = array_filter($ppps, function($ppp) use (&$newid, $dev) {
520
		if ($ppp['type'] == "pppoe" && $ppp['ports'] == $dev) {
521
			return (false);
522
		}
523
		$newid = max($newid, $ppp['ptpid'] + 1);
524
		return (true);
525
	});
526
	config_set_path('ppps/ppp', $ppps);
527

    
528
	/* Add the PPPoE connection. */
529
	$ppp = [
530
	    'ptpid'	=> $newid,
531
	    'type'	=> 'pppoe',
532
	    'if'	=> "pppoe{$newid}",
533
	    'ports'	=> $dev,
534
	    'username'	=> $user,
535
	    'password'	=> base64_encode($pass),
536
	    'bandwidth'	=> "",
537
	    'mtu'	=> "",
538
	    'mru'	=> "",
539
	    'mrru'	=> "",
540
	];
541
	if ($svc_null !== true) {
542
		$ppp['provider'] = $svc;
543
	}
544
	config_set_path('ppps/ppp/', $ppp);
545
	config_set_path("interfaces/{$if}/enable", '');
546
	config_set_path("interfaces/{$if}/ipaddr", 'pppoe');
547
	config_set_path("interfaces/{$if}/if", "pppoe{$newid}");
548
	config_del_path("interfaces/{$if}/subnet");
549
	config_del_path("interfaces/{$if}/media");
550
	config_del_path("interfaces/{$if}/mediaopt");
551
	config_del_path("interfaces/{$if}/gateway");
552

    
553
	return (0);
554
}
555

    
556
function installer_add_vlan($dev, $if, $descr, $tag, $pcp) {
557

    
558
	if (intval($tag) < 1 || intval($tag) > 4094 ||
559
	    intval($pcp) < 0 || intval($pcp) > 7) {
560
		return (-1);
561
	}
562

    
563
	/* Delete the VLAN is already present. */
564
	$vlans = config_get_path('vlans/vlan', []);
565
	$vlans = array_filter($vlans, function($vlan) use ($dev, $tag) {
566
		if ($vlan['tag'] == $tag && $vlan['if'] == $dev) {
567
			return (false);
568
		}
569
		return (true);
570
	});
571
	config_set_path('vlans/vlan', $vlans);
572

    
573
	/* Remove other interfaces using this VLAN tag. */
574
	$ifs = config_get_path('interfaces', []);
575
	$ifs = array_filter($ifs, function($intf) use ($dev, $if, $tag) {
576
		if ($intf != $if && $intf['if'] == "{$dev}.{$tag}") {
577
			return (false);
578
		}
579
		return (true);
580
	});
581
	config_set_path('interfaces', $ifs);
582

    
583
	/* Add the VLAN. */
584
	$vlan = [
585
	    'if'	=> $dev,
586
	    'tag'	=> $tag,
587
	    'pcp'	=> $pcp,
588
	    'descr'	=> $descr,
589
	    'vlanif'	=> "{$dev}.{$tag}",
590
	];
591
	config_set_path('vlans/vlan/', $vlan);
592

    
593
	return (0);
594
}
595

    
596
function installer_disable_lan() {
597
	config_del_path('interfaces/lan');
598

    
599
	return (0);
600
}
601

    
602
function installer_setup_lan($lan) {
603

    
604
	if (!isset($lan->if) ||
605
	    !does_interface_exist($lan->if)) {
606
		return (-1);
607
	}
608

    
609
	$if = $lan->if;
610

    
611
	/* VLAN setup. */
612
	if (isset($lan->vlan) && intval($lan->vlan) > 0) {
613
		if (installer_add_vlan($lan->if, "lan", "LAN",
614
		    $lan->vlan, $lan->vlan_pcp) != 0) {
615
			return (-1);
616
		}
617
		$if = "{$lan->if}.{$lan->vlan}";
618
	}
619

    
620
	config_set_path('interfaces/lan/if', $if);
621
	switch ($lan->mode) {
622
	case "DHCP":
623
		config_set_path('interfaces/lan/enable', '');
624
		config_set_path('interfaces/lan/ipaddr', 'dhcp');
625
		config_set_path('interfaces/lan/ipaddrv6', 'dhcp6');
626
		config_set_path('interfaces/lan/subnet', '');
627
		config_set_path('interfaces/lan/gateway', '');
628
		config_set_path('interfaces/lan/media', '');
629
		config_set_path('interfaces/lan/mediaopt', '');
630
		break;
631
	case "STATIC":
632
		list ($ip, $subnet) = explode("/", $lan->ip);
633
		if (empty($subnet)) {
634
			$subnet = "24";
635
		}
636
		config_set_path('interfaces/lan/enable', '');
637
		config_set_path('interfaces/lan/ipaddr', $ip);
638
		config_del_path('interfaces/lan/ipaddrv6');
639
		config_set_path('interfaces/lan/subnet', $subnet);
640
		config_set_path('interfaces/lan/media', '');
641
		config_set_path('interfaces/lan/mediaopt', '');
642
		break;
643
	}
644

    
645
	/* dhcpd. */
646
	if (isset($lan->dhcpd_enabled) &&
647
	    isset($lan->dhcpd_range_start) &&
648
	    isset($lan->dhcpd_range_end) &&
649
	    $lan->dhcpd_enabled === true) {
650
		config_set_path('dhcpd/lan/enable', '');
651
		config_set_path('dhcpd/lan/range/from', $lan->dhcpd_range_start);
652
		config_set_path('dhcpd/lan/range/to', $lan->dhcpd_range_end);
653
	} else {
654
		config_del_path('dhcpd/lan/enable');
655
	}
656

    
657
	return (0);
658
}
659

    
660
function installer_setup_wan($wan) {
661

    
662
	if (!isset($wan->if) ||
663
	    !does_interface_exist($wan->if)) {
664
		return (-1);
665
	}
666

    
667
	$if = $wan->if;
668

    
669
	/* VLAN setup. */
670
	if (isset($wan->vlan) && intval($wan->vlan) > 0) {
671
		if (installer_add_vlan($wan->if, "wan", "WAN",
672
		    $wan->vlan, $wan->vlan_pcp) != 0) {
673
			return (-1);
674
		}
675
		$if = "{$wan->if}.{$wan->vlan}";
676
	}
677

    
678
	config_set_path('interfaces/wan/if', $if);
679
	switch ($wan->mode) {
680
	case "DHCP":
681
		config_set_path('interfaces/wan/enable', '');
682
		config_set_path('interfaces/wan/ipaddr', 'dhcp');
683
		config_set_path('interfaces/wan/ipaddrv6', 'dhcp6');
684
		config_set_path('interfaces/wan/subnet', '');
685
		config_set_path('interfaces/wan/gateway', '');
686
		config_set_path('interfaces/wan/media', '');
687
		config_set_path('interfaces/wan/mediaopt', '');
688
		break;
689
	case "PPPoE":
690
		if (installer_add_pppoe($if, "wan", $wan->PPPoE_User,
691
		    $wan->PPPoE_Password, $wan->PPPoE_Service_Name,
692
		    $wan->PPPoE_NULL_Service) != 0) {
693
			return (-1);
694
		}
695
		break;
696
	case "STATIC":
697
		list ($ip, $subnet) = explode("/", $wan->ip);
698
		if (empty($subnet)) {
699
			$subnet = "24";
700
		}
701
		config_set_path('interfaces/wan/enable', '');
702
		config_set_path('interfaces/wan/ipaddr', $ip);
703
		config_del_path('interfaces/wan/ipaddrv6');
704
		config_set_path('interfaces/wan/subnet', $subnet);
705
		config_set_path('interfaces/wan/media', '');
706
		config_set_path('interfaces/wan/mediaopt', '');
707
		if (isset($wan->default_gateway) &&
708
		    strlen($wan->default_gateway) > 0) {
709
			if (installer_add_gateway("wan", $wan->default_gateway) != 0) {
710
				return (-1);
711
			}
712
		}
713
		break;
714
	}
715

    
716
	/* DNS. */
717
	if (isset($wan->DNS_server) && strlen($wan->DNS_server) > 0) {
718
		config_set_path('system/dnsserver', $wan->DNS_server);
719
	} else {
720
		config_del_path('system/dnsserver');
721
	}
722

    
723
	return (0);
724
}
725

    
726
function installer_settings_import() {
727
	global $g;
728

    
729
	/* Should not happen. */
730
	if (!file_exists("{$g['conf_path']}/installer-settings.json")) {
731
		return (-1);
732
	}
733
	/* Do not accept files bigger than 64KiB. */
734
	$size = filesize("{$g['conf_path']}/installer-settings.json");
735
	if ($size === false || $size >= 64 * 1024) {
736
		return (-1);
737
	}
738

    
739
	/* Read the settings from the Netgate Installer. */
740
	$settings = file_get_contents("{$g['conf_path']}/installer-settings.json");
741
	if ($settings === false) {
742
		return (0);
743
	}
744
	$json = json_decode($settings);
745
	if ($json === NULL ||
746
	    !isset($json->lan) ||
747
	    !isset($json->wan)) {
748
		return (0);
749
	}
750
	$lan = $json->lan;
751
	$wan = $json->wan;
752

    
753
	/* WAN setup. */
754
	if (isset($wan->if) &&
755
	    does_interface_exist($wan->if) &&
756
	    installer_setup_wan($wan) != 0) {
757
		return (-1);
758
	}
759

    
760
	/* LAN setup. */
761
	if (isset($lan->if) &&
762
	    does_interface_exist($lan->if)) {
763
		if (installer_setup_lan($lan) != 0) {
764
			return (-1);
765
		}
766
	} else {
767
		installer_disable_lan();
768
	}
769

    
770
	/* Remove the settings file. */
771
	unlink("{$g['conf_path']}/installer-settings.json");
772

    
773
	/* Let the system know that the interface assignment is done. */
774
	touch("{$g['conf_path']}/assign_complete");
775

    
776
	write_config(gettext("Network settings imported from the Netgate Installer"));
777

    
778
	return (0);
779
}
780

    
781
function check_for_alternate_interfaces() {
782
	// If the WAN and/or LAN devices in the factory default config do not exist,
783
	// then look for alternate devices.
784
	// This lets many systems boot a factory default config without being
785
	// forced to do interface assignment on the console.
786

    
787
	$specplatform = system_identify_specific_platform();
788
	$default_device = array();
789

    
790
	// If we recognise the platform, then specify the devices directly.
791
	$if_config = config_get_path('interfaces', []);
792
	switch ($specplatform['name']) {
793
		case '5100':
794
			$if_config['wan']['if'] = 'igb0';
795
			$if_config['lan']['if'] = 'igb1';
796
			$if_config['opt1'] = array(
797
				'enable' => true,
798
				'if' => 'ix0',
799
				'descr' => 'OPT1'
800
			);
801
			$if_config['opt2'] = array(
802
				'enable' => true,
803
				'if' => 'ix1',
804
				'descr' => 'OPT2'
805
			);
806
			$if_config['opt3'] = array(
807
				'enable' => true,
808
				'if' => 'ix2',
809
				'descr' => 'OPT3'
810
			);
811
			$if_config['opt4'] = array(
812
				'enable' => true,
813
				'if' => 'ix3',
814
				'descr' => 'OPT4'
815
			);
816
			break;
817
		case 'alix':
818
			$default_device['wan'] = "vr1";
819
			$default_device['lan'] = "vr0";
820
			break;
821
		case 'APU':
822
			$default_device['wan'] = "re1";
823
			$default_device['lan'] = "re2";
824
			break;
825
		case 'Turbot Dual-E':
826
			$if_config['wan']['if'] = 'igb0';
827
			$if_config['lan']['if'] = 'igb1';
828
			break;
829
		case 'C2758':
830
			$if_config['wan']['if'] = 'igb0';
831
			$if_config['lan']['if'] = 'igb1';
832
			$if_config['opt1'] = array(
833
				'if' => 'igb2',
834
				'descr' => 'OPT1'
835
			);
836
			$if_config['opt2'] = array(
837
				'if' => 'igb3',
838
				'descr' => 'OPT2'
839
			);
840
			break;
841
		case 'RCC-VE':
842
		case 'SG-2220':
843
			/* SG-4860 or SG-8860 */
844
			if (does_interface_exist('igb4')) {
845
				$if_config['wan']['if'] = 'igb1';
846
				$if_config['lan']['if'] = 'igb0';
847
			} else {
848
				$if_config['wan']['if'] = 'igb0';
849
				$if_config['lan']['if'] = 'igb1';
850
			}
851
			/* It has 4 ports */
852
			if (does_interface_exist('igb3')) {
853
				$if_config['opt1'] = array(
854
					'if' => 'igb2',
855
					'descr' => 'OPT1'
856
				);
857
				$if_config['opt2'] = array(
858
					'if' => 'igb3',
859
					'descr' => 'OPT2'
860
				);
861
			}
862
			/* It has 6 ports */
863
			if (does_interface_exist('igb5')) {
864
				$if_config['opt3'] = array(
865
					'if' => 'igb4',
866
					'descr' => 'OPT3'
867
				);
868
				$if_config['opt4'] = array(
869
					'if' => 'igb5',
870
					'descr' => 'OPT4'
871
				);
872
			}
873
			break;
874
		case '1537':
875
			if (does_interface_exist('cxl0')) {
876
				/* It has 10G SFP+ addon */
877
				$if_config['wan']['if'] = 'cxl0';
878
				$if_config['lan']['if'] = 'cxl1';
879
				$if_config['opt1'] = array(
880
					'if' => 'igb0',
881
					'descr' => 'OPT1'
882
				);
883
				$if_config['opt2'] = array(
884
					'enable' => true,
885
					'if' => 'ix0',
886
					'descr' => 'OPT2'
887
				);
888
				$if_config['opt3'] = array(
889
					'if' => 'igb1',
890
					'descr' => 'OPT3'
891
				);
892
				$if_config['opt4'] = array(
893
					'enable' => true,
894
					'if' => 'ix1',
895
					'descr' => 'OPT4'
896
				);
897
			} elseif (does_interface_exist('igb4')) {
898
				/* It has 4 port ethernet addon */
899
				$if_config['wan']['if'] = 'igb4';
900
				$if_config['lan']['if'] = 'igb5';
901
				$if_config['opt1'] = array(
902
					'enable' => true,
903
					'if' => 'ix0',
904
					'descr' => 'OPT1'
905
				);
906
				$if_config['opt2'] = array(
907
					'enable' => true,
908
					'if' => 'ix1',
909
					'descr' => 'OPT2'
910
				);
911
				$if_config['opt3'] = array(
912
					'if' => 'igb3',
913
					'descr' => 'OPT3'
914
				);
915
				$if_config['opt4'] = array(
916
					'if' => 'igb2',
917
					'descr' => 'OPT4'
918
				);
919
				$if_config['opt5'] = array(
920
					'if' => 'igb1',
921
					'descr' => 'OPT5'
922
				);
923
				$if_config['opt6'] = array(
924
					'if' => 'igb0',
925
					'descr' => 'OPT6'
926
				);
927
			} else {
928
				$if_config['wan']['if'] = 'igb0';
929
				$if_config['lan']['if'] = 'igb1';
930
				$if_config['opt1'] = array(
931
					'enable' => true,
932
					'if' => 'ix0',
933
					'descr' => 'OPT1'
934
				);
935
				$if_config['opt2'] = array(
936
					'enable' => true,
937
					'if' => 'ix1',
938
					'descr' => 'OPT2'
939
				);
940
			}
941
			break;
942
		case '1540':
943
		case '1541':
944
			if (does_interface_exist('igb2')) {
945
				/* It has 4 port Intel 1Gb expansion card */
946
				$if_config['wan']['if'] = 'igb4';
947
				$if_config['lan']['if'] = 'igb5';
948
				$if_config['opt1'] = array(
949
					'enable' => true,
950
					'if' => 'ix0',
951
					'descr' => 'OPT1'
952
				);
953
				$if_config['opt2'] = array(
954
					'enable' => true,
955
					'if' => 'ix1',
956
					'descr' => 'OPT2'
957
				);
958
				$if_config['opt3'] = array(
959
					'if' => 'igb3',
960
					'descr' => 'OPT3'
961
				);
962
				$if_config['opt4'] = array(
963
					'if' => 'igb2',
964
					'descr' => 'OPT4'
965
				);
966
				$if_config['opt5'] = array(
967
					'if' => 'igb1',
968
					'descr' => 'OPT5'
969
				);
970
				$if_config['opt6'] = array(
971
					'if' => 'igb0',
972
					'descr' => 'OPT6'
973
				);
974
			} elseif (does_interface_exist('cxl0')) {
975
				/* It has 2 port Chelsio 10Gb expansion card */
976
				$if_config['wan']['if'] = 'cxl0';
977
				$if_config['lan']['if'] = 'cxl1';
978
				$if_config['opt1'] = array(
979
					'if' => 'igb0',
980
					'descr' => 'OPT1'
981
				);
982
				$if_config['opt2'] = array(
983
					'enable' => true,
984
					'if' => 'ix0',
985
					'descr' => 'OPT2'
986
				);
987
				$if_config['opt3'] = array(
988
					'if' => 'igb1',
989
					'descr' => 'OPT3'
990
				);
991
				$if_config['opt4'] = array(
992
					'enable' => true,
993
					'if' => 'ix1',
994
					'descr' => 'OPT4'
995
				);
996
			} else {
997
				$if_config['wan']['if'] = 'ix0';
998
				$if_config['lan']['if'] = 'ix1';
999
				$if_config['opt1'] = array(
1000
					'if' => 'igb0',
1001
					'descr' => 'OPT1'
1002
				);
1003
				$if_config['opt2'] = array(
1004
					'if' => 'igb1',
1005
					'descr' => 'OPT2'
1006
				);
1007
			}
1008
			break;
1009
		case 'RCC':
1010
			if (does_interface_exist('igb7')) {
1011
				// has quad port expansion card
1012
				$if_config['opt5'] = array(
1013
					'if' => 'igb0',
1014
					'descr' => 'OPT5'
1015
				);
1016
				$if_config['opt6'] = array(
1017
					'if' => 'igb1',
1018
					'descr' => 'OPT6'
1019
				);
1020
				$if_config['opt7'] = array(
1021
					'if' => 'igb2',
1022
					'descr' => 'OPT7'
1023
				);
1024
				$if_config['opt8'] = array(
1025
					'if' => 'igb3',
1026
					'descr' => 'OPT8'
1027
				);
1028
				$if_config['wan']['if'] = 'igb4';
1029
				$if_config['lan']['if'] = 'igb6';
1030
				$if_config['opt1'] = array(
1031
					'if' => 'igb5',
1032
					'descr' => 'OPT1'
1033
				);
1034
				$if_config['opt2'] = array(
1035
					'if' => 'igb7',
1036
					'descr' => 'OPT2'
1037
				);
1038
			} else {
1039
				$if_config['wan']['if'] = 'igb0';
1040
				$if_config['lan']['if'] = 'igb2';
1041
				$if_config['opt1'] = array(
1042
					'if' => 'igb1',
1043
					'descr' => 'OPT1'
1044
				);
1045
				$if_config['opt2'] = array(
1046
					'if' => 'igb3',
1047
					'descr' => 'OPT2'
1048
				);
1049
			}
1050
			$if_config['opt3'] = array(
1051
				'enable' => true,
1052
				'if' => 'ix0',
1053
				'descr' => 'OPT3'
1054
			);
1055
			$if_config['opt4'] = array(
1056
				'enable' => true,
1057
				'if' => 'ix1',
1058
				'descr' => 'OPT4'
1059
			);
1060
			break;
1061
		default:
1062
			$default_device['wan'] = "";
1063
			$default_device['lan'] = "";
1064
			break;
1065
	}
1066
	config_set_path('interfaces', $if_config);
1067

    
1068
	// Other common device names can be put here and will be looked for
1069
	// if the system was not one of the known platforms.
1070
	$other_devices_arr['wan'] = array("vr1", "re1", "igb0", "em0");
1071
	$other_devices_arr['lan'] = array("vr0", "re2", "igb1", "em1");
1072
	$interface_assignment_changed = false;
1073

    
1074
	foreach ($other_devices_arr as $ifname => $other_devices) {
1075
		$if_config = config_get_path("interfaces/{$ifname}/if");
1076
		if (!does_interface_exist($if_config)) {
1077
			if (does_interface_exist($default_device[$ifname])) {
1078
				$if_config = $default_device[$ifname];
1079
				$interface_assignment_changed = true;
1080
			} else {
1081
				foreach ($other_devices as $other_device) {
1082
					if (does_interface_exist($other_device)) {
1083
						$if_config = $other_device;
1084
						$interface_assignment_changed = true;
1085
						break;
1086
					}
1087
				}
1088
			}
1089
			config_set_path("interfaces/{$ifname}/if", $if_config);
1090
		}
1091
	}
1092

    
1093
	if ($interface_assignment_changed) {
1094
		write_config("Factory default boot detected WAN " . config_get_path('interfaces/wan/if') . " and LAN " . config_get_path('interfaces/lan/if'));
1095
	}
1096
}
1097

    
1098
?>
(8-8/61)