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