2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
52 struct if_clonereq ifcr;
57 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
59 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
61 memset(&ifcr, 0, sizeof(ifcr));
63 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
64 err(1, "SIOCIFGCLONERS for count");
66 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
68 err(1, "unable to allocate cloner name buffer");
70 ifcr.ifcr_count = ifcr.ifcr_total;
71 ifcr.ifcr_buffer = buf;
73 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
74 err(1, "SIOCIFGCLONERS for names");
77 * In case some disappeared in the mean time, clamp it down.
79 if (ifcr.ifcr_count > ifcr.ifcr_total)
80 ifcr.ifcr_count = ifcr.ifcr_total;
82 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
93 char ifprefix[IFNAMSIZ];
94 clone_callback_func *clone_cb;
95 SLIST_ENTRY(clone_defcb) next;
98 static SLIST_HEAD(, clone_defcb) clone_defcbh =
99 SLIST_HEAD_INITIALIZER(clone_defcbh);
102 clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
104 struct clone_defcb *dcp;
106 dcp = malloc(sizeof(*dcp));
107 strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
109 SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
113 * Do the actual clone operation. Any parameters must have been
114 * setup by now. If a callback has been setup to do the work
115 * then defer to it; otherwise do a simple create operation with
119 ifclonecreate(int s, void *arg)
122 struct clone_defcb *dcp;
123 clone_callback_func *clone_cb = NULL;
125 memset(&ifr, 0, sizeof(ifr));
126 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
128 if (clone_cb == NULL) {
129 /* Try to find a default callback */
130 SLIST_FOREACH(dcp, &clone_defcbh, next) {
131 if (strncmp(dcp->ifprefix, ifr.ifr_name,
132 strlen(dcp->ifprefix)) == 0) {
133 clone_cb = dcp->clone_cb;
138 if (clone_cb == NULL) {
139 /* NB: no parameters */
140 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
141 err(1, "SIOCIFCREATE2");
147 * If we get a different name back than we put in, print it.
149 if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
150 strlcpy(name, ifr.ifr_name, sizeof(name));
151 printf("%s\n", name);
156 DECL_CMD_FUNC(clone_create, arg, d)
158 callback_register(ifclonecreate, NULL);
162 DECL_CMD_FUNC(clone_destroy, arg, d)
164 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
165 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
166 err(1, "SIOCIFDESTROY");
169 static struct cmd clone_cmds[] = {
170 DEF_CLONE_CMD("create", 0, clone_create),
171 DEF_CMD("destroy", 0, clone_destroy),
172 DEF_CLONE_CMD("plumb", 0, clone_create),
173 DEF_CMD("unplumb", 0, clone_destroy),
177 clone_Copt_cb(const char *optarg __unused)
182 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
184 static __constructor void
187 #define N(a) (sizeof(a) / sizeof(a[0]))
190 for (i = 0; i < N(clone_cmds); i++)
191 cmd_register(&clone_cmds[i]);
192 opt_register(&clone_Copt);