]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/ifclone.c
ping: fix data type of a variable for a packet sequence number
[FreeBSD/FreeBSD.git] / sbin / ifconfig / ifclone.c
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 <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "ifconfig.h"
50
51 static void
52 list_cloners(void)
53 {
54         struct if_clonereq ifcr;
55         char *cp, *buf;
56         int idx;
57         int s;
58
59         s = socket(AF_LOCAL, SOCK_DGRAM, 0);
60         if (s == -1)
61                 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
62
63         memset(&ifcr, 0, sizeof(ifcr));
64
65         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
66                 err(1, "SIOCIFGCLONERS for count");
67
68         buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
69         if (buf == NULL)
70                 err(1, "unable to allocate cloner name buffer");
71
72         ifcr.ifcr_count = ifcr.ifcr_total;
73         ifcr.ifcr_buffer = buf;
74
75         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
76                 err(1, "SIOCIFGCLONERS for names");
77
78         /*
79          * In case some disappeared in the mean time, clamp it down.
80          */
81         if (ifcr.ifcr_count > ifcr.ifcr_total)
82                 ifcr.ifcr_count = ifcr.ifcr_total;
83
84         for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
85                 if (idx > 0)
86                         putchar(' ');
87                 printf("%s", cp);
88         }
89
90         putchar('\n');
91         free(buf);
92         close(s);
93 }
94
95 struct clone_defcb {
96         char ifprefix[IFNAMSIZ];
97         clone_callback_func *clone_cb;
98         SLIST_ENTRY(clone_defcb) next;
99 };
100
101 static SLIST_HEAD(, clone_defcb) clone_defcbh =
102    SLIST_HEAD_INITIALIZER(clone_defcbh);
103
104 void
105 clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
106 {
107         struct clone_defcb *dcp;
108
109         dcp = malloc(sizeof(*dcp));
110         strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
111         dcp->clone_cb = p;
112         SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
113 }
114
115 /*
116  * Do the actual clone operation.  Any parameters must have been
117  * setup by now.  If a callback has been setup to do the work
118  * then defer to it; otherwise do a simple create operation with
119  * no parameters.
120  */
121 static void
122 ifclonecreate(int s, void *arg)
123 {
124         struct ifreq ifr;
125         struct clone_defcb *dcp;
126         clone_callback_func *clone_cb = NULL;
127
128         memset(&ifr, 0, sizeof(ifr));
129         (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
130
131         if (clone_cb == NULL) {
132                 /* Try to find a default callback */
133                 SLIST_FOREACH(dcp, &clone_defcbh, next) {
134                         if (strncmp(dcp->ifprefix, ifr.ifr_name,
135                             strlen(dcp->ifprefix)) == 0) {
136                                 clone_cb = dcp->clone_cb;
137                                 break;
138                         }
139                 }
140         }
141         if (clone_cb == NULL) {
142                 /* NB: no parameters */
143                 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
144                         err(1, "SIOCIFCREATE2");
145         } else {
146                 clone_cb(s, &ifr);
147         }
148
149         /*
150          * If we get a different name back than we put in, update record and
151          * indicate it should be printed later.
152          */
153         if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
154                 strlcpy(name, ifr.ifr_name, sizeof(name));
155                 printifname = 1;
156         }
157 }
158
159 static
160 DECL_CMD_FUNC(clone_create, arg, d)
161 {
162         callback_register(ifclonecreate, NULL);
163 }
164
165 static
166 DECL_CMD_FUNC(clone_destroy, arg, d)
167 {
168         (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
169         if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
170                 err(1, "SIOCIFDESTROY");
171 }
172
173 static struct cmd clone_cmds[] = {
174         DEF_CLONE_CMD("create", 0,      clone_create),
175         DEF_CMD("destroy",      0,      clone_destroy),
176         DEF_CLONE_CMD("plumb",  0,      clone_create),
177         DEF_CMD("unplumb",      0,      clone_destroy),
178 };
179
180 static void
181 clone_Copt_cb(const char *optarg __unused)
182 {
183         list_cloners();
184         exit(exit_code);
185 }
186 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
187
188 static __constructor void
189 clone_ctor(void)
190 {
191         size_t i;
192
193         for (i = 0; i < nitems(clone_cmds);  i++)
194                 cmd_register(&clone_cmds[i]);
195         opt_register(&clone_Copt);
196 }