diff --git a/usr.sbin/sysinstall/tcpip.c b/usr.sbin/sysinstall/tcpip.c index f505a0581c0d..75a40e87512b 100644 --- a/usr.sbin/sysinstall/tcpip.c +++ b/usr.sbin/sysinstall/tcpip.c @@ -40,10 +40,17 @@ #include "sysinstall.h" #include #include +#include #include +#include + #include +#include +#include + #include #include +#include /* The help file for the TCP/IP setup screen */ #define TCP_HELPFILE "tcp" @@ -636,6 +643,51 @@ netHook(dialogMenuItem *self) return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE; } +static char * +tcpDeviceScan(void) +{ + int s; + struct ifmediareq ifmr; + struct ifaddrs *ifap, *ifa; + struct if_data *ifd; + char *network_dev; + + if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0) + return (NULL); + + if (getifaddrs(&ifap) < 0) + return (NULL); + + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { + memset(&ifmr, 0, sizeof(ifmr)); + strlcpy(ifmr.ifm_name, ifa->ifa_name, sizeof(ifmr.ifm_name)); + + if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) + continue; /* some devices don't support this */ + + if ((ifmr.ifm_status & IFM_AVALID) == 0) + continue; /* not active */ + + if (IFM_TYPE(ifmr.ifm_active) != IFM_ETHER) + continue; /* not an ethernet device */ + + if (ifmr.ifm_status & IFM_ACTIVE) { + network_dev = strdup(ifa->ifa_name); + freeifaddrs(ifap); + + if (!variable_get(VAR_NONINTERACTIVE)) + msgConfirm("Using interface %s", network_dev); + + msgDebug("tcpDeviceScan found %s", network_dev); + return (network_dev); + } + } + + freeifaddrs(ifap); + + return (NULL); +} + /* Get a network device */ Device * tcpDeviceSelect(void) @@ -647,27 +699,38 @@ tcpDeviceSelect(void) rval = NULL; - if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) { + if (variable_get(VAR_NETWORK_DEVICE)) { network_dev = variable_get(VAR_NETWORK_DEVICE); + /* + * netDev can be set to several types of values. + * If netDev is set to ANY, scan all network devices + * looking for a valid link, and go with the first + * device found. netDev can also be specified as a + * comma delimited list, with each network device + * tried in order. netDev can also be set to a single + * network device. + */ + if (!strcmp(network_dev, "ANY")) + network_dev = strdup(tcpDeviceScan()); + while ((dev = strsep(&network_dev, ",")) != NULL) { devs = deviceFind(dev, DEVICE_TYPE_NETWORK); cnt = deviceCount(devs); + if (cnt) { - if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS)) - return(devs[0]); + if (DITEM_STATUS(tcpOpenDialog(devs[0])) == DITEM_SUCCESS) + return (devs[0]); } } + + if (!variable_get(VAR_NONINTERACTIVE)) + msgConfirm("No network devices available!"); + + return (NULL); } - devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK); - cnt = deviceCount(devs); - - if (!cnt) { - msgConfirm("No network devices available!"); - return NULL; - } - else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) { + if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) { if (!msgYesNo("Running multi-user, assume that the network is already configured?")) return devs[0]; }