ifclone.c revision 138593
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
31static const char rcsid[] =
32  "$FreeBSD: head/sbin/ifconfig/ifclone.c 138593 2004-12-08 19:18:07Z sam $";
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
48static void
49list_cloners(void)
50{
51	struct if_clonereq ifcr;
52	char *cp, *buf;
53	int idx;
54	int s;
55
56	s = socket(AF_INET, SOCK_DGRAM, 0);
57	if (s == -1)
58		err(1, "socket(AF_INET,SOCK_DGRAM)");
59
60	memset(&ifcr, 0, sizeof(ifcr));
61
62	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
63		err(1, "SIOCIFGCLONERS for count");
64
65	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
66	if (buf == NULL)
67		err(1, "unable to allocate cloner name buffer");
68
69	ifcr.ifcr_count = ifcr.ifcr_total;
70	ifcr.ifcr_buffer = buf;
71
72	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
73		err(1, "SIOCIFGCLONERS for names");
74
75	/*
76	 * In case some disappeared in the mean time, clamp it down.
77	 */
78	if (ifcr.ifcr_count > ifcr.ifcr_total)
79		ifcr.ifcr_count = ifcr.ifcr_total;
80
81	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
82		if (idx > 0)
83			putchar(' ');
84		printf("%s", cp);
85	}
86
87	putchar('\n');
88	free(buf);
89}
90
91void
92clone_create(void)
93{
94	int s;
95
96	s = socket(AF_INET, SOCK_DGRAM, 0);
97	if (s == -1)
98		err(1, "socket(AF_INET,SOCK_DGRAM)");
99
100	memset(&ifr, 0, sizeof(ifr));
101	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
102	if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
103		err(1, "SIOCIFCREATE");
104
105	/*
106	 * If we get a different name back then we put in, we probably
107	 * want to print it out, but we might change our mind later so
108	 * we just signal our intrest and leave the printout for later.
109	 */
110	if (strcmp(name, ifr.ifr_name) != 0) {
111		printname = 1;
112		strlcpy(name, ifr.ifr_name, sizeof(name));
113	}
114
115	close(s);
116}
117
118static void
119clone_destroy(const char *val, int d, int s, const struct afswtch *rafp)
120{
121
122	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
123	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
124		err(1, "SIOCIFDESTROY");
125	/*
126	 * If we create and destroy an interface in the same command,
127	 * there isn't any reason to print it's name.
128	 */
129	printname = 0;
130}
131
132static struct cmd clone_cmds[] = {
133	DEF_CMD("destroy",	0,	clone_destroy),
134	DEF_CMD("unplumb",	0,	clone_destroy),
135};
136
137static void
138clone_Copt_cb(const char *optarg __unused)
139{
140	list_cloners();
141	exit(0);
142}
143static struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
144
145static __constructor void
146clone_ctor(void)
147{
148#define	N(a)	(sizeof(a) / sizeof(a[0]))
149	int i;
150
151	for (i = 0; i < N(clone_cmds);  i++)
152		cmd_register(&clone_cmds[i]);
153	opt_register(&clone_Copt);
154#undef N
155}
156