Subject: Re: ifconfig and aliases
To: Christian E. Hopps <chopps@merit.edu>
From: Jun Xie <jun@qnx.com>
List: tech-net
Date: 07/21/1999 17:22:32
> Thor Lancelot Simon <tls@rek.tjls.com> writes:
> 
> > On Tue, Jul 20, 1999 at 12:39:28AM +0200, Jens A Nilsson wrote:
> > > Darren Reed <darrenr@reed.wattle.id.au>,wrote on Jul 20, 1999 at 08:24 +1000: 
> > > > In some email I received from Jens A Nilsson, sie wrote:
> > > > > 
> > > > > I don't understand "ifconfig lo0 127.0.0.1 netmask 0xffff0000"
> > > > > changes the netmask correctly.
> > > > 
> > > > Why should changing the netmask require you to specify the IP# ?
> > > 
> > > Because the netmask belongs to that IP address, but if you don't
> > > expect an interface to have more than one IP address I can see your
> > > point. Ifconfig should not discard a command such as
> > > "ifconfig lo0 netmask 0xffff0000", it should either complain or
> > > change the netmask of the primary IP address.
> > 
> > There is no "primary IP address".  The IP addresses are stored in a hash
> > table; there's some old code that still thinks there's a "primary IP address"
> > but it's deceived by a hack.

I think there is still "primary IP address". The IP addresses are stored
in tail queues. Do you think ifconfig is old code? How does it decide
an IP address of an interface is alias or not when it implements the
-A command line option?

> So we have no idea what IP gets written into a packet sent out a
> specific interface with more then 1 IP anymore?
> 
> Thats whats I've been calling the primary-ip in my head at least.  I
> understand the semantics of this weren't well documented; however, I
> believe it used to be a deterministic thing.
> 
> If this isn't the case maybe we should add it back in (and better
> define primary IP in the process).

I think it's still a deterministic thing.
If a local IP address is not yet specified for an outgoing packet,
the primary IP address is used, which is the first non-loopback IP
address on the tail queue (headed by in_ifaddr). Check out
in_selectsrc() in src/sys/netinet/in_pcb.c.

ifconfig thinks an IP address is a primary IP address (non-alias
address) of an interface if it's the same as what's returned from
ioctl(SIOCGIFADDR). ioctl(SIOCGIFADDR) gets the address using the
following macro:

/*
 * Macro for finding an internet address structure (in_ifaddr) corresponding
 * to a given interface (ifnet structure).
 */
#define IFP_TO_IA(ifp, ia) \
        /* struct ifnet *ifp; */ \
        /* struct in_ifaddr *ia; */ \
{ \
        register struct ifaddr *ifa; \
\
        for (ifa = (ifp)->if_addrlist.tqh_first; \
            ifa != NULL && ifa->ifa_addr->sa_family != AF_INET; \
            ifa = ifa->ifa_list.tqe_next) \
                continue; \
        (ia) = ifatoia(ifa); \
}

So it returns the first inet address on the ifaddr list of the
interface.

Jun Xie