Index: head/lib/libifconfig/Makefile =================================================================== --- head/lib/libifconfig/Makefile (revision 329852) +++ head/lib/libifconfig/Makefile (revision 329853) @@ -1,21 +1,23 @@ # $FreeBSD$ PACKAGE= lib${LIB} LIB= ifconfig PRIVATELIB= true # Don't build shared library, for now. 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 #MAN= libifconfig.3 CFLAGS+= -I${.CURDIR} -WARNS?=6 +NO_WCAST_ALIGN= yes .include Index: head/lib/libifconfig/libifconfig.c =================================================================== --- head/lib/libifconfig/libifconfig.c (revision 329852) +++ head/lib/libifconfig/libifconfig.c (revision 329853) @@ -1,516 +1,630 @@ /* - * 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 * 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$ */ - /* - * 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 #include #include #include #include +#include +#include +#include #include #include #include +#include +#include #include #include #include #include +#include + #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); } void ifconfig_close(ifconfig_handle_t *h) { for (int i = 0; i <= AF_MAX; i++) { if (h->sockets[i] != -1) { (void)close(h->sockets[i]); } } + freeifaddrs(h->ifap); free(h); } ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h) { return (h->error.errtype); } int ifconfig_err_errno(ifconfig_handle_t *h) { return (h->error.errcode); } unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h) { return (h->error.ioctl_request); } 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) { struct ifreq ifr; char *descr; size_t descrlen; descr = NULL; descrlen = 64; memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); for (;;) { if ((descr = reallocf(descr, descrlen)) == NULL) { h->error.errtype = OTHER; h->error.errcode = ENOMEM; return (-1); } ifr.ifr_buffer.buffer = descr; ifr.ifr_buffer.length = descrlen; if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFDESCR, &ifr) != 0) { + free(descr); return (-1); } if (ifr.ifr_buffer.buffer == descr) { 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) { descrlen = ifr.ifr_buffer.length; continue; } break; } free(descr); h->error.errtype = OTHER; h->error.errcode = 0; return (-1); } int ifconfig_set_description(ifconfig_handle_t *h, const char *name, const char *newdescription) { struct ifreq ifr; int desclen; memset(&ifr, 0, sizeof(ifr)); desclen = strlen(newdescription); /* * Unset description if the new description is 0 characters long. * TODO: Decide whether this should be an error condition instead. */ if (desclen == 0) { return (ifconfig_unset_description(h, name)); } (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); ifr.ifr_buffer.length = desclen + 1; ifr.ifr_buffer.buffer = strdup(newdescription); if (ifr.ifr_buffer.buffer == NULL) { h->error.errtype = OTHER; h->error.errcode = ENOMEM; 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); } free(ifr.ifr_buffer.buffer); return (0); } int ifconfig_unset_description(ifconfig_handle_t *h, const char *name) { struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_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); } int ifconfig_set_name(ifconfig_handle_t *h, const char *name, const char *newname) { struct ifreq ifr; char *tmpname; memset(&ifr, 0, sizeof(ifr)); tmpname = strdup(newname); if (tmpname == NULL) { h->error.errtype = OTHER; h->error.errcode = ENOMEM; return (-1); } (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); } free(tmpname); return (0); } 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 ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu) { struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); (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); } return (0); } int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu) { struct ifreq ifr; 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); } *mtu = ifr.ifr_mtu; return (0); } 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; memset(&ifr, 0, sizeof(ifr)); (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); } return (0); } int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric) { struct ifreq ifr; 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); } *metric = ifr.ifr_metric; return (0); } int ifconfig_set_capability(ifconfig_handle_t *h, const char *name, const int capability) { struct ifreq ifr; struct ifconfig_capabilities ifcap; int flags, value; memset(&ifr, 0, sizeof(ifr)); - if (ifconfig_get_capability(h, name, - &ifcap) != 0) { + if (ifconfig_get_capability(h, name, &ifcap) != 0) { return (-1); } value = capability; flags = ifcap.curcap; if (value < 0) { value = -value; flags &= ~value; } else { flags |= value; } flags &= ifcap.reqcap; (void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); /* * TODO: Verify that it's safe to not have ifr.ifr_curcap * 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); } int ifconfig_get_capability(ifconfig_handle_t *h, const char *name, struct ifconfig_capabilities *capability) { struct ifreq ifr; 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; capability->reqcap = ifr.ifr_reqcap; return (0); } 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; 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); } int 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)); /* * TODO: * Insert special snowflake handling here. See GitHub issue #12 for details. * In the meantime, hard-nosupport interfaces that need special handling. */ if ((strncmp(name, "wlan", strlen("wlan")) == 0) || (strncmp(name, "vlan", strlen("vlan")) == 0) || (strncmp(name, "vxlan", strlen("vxlan")) == 0)) { h->error.errtype = OTHER; h->error.errcode = ENOSYS; return (-1); } /* No special handling for this interface type. */ if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCIFCREATE2, &ifr) < 0) { return (-1); } *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); } Index: head/lib/libifconfig/libifconfig.h =================================================================== --- head/lib/libifconfig/libifconfig.h (revision 329852) +++ head/lib/libifconfig/libifconfig.h (revision 329853) @@ -1,107 +1,256 @@ /* - * 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, * 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$ */ #pragma once +#include +#include + +#define ND6_IFF_DEFAULTIF 0x8000 + typedef enum { - OTHER, IOCTL, SOCKET + OK = 0, + OTHER, + IOCTL, + SOCKET } ifconfig_errtype; /* * Opaque definition so calling application can just pass a * 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')*/ int curcap; /** Requested capabilities (ifconfig prints this as '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); * lifh = NULL; *} */ ifconfig_handle_t *ifconfig_open(void); /** Frees resources held in the provided state object. * @param h The state object to close. * @see #ifconfig_open(void) */ void ifconfig_close(ifconfig_handle_t *h); /** Identifies what kind of error occured. */ 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, char **description); int ifconfig_set_description(ifconfig_handle_t *h, const char *name, const char *newdescription); int ifconfig_unset_description(ifconfig_handle_t *h, const char *name); 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); int ifconfig_set_capability(ifconfig_handle_t *h, const char *name, const int capability); 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 */ int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name); /** Creates a (virtual) interface * @param name Name of interface to create. Example: bridge or bridge42 * @param name ifname Is set to actual name of created interface */ 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); Index: head/lib/libifconfig/libifconfig_carp.c =================================================================== --- head/lib/libifconfig/libifconfig_carp.c (nonexistent) +++ head/lib/libifconfig/libifconfig_carp.c (revision 329853) @@ -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 +#include + +#include +#include + +#include +#include + +#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); +} Property changes on: head/lib/libifconfig/libifconfig_carp.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/lib/libifconfig/libifconfig_inet.c =================================================================== --- head/lib/libifconfig/libifconfig_inet.c (nonexistent) +++ head/lib/libifconfig/libifconfig_inet.c (revision 329853) @@ -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 +#include + +#include +#include + +#include +#include +#include +#include + +#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); +} Property changes on: head/lib/libifconfig/libifconfig_inet.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/lib/libifconfig/libifconfig_inet6.c =================================================================== --- head/lib/libifconfig/libifconfig_inet6.c (nonexistent) +++ head/lib/libifconfig/libifconfig_inet6.c (revision 329853) @@ -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 +#include + +#include +#include + +#include +#include +#include +#include + +#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); +} Property changes on: head/lib/libifconfig/libifconfig_inet6.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/lib/libifconfig/libifconfig_internal.c =================================================================== --- head/lib/libifconfig/libifconfig_internal.c (revision 329852) +++ head/lib/libifconfig/libifconfig_internal.c (revision 329853) @@ -1,98 +1,104 @@ /* - * 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, * 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 #include #include +#include #include #include #include #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; if (ifconfig_socket(h, addressfamily, &s) != 0) { 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); } /* * Function to get socket for the specified address family. * If the socket doesn't already exist, attempt to create it. */ int ifconfig_socket(ifconfig_handle_t *h, const int addressfamily, int *s) { if (addressfamily > AF_MAX) { h->error.errtype = SOCKET; h->error.errcode = EINVAL; return (-1); } if (h->sockets[addressfamily] != -1) { *s = h->sockets[addressfamily]; return (0); } /* We don't have a socket of that type available. Create one. */ h->sockets[addressfamily] = socket(addressfamily, SOCK_DGRAM, 0); if (h->sockets[addressfamily] == -1) { h->error.errtype = SOCKET; h->error.errcode = errno; return (-1); } *s = h->sockets[addressfamily]; return (0); } Index: head/lib/libifconfig/libifconfig_internal.h =================================================================== --- head/lib/libifconfig/libifconfig_internal.h (revision 329852) +++ head/lib/libifconfig/libifconfig_internal.h (revision 329853) @@ -1,84 +1,85 @@ /* - * 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, * 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$ */ #pragma once #include "libifconfig.h" struct errstate { /** * Type of error. */ ifconfig_errtype errtype; /** * The error occured in this ioctl() request. * Populated if errtype = IOCTL */ unsigned long ioctl_request; /** * The value of the global errno variable when the error occured. */ int errcode; }; 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 from * cache, or creates it if it doesn't already exist. * @param addressfamily The address family of the socket to retrieve * @param s The retrieved socket. * @return 0 on success, -1 on failure. * {@example * This example shows how to retrieve a socket from the cache. * {@code * static void myfunc() \{ * int s; * if (ifconfig_socket(AF_LOCAL, &s) != 0) \{ * // Handle error state here * \} * // user code here * \} * } * } */ 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); Index: head/lib/libifconfig/libifconfig_lagg.c =================================================================== --- head/lib/libifconfig/libifconfig_lagg.c (nonexistent) +++ head/lib/libifconfig/libifconfig_lagg.c (revision 329853) @@ -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 +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#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); +} Property changes on: head/lib/libifconfig/libifconfig_lagg.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/lib/libifconfig/libifconfig_media.c =================================================================== --- head/lib/libifconfig/libifconfig_media.c (nonexistent) +++ head/lib/libifconfig/libifconfig_media.c (revision 329853) @@ -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 +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 (""); + } 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; + } + } + } +} Property changes on: head/lib/libifconfig/libifconfig_media.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/share/examples/libifconfig/Makefile =================================================================== --- head/share/examples/libifconfig/Makefile (revision 329852) +++ head/share/examples/libifconfig/Makefile (revision 329853) @@ -1,8 +1,11 @@ # $FreeBSD$ default: $(CC) -Wall -fPIC -lifconfig -g -o example_setdescription setdescription.c $(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_* Index: head/share/examples/libifconfig/ifchangevlan.c =================================================================== --- head/share/examples/libifconfig/ifchangevlan.c (nonexistent) +++ head/share/examples/libifconfig/ifchangevlan.c (revision 329853) @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +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); +} Property changes on: head/share/examples/libifconfig/ifchangevlan.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/share/examples/libifconfig/ifcreate.c =================================================================== --- head/share/examples/libifconfig/ifcreate.c (revision 329852) +++ head/share/examples/libifconfig/ifcreate.c (revision 329853) @@ -1,90 +1,96 @@ /* - * 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, * 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 #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *ifname, *ifactualname; + ifconfig_handle_t *lifh; if (argc != 2) { errx(EINVAL, "Invalid number of arguments." " Only one argument is accepted, and it should be the name" " of the interface to be created."); } /* We have a static number of arguments. Therefore we can do it simple. */ ifname = strdup(argv[1]); 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); lifh = NULL; free(ifname); 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(ifname); free(ifactualname); return (-1); } Index: head/share/examples/libifconfig/ifcreatevlan.c =================================================================== --- head/share/examples/libifconfig/ifcreatevlan.c (nonexistent) +++ head/share/examples/libifconfig/ifcreatevlan.c (revision 329853) @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + + +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); +} Property changes on: head/share/examples/libifconfig/ifcreatevlan.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/share/examples/libifconfig/ifdestroy.c =================================================================== --- head/share/examples/libifconfig/ifdestroy.c (revision 329852) +++ head/share/examples/libifconfig/ifdestroy.c (revision 329853) @@ -1,84 +1,90 @@ /* - * 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, * 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 #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *ifname; + ifconfig_handle_t *lifh; if (argc != 2) { errx(EINVAL, "Invalid number of arguments." " Only one argument is accepted, and it should be the name" " of the interface to be destroyed."); } /* We have a static number of arguments. Therefore we can do it simple. */ ifname = strdup(argv[1]); 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); lifh = NULL; free(ifname); 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) == SIOCIFDESTROY) { warnx( "Failed to destroy interface (SIOCIFDESTROY)\n"); } break; default: warnx( "Should basically never end up here in this example.\n"); break; } ifconfig_close(lifh); lifh = NULL; free(ifname); return (-1); } Index: head/share/examples/libifconfig/setdescription.c =================================================================== --- head/share/examples/libifconfig/setdescription.c (revision 329852) +++ head/share/examples/libifconfig/setdescription.c (revision 329853) @@ -1,89 +1,98 @@ /* - * 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, * 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 +#include + #include #include #include #include #include #include int main(int argc, char *argv[]) { char *ifname, *ifdescr, *curdescr; + ifconfig_handle_t *lifh; if (argc != 3) { errx(EINVAL, "Invalid number of arguments." " First argument should be interface name, second argument" " should be the description to set."); } /* We have a static number of arguments. Therefore we can do it simple. */ ifname = strdup(argv[1]); ifdescr = strdup(argv[2]); curdescr = NULL; 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); } printf("New description: %s\n\n", ifdescr); if (ifconfig_set_description(lifh, ifname, ifdescr) == 0) { printf("New description successfully set.\n"); } else { switch (ifconfig_err_errtype(lifh)) { case SOCKET: err(ifconfig_err_errno(lifh), "Socket error"); break; case IOCTL: err(ifconfig_err_errno( lifh), "IOCTL(%lu) error", ifconfig_err_ioctlreq(lifh)); break; - case OTHER: + default: err(ifconfig_err_errno(lifh), "Other error"); break; } } free(ifname); free(ifdescr); free(curdescr); ifname = NULL; ifdescr = NULL; curdescr = NULL; ifconfig_close(lifh); return (0); } Index: head/share/examples/libifconfig/setmtu.c =================================================================== --- head/share/examples/libifconfig/setmtu.c (revision 329852) +++ head/share/examples/libifconfig/setmtu.c (revision 329853) @@ -1,92 +1,98 @@ /* - * 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, * 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 #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *ifname, *ptr; int mtu; + ifconfig_handle_t *lifh; if (argc != 3) { errx(EINVAL, "Invalid number of arguments." " First argument should be interface name, second argument" " should be the MTU to set."); } /* We have a static number of arguments. Therefore we can do it simple. */ ifname = strdup(argv[1]); mtu = (int)strtol(argv[2], &ptr, 10); 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); lifh = NULL; free(ifname); 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) == SIOCSIFMTU) { warnx("Failed to set MTU (SIOCSIFMTU)\n"); } else { warnx( "Failed to set MTU due to error in unexpected ioctl() call %lu. Error code: %i.\n", ifconfig_err_ioctlreq(lifh), ifconfig_err_errno(lifh)); } break; default: warnx( "Should basically never end up here in this example.\n"); break; } ifconfig_close(lifh); lifh = NULL; free(ifname); return (-1); } Index: head/share/examples/libifconfig/status.c =================================================================== --- head/share/examples/libifconfig/status.c (nonexistent) +++ head/share/examples/libifconfig/status.c (revision 329853) @@ -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 +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +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 = ""; + 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); +} Property changes on: head/share/examples/libifconfig/status.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.sbin/wlandebug/wlandebug.c =================================================================== --- head/usr.sbin/wlandebug/wlandebug.c (revision 329852) +++ head/usr.sbin/wlandebug/wlandebug.c (revision 329853) @@ -1,267 +1,269 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting * 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, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any * redistribution must be conditioned upon including a substantially * similar Disclaimer requirement for further binary redistribution. * * NO WARRANTY * 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 NONINFRINGEMENT, MERCHANTIBILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES. * * $FreeBSD$ */ /* * wlandebug [-i interface] flags * (default interface is wlan.0). */ #include #include +#include + #include #include #include #include #include #include #include #define N(a) (sizeof(a)/sizeof(a[0])) const char *progname; #define IEEE80211_MSG_11N 0x80000000 /* 11n mode debug */ #define IEEE80211_MSG_DEBUG 0x40000000 /* IFF_DEBUG equivalent */ #define IEEE80211_MSG_DUMPPKTS 0x20000000 /* IFF_LINK2 equivalant */ #define IEEE80211_MSG_CRYPTO 0x10000000 /* crypto work */ #define IEEE80211_MSG_INPUT 0x08000000 /* input handling */ #define IEEE80211_MSG_XRATE 0x04000000 /* rate set handling */ #define IEEE80211_MSG_ELEMID 0x02000000 /* element id parsing */ #define IEEE80211_MSG_NODE 0x01000000 /* node handling */ #define IEEE80211_MSG_ASSOC 0x00800000 /* association handling */ #define IEEE80211_MSG_AUTH 0x00400000 /* authentication handling */ #define IEEE80211_MSG_SCAN 0x00200000 /* scanning */ #define IEEE80211_MSG_OUTPUT 0x00100000 /* output handling */ #define IEEE80211_MSG_STATE 0x00080000 /* state machine */ #define IEEE80211_MSG_POWER 0x00040000 /* power save handling */ #define IEEE80211_MSG_HWMP 0x00020000 /* hybrid mesh protocol */ #define IEEE80211_MSG_DOT1XSM 0x00010000 /* 802.1x state machine */ #define IEEE80211_MSG_RADIUS 0x00008000 /* 802.1x radius client */ #define IEEE80211_MSG_RADDUMP 0x00004000 /* dump 802.1x radius packets */ #define IEEE80211_MSG_MESH 0x00002000 /* mesh networking */ #define IEEE80211_MSG_WPA 0x00001000 /* WPA/RSN protocol */ #define IEEE80211_MSG_ACL 0x00000800 /* ACL handling */ #define IEEE80211_MSG_WME 0x00000400 /* WME protocol */ #define IEEE80211_MSG_SUPERG 0x00000200 /* Atheros SuperG protocol */ #define IEEE80211_MSG_DOTH 0x00000100 /* 802.11h support */ #define IEEE80211_MSG_INACT 0x00000080 /* inactivity handling */ #define IEEE80211_MSG_ROAM 0x00000040 /* sta-mode roaming */ #define IEEE80211_MSG_RATECTL 0x00000020 /* tx rate control */ #define IEEE80211_MSG_ACTION 0x00000010 /* action frame handling */ #define IEEE80211_MSG_WDS 0x00000008 /* WDS handling */ #define IEEE80211_MSG_IOCTL 0x00000004 /* ioctl handling */ #define IEEE80211_MSG_TDMA 0x00000002 /* TDMA handling */ static struct { const char *name; u_int bit; } flags[] = { { "11n", IEEE80211_MSG_11N }, { "debug", IEEE80211_MSG_DEBUG }, { "dumppkts", IEEE80211_MSG_DUMPPKTS }, { "crypto", IEEE80211_MSG_CRYPTO }, { "input", IEEE80211_MSG_INPUT }, { "xrate", IEEE80211_MSG_XRATE }, { "elemid", IEEE80211_MSG_ELEMID }, { "node", IEEE80211_MSG_NODE }, { "assoc", IEEE80211_MSG_ASSOC }, { "auth", IEEE80211_MSG_AUTH }, { "scan", IEEE80211_MSG_SCAN }, { "output", IEEE80211_MSG_OUTPUT }, { "state", IEEE80211_MSG_STATE }, { "power", IEEE80211_MSG_POWER }, { "hwmp", IEEE80211_MSG_HWMP }, { "dot1xsm", IEEE80211_MSG_DOT1XSM }, { "radius", IEEE80211_MSG_RADIUS }, { "raddump", IEEE80211_MSG_RADDUMP }, { "mesh", IEEE80211_MSG_MESH }, { "wpa", IEEE80211_MSG_WPA }, { "acl", IEEE80211_MSG_ACL }, { "wme", IEEE80211_MSG_WME }, { "superg", IEEE80211_MSG_SUPERG }, { "doth", IEEE80211_MSG_DOTH }, { "inact", IEEE80211_MSG_INACT }, { "roam", IEEE80211_MSG_ROAM }, { "rate", IEEE80211_MSG_RATECTL }, { "action", IEEE80211_MSG_ACTION }, { "wds", IEEE80211_MSG_WDS }, { "ioctl", IEEE80211_MSG_IOCTL }, { "tdma", IEEE80211_MSG_TDMA }, }; static u_int getflag(const char *name, int len) { int i; for (i = 0; i < N(flags); i++) if (strncasecmp(flags[i].name, name, len) == 0) return flags[i].bit; return 0; } static void usage(void) { int i; fprintf(stderr, "usage: %s [-d | -i device] [flags]\n", progname); fprintf(stderr, "where flags are:\n"); for (i = 0; i < N(flags); i++) printf("%s\n", flags[i].name); exit(-1); } static void setoid(char oid[], size_t oidlen, const char *wlan) { #ifdef __linux__ if (wlan) snprintf(oid, oidlen, "net.%s.debug", wlan); #elif __FreeBSD__ if (wlan) snprintf(oid, oidlen, "net.wlan.%s.debug", wlan+4); else snprintf(oid, oidlen, "net.wlan.debug"); #elif __NetBSD__ if (wlan) snprintf(oid, oidlen, "net.link.ieee80211.%s.debug", wlan); else snprintf(oid, oidlen, "net.link.ieee80211.debug"); #else #error "No support for this system" #endif } static void get_orig_iface_name(char *oid, size_t oid_size, char *name) { struct ifconfig_handle *h; char *orig_name; h = ifconfig_open(); if (ifconfig_get_orig_name(h, name, &orig_name) < 0) { /* check for original interface name. */ orig_name = name; } if (strlen(orig_name) < strlen("wlan") + 1 || strncmp(orig_name, "wlan", 4) != 0) errx(1, "expecting a wlan interface name"); ifconfig_close(h); setoid(oid, oid_size, orig_name); if (orig_name != name) free(orig_name); } int main(int argc, char *argv[]) { const char *cp, *tp; const char *sep; int op, i; u_int32_t debug, ndebug; size_t debuglen; char oid[256]; progname = argv[0]; setoid(oid, sizeof(oid), "wlan0"); if (argc > 1) { if (strcmp(argv[1], "-d") == 0) { setoid(oid, sizeof(oid), NULL); argc -= 1, argv += 1; } else if (strcmp(argv[1], "-i") == 0) { if (argc <= 2) errx(1, "missing interface name for -i option"); get_orig_iface_name(oid, sizeof(oid), argv[2]); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-?") == 0) usage(); } debuglen = sizeof(debug); if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0) err(1, "sysctl-get(%s)", oid); ndebug = debug; for (; argc > 1; argc--, argv++) { cp = argv[1]; do { u_int bit; if (*cp == '-') { cp++; op = -1; } else if (*cp == '+') { cp++; op = 1; } else op = 0; for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';) tp++; bit = getflag(cp, tp-cp); if (op < 0) ndebug &= ~bit; else if (op > 0) ndebug |= bit; else { if (bit == 0) { int c = *cp; if (isdigit(c)) bit = strtoul(cp, NULL, 0); else errx(1, "unknown flag %.*s", (int)(tp-cp), cp); } ndebug = bit; } } while (*(cp = tp) != '\0'); } if (debug != ndebug) { printf("%s: 0x%x => ", oid, debug); if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0) err(1, "sysctl-set(%s)", oid); printf("0x%x", ndebug); debug = ndebug; } else printf("%s: 0x%x", oid, debug); sep = "<"; for (i = 0; i < N(flags); i++) if (debug & flags[i].bit) { printf("%s%s", sep, flags[i].name); sep = ","; } printf("%s\n", *sep != '<' ? ">" : ""); return 0; }