git: a72b78905a3d - main - ifconfig: simplify carp vhid setup.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Sat, 20 May 2023 09:49:40 UTC
The branch main has been updated by melifaro:

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

commit a72b78905a3df924875a2b18e4199f64f1ab432f
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-05-19 10:18:43 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-05-20 09:49:15 +0000

    ifconfig: simplify carp vhid setup.
    
    Currently carp implementation peeks into the opaque 'afp->af_addreq'
     buffer, assumes it knows the af-specific layout and assigns vhid
     directly.
    Simplify the code and remove abstraction leak by introducing per-afp
     callback for setting vhid.
    This change is a pre-requisite to set addresses via Netlink,
     as Netlink implementiation uses different structure layout.
    
    Reviewed by:    kp
    Differential Revision: https://reviews.freebsd.org/D40160
    MFC after:      2 weeks
---
 sbin/ifconfig/af_inet.c  |  8 ++++++++
 sbin/ifconfig/af_inet6.c |  7 +++++++
 sbin/ifconfig/carp.c     | 26 ++------------------------
 sbin/ifconfig/ifconfig.h |  2 ++
 4 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/sbin/ifconfig/af_inet.c b/sbin/ifconfig/af_inet.c
index 4569c9c362e9..cb030dbc711b 100644
--- a/sbin/ifconfig/af_inet.c
+++ b/sbin/ifconfig/af_inet.c
@@ -285,6 +285,13 @@ in_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
 		warn("SIOCSIFPHYADDR");
 }
 
+static void
+in_set_vhid(int vhid)
+{
+	in_addreq.ifra_vhid = vhid;
+}
+
+
 static struct afswtch af_inet = {
 	.af_name	= "inet",
 	.af_af		= AF_INET,
@@ -297,6 +304,7 @@ static struct afswtch af_inet = {
 	.af_postproc	= in_postproc,
 	.af_status_tunnel = in_status_tunnel,
 	.af_settunnel	= in_set_tunnel,
+	.af_setvhid	= in_set_vhid,
 	.af_difaddr	= SIOCDIFADDR,
 	.af_aifaddr	= SIOCAIFADDR,
 	.af_ridreq	= &in_ridreq,
diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c
index 0f4e0e75e44a..1bb08c8a6a5a 100644
--- a/sbin/ifconfig/af_inet6.c
+++ b/sbin/ifconfig/af_inet6.c
@@ -555,6 +555,12 @@ in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
 		warn("SIOCSIFPHYADDR_IN6");
 }
 
+static void
+in6_set_vhid(int vhid)
+{
+	in6_addreq.ifra_vhid = vhid;
+}
+
 static struct cmd inet6_cmds[] = {
 	DEF_CMD_ARG("prefixlen",			setifprefixlen),
 	DEF_CMD("anycast",	IN6_IFF_ANYCAST,	setip6flags),
@@ -605,6 +611,7 @@ static struct afswtch af_inet6 = {
 	.af_postproc	= in6_postproc,
 	.af_status_tunnel = in6_status_tunnel,
 	.af_settunnel	= in6_set_tunnel,
+	.af_setvhid	= in6_set_vhid,
 	.af_difaddr	= SIOCDIFADDR_IN6,
 	.af_aifaddr	= SIOCAIFADDR_IN6,
 	.af_ridreq	= &in6_addreq,
diff --git a/sbin/ifconfig/carp.c b/sbin/ifconfig/carp.c
index 2a2d8ce407ab..cc38eed4cb4b 100644
--- a/sbin/ifconfig/carp.c
+++ b/sbin/ifconfig/carp.c
@@ -111,31 +111,9 @@ setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
 		errx(1, "vhid must be greater than 0 and less than %u",
 		    CARP_MAXVHID);
 
-	switch (afp->af_af) {
-#ifdef INET
-	case AF_INET:
-	    {
-		struct in_aliasreq *ifra;
-
-		ifra = (struct in_aliasreq *)afp->af_addreq;
-		ifra->ifra_vhid = carpr_vhid;
-		break;
-	    }
-#endif
-#ifdef INET6
-	case AF_INET6:
-	    {
-		struct in6_aliasreq *ifra;
-
-		ifra = (struct in6_aliasreq *)afp->af_addreq;
-		ifra->ifra_vhid = carpr_vhid;
-		break;
-	    }
-#endif
-	default:
+	if (afp->af_setvhid == NULL)
 		errx(1, "%s doesn't support carp(4)", afp->af_name);
-	}
-
+	afp->af_setvhid(carpr_vhid);
 	callback_register(setcarp_callback, NULL);
 }
 
diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h
index 70a2c92199b6..7b2b88a4dfac 100644
--- a/sbin/ifconfig/ifconfig.h
+++ b/sbin/ifconfig/ifconfig.h
@@ -161,6 +161,7 @@ struct io_handler {
 	struct snl_state	*ss;	/* NETLINK_ROUTE snl(3) socket */
 };
 
+typedef void af_setvhid_f(int vhid);
 typedef	void af_status_nl_f(struct ifconfig_args *args, struct io_handler *h,
     if_link_t *link, if_addr_t *ifa);
 
@@ -188,6 +189,7 @@ struct afswtch {
 	void		(*af_getprefix)(const char *, int);
 	void		(*af_postproc)(int s, const struct afswtch *,
 			    int newaddr, int ifflags);
+	af_setvhid_f	*af_setvhid;	/* Set CARP vhid for an address */
 	u_long		af_difaddr;	/* set dst if address ioctl */
 	u_long		af_aifaddr;	/* set if address ioctl */
 	void		*af_ridreq;	/* */