ifclone.c revision 177799
177542Swpaul/*
277542Swpaul * Copyright (c) 1983, 1993
377542Swpaul *	The Regents of the University of California.  All rights reserved.
477542Swpaul *
577542Swpaul * Redistribution and use in source and binary forms, with or without
677542Swpaul * modification, are permitted provided that the following conditions
777542Swpaul * are met:
877542Swpaul * 1. Redistributions of source code must retain the above copyright
977542Swpaul *    notice, this list of conditions and the following disclaimer.
1077542Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1177542Swpaul *    notice, this list of conditions and the following disclaimer in the
1277542Swpaul *    documentation and/or other materials provided with the distribution.
1377542Swpaul * 4. Neither the name of the University nor the names of its contributors
1477542Swpaul *    may be used to endorse or promote products derived from this software
1577542Swpaul *    without specific prior written permission.
1677542Swpaul *
1777542Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1877542Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1977542Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2077542Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2177542Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2277542Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2377542Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2477542Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2577542Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2677542Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2777542Swpaul * SUCH DAMAGE.
2877542Swpaul */
2977542Swpaul
3077542Swpaul#ifndef lint
3177542Swpaulstatic const char rcsid[] =
3277542Swpaul  "$FreeBSD: head/sbin/ifconfig/ifclone.c 177799 2008-03-31 15:38:07Z sam $";
3377542Swpaul#endif /* not lint */
3477542Swpaul
3577542Swpaul#include <sys/types.h>
3677542Swpaul#include <sys/ioctl.h>
3777542Swpaul#include <sys/socket.h>
3877542Swpaul#include <net/if.h>
3977542Swpaul
4077542Swpaul#include <err.h>
4177542Swpaul#include <stdio.h>
4277542Swpaul#include <stdlib.h>
4377542Swpaul#include <string.h>
4477542Swpaul#include <unistd.h>
4577542Swpaul
4677542Swpaul#include "ifconfig.h"
4777542Swpaul
4877542Swpaulstatic void
4977542Swpaullist_cloners(void)
5077542Swpaul{
5177542Swpaul	struct if_clonereq ifcr;
5277542Swpaul	char *cp, *buf;
5377542Swpaul	int idx;
5477542Swpaul	int s;
5577542Swpaul
5677542Swpaul	s = socket(AF_INET, SOCK_DGRAM, 0);
5777542Swpaul	if (s == -1)
5877542Swpaul		err(1, "socket(AF_INET,SOCK_DGRAM)");
5977542Swpaul
6077542Swpaul	memset(&ifcr, 0, sizeof(ifcr));
6177542Swpaul
6277542Swpaul	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
6377542Swpaul		err(1, "SIOCIFGCLONERS for count");
6477542Swpaul
6577542Swpaul	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
6677542Swpaul	if (buf == NULL)
6777542Swpaul		err(1, "unable to allocate cloner name buffer");
6877542Swpaul
6977542Swpaul	ifcr.ifcr_count = ifcr.ifcr_total;
7077542Swpaul	ifcr.ifcr_buffer = buf;
7177542Swpaul
7277542Swpaul	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
7377542Swpaul		err(1, "SIOCIFGCLONERS for names");
7477542Swpaul
7577542Swpaul	/*
7677542Swpaul	 * In case some disappeared in the mean time, clamp it down.
7777542Swpaul	 */
7877542Swpaul	if (ifcr.ifcr_count > ifcr.ifcr_total)
7977542Swpaul		ifcr.ifcr_count = ifcr.ifcr_total;
8077542Swpaul
8177542Swpaul	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
8277542Swpaul		if (idx > 0)
8377542Swpaul			putchar(' ');
8477542Swpaul		printf("%s", cp);
8577542Swpaul	}
8677542Swpaul
8777542Swpaul	putchar('\n');
8877542Swpaul	free(buf);
8977542Swpaul}
9077542Swpaul
9177542Swpaulstatic clone_callback_func *clone_cb = NULL;
9277542Swpaul
9377542Swpaulvoid
9477542Swpaulclone_setcallback(clone_callback_func *p)
9577542Swpaul{
9677542Swpaul	if (clone_cb != NULL && clone_cb != p)
9777542Swpaul		errx(1, "conflicting device create parameters");
9877542Swpaul	clone_cb = p;
9977542Swpaul}
10077542Swpaul
10177542Swpaul/*
10277542Swpaul * Do the actual clone operation.  Any parameters must have been
10377542Swpaul * setup by now.  If a callback has been setup to do the work
10477542Swpaul * then defer to it; otherwise do a simple create operation with
10577542Swpaul * no parameters.
10677542Swpaul */
10777542Swpaulstatic void
10877542Swpaulifclonecreate(int s, void *arg)
10977542Swpaul{
11077542Swpaul	struct ifreq ifr;
11177542Swpaul
11277542Swpaul	memset(&ifr, 0, sizeof(ifr));
11377542Swpaul	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
11477542Swpaul	if (clone_cb == NULL) {
11577542Swpaul		/* NB: no parameters */
11677542Swpaul		if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
11777542Swpaul			err(1, "SIOCIFCREATE2");
11877542Swpaul	} else {
11977542Swpaul		clone_cb(s, &ifr);
12077542Swpaul	}
12177542Swpaul
12277542Swpaul	/*
12377542Swpaul	 * If we get a different name back than we put in, print it.
12477542Swpaul	 */
12577542Swpaul	if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
12677542Swpaul		strlcpy(name, ifr.ifr_name, sizeof(name));
12777542Swpaul		printf("%s\n", name);
12899498Salfred	}
12999498Salfred}
13099498Salfred
13177542Swpaulstatic
13299498SalfredDECL_CMD_FUNC(clone_create, arg, d)
13399498Salfred{
13499498Salfred	callback_register(ifclonecreate, NULL);
13599498Salfred}
13677542Swpaul
13799498Salfredstatic
13899498SalfredDECL_CMD_FUNC(clone_destroy, arg, d)
13999498Salfred{
14099498Salfred	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
14199498Salfred	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
14299498Salfred		err(1, "SIOCIFDESTROY");
14399498Salfred}
14499498Salfred
14599498Salfredstatic struct cmd clone_cmds[] = {
14699498Salfred	DEF_CLONE_CMD("create",	0,	clone_create),
14799498Salfred	DEF_CMD("destroy",	0,	clone_destroy),
14899498Salfred	DEF_CLONE_CMD("plumb",	0,	clone_create),
14999498Salfred	DEF_CMD("unplumb",	0,	clone_destroy),
15099498Salfred};
15199498Salfred
15277542Swpaulstatic void
15399498Salfredclone_Copt_cb(const char *optarg __unused)
15499498Salfred{
15577542Swpaul	list_cloners();
15699498Salfred	exit(0);
15799498Salfred}
15899498Salfredstatic struct option clone_Copt = { "C", "[-C]", clone_Copt_cb };
15977542Swpaul
16099498Salfredstatic __constructor void
16199498Salfredclone_ctor(void)
16299498Salfred{
16399498Salfred#define	N(a)	(sizeof(a) / sizeof(a[0]))
16499498Salfred	int i;
16577542Swpaul
16677542Swpaul	for (i = 0; i < N(clone_cmds);  i++)
16777542Swpaul		cmd_register(&clone_cmds[i]);
16877542Swpaul	opt_register(&clone_Copt);
16977542Swpaul#undef N
17077542Swpaul}
17177542Swpaul