ipadm_if.c revision 6e91bba0d6c6bdabbba62cefae583715a4a58e2a
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <errno.h>
2N/A#include <sys/sockio.h>
2N/A#include <string.h>
2N/A#include <assert.h>
2N/A#include <unistd.h>
2N/A#include <stropts.h>
2N/A#include <strings.h>
2N/A#include <libdlpi.h>
2N/A#include <libdllink.h>
2N/A#include <libinetutil.h>
2N/A#include <inet/ip.h>
2N/A#include <limits.h>
2N/A#include <zone.h>
2N/A#include <ipadm_ndpd.h>
2N/A#include "libipadm_impl.h"
2N/A
2N/Astatic ipadm_status_t i_ipadm_slifname_arp(char *, uint64_t, int);
2N/Astatic ipadm_status_t i_ipadm_slifname(ipadm_handle_t, char *, char *,
2N/A uint64_t, int, uint32_t);
2N/Astatic ipadm_status_t i_ipadm_create_ipmp_peer(ipadm_handle_t, char *,
2N/A sa_family_t);
2N/Astatic ipadm_status_t i_ipadm_persist_if(ipadm_handle_t, const char *,
2N/A sa_family_t);
2N/A
2N/A/*
2N/A * Returns B_FALSE if the interface in `ifname' has at least one address that is
2N/A * IFF_UP in the addresses in `ifa'.
2N/A */
2N/Astatic boolean_t
2N/Ai_ipadm_is_if_down(char *ifname, struct ifaddrs *ifa)
2N/A{
2N/A struct ifaddrs *ifap;
2N/A char cifname[LIFNAMSIZ];
2N/A char *sep;
2N/A
2N/A for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
2N/A (void) strlcpy(cifname, ifap->ifa_name, sizeof (cifname));
2N/A if ((sep = strrchr(cifname, IPADM_LOGICAL_SEP)) != NULL)
2N/A *sep = '\0';
2N/A /*
2N/A * If this condition is true, there is at least one
2N/A * address that is IFF_UP. So, we need to return B_FALSE.
2N/A */
2N/A if (strcmp(cifname, ifname) == 0 &&
2N/A (ifap->ifa_flags & IFF_UP)) {
2N/A return (B_FALSE);
2N/A }
2N/A }
2N/A /* We did not find any IFF_UP addresses. */
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * Retrieves the information for the interface `ifname' from active
2N/A * config if `ifname' is specified and returns the result in the list `if_info'.
2N/A * Otherwise, it retrieves the information for all the interfaces in
2N/A * the active config and returns the result in the list `if_info'.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_active_if_info(ipadm_handle_t iph, const char *ifname,
2N/A ipadm_if_info_t **if_info, int64_t lifc_flags)
2N/A{
2N/A struct lifreq *buf;
2N/A struct lifreq *lifrp;
2N/A struct lifreq lifrl;
2N/A ipadm_if_info_t *last = NULL;
2N/A ipadm_if_info_t *ifp;
2N/A int s;
2N/A int n;
2N/A int numifs;
2N/A ipadm_status_t status;
2N/A
2N/A *if_info = NULL;
2N/A /*
2N/A * Get information for all interfaces.
2N/A */
2N/A if (getallifs(iph->iph_sock, 0, &buf, &numifs, lifc_flags) != 0)
2N/A return (ipadm_errno2status(errno));
2N/A
2N/A lifrp = buf;
2N/A for (n = 0; n < numifs; n++, lifrp++) {
2N/A /* Skip interfaces with logical num != 0 */
2N/A if (i_ipadm_get_lnum(lifrp->lifr_name) != 0)
2N/A continue;
2N/A /*
2N/A * Skip the current interface if a specific `ifname' has
2N/A * been requested and current interface does not match
2N/A * `ifname'.
2N/A */
2N/A if (ifname != NULL && strcmp(lifrp->lifr_name, ifname) != 0)
2N/A continue;
2N/A /*
2N/A * Check if the interface already exists in our list.
2N/A * If it already exists, we need to update its flags.
2N/A */
2N/A for (ifp = *if_info; ifp != NULL; ifp = ifp->ifi_next) {
2N/A if (strcmp(lifrp->lifr_name, ifp->ifi_name) == 0)
2N/A break;
2N/A }
2N/A if (ifp == NULL) {
2N/A ifp = calloc(1, sizeof (ipadm_if_info_t));
2N/A if (ifp == NULL) {
2N/A status = ipadm_errno2status(errno);
2N/A goto fail;
2N/A }
2N/A (void) strlcpy(ifp->ifi_name, lifrp->lifr_name,
2N/A sizeof (ifp->ifi_name));
2N/A /* Update the `ifi_next' pointer for this new node */
2N/A if (*if_info == NULL)
2N/A *if_info = ifp;
2N/A else
2N/A last->ifi_next = ifp;
2N/A last = ifp;
2N/A }
2N/A
2N/A /*
2N/A * Retrieve the flags for the interface by doing a
2N/A * SIOCGLIFFLAGS to populate the `ifi_cflags' field.
2N/A */
2N/A (void) strlcpy(lifrl.lifr_name,
2N/A lifrp->lifr_name, sizeof (lifrl.lifr_name));
2N/A s = (lifrp->lifr_addr.ss_family == AF_INET) ?
2N/A iph->iph_sock : iph->iph_sock6;
2N/A if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifrl) < 0)
2N/A continue;
2N/A if (lifrl.lifr_flags & IFF_BROADCAST)
2N/A ifp->ifi_cflags |= IFIF_BROADCAST;
2N/A if (lifrl.lifr_flags & IFF_MULTICAST)
2N/A ifp->ifi_cflags |= IFIF_MULTICAST;
2N/A if (lifrl.lifr_flags & IFF_POINTOPOINT)
2N/A ifp->ifi_cflags |= IFIF_POINTOPOINT;
2N/A if (lifrl.lifr_flags & IFF_VIRTUAL)
2N/A ifp->ifi_cflags |= IFIF_VIRTUAL;
2N/A if (lifrl.lifr_flags & IFF_IPMP)
2N/A ifp->ifi_cflags |= IFIF_IPMP;
2N/A if (lifrl.lifr_flags & IFF_STANDBY)
2N/A ifp->ifi_cflags |= IFIF_STANDBY;
2N/A if (lifrl.lifr_flags & IFF_INACTIVE)
2N/A ifp->ifi_cflags |= IFIF_INACTIVE;
2N/A if (lifrl.lifr_flags & IFF_VRRP)
2N/A ifp->ifi_cflags |= IFIF_VRRP;
2N/A if (lifrl.lifr_flags & IFF_NOACCEPT)
2N/A ifp->ifi_cflags |= IFIF_NOACCEPT;
2N/A if (lifrl.lifr_flags & IFF_IPV4)
2N/A ifp->ifi_cflags |= IFIF_IPV4;
2N/A if (lifrl.lifr_flags & IFF_IPV6)
2N/A ifp->ifi_cflags |= IFIF_IPV6;
2N/A }
2N/A free(buf);
2N/A return (IPADM_SUCCESS);
2N/Afail:
2N/A free(buf);
2N/A ipadm_free_if_info(*if_info);
2N/A *if_info = NULL;
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Returns the interface information for `ifname' in `if_info' from persistent
2N/A * config if `ifname' is non-null. Otherwise, it returns all the interfaces
2N/A * from persistent config in `if_info'.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_persist_if_info(ipadm_handle_t iph, const char *ifname,
2N/A ipadm_if_info_t **if_info)
2N/A{
2N/A ipadm_status_t status = IPADM_SUCCESS;
2N/A ipmgmt_getif_arg_t getif;
2N/A ipmgmt_getif_rval_t *rvalp;
2N/A ipadm_if_info_t *ifp, *curr, *prev = NULL;
2N/A int i = 0, err = 0;
2N/A
2N/A bzero(&getif, sizeof (getif));
2N/A if (ifname != NULL)
2N/A (void) strlcpy(getif.ia_ifname, ifname, LIFNAMSIZ);
2N/A getif.ia_cmd = IPMGMT_CMD_GETIF;
2N/A
2N/A *if_info = NULL;
2N/A
2N/A if ((rvalp = malloc(sizeof (ipmgmt_getif_rval_t))) == NULL)
2N/A return (ipadm_errno2status(errno));
2N/A err = ipadm_door_call(iph, &getif, sizeof (getif), (void **)&rvalp,
2N/A sizeof (*rvalp), B_TRUE);
2N/A if (err == ENOENT) {
2N/A free(rvalp);
2N/A if (ifname != NULL)
2N/A return (ipadm_errno2status(err));
2N/A return (IPADM_SUCCESS);
2N/A } else if (err != 0) {
2N/A free(rvalp);
2N/A return (ipadm_errno2status(err));
2N/A }
2N/A
2N/A ifp = rvalp->ir_ifinfo;
2N/A for (i = 0; i < rvalp->ir_ifcnt; i++) {
2N/A ifp = rvalp->ir_ifinfo + i;
2N/A if ((curr = malloc(sizeof (*curr))) == NULL) {
2N/A status = ipadm_errno2status(errno);
2N/A ipadm_free_if_info(prev);
2N/A break;
2N/A }
2N/A (void) bcopy(ifp, curr, sizeof (*curr));
2N/A curr->ifi_next = prev;
2N/A prev = curr;
2N/A }
2N/A *if_info = curr;
2N/A free(rvalp);
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Collects information for `ifname' if one is specified from both
2N/A * active and persistent config in `if_info'. If no `ifname' is specified,
2N/A * this returns all the interfaces in active and persistent config in
2N/A * `if_info'.
2N/A */
2N/Aipadm_status_t
2N/Ai_ipadm_get_all_if_info(ipadm_handle_t iph, const char *ifname,
2N/A ipadm_if_info_t **if_info, int64_t lifc_flags)
2N/A{
2N/A ipadm_status_t status;
2N/A ipadm_if_info_t *aifinfo = NULL;
2N/A ipadm_if_info_t *pifinfo = NULL;
2N/A ipadm_if_info_t *aifp;
2N/A ipadm_if_info_t *pifp;
2N/A ipadm_if_info_t *last = NULL;
2N/A struct ifaddrs *ifa;
2N/A struct ifaddrs *ifap;
2N/A
2N/A /*
2N/A * Retrive the information for the requested `ifname' or all
2N/A * interfaces from active configuration.
2N/A */
2N/Aretry:
2N/A status = i_ipadm_active_if_info(iph, ifname, &aifinfo, lifc_flags);
2N/A if (status != IPADM_SUCCESS)
2N/A return (status);
2N/A /* Get the interface state for each interface in `aifinfo'. */
2N/A if (aifinfo != NULL) {
2N/A /* We need all addresses to get the interface state */
2N/A if (getallifaddrs(AF_UNSPEC, &ifa, (LIFC_NOXMIT|LIFC_TEMPORARY|
2N/A LIFC_ALLZONES|LIFC_UNDER_IPMP)) != 0) {
2N/A status = ipadm_errno2status(errno);
2N/A goto fail;
2N/A }
2N/A for (aifp = aifinfo; aifp != NULL; aifp = aifp->ifi_next) {
2N/A /*
2N/A * Find the `ifaddrs' structure from `ifa'
2N/A * for this interface. We need the IFF_* flags
2N/A * to find the interface state.
2N/A */
2N/A for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
2N/A if (strcmp(ifap->ifa_name, aifp->ifi_name) == 0)
2N/A break;
2N/A }
2N/A if (ifap == NULL) {
2N/A /*
2N/A * The interface might have been removed
2N/A * from kernel. Retry getting all the active
2N/A * interfaces.
2N/A */
2N/A freeifaddrs(ifa);
2N/A ipadm_free_if_info(aifinfo);
2N/A aifinfo = NULL;
2N/A goto retry;
2N/A }
2N/A if (!(ifap->ifa_flags & IFF_RUNNING) ||
2N/A (ifap->ifa_flags & IFF_FAILED))
2N/A aifp->ifi_state = IFIS_FAILED;
2N/A else if (ifap->ifa_flags & IFF_OFFLINE)
2N/A aifp->ifi_state = IFIS_OFFLINE;
2N/A else if (i_ipadm_is_if_down(aifp->ifi_name, ifa))
2N/A aifp->ifi_state = IFIS_DOWN;
2N/A else
2N/A aifp->ifi_state = IFIS_OK;
2N/A if (aifp->ifi_next == NULL)
2N/A last = aifp;
2N/A }
2N/A freeifaddrs(ifa);
2N/A }
2N/A /*
2N/A * Get the persistent interface information in `pifinfo'.
2N/A */
2N/A status = i_ipadm_persist_if_info(iph, ifname, &pifinfo);
2N/A if (status == IPADM_NOTFOUND) {
2N/A *if_info = aifinfo;
2N/A return (IPADM_SUCCESS);
2N/A }
2N/A if (status != IPADM_SUCCESS)
2N/A goto fail;
2N/A /*
2N/A * If a persistent interface is also found in `aifinfo', update
2N/A * its entry in `aifinfo' with the persistent information from
2N/A * `pifinfo'. If an interface is found in `pifinfo', but not in
2N/A * `aifinfo', it means that this interface was disabled. We should
2N/A * add this interface to `aifinfo' and set it state to IFIF_DISABLED.
2N/A */
2N/A for (pifp = pifinfo; pifp != NULL; pifp = pifp->ifi_next) {
2N/A for (aifp = aifinfo; aifp != NULL; aifp = aifp->ifi_next) {
2N/A if (strcmp(aifp->ifi_name, pifp->ifi_name) == 0) {
2N/A aifp->ifi_pflags = pifp->ifi_pflags;
2N/A break;
2N/A }
2N/A }
2N/A if (aifp == NULL) {
2N/A aifp = malloc(sizeof (ipadm_if_info_t));
2N/A if (aifp == NULL) {
2N/A status = ipadm_errno2status(errno);
2N/A goto fail;
2N/A }
2N/A *aifp = *pifp;
2N/A aifp->ifi_next = NULL;
2N/A aifp->ifi_state = IFIS_DISABLED;
2N/A if (last != NULL)
2N/A last->ifi_next = aifp;
2N/A else
2N/A aifinfo = aifp;
2N/A last = aifp;
2N/A }
2N/A }
2N/A *if_info = aifinfo;
2N/A ipadm_free_if_info(pifinfo);
2N/A return (IPADM_SUCCESS);
2N/Afail:
2N/A *if_info = NULL;
2N/A ipadm_free_if_info(aifinfo);
2N/A ipadm_free_if_info(pifinfo);
2N/A return (status);
2N/A}
2N/A
2N/Aint
2N/Ai_ipadm_get_lnum(const char *ifname)
2N/A{
2N/A char *num = strrchr(ifname, IPADM_LOGICAL_SEP);
2N/A
2N/A if (num == NULL)
2N/A return (0);
2N/A
2N/A return (atoi(++num));
2N/A}
2N/A
2N/A/*
2N/A * Sets the output argument `exists' to true or false based on whether
2N/A * any persistent configuration is available for `ifname' and returns
2N/A * IPADM_SUCCESS as status. If the persistent information cannot be retrieved,
2N/A * `exists' is unmodified and an error status is returned.
2N/A */
2N/Aipadm_status_t
2N/Ai_ipadm_if_pexists(ipadm_handle_t iph, const char *ifname, sa_family_t af,
2N/A boolean_t *exists)
2N/A{
2N/A ipadm_if_info_t *ifinfo;
2N/A ipadm_status_t status;
2N/A
2N/A status = i_ipadm_persist_if_info(iph, ifname, &ifinfo);
2N/A if (status == IPADM_SUCCESS) {
2N/A *exists = ((af == AF_INET &&
2N/A (ifinfo->ifi_pflags & IFIF_IPV4)) ||
2N/A (af == AF_INET6 &&
2N/A (ifinfo->ifi_pflags & IFIF_IPV6)));
2N/A free(ifinfo);
2N/A } else if (status == IPADM_NOTFOUND) {
2N/A status = IPADM_SUCCESS;
2N/A *exists = B_FALSE;
2N/A }
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Open "/dev/udp{,6}" for use as a multiplexor to PLINK the interface stream
2N/A * under. We use "/dev/udp" instead of "/dev/ip" since STREAMS will not let
2N/A * you PLINK a driver under itself, and "/dev/ip" is typically the driver at
2N/A * the bottom of the stream for tunneling interfaces.
2N/A */
2N/Aipadm_status_t
2N/Aipadm_open_arp_on_udp(const char *udp_dev_name, int *fd)
2N/A{
2N/A int err;
2N/A
2N/A if ((*fd = open(udp_dev_name, O_RDWR)) == -1)
2N/A return (ipadm_errno2status(errno));
2N/A
2N/A /*
2N/A * Pop off all undesired modules (note that the user may have
2N/A * configured autopush to add modules above udp), and push the
2N/A * arp module onto the resulting stream. This is used to make
2N/A * IP+ARP be able to atomically track the muxid for the I_PLINKed
2N/A * STREAMS, thus it isn't related to ARP running the ARP protocol.
2N/A */
2N/A while (ioctl(*fd, I_POP, 0) != -1)
2N/A ;
2N/A if (errno == EINVAL && ioctl(*fd, I_PUSH, ARP_MOD_NAME) != -1)
2N/A return (IPADM_SUCCESS);
2N/A err = errno;
2N/A (void) close(*fd);
2N/A
2N/A return (ipadm_errno2status(err));
2N/A}
2N/A
2N/A/*
2N/A * i_ipadm_create_ipmp() is called from i_ipadm_create_ipmp_peer() when an
2N/A * underlying interface in an ipmp group G is plumbed for an address family,
2N/A * but the meta-interface for the other address family `af' does not exist
2N/A * yet for the group G. If `af' is IPv6, we need to bring up the
2N/A * link-local address.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_create_ipmp(ipadm_handle_t iph, char *ifname, sa_family_t af,
2N/A const char *grname, uint32_t ipadm_flags)
2N/A{
2N/A ipadm_status_t status;
2N/A struct lifreq lifr;
2N/A int sock;
2N/A int err;
2N/A
2N/A assert(ipadm_flags & IPADM_OPT_IPMP);
2N/A
2N/A /* Create the ipmp underlying interface */
2N/A status = i_ipadm_create_if(iph, ifname, af, ipadm_flags);
2N/A if (status != IPADM_SUCCESS && status != IPADM_IF_EXISTS)
2N/A return (status);
2N/A
2N/A /*
2N/A * To preserve backward-compatibility, always bring up the link-local
2N/A * address for implicitly-created IPv6 IPMP interfaces.
2N/A */
2N/A if (af == AF_INET6)
2N/A (void) i_ipadm_set_flags(iph, ifname, AF_INET6, IFF_UP, 0);
2N/A
2N/A sock = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
2N/A /*
2N/A * If the caller requested a different group name, issue a
2N/A * SIOCSLIFGROUPNAME on the new IPMP interface.
2N/A */
2N/A bzero(&lifr, sizeof (lifr));
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (strcmp(lifr.lifr_name, grname) != 0) {
2N/A (void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
2N/A if (ioctl(sock, SIOCSLIFGROUPNAME, &lifr) == -1) {
2N/A err = errno;
2N/A /* Remove the interface we created. */
2N/A if (status == IPADM_SUCCESS) {
2N/A (void) i_ipadm_delete_if(iph, ifname, af,
2N/A ipadm_flags);
2N/A }
2N/A return (ipadm_errno2status(err));
2N/A }
2N/A }
2N/A
2N/A return (IPADM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Checks if `ifname' is plumbed and in an IPMP group on its "other" address
2N/A * family. If so, create a matching IPMP group for address family `af'.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_create_ipmp_peer(ipadm_handle_t iph, char *ifname, sa_family_t af)
2N/A{
2N/A lifgroupinfo_t lifgr;
2N/A ipadm_status_t status = IPADM_SUCCESS;
2N/A struct lifreq lifr;
2N/A int other_af_sock;
2N/A
2N/A assert(af == AF_INET || af == AF_INET6);
2N/A
2N/A other_af_sock = (af == AF_INET ? iph->iph_sock6 : iph->iph_sock);
2N/A
2N/A /*
2N/A * iph is the handle for the interface that we are trying to plumb.
2N/A * other_af_sock is the socket for the "other" address family.
2N/A */
2N/A bzero(&lifr, sizeof (lifr));
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (ioctl(other_af_sock, SIOCGLIFGROUPNAME, &lifr) != 0)
2N/A return (IPADM_SUCCESS);
2N/A
2N/A (void) strlcpy(lifgr.gi_grname, lifr.lifr_groupname, LIFGRNAMSIZ);
2N/A if (ioctl(other_af_sock, SIOCGLIFGROUPINFO, &lifgr) != 0)
2N/A return (IPADM_SUCCESS);
2N/A
2N/A /*
2N/A * If `ifname' *is* the IPMP group interface, or if the relevant
2N/A * address family is already configured, then there's nothing to do.
2N/A */
2N/A if (strcmp(lifgr.gi_grifname, ifname) == 0 ||
2N/A (af == AF_INET && lifgr.gi_v4) || (af == AF_INET6 && lifgr.gi_v6)) {
2N/A return (IPADM_SUCCESS);
2N/A }
2N/A
2N/A status = i_ipadm_create_ipmp(iph, lifgr.gi_grifname, af,
2N/A lifgr.gi_grname, IPADM_OPT_ACTIVE|IPADM_OPT_IPMP);
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Issues the ioctl SIOCSLIFNAME to kernel on the given ARP stream fd.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_slifname_arp(char *ifname, uint64_t flags, int fd)
2N/A{
2N/A struct lifreq lifr;
2N/A ifspec_t ifsp;
2N/A
2N/A bzero(&lifr, sizeof (lifr));
2N/A (void) ifparse_ifspec(ifname, &ifsp);
2N/A lifr.lifr_ppa = ifsp.ifsp_ppa;
2N/A lifr.lifr_flags = flags;
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A /*
2N/A * Tell ARP the name and unit number for this interface.
2N/A * Note that arp has no support for transparent ioctls.
2N/A */
2N/A if (i_ipadm_strioctl(fd, SIOCSLIFNAME, (char *)&lifr,
2N/A sizeof (lifr)) == -1) {
2N/A return (ipadm_errno2status(errno));
2N/A }
2N/A return (IPADM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * Issues the ioctl SIOCSLIFNAME to kernel. If IPADM_OPT_GENPPA is set in
2N/A * `ipadm_flags', then a ppa will be generated. `newif' will be updated
2N/A * with the generated ppa.
2N/A */
2N/Astatic ipadm_status_t
2N/Ai_ipadm_slifname(ipadm_handle_t iph, char *ifname, char *newif, uint64_t flags,
2N/A int fd, uint32_t ipadm_flags)
2N/A{
2N/A struct lifreq lifr;
2N/A ipadm_status_t status = IPADM_SUCCESS;
2N/A int err = 0;
2N/A sa_family_t af;
2N/A int ppa;
2N/A ifspec_t ifsp;
2N/A boolean_t valid_if;
2N/A
2N/A bzero(&lifr, sizeof (lifr));
2N/A if (ipadm_flags & IPADM_OPT_GENPPA) {
2N/A /*
2N/A * We'd like to just set lifr_ppa to UINT_MAX and have the
2N/A * kernel pick a PPA. Unfortunately, that would mishandle
2N/A * two cases:
2N/A *
2N/A * 1. If the PPA is available but the groupname is taken
2N/A * (e.g., the "ipmp2" IP interface name is available
2N/A * but the "ipmp2" groupname is taken) then the
2N/A * auto-assignment by the kernel will fail.
2N/A *
2N/A * 2. If we're creating (e.g.) an IPv6-only IPMP
2N/A * interface, and there's already an IPv4-only IPMP
2N/A * interface, the kernel will allow us to accidentally
2N/A * reuse the IPv6 IPMP interface name (since
2N/A * SIOCSLIFNAME uniqueness is per-interface-type).
2N/A * This will cause administrative confusion.
2N/A *
2N/A * Thus, we instead take a brute-force approach of checking
2N/A * whether the IPv4 or IPv6 name is already in-use before
2N/A * attempting the SIOCSLIFNAME. As per (1) above, the
2N/A * SIOCSLIFNAME may still fail, in which case we just proceed
2N/A * to the next one. If this approach becomes too slow, we
2N/A * can add a new SIOC* to handle this case in the kernel.
2N/A */
2N/A for (ppa = 0; ppa < UINT_MAX; ppa++) {
2N/A (void) snprintf(lifr.lifr_name, LIFNAMSIZ, "%s%d",
2N/A ifname, ppa);
2N/A
2N/A if (ioctl(iph->iph_sock, SIOCGLIFFLAGS, &lifr) != -1 ||
2N/A errno != ENXIO)
2N/A continue;
2N/A
2N/A if (ioctl(iph->iph_sock6, SIOCGLIFFLAGS, &lifr) != -1 ||
2N/A errno != ENXIO)
2N/A continue;
2N/A
2N/A lifr.lifr_ppa = ppa;
2N/A lifr.lifr_flags = flags;
2N/A
2N/A err = ioctl(fd, SIOCSLIFNAME, &lifr);
2N/A if (err != -1 || errno != EEXIST)
2N/A break;
2N/A }
2N/A if (err == -1) {
2N/A status = ipadm_errno2status(errno);
2N/A } else {
2N/A /*
2N/A * PPA has been successfully established.
2N/A * Update `newif' with the ppa.
2N/A */
2N/A assert(newif != NULL);
2N/A if (snprintf(newif, LIFNAMSIZ, "%s%d", ifname,
2N/A ppa) >= LIFNAMSIZ)
2N/A return (IPADM_INVALID_ARG);
2N/A }
2N/A } else {
2N/A /* We should have already validated the interface name. */
2N/A valid_if = ifparse_ifspec(ifname, &ifsp);
2N/A assert(valid_if);
2N/A
2N/A /*
2N/A * Before we call SIOCSLIFNAME, ensure that the IPMP group
2N/A * interface for this address family exists. Otherwise, the
2N/A * kernel will kick the interface out of the group when we do
2N/A * the SIOCSLIFNAME.
2N/A *
2N/A * Example: suppose bge0 is plumbed for IPv4 and in group "a".
2N/A * If we're now plumbing bge0 for IPv6, but the IPMP group
2N/A * interface for "a" is not plumbed for IPv6, the SIOCSLIFNAME
2N/A * will kick bge0 out of group "a", which is undesired.
2N/A */
2N/A if (flags & IFF_IPV4)
2N/A af = AF_INET;
2N/A else
2N/A af = AF_INET6;
2N/A status = i_ipadm_create_ipmp_peer(iph, ifname, af);
2N/A if (status != IPADM_SUCCESS)
2N/A return (status);
2N/A lifr.lifr_ppa = ifsp.ifsp_ppa;
2N/A lifr.lifr_flags = flags;
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (ioctl(fd, SIOCSLIFNAME, &lifr) == -1)
2N/A status = ipadm_errno2status(errno);
2N/A }
2N/A
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Plumbs the interface `ifname' for the address family `af'. It also persists
2N/A * the interface for `af' if IPADM_OPT_PERSIST is set in `ipadm_flags'.
2N/A */
2N/Aipadm_status_t
2N/Ai_ipadm_plumb_if(ipadm_handle_t iph, char *ifname, sa_family_t af,
2N/A uint32_t ipadm_flags)
2N/A{
2N/A int ip_muxid;
2N/A int mux_fd = -1, ip_fd, arp_fd;
2N/A char *udp_dev_name;
2N/A dlpi_handle_t dh_arp = NULL, dh_ip;
2N/A uint64_t ifflags;
2N/A struct lifreq lifr;
2N/A uint_t dlpi_flags;
2N/A ipadm_status_t status = IPADM_SUCCESS;
2N/A char *linkname;
2N/A boolean_t legacy = (iph->iph_flags & IPH_LEGACY);
2N/A zoneid_t zoneid;
2N/A char newif[LIFNAMSIZ];
2N/A char lifname[LIFNAMSIZ];
2N/A datalink_id_t linkid;
2N/A int sock;
2N/A boolean_t islo;
2N/A boolean_t is_persistent =
2N/A ((ipadm_flags & IPADM_OPT_PERSIST) != 0);
2N/A uint32_t dlflags;
2N/A dladm_status_t dlstatus;
2N/A
2N/A if (iph->iph_dlh != NULL) {
2N/A dlstatus = dladm_name2info(iph->iph_dlh, ifname, &linkid,
2N/A &dlflags, NULL, NULL);
2N/A }
2N/A /*
2N/A * If we're in the global zone and we're plumbing a datalink, make
2N/A * sure that the datalink is not assigned to a non-global zone. Note
2N/A * that the non-global zones don't need this check, because zoneadm
2N/A * has taken care of this when the zones boot.
2N/A */
2N/A if (getzoneid() == GLOBAL_ZONEID && dlstatus == DLADM_STATUS_OK) {
2N/A zoneid = ALL_ZONES;
2N/A if (zone_check_datalink(&zoneid, linkid) == 0) {
2N/A /* interface is in use by a non-global zone. */
2N/A return (IPADM_IF_INUSE);
2N/A }
2N/A }
2N/A
2N/A /* loopback interfaces are just added as logical interface */
2N/A bzero(&lifr, sizeof (lifr));
2N/A islo = i_ipadm_is_loopback(ifname);
2N/A if (islo || i_ipadm_get_lnum(ifname) != 0) {
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (af == AF_INET)
2N/A sock = iph->iph_sock;
2N/A else
2N/A sock = iph->iph_sock6;
2N/A if (islo && ioctl(sock, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
2N/A return (IPADM_IF_EXISTS);
2N/A if (ioctl(sock, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
2N/A return (ipadm_errno2status(errno));
2N/A
2N/A /*
2N/A * By default, kernel configures 127.0.0.1 on the loopback
2N/A * interface. Replace this with 0.0.0.0 to be consistent
2N/A * with interface creation on other physical interfaces.
2N/A */
2N/A if (islo && !legacy) {
2N/A bzero(&lifr.lifr_addr, sizeof (lifr.lifr_addr));
2N/A lifr.lifr_addr.ss_family = af;
2N/A if (ioctl(sock, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
2N/A return (ipadm_errno2status(errno));
2N/A if (is_persistent) {
2N/A status = i_ipadm_persist_if(iph, ifname, af);
2N/A if (status != IPADM_SUCCESS) {
2N/A (void) i_ipadm_delete_if(iph, ifname,
2N/A af, IPADM_OPT_ACTIVE);
2N/A }
2N/A }
2N/A }
2N/A return (status);
2N/A }
2N/A
2N/A dlpi_flags = DLPI_NOATTACH;
2N/A
2N/A /*
2N/A * If IPADM_OPT_IPMP is specified, then this is a request
2N/A * to create an IPMP interface atop /dev/ipmpstub0. (We can't simply
2N/A * pass "ipmpstub0" as devname since an admin *could* have a normal
2N/A * vanity-named link named "ipmpstub0" that they'd like to plumb.)
2N/A */
2N/A if (ipadm_flags & IPADM_OPT_IPMP) {
2N/A dlpi_flags |= DLPI_DEVONLY;
2N/A linkname = "ipmpstub0";
2N/A } else {
2N/A /*
2N/A * Verify that the user is not creating a persistent
2N/A * IP interface on a non-persistent data-link.
2N/A */
2N/A if (!i_ipadm_is_vni(ifname) && dlstatus == DLADM_STATUS_OK &&
2N/A is_persistent && !(dlflags & DLADM_OPT_PERSIST)) {
2N/A return (IPADM_TEMPORARY_OBJ);
2N/A }
2N/A linkname = ifname;
2N/A }
2N/A
2N/A /*
2N/A * We use DLPI_NOATTACH because the ip module will do the attach
2N/A * itself for DLPI style-2 devices.
2N/A */
2N/A if (dlpi_open(linkname, &dh_ip, dlpi_flags) != DLPI_SUCCESS)
2N/A return (IPADM_DLPI_FAILURE);
2N/A ip_fd = dlpi_fd(dh_ip);
2N/A if (ioctl(ip_fd, I_PUSH, IP_MOD_NAME) == -1) {
2N/A status = ipadm_errno2status(errno);
2N/A goto done;
2N/A }
2N/A
2N/A /*
2N/A * Set IFF_IPV4/IFF_IPV6 flags. The kernel only allows modifications
2N/A * to IFF_IPv4, IFF_IPV6, IFF_BROADCAST, IFF_XRESOLV, IFF_NOLINKLOCAL.
2N/A */
2N/A ifflags = 0;
2N/A
2N/A /* Set the name string and the IFF_IPV* flag */
2N/A if (af == AF_INET) {
2N/A ifflags = IFF_IPV4;
2N/A } else {
2N/A ifflags = IFF_IPV6;
2N/A /*
2N/A * With the legacy method, the link-local address should be
2N/A * configured as part of the interface plumb, using the default
2N/A * token. If IPH_LEGACY is not specified, we want to set :: as
2N/A * the address and require the admin to explicitly call
2N/A * ipadm_create_addr() with the address object type set to
2N/A * IPADM_ADDR_IPV6_ADDRCONF to create the link-local address
2N/A * as well as the autoconfigured addresses.
2N/A */
2N/A if (!legacy && !i_ipadm_is_6to4(iph, ifname))
2N/A ifflags |= IFF_NOLINKLOCAL;
2N/A }
2N/A (void) strlcpy(newif, ifname, sizeof (newif));
2N/A status = i_ipadm_slifname(iph, ifname, newif, ifflags, ip_fd,
2N/A ipadm_flags);
2N/A if (status != IPADM_SUCCESS)
2N/A goto done;
2N/A
2N/A /* Get the full set of existing flags for this stream */
2N/A status = i_ipadm_get_flags(iph, newif, af, &ifflags);
2N/A if (status != IPADM_SUCCESS)
2N/A goto done;
2N/A
2N/A udp_dev_name = (af == AF_INET6 ? UDP6_DEV_NAME : UDP_DEV_NAME);
2N/A status = ipadm_open_arp_on_udp(udp_dev_name, &mux_fd);
2N/A if (status != IPADM_SUCCESS)
2N/A goto done;
2N/A
2N/A /* Check if arp is not needed */
2N/A if (ifflags & (IFF_NOARP|IFF_IPV6)) {
2N/A /*
2N/A * PLINK the interface stream so that the application can exit
2N/A * without tearing down the stream.
2N/A */
2N/A if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1)
2N/A status = ipadm_errno2status(errno);
2N/A goto done;
2N/A }
2N/A
2N/A /*
2N/A * This interface does use ARP, so set up a separate stream
2N/A * from the interface to ARP.
2N/A *
2N/A * We use DLPI_NOATTACH because the arp module will do the attach
2N/A * itself for DLPI style-2 devices.
2N/A */
2N/A if (dlpi_open(linkname, &dh_arp, dlpi_flags) != DLPI_SUCCESS) {
2N/A status = IPADM_DLPI_FAILURE;
2N/A goto done;
2N/A }
2N/A
2N/A arp_fd = dlpi_fd(dh_arp);
2N/A if (ioctl(arp_fd, I_PUSH, ARP_MOD_NAME) == -1) {
2N/A status = ipadm_errno2status(errno);
2N/A goto done;
2N/A }
2N/A
2N/A status = i_ipadm_slifname_arp(newif, ifflags, arp_fd);
2N/A if (status != IPADM_SUCCESS)
2N/A goto done;
2N/A /*
2N/A * PLINK the IP and ARP streams so that ifconfig can exit
2N/A * without tearing down the stream.
2N/A */
2N/A if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1) {
2N/A status = ipadm_errno2status(errno);
2N/A goto done;
2N/A }
2N/A
2N/A if (ioctl(mux_fd, I_PLINK, arp_fd) < 0) {
2N/A status = ipadm_errno2status(errno);
2N/A (void) ioctl(mux_fd, I_PUNLINK, ip_muxid);
2N/A }
2N/A
2N/Adone:
2N/A dlpi_close(dh_ip);
2N/A if (dh_arp != NULL)
2N/A dlpi_close(dh_arp);
2N/A
2N/A if (mux_fd != -1)
2N/A (void) close(mux_fd);
2N/A
2N/A if (status == IPADM_SUCCESS) {
2N/A /* copy back new ifname */
2N/A (void) strlcpy(ifname, newif, LIFNAMSIZ);
2N/A /*
2N/A * If it is a 6to4 tunnel, create a default
2N/A * addrobj name for the default address on the 0'th
2N/A * logical interface and set IFF_UP in the interface flags.
2N/A */
2N/A if (i_ipadm_is_6to4(iph, ifname)) {
2N/A struct ipadm_addrobj_s addr;
2N/A
2N/A i_ipadm_init_addr(&addr, ifname, "", IPADM_ADDR_STATIC);
2N/A addr.ipadm_af = af;
2N/A status = i_ipadm_lookupadd_addrobj(iph, &addr);
2N/A if (status != IPADM_SUCCESS)
2N/A return (status);
2N/A status = ipadm_add_aobjname(iph, ifname,
2N/A af, addr.ipadm_aobjname, IPADM_ADDR_STATIC, 0);
2N/A if (status != IPADM_SUCCESS)
2N/A return (status);
2N/A addr.ipadm_lifnum = 0;
2N/A i_ipadm_addrobj2lifname(&addr, lifname,
2N/A sizeof (lifname));
2N/A status = i_ipadm_set_flags(iph, lifname, af,
2N/A IFF_UP, 0);
2N/A if (status != IPADM_SUCCESS)
2N/A return (status);
2N/A } else {
2N/A /*
2N/A * Prevent static IPv6 addresses from triggering
2N/A * autoconf. This does not have to be done for
2N/A * 6to4 tunnel interfaces, since in.ndpd will
2N/A * not autoconfigure those interfaces.
2N/A */
2N/A if (af == AF_INET6 && !legacy)
2N/A (void) i_ipadm_disable_autoconf(newif);
2N/A }
2N/A
2N/A /*
2N/A * If IPADM_OPT_PERSIST was set in flags, store the
2N/A * interface in persistent DB.
2N/A */
2N/A if (is_persistent) {
2N/A status = i_ipadm_persist_if(iph, newif, af);
2N/A if (status != IPADM_SUCCESS) {
2N/A (void) i_ipadm_delete_if(iph, newif, af,
2N/A IPADM_OPT_ACTIVE);
2N/A }
2N/A }
2N/A }
2N/A if (status == IPADM_EXISTS)
2N/A status = IPADM_IF_EXISTS;
2N/A return (status);
2N/A}
2N/A
2N/A/*
2N/A * Unplumbs the interface in `ifname' of family `af'.
2N/A */
2N/Aipadm_status_t
2N/Ai_ipadm_unplumb_if(ipadm_handle_t iph, const char *ifname, sa_family_t af)
2N/A{
2N/A int ip_muxid, arp_muxid;
2N/A int mux_fd = -1;
2N/A int muxid_fd = -1;
2N/A char *udp_dev_name;
2N/A uint64_t flags;
2N/A boolean_t changed_arp_muxid = B_FALSE;
2N/A int save_errno;
2N/A struct lifreq lifr;
2N/A ipadm_status_t ret = IPADM_SUCCESS;
2N/A int sock;
2N/A lifgroupinfo_t lifgr;
2N/A ifaddrlistx_t *ifaddrs, *ifaddrp;
2N/A boolean_t v6 = (af == AF_INET6);
2N/A
2N/A /* Just do SIOCLIFREMOVEIF on loopback interfaces */
2N/A bzero(&lifr, sizeof (lifr));
2N/A if (i_ipadm_is_loopback(ifname) ||
2N/A (i_ipadm_get_lnum(ifname) != 0 && (iph->iph_flags & IPH_LEGACY))) {
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (ioctl((af == AF_INET) ? iph->iph_sock : iph->iph_sock6,
2N/A SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
2N/A return (ipadm_errno2status(errno));
2N/A }
2N/A return (IPADM_SUCCESS);
2N/A }
2N/A
2N/A /*
2N/A * We used /dev/udp or udp6 to set up the mux. So we have to use
2N/A * the same now for PUNLINK also.
2N/A */
2N/A if (v6) {
2N/A udp_dev_name = UDP6_DEV_NAME;
2N/A sock = iph->iph_sock6;
2N/A } else {
2N/A udp_dev_name = UDP_DEV_NAME;
2N/A sock = iph->iph_sock;
2N/A }
2N/A if ((muxid_fd = open(udp_dev_name, O_RDWR)) == -1) {
2N/A ret = ipadm_errno2status(errno);
2N/A goto done;
2N/A }
2N/A ret = ipadm_open_arp_on_udp(udp_dev_name, &mux_fd);
2N/A if (ret != IPADM_SUCCESS)
2N/A goto done;
2N/A (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2N/A if (ioctl(muxid_fd, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
ret = ipadm_errno2status(errno);
goto done;
}
flags = lifr.lifr_flags;
again:
if (flags & IFF_IPMP) {
/*
* There are two reasons the I_PUNLINK can fail with EBUSY:
* (1) if IP interfaces are in the group, or (2) if IPMP data
* addresses are administratively up. For case (1), we fail
* here with a specific error message. For case (2), we bring
* down the addresses prior to doing the I_PUNLINK. If the
* I_PUNLINK still fails with EBUSY then the configuration
* must have changed after our checks, in which case we branch
* back up to `again' and rerun this logic. The net effect is
* that unplumbing an IPMP interface will only fail with EBUSY
* if IP interfaces are in the group.
*/
if (ioctl(sock, SIOCGLIFGROUPNAME, &lifr) == -1) {
ret = ipadm_errno2status(errno);
goto done;
}
(void) strlcpy(lifgr.gi_grname, lifr.lifr_groupname,
LIFGRNAMSIZ);
if (ioctl(sock, SIOCGLIFGROUPINFO, &lifgr) == -1) {
ret = ipadm_errno2status(errno);
goto done;
}
if ((v6 && lifgr.gi_nv6 != 0) || (!v6 && lifgr.gi_nv4 != 0)) {
ret = IPADM_GRP_NOTEMPTY;
goto done;
}
/*
* The kernel will fail the I_PUNLINK if the IPMP interface
* has administratively up addresses; bring them down.
*/
if (ifaddrlistx(ifname, IFF_UP|IFF_DUPLICATE,
0, &ifaddrs) == -1) {
ret = ipadm_errno2status(errno);
goto done;
}
ifaddrp = ifaddrs;
for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
int sock = (ifaddrp->ia_flags & IFF_IPV4) ?
iph->iph_sock : iph->iph_sock6;
struct lifreq lifrl;
if (((ifaddrp->ia_flags & IFF_IPV6) && !v6) ||
(!(ifaddrp->ia_flags & IFF_IPV6) && v6))
continue;
bzero(&lifrl, sizeof (lifrl));
(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name,
sizeof (lifrl.lifr_name));
if (ioctl(sock, SIOCGLIFFLAGS, &lifrl) < 0) {
ret = ipadm_errno2status(errno);
ifaddrlistx_free(ifaddrs);
goto done;
}
if (lifrl.lifr_flags & IFF_UP) {
ret = i_ipadm_set_flags(iph, lifrl.lifr_name,
((lifrl.lifr_flags & IFF_IPV4) ? AF_INET :
AF_INET6), 0, IFF_UP);
if (ret != IPADM_SUCCESS) {
ifaddrlistx_free(ifaddrs);
goto done;
}
} else if (lifrl.lifr_flags & IFF_DUPLICATE) {
if (ioctl(sock, SIOCGLIFADDR, &lifrl) < 0 ||
ioctl(sock, SIOCSLIFADDR, &lifrl) < 0) {
ret = ipadm_errno2status(errno);
ifaddrlistx_free(ifaddrs);
goto done;
}
}
}
ifaddrlistx_free(ifaddrs);
}
if (ioctl(muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
ret = ipadm_errno2status(errno);
goto done;
}
arp_muxid = lifr.lifr_arp_muxid;
ip_muxid = lifr.lifr_ip_muxid;
/*
* We don't have a good way of knowing whether the arp stream is
* plumbed. We can't rely on IFF_NOARP because someone could
* have turned it off later using "ifconfig xxx -arp".
*/
if (arp_muxid != 0) {
if (ioctl(mux_fd, I_PUNLINK, arp_muxid) < 0) {
/*
* See the comment before the SIOCGLIFGROUPNAME call.
*/
if (errno == EBUSY && (flags & IFF_IPMP))
goto again;
if ((errno == EINVAL) &&
(flags & (IFF_NOARP | IFF_IPV6))) {
/*
* Some plumbing utilities set the muxid to
* -1 or some invalid value to signify that
* there is no arp stream. Set the muxid to 0
* before trying to unplumb the IP stream.
* IP does not allow the IP stream to be
* unplumbed if it sees a non-null arp muxid,
* for consistency of IP-ARP streams.
*/
lifr.lifr_arp_muxid = 0;
(void) ioctl(muxid_fd, SIOCSLIFMUXID,
(caddr_t)&lifr);
changed_arp_muxid = B_TRUE;
}
/*
* In case of any other error, we continue with
* the unplumb.
*/
}
}
if (ioctl(mux_fd, I_PUNLINK, ip_muxid) < 0) {
if (changed_arp_muxid) {
/*
* Some error occurred, and we need to restore
* everything back to what it was.
*/
save_errno = errno;
lifr.lifr_arp_muxid = arp_muxid;
lifr.lifr_ip_muxid = ip_muxid;
(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
errno = save_errno;
}
/*
* See the comment before the SIOCGLIFGROUPNAME call.
*/
if (errno == EBUSY && (flags & IFF_IPMP))
goto again;
ret = ipadm_errno2status(errno);
}
done:
if (muxid_fd != -1)
(void) close(muxid_fd);
if (mux_fd != -1)
(void) close(mux_fd);
if (af == AF_INET6 && ret == IPADM_SUCCESS) {
/*
* in.ndpd maintains the phyints in its memory even after
* the interface is plumbed, so that it can be reused when
* the interface gets plumbed again. The default behavior
* of in.ndpd is to start autoconfiguration for an interface
* that gets plumbed. We need to send the
* message IPADM_ENABLE_AUTOCONF to in.ndpd to restore this
* default behavior on replumb.
*/
(void) i_ipadm_enable_autoconf(ifname);
}
return (ret);
}
/*
* Saves the given interface name `ifname' with address family `af' in
* persistent DB.
*/
static ipadm_status_t
i_ipadm_persist_if(ipadm_handle_t iph, const char *ifname, sa_family_t af)
{
ipmgmt_if_arg_t ifarg;
int err;
(void) strlcpy(ifarg.ia_ifname, ifname, sizeof (ifarg.ia_ifname));
ifarg.ia_family = af;
ifarg.ia_cmd = IPMGMT_CMD_SETIF;
ifarg.ia_flags = IPMGMT_PERSIST;
err = ipadm_door_call(iph, &ifarg, sizeof (ifarg), NULL, 0, B_FALSE);
return (ipadm_errno2status(err));
}
/*
* Remove the IP interface from active configuration. If IPADM_OPT_PERSIST
* is set in `ipadm_flags', it is also removed from persistent configuration.
*/
ipadm_status_t
i_ipadm_delete_if(ipadm_handle_t iph, const char *ifname, sa_family_t af,
uint32_t ipadm_flags)
{
ipadm_status_t ret = IPADM_SUCCESS;
ipadm_status_t db_status;
char tmp_ifname[LIFNAMSIZ];
char *cp;
struct ipadm_addrobj_s ipaddr;
boolean_t is_persistent =
(ipadm_flags & IPADM_OPT_PERSIST);
ret = i_ipadm_unplumb_if(iph, ifname, af);
if (ret != IPADM_SUCCESS)
goto done;
cp = strrchr(ifname, IPADM_LOGICAL_SEP);
if (cp != NULL) {
assert(iph->iph_flags & IPH_LEGACY);
/*
* This is a non-zero logical interface.
* Find the addrobj and remove it from the daemon's memory.
*/
(void) strlcpy(tmp_ifname, ifname, sizeof (tmp_ifname));
tmp_ifname[cp - ifname] = '\0';
*cp++ = '\0';
ipaddr.ipadm_lifnum = atoi(cp);
(void) strlcpy(ipaddr.ipadm_ifname, tmp_ifname,
sizeof (ipaddr.ipadm_ifname));
ipaddr.ipadm_af = af;
ret = i_ipadm_get_lif2addrobj(iph, &ipaddr);
if (ret == IPADM_SUCCESS) {
ret = i_ipadm_delete_addrobj(iph, &ipaddr,
IPADM_OPT_ACTIVE);
} else if (ret == IPADM_NOTFOUND) {
ret = IPADM_SUCCESS;
}
return (ret);
}
done:
/*
* Even if interface does not exist, remove all its addresses and
* properties from the persistent store. If interface does not
* exist both in kernel and the persistent store, return IPADM_ENXIO.
*/
if ((ret == IPADM_ENXIO && is_persistent) || ret == IPADM_SUCCESS) {
db_status = i_ipadm_delete_ifobj(iph, ifname, af,
is_persistent);
if (db_status == IPADM_SUCCESS)
ret = IPADM_SUCCESS;
}
return (ret);
}
/*
* Resets all addresses on interface `ifname' with address family `af'
* from ipmgmtd daemon. If is_persistent = B_TRUE, all interface properties
* and address objects of `ifname' for `af' are also removed from the
* persistent DB.
*/
ipadm_status_t
i_ipadm_delete_ifobj(ipadm_handle_t iph, const char *ifname, sa_family_t af,
boolean_t is_persistent)
{
ipmgmt_if_arg_t ifarg;
int err;
ifarg.ia_cmd = IPMGMT_CMD_RESETIF;
ifarg.ia_flags = IPMGMT_ACTIVE;
if (is_persistent)
ifarg.ia_flags |= IPMGMT_PERSIST;
ifarg.ia_family = af;
(void) strlcpy(ifarg.ia_ifname, ifname, LIFNAMSIZ);
err = ipadm_door_call(iph, &ifarg, sizeof (ifarg), NULL, 0, B_FALSE);
return (ipadm_errno2status(err));
}
/*
* Create the interface by plumbing it for IP.
* This function will check if there is saved configuration information
* for `ifname' and return IPADM_OP_DISABLE_OBJ if the name-space
* for `ifname' is taken.
*/
ipadm_status_t
i_ipadm_create_if(ipadm_handle_t iph, char *ifname, sa_family_t af,
uint32_t ipadm_flags)
{
ipadm_status_t status;
boolean_t p_exists;
sa_family_t other_af;
/*
* Return error, if the interface already exists in either the active
* or the persistent configuration.
*/
if (ipadm_if_enabled(iph, ifname, af))
return (IPADM_IF_EXISTS);
status = i_ipadm_if_pexists(iph, ifname, af, &p_exists);
if (status != IPADM_SUCCESS)
return (status);
other_af = (af == AF_INET ? AF_INET6 : AF_INET);
if (p_exists) {
if (!ipadm_if_enabled(iph, ifname, other_af))
return (IPADM_OP_DISABLE_OBJ);
else
ipadm_flags &= ~IPADM_OPT_PERSIST;
}
return (i_ipadm_plumb_if(iph, ifname, af, ipadm_flags));
}
/*
* Plumbs an interface. Creates both IPv4 and IPv6 interfaces by
* default, unless a value in `af' is specified. The interface may be plumbed
* only if there is no previously saved persistent configuration information
* for the interface (in which case the ipadm_enable_if() function must
* be used to enable the interface).
*
* Returns: IPADM_SUCCESS, IPADM_FAILURE, IPADM_IF_EXISTS,
* IPADM_IF_PERSIST_EXISTS, IPADM_DLPI_FAILURE,
* or appropriate ipadm_status_t corresponding to the errno.
*
* `ifname' must point to memory that can hold upto LIFNAMSIZ chars. It may
* be over-written with the actual interface name when a PPA has to be
* internally generated by the library.
*/
ipadm_status_t
ipadm_create_if(ipadm_handle_t iph, char *ifname, sa_family_t af,
uint32_t flags)
{
ipadm_status_t status;
boolean_t created_v4 = B_FALSE;
char newifname[LIFNAMSIZ];
/* Check for the required authorization */
if (!ipadm_check_auth())
return (IPADM_EAUTH);
if (flags == 0 || ((flags & IPADM_OPT_PERSIST) &&
!(flags & IPADM_OPT_ACTIVE)) ||
(flags & ~(IPADM_COMMON_OPT_MASK | IPADM_OPT_IPMP |
IPADM_OPT_GENPPA))) {
return (IPADM_INVALID_ARG);
}
if (flags & IPADM_OPT_GENPPA) {
if (snprintf(newifname, LIFNAMSIZ, "%s0", ifname) >=
LIFNAMSIZ)
return (IPADM_INVALID_ARG);
} else {
if (strlcpy(newifname, ifname, LIFNAMSIZ) >= LIFNAMSIZ)
return (IPADM_INVALID_ARG);
}
if (!i_ipadm_validate_ifname(iph, newifname))
return (IPADM_INVALID_ARG);
if ((af == AF_INET || af == AF_UNSPEC) &&
!i_ipadm_is_6to4(iph, ifname)) {
status = i_ipadm_create_if(iph, ifname, AF_INET, flags);
if (status != IPADM_SUCCESS)
return (status);
created_v4 = B_TRUE;
}
if (af == AF_INET6 || af == AF_UNSPEC) {
status = i_ipadm_create_if(iph, ifname, AF_INET6, flags);
if (status != IPADM_SUCCESS) {
if (created_v4) {
(void) i_ipadm_delete_if(iph, ifname, AF_INET,
IPADM_OPT_ACTIVE);
}
return (status);
}
}
return (IPADM_SUCCESS);
}
/*
* Deletes the interface in `ifname'. Removes both IPv4 and IPv6 interfaces
* when `af' = AF_UNSPEC.
*/
ipadm_status_t
ipadm_delete_if(ipadm_handle_t iph, const char *ifname, sa_family_t af,
uint32_t flags)
{
ipadm_status_t status1 = IPADM_SUCCESS;
ipadm_status_t status2 = IPADM_SUCCESS;
ipadm_status_t other;
/* Check for the required authorization */
if (!ipadm_check_auth())
return (IPADM_EAUTH);
/* Validate the `ifname' for any logical interface. */
if (flags == 0 || (flags & ~(IPADM_COMMON_OPT_MASK)) ||
!i_ipadm_validate_ifname(iph, ifname))
return (IPADM_INVALID_ARG);
if (af == AF_INET || af == AF_UNSPEC)
status1 = i_ipadm_delete_if(iph, ifname, AF_INET, flags);
if (af == AF_INET6 || af == AF_UNSPEC)
status2 = i_ipadm_delete_if(iph, ifname, AF_INET6, flags);
/*
* If the family has been uniquely identified, we return the
* associated status, even if that is ENXIO. Calls from ifconfig
* which can only unplumb one of IPv4/IPv6 at any time fall under
* this category.
*/
if (af == AF_INET)
return (status1);
else if (af == AF_INET6)
return (status2);
else if (af != AF_UNSPEC)
return (IPADM_INVALID_ARG);
/*
* If af is AF_UNSPEC, then we return the following:
* status1, if status1 == status2
* IPADM_SUCCESS, if either of status1 or status2 is SUCCESS
* and the other status is ENXIO
* IPADM_ENXIO, if both status1 and status2 are ENXIO
* IPADM_FAILURE otherwise.
*/
if (status1 == status2) {
/* covers the case when both status1 and status2 are ENXIO */
return (status1);
} else if (status1 == IPADM_SUCCESS || status2 == IPADM_SUCCESS) {
if (status1 == IPADM_SUCCESS)
other = status2;
else
other = status1;
return (other == IPADM_ENXIO ? IPADM_SUCCESS : IPADM_FAILURE);
} else {
return (IPADM_FAILURE);
}
}
/*
* Returns information about all interfaces in both active and persistent
* configuration. If `ifname' is not NULL, it returns only the interface
* identified by `ifname'.
*
* Return values:
* On success: IPADM_SUCCESS.
* On error : IPADM_INVALID_ARG, IPADM_ENXIO or IPADM_FAILURE.
*/
ipadm_status_t
ipadm_if_info(ipadm_handle_t iph, const char *ifname,
ipadm_if_info_t **if_info, uint32_t flags, int64_t lifc_flags)
{
ipadm_status_t status;
ifspec_t ifsp;
if (if_info == NULL || iph == NULL || flags != 0)
return (IPADM_INVALID_ARG);
if (ifname != NULL &&
(!ifparse_ifspec(ifname, &ifsp) || ifsp.ifsp_lunvalid)) {
return (IPADM_INVALID_ARG);
}
status = i_ipadm_get_all_if_info(iph, ifname, if_info, lifc_flags);
if (status != IPADM_SUCCESS)
return (status);
if (ifname != NULL && *if_info == NULL)
return (IPADM_ENXIO);
return (IPADM_SUCCESS);
}
/*
* Frees the linked list allocated by ipadm_if_info().
*/
void
ipadm_free_if_info(ipadm_if_info_t *ifinfo)
{
ipadm_if_info_t *ifinfo_next;
for (; ifinfo != NULL; ifinfo = ifinfo_next) {
ifinfo_next = ifinfo->ifi_next;
free(ifinfo);
}
}
/*
* Re-enable the interface `ifname' based on the saved configuration
* for `ifname'.
*/
ipadm_status_t
ipadm_enable_if(ipadm_handle_t iph, const char *ifname, uint32_t flags)
{
nvlist_t *ifnvl;
ipadm_status_t status;
ifspec_t ifsp;
/* Check for the required authorization */
if (!ipadm_check_auth())
return (IPADM_EAUTH);
/* Check for logical interfaces. */
if (!ifparse_ifspec(ifname, &ifsp) || ifsp.ifsp_lunvalid)
return (IPADM_INVALID_ARG);
/* Enabling an interface persistently is not supported. */
if (flags & IPADM_OPT_PERSIST)
return (IPADM_NOTSUP);
/*
* Return early by checking if the interface is already enabled.
*/
if (ipadm_if_enabled(iph, ifname, AF_INET) &&
ipadm_if_enabled(iph, ifname, AF_INET6)) {
return (IPADM_IF_EXISTS);
}
/*
* Enable the interface and restore all its interface properties
* and address objects.
*/
status = i_ipadm_init_ifs(iph, ifname, &ifnvl);
if (status != IPADM_SUCCESS)
return (status);
assert(ifnvl != NULL);
/*
* ipadm_enable_if() does exactly what ipadm_init_ifs() does,
* but only for one interface. We need to set IPH_INIT because
* ipmgmtd daemon does not have to write the interface to persistent
* db. The interface is already available in persistent db
* and we are here to re-enable the persistent configuration.
*/
iph->iph_flags |= IPH_INIT;
status = i_ipadm_init_ifobj(iph, ifname, ifnvl);
iph->iph_flags &= ~IPH_INIT;
return (status);
}
/*
* Disable the interface `ifname' by removing it from the active configuration.
* Error code return values follow the model in ipadm_delete_if()
*/
ipadm_status_t
ipadm_disable_if(ipadm_handle_t iph, const char *ifname, uint32_t flags)
{
ipadm_status_t status1, status2, other;
ifspec_t ifsp;
/* Check for the required authorization */
if (!ipadm_check_auth())
return (IPADM_EAUTH);
/* Check for logical interfaces. */
if (!ifparse_ifspec(ifname, &ifsp) || ifsp.ifsp_lunvalid)
return (IPADM_INVALID_ARG);
/* Disabling an interface persistently is not supported. */
if (flags & IPADM_OPT_PERSIST)
return (IPADM_NOTSUP);
status1 = i_ipadm_unplumb_if(iph, ifname, AF_INET6);
if (status1 == IPADM_SUCCESS)
status1 = i_ipadm_delete_ifobj(iph, ifname, AF_INET6, B_FALSE);
status2 = i_ipadm_unplumb_if(iph, ifname, AF_INET);
if (status2 == IPADM_SUCCESS)
status2 = i_ipadm_delete_ifobj(iph, ifname, AF_INET, B_FALSE);
if (status1 == status2) {
return (status2);
} else if (status1 == IPADM_SUCCESS || status2 == IPADM_SUCCESS) {
if (status1 == IPADM_SUCCESS)
other = status2;
else
other = status1;
return (other == IPADM_ENXIO ? IPADM_SUCCESS : IPADM_FAILURE);
} else {
return (IPADM_FAILURE);
}
}