Merge branch 'vendor/BZIP'
[dragonfly.git] / sbin / ifconfig / ifclone.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: head/sbin/ifconfig/ifclone.c 194799 2009-06-23 23:49:52Z delphij $
30  * $DragonFly$
31  */
32
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include "ifconfig.h"
46
47 static void
48 list_cloners(void)
49 {
50         struct if_clonereq ifcr;
51         char *cp, *buf;
52         int idx;
53         int s;
54
55         s = socket(AF_INET, SOCK_DGRAM, 0);
56         if (s == -1)
57                 err(1, "socket(AF_INET,SOCK_DGRAM)");
58
59         memset(&ifcr, 0, sizeof(ifcr));
60
61         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
62                 err(1, "SIOCIFGCLONERS for count");
63
64         buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
65         if (buf == NULL)
66                 err(1, "unable to allocate cloner name buffer");
67
68         ifcr.ifcr_count = ifcr.ifcr_total;
69         ifcr.ifcr_buffer = buf;
70
71         if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
72                 err(1, "SIOCIFGCLONERS for names");
73
74         /*
75          * In case some disappeared in the mean time, clamp it down.
76          */
77         if (ifcr.ifcr_count > ifcr.ifcr_total)
78                 ifcr.ifcr_count = ifcr.ifcr_total;
79
80         for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
81                 if (idx > 0)
82                         putchar(' ');
83                 printf("%s", cp);
84         }
85
86         putchar('\n');
87         free(buf);
88 }
89
90 struct clone_defcb {
91         char ifprefix[IFNAMSIZ];
92         clone_callback_func *clone_cb;
93         SLIST_ENTRY(clone_defcb) next;
94 };
95
96 static SLIST_HEAD(, clone_defcb) clone_defcbh =
97    SLIST_HEAD_INITIALIZER(clone_defcbh);
98
99 void
100 clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
101 {
102         struct clone_defcb *dcp;
103
104         dcp = malloc(sizeof(*dcp));
105         strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
106         dcp->clone_cb = p;
107         SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
108 }
109
110 /*
111  * Do the actual clone operation.  Any parameters must have been
112  * setup by now.  If a callback has been setup to do the work
113  * then defer to it; otherwise do a simple create operation with
114  * no parameters.
115  */
116 static void
117 ifclonecreate(int s, void *arg)
118 {
119         struct ifreq ifr;
120         struct clone_defcb *dcp;
121         clone_callback_func *clone_cb = NULL;
122
123         memset(&ifr, 0, sizeof(ifr));
124         (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
125
126         if (clone_cb == NULL) {
127                 /* Try to find a default callback */
128                 SLIST_FOREACH(dcp, &clone_defcbh, next) {
129                         if (strncmp(dcp->ifprefix, ifr.ifr_name,
130                             strlen(dcp->ifprefix)) == 0) {
131                                 clone_cb = dcp->clone_cb;
132                                 break;
133                         }
134                 }
135         }
136         if (clone_cb == NULL) {
137                 /* NB: no parameters */
138                 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
139                         err(1, "SIOCIFCREATE2");
140         } else {
141                 clone_cb(s, &ifr);
142         }
143
144         /*
145          * If we get a different name back than we put in, print it.
146          */
147         if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
148                 strlcpy(name, ifr.ifr_name, sizeof(name));
149                 printf("%s\n", name);
150         }
151 }
152
153 static
154 DECL_CMD_FUNC(clone_create, arg, d)
155 {
156         callback_register(ifclonecreate, NULL);
157 }
158
159 static
160 DECL_CMD_FUNC(clone_destroy, arg, d)
161 {
162         (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
163         if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
164                 err(1, "SIOCIFDESTROY");
165 }
166
167 static struct cmd clone_cmds[] = {
168         DEF_CLONE_CMD("create", 0,      clone_create),
169         DEF_CMD("destroy",      0,      clone_destroy),
170         DEF_CLONE_CMD("plumb",  0,      clone_create),
171         DEF_CMD("unplumb",      0,      clone_destroy),
172 };
173
174 static void
175 clone_Copt_cb(const char *optarg __unused)
176 {
177         list_cloners();
178         exit(0);
179 }
180 static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb };
181
182 static __constructor void
183 clone_ctor(void)
184 {
185 #define N(a)    (sizeof(a) / sizeof(a[0]))
186         size_t i;
187
188         for (i = 0; i < N(clone_cmds);  i++)
189                 cmd_register(&clone_cmds[i]);
190         opt_register(&clone_Copt);
191 #undef N
192 }