xref: /freebsd/sbin/ifconfig/ifclone.c (revision 25fb30bd9abc492359ad1f66901a06cb8cd08370)
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 #include <libifconfig.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "ifconfig.h"
51 
52 typedef enum {
53 	MT_PREFIX,
54 	MT_FILTER,
55 } clone_match_type;
56 
57 static void
58 list_cloners(void)
59 {
60 	ifconfig_handle_t *lifh;
61 	char *cloners;
62 	size_t cloners_count;
63 
64 	lifh = ifconfig_open();
65 	if (lifh == NULL)
66 		return;
67 
68 	if (ifconfig_list_cloners(lifh, &cloners, &cloners_count) < 0)
69 		errc(1, ifconfig_err_errno(lifh), "unable to list cloners");
70 	ifconfig_close(lifh);
71 
72 	for (const char *name = cloners;
73 	    name < cloners + cloners_count * IFNAMSIZ;
74 	    name += IFNAMSIZ) {
75 		if (name > cloners)
76 			putchar(' ');
77 		printf("%s", name);
78 	}
79 	putchar('\n');
80 	free(cloners);
81 }
82 
83 struct clone_defcb {
84 	union {
85 		char ifprefix[IFNAMSIZ];
86 		clone_match_func *ifmatch;
87 	};
88 	clone_match_type clone_mt;
89 	clone_callback_func *clone_cb;
90 	SLIST_ENTRY(clone_defcb) next;
91 };
92 
93 static SLIST_HEAD(, clone_defcb) clone_defcbh =
94    SLIST_HEAD_INITIALIZER(clone_defcbh);
95 
96 void
97 clone_setdefcallback_prefix(const char *ifprefix, clone_callback_func *p)
98 {
99 	struct clone_defcb *dcp;
100 
101 	dcp = malloc(sizeof(*dcp));
102 	strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
103 	dcp->clone_mt = MT_PREFIX;
104 	dcp->clone_cb = p;
105 	SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
106 }
107 
108 void
109 clone_setdefcallback_filter(clone_match_func *filter, clone_callback_func *p)
110 {
111 	struct clone_defcb *dcp;
112 
113 	dcp = malloc(sizeof(*dcp));
114 	dcp->ifmatch  = filter;
115 	dcp->clone_mt = MT_FILTER;
116 	dcp->clone_cb = p;
117 	SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
118 }
119 
120 /*
121  * Do the actual clone operation.  Any parameters must have been
122  * setup by now.  If a callback has been setup to do the work
123  * then defer to it; otherwise do a simple create operation with
124  * no parameters.
125  */
126 static void
127 ifclonecreate(int s, void *arg)
128 {
129 	struct ifreq ifr;
130 	struct clone_defcb *dcp;
131 	clone_callback_func *clone_cb = NULL;
132 
133 	memset(&ifr, 0, sizeof(ifr));
134 	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
135 
136 	if (clone_cb == NULL) {
137 		/* Try to find a default callback */
138 		SLIST_FOREACH(dcp, &clone_defcbh, next) {
139 			if ((dcp->clone_mt == MT_PREFIX) &&
140 			    (strncmp(dcp->ifprefix, ifr.ifr_name,
141 			             strlen(dcp->ifprefix)) == 0)) {
142 				clone_cb = dcp->clone_cb;
143 				break;
144 			}
145 			if ((dcp->clone_mt == MT_FILTER) &&
146 			          dcp->ifmatch(ifr.ifr_name)) {
147 				clone_cb = dcp->clone_cb;
148 				break;
149 			}
150 		}
151 	}
152 	if (clone_cb == NULL) {
153 		/* NB: no parameters */
154 		if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
155 			err(1, "SIOCIFCREATE2");
156 	} else {
157 		clone_cb(s, &ifr);
158 	}
159 
160 	/*
161 	 * If we get a different name back than we put in, update record and
162 	 * indicate it should be printed later.
163 	 */
164 	if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
165 		strlcpy(name, ifr.ifr_name, sizeof(name));
166 		printifname = 1;
167 	}
168 }
169 
170 static
171 DECL_CMD_FUNC(clone_create, arg, d)
172 {
173 	callback_register(ifclonecreate, NULL);
174 }
175 
176 static
177 DECL_CMD_FUNC(clone_destroy, arg, d)
178 {
179 	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
180 	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
181 		err(1, "SIOCIFDESTROY");
182 }
183 
184 static struct cmd clone_cmds[] = {
185 	DEF_CLONE_CMD("create",	0,	clone_create),
186 	DEF_CMD("destroy",	0,	clone_destroy),
187 	DEF_CLONE_CMD("plumb",	0,	clone_create),
188 	DEF_CMD("unplumb",	0,	clone_destroy),
189 };
190 
191 static void
192 clone_Copt_cb(const char *optarg __unused)
193 {
194 	list_cloners();
195 	exit(exit_code);
196 }
197 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
198 
199 static __constructor void
200 clone_ctor(void)
201 {
202 	size_t i;
203 
204 	for (i = 0; i < nitems(clone_cmds);  i++)
205 		cmd_register(&clone_cmds[i]);
206 	opt_register(&clone_Copt);
207 }
208