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