]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifclone.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / 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
30 #ifndef lint
31 static const char rcsid[] =
32 "$FreeBSD: src/sbin/ifconfig/ifclone.c,v 1.3.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $";
33 #endif /* not lint */
34
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include "ifconfig.h"
47
48 static void
49 list_cloners(void)
50 {
51 #ifdef notdef
52 struct if_clonereq ifcr;
53 char *cp, *buf;
54 int idx;
55 int s;
56
57 s = socket(AF_INET, SOCK_DGRAM, 0);
58 if (s == -1)
59 err(1, "socket(AF_INET,SOCK_DGRAM)");
60
61 memset(&ifcr, 0, sizeof(ifcr));
62
63 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
64 err(1, "SIOCIFGCLONERS for count");
65
66 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
67 if (buf == NULL)
68 err(1, "unable to allocate cloner name buffer");
69
70 ifcr.ifcr_count = ifcr.ifcr_total;
71 ifcr.ifcr_buffer = buf;
72
73 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
74 err(1, "SIOCIFGCLONERS for names");
75
76 /*
77 * In case some disappeared in the mean time, clamp it down.
78 */
79 if (ifcr.ifcr_count > ifcr.ifcr_total)
80 ifcr.ifcr_count = ifcr.ifcr_total;
81
82 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
83 if (idx > 0)
84 putchar(' ');
85 printf("%s", cp);
86 }
87
88 putchar('\n');
89 free(buf);
90 #endif
91 }
92
93 static clone_callback_func *clone_cb = NULL;
94
95 void
96 clone_setcallback(clone_callback_func *p)
97 {
98 if (clone_cb != NULL && clone_cb != p)
99 errx(1, "conflicting device create parameters");
100 clone_cb = p;
101 }
102
103 /*
104 * Do the actual clone operation. Any parameters must have been
105 * setup by now. If a callback has been setup to do the work
106 * then defer to it; otherwise do a simple create operation with
107 * no parameters.
108 */
109 static void
110 ifclonecreate(int s, void *arg)
111 {
112 struct ifreq ifr;
113
114 memset(&ifr, 0, sizeof(ifr));
115 (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
116 if (clone_cb == NULL) {
117 #ifdef SIOCIFCREATE2
118 /* NB: no parameters */
119 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
120 err(1, "SIOCIFCREATE2");
121 #else
122 if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
123 err(1, "SIOCIFCREATE");
124 #endif
125 } else {
126 clone_cb(s, &ifr);
127 }
128
129 /*
130 * If we get a different name back than we put in, print it.
131 */
132 if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
133 strlcpy(name, ifr.ifr_name, sizeof(name));
134 printf("%s\n", name);
135 }
136 }
137
138 static
139 DECL_CMD_FUNC(clone_create, arg, d)
140 {
141 callback_register(ifclonecreate, NULL);
142 }
143
144 static
145 DECL_CMD_FUNC(clone_destroy, arg, d)
146 {
147 (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
148 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
149 err(1, "SIOCIFDESTROY");
150 }
151
152 static struct cmd clone_cmds[] = {
153 DEF_CLONE_CMD("create", 0, clone_create),
154 DEF_CMD("destroy", 0, clone_destroy),
155 DEF_CLONE_CMD("plumb", 0, clone_create),
156 DEF_CMD("unplumb", 0, clone_destroy),
157 };
158
159 static void
160 clone_Copt_cb(const char *optarg __unused)
161 {
162 list_cloners();
163 exit(0);
164 }
165 static struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
166
167 static __constructor void
168 clone_ctor(void)
169 {
170 #define N(a) (sizeof(a) / sizeof(a[0]))
171 int i;
172
173 for (i = 0; i < N(clone_cmds); i++)
174 cmd_register(&clone_cmds[i]);
175 opt_register(&clone_Copt);
176 #undef N
177 }