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