git: 9d4a08d162d8 - main - linux: use sa_family_t for address family conversions

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 29 Mar 2024 20:36:12 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=9d4a08d162d87ba120f418a1a71facd2c631b549

commit 9d4a08d162d87ba120f418a1a71facd2c631b549
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-03-29 20:35:37 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-03-29 20:35:37 +0000

    linux: use sa_family_t for address family conversions
    
    Express "conversion failed" with maximum possible value.  This allows to
    reduce number of size/signedness conversion in the code that utilizes the
    functions.
    
    PR:                     274536
    Reviewed by:            melifaro
    Differential Revision:  https://reviews.freebsd.org/D44375
---
 sys/compat/linux/linux.c        | 18 +++++++++---------
 sys/compat/linux/linux_common.h |  5 +++--
 sys/compat/linux/linux_socket.c |  9 +++++----
 3 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/sys/compat/linux/linux.c b/sys/compat/linux/linux.c
index 69849b1d98ad..61b207070963 100644
--- a/sys/compat/linux/linux.c
+++ b/sys/compat/linux/linux.c
@@ -501,8 +501,8 @@ linux_ifhwaddr(struct ifnet *ifp, struct l_sockaddr *lsa)
 	return (ENOENT);
 }
 
-int
-linux_to_bsd_domain(int domain)
+sa_family_t
+linux_to_bsd_domain(sa_family_t domain)
 {
 
 	switch (domain) {
@@ -523,11 +523,11 @@ linux_to_bsd_domain(int domain)
 	case LINUX_AF_NETLINK:
 		return (AF_NETLINK);
 	}
-	return (-1);
+	return (AF_UNKNOWN);
 }
 
-int
-bsd_to_linux_domain(int domain)
+sa_family_t
+bsd_to_linux_domain(sa_family_t domain)
 {
 
 	switch (domain) {
@@ -548,7 +548,7 @@ bsd_to_linux_domain(int domain)
 	case AF_NETLINK:
 		return (LINUX_AF_NETLINK);
 	}
-	return (-1);
+	return (AF_UNKNOWN);
 }
 
 /*
@@ -562,13 +562,13 @@ bsd_to_linux_sockaddr(const struct sockaddr *sa, struct l_sockaddr **lsa,
     socklen_t len)
 {
 	struct l_sockaddr *kosa;
-	int bdom;
+	sa_family_t bdom;
 
 	*lsa = NULL;
 	if (len < 2 || len > UCHAR_MAX)
 		return (EINVAL);
 	bdom = bsd_to_linux_domain(sa->sa_family);
-	if (bdom == -1)
+	if (bdom == AF_UNKNOWN)
 		return (EAFNOSUPPORT);
 
 	kosa = malloc(len, M_LINUX, M_WAITOK);
@@ -615,7 +615,7 @@ linux_to_bsd_sockaddr(const struct l_sockaddr *osa, struct sockaddr **sap,
 		goto out;
 
 	bdom = linux_to_bsd_domain(kosa->sa_family);
-	if (bdom == -1) {
+	if (bdom == AF_UNKNOWN) {
 		error = EAFNOSUPPORT;
 		goto out;
 	}
diff --git a/sys/compat/linux/linux_common.h b/sys/compat/linux/linux_common.h
index 485ebaab58f2..97f5a259f300 100644
--- a/sys/compat/linux/linux_common.h
+++ b/sys/compat/linux/linux_common.h
@@ -38,8 +38,9 @@ unsigned short	linux_ifflags(struct ifnet *);
 int		linux_ifhwaddr(struct ifnet *ifp, struct l_sockaddr *lsa);
 
 unsigned short	bsd_to_linux_ifflags(int);
-int		linux_to_bsd_domain(int domain);
-int		bsd_to_linux_domain(int domain);
+sa_family_t	linux_to_bsd_domain(sa_family_t domain);
+sa_family_t	bsd_to_linux_domain(sa_family_t domain);
+#define	AF_UNKNOWN	UINT8_MAX
 int		bsd_to_linux_sockaddr(const struct sockaddr *sa,
 		    struct l_sockaddr **lsa, socklen_t len);
 int		linux_to_bsd_sockaddr(const struct l_sockaddr *lsa,
diff --git a/sys/compat/linux/linux_socket.c b/sys/compat/linux/linux_socket.c
index ae22af90d443..1e578982fced 100644
--- a/sys/compat/linux/linux_socket.c
+++ b/sys/compat/linux/linux_socket.c
@@ -870,7 +870,8 @@ static const char *linux_netlink_names[] = {
 int
 linux_socket(struct thread *td, struct linux_socket_args *args)
 {
-	int domain, retval_socket, type;
+	int retval_socket, type;
+	sa_family_t domain;
 
 	type = args->type & LINUX_SOCK_TYPE_MASK;
 	if (type < 0 || type > LINUX_SOCK_MAX)
@@ -880,7 +881,7 @@ linux_socket(struct thread *td, struct linux_socket_args *args)
 	if (retval_socket != 0)
 		return (retval_socket);
 	domain = linux_to_bsd_domain(args->domain);
-	if (domain == -1) {
+	if (domain == AF_UNKNOWN) {
 		/* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
 		type = args->type & LINUX_SOCK_TYPE_MASK;
 		if (args->domain == LINUX_AF_NETLINK &&
@@ -2309,8 +2310,8 @@ linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
 			    name, &newval, UIO_SYSSPACE, &len);
 			if (error != 0)
 				return (error);
-			newval = bsd_to_linux_domain(newval);
-			if (newval == -1)
+			newval = bsd_to_linux_domain((sa_family_t)newval);
+			if (newval == AF_UNKNOWN)
 				return (ENOPROTOOPT);
 			return (linux_sockopt_copyout(td, &newval,
 			    len, args));