Module Name:    src
Committed By:   roy
Date:           Mon Sep 28 13:50:22 UTC 2020

Modified Files:
        src/sbin/ifconfig: ifconfig.c

Log Message:
ifconfig: Just look at if_data->ifi_link_state for carrier

It should be the sole source of truth.
if_data is also carried in ifa_data from getifaddrs(3) which saves
more ioctl calls.


To generate a diff of this commit:
cvs rdiff -u -r1.246 -r1.247 src/sbin/ifconfig/ifconfig.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/ifconfig/ifconfig.c
diff -u src/sbin/ifconfig/ifconfig.c:1.246 src/sbin/ifconfig/ifconfig.c:1.247
--- src/sbin/ifconfig/ifconfig.c:1.246	Sat Sep 26 23:43:26 2020
+++ src/sbin/ifconfig/ifconfig.c	Mon Sep 28 13:50:22 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifconfig.c,v 1.246 2020/09/26 23:43:26 roy Exp $	*/
+/*	$NetBSD: ifconfig.c,v 1.247 2020/09/28 13:50:22 roy Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: ifconfig.c,v 1.246 2020/09/26 23:43:26 roy Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.247 2020/09/28 13:50:22 roy Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -852,6 +852,7 @@ printall(const char *ifname, prop_dictio
 	struct ifaddrs *ifap, *ifa;
 	struct ifreq ifr;
 	const struct sockaddr_dl *sdl = NULL;
+	const struct if_data *ifi = NULL;
 	prop_dictionary_t env, oenv;
 	int idx;
 	char *p;
@@ -880,8 +881,10 @@ printall(const char *ifname, prop_dictio
 
 		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
 			continue;
-		if (ifa->ifa_addr->sa_family == AF_LINK)
+		if (ifa->ifa_addr->sa_family == AF_LINK) {
 			sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
+			ifi = (const struct if_data *)ifa->ifa_data;
+		}
 		if (p && strcmp(p, ifa->ifa_name) == 0)
 			continue;
 		if (!prop_dictionary_set_string(env, "if", ifa->ifa_name))
@@ -895,7 +898,8 @@ printall(const char *ifname, prop_dictio
 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
 			continue;
 
-		if (sflag && carrier(env))
+		if (sflag && (ifi == NULL ||
+		              ifi->ifi_link_state == LINK_STATE_DOWN))
 			continue;
 		idx++;
 		/*
@@ -910,6 +914,7 @@ printall(const char *ifname, prop_dictio
 
 		status(sdl, ifa->ifa_data, env, oenv);
 		sdl = NULL;
+		ifi = NULL;
 	}
 	if (lflag)
 		printf("\n");
@@ -1213,34 +1218,15 @@ setifmtu(prop_dictionary_t env, prop_dic
 static int
 carrier(prop_dictionary_t env)
 {
-	struct ifmediareq ifmr = { .ifm_status = 0 };
+	struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
 
-	if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
-		/*
-		 * Interface doesn't support SIOC{G,S}IFMEDIA;
-		 * check link state.
-		 */
-		struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
+	if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
+		return EXIT_FAILURE;
 
-		if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
-			return EXIT_FAILURE;
-		if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_DOWN)
-			return EXIT_FAILURE;
-		else
-			return EXIT_SUCCESS;
-	}
-	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
-		/*
-		 * Interface doesn't report media-valid status.
-		 * assume ok.
-		 */
-		return EXIT_SUCCESS;
-	}
-	/* otherwise, return ok for active, not-ok if not active. */
-	if (ifmr.ifm_status & IFM_ACTIVE)
-		return EXIT_SUCCESS;
-	else
+	if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_DOWN)
 		return EXIT_FAILURE;
+	else /* Assume UP if UNKNOWN */
+		return EXIT_SUCCESS;
 }
 
 static void

Reply via email to