1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char rcsid[] =
34 "$FreeBSD$";
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42
43 #include <err.h>
44 #ifndef FSTACK
45 #include <libifconfig.h>
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "ifconfig.h"
53
54 typedef enum {
55 MT_PREFIX,
56 MT_FILTER,
57 } clone_match_type;
58
59 static void
list_cloners(void)60 list_cloners(void)
61 {
62 #ifndef FSTACK
63 ifconfig_handle_t *lifh;
64 char *cloners;
65 size_t cloners_count;
66
67 lifh = ifconfig_open();
68 if (lifh == NULL)
69 return;
70
71 if (ifconfig_list_cloners(lifh, &cloners, &cloners_count) < 0)
72 errc(1, ifconfig_err_errno(lifh), "unable to list cloners");
73 ifconfig_close(lifh);
74
75 for (const char *name = cloners;
76 name < cloners + cloners_count * IFNAMSIZ;
77 name += IFNAMSIZ) {
78 if (name > cloners)
79 putchar(' ');
80 printf("%s", name);
81 }
82 putchar('\n');
83 free(cloners);
84 #else
85 struct if_clonereq ifcr;
86 char *cp, *buf;
87 int idx;
88 int s;
89
90 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
91 if (s == -1)
92 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
93
94 memset(&ifcr, 0, sizeof(ifcr));
95
96 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
97 err(1, "SIOCIFGCLONERS for count");
98
99 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
100 if (buf == NULL)
101 err(1, "unable to allocate cloner name buffer");
102
103 ifcr.ifcr_count = ifcr.ifcr_total;
104 ifcr.ifcr_buffer = buf;
105
106 size_t offset = (char *)&(ifcr.ifcr_buffer) - (char *)&(ifcr);
107 size_t clen = ifcr.ifcr_total * IFNAMSIZ;
108 if (ioctl_va(s, SIOCIFGCLONERS, &ifcr, 3, offset, buf, clen) < 0)
109 err(1, "SIOCIFGCLONERS for names");
110
111 /*
112 * In case some disappeared in the mean time, clamp it down.
113 */
114 if (ifcr.ifcr_count > ifcr.ifcr_total)
115 ifcr.ifcr_count = ifcr.ifcr_total;
116
117 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
118 if (idx > 0)
119 putchar(' ');
120 printf("%s", cp);
121 }
122
123 putchar('\n');
124 free(buf);
125 #endif
126 }
127
128 struct clone_defcb {
129 union {
130 char ifprefix[IFNAMSIZ];
131 clone_match_func *ifmatch;
132 };
133 clone_match_type clone_mt;
134 clone_callback_func *clone_cb;
135 SLIST_ENTRY(clone_defcb) next;
136 };
137
138 static SLIST_HEAD(, clone_defcb) clone_defcbh =
139 SLIST_HEAD_INITIALIZER(clone_defcbh);
140
141 void
clone_setdefcallback_prefix(const char * ifprefix,clone_callback_func * p)142 clone_setdefcallback_prefix(const char *ifprefix, clone_callback_func *p)
143 {
144 struct clone_defcb *dcp;
145
146 dcp = malloc(sizeof(*dcp));
147 strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
148 dcp->clone_mt = MT_PREFIX;
149 dcp->clone_cb = p;
150 SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
151 }
152
153 void
clone_setdefcallback_filter(clone_match_func * filter,clone_callback_func * p)154 clone_setdefcallback_filter(clone_match_func *filter, clone_callback_func *p)
155 {
156 struct clone_defcb *dcp;
157
158 dcp = malloc(sizeof(*dcp));
159 dcp->ifmatch = filter;
160 dcp->clone_mt = MT_FILTER;
161 dcp->clone_cb = p;
162 SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
163 }
164
165 /*
166 * Do the actual clone operation. Any parameters must have been
167 * setup by now. If a callback has been setup to do the work
168 * then defer to it; otherwise do a simple create operation with
169 * no parameters.
170 */
171 static void
ifclonecreate(int s,void * arg)172 ifclonecreate(int s, void *arg)
173 {
174 struct ifreq ifr;
175 struct clone_defcb *dcp;
176
177 memset(&ifr, 0, sizeof(ifr));
178 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
179
180 /* Try to find a default callback by filter */
181 SLIST_FOREACH(dcp, &clone_defcbh, next) {
182 if (dcp->clone_mt == MT_FILTER &&
183 dcp->ifmatch(ifr.ifr_name) != 0)
184 break;
185 }
186
187 if (dcp == NULL) {
188 /* Try to find a default callback by prefix */
189 SLIST_FOREACH(dcp, &clone_defcbh, next) {
190 if (dcp->clone_mt == MT_PREFIX &&
191 strncmp(dcp->ifprefix, ifr.ifr_name,
192 strlen(dcp->ifprefix)) == 0)
193 break;
194 }
195 }
196
197 if (dcp == NULL || dcp->clone_cb == NULL) {
198 /* NB: no parameters */
199 ioctl_ifcreate(s, &ifr);
200 } else {
201 dcp->clone_cb(s, &ifr);
202 }
203
204 /*
205 * If we get a different name back than we put in, update record and
206 * indicate it should be printed later.
207 */
208 if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
209 strlcpy(name, ifr.ifr_name, sizeof(name));
210 printifname = 1;
211 }
212 }
213
214 static
DECL_CMD_FUNC(clone_create,arg,d)215 DECL_CMD_FUNC(clone_create, arg, d)
216 {
217 callback_register(ifclonecreate, NULL);
218 }
219
220 static
DECL_CMD_FUNC(clone_destroy,arg,d)221 DECL_CMD_FUNC(clone_destroy, arg, d)
222 {
223 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
224 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
225 err(1, "SIOCIFDESTROY");
226 }
227
228 static struct cmd clone_cmds[] = {
229 DEF_CLONE_CMD("create", 0, clone_create),
230 DEF_CMD("destroy", 0, clone_destroy),
231 DEF_CLONE_CMD("plumb", 0, clone_create),
232 DEF_CMD("unplumb", 0, clone_destroy),
233 };
234
235 static void
clone_Copt_cb(const char * optarg __unused)236 clone_Copt_cb(const char *optarg __unused)
237 {
238 list_cloners();
239 exit(exit_code);
240 }
241 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
242
243 static __constructor void
clone_ctor(void)244 clone_ctor(void)
245 {
246 size_t i;
247
248 for (i = 0; i < nitems(clone_cmds); i++)
249 cmd_register(&clone_cmds[i]);
250 opt_register(&clone_Copt);
251 }
252