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
This commit is contained in:
Andrey V. Elsukov 2020-10-14 09:22:54 +00:00
parent 6cc4520b0a
commit 6952c3e1ac
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=366695
3 changed files with 69 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 */