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: stable/10/sbin/ifconfig/ifclone.c 368505 2020-12-10 09:39:27Z hselasky $"; 33138593Ssam#endif /* not lint */ 34138593Ssam 35289986Sngie#include <sys/param.h> 36289986Sngie#include <sys/ioctl.h> 37189096Srpaulo#include <sys/queue.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); 90319265Sasomers close(s); 91138593Ssam} 92138593Ssam 93189096Srpaulostruct clone_defcb { 94189096Srpaulo char ifprefix[IFNAMSIZ]; 95189096Srpaulo clone_callback_func *clone_cb; 96189096Srpaulo SLIST_ENTRY(clone_defcb) next; 97189096Srpaulo}; 98160196Ssam 99189096Srpaulostatic SLIST_HEAD(, clone_defcb) clone_defcbh = 100189096Srpaulo SLIST_HEAD_INITIALIZER(clone_defcbh); 101189096Srpaulo 102138593Ssamvoid 103189096Srpauloclone_setdefcallback(const char *ifprefix, clone_callback_func *p) 104138593Ssam{ 105189096Srpaulo struct clone_defcb *dcp; 106189096Srpaulo 107189096Srpaulo dcp = malloc(sizeof(*dcp)); 108189096Srpaulo strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1); 109189096Srpaulo dcp->clone_cb = p; 110189096Srpaulo SLIST_INSERT_HEAD(&clone_defcbh, dcp, next); 111160196Ssam} 112138593Ssam 113160196Ssam/* 114160196Ssam * Do the actual clone operation. Any parameters must have been 115160196Ssam * setup by now. If a callback has been setup to do the work 116160196Ssam * then defer to it; otherwise do a simple create operation with 117160196Ssam * no parameters. 118160196Ssam */ 119160196Ssamstatic void 120160196Ssamifclonecreate(int s, void *arg) 121160196Ssam{ 122160196Ssam struct ifreq ifr; 123189096Srpaulo struct clone_defcb *dcp; 124189096Srpaulo clone_callback_func *clone_cb = NULL; 125368505Shselasky const char *ifr_name = strchr(name, '.') ? "vlan" : name; 126138593Ssam 127138593Ssam memset(&ifr, 0, sizeof(ifr)); 128138593Ssam (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 129189096Srpaulo 130160196Ssam if (clone_cb == NULL) { 131189096Srpaulo /* Try to find a default callback */ 132189096Srpaulo SLIST_FOREACH(dcp, &clone_defcbh, next) { 133368505Shselasky if (strncmp(dcp->ifprefix, ifr_name, 134189096Srpaulo strlen(dcp->ifprefix)) == 0) { 135189096Srpaulo clone_cb = dcp->clone_cb; 136189096Srpaulo break; 137189096Srpaulo } 138189096Srpaulo } 139189096Srpaulo } 140189096Srpaulo if (clone_cb == NULL) { 141160196Ssam /* NB: no parameters */ 142160196Ssam if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) 143160196Ssam err(1, "SIOCIFCREATE2"); 144160196Ssam } else { 145160196Ssam clone_cb(s, &ifr); 146160196Ssam } 147138593Ssam 148138593Ssam /* 149296425Skp * If we get a different name back than we put in, update record and 150296425Skp * indicate it should be printed later. 151138593Ssam */ 152160196Ssam if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) { 153138593Ssam strlcpy(name, ifr.ifr_name, sizeof(name)); 154296425Skp printifname = 1; 155138593Ssam } 156160196Ssam} 157138593Ssam 158160196Ssamstatic 159160196SsamDECL_CMD_FUNC(clone_create, arg, d) 160160196Ssam{ 161160196Ssam callback_register(ifclonecreate, NULL); 162138593Ssam} 163138593Ssam 164160196Ssamstatic 165160196SsamDECL_CMD_FUNC(clone_destroy, arg, d) 166138593Ssam{ 167300285Struckman (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 168138593Ssam if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) 169138593Ssam err(1, "SIOCIFDESTROY"); 170138593Ssam} 171138593Ssam 172138593Ssamstatic struct cmd clone_cmds[] = { 173177799Ssam DEF_CLONE_CMD("create", 0, clone_create), 174138593Ssam DEF_CMD("destroy", 0, clone_destroy), 175177799Ssam DEF_CLONE_CMD("plumb", 0, clone_create), 176138593Ssam DEF_CMD("unplumb", 0, clone_destroy), 177138593Ssam}; 178138593Ssam 179138593Ssamstatic void 180138593Ssamclone_Copt_cb(const char *optarg __unused) 181138593Ssam{ 182138593Ssam list_cloners(); 183138593Ssam exit(0); 184138593Ssam} 185194799Sdelphijstatic struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb }; 186138593Ssam 187138593Ssamstatic __constructor void 188138593Ssamclone_ctor(void) 189138593Ssam{ 190194799Sdelphij size_t i; 191138593Ssam 192289986Sngie for (i = 0; i < nitems(clone_cmds); i++) 193138593Ssam cmd_register(&clone_cmds[i]); 194138593Ssam opt_register(&clone_Copt); 195138593Ssam} 196