1138593Ssam/* 2138593Ssam * Copyright (c) 1983, 1993 3138593Ssam * The Regents of the University of California. All rights reserved. 4138593Ssam * 5138593Ssam * Redistribution and use in source and binary forms, with or without 6138593Ssam * modification, are permitted provided that the following conditions 7138593Ssam * are met: 8138593Ssam * 1. Redistributions of source code must retain the above copyright 9138593Ssam * notice, this list of conditions and the following disclaimer. 10138593Ssam * 2. Redistributions in binary form must reproduce the above copyright 11138593Ssam * notice, this list of conditions and the following disclaimer in the 12138593Ssam * documentation and/or other materials provided with the distribution. 13138593Ssam * 4. Neither the name of the University nor the names of its contributors 14138593Ssam * may be used to endorse or promote products derived from this software 15138593Ssam * without specific prior written permission. 16138593Ssam * 17138593Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18138593Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19138593Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20138593Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21138593Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22138593Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23138593Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24138593Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25138593Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26138593Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27138593Ssam * SUCH DAMAGE. 28138593Ssam */ 29138593Ssam 30138593Ssam#ifndef lint 31138593Ssamstatic const char rcsid[] = 32138593Ssam "$FreeBSD$"; 33138593Ssam#endif /* not lint */ 34138593Ssam 35189096Srpaulo#include <sys/queue.h> 36138593Ssam#include <sys/types.h> 37138593Ssam#include <sys/ioctl.h> 38138593Ssam#include <sys/socket.h> 39138593Ssam#include <net/if.h> 40138593Ssam 41138593Ssam#include <err.h> 42138593Ssam#include <stdio.h> 43138593Ssam#include <stdlib.h> 44138593Ssam#include <string.h> 45138593Ssam#include <unistd.h> 46138593Ssam 47138593Ssam#include "ifconfig.h" 48138593Ssam 49138593Ssamstatic void 50138593Ssamlist_cloners(void) 51138593Ssam{ 52138593Ssam struct if_clonereq ifcr; 53138593Ssam char *cp, *buf; 54138593Ssam int idx; 55138593Ssam int s; 56138593Ssam 57189864Sjamie s = socket(AF_LOCAL, SOCK_DGRAM, 0); 58138593Ssam if (s == -1) 59189864Sjamie err(1, "socket(AF_LOCAL,SOCK_DGRAM)"); 60138593Ssam 61138593Ssam memset(&ifcr, 0, sizeof(ifcr)); 62138593Ssam 63138593Ssam if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) 64138593Ssam err(1, "SIOCIFGCLONERS for count"); 65138593Ssam 66138593Ssam buf = malloc(ifcr.ifcr_total * IFNAMSIZ); 67138593Ssam if (buf == NULL) 68138593Ssam err(1, "unable to allocate cloner name buffer"); 69138593Ssam 70138593Ssam ifcr.ifcr_count = ifcr.ifcr_total; 71138593Ssam ifcr.ifcr_buffer = buf; 72138593Ssam 73138593Ssam if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) 74138593Ssam err(1, "SIOCIFGCLONERS for names"); 75138593Ssam 76138593Ssam /* 77138593Ssam * In case some disappeared in the mean time, clamp it down. 78138593Ssam */ 79138593Ssam if (ifcr.ifcr_count > ifcr.ifcr_total) 80138593Ssam ifcr.ifcr_count = ifcr.ifcr_total; 81138593Ssam 82138593Ssam for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) { 83138593Ssam if (idx > 0) 84138593Ssam putchar(' '); 85138593Ssam printf("%s", cp); 86138593Ssam } 87138593Ssam 88138593Ssam putchar('\n'); 89138593Ssam free(buf); 90138593Ssam} 91138593Ssam 92189096Srpaulostruct clone_defcb { 93189096Srpaulo char ifprefix[IFNAMSIZ]; 94189096Srpaulo clone_callback_func *clone_cb; 95189096Srpaulo SLIST_ENTRY(clone_defcb) next; 96189096Srpaulo}; 97160196Ssam 98189096Srpaulostatic SLIST_HEAD(, clone_defcb) clone_defcbh = 99189096Srpaulo SLIST_HEAD_INITIALIZER(clone_defcbh); 100189096Srpaulo 101138593Ssamvoid 102189096Srpauloclone_setdefcallback(const char *ifprefix, clone_callback_func *p) 103138593Ssam{ 104189096Srpaulo struct clone_defcb *dcp; 105189096Srpaulo 106189096Srpaulo dcp = malloc(sizeof(*dcp)); 107189096Srpaulo strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1); 108189096Srpaulo dcp->clone_cb = p; 109189096Srpaulo SLIST_INSERT_HEAD(&clone_defcbh, dcp, next); 110160196Ssam} 111138593Ssam 112160196Ssam/* 113160196Ssam * Do the actual clone operation. Any parameters must have been 114160196Ssam * setup by now. If a callback has been setup to do the work 115160196Ssam * then defer to it; otherwise do a simple create operation with 116160196Ssam * no parameters. 117160196Ssam */ 118160196Ssamstatic void 119160196Ssamifclonecreate(int s, void *arg) 120160196Ssam{ 121160196Ssam struct ifreq ifr; 122189096Srpaulo struct clone_defcb *dcp; 123189096Srpaulo clone_callback_func *clone_cb = NULL; 124138593Ssam 125138593Ssam memset(&ifr, 0, sizeof(ifr)); 126138593Ssam (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 127189096Srpaulo 128160196Ssam if (clone_cb == NULL) { 129189096Srpaulo /* Try to find a default callback */ 130189096Srpaulo SLIST_FOREACH(dcp, &clone_defcbh, next) { 131189096Srpaulo if (strncmp(dcp->ifprefix, ifr.ifr_name, 132189096Srpaulo strlen(dcp->ifprefix)) == 0) { 133189096Srpaulo clone_cb = dcp->clone_cb; 134189096Srpaulo break; 135189096Srpaulo } 136189096Srpaulo } 137189096Srpaulo } 138189096Srpaulo if (clone_cb == NULL) { 139160196Ssam /* NB: no parameters */ 140160196Ssam if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) 141160196Ssam err(1, "SIOCIFCREATE2"); 142160196Ssam } else { 143160196Ssam clone_cb(s, &ifr); 144160196Ssam } 145138593Ssam 146138593Ssam /* 147160196Ssam * If we get a different name back than we put in, print it. 148138593Ssam */ 149160196Ssam if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) { 150138593Ssam strlcpy(name, ifr.ifr_name, sizeof(name)); 151160196Ssam printf("%s\n", name); 152138593Ssam } 153160196Ssam} 154138593Ssam 155160196Ssamstatic 156160196SsamDECL_CMD_FUNC(clone_create, arg, d) 157160196Ssam{ 158160196Ssam callback_register(ifclonecreate, NULL); 159138593Ssam} 160138593Ssam 161160196Ssamstatic 162160196SsamDECL_CMD_FUNC(clone_destroy, arg, d) 163138593Ssam{ 164138593Ssam (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 165138593Ssam if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) 166138593Ssam err(1, "SIOCIFDESTROY"); 167138593Ssam} 168138593Ssam 169138593Ssamstatic struct cmd clone_cmds[] = { 170177799Ssam DEF_CLONE_CMD("create", 0, clone_create), 171138593Ssam DEF_CMD("destroy", 0, clone_destroy), 172177799Ssam DEF_CLONE_CMD("plumb", 0, clone_create), 173138593Ssam DEF_CMD("unplumb", 0, clone_destroy), 174138593Ssam}; 175138593Ssam 176138593Ssamstatic void 177138593Ssamclone_Copt_cb(const char *optarg __unused) 178138593Ssam{ 179138593Ssam list_cloners(); 180138593Ssam exit(0); 181138593Ssam} 182194799Sdelphijstatic struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb }; 183138593Ssam 184138593Ssamstatic __constructor void 185138593Ssamclone_ctor(void) 186138593Ssam{ 187138593Ssam#define N(a) (sizeof(a) / sizeof(a[0])) 188194799Sdelphij size_t i; 189138593Ssam 190138593Ssam for (i = 0; i < N(clone_cmds); i++) 191138593Ssam cmd_register(&clone_cmds[i]); 192138593Ssam opt_register(&clone_Copt); 193138593Ssam#undef N 194138593Ssam} 195