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 /share/examples/libifconfig | |
parent | beaafe4f86876bf5b6ba8a6ff4b9b0f484054214 (diff) |
libifconfig: multiple feature additions
Added the ability to:
* Create virtual interfaces
* Create vlan interfaces
* Get interface fib
* Get interface groups
* Get interface status
* Get nd6 info
* Get media status
* Get additional ifaddr info in a convenient struct
* Get vhids
* Get carp info
* Get lagg and laggport status
* Iterate over all interfaces and ifaddrs
And add more examples, too.
Note that this is a backwards-incompatible change. But that's ok, because it's
a private library.
MFC after: 3 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D14463
Notes
Notes:
svn path=/head/; revision=329853
Diffstat (limited to 'share/examples/libifconfig')
-rw-r--r-- | share/examples/libifconfig/Makefile | 3 | ||||
-rw-r--r-- | share/examples/libifconfig/ifchangevlan.c | 109 | ||||
-rw-r--r-- | share/examples/libifconfig/ifcreate.c | 10 | ||||
-rw-r--r-- | share/examples/libifconfig/ifcreatevlan.c | 105 | ||||
-rw-r--r-- | share/examples/libifconfig/ifdestroy.c | 10 | ||||
-rw-r--r-- | share/examples/libifconfig/setdescription.c | 15 | ||||
-rw-r--r-- | share/examples/libifconfig/setmtu.c | 10 | ||||
-rw-r--r-- | share/examples/libifconfig/status.c | 530 |
8 files changed, 783 insertions, 9 deletions
diff --git a/share/examples/libifconfig/Makefile b/share/examples/libifconfig/Makefile index 6926f65b1afa..1c29c4dad6cc 100644 --- a/share/examples/libifconfig/Makefile +++ b/share/examples/libifconfig/Makefile @@ -4,5 +4,8 @@ default: $(CC) -Wall -fPIC -lifconfig -g -o example_setmtu setmtu.c $(CC) -Wall -fPIC -lifconfig -g -o example_ifdestroy ifdestroy.c $(CC) -Wall -fPIC -lifconfig -g -o example_ifcreate ifcreate.c + $(CC) -Wall -fPIC -lifconfig -g -o example_ifcreatevlan ifcreatevlan.c + $(CC) -Wall -fPIC -lifconfig -g -o example_ifchanagevlan ifchanagevlan.c + $(CC) -Wall -fPIC -lifconfig -g -o example_status status.c clean: rm -f example_* diff --git a/share/examples/libifconfig/ifchangevlan.c b/share/examples/libifconfig/ifchangevlan.c new file mode 100644 index 000000000000..05a51db68d04 --- /dev/null +++ b/share/examples/libifconfig/ifchangevlan.c @@ -0,0 +1,109 @@ +/* + * Copyright (c) 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 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. + * + * 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$ + */ + +#include <err.h> +#include <errno.h> +#include <net/if.h> +#include <sys/ioctl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libifconfig.h> + +#include <net/if_vlan_var.h> + +int +main(int argc, char *argv[]) +{ + char *ifname, *parentif; + unsigned short vlantag; + const char *errstr; + ifconfig_handle_t *lifh; + + if (argc != 4) { + errx(EINVAL, "Invalid number of arguments." + " Should provide exactly three arguments: " + "INTERFACE, PARENT_INTERFACE and VLAN_TAG."); + } + + /* We have a static number of arguments. Therefore we can do it simple. */ + ifname = strdup(argv[1]); + parentif = strdup(argv[2]); + vlantag = strtonum(argv[3], 0, USHRT_MAX, &errstr); + + if (errstr != NULL) { + errx(1, "VLAN_TAG must be between 0 and %i.\n", USHRT_MAX); + } + + printf("Interface: %s\nNew VLAN tag: %i\n", ifname, vlantag); + + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + + if (ifconfig_set_vlantag(lifh, ifname, parentif, vlantag) == 0) { + printf("Successfully changed vlan tag.\n"); + ifconfig_close(lifh); + lifh = NULL; + free(ifname); + free(parentif); + return (0); + } + + switch (ifconfig_err_errtype(lifh)) { + case SOCKET: + warnx("couldn't create socket. This shouldn't happen.\n"); + break; + case IOCTL: + if (ifconfig_err_ioctlreq(lifh) == SIOCGETVLAN) { + warnx("Target interface isn't a VLAN interface.\n"); + } + if (ifconfig_err_ioctlreq(lifh) == SIOCSETVLAN) { + warnx( + "Couldn't change VLAN properties of interface.\n"); + } + break; + default: + warnx( + "This is a thorough example accommodating for temporary" + " 'not implemented yet' errors. That's likely what happened" + " now. If not, your guess is as good as mine. ;)" + " Error code: %d\n", ifconfig_err_errno( + lifh)); + break; + } + + ifconfig_close(lifh); + lifh = NULL; + free(ifname); + free(parentif); + return (-1); +} diff --git a/share/examples/libifconfig/ifcreate.c b/share/examples/libifconfig/ifcreate.c index 7a7fad64d1ed..2e7e7c06f6f0 100644 --- a/share/examples/libifconfig/ifcreate.c +++ b/share/examples/libifconfig/ifcreate.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, @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) { char *ifname, *ifactualname; + ifconfig_handle_t *lifh; if (argc != 2) { errx(EINVAL, "Invalid number of arguments." @@ -52,7 +53,12 @@ main(int argc, char *argv[]) printf("Requested interface name: %s\n", ifname); - ifconfig_handle_t *lifh = ifconfig_open(); + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + if (ifconfig_create_interface(lifh, ifname, &ifactualname) == 0) { printf("Successfully created interface '%s'\n", ifactualname); ifconfig_close(lifh); diff --git a/share/examples/libifconfig/ifcreatevlan.c b/share/examples/libifconfig/ifcreatevlan.c new file mode 100644 index 000000000000..87feed8a69c8 --- /dev/null +++ b/share/examples/libifconfig/ifcreatevlan.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 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 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. + * + * 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$ + */ + +#include <err.h> +#include <errno.h> +#include <net/if.h> +#include <sys/ioctl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libifconfig.h> + + +int +main(int argc, char *argv[]) +{ + char *parentif, *ifactualname; + unsigned short vlantag; + const char *errstr; + ifconfig_handle_t *lifh; + + if (argc != 3) { + errx(EINVAL, "Invalid number of arguments." + " Should provide exactly two arguments: " + "PARENT_INTERFACE and VLAN_TAG."); + } + + /* We have a static number of arguments. Therefore we can do it simple. */ + parentif = strdup(argv[1]); + vlantag = strtonum(argv[2], 0, USHRT_MAX, &errstr); + + if (errstr != NULL) { + errx(1, "VLAN_TAG must be between 0 and %i.\n", USHRT_MAX); + } + + printf("Parent interface: %s\nVLAN tag: %i\n", parentif, vlantag); + + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + + if (ifconfig_create_interface_vlan(lifh, "vlan", &ifactualname, + parentif, vlantag) == 0) { + printf("Successfully created interface '%s'\n", ifactualname); + ifconfig_close(lifh); + lifh = NULL; + free(parentif); + free(ifactualname); + return (0); + } + + switch (ifconfig_err_errtype(lifh)) { + case SOCKET: + warnx("couldn't create socket. This shouldn't happen.\n"); + break; + case IOCTL: + if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) { + warnx( + "Failed to create interface (SIOCIFCREATE2)\n"); + } + break; + default: + warnx( + "This is a thorough example accommodating for temporary" + " 'not implemented yet' errors. That's likely what happened" + " now. If not, your guess is as good as mine. ;)" + " Error code: %d\n", ifconfig_err_errno( + lifh)); + break; + } + + ifconfig_close(lifh); + lifh = NULL; + free(parentif); + free(ifactualname); + return (-1); +} diff --git a/share/examples/libifconfig/ifdestroy.c b/share/examples/libifconfig/ifdestroy.c index 4dd556f9d8c8..b031e3fd594e 100644 --- a/share/examples/libifconfig/ifdestroy.c +++ b/share/examples/libifconfig/ifdestroy.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, @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) { char *ifname; + ifconfig_handle_t *lifh; if (argc != 2) { errx(EINVAL, "Invalid number of arguments." @@ -52,7 +53,12 @@ main(int argc, char *argv[]) printf("Interface name: %s\n", ifname); - ifconfig_handle_t *lifh = ifconfig_open(); + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + if (ifconfig_destroy_interface(lifh, ifname) == 0) { printf("Successfully destroyed interface '%s'.", ifname); ifconfig_close(lifh); diff --git a/share/examples/libifconfig/setdescription.c b/share/examples/libifconfig/setdescription.c index 371b49729379..1d8d03e82b9a 100644 --- a/share/examples/libifconfig/setdescription.c +++ b/share/examples/libifconfig/setdescription.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, @@ -26,6 +26,9 @@ * $FreeBSD$ */ +#include <arpa/inet.h> +#include <net/if.h> + #include <err.h> #include <errno.h> #include <stdio.h> @@ -38,6 +41,7 @@ int main(int argc, char *argv[]) { char *ifname, *ifdescr, *curdescr; + ifconfig_handle_t *lifh; if (argc != 3) { errx(EINVAL, "Invalid number of arguments." @@ -52,7 +56,12 @@ main(int argc, char *argv[]) printf("Interface name: %s\n", ifname); - ifconfig_handle_t *lifh = ifconfig_open(); + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + if (ifconfig_get_description(lifh, ifname, &curdescr) == 0) { printf("Old description: %s\n", curdescr); } @@ -71,7 +80,7 @@ main(int argc, char *argv[]) lifh), "IOCTL(%lu) error", ifconfig_err_ioctlreq(lifh)); break; - case OTHER: + default: err(ifconfig_err_errno(lifh), "Other error"); break; } diff --git a/share/examples/libifconfig/setmtu.c b/share/examples/libifconfig/setmtu.c index 1f8f33873ecf..bef61ae402ec 100644 --- a/share/examples/libifconfig/setmtu.c +++ b/share/examples/libifconfig/setmtu.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, @@ -42,6 +42,7 @@ main(int argc, char *argv[]) { char *ifname, *ptr; int mtu; + ifconfig_handle_t *lifh; if (argc != 3) { errx(EINVAL, "Invalid number of arguments." @@ -56,7 +57,12 @@ main(int argc, char *argv[]) printf("Interface name: %s\n", ifname); printf("New MTU: %d", mtu); - ifconfig_handle_t *lifh = ifconfig_open(); + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(ENOMEM, "Failed to open libifconfig handle."); + return (-1); + } + if (ifconfig_set_mtu(lifh, ifname, mtu) == 0) { printf("Successfully changed MTU of %s to %d\n", ifname, mtu); ifconfig_close(lifh); diff --git a/share/examples/libifconfig/status.c b/share/examples/libifconfig/status.c new file mode 100644 index 000000000000..62fd3f35c8de --- /dev/null +++ b/share/examples/libifconfig/status.c @@ -0,0 +1,530 @@ +/* + * Copyright (c) 2017, Spectra Logic Corporation + * 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. + * + * + * 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$ + */ + +#include <sys/param.h> +#include <sys/ioctl.h> +#include <sys/socket.h> + +#include <arpa/inet.h> +#include <net/ethernet.h> +#include <net/if.h> +#include <net/if_dl.h> +#include <net/if_lagg.h> +#include <net/if_media.h> +#include <net/if_types.h> +#include <netinet/in.h> +#include <netinet/ip_carp.h> +#include <netinet6/in6_var.h> +#include <netinet6/nd6.h> + +#include <err.h> +#include <errno.h> +#include <ifaddrs.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <libifconfig.h> + +static const char *carp_states[] = { CARP_STATES }; + +static void +print_carp(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct carpreq carpr[CARP_MAXVHID]; + int i; + + if (ifconfig_carp_get_info(lifh, ifa->ifa_name, carpr, CARP_MAXVHID)) { + return; /* Probably not configured on this interface */ + } + for (i = 0; i < carpr[0].carpr_count; i++) { + printf("\tcarp: %s vhid %d advbase %d advskew %d", + carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, + carpr[i].carpr_advbase, carpr[i].carpr_advskew); + printf("\n"); + } +} + +static void +print_inet4_addr(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct ifconfig_inet_addr addr; + char addr_buf[NI_MAXHOST]; + + if (ifconfig_inet_get_addrinfo(lifh, ifa->ifa_name, ifa, &addr) != 0) { + return; + } + + inet_ntop(AF_INET, &addr.sin->sin_addr, addr_buf, sizeof(addr_buf)); + printf("\tinet %s", addr_buf); + + if (addr.dst) { + printf(" --> %s", inet_ntoa(addr.dst->sin_addr)); + } + + printf(" netmask 0x%x ", ntohl(addr.netmask->sin_addr.s_addr)); + + if ((addr.broadcast != NULL) && + (addr.broadcast->sin_addr.s_addr != 0)) { + printf("broadcast %s ", inet_ntoa(addr.broadcast->sin_addr)); + } + + if (addr.vhid != 0) { + printf("vhid %d ", addr.vhid); + } + printf("\n"); +} + +static void +print_inet6_addr(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct ifconfig_inet6_addr addr; + char addr_buf[NI_MAXHOST]; + struct timespec now; + + /* Print the address */ + if (ifconfig_inet6_get_addrinfo(lifh, ifa->ifa_name, ifa, &addr) != 0) { + err(1, "ifconfig_inet6_get_addrinfo"); + } + if (0 != getnameinfo((struct sockaddr *)addr.sin6, addr.sin6->sin6_len, + addr_buf, sizeof(addr_buf), NULL, 0, NI_NUMERICHOST)) { + inet_ntop(AF_INET6, &addr.sin6->sin6_addr, addr_buf, + sizeof(addr_buf)); + } + printf("\tinet6 %s", addr_buf); + + if (addr.dstin6) { + inet_ntop(AF_INET6, addr.dstin6, addr_buf, sizeof(addr_buf)); + printf(" --> %s", addr_buf); + } + + /* Print the netmask */ + printf(" prefixlen %d ", addr.prefixlen); + + /* Print the scopeid*/ + if (addr.sin6->sin6_scope_id) { + printf("scopeid 0x%x ", addr.sin6->sin6_scope_id); + } + + /* Print the flags */ + if ((addr.flags & IN6_IFF_ANYCAST) != 0) { + printf("anycast "); + } + if ((addr.flags & IN6_IFF_TENTATIVE) != 0) { + printf("tentative "); + } + if ((addr.flags & IN6_IFF_DUPLICATED) != 0) { + printf("duplicated "); + } + if ((addr.flags & IN6_IFF_DETACHED) != 0) { + printf("detached "); + } + if ((addr.flags & IN6_IFF_DEPRECATED) != 0) { + printf("deprecated "); + } + if ((addr.flags & IN6_IFF_AUTOCONF) != 0) { + printf("autoconf "); + } + if ((addr.flags & IN6_IFF_TEMPORARY) != 0) { + printf("temporary "); + } + if ((addr.flags & IN6_IFF_PREFER_SOURCE) != 0) { + printf("prefer_source "); + } + + /* Print the lifetimes */ + clock_gettime(CLOCK_MONOTONIC_FAST, &now); + if (addr.lifetime.ia6t_preferred || addr.lifetime.ia6t_expire) { + printf("pltime "); + if (addr.lifetime.ia6t_preferred) { + printf("%ld ", MAX(0l, + addr.lifetime.ia6t_preferred - now.tv_sec)); + } else { + printf("infty "); + } + + printf("vltime "); + if (addr.lifetime.ia6t_expire) { + printf("%ld ", MAX(0l, + addr.lifetime.ia6t_expire - now.tv_sec)); + } else { + printf("infty "); + } + } + + /* Print the vhid */ + if (addr.vhid != 0) { + printf("vhid %d ", addr.vhid); + } + printf("\n"); +} + +static void +print_link_addr(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + char addr_buf[NI_MAXHOST]; + struct sockaddr_dl *sdl; + int n; + + sdl = (struct sockaddr_dl *)ifa->ifa_addr; + if ((sdl != NULL) && (sdl->sdl_alen > 0)) { + if (((sdl->sdl_type == IFT_ETHER) || + (sdl->sdl_type == IFT_L2VLAN) || + (sdl->sdl_type == IFT_BRIDGE)) && + (sdl->sdl_alen == ETHER_ADDR_LEN)) { + ether_ntoa_r((struct ether_addr *)LLADDR(sdl), + addr_buf); + printf("\tether %s\n", addr_buf); + } else { + n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; + + printf("\tlladdr %s\n", link_ntoa(sdl) + n); + } + } +} + +static void +print_ifaddr(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata __unused) +{ + switch (ifa->ifa_addr->sa_family) { + case AF_INET: + print_inet4_addr(lifh, ifa); + break; + case AF_INET6: + + /* + * printing AF_INET6 status requires calling SIOCGIFAFLAG_IN6 + * and SIOCGIFALIFETIME_IN6. TODO: figure out the best way to + * do that from within libifconfig + */ + print_inet6_addr(lifh, ifa); + break; + case AF_LINK: + print_link_addr(lifh, ifa); + break; + case AF_LOCAL: + case AF_UNSPEC: + default: + /* TODO */ + break; + } +} + +static void +print_nd6(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct in6_ndireq nd; + + if (ifconfig_get_nd6(lifh, ifa->ifa_name, &nd) == 0) { + printf("\tnd6 options=%x\n", nd.ndi.flags); + } else { + err(1, "Failed to get nd6 options"); + } +} + +static void +print_fib(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + int fib; + + if (ifconfig_get_fib(lifh, ifa->ifa_name, &fib) == 0) { + printf("\tfib: %d\n", fib); + } else { + err(1, "Failed to get interface FIB"); + } +} + +static void +print_lagg(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct lagg_protos lpr[] = LAGG_PROTOS; + struct ifconfig_lagg_status *ls; + struct lacp_opreq *lp; + const char *proto = "<unknown>"; + int i; + + if (ifconfig_lagg_get_lagg_status(lifh, ifa->ifa_name, &ls) < 0) { + if (ifconfig_err_errno(lifh) == EINVAL) { + return; + } + err(1, "Failed to get interface lagg status"); + } + + /* First print the proto */ + for (i = 0; i < nitems(lpr); i++) { + if (ls->ra->ra_proto == lpr[i].lpr_proto) { + proto = lpr[i].lpr_name; + break; + } + } + printf("\tlaggproto %s", proto); + + /* Now print the lagg hash */ + if (ls->rf->rf_flags & LAGG_F_HASHMASK) { + const char *sep = ""; + + printf(" lagghash "); + if (ls->rf->rf_flags & LAGG_F_HASHL2) { + printf("%sl2", sep); + sep = ","; + } + if (ls->rf->rf_flags & LAGG_F_HASHL3) { + printf("%sl3", sep); + sep = ","; + } + if (ls->rf->rf_flags & LAGG_F_HASHL4) { + printf("%sl4", sep); + sep = ","; + } + } + putchar('\n'); + printf("\tlagg options:\n"); + printf("\t\tflags=%x", ls->ro->ro_opts); + putchar('\n'); + printf("\t\tflowid_shift: %d\n", ls->ro->ro_flowid_shift); + if (ls->ra->ra_proto == LAGG_PROTO_ROUNDROBIN) { + printf("\t\trr_limit: %d\n", ls->ro->ro_bkt); + } + printf("\tlagg statistics:\n"); + printf("\t\tactive ports: %d\n", ls->ro->ro_active); + printf("\t\tflapping: %u\n", ls->ro->ro_flapping); + for (i = 0; i < ls->ra->ra_ports; i++) { + lp = (struct lacp_opreq *)&ls->ra->ra_port[i].rp_lacpreq; + printf("\tlaggport: %s ", ls->ra->ra_port[i].rp_portname); + printf("flags=%x", ls->ra->ra_port[i].rp_flags); + if (ls->ra->ra_proto == LAGG_PROTO_LACP) { + printf(" state=%x", lp->actor_state); + } + putchar('\n'); + } + + printf("\n"); + ifconfig_lagg_free_lagg_status(ls); +} + +static void +print_laggport(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct lagg_reqport rp; + + if (ifconfig_lagg_get_laggport_status(lifh, ifa->ifa_name, &rp) < 0) { + if ((ifconfig_err_errno(lifh) == EINVAL) || + (ifconfig_err_errno(lifh) == ENOENT)) { + return; + } else { + err(1, "Failed to get lagg port status"); + } + } + + printf("\tlaggdev: %s\n", rp.rp_ifname); +} + +static void +print_groups(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + struct ifgroupreq ifgr; + struct ifg_req *ifg; + int len; + int cnt = 0; + + if (ifconfig_get_groups(lifh, ifa->ifa_name, &ifgr) != 0) { + err(1, "Failed to get groups"); + } + + ifg = ifgr.ifgr_groups; + len = ifgr.ifgr_len; + for (; ifg && len >= sizeof(struct ifg_req); ifg++) { + len -= sizeof(struct ifg_req); + if (strcmp(ifg->ifgrq_group, "all")) { + if (cnt == 0) { + printf("\tgroups: "); + } + cnt++; + printf("%s ", ifg->ifgrq_group); + } + } + if (cnt) { + printf("\n"); + } + + free(ifgr.ifgr_groups); +} + +static void +print_media(ifconfig_handle_t *lifh, struct ifaddrs *ifa) +{ + int i; + + /* Outline: + * 1) Determine whether the iface supports SIOGIFMEDIA or SIOGIFXMEDIA + * 2) Get the full media list + * 3) Print the current media word + * 4) Print the active media word, if different + * 5) Print the status + * 6) Print the supported media list + * + * How to print the media word: + * 1) Get the top-level interface type and description + * 2) Print the subtype + * 3) For current word only, print the top type, if it exists + * 4) Print options list + * 5) Print the instance, if there is one + * + * How to get the top-level interface type + * 1) Shift ifmw right by 0x20 and index into IFM_TYPE_DESCRIPTIONS + * + * How to get the top-level interface subtype + * 1) Shift ifmw right by 0x20, index into ifmedia_types_to_subtypes + * 2) Iterate through the resulting table's subtypes table, ignoring + * aliases. Iterate through the resulting ifmedia_description + * tables, finding an entry with the right media subtype + */ + struct ifmediareq *ifmr; + char opts[80]; + + if (ifconfig_media_get_mediareq(lifh, ifa->ifa_name, &ifmr) != 0) { + if (ifconfig_err_errtype(lifh) != OK) { + err(1, "Failed to get media info"); + } else { + return; /* Interface doesn't support media info */ + } + } + + printf("\tmedia: %s %s", ifconfig_media_get_type(ifmr->ifm_current), + ifconfig_media_get_subtype(ifmr->ifm_current)); + if (ifmr->ifm_active != ifmr->ifm_current) { + printf(" (%s", ifconfig_media_get_subtype(ifmr->ifm_active)); + ifconfig_media_get_options_string(ifmr->ifm_active, opts, + sizeof(opts)); + if (opts[0] != '\0') { + printf(" <%s>)\n", opts); + } else { + printf(")\n"); + } + } else { + printf("\n"); + } + + if (ifmr->ifm_status & IFM_AVALID) { + printf("\tstatus: %s\n", + ifconfig_media_get_status(ifmr)); + } + + printf("\tsupported media:\n"); + for (i = 0; i < ifmr->ifm_count; i++) { + printf("\t\tmedia %s", + ifconfig_media_get_subtype(ifmr->ifm_ulist[i])); + ifconfig_media_get_options_string(ifmr->ifm_ulist[i], opts, + sizeof(opts)); + if (opts[0] != '\0') { + printf(" mediaopt %s\n", opts); + } else { + printf("\n"); + } + } + free(ifmr); +} + +static void +print_iface(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata __unused) +{ + int metric, mtu; + char *description = NULL; + struct ifconfig_capabilities caps; + struct ifstat ifs; + + printf("%s: flags=%x ", ifa->ifa_name, ifa->ifa_flags); + + if (ifconfig_get_metric(lifh, ifa->ifa_name, &metric) == 0) { + printf("metric %d ", metric); + } else { + err(1, "Failed to get interface metric"); + } + + if (ifconfig_get_mtu(lifh, ifa->ifa_name, &mtu) == 0) { + printf("mtu %d\n", mtu); + } else { + err(1, "Failed to get interface MTU"); + } + + if (ifconfig_get_description(lifh, ifa->ifa_name, &description) == 0) { + printf("\tdescription: %s\n", description); + } + + if (ifconfig_get_capability(lifh, ifa->ifa_name, &caps) == 0) { + if (caps.curcap != 0) { + printf("\toptions=%x\n", caps.curcap); + } + if (caps.reqcap != 0) { + printf("\tcapabilities=%x\n", caps.reqcap); + } + } else { + err(1, "Failed to get interface capabilities"); + } + + ifconfig_foreach_ifaddr(lifh, ifa, print_ifaddr, NULL); + + /* This paragraph is equivalent to ifconfig's af_other_status funcs */ + print_nd6(lifh, ifa); + print_media(lifh, ifa); + print_groups(lifh, ifa); + print_fib(lifh, ifa); + print_carp(lifh, ifa); + print_lagg(lifh, ifa); + print_laggport(lifh, ifa); + + if (ifconfig_get_ifstatus(lifh, ifa->ifa_name, &ifs) == 0) { + printf("%s", ifs.ascii); + } + + free(description); +} + +int +main(int argc, char *argv[]) +{ + ifconfig_handle_t *lifh; + + if (argc != 1) { + errx(1, "Usage: example_status"); + } + + lifh = ifconfig_open(); + if (lifh == NULL) { + errx(1, "Failed to open libifconfig handle."); + } + + if (ifconfig_foreach_iface(lifh, print_iface, NULL) != 0) { + err(1, "Failed to get interfaces"); + } + + ifconfig_close(lifh); + lifh = NULL; + return (-1); +} |