xref: /f-stack/tools/ifconfig/ifclone.c (revision ebf5cedb)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <net/if.h>
40 
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "ifconfig.h"
48 
49 static void
50 list_cloners(void)
51 {
52 	struct if_clonereq ifcr;
53 	char *cp, *buf;
54 	int idx;
55 	int s;
56 
57 	s = socket(AF_LOCAL, SOCK_DGRAM, 0);
58 	if (s == -1)
59 		err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
60 
61 	memset(&ifcr, 0, sizeof(ifcr));
62 
63 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
64 		err(1, "SIOCIFGCLONERS for count");
65 
66 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
67 	if (buf == NULL)
68 		err(1, "unable to allocate cloner name buffer");
69 
70 	ifcr.ifcr_count = ifcr.ifcr_total;
71 	ifcr.ifcr_buffer = buf;
72 
73 #ifndef FSTACK
74 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
75 #else
76 	size_t offset = (char *)&(ifcr.ifcr_buffer) - (char *)&(ifcr);
77 	size_t clen = ifcr.ifcr_total * IFNAMSIZ;
78 	if (ioctl_va(s, SIOCIFGCLONERS, &ifcr, 3, offset, buf, clen) < 0)
79 #endif
80 		err(1, "SIOCIFGCLONERS for names");
81 
82 	/*
83 	 * In case some disappeared in the mean time, clamp it down.
84 	 */
85 	if (ifcr.ifcr_count > ifcr.ifcr_total)
86 		ifcr.ifcr_count = ifcr.ifcr_total;
87 
88 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
89 		if (idx > 0)
90 			putchar(' ');
91 		printf("%s", cp);
92 	}
93 
94 	putchar('\n');
95 	free(buf);
96 }
97 
98 struct clone_defcb {
99 	char ifprefix[IFNAMSIZ];
100 	clone_callback_func *clone_cb;
101 	SLIST_ENTRY(clone_defcb) next;
102 };
103 
104 static SLIST_HEAD(, clone_defcb) clone_defcbh =
105    SLIST_HEAD_INITIALIZER(clone_defcbh);
106 
107 void
108 clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
109 {
110 	struct clone_defcb *dcp;
111 
112 	dcp = malloc(sizeof(*dcp));
113 	strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
114 	dcp->clone_cb = p;
115 	SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
116 }
117 
118 /*
119  * Do the actual clone operation.  Any parameters must have been
120  * setup by now.  If a callback has been setup to do the work
121  * then defer to it; otherwise do a simple create operation with
122  * no parameters.
123  */
124 static void
125 ifclonecreate(int s, void *arg)
126 {
127 	struct ifreq ifr;
128 	struct clone_defcb *dcp;
129 	clone_callback_func *clone_cb = NULL;
130 
131 	memset(&ifr, 0, sizeof(ifr));
132 	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
133 
134 	if (clone_cb == NULL) {
135 		/* Try to find a default callback */
136 		SLIST_FOREACH(dcp, &clone_defcbh, next) {
137 			if (strncmp(dcp->ifprefix, ifr.ifr_name,
138 			    strlen(dcp->ifprefix)) == 0) {
139 				clone_cb = dcp->clone_cb;
140 				break;
141 			}
142 		}
143 	}
144 	if (clone_cb == NULL) {
145 		/* NB: no parameters */
146 		if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
147 			err(1, "SIOCIFCREATE2");
148 	} else {
149 		clone_cb(s, &ifr);
150 	}
151 
152 	/*
153 	 * If we get a different name back than we put in, update record and
154 	 * indicate it should be printed later.
155 	 */
156 	if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
157 		strlcpy(name, ifr.ifr_name, sizeof(name));
158 		printifname = 1;
159 	}
160 }
161 
162 static
163 DECL_CMD_FUNC(clone_create, arg, d)
164 {
165 	callback_register(ifclonecreate, NULL);
166 }
167 
168 static
169 DECL_CMD_FUNC(clone_destroy, arg, d)
170 {
171 	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
172 	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
173 		err(1, "SIOCIFDESTROY");
174 }
175 
176 static struct cmd clone_cmds[] = {
177 	DEF_CLONE_CMD("create",	0,	clone_create),
178 	DEF_CMD("destroy",	0,	clone_destroy),
179 	DEF_CLONE_CMD("plumb",	0,	clone_create),
180 	DEF_CMD("unplumb",	0,	clone_destroy),
181 };
182 
183 static void
184 clone_Copt_cb(const char *optarg __unused)
185 {
186 	list_cloners();
187 	exit(0);
188 }
189 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
190 
191 static __constructor void
192 clone_ctor(void)
193 {
194 	size_t i;
195 
196 	for (i = 0; i < nitems(clone_cmds);  i++)
197 		cmd_register(&clone_cmds[i]);
198 	opt_register(&clone_Copt);
199 }
200