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