2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2004-12-08 19:18:07 +00:00
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2004-12-08 19:18:07 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2015-09-27 07:51:18 +00:00
|
|
|
#include <sys/param.h>
|
2004-12-08 19:18:07 +00:00
|
|
|
#include <sys/ioctl.h>
|
2015-09-27 07:51:18 +00:00
|
|
|
#include <sys/queue.h>
|
2004-12-08 19:18:07 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "ifconfig.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
list_cloners(void)
|
|
|
|
{
|
|
|
|
struct if_clonereq ifcr;
|
|
|
|
char *cp, *buf;
|
|
|
|
int idx;
|
|
|
|
int s;
|
|
|
|
|
2009-03-15 22:33:18 +00:00
|
|
|
s = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
2004-12-08 19:18:07 +00:00
|
|
|
if (s == -1)
|
2009-03-15 22:33:18 +00:00
|
|
|
err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
|
2004-12-08 19:18:07 +00:00
|
|
|
|
|
|
|
memset(&ifcr, 0, sizeof(ifcr));
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
|
|
|
|
err(1, "SIOCIFGCLONERS for count");
|
|
|
|
|
|
|
|
buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
|
|
|
|
if (buf == NULL)
|
|
|
|
err(1, "unable to allocate cloner name buffer");
|
|
|
|
|
|
|
|
ifcr.ifcr_count = ifcr.ifcr_total;
|
|
|
|
ifcr.ifcr_buffer = buf;
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
|
|
|
|
err(1, "SIOCIFGCLONERS for names");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case some disappeared in the mean time, clamp it down.
|
|
|
|
*/
|
|
|
|
if (ifcr.ifcr_count > ifcr.ifcr_total)
|
|
|
|
ifcr.ifcr_count = ifcr.ifcr_total;
|
|
|
|
|
|
|
|
for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
|
|
|
|
if (idx > 0)
|
|
|
|
putchar(' ');
|
|
|
|
printf("%s", cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
free(buf);
|
Various Coverity fixes in ifconfig(8)
* Exit early if kldload(2) fails (1011259). This is the only change that
affects ifconfig's behavior.
* Close memory and resource leaks (1305624, 1305205, 1007100)
* Mark usage() as _Noreturn (1305806, 1305750)
* Fix some dereference after null checks (1011474, 270774)
Reported by: Coverity
CID: 1305624, 1305205, 1007100, 1305806, 1305750, 1011474,
CID: 270774, 1011259
Reviewed by: cem
MFC after: 3 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D10587
2017-05-03 17:21:01 +00:00
|
|
|
close(s);
|
2004-12-08 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-27 00:31:34 +00:00
|
|
|
struct clone_defcb {
|
|
|
|
char ifprefix[IFNAMSIZ];
|
|
|
|
clone_callback_func *clone_cb;
|
|
|
|
SLIST_ENTRY(clone_defcb) next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static SLIST_HEAD(, clone_defcb) clone_defcbh =
|
|
|
|
SLIST_HEAD_INITIALIZER(clone_defcbh);
|
2006-07-09 06:10:23 +00:00
|
|
|
|
2004-12-08 19:18:07 +00:00
|
|
|
void
|
2009-02-27 00:31:34 +00:00
|
|
|
clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
|
2004-12-08 19:18:07 +00:00
|
|
|
{
|
2009-02-27 00:31:34 +00:00
|
|
|
struct clone_defcb *dcp;
|
|
|
|
|
|
|
|
dcp = malloc(sizeof(*dcp));
|
|
|
|
strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
|
|
|
|
dcp->clone_cb = p;
|
|
|
|
SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
|
2006-07-09 06:10:23 +00:00
|
|
|
}
|
2004-12-08 19:18:07 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
/*
|
|
|
|
* Do the actual clone operation. Any parameters must have been
|
|
|
|
* setup by now. If a callback has been setup to do the work
|
|
|
|
* then defer to it; otherwise do a simple create operation with
|
|
|
|
* no parameters.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ifclonecreate(int s, void *arg)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
2009-02-27 00:31:34 +00:00
|
|
|
struct clone_defcb *dcp;
|
|
|
|
clone_callback_func *clone_cb = NULL;
|
2004-12-08 19:18:07 +00:00
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
2009-02-27 00:31:34 +00:00
|
|
|
|
|
|
|
if (clone_cb == NULL) {
|
|
|
|
/* Try to find a default callback */
|
|
|
|
SLIST_FOREACH(dcp, &clone_defcbh, next) {
|
|
|
|
if (strncmp(dcp->ifprefix, ifr.ifr_name,
|
|
|
|
strlen(dcp->ifprefix)) == 0) {
|
|
|
|
clone_cb = dcp->clone_cb;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-09 06:10:23 +00:00
|
|
|
if (clone_cb == NULL) {
|
|
|
|
/* NB: no parameters */
|
|
|
|
if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
|
|
|
|
err(1, "SIOCIFCREATE2");
|
|
|
|
} else {
|
|
|
|
clone_cb(s, &ifr);
|
|
|
|
}
|
2004-12-08 19:18:07 +00:00
|
|
|
|
|
|
|
/*
|
2016-02-20 11:36:35 +00:00
|
|
|
* If we get a different name back than we put in, update record and
|
|
|
|
* indicate it should be printed later.
|
2004-12-08 19:18:07 +00:00
|
|
|
*/
|
2006-07-09 06:10:23 +00:00
|
|
|
if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
|
2004-12-08 19:18:07 +00:00
|
|
|
strlcpy(name, ifr.ifr_name, sizeof(name));
|
2016-02-20 11:36:35 +00:00
|
|
|
printifname = 1;
|
2004-12-08 19:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static
|
|
|
|
DECL_CMD_FUNC(clone_create, arg, d)
|
2004-12-08 19:18:07 +00:00
|
|
|
{
|
2006-07-09 06:10:23 +00:00
|
|
|
callback_register(ifclonecreate, NULL);
|
|
|
|
}
|
2004-12-08 19:18:07 +00:00
|
|
|
|
2006-07-09 06:10:23 +00:00
|
|
|
static
|
|
|
|
DECL_CMD_FUNC(clone_destroy, arg, d)
|
|
|
|
{
|
Use strlcpy() instead of strncpy() when copying ifname to ensure
that it is NUL terminated. Additional NUL padding is not required
for short names.
Use sizeof(destination) in a few places instead of IFNAMSIZ.
Cast afp->af_ridreq and afp->af_addreq to make the intent of
the code more obvious.
Reported by: Coverity
CID: 1009628, 1009630, 1009631, 1009632, 1009633, 1009635, 1009638
CID: 1009639, 1009640, 1009641, 1009642, 1009643, 1009644, 1009645
CID: 1009646, 1009647, 1010049, 1010050, 1010051, 1010052, 1010053
CID: 1010054, 1011293, 1011294, 1011295, 1011296, 1011297, 1011298
CID: 1011299, 1305821, 1351720, 1351721
MFC after: 1 week
2016-05-16 00:25:24 +00:00
|
|
|
(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
2004-12-08 19:18:07 +00:00
|
|
|
if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
|
|
|
|
err(1, "SIOCIFDESTROY");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cmd clone_cmds[] = {
|
2008-03-31 15:38:07 +00:00
|
|
|
DEF_CLONE_CMD("create", 0, clone_create),
|
2004-12-08 19:18:07 +00:00
|
|
|
DEF_CMD("destroy", 0, clone_destroy),
|
2008-03-31 15:38:07 +00:00
|
|
|
DEF_CLONE_CMD("plumb", 0, clone_create),
|
2004-12-08 19:18:07 +00:00
|
|
|
DEF_CMD("unplumb", 0, clone_destroy),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
clone_Copt_cb(const char *optarg __unused)
|
|
|
|
{
|
|
|
|
list_cloners();
|
|
|
|
exit(0);
|
|
|
|
}
|
2009-06-23 23:49:52 +00:00
|
|
|
static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
|
2004-12-08 19:18:07 +00:00
|
|
|
|
|
|
|
static __constructor void
|
|
|
|
clone_ctor(void)
|
|
|
|
{
|
2009-06-23 23:49:52 +00:00
|
|
|
size_t i;
|
2004-12-08 19:18:07 +00:00
|
|
|
|
2015-09-27 07:51:18 +00:00
|
|
|
for (i = 0; i < nitems(clone_cmds); i++)
|
2004-12-08 19:18:07 +00:00
|
|
|
cmd_register(&clone_cmds[i]);
|
|
|
|
opt_register(&clone_Copt);
|
|
|
|
}
|