Primary and Secondary IP Addresses
We saw in the section "Primary and Secondary Addresses" in Chapter 30 that an IP address can be configured as primary or secondary on a device. The kernel often needs to browse all the addresses configured on a device to find one that matches a given condition. Let's see how the two types of addresses are distinguished and how addresses are browsed.
Secondary IPv4 addresses are tagged with the IFA_F_SECONDARY flag in their in_ifaddr data
structures (see Chapter 19). Because there are
only two configurations—primary and secondary—there is no need for an IFA_F_PRIMARY flag: if an address is not secondary, it is
considered primary.
The kernel provides macros in include/linux/inetdevice.h that make it easier to browse interfaces meeting specific criteria. For each criterion selected, there are usually two macros that are used to bracket a loop: the programmer places the code to process a single address in a block that is started by one macro and terminated by another. The effect is to run a loop applying the code to each address meeting selected criteria.
Here is an example of the macros in use:
for_ifa {
do something with ifa
} endfor_ifaThe for_ifa macro starts a loop with the variable
ifa to represent each address selected. The code
between the macros does not need to be placed in brackets, but it usually is, to make
variables such as ifa local and usable only within the
loop.
A few such macros include:
-
for_ifa -
endfor_ifa Given a device, these two ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access