diff options
| author | Alan Somers <asomers@FreeBSD.org> | 2018-02-23 03:11:43 +0000 |
|---|---|---|
| committer | Alan Somers <asomers@FreeBSD.org> | 2018-02-23 03:11:43 +0000 |
| commit | b1d757bc1dc29cda592d632b955a706215975f1d (patch) | |
| tree | ade1caecfc99c86ddc277bca4a32e057feecaca2 /lib/libifconfig | |
| parent | beaafe4f86876bf5b6ba8a6ff4b9b0f484054214 (diff) | |
Notes
Diffstat (limited to 'lib/libifconfig')
| -rw-r--r-- | lib/libifconfig/Makefile | 6 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig.c | 362 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig.h | 159 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_carp.c | 60 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_inet.c | 98 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_inet6.c | 105 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_internal.c | 30 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_internal.h | 13 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_lagg.c | 107 | ||||
| -rw-r--r-- | lib/libifconfig/libifconfig_media.c | 457 |
10 files changed, 1248 insertions, 149 deletions
diff --git a/lib/libifconfig/Makefile b/lib/libifconfig/Makefile index 9c3116e747b2..94bd3ac5eb86 100644 --- a/lib/libifconfig/Makefile +++ b/lib/libifconfig/Makefile @@ -8,7 +8,9 @@ NO_PIC= SHLIBDIR?= /lib SHLIB_MAJOR= 1 -SRCS= libifconfig.c libifconfig_internal.c +SRCS= libifconfig.c libifconfig_carp.c libifconfig_inet.c +SRCS+= libifconfig_inet6.c libifconfig_internal.c libifconfig_lagg.c +SRCS+= libifconfig_media.c INCSDIR= ${INCLUDEDIR} INCS= libifconfig.h @@ -16,6 +18,6 @@ INCS= libifconfig.h #MAN= libifconfig.3 CFLAGS+= -I${.CURDIR} -WARNS?=6 +NO_WCAST_ALIGN= yes .include <bsd.lib.mk> diff --git a/lib/libifconfig/libifconfig.c b/lib/libifconfig/libifconfig.c index ba0945bb88ae..4d6e702db9de 100644 --- a/lib/libifconfig/libifconfig.c +++ b/lib/libifconfig/libifconfig.c @@ -1,38 +1,7 @@ /* - * Copyright (c) 2016, Marie Helene Kvello-Aune - * 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, - * thislist 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 documentation and/or - * other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. - * - * $FreeBSD$ - */ - -/* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2016-2017, Marie Helene Kvello-Aune. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -61,65 +30,63 @@ * $FreeBSD$ */ - /* - * Copyright 1996 Massachusetts Institute of Technology - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby - * granted, provided that both the above copyright notice and this - * permission notice appear in all copies, that both the above - * copyright notice and this permission notice appear in all - * supporting documentation, and that the name of M.I.T. not be used - * in advertising or publicity pertaining to distribution of the - * software without specific, written prior permission. M.I.T. makes - * no representations about the suitability of this software for any - * purpose. It is provided "as is" without express or implied - * warranty. - * - * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS - * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT - * SHALL M.I.T. 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. - * - * $FreeBSD$ - */ - #include <sys/types.h> #include <sys/ioctl.h> #include <sys/sysctl.h> #include <net/if.h> #include <net/if_mib.h> +#include <netinet/in.h> +#include <netinet6/in6_var.h> +#include <netinet6/nd6.h> #include <err.h> #include <errno.h> #include <fcntl.h> +#include <ifaddrs.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <net/if_vlan_var.h> + #include "libifconfig.h" #include "libifconfig_internal.h" +#define NOTAG ((u_short) -1) + +static bool +isnd6defif(ifconfig_handle_t *h, const char *name) +{ + struct in6_ndifreq ndifreq; + unsigned int ifindex; + + memset(&ndifreq, 0, sizeof(ndifreq)); + strlcpy(ndifreq.ifname, name, sizeof(ndifreq.ifname)); + ifindex = if_nametoindex(ndifreq.ifname); + if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGDEFIFACE_IN6, &ndifreq) < 0) { + return (false); + } + h->error.errtype = OK; + return (ndifreq.ifindex == ifindex); +} ifconfig_handle_t * ifconfig_open(void) { - struct ifconfig_handle *h; + ifconfig_handle_t *h; h = calloc(1, sizeof(*h)); + + if (h == NULL) { + return (NULL); + } for (int i = 0; i <= AF_MAX; i++) { h->sockets[i] = -1; } + return (h); } @@ -132,6 +99,7 @@ ifconfig_close(ifconfig_handle_t *h) (void)close(h->sockets[i]); } } + freeifaddrs(h->ifap); free(h); } @@ -157,6 +125,46 @@ ifconfig_err_ioctlreq(ifconfig_handle_t *h) } int +ifconfig_foreach_iface(ifconfig_handle_t *h, + ifconfig_foreach_func_t cb, void *udata) +{ + int ret; + + ret = ifconfig_getifaddrs(h); + if (ret == 0) { + struct ifaddrs *ifa; + char *ifname = NULL; + + for (ifa = h->ifap; ifa; ifa = ifa->ifa_next) { + if (ifname != ifa->ifa_name) { + ifname = ifa->ifa_name; + cb(h, ifa, udata); + } + } + } + /* Free ifaddrs so we don't accidentally cache stale data */ + freeifaddrs(h->ifap); + h->ifap = NULL; + + return (ret); +} + +void +ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa, + ifconfig_foreach_func_t cb, void *udata) +{ + struct ifaddrs *ift; + + for (ift = ifa; + ift != NULL && + ift->ifa_addr != NULL && + strcmp(ift->ifa_name, ifa->ifa_name) == 0; + ift = ift->ifa_next) { + cb(h, ift, udata); + } +} + +int ifconfig_get_description(ifconfig_handle_t *h, const char *name, char **description) { @@ -179,6 +187,7 @@ ifconfig_get_description(ifconfig_handle_t *h, const char *name, ifr.ifr_buffer.buffer = descr; ifr.ifr_buffer.length = descrlen; if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFDESCR, &ifr) != 0) { + free(descr); return (-1); } @@ -186,6 +195,13 @@ ifconfig_get_description(ifconfig_handle_t *h, const char *name, if (strlen(descr) > 0) { *description = strdup(descr); free(descr); + + if (description == NULL) { + h->error.errtype = OTHER; + h->error.errcode = ENOMEM; + return (-1); + } + return (0); } } else if (ifr.ifr_buffer.length > descrlen) { @@ -228,8 +244,7 @@ ifconfig_set_description(ifconfig_handle_t *h, const char *name, return (-1); } - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFDESCR, - &ifr) != 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFDESCR, &ifr) != 0) { free(ifr.ifr_buffer.buffer); return (-1); } @@ -248,8 +263,7 @@ ifconfig_unset_description(ifconfig_handle_t *h, const char *name) ifr.ifr_buffer.length = 0; ifr.ifr_buffer.buffer = NULL; - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFDESCR, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFDESCR, &ifr) < 0) { return (-1); } return (0); @@ -271,8 +285,7 @@ ifconfig_set_name(ifconfig_handle_t *h, const char *name, const char *newname) (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); ifr.ifr_data = tmpname; - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFNAME, - &ifr) != 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFNAME, &ifr) != 0) { free(tmpname); return (-1); } @@ -285,61 +298,60 @@ int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname, char **orig_name) { - struct ifmibdata ifmd; size_t len; + unsigned int ifindex; int name[6]; - int i, maxifno; + + ifindex = if_nametoindex(ifname); + if (ifindex == 0) { + goto fail; + } name[0] = CTL_NET; name[1] = PF_LINK; name[2] = NETLINK_GENERIC; - name[3] = IFMIB_SYSTEM; - name[4] = IFMIB_IFCOUNT; + name[3] = IFMIB_IFDATA; + name[4] = ifindex; + name[5] = IFDATA_DRIVERNAME; - len = sizeof maxifno; - if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0) { - h->error.errtype = OTHER; - h->error.errcode = errno; - return (-1); + len = 0; + if (sysctl(name, 6, NULL, &len, 0, 0) < 0) { + goto fail; } - name[3] = IFMIB_IFDATA; - name[5] = IFDATA_GENERAL; - for (i = 1; i <= maxifno; i++) { - len = sizeof ifmd; - name[4] = i; - if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) { - if (errno == ENOENT) - continue; + *orig_name = malloc(len); + if (*orig_name == NULL) { + goto fail; + } - goto fail; - } + if (sysctl(name, 6, *orig_name, &len, 0, 0) < 0) { + free(*orig_name); + *orig_name = NULL; + goto fail; + } - if (strncmp(ifmd.ifmd_name, ifname, IFNAMSIZ) != 0) - continue; + return (0); - len = 0; - name[5] = IFDATA_DRIVERNAME; - if (sysctl(name, 6, NULL, &len, 0, 0) < 0) - goto fail; +fail: + h->error.errtype = OTHER; + h->error.errcode = (errno != 0) ? errno : ENOENT; + return (-1); +} - *orig_name = malloc(len); - if (*orig_name == NULL) - goto fail; +int +ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib) +{ + struct ifreq ifr; - if (sysctl(name, 6, *orig_name, &len, 0, 0) < 0) { - free(*orig_name); - *orig_name = NULL; - goto fail; - } + memset(&ifr, 0, sizeof(ifr)); + (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); - return (0); + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFFIB, &ifr) == -1) { + return (-1); } -fail: - h->error.errtype = OTHER; - h->error.errcode = (i <= maxifno) ? errno : ENOENT; - return (-1); + *fib = ifr.ifr_fib; + return (0); } int @@ -351,8 +363,7 @@ ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu) (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); ifr.ifr_mtu = mtu; - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFMTU, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFMTU, &ifr) < 0) { return (-1); } @@ -367,8 +378,7 @@ ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu) memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFMTU, - &ifr) == -1) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFMTU, &ifr) == -1) { return (-1); } @@ -377,6 +387,24 @@ ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu) } int +ifconfig_get_nd6(ifconfig_handle_t *h, const char *name, + struct in6_ndireq *nd) +{ + memset(nd, 0, sizeof(*nd)); + strlcpy(nd->ifname, name, sizeof(nd->ifname)); + if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFINFO_IN6, nd) == -1) { + return (-1); + } + if (isnd6defif(h, name)) { + nd->ndi.flags |= ND6_IFF_DEFAULTIF; + } else if (h->error.errtype != OK) { + return (-1); + } + + return (0); +} + +int ifconfig_set_metric(ifconfig_handle_t *h, const char *name, const int metric) { struct ifreq ifr; @@ -385,8 +413,7 @@ ifconfig_set_metric(ifconfig_handle_t *h, const char *name, const int metric) (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); ifr.ifr_metric = metric; - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFMETRIC, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFMETRIC, &ifr) < 0) { return (-1); } @@ -401,8 +428,7 @@ ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric) memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFMETRIC, - &ifr) == -1) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFMETRIC, &ifr) == -1) { return (-1); } @@ -420,8 +446,7 @@ ifconfig_set_capability(ifconfig_handle_t *h, const char *name, memset(&ifr, 0, sizeof(ifr)); - if (ifconfig_get_capability(h, name, - &ifcap) != 0) { + if (ifconfig_get_capability(h, name, &ifcap) != 0) { return (-1); } @@ -442,8 +467,7 @@ ifconfig_set_capability(ifconfig_handle_t *h, const char *name, * set for this request. */ ifr.ifr_reqcap = flags; - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFCAP, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSIFCAP, &ifr) < 0) { return (-1); } return (0); @@ -458,8 +482,7 @@ ifconfig_get_capability(ifconfig_handle_t *h, const char *name, memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFCAP, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFCAP, &ifr) < 0) { return (-1); } capability->curcap = ifr.ifr_curcap; @@ -468,6 +491,45 @@ ifconfig_get_capability(ifconfig_handle_t *h, const char *name, } int +ifconfig_get_groups(ifconfig_handle_t *h, const char *name, + struct ifgroupreq *ifgr) +{ + int len; + + memset(ifgr, 0, sizeof(*ifgr)); + strlcpy(ifgr->ifgr_name, name, IFNAMSIZ); + + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFGROUP, ifgr) == -1) { + if ((h->error.errcode == EINVAL) || + (h->error.errcode == ENOTTY)) { + return (0); + } else { + return (-1); + } + } + + len = ifgr->ifgr_len; + ifgr->ifgr_groups = (struct ifg_req *)malloc(len); + if (ifgr->ifgr_groups == NULL) { + return (1); + } + bzero(ifgr->ifgr_groups, len); + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFGROUP, ifgr) == -1) { + return (-1); + } + + return (0); +} + +int +ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name, + struct ifstat *ifs) +{ + strlcpy(ifs->ifs_name, name, sizeof(ifs->ifs_name)); + return (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFSTATUS, ifs)); +} + +int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name) { struct ifreq ifr; @@ -475,8 +537,7 @@ ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name) memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); - if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCIFDESTROY, - &ifr) < 0) { + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCIFDESTROY, &ifr) < 0) { return (-1); } return (0); @@ -488,6 +549,7 @@ ifconfig_create_interface(ifconfig_handle_t *h, const char *name, char **ifname) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); + (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); /* @@ -512,5 +574,57 @@ ifconfig_create_interface(ifconfig_handle_t *h, const char *name, char **ifname) } *ifname = strdup(ifr.ifr_name); + if (ifname == NULL) { + h->error.errtype = OTHER; + h->error.errcode = ENOMEM; + return (-1); + } + + return (0); +} + +int +ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name, + char **ifname, const char *vlandev, const unsigned short vlantag) +{ + struct ifreq ifr; + struct vlanreq params; + + if ((vlantag == NOTAG) || (vlandev[0] == '\0')) { + // TODO: Add proper error tracking here + return (-1); + } + + bzero(¶ms, sizeof(params)); + (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); + params.vlr_tag = vlantag; + (void)strlcpy(params.vlr_parent, vlandev, sizeof(params.vlr_parent)); + ifr.ifr_data = (caddr_t)¶ms; + + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCIFCREATE2, &ifr) < 0) { + // TODO: Add proper error tracking here + return (-1); + } + + *ifname = strdup(ifr.ifr_name); + return (0); +} + +int +ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, + const char *vlandev, const unsigned short vlantag) +{ + struct ifreq ifr; + struct vlanreq params; + + bzero(¶ms, sizeof(params)); + params.vlr_tag = vlantag; + strlcpy(params.vlr_parent, vlandev, sizeof(params.vlr_parent)); + + ifr.ifr_data = (caddr_t)¶ms; + (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSETVLAN, &ifr) == -1) { + return (-1); + } return (0); } diff --git a/lib/libifconfig/libifconfig.h b/lib/libifconfig/libifconfig.h index b5508740de10..dd4e91eac07b 100644 --- a/lib/libifconfig/libifconfig.h +++ b/lib/libifconfig/libifconfig.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Marie Helene Kvello-Aune + * Copyright (c) 2016-2017, Marie Helene Kvello-Aune * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -28,8 +28,16 @@ #pragma once +#include <netinet/in.h> +#include <netinet6/in6_var.h> + +#define ND6_IFF_DEFAULTIF 0x8000 + typedef enum { - OTHER, IOCTL, SOCKET + OK = 0, + OTHER, + IOCTL, + SOCKET } ifconfig_errtype; /* @@ -37,7 +45,15 @@ typedef enum { * pointer to it for library use. */ struct ifconfig_handle; -typedef struct ifconfig_handle ifconfig_handle_t; +typedef struct ifconfig_handle ifconfig_handle_t; + +struct carpreq; +struct ifaddrs; +struct in6_ndireq; +struct lagg_reqall; +struct lagg_reqflags; +struct lagg_reqopts; +struct lagg_reqport; struct ifconfig_capabilities { /** Current capabilities (ifconfig prints this as 'options')*/ @@ -46,13 +62,44 @@ struct ifconfig_capabilities { int reqcap; }; +/** Stores extra info associated with an inet address */ +struct ifconfig_inet_addr { + const struct sockaddr_in *sin; + const struct sockaddr_in *netmask; + const struct sockaddr_in *dst; + const struct sockaddr_in *broadcast; + int prefixlen; + uint8_t vhid; +}; + +/** Stores extra info associated with an inet6 address */ +struct ifconfig_inet6_addr { + struct sockaddr_in6 *sin6; + struct sockaddr_in6 *dstin6; + struct in6_addrlifetime lifetime; + int prefixlen; + uint32_t flags; + uint8_t vhid; +}; + +/** Stores extra info associated with a lagg(4) interface */ +struct ifconfig_lagg_status { + struct lagg_reqall *ra; + struct lagg_reqopts *ro; + struct lagg_reqflags *rf; +}; + /** Retrieves a new state object for use in other API calls. * Example usage: *{@code * // Create state object - * ifconfig_handle_t *lifh = ifconfig_open(); + * ifconfig_handle_t *lifh; + * lifh = ifconfig_open(); + * if (lifh == NULL) { + * // Handle error + * } * - * // Do stuff with it + * // Do stuff with the handle * * // Dispose of the state object * ifconfig_close(lifh); @@ -73,6 +120,28 @@ ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h); /** Retrieves the errno associated with the error, if any. */ int ifconfig_err_errno(ifconfig_handle_t *h); +typedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h, + struct ifaddrs *ifa, void *udata); + +/** Iterate over every network interface + * @param h An open ifconfig state object + * @param cb A callback function to call with a pointer to each interface + * @param udata An opaque value that will be passed to the callback. + * @return 0 on success, nonzero if the list could not be iterated + */ +int ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb, + void *udata); + +/** Iterate over every address on a single network interface + * @param h An open ifconfig state object + * @param ifa A pointer that was supplied by a previous call to + * ifconfig_foreach_iface + * @param udata An opaque value that will be passed to the callback. + * @param cb A callback function to call with a pointer to each ifaddr + */ +void ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa, + ifconfig_foreach_func_t cb, void *udata); + /** If error type was IOCTL, this identifies which request failed. */ unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h); int ifconfig_get_description(ifconfig_handle_t *h, const char *name, @@ -84,8 +153,12 @@ int ifconfig_set_name(ifconfig_handle_t *h, const char *name, const char *newname); int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname, char **orig_name); +int ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib); +int ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib); int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu); int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu); +int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name, + struct in6_ndireq *nd); int ifconfig_set_metric(ifconfig_handle_t *h, const char *name, const int metric); int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric); @@ -94,6 +167,70 @@ int ifconfig_set_capability(ifconfig_handle_t *h, const char *name, int ifconfig_get_capability(ifconfig_handle_t *h, const char *name, struct ifconfig_capabilities *capability); +/** Retrieve the list of groups to which this interface belongs + * @param h An open ifconfig state object + * @param name The interface name + * @param ifgr return argument. The caller is responsible for freeing + * ifgr->ifgr_groups + * @return 0 on success, nonzero on failure + */ +int ifconfig_get_groups(ifconfig_handle_t *h, const char *name, + struct ifgroupreq *ifgr); +int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name, + struct ifstat *stat); + +/** Retrieve the interface media information + * @param h An open ifconfig state object + * @param name The interface name + * @param ifmr Return argument. The caller is responsible for freeing it + * @return 0 on success, nonzero on failure + */ +int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name, + struct ifmediareq **ifmr); +const char *ifconfig_media_get_type(int ifmw); +const char *ifconfig_media_get_subtype(int ifmw); +const char *ifconfig_media_get_status(const struct ifmediareq *ifmr); +void ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen); + +int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name, + struct carpreq *carpr, int ncarpr); + +/** Retrieve additional information about an inet address + * @param h An open ifconfig state object + * @param name The interface name + * @param ifa Pointer to the the address structure of interest + * @param addr Return argument. It will be filled with additional information + * about the address. + * @return 0 on success, nonzero on failure. + */ +int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h, + const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr); + +/** Retrieve additional information about an inet6 address + * @param h An open ifconfig state object + * @param name The interface name + * @param ifa Pointer to the the address structure of interest + * @param addr Return argument. It will be filled with additional information + * about the address. + * @return 0 on success, nonzero on failure. + */ +int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h, + const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr); + +/** Retrieve additional information about a lagg(4) interface */ +int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h, + const char *name, struct ifconfig_lagg_status **lagg_status); + +/** Retrieve additional information about a member of a lagg(4) interface */ +int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h, + const char *name, struct lagg_reqport *rp); + +/** Frees the structure returned by ifconfig_lagg_get_status. Does nothing if + * the argument is NULL + * @param laggstat Pointer to the structure to free + */ +void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat); + /** Destroy a virtual interface * @param name Interface to destroy */ @@ -105,3 +242,15 @@ int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name); */ int ifconfig_create_interface(ifconfig_handle_t *h, const char *name, char **ifname); + +/** Creates a (virtual) interface + * @param name Name of interface to create. Example: vlan0 or ix0.50 + * @param name ifname Is set to actual name of created interface + * @param vlandev Name of interface to attach to + * @param vlanid VLAN ID/Tag. Must not be 0. + */ +int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name, + char **ifname, const char *vlandev, const unsigned short vlantag); + +int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, + const char *vlandev, const unsigned short vlantag); diff --git a/lib/libifconfig/libifconfig_carp.c b/lib/libifconfig/libifconfig_carp.c new file mode 100644 index 000000000000..cd31a85dfd9a --- /dev/null +++ b/lib/libifconfig/libifconfig_carp.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 1983, 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 + * documentation and/or other materials provided with the distribution. + * 3. 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. + * + * $FreeBSD$ + */ +#include <sys/param.h> +#include <sys/ioctl.h> + +#include <net/if.h> +#include <netinet/ip_carp.h> + +#include <string.h> +#include <strings.h> + +#include "libifconfig.h" +#include "libifconfig_internal.h" + + +int +ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name, + struct carpreq *carpr, int ncarpr) +{ + struct ifreq ifr; + + bzero(carpr, sizeof(struct carpreq) * ncarpr); + carpr[0].carpr_count = ncarpr; + strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); + ifr.ifr_data = (caddr_t)carpr; + + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGVH, &ifr) != 0) { + return (-1); + } + + return (0); +} diff --git a/lib/libifconfig/libifconfig_inet.c b/lib/libifconfig/libifconfig_inet.c new file mode 100644 index 000000000000..59c74b11a57f --- /dev/null +++ b/lib/libifconfig/libifconfig_inet.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 1983, 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 + * documentation and/or other materials provided with the distribution. + * 3. 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. + * + * $FreeBSD$ + */ +#include <sys/param.h> +#include <sys/ioctl.h> + +#include <net/if.h> +#include <netinet/in.h> + +#include <errno.h> +#include <ifaddrs.h> +#include <string.h> +#include <strings.h> + +#include "libifconfig.h" +#include "libifconfig_internal.h" + +static const struct sockaddr_in NULL_SIN; + +static int +inet_prefixlen(const struct in_addr *addr) +{ + int x; + + x = ffs(ntohl(addr->s_addr)); + return (x == 0 ? 0 : 33 - x); +} + +int +ifconfig_inet_get_addrinfo(ifconfig_handle_t *h __unused, + const char *name __unused, struct ifaddrs *ifa, + struct ifconfig_inet_addr *addr) +{ + bzero(addr, sizeof(*addr)); + + /* Set the address */ + if (ifa->ifa_addr == NULL) { + return (-1); + } else { + addr->sin = (struct sockaddr_in *)ifa->ifa_addr; + } + + /* Set the destination address */ + if (ifa->ifa_flags & IFF_POINTOPOINT) { + if (ifa->ifa_dstaddr) { + addr->dst = (struct sockaddr_in *)ifa->ifa_dstaddr; + } else { + addr->dst = &NULL_SIN; + } + } + + /* Set the netmask and prefixlen */ + if (ifa->ifa_netmask) { + addr->netmask = (struct sockaddr_in *)ifa->ifa_netmask; + } else { + addr->netmask = &NULL_SIN; + } + addr->prefixlen = inet_prefixlen(&addr->netmask->sin_addr); + + /* Set the broadcast */ + if (ifa->ifa_flags & IFF_BROADCAST) { + addr->broadcast = (struct sockaddr_in *)ifa->ifa_broadaddr; + } + + /* Set the vhid */ + if (ifa->ifa_data) { + addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid; + } + + return (0); +} diff --git a/lib/libifconfig/libifconfig_inet6.c b/lib/libifconfig/libifconfig_inet6.c new file mode 100644 index 000000000000..c2edc5debf91 --- /dev/null +++ b/lib/libifconfig/libifconfig_inet6.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 1983, 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 + * documentation and/or other materials provided with the distribution. + * 3. 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. + * + * $FreeBSD$ + */ +#include <sys/param.h> +#include <sys/ioctl.h> + +#include <net/if.h> +#include <netinet/in.h> + +#include <errno.h> +#include <ifaddrs.h> +#include <string.h> +#include <strings.h> + +#include "libifconfig.h" +#include "libifconfig_internal.h" + + +static int +inet6_prefixlen(struct in6_addr *addr) +{ + int prefixlen = 0; + int i; + + for (i = 0; i < 4; i++) { + int x = ffs(ntohl(addr->__u6_addr.__u6_addr32[i])); + prefixlen += x == 0 ? 0 : 33 - x; + if (x != 1) { + break; + } + } + return (prefixlen); +} + +int +ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h, + const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr) +{ + struct sockaddr_in6 *netmask; + struct in6_ifreq ifr6; + + bzero(addr, sizeof(*addr)); + + /* Set the address */ + addr->sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; + + /* Set the destination address */ + if (ifa->ifa_flags & IFF_POINTOPOINT) { + addr->dstin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr; + } + + /* Set the prefixlen */ + netmask = (struct sockaddr_in6 *)ifa->ifa_netmask; + addr->prefixlen = inet6_prefixlen(&netmask->sin6_addr); + + /* Set the flags */ + strlcpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name)); + ifr6.ifr_addr = *addr->sin6; + if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFAFLAG_IN6, &ifr6) < 0) { + return (-1); + } + addr->flags = ifr6.ifr_ifru.ifru_flags6; + + /* Set the lifetimes */ + memset(&addr->lifetime, 0, sizeof(addr->lifetime)); + ifr6.ifr_addr = *addr->sin6; + if (ifconfig_ioctlwrap(h, AF_INET6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) { + return (-1); + } + addr->lifetime = ifr6.ifr_ifru.ifru_lifetime; /* struct copy */ + + /* Set the vhid */ + if (ifa->ifa_data && ifa->ifa_data) { + addr->vhid = ((struct if_data *)ifa->ifa_data)->ifi_vhid; + } + + return (0); +} diff --git a/lib/libifconfig/libifconfig_internal.c b/lib/libifconfig/libifconfig_internal.c index cc0441053d18..6b7f767e23e0 100644 --- a/lib/libifconfig/libifconfig_internal.c +++ b/lib/libifconfig/libifconfig_internal.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Marie Helene Kvello-Aune + * Copyright (c) 2016-2017, Marie Helene Kvello-Aune * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -31,6 +31,7 @@ #include <net/if.h> #include <errno.h> +#include <ifaddrs.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -38,23 +39,22 @@ #include "libifconfig.h" // Needed for ifconfig_errstate #include "libifconfig_internal.h" - int -ifconfig_ioctlwrap_ret(ifconfig_handle_t *h, unsigned long request, int rcode) +ifconfig_getifaddrs(ifconfig_handle_t *h) { + int ret; - if (rcode != 0) { - h->error.errtype = IOCTL; - h->error.ioctl_request = request; - h->error.errcode = errno; + if (h->ifap == NULL) { + ret = getifaddrs(&h->ifap); + return (ret); + } else { + return (0); } - - return (rcode); } int ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily, - unsigned long request, struct ifreq *ifr) + unsigned long request, void *data) { int s; @@ -62,8 +62,14 @@ ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily, return (-1); } - int rcode = ioctl(s, request, ifr); - return (ifconfig_ioctlwrap_ret(h, request, rcode)); + if (ioctl(s, request, data) != 0) { + h->error.errtype = IOCTL; + h->error.ioctl_request = request; + h->error.errcode = errno; + return (-1); + } + + return (0); } /* diff --git a/lib/libifconfig/libifconfig_internal.h b/lib/libifconfig/libifconfig_internal.h index 73694c90e139..d9803a05c675 100644 --- a/lib/libifconfig/libifconfig_internal.h +++ b/lib/libifconfig/libifconfig_internal.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Marie Helene Kvello-Aune + * Copyright (c) 2016-2017, Marie Helene Kvello-Aune * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -52,8 +52,13 @@ struct errstate { struct ifconfig_handle { struct errstate error; int sockets[AF_MAX + 1]; + /** Cached output of getifaddrs */ + struct ifaddrs *ifap; }; +/* Fetch the list of interface addrs, if it hasn't already been fetched */ +int ifconfig_getifaddrs(ifconfig_handle_t *h); + /** * Retrieves socket for address family <paramref name="addressfamily"> from * cache, or creates it if it doesn't already exist. @@ -75,10 +80,6 @@ struct ifconfig_handle { */ int ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s); -/** Function used by other wrapper functions to populate _errstate when appropriate.*/ -int ifconfig_ioctlwrap_ret(ifconfig_handle_t *h, unsigned long request, - int rcode); - /** Function to wrap ioctl() and automatically populate ifconfig_errstate when appropriate.*/ int ifconfig_ioctlwrap(ifconfig_handle_t *h, const int addressfamily, - unsigned long request, struct ifreq *ifr); + unsigned long request, void *data); diff --git a/lib/libifconfig/libifconfig_lagg.c b/lib/libifconfig/libifconfig_lagg.c new file mode 100644 index 000000000000..1ed9ccaf0242 --- /dev/null +++ b/lib/libifconfig/libifconfig_lagg.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 1983, 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 + * documentation and/or other materials provided with the distribution. + * 3. 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. + * + * $FreeBSD$ + */ +#include <sys/param.h> +#include <sys/ioctl.h> + +#include <net/ethernet.h> +#include <net/if.h> +#include <net/if_lagg.h> +#include <net/ieee8023ad_lacp.h> + +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> + +#include "libifconfig.h" +#include "libifconfig_internal.h" + +/* Internal structure used for allocations and frees */ +struct _ifconfig_lagg_status { + struct ifconfig_lagg_status l; /* Must be first */ + struct lagg_reqall ra; + struct lagg_reqopts ro; + struct lagg_reqflags rf; + struct lagg_reqport rpbuf[LAGG_MAX_PORTS]; +}; + +int +ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h, + const char *name, struct lagg_reqport *rp) +{ + strlcpy(rp->rp_ifname, name, sizeof(rp->rp_portname)); + strlcpy(rp->rp_portname, name, sizeof(rp->rp_portname)); + + return (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGLAGGPORT, rp)); +} + +int +ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h, + const char *name, struct ifconfig_lagg_status **lagg_status) +{ + struct _ifconfig_lagg_status *ls; + + ls = calloc(1, sizeof(struct _ifconfig_lagg_status)); + if (ls == NULL) { + h->error.errtype = OTHER; + h->error.errcode = ENOMEM; + return (-1); + } + ls->l.ra = &ls->ra; + ls->l.ro = &ls->ro; + ls->l.rf = &ls->rf; + *lagg_status = &ls->l; + + ls->ra.ra_port = ls->rpbuf; + ls->ra.ra_size = sizeof(ls->rpbuf); + + strlcpy(ls->ro.ro_ifname, name, sizeof(ls->ro.ro_ifname)); + ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGLAGGOPTS, &ls->ro); + + strlcpy(ls->rf.rf_ifname, name, sizeof(ls->rf.rf_ifname)); + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGLAGGFLAGS, &ls->rf) != 0) { + ls->rf.rf_flags = 0; + } + + strlcpy(ls->ra.ra_ifname, name, sizeof(ls->ra.ra_ifname)); + if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGLAGG, &ls->ra) != 0) { + free(ls); + return (-1); + } + + return (0); +} + +void +ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat) +{ + free(laggstat); +} diff --git a/lib/libifconfig/libifconfig_media.c b/lib/libifconfig/libifconfig_media.c new file mode 100644 index 000000000000..03b0be67963f --- /dev/null +++ b/lib/libifconfig/libifconfig_media.c @@ -0,0 +1,457 @@ +/* + * Copyright (c) 1983, 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 + * documentation and/or other materials provided with the distribution. + * 3. 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. + * + * $FreeBSD$ + */ +#include <sys/param.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/sysctl.h> +#include <sys/time.h> + +#include <net/if.h> +#include <net/if_dl.h> +#include <net/if_types.h> +#include <net/if_media.h> +#include <net/route.h> + +#include <assert.h> +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "libifconfig.h" +#include "libifconfig_internal.h" + + +static struct ifmedia_description *get_toptype_desc(int); +static struct ifmedia_type_to_subtype *get_toptype_ttos(int); +static struct ifmedia_description *get_subtype_desc(int, + struct ifmedia_type_to_subtype *ttos); + +#define IFM_OPMODE(x) \ + ((x) & (IFM_IEEE80211_ADHOC | IFM_IEEE80211_HOSTAP | \ + IFM_IEEE80211_IBSS | IFM_IEEE80211_WDS | IFM_IEEE80211_MONITOR | \ + IFM_IEEE80211_MBSS)) +#define IFM_IEEE80211_STA 0 + +static struct ifmedia_description ifm_type_descriptions[] = + IFM_TYPE_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ethernet_descriptions[] = + IFM_SUBTYPE_ETHERNET_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ethernet_aliases[] = + IFM_SUBTYPE_ETHERNET_ALIASES; + +static struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] = + IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_tokenring_descriptions[] = + IFM_SUBTYPE_TOKENRING_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_tokenring_aliases[] = + IFM_SUBTYPE_TOKENRING_ALIASES; + +static struct ifmedia_description ifm_subtype_tokenring_option_descriptions[] = + IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_fddi_descriptions[] = + IFM_SUBTYPE_FDDI_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_fddi_aliases[] = + IFM_SUBTYPE_FDDI_ALIASES; + +static struct ifmedia_description ifm_subtype_fddi_option_descriptions[] = + IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ieee80211_descriptions[] = + IFM_SUBTYPE_IEEE80211_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ieee80211_aliases[] = + IFM_SUBTYPE_IEEE80211_ALIASES; + +static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = + IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = + IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_ieee80211_mode_aliases[] = + IFM_SUBTYPE_IEEE80211_MODE_ALIASES; + +static struct ifmedia_description ifm_subtype_atm_descriptions[] = + IFM_SUBTYPE_ATM_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_atm_aliases[] = + IFM_SUBTYPE_ATM_ALIASES; + +static struct ifmedia_description ifm_subtype_atm_option_descriptions[] = + IFM_SUBTYPE_ATM_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_shared_descriptions[] = + IFM_SUBTYPE_SHARED_DESCRIPTIONS; + +static struct ifmedia_description ifm_subtype_shared_aliases[] = + IFM_SUBTYPE_SHARED_ALIASES; + +static struct ifmedia_description ifm_shared_option_descriptions[] = + IFM_SHARED_OPTION_DESCRIPTIONS; + +static struct ifmedia_description ifm_shared_option_aliases[] = + IFM_SHARED_OPTION_ALIASES; + +struct ifmedia_type_to_subtype { + struct { + struct ifmedia_description *desc; + int alias; + } + subtypes[5]; + struct { + struct ifmedia_description *desc; + int alias; + } + options[4]; + struct { + struct ifmedia_description *desc; + int alias; + } + modes[3]; +}; + +/* must be in the same order as IFM_TYPE_DESCRIPTIONS */ +static struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = +{ + { + { + { &ifm_subtype_shared_descriptions[0], 0 }, + { &ifm_subtype_shared_aliases[0], 1 }, + { &ifm_subtype_ethernet_descriptions[0], 0 }, + { &ifm_subtype_ethernet_aliases[0], 1 }, + { NULL, 0 }, + }, + { + { &ifm_shared_option_descriptions[0], 0 }, + { &ifm_shared_option_aliases[0], 1 }, + { &ifm_subtype_ethernet_option_descriptions[0], 0 }, + { NULL, 0 }, + }, + { + { NULL, 0 }, + }, + }, + { + { + { &ifm_subtype_shared_descriptions[0], 0 }, + { &ifm_subtype_shared_aliases[0], 1 }, + { &ifm_subtype_tokenring_descriptions[0], 0 }, + { &ifm_subtype_tokenring_aliases[0], 1 }, + { NULL, 0 }, + }, + { + { &ifm_shared_option_descriptions[0], 0 }, + { &ifm_shared_option_aliases[0], 1 }, + { &ifm_subtype_tokenring_option_descriptions[0], 0 }, + { NULL, 0 }, + }, + { + { NULL, 0 }, + }, + }, + { + { + { &ifm_subtype_shared_descriptions[0], 0 }, + { &ifm_subtype_shared_aliases[0], 1 }, + { &ifm_subtype_fddi_descriptions[0], 0 }, + { &ifm_subtype_fddi_aliases[0], 1 }, + { NULL, 0 }, + }, + { + { &ifm_shared_option_descriptions[0], 0 }, + { &ifm_shared_option_aliases[0], 1 }, + { &ifm_subtype_fddi_option_descriptions[0], 0 }, + { NULL, 0 }, + }, + { + { NULL, 0 }, + }, + }, + { + { + { &ifm_subtype_shared_descriptions[0], 0 }, + { &ifm_subtype_shared_aliases[0], 1 }, + { &ifm_subtype_ieee80211_descriptions[0], 0 }, + { &ifm_subtype_ieee80211_aliases[0], 1 }, + { NULL, 0 }, + }, + { + { &ifm_shared_option_descriptions[0], 0 }, + { &ifm_shared_option_aliases[0], 1 }, + { &ifm_subtype_ieee80211_option_descriptions[0], 0 }, + { NULL, 0 }, + }, + { + { &ifm_subtype_ieee80211_mode_descriptions[0], 0 }, + { &ifm_subtype_ieee80211_mode_aliases[0], 0 }, + { NULL, 0 }, + }, + }, + { + { + { &ifm_subtype_shared_descriptions[0], 0 }, + { &ifm_subtype_shared_aliases[0], 1 }, + { &ifm_subtype_atm_descriptions[0], 0 }, + { &ifm_subtype_atm_aliases[0], 1 }, + { NULL, 0 }, + }, + { + { &ifm_shared_option_descriptions[0], 0 }, + { &ifm_shared_option_aliases[0], 1 }, + { &ifm_subtype_atm_option_descriptions[0], 0 }, + { NULL, 0 }, + }, + { + { NULL, 0 }, + }, + }, +}; + +static struct ifmedia_description * +get_toptype_desc(int ifmw) +{ + struct ifmedia_description *desc; + + for (desc = ifm_type_descriptions; desc->ifmt_string != NULL; desc++) { + if (IFM_TYPE(ifmw) == desc->ifmt_word) { + break; + } + } + + return (desc); +} + +static struct ifmedia_type_to_subtype * +get_toptype_ttos(int ifmw) +{ + struct ifmedia_description *desc; + struct ifmedia_type_to_subtype *ttos; + + for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes; + desc->ifmt_string != NULL; desc++, ttos++) { + if (IFM_TYPE(ifmw) == desc->ifmt_word) { + break; + } + } + + return (ttos); +} + +static struct ifmedia_description * +get_subtype_desc(int ifmw, + struct ifmedia_type_to_subtype *ttos) +{ + int i; + struct ifmedia_description *desc; + + for (i = 0; ttos->subtypes[i].desc != NULL; i++) { + if (ttos->subtypes[i].alias) { + continue; + } + for (desc = ttos->subtypes[i].desc; + desc->ifmt_string != NULL; desc++) { + if (IFM_SUBTYPE(ifmw) == desc->ifmt_word) { + return (desc); + } + } + } + + return (NULL); +} + +const char * +ifconfig_media_get_type(int ifmw) +{ + struct ifmedia_description *desc; + + /*int seen_option = 0, i;*/ + + /* Find the top-level interface type. */ + desc = get_toptype_desc(ifmw); + if (desc->ifmt_string == NULL) { + return ("<unknown type>"); + } else { + return (desc->ifmt_string); + } +} + +const char * +ifconfig_media_get_subtype(int ifmw) +{ + struct ifmedia_description *desc; + struct ifmedia_type_to_subtype *ttos; + + ttos = get_toptype_ttos(ifmw); + desc = get_subtype_desc(ifmw, ttos); + return (desc->ifmt_string); +} + +/*************************************************************************** +* Above this point, this file is mostly copied from sbin/ifconfig/ifmedia.c +***************************************************************************/ + +/* Internal structure used for allocations and frees */ +struct _ifconfig_media_status { + struct ifmediareq ifmr; + int medialist[0]; +}; + +int +ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name, + struct ifmediareq **ifmr) +{ + struct _ifconfig_media_status *ms, *ms2; + unsigned long cmd = SIOCGIFXMEDIA; + + *ifmr = NULL; + ms = calloc(1, sizeof(*ms)); + if (ms == NULL) { + h->error.errtype = OTHER; + h->error.errcode = ENOMEM; + return (-1); + } + (void)memset(ms, 0, sizeof(*ms)); + (void)strlcpy(ms->ifmr.ifm_name, name, sizeof(ms->ifmr.ifm_name)); + + /* + * Check if interface supports extended media types. + */ + if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) { + cmd = SIOCGIFMEDIA; + if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms->ifmr) < 0) { + /* Interface doesn't support SIOC{G,S}IFMEDIA. */ + h->error.errtype = OK; + free(ms); + return (-1); + } + } + if (ms->ifmr.ifm_count == 0) { + *ifmr = &ms->ifmr; + return (0); /* Interface has no media types ?*/ + } + + ms2 = realloc(ms, sizeof(*ms) + sizeof(int) * ms->ifmr.ifm_count); + if (ms2 == NULL) { + h->error.errtype = OTHER; + h->error.errcode = ENOMEM; + free(ms); + return (-1); + } + ms2->ifmr.ifm_ulist = &ms2->medialist[0]; + + if (ifconfig_ioctlwrap(h, AF_LOCAL, cmd, &ms2->ifmr) < 0) { + free(ms2); + return (-1); + } + + *ifmr = &ms2->ifmr; + return (0); +} + +const char * +ifconfig_media_get_status(const struct ifmediareq *ifmr) +{ + switch (IFM_TYPE(ifmr->ifm_active)) { + case IFM_ETHER: + case IFM_ATM: + if (ifmr->ifm_status & IFM_ACTIVE) { + return ("active"); + } else { + return ("no carrier"); + } + break; + + case IFM_FDDI: + case IFM_TOKEN: + if (ifmr->ifm_status & IFM_ACTIVE) { + return ("inserted"); + } else { + return ("no ring"); + } + break; + + case IFM_IEEE80211: + if (ifmr->ifm_status & IFM_ACTIVE) { + /* NB: only sta mode associates */ + if (IFM_OPMODE(ifmr->ifm_active) == IFM_IEEE80211_STA) { + return ("associated"); + } else { + return ("running"); + } + } else { + return ("no carrier"); + } + break; + default: + return (""); + } +} + +void +ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen) +{ + struct ifmedia_type_to_subtype *ttos; + struct ifmedia_description *desc; + int i, seen_option = 0; + size_t len; + + assert(buflen > 0); + buf[0] = '\0'; + ttos = get_toptype_ttos(ifmw); + for (i = 0; ttos->options[i].desc != NULL; i++) { + if (ttos->options[i].alias) { + continue; + } + for (desc = ttos->options[i].desc; + desc->ifmt_string != NULL; desc++) { + if (ifmw & desc->ifmt_word) { + if (seen_option++) { + strlcat(buf, ",", buflen); + } + len = strlcat(buf, desc->ifmt_string, buflen); + assert(len < buflen); + buf += len; + buflen -= len; + } + } + } +} |
