Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Jul 2024 14:59:54 GMT
From:      Konrad Witaszczyk <def@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: c74a4cea0dc9 - main - usr.sbin/uhsoctl: correct SIOCDIFADDR calls to use ifreq
Message-ID:  <202407221459.46MExskr033058@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by def:

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

commit c74a4cea0dc9dddf996179b1586fb6e9fbf6b702
Author:     Konrad Witaszczyk <def@FreeBSD.org>
AuthorDate: 2024-06-28 10:29:15 +0000
Commit:     Konrad Witaszczyk <def@FreeBSD.org>
CommitDate: 2024-07-22 14:58:19 +0000

    usr.sbin/uhsoctl: correct SIOCDIFADDR calls to use ifreq
    
    The SIOCDIFADDR ioctl takes an ifreq structure object, not an ifaliasreq
    structure object, as its argument.
    
    The ifreq structure does not include a separate mask field and the
    SIOCDIFADDR ioctl handler does not require a network mask as opposed to
    the ifaliasreq structure and the SIOCAIFADDR ioctl. Given that
    SIOCAIFADDR and SIOCDIFADDR ioctls significantly differ and use
    different structures for ioctl arguments, remove ifaddr_add() and move
    its logic handling these ioctls to ifaddr_add()'s callers.
    
    Reviewed by:    brooks, jhb, oshogbo
    Accepted by:    oshogbo (mentor)
    Obtained from:  CheriBSD
    Differential Revision: https://reviews.freebsd.org/D46019
---
 usr.sbin/uhsoctl/uhsoctl.c | 68 ++++++++++++++++++++++------------------------
 1 file changed, 33 insertions(+), 35 deletions(-)

diff --git a/usr.sbin/uhsoctl/uhsoctl.c b/usr.sbin/uhsoctl/uhsoctl.c
index 992858eb45ca..b7aa1a45c667 100644
--- a/usr.sbin/uhsoctl/uhsoctl.c
+++ b/usr.sbin/uhsoctl/uhsoctl.c
@@ -356,27 +356,6 @@ logger(int pri, const char *fmt, ...)
 	va_end(ap);
 }
 
-/* Add/remove IP address from an interface */
-static int
-ifaddr_ad(unsigned long d, const char *ifnam, struct sockaddr *sa, struct sockaddr *mask)
-{
-	struct ifaliasreq req;
-	int fd, error;
-
-	fd = socket(AF_INET, SOCK_DGRAM, 0);
-	if (fd < 0)
-		return (-1);
-
-	memset(&req, 0, sizeof(struct ifaliasreq));
-	strlcpy(req.ifra_name, ifnam, sizeof(req.ifra_name));
-	memcpy(&req.ifra_addr, sa, sa->sa_len);
-	memcpy(&req.ifra_mask, mask, mask->sa_len);
-
-	error = ioctl(fd, d, (char *)&req);
-	close(fd);
-	return (error);
-}
-
 #define if_ifup(ifnam) if_setflags(ifnam, IFF_UP)
 #define if_ifdown(ifnam) if_setflags(ifnam, -IFF_UP)
 
@@ -418,22 +397,45 @@ if_setflags(const char *ifnam, int flags)
 static int
 ifaddr_add(const char *ifnam, struct sockaddr *sa, struct sockaddr *mask)
 {
-	int error;
+	struct ifaliasreq ifra;
+	int error, fd;
+
+	memset(&ifra, 0, sizeof(ifra));
+	strlcpy(ifra.ifra_name, ifnam, sizeof(ifra.ifra_name));
+	memcpy(&ifra.ifra_addr, sa, sa->sa_len);
+	memcpy(&ifra.ifra_mask, mask, mask->sa_len);
+
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (fd < 0)
+		return (-1);
 
-	error = ifaddr_ad(SIOCAIFADDR, ifnam, sa, mask);
+	error = ioctl(fd, SIOCAIFADDR, (caddr_t)&ifra);
 	if (error != 0)
 		warn("ioctl SIOCAIFADDR");
+
+	close(fd);
 	return (error);
 }
 
 static int
-ifaddr_del(const char *ifnam, struct sockaddr *sa, struct sockaddr *mask)
+ifaddr_del(const char *ifnam, struct sockaddr *sa)
 {
-	int error;
+	struct ifreq ifr;
+	int error, fd;
+
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, ifnam, sizeof(ifr.ifr_name));
+	memcpy(&ifr.ifr_addr, sa, sa->sa_len);
 
-	error = ifaddr_ad(SIOCDIFADDR, ifnam, sa, mask);
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (fd < 0)
+		return (-1);
+
+	error = ioctl(fd, SIOCDIFADDR, (caddr_t)&ifr);
 	if (error != 0)
 		warn("ioctl SIOCDIFADDR");
+
+	close(fd);
 	return (error);
 }
 
@@ -897,8 +899,7 @@ at_async_owandata(void *arg, const char *resp)
 	if (ctx->flags & IPASSIGNED) {
 		memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
 		    sizeof(sin.sin_addr.s_addr));
-		ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin,
-		    (struct sockaddr *)&mask);
+		ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin);
 	}
 	inet_pton(AF_INET, ip, &ctx->ip.s_addr);
 	memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
@@ -1258,7 +1259,7 @@ do_connect(struct ctx *ctx, const char *tty)
 static void
 do_disconnect(struct ctx *ctx)
 {
-	struct sockaddr_in sin, mask;
+	struct sockaddr_in sin;
 
 	/* Disconnect */
 	at_cmd(ctx, NULL, NULL, NULL, "AT_OWANCALL=%d,0,0\r\n",
@@ -1267,14 +1268,11 @@ do_disconnect(struct ctx *ctx)
 
 	/* Remove ip-address from interface */
 	if (ctx->flags & IPASSIGNED) {
-		sin.sin_len = mask.sin_len = sizeof(struct sockaddr_in);
-		memset(&mask.sin_addr.s_addr, 0xff,
-		    sizeof(mask.sin_addr.s_addr));
-		sin.sin_family = mask.sin_family = AF_INET;
+		sin.sin_len = sizeof(struct sockaddr_in);
+		sin.sin_family = AF_INET;
 		memcpy(&sin.sin_addr.s_addr, &ctx->ip.s_addr,
 		    sizeof(sin.sin_addr.s_addr));
-		ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin,
-		    (struct sockaddr *)&mask);
+		ifaddr_del(ctx->ifnam, (struct sockaddr *)&sin);
 
 		if_ifdown(ctx->ifnam);
 		ctx->flags &= ~IPASSIGNED;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202407221459.46MExskr033058>