ifconfig(8): Make lagg creation more fault-tolerant
[dragonfly.git] / sbin / ifconfig / ifclone.c
CommitLineData
46158ff5
AL
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
ca74a0a2
SZ
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
dc71b7ab 15 * 3. Neither the name of the University nor the names of its contributors
ca74a0a2
SZ
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
46158ff5 31 * $FreeBSD: head/sbin/ifconfig/ifclone.c 326025 2017-11-20 19:49:47Z pfg $
ca74a0a2
SZ
32 */
33
46158ff5 34#include <sys/param.h>
c5db41b2 35#include <sys/queue.h>
ca74a0a2
SZ
36#include <sys/ioctl.h>
37#include <sys/socket.h>
38#include <net/if.h>
687c518e 39#include <net/if_clone.h>
ca74a0a2
SZ
40
41#include <err.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47#include "ifconfig.h"
48
49static void
50list_cloners(void)
51{
52 struct if_clonereq ifcr;
53 char *cp, *buf;
54 int idx;
55 int s;
56
d4e85612 57 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
ca74a0a2 58 if (s == -1)
d4e85612 59 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
ca74a0a2
SZ
60
61 memset(&ifcr, 0, sizeof(ifcr));
62
63 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
64 err(1, "SIOCIFGCLONERS for count");
65
66 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
67 if (buf == NULL)
68 err(1, "unable to allocate cloner name buffer");
69
70 ifcr.ifcr_count = ifcr.ifcr_total;
71 ifcr.ifcr_buffer = buf;
72
73 if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
74 err(1, "SIOCIFGCLONERS for names");
75
76 /*
77 * In case some disappeared in the mean time, clamp it down.
78 */
79 if (ifcr.ifcr_count > ifcr.ifcr_total)
80 ifcr.ifcr_count = ifcr.ifcr_total;
81
82 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
83 if (idx > 0)
84 putchar(' ');
85 printf("%s", cp);
86 }
87
88 putchar('\n');
89 free(buf);
46158ff5 90 close(s);
ca74a0a2
SZ
91}
92
c5db41b2
RP
93struct clone_defcb {
94 char ifprefix[IFNAMSIZ];
95 clone_callback_func *clone_cb;
96 SLIST_ENTRY(clone_defcb) next;
97};
98
99static SLIST_HEAD(, clone_defcb) clone_defcbh =
100 SLIST_HEAD_INITIALIZER(clone_defcbh);
101
ca74a0a2 102void
c5db41b2 103clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
ca74a0a2 104{
c5db41b2 105 struct clone_defcb *dcp;
ca74a0a2 106
c5db41b2
RP
107 dcp = malloc(sizeof(*dcp));
108 strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
109 dcp->clone_cb = p;
110 SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
111}
112
113/*
114 * Do the actual clone operation. Any parameters must have been
115 * setup by now. If a callback has been setup to do the work
116 * then defer to it; otherwise do a simple create operation with
117 * no parameters.
118 */
119static void
46158ff5 120ifclonecreate(int s, __unused void *arg)
c5db41b2
RP
121{
122 struct ifreq ifr;
123 struct clone_defcb *dcp;
124 clone_callback_func *clone_cb = NULL;
ca74a0a2
SZ
125
126 memset(&ifr, 0, sizeof(ifr));
80d2947b 127 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
c5db41b2
RP
128
129 if (clone_cb == NULL) {
130 /* Try to find a default callback */
131 SLIST_FOREACH(dcp, &clone_defcbh, next) {
132 if (strncmp(dcp->ifprefix, ifr.ifr_name,
133 strlen(dcp->ifprefix)) == 0) {
134 clone_cb = dcp->clone_cb;
135 break;
136 }
137 }
138 }
139 if (clone_cb == NULL) {
140 /* NB: no parameters */
141 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0)
142 err(1, "SIOCIFCREATE2");
143 } else {
144 clone_cb(s, &ifr);
145 }
ca74a0a2
SZ
146
147 /*
46158ff5
AL
148 * If we get a different name back than we put in, update record and
149 * indicate it should be printed later.
ca74a0a2 150 */
c5db41b2 151 if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) {
ca74a0a2 152 strlcpy(name, ifr.ifr_name, sizeof(name));
46158ff5 153 printifname = 1;
ca74a0a2 154 }
ca74a0a2
SZ
155}
156
c5db41b2
RP
157static
158DECL_CMD_FUNC(clone_create, arg, d)
ca74a0a2 159{
c5db41b2
RP
160 callback_register(ifclonecreate, NULL);
161}
ca74a0a2 162
c5db41b2
RP
163static
164DECL_CMD_FUNC(clone_destroy, arg, d)
165{
80d2947b 166 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ca74a0a2
SZ
167 if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
168 err(1, "SIOCIFDESTROY");
ca74a0a2
SZ
169}
170
171static struct cmd clone_cmds[] = {
c5db41b2 172 DEF_CLONE_CMD("create", 0, clone_create),
ca74a0a2 173 DEF_CMD("destroy", 0, clone_destroy),
c5db41b2 174 DEF_CLONE_CMD("plumb", 0, clone_create),
ca74a0a2
SZ
175 DEF_CMD("unplumb", 0, clone_destroy),
176};
177
178static void
179clone_Copt_cb(const char *optarg __unused)
180{
181 list_cloners();
598a666b 182 exit(exit_code);
ca74a0a2 183}
598a666b 184
46158ff5
AL
185static struct option clone_Copt = {
186 .opt = "C",
187 .opt_usage = "[-C]",
188 .cb = clone_Copt_cb,
189};
ca74a0a2 190
d44b223a 191static __constructor(101) void
ca74a0a2
SZ
192clone_ctor(void)
193{
c5db41b2 194 size_t i;
ca74a0a2 195
598a666b 196 for (i = 0; i < nitems(clone_cmds); i++)
ca74a0a2
SZ
197 cmd_register(&clone_cmds[i]);
198 opt_register(&clone_Copt);
ca74a0a2 199}