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));
}
Beispiel #3
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);
	return (0);
}
Beispiel #4
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) {
		free(tmpname);
		return (-1);
	}

	free(tmpname);
	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(&params, 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)&params;

	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_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) {
		return (-1);
	}
	return (0);
}
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);
}
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);
}
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) {
		return (-1);
	}
	return (0);
}
Beispiel #10
0
int
ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib)
{
	struct ifreq ifr;

	memset(&ifr, 0, sizeof(ifr));
	(void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));

	if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCGIFFIB, &ifr) == -1) {
		return (-1);
	}

	*fib = ifr.ifr_fib;
	return (0);
}
Beispiel #11
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) {
		return (-1);
	}

	*metric = ifr.ifr_metric;
	return (0);
}
Beispiel #12
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) {
		return (-1);
	}

	return (0);
}
Beispiel #13
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) {
		return (-1);
	}
	capability->curcap = ifr.ifr_curcap;
	capability->reqcap = ifr.ifr_reqcap;
	return (0);
}
Beispiel #14
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);
}
Beispiel #15
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(&params, sizeof(params));
	params.vlr_tag = vlantag;
	strlcpy(params.vlr_parent, vlandev, sizeof(params.vlr_parent));

	ifr.ifr_data = (caddr_t)&params;
	(void)strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
	if (ifconfig_ioctlwrap(h, AF_LOCAL, SIOCSETVLAN, &ifr) == -1) {
		return (-1);
	}
	return (0);
}
Beispiel #16
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) {
		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) {
		return (-1);
	}
	return (0);
}
Beispiel #17
0
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) {
		free(ifr.ifr_buffer.buffer);
		return (-1);
	}

	free(ifr.ifr_buffer.buffer);
	return (0);
}