xref: /dragonfly/sbin/ifconfig/ifgroup.c (revision 7203d4e3)
1d4024308SAaron LI /*-
2d4024308SAaron LI  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3d4024308SAaron LI  *
4d4024308SAaron LI  * Copyright (c) 2006 Max Laier. All rights reserved.
5d4024308SAaron LI  *
6d4024308SAaron LI  * Redistribution and use in source and binary forms, with or without
7d4024308SAaron LI  * modification, are permitted provided that the following conditions
8d4024308SAaron LI  * are met:
9d4024308SAaron LI  * 1. Redistributions of source code must retain the above copyright
10d4024308SAaron LI  *    notice, this list of conditions and the following disclaimer.
11d4024308SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
12d4024308SAaron LI  *    notice, this list of conditions and the following disclaimer in the
13d4024308SAaron LI  *    documentation and/or other materials provided with the distribution.
14d4024308SAaron LI  *
15d4024308SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d4024308SAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d4024308SAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d4024308SAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d4024308SAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d4024308SAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d4024308SAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d4024308SAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d4024308SAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d4024308SAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d4024308SAaron LI  * SUCH DAMAGE.
26d4024308SAaron LI  *
27d4024308SAaron LI  * $FreeBSD: head/sbin/ifconfig/ifgroup.c 326276 2017-11-27 15:37:16Z pfg $
28d4024308SAaron LI  */
29d4024308SAaron LI 
30d4024308SAaron LI #include <sys/param.h>
31d4024308SAaron LI #include <sys/ioctl.h>
32d4024308SAaron LI #include <sys/socket.h>
33d4024308SAaron LI #include <net/if.h>
34d4024308SAaron LI 
35d4024308SAaron LI #include <ctype.h>
36d4024308SAaron LI #include <err.h>
37d4024308SAaron LI #include <errno.h>
38d4024308SAaron LI #include <stdio.h>
39d4024308SAaron LI #include <stdlib.h>
40d4024308SAaron LI #include <string.h>
41d4024308SAaron LI #include <unistd.h>
42d4024308SAaron LI 
43d4024308SAaron LI #include "ifconfig.h"
44d4024308SAaron LI 
45d4024308SAaron LI static void
setifgroup(const char * group_name,int d __unused,int s,const struct afswtch * rafp __unused)46d4024308SAaron LI setifgroup(const char *group_name, int d __unused, int s,
47d4024308SAaron LI 	   const struct afswtch *rafp __unused)
48d4024308SAaron LI {
49d4024308SAaron LI 	struct ifgroupreq ifgr;
50d4024308SAaron LI 
51d4024308SAaron LI 	memset(&ifgr, 0, sizeof(ifgr));
52*7203d4e3SAaron LI 	strlcpy(ifgr.ifgr_name, IfName, sizeof(ifgr.ifgr_name));
53d4024308SAaron LI 
54d4024308SAaron LI 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
55d4024308SAaron LI 		errx(1, "setifgroup: group names may not end in a digit");
56d4024308SAaron LI 
57d4024308SAaron LI 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
58d4024308SAaron LI 		errx(1, "setifgroup: group name too long");
590b22c3cfSAaron LI 	if (ioctl(s, SIOCAIFGROUP, &ifgr) == -1 && errno != EEXIST)
60d4024308SAaron LI 		err(1," SIOCAIFGROUP");
61d4024308SAaron LI }
62d4024308SAaron LI 
63d4024308SAaron LI static void
unsetifgroup(const char * group_name,int d __unused,int s,const struct afswtch * rafp __unused)64d4024308SAaron LI unsetifgroup(const char *group_name, int d __unused, int s,
65d4024308SAaron LI 	     const struct afswtch *rafp __unused)
66d4024308SAaron LI {
67d4024308SAaron LI 	struct ifgroupreq ifgr;
68d4024308SAaron LI 
69d4024308SAaron LI 	memset(&ifgr, 0, sizeof(ifgr));
70*7203d4e3SAaron LI 	strlcpy(ifgr.ifgr_name, IfName, sizeof(ifgr.ifgr_name));
71d4024308SAaron LI 
72d4024308SAaron LI 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
73d4024308SAaron LI 		errx(1, "unsetifgroup: group names may not end in a digit");
74d4024308SAaron LI 
75d4024308SAaron LI 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
76d4024308SAaron LI 		errx(1, "unsetifgroup: group name too long");
770b22c3cfSAaron LI 	if (ioctl(s, SIOCDIFGROUP, &ifgr) == -1 && errno != ENOENT)
78d4024308SAaron LI 		err(1, "SIOCDIFGROUP");
79d4024308SAaron LI }
80d4024308SAaron LI 
81d4024308SAaron LI static void
getifgroups(int s)82d4024308SAaron LI getifgroups(int s)
83d4024308SAaron LI {
84d4024308SAaron LI 	struct ifgroupreq	 ifgr;
85d4024308SAaron LI 	struct ifg_req		*ifg;
86d4024308SAaron LI 	size_t			 len, cnt;
87d4024308SAaron LI 
88d4024308SAaron LI 	memset(&ifgr, 0, sizeof(ifgr));
89*7203d4e3SAaron LI 	strlcpy(ifgr.ifgr_name, IfName, sizeof(ifgr.ifgr_name));
90d4024308SAaron LI 
910b22c3cfSAaron LI 	if (ioctl(s, SIOCGIFGROUP, &ifgr) == -1) {
92d4024308SAaron LI 		if (errno == EINVAL || errno == ENOTTY)
93d4024308SAaron LI 			return;
94d4024308SAaron LI 		else
95d4024308SAaron LI 			err(1, "SIOCGIFGROUP");
96d4024308SAaron LI 	}
97d4024308SAaron LI 
98d4024308SAaron LI 	len = ifgr.ifgr_len;
9993b0f758SAaron LI 	ifgr.ifgr_groups = calloc(1, len);
100d4024308SAaron LI 	if (ifgr.ifgr_groups == NULL)
101d4024308SAaron LI 		err(1, "getifgroups");
1020b22c3cfSAaron LI 	if (ioctl(s, SIOCGIFGROUP, &ifgr) == -1)
103d4024308SAaron LI 		err(1, "SIOCGIFGROUP");
104d4024308SAaron LI 
105d4024308SAaron LI 	cnt = 0;
106d4024308SAaron LI 	ifg = ifgr.ifgr_groups;
107d4024308SAaron LI 	for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
108d4024308SAaron LI 		len -= sizeof(struct ifg_req);
109d4024308SAaron LI 		if (strcmp(ifg->ifgrq_group, "all") != 0) {
110d4024308SAaron LI 			if (cnt == 0)
111d4024308SAaron LI 				printf("\tgroups:");
112d4024308SAaron LI 			cnt++;
113d4024308SAaron LI 			printf(" %s", ifg->ifgrq_group);
114d4024308SAaron LI 		}
115d4024308SAaron LI 	}
116d4024308SAaron LI 	if (cnt)
117d4024308SAaron LI 		printf("\n");
118d4024308SAaron LI 
119d4024308SAaron LI 	free(ifgr.ifgr_groups);
120d4024308SAaron LI }
121d4024308SAaron LI 
122d4024308SAaron LI static void
printgroup(const char * groupname)123d4024308SAaron LI printgroup(const char *groupname)
124d4024308SAaron LI {
125d4024308SAaron LI 	struct ifgroupreq	 ifgr;
126d4024308SAaron LI 	struct ifg_req		*ifg;
127d4024308SAaron LI 	size_t			 len, cnt = 0;
128d4024308SAaron LI 	int			 s;
129d4024308SAaron LI 
130d4024308SAaron LI 	s = socket(AF_LOCAL, SOCK_DGRAM, 0);
131d4024308SAaron LI 	if (s == -1)
132d4024308SAaron LI 		err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
133d4024308SAaron LI 
134d4024308SAaron LI 	memset(&ifgr, 0, sizeof(ifgr));
135d4024308SAaron LI 	strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
1360b22c3cfSAaron LI 	if (ioctl(s, SIOCGIFGMEMB, &ifgr) == -1) {
13793b0f758SAaron LI 		if (errno == EINVAL || errno == ENOTTY || errno == ENOENT)
138598a666bSAaron LI 			exit(exit_code);
139d4024308SAaron LI 		else
140d4024308SAaron LI 			err(1, "SIOCGIFGMEMB");
141d4024308SAaron LI 	}
142d4024308SAaron LI 
143d4024308SAaron LI 	len = ifgr.ifgr_len;
144d4024308SAaron LI 	if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
145d4024308SAaron LI 		err(1, "printgroup");
1460b22c3cfSAaron LI 	if (ioctl(s, SIOCGIFGMEMB, &ifgr) == -1)
147d4024308SAaron LI 		err(1, "SIOCGIFGMEMB");
148d4024308SAaron LI 
149d4024308SAaron LI 	for (ifg = ifgr.ifgr_groups;
150d4024308SAaron LI 	     ifg && len >= sizeof(struct ifg_req);
151d4024308SAaron LI 	     ifg++) {
152d4024308SAaron LI 		len -= sizeof(struct ifg_req);
153d4024308SAaron LI 		printf("%s\n", ifg->ifgrq_member);
154d4024308SAaron LI 		cnt++;
155d4024308SAaron LI 	}
156d4024308SAaron LI 	free(ifgr.ifgr_groups);
157d4024308SAaron LI 
158598a666bSAaron LI 	exit(exit_code);
159d4024308SAaron LI }
160d4024308SAaron LI 
161d4024308SAaron LI static struct cmd group_cmds[] = {
162d4024308SAaron LI 	DEF_CMD_ARG("group",	setifgroup),
163d4024308SAaron LI 	DEF_CMD_ARG("-group",	unsetifgroup),
164d4024308SAaron LI };
165d4024308SAaron LI static struct afswtch af_group = {
166d4024308SAaron LI 	.af_name		= "af_group",
167d4024308SAaron LI 	.af_af			= AF_UNSPEC,
168d4024308SAaron LI 	.af_other_status	= getifgroups,
169d4024308SAaron LI };
170d4024308SAaron LI static struct option group_gopt = { "g:", "[-g groupname]", printgroup, NULL };
171d4024308SAaron LI 
172ec7e0eebSAaron LI __constructor(132)
173ec7e0eebSAaron LI static void
group_ctor(void)174d4024308SAaron LI group_ctor(void)
175d4024308SAaron LI {
176d4024308SAaron LI 	size_t i;
177d4024308SAaron LI 
178d4024308SAaron LI 	for (i = 0; i < nitems(group_cmds);  i++)
179d4024308SAaron LI 		cmd_register(&group_cmds[i]);
18093b0f758SAaron LI 
181d4024308SAaron LI 	af_register(&af_group);
182d4024308SAaron LI 	opt_register(&group_gopt);
183d4024308SAaron LI }
184