8000 minix/minix/net/lwip/ifaddr.c at master · gotoweb/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2224 lines (1899 loc) · 72.2 KB

ifaddr.c

File metadata and controls

2224 lines (1899 loc) · 72.2 KB
/* LWIP service - ifaddr.c - network interface address management */
/*
* This module is an exception to the regular source organization of this
* service, in that it manages part of another module's data structures, namely
* ifdev. As such, it should be seen as logically part of ifdev. It is
* separated only to keep the source code more manageable. Still, this module
* may use direct access only on the address-related fields of the ifdev
* structure, so that those one day may be move into an ifaddr-specific
* substructure within ifdev.
*/
/*
* We manage three types of addresses here: IPv4 addresses (ifaddr_v4),
* IPv6 addresses (ifaddr_v6), and link-layer a.k.a. MAC addresses (ifaddr_dl).
*
* Managing IPv4 addresses is easy. lwIP supports only one IPv4 address per
* netif. While it would be possible to construct a model where one ifdev
* consists of multiple netifs (with one IPv4 address each), we not support
* this--mostly because it is a pain to keep state synchronized between the
* netifs in that case. Such support can still be added later; the IPv4 API
* exposed from here does support multiple IPv4 addresses already just in case,
* as does much of the code using the API.
*
* For IPv4 addresses we maintain only one extra piece of information here,
* which is whether an IPv4 address has been set at all. This is because for
* our userland (DHCP clients in particular), we must allow assigning 0.0.0.0
* as address to an interface. We do not use the lwIP per-netif IPv4 gateway
* field, nor the concept of a "default netif", in both cases because we
* override all (routing) decisions that would use those settings. lwIP does
* not allow a broadcast address to be set, so support for broadcast addresses
* is botched here: we disregard custom broadcast addresses given to us, and
* instead expose the broadcast address that is used within lwIP.
*
* Managing IPv6 addresses is much more complicated. First of all, even though
* lwIP supports stateless address autoconfiguration (SLAAC) as per RFC 4862,
* we disable that and instead make dhcpcd(8) responsible for all IPv6 address
* configuration. dhcpcd(8) will set addresses and routes as necessary, the
* latter of which are used in lwIP through our routing hooks (in the route
* module). This approach, which is in line with where NetBSD is headed,
* allows us to work around a number of lwIP limitations. As a result we do
* differ in this respect from NetBSD, which may switch between kernel-only,
* dhcpcd-only, and hybrid autoconfiguration, mainly throught the accept_rtadv
* sysctl(7) node. Writing to this node has no real effect on MINIX 3.
*
* All IPv6 addresses have a prefix length, which is almost but not quite the
* same as IPv4's subnet masks (see RFC 5942). We must maintain the per-
* address prefix length ourselves, as lwIP supports IPv6 prefix lengths of 64
* bits only. Our dhcpcd(8)-based approach allows us to work around that.
*
* All IPv6 addresses also have a state and a lifetime, both of which are
* managed by lwIP. Unlike for IPv4, address-derived routes and routing socket
* messages are only created for addresses that are "valid", which means that
* they are in either PREFERRED or DEPRECATED state. This means that we have
* to be aware of all address state transitions between "valid" and "not
* valid", some of which (namely address duplication detection and lifetime
* expirations) are initiated by lwIP. As such, we need to keep shadow state
* for each address, and use a callback to detect whether state has changed.
*
* For understanding of this module as well as lwIP, it is important to note
* that "valid" is not the opposite of "invalid" in this context: "not valid"
* includes the address states INVALID, DUPLICATED, and TENTATIVE, while
* "invalid"/INVALID simply means that the address slot is free.
*
* Each IPv6 address also has associated flags. We support an AUTOCONF flag
* which indicates that no subnet route should be added for the address; on
* MINIX 3, dhcpcd(8) is modified to pass in that flag when appropriate, thus
* solving a problem that NetBSD suffers from, namely that it does not know
* whether a userland-given route is static (implying a subnet) or auto-
* configured (implying no subnet, again as per RFC 5942), leading to it doing
* the wrong thing in dhcpcd-only autoconfiguration mode. The TEMPORARY flag,
* for privacy addresses (RFC 4941) should be the same as on NetBSD; it is
* currently used only in source address selection (RFC 6724). We override
* lwIP's IPv6 source address selection algorithm to include support for not
* just this flag, but also label and proper longest-common-prefix comparisons.
* Finally, there is an HWBASED flag to make sure that when the link-layer
* address is changed, the IPv6 link-local address is changed accordingly only
* if the previous link-local address was also autogenerated from a link-layer
* address and not set manually by userland.
*
* Finally, we support multiple link-layer addresses per interface, but only
* because NetBSD's ifconfig(8) uses an API that expects such multi-address
* support. At any time, only one of the addresses is marked as "active",
* which means it is used as MAC address in outgoing packets. We support only
* one MAC address per device driver, so the support for additional, inactive
* link-layer addresses is there exclusively for ifconfig(8) interoperability.
*
* All interfaces, including those that do not have MAC addresses at all (e.g.,
* loopback interfaces), do have one link-layer address. This is expected in
* particular by getifaddrs(3), which only recognizes interfaces that have a
* link-layer address.
*
* Many features are still missing here, especially for IP addresses. For
* example, we do not yet support destination addresses at all yet, simply
* because there is no interface type that uses them. For IPv6, more work is
* to be done to support proper netif status transitions versus address states,
* fallout from address duplication, and various ND6_IFF_ flags.
*/
#include "lwip.h"
#include "rtsock.h"
#include "route.h"
#include "lwip/etharp.h"
#include <netinet6/in6_var.h>
#include <netinet6/nd6.h>
/*
* Routing flags for local address and local network routing entries. This
* may later have to be refined, for example in order not to set RTF_CLONING
* for routes on interfaces that do not have link-layer addressing.
*
* IMPORTANT: as of NetBSD 8, RTF_CLONING has been renamed to RTF_CONNECTED.
*/
#define IFADDR_HOST_RTFLAGS (RTF_UP | RTF_HOST | RTF_LOCAL)
#define IFADDR_NET_RTFLAGS (RTF_UP | RTF_CLONING)
/* Address-related sysctl(7) settings. */
int ifaddr_auto_linklocal = 1; /* different from NetBSD, see its usage */
int ifaddr_accept_rtadv = 0; /* settable but completely disregarded */
/*
* Initialize the local address administration for an interface that is in the
* process of being created.
*/
void
ifaddr_init(struct ifdev * ifdev)
{
unsigned int i;
ifdev->ifdev_v4set = FALSE;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
ifdev->ifdev_v6state[i] = IP6_ADDR_INVALID;
for (i = 0; i < __arraycount(ifdev->ifdev_hwlist); i++)
ifdev->ifdev_hwlist[i].ifhwa_flags = 0;
}
/*
* Find an IPv4 address locally assigned to a interface. The IPv4 address is
* given as 'addr'. The interface is given as 'ifdev'. On success, return OK,
* with the IPv4 address number stored in 'num'. On failure, return a negative
* error code.
*/
int
ifaddr_v4_find(struct ifdev * ifdev, const struct sockaddr_in * addr,
ifaddr_v4_num_t * num)
{
ip_addr_t ipaddr;
int r;
if ((r = addr_get_inet((const struct sockaddr *)addr, sizeof(*addr),
IPADDR_TYPE_V4, &ipaddr, TRUE /*kame*/, NULL /*port*/)) != OK)
return r;
if (!ifdev->ifdev_v4set ||
!ip_addr_cmp(netif_ip_addr4(ifdev_get_netif(ifdev)), &ipaddr))
return EADDRNOTAVAIL;
*num = 0;
return OK;
}
/*
* Enumerate IPv4 addresses locally assigned to the given interface 'ifdev'.
* The caller should set 'nump' to 0 initially, and increase it by one between
* a successful call and the next enumeration call. Return TRUE on success,
* meaning that starting from the given value of 'nump' there is at least one
* IPv4 address, of which the number is stored in 'nump' on return. Return
* FALSE if there are no more IPv4 addresses locally assigned to the interface.
*/
int
ifaddr_v4_enum(struct ifdev * ifdev, ifaddr_v4_num_t * num)
{
/*
* For now, we support only up to one IPv4 address per interface.
* set if we are to return it.
*/
return (*num == 0 && ifdev->ifdev_v4set);
}
/*
* Obtain information about the IPv4 address 'num' assigned to the interface
* 'ifdev'. On success, return OK, with the IPv4 address stored in 'addr', the
* network mask stored in 'mask', the broadcast stored in 'bcast', and the
* destination address stored in 'dest'. Each of these pointers may be NULL.
* The interface may not have a broadcast and/or destination address; in that
* case, their corresponding structures are not filled in at all, and thus must
* be preinitialized by the caller to a default state. The reason for not
* zeroing them is that some callers use the same buffer for both. On failure,
* return a negative error code.
*/
int
ifaddr_v4_get(struct ifdev * ifdev, ifaddr_v4_num_t num,
struct sockaddr_in * addr, struct sockaddr_in * mask,
struct sockaddr_in * bcast, struct sockaddr_in * dest)
{
const ip_addr_t *ipaddr, *netmask;
struct netif *netif;
ip_addr_t broad;
socklen_t addr_len;
if (!ifaddr_v4_enum(ifdev, &num))
return EADDRNOTAVAIL;
netif = ifdev_get_netif(ifdev);
if (addr != NULL) {
addr_len = sizeof(*addr);
addr_put_inet((struct sockaddr *)addr, &addr_len,
netif_ip_addr4(netif), TRUE /*kame*/, 0 /*port*/);
}
if (mask != NULL) {
addr_len = sizeof(*mask);
/*
* Do not bother using addr_put_netmask() here, as we would
* then first have to compute the prefix length..
*/
addr_put_inet((struct sockaddr *)mask, &addr_len,
netif_ip_netmask4(netif), TRUE /*kame*/, 0 /*port*/);
}
if (bcast != NULL) {
if (netif->flags & NETIF_FLAG_BROADCAST) {
/* Fake a broadcast address. */
ipaddr = netif_ip_addr4(netif);
netmask = netif_ip_netmask4(netif);
ip_addr_set_ip4_u32(&broad,
ip_addr_get_ip4_u32(ipaddr) |
~ip_addr_get_ip4_u32(netmask));
addr_len = sizeof(*bcast);
addr_put_inet((struct sockaddr *)bcast, &addr_len,
&broad, TRUE /*kame*/, 0 /*port*/);
} else {
bcast->sin_len = 0;
bcast->sin_family = AF_UNSPEC;
}
}
if (dest != NULL) {
/* TODO: dest */
dest->sin_len = 0;
dest->sin_family = AF_UNSPEC;
}
return OK;
}
/*
* Obtain NetBSD-style state flags (IN_IFF_) for the given local IPv4 address.
* The given number must identify an existing address. Return the flags.
*/
int
ifaddr_v4_get_flags(struct ifdev * ifdev, ifaddr_v4_num_t num)
{
/* IPv4 per-address flags are not supported yet. */
return 0;
}
/*
* Determine whether there should be a local subnet route for the given
* assigned IPv4 address, and if so, compute the subnet mask to add. Return
* TRUE if a local subnet route should be added, and return the network base
* address in 'netbase' and the number of prefix bits in 'prefixp'. Return
* FALSE if no subnet route should be added for the assigned address.
*/
static unsigned int
ifaddr_v4_netroute(struct ifdev * ifdev, ifaddr_v4_num_t num,
ip_addr_t * netbase, unsigned int * prefixp)
{
const ip_addr_t *ipaddr, *netmask;
unsigned int prefix;
uint32_t val;
/* Do not add subnet masks for loopback interfaces. */
if (ifdev_is_loopback(ifdev))
return FALSE;
assert(num == 0);
assert(ifdev->ifdev_v4set);
ipaddr = netif_ip_addr4(ifdev_get_netif(ifdev));
netmask = netif_ip_netmask4(ifdev_get_netif(ifdev));
/*
* If the subnet is a /32, skip adding a local host route: not only
* would it not be useful, it would fail anyway because we currently do
* not support adding a host-type route and a full-width net-type route
* for the same IP address.
*/
if (ip_addr_get_ip4_u32(netmask) == PP_HTONL(0xffffffffUL))
return FALSE;
/* Compute the network base address. */
ip_addr_set_ip4_u32(netbase,
ip_addr_get_ip4_u32(ipaddr) & ip_addr_get_ip4_u32(netmask));
/* Find the number of prefix bits of the netmask. TODO: improve.. */
val = ntohl(ip_addr_get_ip4_u32(netmask));
for (prefix = 0; prefix < IP4_BITS; prefix++)
if (!(val & (1 << (IP4_BITS - prefix - 1))))
break;
*prefixp = prefix;
return TRUE;
}
/*
* A local IPv4 address has been added to an interface. The interface is given
* as 'ifdev', and the number of the just-added IPv4 address is given as 'num'.
* Generate a routing socket message and add local routes as appropriate.
*/
static void
ifaddr_v4_added(struct ifdev * ifdev, ifaddr_v4_num_t num)
{
const ip_addr_t *ipaddr;
ip_addr_t netbase;
unsigned int prefix;
assert(num == 0);
assert(ifdev->ifdev_v4set);
/* Report the addition of the interface address. */
rtsock_msg_addr_v4(ifdev, RTM_NEWADDR, num);
/*
* Add the local host route. This will always succeed: for addition,
* we just checked with route_can_add(); when updating, we first remove
* the exact same route. For now, we forbid users from messing with
* RTF_LOCAL routes directly, since nothing good (and a whole lot of
* bad) can come out of that, so the routes will not change under us.
*
* Why are we not using lo0 for this route, like the BSDs do? Because
* that approach is not compatible with link-local addresses. Instead,
* we intercept outgoing traffic to the local address, and redirect it
* over lo0, bypassing routing. If we did not do this, we would never
* know the originally intended zone of the outgoing packet. As an
* intended side effect, the traffic does show up on lo0 with BPF, just
* like on BSDs. Similarly, we do not need to set a gateway here.
*
* We currently do not use the routing tables for lookups on local
* addresses - see ifaddr_v6_map() as to why. If we ever do, that adds
* another reason that the interface associated with the route must be
* the interface that owns the address (and not, say, lo0).
*/
ipaddr = netif_ip_addr4(ifdev_get_netif(ifdev));
(void)route_add(ipaddr, IP4_BITS, NULL /*gateway*/, ifdev,
IFADDR_HOST_RTFLAGS, NULL /*rtr*/);
/*
* Add the local network route, if the rules say that we should. Even
* then, adding the route may fail for various reasons, but this route
* is not essential and so we ignore failures here.
*/
if (ifaddr_v4_netroute(ifdev, num, &netbase, &prefix))
(void)route_add(&netbase, prefix, NULL /*gateway*/, ifdev,
IFADDR_NET_RTFLAGS, NULL /*rtr*/);
}
/*
* A particular local IPv4 address is being deleted. See if there is another
* local IPv4 address assigned to another interface that should have the same
* local subnet route (but didn't, as such duplicate routes can obviously not
* be added), and if so, readd the route for that other address.
*/
static void
ifaddr_v4_dupcheck(struct ifdev * oifdev, const ip_addr_t * onetbase,
unsigned int oprefix)
{
struct ifdev *ifdev;
ip_addr_t netbase;
unsigned int prefix;
for (ifdev = NULL; (ifdev = ifdev_enum(ifdev)) != NULL; ) {
if (ifdev == oifdev || !ifdev->ifdev_v4set)
continue;
if (ifaddr_v4_netroute(ifdev, (ifaddr_v4_num_t)0, &netbase,
&prefix) && prefix == oprefix &&
ip_addr_cmp(&netbase, onetbase)) {
(void)route_add(&netbase, prefix, NULL /*gateway*/,
ifdev, IFADDR_NET_RTFLAGS, NULL /*rtr*/);
return;
}
}
}
/*
* A local IPv4 address is about to be deleted from an interface, or the
* interface itself is about to be destroyed. Generate a routing socket
* message about this and delete local routes as appropriate. The interface is
* given as 'ifdev', and the number of the IPv4 address that is about to be
* deleted is given as 'num'.
*/
static void
ifaddr_v4_deleted(struct ifdev * ifdev, ifaddr_v4_num_t num)
{
struct route_entry *route;
ip_addr_t netbase;
unsigned int prefix;
assert(num == 0);
assert(ifdev->ifdev_v4set);
/* Delete the local network route, if we tried adding it at all. */
if (ifaddr_v4_netroute(ifdev, num, &netbase, &prefix) &&
(route = route_find(&netbase, prefix,
FALSE /*is_host*/)) != NULL &&
route_get_flags(route) == IFADDR_NET_RTFLAGS) {
route_delete(route, NULL /*rtr*/);
/*
* Readd the local network route for another interface, if that
* interface has a local address on the very same network.
*/
ifaddr_v4_dupcheck(ifdev, &netbase, prefix);
}
/* Delete the local host route. */
if ((route = route_find(netif_ip_addr4(ifdev_get_netif(ifdev)),
IP4_BITS, TRUE /*is_host*/)) != NULL)
route_delete(route, NULL /*rtr*/);
/* Report the deletion of the interface address. */
rtsock_msg_addr_v4(ifdev, RTM_DELADDR, num);
}
/*
* Add or update an IPv4 address on an interface. The interface is given as
* 'ifdev'. The address to add or update is pointed to by 'addr', which must
* always be a pointer to a valid address. For DHCP clients it must be
* possible to add the 'any' address (0.0.0.0). The network mask, broadcast
* address, and destination address parameters 'mask', 'bcast', and 'dest'
* (respectively) may be NULL pointers or pointers to AF_UNSPEC addresses, and
* will be disregarded if they are. If 'mask' and/or 'bcast' are NULL when
* adding an address, default values will be computed for them. The 'flags'
* field may contain NetBSD-style address flags (IN_IFF_). Return OK if the
* address was successfully added or updated, or a negative error code if not.
*/
int
ifaddr_v4_add(struct ifdev * ifdev, const struct sockaddr_in * addr,
const struct sockaddr_in * mask, const struct sockaddr_in * bcast,
const struct sockaddr_in * dest, int flags)
{
ip_addr_t ipaddr, netmask, broad;
ip4_addr_t ip4zero;
struct netif *netif;
unsigned int dummy;
uint32_t val;
int r;
assert(addr != NULL);
if ((r = addr_get_inet((const struct sockaddr *)addr, sizeof(*addr),
IPADDR_TYPE_V4, &ipaddr, TRUE /*kame*/, NULL /*port*/)) != OK)
return r;
/* Forbid multicast (class D) and experimental (class E) addresses. */
val = ntohl(ip_addr_get_ip4_u32(&ipaddr));
if (ip_addr_ismulticast(&ipaddr) || IP_EXPERIMENTAL(val))
return EINVAL;
if (mask != NULL && mask->sin_family != AF_UNSPEC) {
if ((r = addr_get_netmask((const struct sockaddr *)mask,
sizeof(*mask), IPADDR_TYPE_V4, &dummy, &netmask)) != OK)
return r;
} else {
/*
* Generate a netmask based on IP class. Old, obsolete stuff,
* but we can't have no netmask.
*/
if (IN_CLASSA(val))
ip_addr_set_ip4_u32(&netmask, PP_HTONL(IN_CLASSA_NET));
else if (IN_CLASSB(val))
ip_addr_set_ip4_u32(&netmask, PP_HTONL(IN_CLASSB_NET));
else if (IN_CLASSC(val))
ip_addr_set_ip4_u32(&netmask, PP_HTONL(IN_CLASSC_NET));
else /* should not trigger */
ip_addr_set_ip4_u32(&netmask, PP_HTONL(IN_CLASSD_NET));
}
if (bcast != NULL && bcast->sin_family != AF_UNSPEC) {
if ((r = addr_get_inet((const struct sockaddr *)bcast,
sizeof(*bcast), IPADDR_TYPE_V4, &broad, TRUE /*kame*/,
NULL /*port*/)) != OK)
return r;
/*
* lwIP does not allow setting the broadcast address, so we
* must ensure that the given address is what lwIP uses anyway.
* No need to perform byte order swaps here.
*/
if (ip_addr_get_ip4_u32(&broad) !=
(ip_addr_get_ip4_u32(&ipaddr) |
~ip_addr_get_ip4_u32(&netmask)))
return EINVAL;
}
/* TODO: dest (note: may be NULL) */
/*
* We currently do not support any IPv4 address flags. Even though
* supporting them would make maintaining dhcpcd(8) easier, lwIP does
* not offers the means to implement them properly.
*/
if (flags != 0)
return EINVAL;
netif = ifdev_get_netif(ifdev);
/* Should we add a new address, or update an existing one? */
if (!ifdev->ifdev_v4set ||
!ip_addr_cmp(netif_ip_addr4(netif), &ipaddr)) {
/*
* Add a new address. lwIP supports only one IPv4 address per
* netif.
*/
if (ifdev->ifdev_v4set)
return ENOBUFS; /* TODO: a better error code */
/*
* It must be possible to add the address to the routing table,
* so make sure that we can add such a route later on. The
* error code should be accurate for most real-world cases.
*/
if (!route_can_add(&ipaddr, IP4_BITS, TRUE /*is_host*/))
return EEXIST;
ip4_addr_set_zero(&ip4zero);
netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask),
&ip4zero);
ifdev->ifdev_v4set = TRUE;
} else {
/*
* Update an existing address. First report the address as
* deleted. Do not actually delete the address in netif,
* because that would cause problems with its changing IP
* addresses on existing sockets.
*/
ifaddr_v4_deleted(ifdev, (ifaddr_v4_num_t)0);
/* Update the one part that may have actually changed. */
netif_set_netmask(netif, ip_2_ip4(&netmask));
}
/* In both cases, we now need to report the address as added. */
ifaddr_v4_added(ifdev, (ifaddr_v4_num_t)0);
return OK;
}
/*
* Delete an IPv4 address from an interface. The given address number 'num'
* must have been obtained from ifaddr_v4_find() or ifaddr_v4_enum() on the
* same interface just before. This function always succeeds.
*/
void
ifaddr_v4_del(struct ifdev * ifdev, ifaddr_v4_num_t num)
{
ip4_addr_t ip4zero;
assert(num == 0);
assert(ifdev->ifdev_v4set);
/*
* Report the address as deleted. Always do this first, because the
* reporting requires that the address is still there.
*/
ifaddr_v4_deleted(ifdev, num);
/* Then actually delete the address. */
ip4_addr_set_zero(&ip4zero);
netif_set_addr(ifdev_get_netif(ifdev), &ip4zero, &ip4zero, &ip4zero);
ifdev->ifdev_v4set = FALSE;
}
/*
* Announce all IPv4 addresses associated with the given interface as deleted,
* Used (only) right before the interface is destroyed.
*/
void
ifaddr_v4_clear(struct ifdev * ifdev)
{
if (ifdev->ifdev_v4set)
ifaddr_v4_deleted(ifdev, (ifaddr_v4_num_t)0);
}
/*
* Return the first interface device that owns the given IPv4 address, or NULL
* if it is not a valid local IPv4 address.
*/
struct ifdev *
ifaddr_v4_map_by_addr(const ip4_addr_t * ip4addr)
{
struct ifdev *ifdev;
/*
* It would be nice to be able to do a route lookup on an RTF_LOCAL
* entry here, but we do not do this for IPv6 either - see the comment
* in ifaddr_v6_map() - and it is much less needed here, because each
* interface has at most one IPv4 address.
*/
for (ifdev = NULL; (ifdev = ifdev_enum(ifdev)) != NULL; ) {
if (ifdev->ifdev_v4set &&
ip4_addr_cmp(netif_ip4_addr(ifdev_get_netif(ifdev)),
ip4addr))
return ifdev;
}
return NULL;
}
/*
* Return the first interface device for which the given IPv4 address is on a
* configured local subnet, or NULL if no match was found.
*/
static struct ifdev *
ifaddr_v4_map_by_subnet(const ip4_addr_t * ip4addr)
{
struct ifdev *ifdev;
struct netif *netif;
uint32_t addr1, addr2, mask;
addr1 = ip4_addr_get_u32(ip4addr);
/*
* Here, we must never do a route lookup, because this routine is used
* for SO_DONTROUTE/MSG_DONTROUTE.
*/
for (ifdev = NULL; (ifdev = ifdev_enum(ifdev)) != NULL; ) {
if (!ifdev->ifdev_v4set)
continue;
netif = ifdev_get_netif(ifdev);
addr2 = ip4_addr_get_u32(netif_ip4_addr(netif));
mask = ip4_addr_get_u32(netif_ip4_netmask(netif));
if ((addr1 & mask) == (addr2 & mask))
return ifdev;
}
return NULL;
}
/*
* Return TRUE if the given local IPv6 interface address is valid (= preferred
* or deprecated), or FALSE if it is not (= tentative or duplicated). The
* address slot must be in use, that is, it must not be free (= invalid).
*/
static int
ifaddr_v6_isvalid(struct ifdev * ifdev, ifaddr_v6_num_t num)
{
int state;
state = ifdev->ifdev_v6state[num];
/* Note that 'valid' and 'invalid' are not each other's inverse! */
assert(!ip6_addr_isinvalid(state));
return ip6_addr_isvalid(state);
}
/*
* Find an IPv6 address assigned to the given interface that matches the given
* IPv6 address. Return TRUE if a match was found, with its number stored in
* 'nump'. Return FALSE if the address is not assigned to the interface.
*/
static int
ifaddr_v6_match(struct ifdev * ifdev, const ip_addr_t * ipaddr,
ifaddr_v6_num_t * nump)
{
int8_t i;
assert(IP_IS_V6(ipaddr));
i = netif_get_ip6_addr_match(ifdev_get_netif(ifdev), ip_2_ip6(ipaddr));
if (i < 0)
return FALSE;
*nump = i;
return TRUE;
}
/*
* Find an IPv6 address locally assigned to a interface. The IPv6 address is
* given as 'addr6', and must use KAME-style embedding for zones. The
* interface is given as 'ifdev'. On success, return OK, with the IPv6 address
* number stored in 'num'. On failure, return a negative error code. This
* function also returns tentative and duplicated addresses.
*/
int
ifaddr_v6_find(struct ifdev * ifdev, const struct sockaddr_in6 * addr6,
ifaddr_v6_num_t * nump)
{
ip_addr_t ipaddr;
int r;
if ((r = addr_get_inet((const struct sockaddr *)addr6, sizeof(*addr6),
IPADDR_TYPE_V6, &ipaddr, TRUE /*kame*/, NULL /*port*/)) != OK)
return r;
if (ip6_addr_has_zone(ip_2_ip6(&ipaddr)) &&
ip6_addr_zone(ip_2_ip6(&ipaddr)) != ifdev_get_index(ifdev))
return EADDRNOTAVAIL;
if (!ifaddr_v6_match(ifdev, &ipaddr, nump))
return EADDRNOTAVAIL;
return OK;
}
/*
* Enumerate IPv6 addresses locally assigned to the given interface 'ifdev'.
* The caller should set 'nump' to 0 initially, and increase it by one between
* a successful call and the next enumeration call. Return TRUE on success,
* meaning that starting from the given value of 'nump' there is at least one
* IPv6 address, of which the number is stored in 'nump' on return. Return
* FALSE if there are no more IPv6 addresses locally assigned to the interface.
* This function also returns tentative and duplicated address entries.
*/
int
ifaddr_v6_enum(struct ifdev * ifdev, ifaddr_v6_num_t * nump)
{
ifaddr_v6_num_t num;
for (num = *nump; num < LWIP_IPV6_NUM_ADDRESSES; num++) {
if (!ip6_addr_isinvalid(ifdev->ifdev_v6state[num])) {
*nump = num;
return TRUE;
}
}
return FALSE;
}
/*
* Obtain information about the IPv6 address 'num' assigned to the interface
* 'ifdev'. Store the IPv6 address in 'addr6', the network mask in 'mask6',
* and the destination address in 'dest6'. Each of these pointers may be NULL.
* The returned addresses use KAME-style embedding for zones. This function
* also returns tentative and duplicated addresses. It always succeeds.
*/
void
ifaddr_v6_get(struct ifdev * ifdev, ifaddr_v6_num_t num,
struct sockaddr_in6 * addr6, struct sockaddr_in6 * mask6,
struct sockaddr_in6 * dest6)
{
struct netif *netif;
socklen_t addr_len;
/*
* Due to route message generation upon address addition and deletion,
* either the ifdev_v6state or the netif state may not yet have been
* updated here.
*/
assert(!ip6_addr_isinvalid(ifdev->ifdev_v6state[num]) ||
!ip6_addr_isinvalid(netif_ip6_addr_state(ifdev_get_netif(ifdev),
(int)num)));
netif = ifdev_get_netif(ifdev);
if (addr6 != NULL) {
addr_len = sizeof(*addr6);
(void)addr_put_inet((struct sockaddr *)addr6, &addr_len,
netif_ip_addr6(netif, (int)num), TRUE /*kame*/,
0 /*port*/);
}
if (mask6 != NULL) {
addr_len = sizeof(*mask6);
addr_put_netmask((struct sockaddr *)mask6, &addr_len,
IPADDR_TYPE_V6, ifdev->ifdev_v6prefix[num]);
}
if (dest6 != NULL) {
/* TODO: dest6 */
dest6->sin6_len = 0;
dest6->sin6_family = AF_UNSPEC;
}
}
/*
* Obtain NetBSD-style state flags (IN6_IFF_) for the given local IPv6 address.
* The given number must identify an existing address. Return the flags.
*/
int
ifaddr_v6_get_flags(struct ifdev * ifdev, ifaddr_v6_num_t num)
{
int state, flags;
state = ifdev->ifdev_v6state[num];
assert(!ip6_addr_isinvalid(state));
flags = 0;
if (ip6_addr_isduplicated(state))
flags |= IN6_IFF_DUPLICATED;
if (ip6_addr_istentative(state))
flags |= IN6_IFF_TENTATIVE;
if (ip6_addr_isdeprecated(state))
flags |= IN6_IFF_DEPRECATED;
if (ifdev->ifdev_v6flags[num] & IFADDR_V6F_AUTOCONF)
flags |= IN6_IFF_AUTOCONF;
if (ifdev->ifdev_v6flags[num] & IFADDR_V6F_TEMPORARY)
flags |= IN6_IFF_TEMPORARY;
return flags;
}
/*
* Obtain lifetime information about the given local IPv6 address. The given
* 'lifetime' structure is filled as a result. This function always succeeds.
*/
void
ifaddr_v6_get_lifetime(struct ifdev * ifdev, ifaddr_v6_num_t num,
struct in6_addrlifetime * lifetime)
{
struct netif *netif;
uint32_t valid_life, pref_life;
time_t now;
assert(!ip6_addr_isinvalid(ifdev->ifdev_v6state[num]));
netif = ifdev_get_netif(ifdev);
valid_life = netif_ip6_addr_valid_life(netif, (int)num);
pref_life = netif_ip6_addr_pref_life(netif, (int)num);
/*
* Represent 'static' as 'infinite' to userland. This applies only to
* link-local addresses, which do not have lifetimes at all.
*/
if (ip6_addr_life_isstatic(valid_life)) {
valid_life = IP6_ADDR_LIFE_INFINITE;
pref_life = IP6_ADDR_LIFE_INFINITE;
}
now = clock_time(NULL);
/*
* TODO: the _vltime and _pltime values filled in here are not correct.
* They should be set to the originally assigned values rather than the
* current ones. Getting this right would mean we'd have to save the
* original values. So far it does not look like userland needs that..
*/
memset(lifetime, 0, sizeof(*lifetime));
lifetime->ia6t_vltime = valid_life;
lifetime->ia6t_pltime = pref_life;
if (!ip6_addr_life_isinfinite(valid_life))
lifetime->ia6t_expire = now + valid_life;
if (!ip6_addr_life_isinfinite(pref_life))
lifetime->ia6t_preferred = now + pref_life;
}
/*
* Determine whether there should be a local subnet route for the given
* assigned IPv6 address, and if so, compute the subnet mask to add. Return
* TRUE if a local subnet route should be added, and return the network base
* address in 'netbase' and the number of prefix bits in 'prefixp'. Return
* FALSE if no subnet route should be added for the assigned address.
*/
static unsigned int
ifaddr_v6_netroute(struct ifdev * ifdev, ifaddr_v6_num_t num,
ip_addr_t * netbase, unsigned int * prefixp)
{
const ip_addr_t *ipaddr;
ipaddr = netif_ip_addr6(ifdev_get_netif(ifdev), (int)num);
/*
* A local network route should be added only if all of the following
* conditions are met:
*
* 1) The address is not auto-configured. Autoconfigured addresses do
* not have an implied subnet, as explained in RFC 5942.
* Consistency with respect to subnet routes is why we do not allow
* changing the AUTOCONF flag after an address has been added.
* 2) The subnet assignment is not a /128 prefix. Not only would such
* a route not be useful, adding it would fail anyway because we
* currently do not support adding a host-type route and a
* full-width net-type route for the same IP address.
* 3) If the interface is a loopback device, the address is not a link-
* local address. This appears to be what NetBSD does, but
* additional loopback-related exceptions may be needed here.
*/
if ((ifdev->ifdev_v6flags[num] & IFADDR_V6F_AUTOCONF) ||
ifdev->ifdev_v6prefix[num] == IP6_BITS ||
(ifdev_is_loopback(ifdev) &&
ip6_addr_islinklocal(ip_2_ip6(ipaddr))))
return FALSE;
addr_normalize(netbase, ipaddr, ifdev->ifdev_v6prefix[num]);
*prefixp = ifdev->ifdev_v6prefix[num];
return TRUE;
}
/*
* A local IPv6 has become valid (preferred or deprecated) after previously
* being invalid (tentative, duplicated, or free). Report the addition of the
* now-usable address, and add appropriate routes to the IPv6 routing table.
*
* This function is *not* called immediately when an address is added, but
* rather when the address becomes valid (meaning it is no longer tentative,
* and thus supposedly collision-free). For that reason, unlike for IPv4, this
* function is only ever called indirectly, through the netif status callback.
*/
static void
ifaddr_v6_added(struct ifdev * ifdev, ifaddr_v6_num_t num)
{
const ip_addr_t *ipaddr;
ip_addr_t base;
ip6_addr_t *base6;
unsigned int prefix;
/* Check the netif as ifdev_v6state is not yet updated here. */
assert(!ip6_addr_isinvalid(netif_ip6_addr_state(ifdev_get_netif(ifdev),
(int)num)));
/* Report the addition of the interface address. */
rtsock_msg_addr_v6(ifdev, RTM_NEWADDR, num);
/*
* Add the local host route. This will always succeed. See the IPv4
* version of this code for more information.
*/
ipaddr = netif_ip_addr6(ifdev_get_netif(ifdev), (int)num);
(void)route_add(ipaddr, IP6_BITS, NULL /*gateway*/, ifdev,
IFADDR_HOST_RTFLAGS, NULL /*rtr*/);
/*
* Add the local network route, if the rules say that we should. Even
* then, adding the route may fail for various reasons, but this route
* is not essential and so we ignore failures here.
*/
if (ifaddr_v6_netroute(ifdev, num, &base, &prefix))
(void)route_add(&base, prefix, NULL /*gateway*/, ifdev,
IFADDR_NET_RTFLAGS, NULL /*rtr*/);
/*
* Add the node-local and link-local scope multicast routes. These are
* interface-specific rather than address-specific. They are (re)added
* for every address, and never deleted until interface destruction.
*/
ip_addr_set_zero_ip6(&base);
base6 = ip_2_ip6(&base);
base6->addr[0] = htonl(0xff010000UL | ifdev_get_index(ifdev));
(void)route_add(&base, 32, NULL /*gateway*/, ifdev, IFADDR_NET_RTFLAGS,
NULL /*rtr*/);
base6->addr[0] = htonl(0xff020000UL | ifdev_get_index(ifdev));
(void)route_add(&base, 32, NULL /*gateway*/, ifdev, IFADDR_NET_RTFLAGS,
NULL /*rtr*/);
}
/*
* A particular local IPv6 address is being deleted. See if there is another
* local IPv6 address assigned that should have the same local subnet route
* (but didn't, as such duplicate routes can obviously not be added), and if
* so, readd the route for that other address, possibly for the same interface.
*/
static void
ifaddr_v6_dupcheck(struct ifdev * oifdev, const ip_addr_t * onetbase,
unsigned int oprefix)
{
struct ifdev *ifdev;
ip_addr_t netbase;
unsigned int prefix;
ifaddr_v6_num_t num;
for (ifdev = NULL; (ifdev = ifdev_enum(ifdev)) != NULL; ) {
if (ifdev == oifdev)
continue;
for (num = 0; num < LWIP_IPV6_NUM_ADDRESSES; num++) {
if (ip6_addr_isinvalid(ifdev->ifdev_v6state[num]) ||
0