5 * Gary J Palmer. All rights reserved.
7 * Jordan K. Hubbard. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * verbatim and that no modifications are made prior to this
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 * OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * All kinds of hacking also performed by jkh on this code. Don't
35 * blame Gary for every bogosity you see here.. :-)
40 #include "sysinstall.h"
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <netinet/in.h>
49 #include <net/if_media.h>
55 /* The help file for the TCP/IP setup screen */
56 #define TCP_HELPFILE "tcp"
58 /* These are nasty, but they make the layout structure a lot easier ... */
60 static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN],
61 gateway[IPADDR_FIELD_LEN], nameserver[INET6_ADDRSTRLEN];
62 static int okbutton, cancelbutton;
63 static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN];
64 static char ipv6addr[INET6_ADDRSTRLEN];
66 /* What the screen size is meant to be */
67 #define TCP_DIALOG_Y 0
68 #define TCP_DIALOG_X 8
69 #define TCP_DIALOG_WIDTH COLS - 16
70 #define TCP_DIALOG_HEIGHT LINES - 2
72 static Layout layout[] = {
73 #define LAYOUT_HOSTNAME 0
74 { 1, 2, 25, HOSTNAME_FIELD_LEN - 1,
75 "Host:", "Your fully-qualified hostname, e.g. foo.example.com",
76 hostname, STRINGOBJ, NULL },
77 #define LAYOUT_DOMAINNAME 1
78 { 1, 35, 20, HOSTNAME_FIELD_LEN - 1,
80 "The name of the domain that your machine is in, e.g. example.com",
81 domainname, STRINGOBJ, NULL },
82 #define LAYOUT_GATEWAY 2
83 { 5, 2, 18, IPADDR_FIELD_LEN - 1,
85 "IPv4 address of host forwarding packets to non-local destinations",
86 gateway, STRINGOBJ, NULL },
87 #define LAYOUT_NAMESERVER 3
88 { 5, 35, 18, INET6_ADDRSTRLEN - 1,
89 "Name server:", "IPv4 or IPv6 address of your local DNS server",
90 nameserver, STRINGOBJ, NULL },
91 #define LAYOUT_IPADDR 4
92 { 10, 10, 18, IPADDR_FIELD_LEN - 1,
94 "The IPv4 address to be used for this interface",
95 ipaddr, STRINGOBJ, NULL },
96 #define LAYOUT_NETMASK 5
97 { 10, 35, 18, IPADDR_FIELD_LEN - 1,
99 "The netmask for this interface, e.g. 0xffffff00 for a class C network",
100 netmask, STRINGOBJ, NULL },
101 #define LAYOUT_EXTRAS 6
102 { 14, 10, 37, HOSTNAME_FIELD_LEN - 1,
103 "Extra options to ifconfig (usually empty):",
104 "Any interface-specific options to ifconfig you would like to add",
105 extras, STRINGOBJ, NULL },
106 #define LAYOUT_OKBUTTON 7
108 "OK", "Select this if you are happy with these settings",
109 &okbutton, BUTTONOBJ, NULL },
110 #define LAYOUT_CANCELBUTTON 8
112 "CANCEL", "Select this if you wish to cancel this screen",
113 &cancelbutton, BUTTONOBJ, NULL },
117 #define _validByte(b) ((b) >= 0 && (b) <= 255)
124 msgConfirm("%s", msg);
127 /* Verify IP address integrity */
129 verifyIP(char *ip, unsigned long *mask, unsigned long *out)
132 char *endptr, *endptr_prev;
134 unsigned long parsedip;
135 unsigned long max_addr = (255 << 24) | (255 << 16) | (255 << 8) | 255;
139 a = strtol(ip, &endptr, 10);
140 if (endptr - ip == 0 || *endptr++ != '.')
142 endptr_prev = endptr;
143 b = strtol(endptr, &endptr, 10);
144 if (endptr - endptr_prev == 0 || *endptr++ != '.')
146 endptr_prev = endptr;
147 c = strtol(endptr, &endptr, 10);
148 if (endptr - endptr_prev == 0 || *endptr++ != '.')
150 endptr_prev = endptr;
151 d = strtol(endptr, &endptr, 10);
152 if (*endptr != '\0' || endptr - endptr_prev == 0)
154 if (!_validByte(a) || !_validByte(b) || !_validByte(c) || !_validByte(d))
156 parsedip = (a << 24) | (b << 16) | (c << 8) | d;
160 * The ip address must not be network or broadcast address.
162 if (mask && ((parsedip == (parsedip & *mask)) ||
163 (parsedip == ((parsedip & *mask) + max_addr - *mask))))
171 struct addrinfo hints, *res;
173 memset(&hints, 0, sizeof(hints));
174 hints.ai_family = AF_INET6;
175 hints.ai_socktype = SOCK_STREAM;
176 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
177 if (getaddrinfo(ip, NULL, &hints, &res) == 0) {
184 /* Verify IPv4 netmask as being well-formed as
185 a 0x or AAA.BBB.CCC.DDD mask */
187 verifyNetmask(const char *netmask, unsigned long *out)
193 if (netmask[0] == '0' && (netmask[1] == 'x' || netmask[1] == 'X')) {
194 /* Parse out hex mask */
195 mask = strtoul(netmask, &endptr, 0);
199 /* Parse out quad decimal mask */
200 tmp = strtoul(netmask, &endptr, 10);
201 if (!_validByte(tmp) || *endptr++ != '.')
204 tmp = strtoul(endptr, &endptr, 10);
205 if (!_validByte(tmp) || *endptr++ != '.')
207 mask = (mask << 8) + tmp;
208 tmp = strtoul(endptr, &endptr, 10);
209 if (!_validByte(tmp) || *endptr++ != '.')
211 mask = (mask << 8) + tmp;
212 tmp = strtoul(endptr, &endptr, 10);
213 if (!_validByte(tmp) || *endptr++ != '\0')
215 mask = (mask << 8) + tmp;
217 /* Verify that we have a continous netmask */
218 if ((((-mask & mask) - 1) | mask) != 0xffffffff)
226 verifyGW(char *gw, unsigned long *ip, unsigned long *mask)
228 unsigned long parsedgw;
230 if (!verifyIP(gw, mask, &parsedgw))
232 /* Gateway needs to be within the set of IPs reachable through the
234 if (ip && mask && ((parsedgw & *mask) != (*ip & *mask)))
239 /* Check for the settings on the screen - the per-interface stuff is
240 moved to the main handling code now to do it on the fly - sigh */
244 unsigned long parsedip;
245 unsigned long parsednetmask;
248 feepout("Must specify a host name of some sort!");
249 else if (netmask[0] && !verifyNetmask(netmask, &parsednetmask))
250 feepout("Invalid netmask value");
251 else if (nameserver[0] && !verifyIP(nameserver, NULL, NULL) &&
252 !verifyIP6(nameserver))
253 feepout("Invalid name server IP address specified");
254 else if (ipaddr[0] && !verifyIP(ipaddr, &parsednetmask, &parsedip))
255 feepout("Invalid IPv4 address");
256 else if (gateway[0] && strcmp(gateway, "NO") &&
257 !verifyGW(gateway, ipaddr[0] ? &parsedip : NULL,
258 netmask[0] ? &parsednetmask : NULL))
259 feepout("Invalid gateway IPv4 address specified");
266 dhcpGetInfo(Device *devp)
268 char leasefile[PATH_MAX];
270 snprintf(leasefile, sizeof(leasefile), "%sdhclient.leases.%s",
271 _PATH_VARDB, devp->name);
272 /* If it fails, do it the old-fashioned way */
273 if (dhcpParseLeases(leasefile, hostname, domainname,
274 nameserver, ipaddr, gateway, netmask) == -1) {
276 char *cp, cmd[256], data[2048];
279 /* Bah, now we have to kludge getting the information from ifconfig */
280 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
281 ifp = popen(cmd, "r");
283 j = fread(data, 1, sizeof(data), ifp);
285 if (j < 0) /* paranoia */
289 msgDebug("DHCP configured interface returns %s\n", data);
290 /* XXX This is gross as it assumes a certain ordering to
291 ifconfig's output! XXX */
292 if ((cp = strstr(data, "inet ")) != NULL) {
294 cp += 5; /* move over keyword */
296 ipaddr[i++] = *(cp++);
298 if (!strncmp(++cp, "netmask", 7)) {
302 netmask[i++] = *(cp++);
309 /* If we didn't get a name server value, hunt for it in resolv.conf */
310 if (!nameserver[0] && file_readable("/etc/resolv.conf"))
311 configEnvironmentResolv("/etc/resolv.conf");
313 variable_set2(VAR_HOSTNAME, hostname, 0);
317 rtsolGetInfo(Device *devp)
320 char *cp, cmd[256], data[2048];
323 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
324 if ((ifp = popen(cmd, "r")) == NULL)
326 while (fgets(data, sizeof(data), ifp) != NULL) {
328 msgDebug("RTSOL configured interface returns %s\n", data);
329 if ((cp = strstr(data, "inet6 ")) != NULL) {
330 cp += 6; /* move over keyword */
331 if (strncmp(cp, "fe80:", 5)) {
334 ipv6addr[i++] = *(cp++);
342 /* This is it - how to get TCP setup values */
344 tcpOpenDialog(Device *devp)
346 WINDOW *ds_win, *save = NULL;
347 ComposeObj *obj = NULL;
348 int n = 0, filled = 0, cancel = FALSE;
349 int max, ret = DITEM_SUCCESS;
350 int use_dhcp = FALSE;
351 int use_rtsol = FALSE;
356 /* Initialise vars from previous device values */
358 DevInfo *di = (DevInfo *)devp->private;
360 SAFE_STRCPY(ipaddr, di->ipaddr);
361 SAFE_STRCPY(netmask, di->netmask);
362 SAFE_STRCPY(extras, di->extras);
363 use_dhcp = di->use_dhcp;
364 use_rtsol = di->use_rtsol;
366 else { /* See if there are any defaults */
368 char *old_interactive = NULL;
371 * This is a hack so that the dialogs below are interactive in a
372 * script if we have requested interactive behavior.
374 if (variable_get(VAR_NONINTERACTIVE) &&
375 variable_get(VAR_NETINTERACTIVE)) {
376 old_interactive = strdup(VAR_NONINTERACTIVE);
377 variable_unset(VAR_NONINTERACTIVE);
382 * Try a RTSOL scan if such behavior is desired.
383 * If the variable was configured and is YES, do it.
384 * If it was configured to anything else, treat it as NO.
385 * Otherwise, ask the question interactively.
387 if (!variable_get(VAR_NO_INET6) &&
388 (!variable_cmp(VAR_TRY_RTSOL, "YES") ||
389 (variable_get(VAR_TRY_RTSOL)==0 && !msgNoYes("Do you want to try IPv6 configuration of the interface?")))) {
394 sysctlbyname("net.inet6.ip6.forwarding", NULL, 0, &i, sizeof(i));
396 sysctlbyname("net.inet6.ip6.accept_rtadv", NULL, 0, &i, sizeof(i));
397 vsystem("ifconfig %s up", devp->name);
399 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
402 msgNotify("Scanning for RA servers...");
403 if (0 == vsystem("rtsol %s", devp->name)) {
405 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
415 * First try a DHCP scan if such behavior is desired.
416 * If the variable was configured and is YES, do it.
417 * If it was configured to anything else, treat it as NO.
418 * Otherwise, ask the question interactively.
420 if (!variable_cmp(VAR_TRY_DHCP, "YES") ||
421 (variable_get(VAR_TRY_DHCP)==0 && !msgNoYes("Do you want to try DHCP configuration of the interface?"))) {
425 msgNotify("Scanning for DHCP servers...");
426 /* XXX clear any existing lease */
427 /* XXX limit protocol to N tries */
428 if (0 == vsystem("dhclient %s", devp->name)) {
436 /* Restore old VAR_NONINTERACTIVE if needed. */
437 if (old_interactive != NULL) {
438 variable_set2(VAR_NONINTERACTIVE, old_interactive, 0);
439 free(old_interactive);
442 /* Special hack so it doesn't show up oddly in the tcpip setup menu */
443 if (!strcmp(gateway, "NO"))
446 /* Get old IP address from variable space, if available */
448 if ((cp = variable_get(VAR_IPADDR)) != NULL)
449 SAFE_STRCPY(ipaddr, cp);
450 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL)
451 SAFE_STRCPY(ipaddr, cp);
454 /* Get old netmask from variable space, if available */
456 if ((cp = variable_get(VAR_NETMASK)) != NULL)
457 SAFE_STRCPY(netmask, cp);
458 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL)
459 SAFE_STRCPY(netmask, cp);
462 /* Get old extras string from variable space, if available */
464 if ((cp = variable_get(VAR_EXTRAS)) != NULL)
465 SAFE_STRCPY(extras, cp);
466 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL)
467 SAFE_STRCPY(extras, cp);
471 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */
473 tmp = variable_get(VAR_HOSTNAME);
475 SAFE_STRCPY(hostname, tmp);
477 if (!domainname[0]) {
478 tmp = variable_get(VAR_DOMAINNAME);
480 SAFE_STRCPY(domainname, tmp);
483 tmp = variable_get(VAR_GATEWAY);
484 if (tmp && strcmp(tmp, "NO"))
485 SAFE_STRCPY(gateway, tmp);
487 if (!nameserver[0]) {
488 tmp = variable_get(VAR_NAMESERVER);
490 SAFE_STRCPY(nameserver, tmp);
493 /* If non-interactive, jump straight over the dialog crap and into config section */
494 if (variable_get(VAR_NONINTERACTIVE) &&
495 !variable_get(VAR_NETINTERACTIVE)) {
497 msgConfirm("WARNING: hostname variable not set and is a non-optional\n"
498 "parameter. Please add this to your installation script\n"
499 "or set the netInteractive variable (see sysinstall man page)");
504 /* Now do all the screen I/O */
505 dialog_clear_norefresh();
507 /* Modify the help line for PLIP config */
508 if (!strncmp(devp->name, "plip", 4))
509 layout[LAYOUT_EXTRAS].help =
510 "For PLIP configuration, you must enter the peer's IP address here.";
512 /* We need a curses window */
513 tmp = " Network Configuration ";
515 tmp = string_concat(tmp, "(IPv6 ready) ");
516 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, tmp,
517 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) {
519 msgConfirm("Cannot open TCP/IP dialog window!!");
521 return DITEM_FAILURE;
524 /* Draw interface configuration box */
525 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17,
526 dialog_attr, border_attr);
527 wattrset(ds_win, dialog_attr);
528 sprintf(title, " Configuration for Interface %s ", devp->name);
529 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title);
531 /* Some more initialisation before we go into the main input loop */
532 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max);
535 cancelbutton = okbutton = 0;
536 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) {
537 /* Prevent this from being irritating if user really means NO */
539 /* Insert a default value for the netmask, 0xffffff00 is
540 * the most appropriate one (entire class C, or subnetted
541 * class A/B network).
544 strcpy(netmask, "255.255.255.0");
545 RefreshStringObj(layout[LAYOUT_NETMASK].obj);
548 if (!index(hostname, '.') && domainname[0]) {
549 strcat(hostname, ".");
550 strcat(hostname, domainname);
551 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj);
554 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) {
555 SAFE_STRCPY(domainname, tmp + 1);
556 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj);
561 if (!cancel && !verifySettings())
564 /* Clear this crap off the screen */
566 dialog_clear_norefresh();
569 /* We actually need to inform the rest of sysinstall about this
570 data now if the user hasn't selected cancel. Save the stuff
571 out to the environment via the variable_set() mechanism */
576 char temp[512], ifn[255];
577 int ipv4_enable = FALSE;
580 variable_set2(VAR_HOSTNAME, hostname, 1);
581 sethostname(hostname, strlen(hostname));
584 variable_set2(VAR_DOMAINNAME, domainname, 0);
586 variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1);
588 variable_set2(VAR_NAMESERVER, nameserver, 0);
590 variable_set2(VAR_IPADDR, ipaddr, 0);
592 variable_set2(VAR_IPV6ADDR, ipv6addr, 0);
595 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo));
597 SAFE_STRCPY(di->ipaddr, ipaddr);
598 SAFE_STRCPY(di->netmask, netmask);
599 SAFE_STRCPY(di->extras, extras);
600 di->use_dhcp = use_dhcp;
601 di->use_rtsol = use_rtsol;
603 if (use_dhcp || ipaddr[0])
606 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name);
608 if (strlen(extras) > 0)
609 sprintf(temp, "DHCP %s", extras);
611 sprintf(temp, "DHCP");
613 sprintf(temp, "inet %s %s netmask %s",
614 ipaddr, extras, netmask);
615 variable_set2(ifn, temp, 1);
618 variable_set2(VAR_IPV6_ENABLE, "YES", 1);
620 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */
629 static Device *NetDev;
632 netHook(dialogMenuItem *self)
636 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK);
638 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE)
643 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
650 struct ifmediareq ifmr;
651 struct ifaddrs *ifap, *ifa;
654 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
657 if (getifaddrs(&ifap) < 0)
660 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
661 memset(&ifmr, 0, sizeof(ifmr));
662 strlcpy(ifmr.ifm_name, ifa->ifa_name, sizeof(ifmr.ifm_name));
664 if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
665 continue; /* some devices don't support this */
667 if ((ifmr.ifm_status & IFM_AVALID) == 0)
668 continue; /* not active */
670 if (IFM_TYPE(ifmr.ifm_active) != IFM_ETHER)
671 continue; /* not an ethernet device */
673 if (ifmr.ifm_status & IFM_ACTIVE) {
674 network_dev = strdup(ifa->ifa_name);
677 if (!variable_get(VAR_NONINTERACTIVE))
678 msgConfirm("Using interface %s", network_dev);
680 msgDebug("tcpDeviceScan found %s", network_dev);
681 return (network_dev);
692 /* Get a network device */
694 tcpDeviceSelect(void)
697 Device **devs, *rval;
698 char *dev, *network_dev;
703 if (variable_get(VAR_NETWORK_DEVICE)) {
704 network_dev = variable_get(VAR_NETWORK_DEVICE);
707 * netDev can be set to several types of values.
708 * If netDev is set to ANY, scan all network devices
709 * looking for a valid link, and go with the first
710 * device found. netDev can also be specified as a
711 * comma delimited list, with each network device
712 * tried in order. netDev can also be set to a single
715 if (!strcmp(network_dev, "ANY"))
716 network_dev = strdup(tcpDeviceScan());
718 while ((dev = strsep(&network_dev, ",")) != NULL) {
719 devs = deviceFind(dev, DEVICE_TYPE_NETWORK);
720 cnt = deviceCount(devs);
723 if (DITEM_STATUS(tcpOpenDialog(devs[0])) == DITEM_SUCCESS)
728 if (!variable_get(VAR_NONINTERACTIVE))
729 msgConfirm("No network devices available!");
734 devs = deviceFind(NULL, DEVICE_TYPE_NETWORK);
735 cnt = deviceCount(devs);
737 if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
738 if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
742 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
748 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL);
750 msgFatal("Unable to create network device menu! Argh!");
751 status = dmenuOpenSimple(menu, FALSE);
759 /* Do it from a menu that doesn't care about status */
761 tcpMenuSelect(dialogMenuItem *self)
766 variable_set("NETWORK_CONFIGURED=NO",0);
767 tmp = tcpDeviceSelect();
768 variable_unset("NETWORK_CONFIGURED");
770 if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
771 if (!DEVICE_INIT(tmp))
772 msgConfirm("Initialization of %s device failed.", tmp->name);
774 return DITEM_SUCCESS;