ip_rts.c revision b8965a531245c9dd5e0514fc718dee5c9f61bfcf
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)rtsock.c 8.6 (Berkeley) 2/11/95
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains routines that processes routing socket requests.
*/
#include <inet/ip_ftable.h>
#include <inet/ipclassifier.h>
int *error);
/*
* Send the ack to all the routing queues. In case of the originating queue,
* send it only if the loopback is set.
*
* Messages are sent upstream only on routing sockets that did not specify an
* address family when they were created or when the address family matches the
* one specified by the caller.
*
*/
void
{
int checkqfull;
/*
* If there was a family specified when this routing socket was
* created and it doesn't match the family of the message to
* copy, then continue.
*/
continue;
}
/*
* For the originating queue, we only copy the message upstream
* if loopback is set. For others reading on the routing
* socket, we check if there is room upstream for a copy of the
* message.
*/
if (connp->conn_loopback == 0) {
continue;
}
} else {
checkqfull = B_TRUE;
}
}
/* Follow the next pointer before releasing the conn. */
connp = next_connp;
}
}
/*
* Takes an ire and sends an ack to all the routing sockets. This
* routine is used
* - when ire_expire deletes a stale redirect
*/
void
{
return;
switch (ire->ire_ipversion) {
case IPV4_VERSION:
return;
0, NULL);
break;
case IPV6_VERSION:
return;
break;
}
if (error != 0)
else
}
/* ARGSUSED */
static void
{
}
/*
* Processes requests received on a routing socket. It extracts all the
* arguments and calls the appropriate function to process the request.
*
* RTA_SRC bit flag requests are sent by mipagent and 'route -setsrc'.
* RTA_SRCIFP bit flag requests are sent by mipagent only.
*
* In general, this function does not consume the message supplied but rather
* sends the message upstream with an appropriate UNIX errno.
*
* We may need to restart this operation if the ipif cannot be looked up
* due to an exclusive operation that is currently in progress. The restart
* entry point is ip_rts_request_retry. While the request is enqueud in the
* ipsq the ioctl could be aborted and the conn close. To ensure that we don't
* have stale conn pointers, ip_wput_ioctl does a conn refhold. This is
* released at the completion of the rts ioctl at the end of this function
* by calling CONN_OPER_PENDING_DONE or when the ioctl is aborted and
* conn close occurs in conn_ioctl_cleanup.
*/
int
{
int error = 0;
int match_flags = MATCH_IRE_DSTONLY;
int found_addrs;
/* ioc_mp holds mp */
/*
* The Routing Socket data starts on
* next block. If there is no next block
* this is an indication from routing module
* that it is a routing socket stream queue.
*/
goto done;
}
} else {
/*
* This is a message from RTS module
* indicating that this is a Routing Socket
* Stream. Insert this conn_t in routing
* socket client list.
*/
goto done;
}
goto done;
}
goto done;
}
/*
* Check the routing message for basic consistency including the
* version number and that the number of octets written is the same
* as specified by the rtm_msglen field.
*
* At this point, an error can be delivered back via rtm_errno.
*/
goto done;
}
goto done;
}
/* Only allow RTM_GET or RTM_RESOLVE for unprivileged process */
goto done;
}
if (error != 0)
goto done;
if ((found_addrs & RTA_DST) == 0) {
goto done;
}
/*
* Based on the address family of the destination address, determine
* the destination, gateway and netmask and return the appropriate error
* if an unknown address family was specified (following the errno
* values that 4.4BSD-Lite2 returns.)
*/
switch (af) {
case AF_INET:
/*
* RTA_SRCIFP is supported for interface route only.
* Thus a gateway route with srcifindex is rejected,
* except if it's a request to add reverse tunnel
* route.
*/
(found_addrs & RTA_SRCIFP) &&
!(found_addrs & RTA_SRC)) {
goto done;
}
if (((found_addrs & RTA_NETMASK) == 0) ||
else
break;
case AF_INET6:
/*
* RTA_SRCIFP is not a valid flag for IPv6 routes.
*/
if (found_addrs & RTA_SRCIFP) {
goto done;
}
if (((found_addrs & RTA_NETMASK) == 0) ||
break;
default:
/*
* These errno values are meant to be compatible with
* 4.4BSD-Lite2 for the given message types.
*/
case RTM_ADD:
case RTM_DELETE:
goto done;
case RTM_GET:
case RTM_CHANGE:
goto done;
default:
error = EOPNOTSUPP;
goto done;
}
}
/*
* At this point, the address family must be something known.
*/
if (index != 0) {
/*
* IPC must be refheld somewhere in ip_wput_nondata or
* ip_wput_ioctl etc... and cleaned up if ioctl is killed.
* If ILL_CHANGING the request is queued in the ipsq.
*/
if (error != EINPROGRESS)
goto done;
}
/*
* If this is replacement ipif, prevent a route from
* being added.
*/
goto done;
}
}
/* RTA_SRCIFP is unsupported on AF_INET6. */
/* If ILL_CHANGING the request is queued in the ipsq. */
if (error != EINPROGRESS)
goto done;
}
}
/*
* If a netmask was supplied in the message, then subsequent route
* lookups will attempt to match on the netmask as well.
*/
if ((found_addrs & RTA_NETMASK) != 0)
/*
* We only process any passed-in route security attributes for
* either RTM_ADD or RTM_CHANGE message; We overload them
* to do an RTM_GET as a different label; ignore otherwise.
*/
}
case RTM_ADD:
/* if we are adding a route, gateway is a must */
if ((found_addrs & RTA_GATEWAY) == 0) {
goto done;
}
/* Multirouting does not support net routes. */
RTF_MULTIRT) {
goto done;
}
/*
* Multirouting and user-specified source addresses
* do not support interface based routing.
* Assigning a source address to an interface based
* route is achievable by plumbing a new ipif and
* setting up the interface route via this ipif,
* though.
*/
goto done;
}
}
switch (af) {
case AF_INET:
if (src_addr != INADDR_ANY) {
/*
* If there is a source address, but
* no RTF_SETSRC modifier, setup a MobileIP
* reverse tunnel.
*/
break;
}
/*
* The RTF_SETSRC flag is present, check that
* the supplied src address is not the loopback
* address. This would produce martian packets.
*/
goto done;
}
/*
* Also check that the supplied address is a
* valid, local one.
*/
if (error != EINPROGRESS)
goto done;
}
(tmp_ipif->ipif_flags &
(IPIF_NOLOCAL | IPIF_ANYCAST))) {
goto done;
}
} else {
/*
* The RTF_SETSRC modifier must be associated
* to a non-null source address.
*/
goto done;
}
}
rtsap);
break;
case AF_INET6:
if (!IN6_IS_ADDR_UNSPECIFIED(&src_addr_v6)) {
/*
* If there is a source address, but
* no RTF_SETSRC modifier, reject, as
* MobileIP IPv6 reverse tunnels are
* not supported.
*/
goto done;
}
/*
* The RTF_SETSRC flag is present, check that
* the supplied src address is not the loopback
* address. This would produce martian packets.
*/
if (IN6_IS_ADDR_LOOPBACK(&src_addr_v6)) {
goto done;
}
/*
* Also check that the supplied address is a
* valid, local one.
*/
if (error != EINPROGRESS)
goto done;
}
(tmp_ipif->ipif_flags &
(IPIF_NOLOCAL | IPIF_ANYCAST))) {
goto done;
}
break;
}
/*
* The RTF_SETSRC modifier must be associated
* to a non-null source address.
*/
goto done;
}
break;
}
if (error != 0)
goto done;
break;
case RTM_DELETE:
/* if we are deleting a route, gateway is a must */
if ((found_addrs & RTA_GATEWAY) == 0) {
goto done;
}
/*
* The RTF_SETSRC modifier does not make sense
* when deleting a route.
*/
goto done;
}
switch (af) {
case AF_INET:
/*
* If there is a source address, delete
* a MobileIP reverse tunnel.
*/
if (src_addr != INADDR_ANY) {
src_ipif);
break;
}
break;
case AF_INET6:
break;
}
break;
case RTM_GET:
case RTM_CHANGE:
/*
* In the case of RTM_GET, the forwarding table should be
* searched recursively with default being matched if the
* specific route doesn't exist. Also, if a gateway was
* specified then the gateway address must also be matched.
*
* In the case of RTM_CHANGE, the gateway address (if supplied)
* is the new gateway address so matching on the gateway address
* is not done. This can lead to ambiguity when looking up the
* route to change as usually only the destination (and netmask,
* if supplied) is used for the lookup. However if a RTA_IFP
* sockaddr is also supplied, it can disambiguate which route to
* change provided the ambigous routes are tied to distinct
* ill's (or interface indices). If the routes are not tied to
* any particular interfaces (for example, with traditional
* gateway routes), then a RTA_IFP sockaddr will be of no use as
* it won't match any such routes.
* RTA_SRC is not supported for RTM_GET and RTM_CHANGE,
* except when RTM_CHANGE is combined to RTF_SETSRC.
*/
if (((found_addrs & RTA_SRC) != 0) &&
error = EOPNOTSUPP;
goto done;
}
match_flags |=
if ((found_addrs & RTA_GATEWAY) != 0)
if (ioc_cr)
if (rtsa_validate(rtsap) != 0) {
goto done;
}
goto done;
}
tsl = labelalloc(
}
}
if ((found_addrs & RTA_GATEWAY) &&
/*
* Do not want to change the gateway,
* but rather the source address.
*/
}
}
/*
* If the netmask is all ones (either as supplied or as derived
* above), then first check for an IRE_LOOPBACK or
* IRE_LOCAL entry.
*
* If we didn't check for or find an IRE_LOOPBACK or IRE_LOCAL
* entry, then look in the forwarding table.
*/
switch (af) {
case AF_INET:
if (net_mask == IP_HOST_MASK) {
}
tsl, match_flags);
}
break;
case AF_INET6:
}
}
break;
}
goto done;
}
/* we know the IRE before we come here */
case RTM_GET:
goto done;
}
break;
case RTM_CHANGE:
/*
* Do not allow to the multirouting state of a route
* to be changed. This aims to prevent undesirable
* stages where both multirt and non-multirt routes
* for the same destination are declared.
*/
goto done;
}
/*
* Note that we do not need to do
* ire_flush_cache_*(IRE_FLUSH_ADD) as a change
* in metrics or gateway will not affect existing
* routes since it does not create a more specific
* route.
*/
switch (af) {
case AF_INET:
if ((found_addrs & RTA_GATEWAY) != 0 &&
}
goto done;
}
}
if ((found_addrs & RTA_SRC) != 0 &&
if (src_addr != INADDR_ANY) {
/*
* The RTF_SETSRC flag is
* present, check that the
* supplied src address is not
* the loopback address. This
* would produce martian
* packets.
*/
if (src_addr ==
htonl(INADDR_LOOPBACK)) {
goto done;
}
/*
* Also check that the the
* supplied addr is a valid
* local address.
*/
&error);
EINPROGRESS) ?
error :
goto done;
}
if (!(tmp_ipif->ipif_flags &
IPIF_UP) ||
(tmp_ipif->ipif_flags &
(IPIF_NOLOCAL |
IPIF_ANYCAST))) {
goto done;
}
} else {
}
}
break;
case AF_INET6:
if ((found_addrs & RTA_GATEWAY) != 0 &&
}
goto done;
}
}
if ((found_addrs & RTA_SRC) != 0 &&
if (!IN6_IS_ADDR_UNSPECIFIED(
&src_addr_v6)) {
/*
* The RTF_SETSRC flag is
* present, check that the
* supplied src address is not
* the loopback address. This
* would produce martian
* packets.
*/
if (IN6_IS_ADDR_LOOPBACK(
&src_addr_v6)) {
goto done;
}
/*
* Also check that the the
* supplied addr is a valid
* local address.
*/
&src_addr_v6, NULL,
&error);
EINPROGRESS) ?
error :
goto done;
}
if (!(tmp_ipif->ipif_flags &
IPIF_UP) ||
(tmp_ipif->ipif_flags &
(IPIF_NOLOCAL |
IPIF_ANYCAST))) {
goto done;
}
} else {
}
}
break;
}
/*
* Create and add the security attribute to
* prefix IRE; it will add a reference to the
* group upon allocating a new entry. If it
* finds an already-existing entry for the
* security attribute, it simply returns it
* and no new group reference is made.
*/
GC_REFRELE(gc);
} else {
/* gc_create failed */
}
goto done;
}
/*
* Now delete any existing gateway IRE caches
* as well as all caches using the gateway,
* and allow them to be created on demand
* through ip_newroute{_v6}.
*/
} else {
}
}
break;
}
break;
default:
error = EOPNOTSUPP;
break;
}
done:
if (gcgrp_xtraref)
if (error == EINPROGRESS)
return (error);
if (error != 0) {
/* Send error ACK */
} else {
/* OK ACK already set up by caller except this */
ip2dbg(("ip_rts_request: OK ACK\n"));
}
}
/* conn was refheld in ip_wput_ioctl. */
return (error);
}
/*
* Build a reply to the RTM_GET request contained in the given message block
* using the retrieved IRE of the destination address, the parent IRE (if it
* exists) and the address family.
*
* Returns a pointer to a message block containing the reply if successful,
* otherwise NULL is returned.
*/
static mblk_t *
{
int rtm_addrs;
int rtm_flags;
int sacnt = 0;
sacnt = 1;
}
/* do nothing if there's no gc to report */
/* we might as well drop the lock now */
}
}
}
/*
* Always return RTA_DST, RTA_GATEWAY and RTA_NETMASK.
*
* RTA_IFP and RTA_IFA if either is defined, and also
* returns RTA_BRD if the appropriate interface is
* point-to-point.
*/
}
return (NULL);
}
/*
* We set the destination address, gateway address,
* netmask and flags in the RTM_GET response depending
* on whether we found a parent IRE or not.
* In particular, if we did find a parent IRE during the
* recursive search, use that IRE's gateway address.
* Otherwise, we use the IRE's source address for the
* gateway address.
*/
switch (af) {
case AF_INET:
} else {
}
break;
case AF_INET6:
} else {
}
break;
}
/*
* The rtm_msglen, rtm_version and rtm_type fields in
* RTM_GET response are filled in by rts_fill_msg.
*
* rtm_addrs and rtm_flags are filled in based on what
* was requested and the state of the IREs looked up
* above.
*
* rtm_inits and rtm_rmx are filled in with metrics
* based on whether a parent IRE was found or not.
*
* TODO: rtm_index and rtm_use should probably be
* filled in with something resonable here and not just
* copied from the request.
*/
else
return (new_mp);
}
/*
* Fill the given if_data_t with interface statistics.
*/
static void
{
/* protocol. */
}
/*
* Set the metrics on a forwarding table route.
*/
static void
{
/*
* Bypass obtaining the lock and searching ipif_saved_ire_mp in the
* common case of no metrics.
*/
if (which == 0)
return;
/*
* iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
* microseconds.
*/
if (which & RTV_RTTVAR)
/*
* Update the metrics in the IRE itself.
*/
if (which & RTV_SSTHRESH)
if (which & RTV_RTTVAR)
/*
* Search through the ifrt_t chain hanging off the IPIF in order to
* reflect the metric change there.
*/
return;
}
/*
* On a given ipif, the triple of address, gateway and mask is
* unique for each saved IRE (in the case of ordinary interface
* routes, the gateway address is all-zeroes).
*/
&ire->ire_addr_v6) ||
&gw_addr_v6) ||
&ire->ire_mask_v6))
continue;
} else {
continue;
}
if (which & RTV_SSTHRESH) {
}
if (which & RTV_RTTVAR)
break;
}
}
/*
* Get the metrics from a forwarding table route.
*/
static int
{
int metrics_set = 0;
/*
* iulp_rtt and iulp_rtt_sd are in milliseconds, but 4.4BSD-Lite2's
* microseconds.
*/
metrics_set |= RTV_RTT;
metrics_set |= RTV_MTU;
metrics_set |= RTV_SPIPE;
metrics_set |= RTV_RPIPE;
return (metrics_set);
}
/*
* Takes a pointer to a routing message and extracts necessary info by looking
* at the rtm->rtm_addrs bits and store the requested sockaddrs in the pointers
* passed (all of which must be valid).
*
* The bitmask of sockaddrs actually found in the message is returned, or zero
* is returned in the case of an error.
*/
static int
{
int i;
int addr_bits;
int length;
int found_addrs = 0;
struct sockaddr_dl *sdl;
*indexp = 0;
*src_indexp = 0;
*error = 0;
/*
* At present we handle only RTA_DST, RTA_GATEWAY, RTA_NETMASK, RTA_IFP,
* RTA_IFA and RTA_AUTHOR. The rest will be added as we need them.
*/
/*
* The address family we are working with starts out as
* AF_UNSPEC, but is set to the one specified with the
* destination address.
*
* If the "working" address family that has been set to
* something other than AF_UNSPEC, then the address family of
* subsequent sockaddrs must either be AF_UNSPEC (for
* compatibility with older programs) or must be the same as our
* "working" one.
*
* This code assumes that RTA_DST (1) comes first in the loop.
*/
if (addr_bits == 0)
continue;
switch (addr_bits) {
case RTA_DST:
break;
case RTA_GATEWAY:
return (0);
break;
case RTA_NETMASK:
return (0);
break;
case RTA_IFP:
return (0);
size = sizeof (struct sockaddr_dl);
break;
case RTA_SRC:
/* Source address of the incoming packet */
break;
case RTA_SRCIFP:
/* Return incoming interface index pointer */
return (0);
size = sizeof (struct sockaddr_dl);
break;
case RTA_IFA:
return (0);
break;
case RTA_AUTHOR:
return (0);
break;
default:
return (0);
}
if (size == 0)
return (0);
found_addrs |= addr_bits;
}
/*
* Parse the routing message and look for any security-
* related attributes for the route. For each valid
* route security attributes.
*/
return (found_addrs);
}
/*
* Fills the message with the given info.
*/
static void
{
int i;
/*
* First find the type of the message
* and its length.
*/
/*
* Now find the size of the data
* that follows the message header.
*/
for (i = 0; i < RTA_NUMBITS; i++) {
switch (rtm_addrs & (1 << i)) {
case RTA_DST:
break;
case RTA_GATEWAY:
break;
case RTA_NETMASK:
break;
case RTA_IFP:
break;
case RTA_SRCIFP:
/*
* RTA_SRCIFP is not yet supported
* for RTM_GET and RTM_CHANGE
*/
break;
case RTA_IFA:
case RTA_SRC:
break;
case RTA_AUTHOR:
break;
case RTA_BRD:
/*
* RTA_BRD is used typically to specify a point-to-point
* destination address.
*/
break;
}
}
int i;
}
}
/*
* set the fields that are common to
* to different messages.
*/
}
/*
* Allocates and initializes a routing socket message.
*/
mblk_t *
{
return (mp);
return (mp);
}
/*
* Returns the size of the routing
* socket message header size.
*/
rts_header_msg_size(int type)
{
switch (type) {
case RTM_DELADDR:
case RTM_NEWADDR:
return (sizeof (ifa_msghdr_t));
case RTM_IFINFO:
return (sizeof (if_msghdr_t));
default:
return (sizeof (rt_msghdr_t));
}
}
/*
* Returns the size of the message needed with the given rtm_addrs and family.
*
* It is assumed that all of the sockaddrs (with the exception of RTA_IFP) are
* of the same family (currently either AF_INET or AF_INET6).
*/
{
int i;
for (i = 0; i < RTA_NUMBITS; i++) {
switch (rtm_addrs & (1 << i)) {
case RTA_IFP:
length += sizeof (struct sockaddr_dl);
break;
case RTA_DST:
case RTA_GATEWAY:
case RTA_NETMASK:
case RTA_SRC:
case RTA_SRCIFP:
case RTA_IFA:
case RTA_AUTHOR:
case RTA_BRD:
switch (af) {
case AF_INET:
break;
case AF_INET6:
break;
}
break;
}
}
if (sacnt > 0)
return (length);
}
/*
* This routine is called to generate a message to the routing
* socket indicating that a redirect has occured, a routing lookup
* has failed, or that a protocol has detected timeouts to a particular
* destination. This routine is called for message types RTM_LOSING,
* RTM_REDIRECT, and RTM_MISS.
*/
void
{
if (rtm_addrs == 0)
return;
return;
}
/*
* This routine is called to generate a message to the routing
* socket indicating that the status of a network interface has changed.
* Message type generated RTM_IFINFO.
*/
void
{
/*
* This message should be generated only
* when the physical device is changing
* state.
*/
return;
return;
} else {
return;
0, NULL);
}
}
/*
* This is called to generate messages to the routing socket
* indicating a network interface has had addresses associated with it.
*/
void
{
int pass;
int ncmd;
int rtm_addrs;
else
/*
* If the request is DELETE, send RTM_DELETE and RTM_DELADDR.
* if the request is ADD, send RTM_NEWADDR and RTM_ADD.
*/
continue;
switch (af) {
case AF_INET:
0, NULL);
break;
case AF_INET6:
break;
}
ifam->ifam_index =
}
continue;
switch (af) {
case AF_INET:
break;
case AF_INET6:
break;
}
if (error == 0)
}
}
}
/*
* Based on the address family specified in a sockaddr, copy the address field
* into an in6_addr_t.
*
* In the case of AF_UNSPEC, we assume the family is actually AF_INET for
* compatibility with programs that leave the family cleared in the sockaddr.
* Callers of rts_copyfromsockaddr should check the family themselves if they
* wish to verify its value.
*
* In the case of AF_INET6, a check is made to ensure that address is not an
* IPv4-mapped address.
*/
{
case AF_INET:
case AF_UNSPEC:
return (sizeof (sin_t));
case AF_INET6:
if (IN6_IS_ADDR_V4MAPPED(addrp))
return (0);
return (sizeof (sin6_t));
default:
return (0);
}
}