xref: /freebsd/sbin/ifconfig/ifclone.c (revision 315ee00f)
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 	char *cloners;
61 	size_t cloners_count;
62 
63 	if (ifconfig_list_cloners(lifh, &cloners, &cloners_count) < 0)
64 		errc(1, ifconfig_err_errno(lifh), "unable to list cloners");
65 
66 	for (const char *name = cloners;
67 	    name < cloners + cloners_count * IFNAMSIZ;
68 	    name += IFNAMSIZ) {
69 		if (name > cloners)
70 			putchar(' ');
71 		printf("%s", name);
72 	}
73 	putchar('\n');
74 	free(cloners);
75 }
76 
77 struct clone_defcb {
78 	union {
79 		char ifprefix[IFNAMSIZ];
80 		clone_match_func *ifmatch;
81 	};
82 	clone_match_type clone_mt;
83 	clone_callback_func *clone_cb;
84 	SLIST_ENTRY(clone_defcb) next;
85 };
86 
87 static SLIST_HEAD(, clone_defcb) clone_defcbh =
88    SLIST_HEAD_INITIALIZER(clone_defcbh);
89 
90 void
91 clone_setdefcallback_prefix(const char *ifprefix, clone_callback_func *p)
92 {
93 	struct clone_defcb *dcp;
94 
95 	dcp = malloc(sizeof(*dcp));
96 	strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
97 	dcp->clone_mt = MT_PREFIX;
98 	dcp->clone_cb = p;
99 	SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
100 }
101 
102 void
103 clone_setdefcallback_filter(clone_match_func *filter, clone_callback_func *p)
104 {
105 	struct clone_defcb *dcp;
106 
107 	dcp = malloc(sizeof(*dcp));
108 	dcp->ifmatch  = filter;
109 	dcp->clone_mt = MT_FILTER;
110 	dcp->clone_cb = p;
111 	SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
112 }
113 
114 /*
115  * Do the actual clone operation.  Any parameters must have been
116  * setup by now.  If a callback has been setup to do the work
117  * then defer to it; otherwise do a simple create operation with
118  * no parameters.
119  */
120 static void
121 ifclonecreate(if_ctx *ctx, void *arg __unused)
122 {
123 	struct ifreq ifr = {};
124 	struct clone_defcb *dcp;
125 
126 	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
127 
128 	/* Try to find a default callback by filter */
129 	SLIST_FOREACH(dcp, &clone_defcbh, next) {
130 		if (dcp->clone_mt == MT_FILTER &&
131 		    dcp->ifmatch(ifr.ifr_name) != 0)
132 			break;
133 	}
134 
135 	if (dcp == NULL) {
136 		/* Try to find a default callback by prefix */
137 		SLIST_FOREACH(dcp, &clone_defcbh, next) {
138 			if (dcp->clone_mt == MT_PREFIX &&
139 			    strncmp(dcp->ifprefix, ifr.ifr_name,
140 			    strlen(dcp->ifprefix)) == 0)
141 				break;
142 		}
143 	}
144 
145 	if (dcp == NULL || dcp->clone_cb == NULL) {
146 		/* NB: no parameters */
147 		ifcreate_ioctl(ctx, &ifr);
148 	} else {
149 		dcp->clone_cb(ctx, &ifr);
150 	}
151 }
152 
153 static void
154 clone_create(if_ctx *ctx __unused, const char *cmd __unused, int d __unused)
155 {
156 	callback_register(ifclonecreate, NULL);
157 }
158 
159 static void
160 clone_destroy(if_ctx *ctx, const char *cmd __unused, int d __unused)
161 {
162 	struct ifreq ifr = {};
163 
164 	if (ioctl_ctx_ifr(ctx, SIOCIFDESTROY, &ifr) < 0)
165 		err(1, "SIOCIFDESTROY");
166 }
167 
168 static struct cmd clone_cmds[] = {
169 	DEF_CLONE_CMD("create",	0,	clone_create),
170 	DEF_CMD("destroy",	0,	clone_destroy),
171 	DEF_CLONE_CMD("plumb",	0,	clone_create),
172 	DEF_CMD("unplumb",	0,	clone_destroy),
173 };
174 
175 static void
176 clone_Copt_cb(const char *arg __unused)
177 {
178 	list_cloners();
179 	exit(exit_code);
180 }
181 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
182 
183 static __constructor void
184 clone_ctor(void)
185 {
186 	size_t i;
187 
188 	for (i = 0; i < nitems(clone_cmds);  i++)
189 		cmd_register(&clone_cmds[i]);
190 	opt_register(&clone_Copt);
191 }
192