From 917a85eb255679322675048bed154c1f179f606a Mon Sep 17 00:00:00 2001 From: ae Date: Wed, 14 Oct 2020 09:22:54 +0000 Subject: [PATCH] Implement SIOCGIFALIAS. It is lightweight way to check if an IPv4 address exists. Submitted by: Roy Marples Reviewed by: gnn, melifaro MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D26636 --- share/man/man4/netintro.4 | 9 +++++- sys/netinet/in.c | 60 +++++++++++++++++++++++++++++++++++++++ sys/sys/sockio.h | 1 + 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/share/man/man4/netintro.4 b/share/man/man4/netintro.4 index 574684d1cf1..6066c9e7417 100644 --- a/share/man/man4/netintro.4 +++ b/share/man/man4/netintro.4 @@ -28,7 +28,7 @@ .\" @(#)netintro.4 8.2 (Berkeley) 11/30/93 .\" $FreeBSD$ .\" -.Dd January 26, 2012 +.Dd October 14, 2020 .Dt NETINTRO 4 .Os .Sh NAME @@ -349,6 +349,13 @@ multiple masks or destination addresses, and also adopts the convention that specification of the default address means to delete the first address for the interface belonging to the address family in which the original socket was opened. +.It Dv SIOCGIFALIAS +This request provides means to get additional addresses +together with netmask and broadcast/destination from an +interface. +It also uses the +.Vt ifaliasreq +structure. .It Dv SIOCGIFCONF Get interface configuration list. This request takes an diff --git a/sys/netinet/in.c b/sys/netinet/in.c index 31a66e277b2..dd2286195c4 100644 --- a/sys/netinet/in.c +++ b/sys/netinet/in.c @@ -72,6 +72,7 @@ __FBSDID("$FreeBSD$"); static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *); static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *); +static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *); static void in_socktrim(struct sockaddr_in *); static void in_purgemaddrs(struct ifnet *); @@ -237,6 +238,11 @@ in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, case SIOCGIFDSTADDR: case SIOCGIFNETMASK: break; + case SIOCGIFALIAS: + sx_xlock(&in_control_sx); + error = in_gifaddr_ioctl(cmd, data, ifp, td); + sx_xunlock(&in_control_sx); + return (error); case SIOCDIFADDR: sx_xlock(&in_control_sx); error = in_difaddr_ioctl(cmd, data, ifp, td); @@ -649,6 +655,60 @@ in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) return (0); } +static int +in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) +{ + struct in_aliasreq *ifra = (struct in_aliasreq *)data; + const struct sockaddr_in *addr = &ifra->ifra_addr; + struct epoch_tracker et; + struct ifaddr *ifa; + struct in_ifaddr *ia; + + /* + * ifra_addr must be present and be of INET family. + */ + if (addr->sin_len != sizeof(struct sockaddr_in) || + addr->sin_family != AF_INET) + return (EINVAL); + + /* + * See whether address exist. + */ + ia = NULL; + NET_EPOCH_ENTER(et); + CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { + struct in_ifaddr *it; + + if (ifa->ifa_addr->sa_family != AF_INET) + continue; + + it = (struct in_ifaddr *)ifa; + if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && + prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) { + ia = it; + break; + } + } + if (ia == NULL) { + NET_EPOCH_EXIT(et); + return (EADDRNOTAVAIL); + } + + ifra->ifra_mask = ia->ia_sockmask; + if ((ifp->if_flags & IFF_POINTOPOINT) && + ia->ia_dstaddr.sin_family == AF_INET) + ifra->ifra_dstaddr = ia->ia_dstaddr; + else if ((ifp->if_flags & IFF_BROADCAST) && + ia->ia_broadaddr.sin_family == AF_INET) + ifra->ifra_broadaddr = ia->ia_broadaddr; + else + memset(&ifra->ifra_broadaddr, 0, + sizeof(ifra->ifra_broadaddr)); + + NET_EPOCH_EXIT(et); + return (0); +} + #define rtinitflags(x) \ ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ ? RTF_HOST : 0) diff --git a/sys/sys/sockio.h b/sys/sys/sockio.h index 03502d770c5..93b8af28e17 100644 --- a/sys/sys/sockio.h +++ b/sys/sys/sockio.h @@ -84,6 +84,7 @@ #define SIOCGIFDESCR _IOWR('i', 42, struct ifreq) /* get ifnet descr */ #define SIOCAIFADDR _IOW('i', 43, struct ifaliasreq)/* add/chg IF alias */ #define SIOCGIFDATA _IOW('i', 44, struct ifreq) /* get if_data */ +#define SIOCGIFALIAS _IOWR('i', 45, struct ifaliasreq)/* get IF alias */ #define SIOCADDMULTI _IOW('i', 49, struct ifreq) /* add m'cast addr */ #define SIOCDELMULTI _IOW('i', 50, struct ifreq) /* del m'cast addr */ -- 2.45.2