Author: ae
Date: Wed Oct 14 09:22:54 2020
New Revision: 366695
URL: https://svnweb.freebsd.org/changeset/base/366695

Log:
  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

Modified:
  head/share/man/man4/netintro.4
  head/sys/netinet/in.c
  head/sys/sys/sockio.h

Modified: head/share/man/man4/netintro.4
==============================================================================
--- head/share/man/man4/netintro.4      Wed Oct 14 08:04:39 2020        
(r366694)
+++ head/share/man/man4/netintro.4      Wed Oct 14 09:22:54 2020        
(r366695)
@@ -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 adop
 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

Modified: head/sys/netinet/in.c
==============================================================================
--- head/sys/netinet/in.c       Wed Oct 14 08:04:39 2020        (r366694)
+++ head/sys/netinet/in.c       Wed Oct 14 09:22:54 2020        (r366695)
@@ -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
        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);
@@ -646,6 +652,60 @@ in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifne
            IFADDR_EVENT_DEL);
        ifa_free(&ia->ia_ifa);          /* in_ifaddrhead */
 
+       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);
 }
 

Modified: head/sys/sys/sockio.h
==============================================================================
--- head/sys/sys/sockio.h       Wed Oct 14 08:04:39 2020        (r366694)
+++ head/sys/sys/sockio.h       Wed Oct 14 09:22:54 2020        (r366695)
@@ -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 */
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to