ifconfig.c revision 1.244 1 1.244 roy /* $NetBSD: ifconfig.c,v 1.244 2020/09/23 02:09:18 roy Exp $ */
2 1.24 thorpej
3 1.41 thorpej /*-
4 1.64 thorpej * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 1.24 thorpej * All rights reserved.
6 1.24 thorpej *
7 1.41 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.41 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.41 thorpej * NASA Ames Research Center.
10 1.41 thorpej *
11 1.24 thorpej * Redistribution and use in source and binary forms, with or without
12 1.24 thorpej * modification, are permitted provided that the following conditions
13 1.24 thorpej * are met:
14 1.24 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.24 thorpej * notice, this list of conditions and the following disclaimer.
16 1.24 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.24 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.24 thorpej * documentation and/or other materials provided with the distribution.
19 1.24 thorpej *
20 1.41 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.41 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.41 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.41 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.41 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.41 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.41 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.41 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.41 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.41 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.41 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.24 thorpej */
32 1.19 cgd
33 1.1 cgd /*
34 1.13 mycroft * Copyright (c) 1983, 1993
35 1.13 mycroft * The Regents of the University of California. All rights reserved.
36 1.1 cgd *
37 1.1 cgd * Redistribution and use in source and binary forms, with or without
38 1.1 cgd * modification, are permitted provided that the following conditions
39 1.1 cgd * are met:
40 1.1 cgd * 1. Redistributions of source code must retain the above copyright
41 1.1 cgd * notice, this list of conditions and the following disclaimer.
42 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 cgd * notice, this list of conditions and the following disclaimer in the
44 1.1 cgd * documentation and/or other materials provided with the distribution.
45 1.138 agc * 3. Neither the name of the University nor the names of its contributors
46 1.1 cgd * may be used to endorse or promote products derived from this software
47 1.1 cgd * without specific prior written permission.
48 1.1 cgd *
49 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1 cgd * SUCH DAMAGE.
60 1.1 cgd */
61 1.1 cgd
62 1.39 lukem #include <sys/cdefs.h>
63 1.1 cgd #ifndef lint
64 1.212 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
65 1.212 lukem The Regents of the University of California. All rights reserved.");
66 1.244 roy __RCSID("$NetBSD: ifconfig.c,v 1.244 2020/09/23 02:09:18 roy Exp $");
67 1.1 cgd #endif /* not lint */
68 1.1 cgd
69 1.1 cgd #include <sys/param.h>
70 1.208 dyoung #include <sys/queue.h>
71 1.1 cgd #include <sys/socket.h>
72 1.1 cgd #include <sys/ioctl.h>
73 1.1 cgd
74 1.1 cgd #include <net/if.h>
75 1.30 thorpej #include <net/if_dl.h>
76 1.24 thorpej #include <net/if_media.h>
77 1.62 chopps #include <net/if_ether.h>
78 1.166 thorpej #include <netinet/in.h> /* XXX */
79 1.166 thorpej #include <netinet/in_var.h> /* XXX */
80 1.227 mbalmer
81 1.13 mycroft #include <netdb.h>
82 1.1 cgd
83 1.1 cgd #include <sys/protosw.h>
84 1.1 cgd
85 1.187 dyoung #include <assert.h>
86 1.13 mycroft #include <ctype.h>
87 1.13 mycroft #include <err.h>
88 1.13 mycroft #include <errno.h>
89 1.185 dyoung #include <stdbool.h>
90 1.50 chopps #include <stddef.h>
91 1.1 cgd #include <stdio.h>
92 1.1 cgd #include <stdlib.h>
93 1.1 cgd #include <string.h>
94 1.13 mycroft #include <unistd.h>
95 1.78 itojun #include <ifaddrs.h>
96 1.130 christos #include <util.h>
97 1.1 cgd
98 1.156 thorpej #include "extern.h"
99 1.161 thorpej
100 1.208 dyoung #include "media.h"
101 1.187 dyoung #include "parse.h"
102 1.187 dyoung #include "env.h"
103 1.224 pooka #include "prog_ops.h"
104 1.153 yamt
105 1.232 roy #define WAIT_DAD 10000000 /* nanoseconds between each poll, 10ms */
106 1.232 roy
107 1.236 roy static bool bflag, dflag, hflag, sflag, uflag, Wflag, wflag;
108 1.220 dyoung bool lflag, Nflag, vflag, zflag;
109 1.236 roy static long wflag_secs, Wflag_secs;
110 1.1 cgd
111 1.236 roy static char gflags[10 + 26 * 2 + 1] = "AabCdhlNsuvW:w:z";
112 1.208 dyoung bool gflagset[10 + 26 * 2];
113 1.187 dyoung
114 1.208 dyoung static int carrier(prop_dictionary_t);
115 1.208 dyoung static int clone_command(prop_dictionary_t, prop_dictionary_t);
116 1.208 dyoung static void do_setifpreference(prop_dictionary_t);
117 1.208 dyoung static int flag_index(int);
118 1.208 dyoung static void init_afs(void);
119 1.208 dyoung static int list_cloners(prop_dictionary_t, prop_dictionary_t);
120 1.208 dyoung static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
121 1.232 roy static int wait_dad_exec(prop_dictionary_t, prop_dictionary_t);
122 1.187 dyoung static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
123 1.208 dyoung static int notrailers(prop_dictionary_t, prop_dictionary_t);
124 1.208 dyoung static void printall(const char *, prop_dictionary_t);
125 1.208 dyoung static int setifaddr(prop_dictionary_t, prop_dictionary_t);
126 1.208 dyoung static int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
127 1.208 dyoung static int setifcaps(prop_dictionary_t, prop_dictionary_t);
128 1.208 dyoung static int setifdstormask(prop_dictionary_t, prop_dictionary_t);
129 1.208 dyoung static int setifflags(prop_dictionary_t, prop_dictionary_t);
130 1.208 dyoung static int setifmetric(prop_dictionary_t, prop_dictionary_t);
131 1.208 dyoung static int setifmtu(prop_dictionary_t, prop_dictionary_t);
132 1.208 dyoung static int setifnetmask(prop_dictionary_t, prop_dictionary_t);
133 1.208 dyoung static int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
134 1.223 pooka static int setlinkstr(prop_dictionary_t, prop_dictionary_t);
135 1.223 pooka static int unsetlinkstr(prop_dictionary_t, prop_dictionary_t);
136 1.239 ozaki static int setifdescr(prop_dictionary_t, prop_dictionary_t);
137 1.239 ozaki static int unsetifdescr(prop_dictionary_t, prop_dictionary_t);
138 1.243 roy static void status(const struct sockaddr_dl *, prop_dictionary_t,
139 1.189 dyoung prop_dictionary_t);
140 1.226 joerg __dead static void usage(void);
141 1.187 dyoung
142 1.198 dyoung static const struct kwinst ifflagskw[] = {
143 1.215 plunky IFKW("arp", -IFF_NOARP)
144 1.187 dyoung , IFKW("debug", IFF_DEBUG)
145 1.187 dyoung , IFKW("link0", IFF_LINK0)
146 1.187 dyoung , IFKW("link1", IFF_LINK1)
147 1.187 dyoung , IFKW("link2", IFF_LINK2)
148 1.205 dyoung , {.k_word = "down", .k_type = KW_T_INT, .k_int = -IFF_UP}
149 1.205 dyoung , {.k_word = "up", .k_type = KW_T_INT, .k_int = IFF_UP}
150 1.187 dyoung };
151 1.187 dyoung
152 1.198 dyoung static const struct kwinst ifcapskw[] = {
153 1.187 dyoung IFKW("ip4csum-tx", IFCAP_CSUM_IPv4_Tx)
154 1.187 dyoung , IFKW("ip4csum-rx", IFCAP_CSUM_IPv4_Rx)
155 1.187 dyoung , IFKW("tcp4csum-tx", IFCAP_CSUM_TCPv4_Tx)
156 1.187 dyoung , IFKW("tcp4csum-rx", IFCAP_CSUM_TCPv4_Rx)
157 1.187 dyoung , IFKW("udp4csum-tx", IFCAP_CSUM_UDPv4_Tx)
158 1.187 dyoung , IFKW("udp4csum-rx", IFCAP_CSUM_UDPv4_Rx)
159 1.187 dyoung , IFKW("tcp6csum-tx", IFCAP_CSUM_TCPv6_Tx)
160 1.187 dyoung , IFKW("tcp6csum-rx", IFCAP_CSUM_TCPv6_Rx)
161 1.187 dyoung , IFKW("udp6csum-tx", IFCAP_CSUM_UDPv6_Tx)
162 1.187 dyoung , IFKW("udp6csum-rx", IFCAP_CSUM_UDPv6_Rx)
163 1.187 dyoung , IFKW("ip4csum", IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
164 1.187 dyoung , IFKW("tcp4csum", IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
165 1.187 dyoung , IFKW("udp4csum", IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
166 1.187 dyoung , IFKW("tcp6csum", IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
167 1.187 dyoung , IFKW("udp6csum", IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
168 1.187 dyoung , IFKW("tso4", IFCAP_TSOv4)
169 1.187 dyoung , IFKW("tso6", IFCAP_TSOv6)
170 1.187 dyoung };
171 1.45 thorpej
172 1.187 dyoung extern struct pbranch command_root;
173 1.187 dyoung extern struct pbranch opt_command;
174 1.187 dyoung extern struct pbranch opt_family, opt_silent_family;
175 1.200 dyoung extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
176 1.223 pooka extern struct pstr parse_linkstr;
177 1.187 dyoung
178 1.187 dyoung struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
179 1.187 dyoung setifmetric, "metric", &command_root.pb_parser);
180 1.187 dyoung
181 1.187 dyoung struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
182 1.187 dyoung setifmtu, "mtu", &command_root.pb_parser);
183 1.187 dyoung
184 1.187 dyoung struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
185 1.187 dyoung "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
186 1.187 dyoung
187 1.187 dyoung struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
188 1.187 dyoung "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
189 1.187 dyoung &command_root.pb_parser);
190 1.187 dyoung
191 1.187 dyoung struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
192 1.187 dyoung setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
193 1.187 dyoung
194 1.187 dyoung struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
195 1.187 dyoung "broadcast address",
196 1.187 dyoung setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
197 1.187 dyoung
198 1.239 ozaki struct pstr parse_descr = PSTR_INITIALIZER1(&parse_descr, "descr",
199 1.239 ozaki setifdescr, "descr", false, &command_root.pb_parser);
200 1.239 ozaki
201 1.198 dyoung static const struct kwinst misckw[] = {
202 1.208 dyoung {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
203 1.187 dyoung .k_type = KW_T_BOOL, .k_neg = true,
204 1.187 dyoung .k_bool = true, .k_negbool = false,
205 1.208 dyoung .k_nextparser = &command_root.pb_parser}
206 1.187 dyoung , {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
207 1.187 dyoung , {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
208 1.208 dyoung .k_type = KW_T_BOOL, .k_bool = false,
209 1.187 dyoung .k_nextparser = &command_root.pb_parser}
210 1.187 dyoung , {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
211 1.187 dyoung , {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
212 1.187 dyoung , {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
213 1.187 dyoung , {.k_word = "preference", .k_act = "address",
214 1.187 dyoung .k_nextparser = &parse_preference.pi_parser}
215 1.187 dyoung , {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
216 1.187 dyoung , {.k_word = "trailers", .k_neg = true,
217 1.187 dyoung .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
218 1.223 pooka , {.k_word = "linkstr", .k_nextparser = &parse_linkstr.ps_parser }
219 1.223 pooka , {.k_word = "-linkstr", .k_exec = unsetlinkstr,
220 1.223 pooka .k_nextparser = &command_root.pb_parser }
221 1.239 ozaki , {.k_word = "descr", .k_nextparser = &parse_descr.ps_parser}
222 1.239 ozaki , {.k_word = "description", .k_nextparser = &parse_descr.ps_parser}
223 1.239 ozaki , {.k_word = "-descr", .k_exec = unsetifdescr,
224 1.239 ozaki .k_nextparser = &command_root.pb_parser}
225 1.239 ozaki , {.k_word = "-description", .k_exec = unsetifdescr,
226 1.239 ozaki .k_nextparser = &command_root.pb_parser}
227 1.1 cgd };
228 1.1 cgd
229 1.187 dyoung /* key: clonecmd */
230 1.198 dyoung static const struct kwinst clonekw[] = {
231 1.205 dyoung {.k_word = "create", .k_type = KW_T_INT, .k_int = SIOCIFCREATE,
232 1.187 dyoung .k_nextparser = &opt_silent_family.pb_parser},
233 1.205 dyoung {.k_word = "destroy", .k_type = KW_T_INT, .k_int = SIOCIFDESTROY}
234 1.187 dyoung };
235 1.24 thorpej
236 1.208 dyoung static struct kwinst familykw[24];
237 1.1 cgd
238 1.187 dyoung struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
239 1.187 dyoung list_cloners, "none");
240 1.187 dyoung
241 1.232 roy struct pterm wait_dad = PTERM_INITIALIZER(&wait_dad, "wait DAD", wait_dad_exec,
242 1.232 roy "none");
243 1.232 roy
244 1.187 dyoung struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
245 1.187 dyoung "none");
246 1.187 dyoung
247 1.187 dyoung struct pkw family_only =
248 1.187 dyoung PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
249 1.187 dyoung __arraycount(familykw), &no_cmds.pt_parser);
250 1.187 dyoung
251 1.187 dyoung struct paddr address = PADDR_INITIALIZER(&address,
252 1.187 dyoung "local address (address 1)",
253 1.187 dyoung setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
254 1.187 dyoung
255 1.187 dyoung struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
256 1.187 dyoung "destination/netmask (address 2)",
257 1.187 dyoung setifdstormask, "dstormask", NULL, "address", "dstormask",
258 1.187 dyoung &command_root.pb_parser);
259 1.187 dyoung
260 1.187 dyoung struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
261 1.187 dyoung "broadcast address (address 3)",
262 1.187 dyoung setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
263 1.187 dyoung &command_root.pb_parser);
264 1.187 dyoung
265 1.223 pooka struct pstr parse_linkstr = PSTR_INITIALIZER(&parse_linkstr, "linkstr",
266 1.223 pooka setlinkstr, "linkstr", &command_root.pb_parser);
267 1.223 pooka
268 1.208 dyoung static SIMPLEQ_HEAD(, afswtch) aflist = SIMPLEQ_HEAD_INITIALIZER(aflist);
269 1.208 dyoung
270 1.209 dyoung static SIMPLEQ_HEAD(, usage_func) usage_funcs =
271 1.209 dyoung SIMPLEQ_HEAD_INITIALIZER(usage_funcs);
272 1.208 dyoung static SIMPLEQ_HEAD(, status_func) status_funcs =
273 1.208 dyoung SIMPLEQ_HEAD_INITIALIZER(status_funcs);
274 1.208 dyoung static SIMPLEQ_HEAD(, statistics_func) statistics_funcs =
275 1.208 dyoung SIMPLEQ_HEAD_INITIALIZER(statistics_funcs);
276 1.208 dyoung static SIMPLEQ_HEAD(, cmdloop_branch) cmdloop_branches =
277 1.208 dyoung SIMPLEQ_HEAD_INITIALIZER(cmdloop_branches);
278 1.208 dyoung
279 1.187 dyoung struct branch opt_clone_brs[] = {
280 1.187 dyoung {.b_nextparser = &cloning.pk_parser}
281 1.187 dyoung , {.b_nextparser = &opt_family.pb_parser}
282 1.187 dyoung }, opt_silent_family_brs[] = {
283 1.187 dyoung {.b_nextparser = &silent_family.pk_parser}
284 1.187 dyoung , {.b_nextparser = &command_root.pb_parser}
285 1.187 dyoung }, opt_family_brs[] = {
286 1.187 dyoung {.b_nextparser = &family.pk_parser}
287 1.187 dyoung , {.b_nextparser = &opt_command.pb_parser}
288 1.187 dyoung }, command_root_brs[] = {
289 1.208 dyoung {.b_nextparser = &ifflags.pk_parser}
290 1.187 dyoung , {.b_nextparser = &ifcaps.pk_parser}
291 1.208 dyoung , {.b_nextparser = &kwmedia.pk_parser}
292 1.187 dyoung , {.b_nextparser = &misc.pk_parser}
293 1.187 dyoung , {.b_nextparser = &address.pa_parser}
294 1.187 dyoung , {.b_nextparser = &dstormask.pa_parser}
295 1.187 dyoung , {.b_nextparser = &broadcast.pa_parser}
296 1.187 dyoung , {.b_nextparser = NULL}
297 1.187 dyoung }, opt_command_brs[] = {
298 1.187 dyoung {.b_nextparser = &no_cmds.pt_parser}
299 1.187 dyoung , {.b_nextparser = &command_root.pb_parser}
300 1.187 dyoung };
301 1.187 dyoung
302 1.187 dyoung struct branch opt_family_only_brs[] = {
303 1.187 dyoung {.b_nextparser = &no_cmds.pt_parser}
304 1.187 dyoung , {.b_nextparser = &family_only.pk_parser}
305 1.187 dyoung };
306 1.187 dyoung struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
307 1.187 dyoung "opt-family-only", opt_family_only_brs,
308 1.187 dyoung __arraycount(opt_family_only_brs), true);
309 1.187 dyoung struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
310 1.187 dyoung "optional command",
311 1.187 dyoung opt_command_brs, __arraycount(opt_command_brs), true);
312 1.187 dyoung
313 1.187 dyoung struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
314 1.187 dyoung "command-root", command_root_brs, __arraycount(command_root_brs), true);
315 1.187 dyoung
316 1.187 dyoung struct piface iface_opt_family_only =
317 1.187 dyoung PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
318 1.187 dyoung NULL, "if", &opt_family_only.pb_parser);
319 1.187 dyoung
320 1.187 dyoung struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
321 1.187 dyoung familykw, __arraycount(familykw), &opt_command.pb_parser);
322 1.187 dyoung
323 1.187 dyoung struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
324 1.187 dyoung NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
325 1.187 dyoung
326 1.208 dyoung struct pkw *family_users[] = {&family_only, &family, &silent_family};
327 1.208 dyoung
328 1.187 dyoung struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
329 1.187 dyoung "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
330 1.187 dyoung
331 1.187 dyoung struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
332 1.187 dyoung "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
333 1.187 dyoung
334 1.187 dyoung struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
335 1.187 dyoung "clonecmd", clonekw, __arraycount(clonekw), NULL);
336 1.187 dyoung
337 1.187 dyoung struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
338 1.187 dyoung misckw, __arraycount(misckw), NULL);
339 1.187 dyoung
340 1.187 dyoung struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
341 1.187 dyoung "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
342 1.187 dyoung
343 1.187 dyoung struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
344 1.187 dyoung "optional silent family", opt_silent_family_brs,
345 1.187 dyoung __arraycount(opt_silent_family_brs), true);
346 1.187 dyoung
347 1.187 dyoung struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
348 1.187 dyoung "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
349 1.187 dyoung
350 1.187 dyoung struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
351 1.187 dyoung "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
352 1.187 dyoung
353 1.187 dyoung struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
354 1.187 dyoung media_status_exec, "if", NULL);
355 1.187 dyoung
356 1.209 dyoung static bool
357 1.209 dyoung flag_is_registered(const char *flags, int flag)
358 1.209 dyoung {
359 1.209 dyoung return flags != NULL && strchr(flags, flag) != NULL;
360 1.209 dyoung }
361 1.209 dyoung
362 1.208 dyoung static int
363 1.209 dyoung check_flag(const char *flags, int flag)
364 1.208 dyoung {
365 1.209 dyoung if (flag_is_registered(flags, flag)) {
366 1.208 dyoung errno = EEXIST;
367 1.208 dyoung return -1;
368 1.208 dyoung }
369 1.208 dyoung
370 1.208 dyoung if (flag >= '0' && flag <= '9')
371 1.208 dyoung return 0;
372 1.208 dyoung if (flag >= 'a' && flag <= 'z')
373 1.208 dyoung return 0;
374 1.208 dyoung if (flag >= 'A' && flag <= 'Z')
375 1.208 dyoung return 0;
376 1.208 dyoung
377 1.208 dyoung errno = EINVAL;
378 1.208 dyoung return -1;
379 1.208 dyoung }
380 1.208 dyoung
381 1.208 dyoung void
382 1.208 dyoung cmdloop_branch_init(cmdloop_branch_t *b, struct parser *p)
383 1.208 dyoung {
384 1.208 dyoung b->b_parser = p;
385 1.208 dyoung }
386 1.208 dyoung
387 1.208 dyoung void
388 1.208 dyoung statistics_func_init(statistics_func_t *f, statistics_cb_t func)
389 1.208 dyoung {
390 1.208 dyoung f->f_func = func;
391 1.208 dyoung }
392 1.208 dyoung
393 1.208 dyoung void
394 1.208 dyoung status_func_init(status_func_t *f, status_cb_t func)
395 1.208 dyoung {
396 1.208 dyoung f->f_func = func;
397 1.208 dyoung }
398 1.208 dyoung
399 1.209 dyoung void
400 1.209 dyoung usage_func_init(usage_func_t *f, usage_cb_t func)
401 1.209 dyoung {
402 1.209 dyoung f->f_func = func;
403 1.209 dyoung }
404 1.209 dyoung
405 1.208 dyoung int
406 1.208 dyoung register_cmdloop_branch(cmdloop_branch_t *b)
407 1.208 dyoung {
408 1.208 dyoung SIMPLEQ_INSERT_TAIL(&cmdloop_branches, b, b_next);
409 1.208 dyoung return 0;
410 1.208 dyoung }
411 1.208 dyoung
412 1.208 dyoung int
413 1.208 dyoung register_statistics(statistics_func_t *f)
414 1.208 dyoung {
415 1.208 dyoung SIMPLEQ_INSERT_TAIL(&statistics_funcs, f, f_next);
416 1.208 dyoung return 0;
417 1.208 dyoung }
418 1.208 dyoung
419 1.208 dyoung int
420 1.208 dyoung register_status(status_func_t *f)
421 1.208 dyoung {
422 1.208 dyoung SIMPLEQ_INSERT_TAIL(&status_funcs, f, f_next);
423 1.208 dyoung return 0;
424 1.208 dyoung }
425 1.208 dyoung
426 1.208 dyoung int
427 1.209 dyoung register_usage(usage_func_t *f)
428 1.209 dyoung {
429 1.209 dyoung SIMPLEQ_INSERT_TAIL(&usage_funcs, f, f_next);
430 1.209 dyoung return 0;
431 1.209 dyoung }
432 1.209 dyoung
433 1.209 dyoung int
434 1.208 dyoung register_family(struct afswtch *af)
435 1.208 dyoung {
436 1.208 dyoung SIMPLEQ_INSERT_TAIL(&aflist, af, af_next);
437 1.208 dyoung return 0;
438 1.208 dyoung }
439 1.227 mbalmer
440 1.208 dyoung int
441 1.208 dyoung register_flag(int flag)
442 1.208 dyoung {
443 1.208 dyoung if (check_flag(gflags, flag) == -1)
444 1.208 dyoung return -1;
445 1.208 dyoung
446 1.208 dyoung if (strlen(gflags) + 1 >= sizeof(gflags)) {
447 1.208 dyoung errno = ENOMEM;
448 1.208 dyoung return -1;
449 1.208 dyoung }
450 1.208 dyoung
451 1.208 dyoung gflags[strlen(gflags)] = flag;
452 1.208 dyoung
453 1.208 dyoung return 0;
454 1.208 dyoung }
455 1.227 mbalmer
456 1.208 dyoung static int
457 1.208 dyoung flag_index(int flag)
458 1.208 dyoung {
459 1.208 dyoung if (flag >= '0' && flag <= '9')
460 1.208 dyoung return flag - '0';
461 1.208 dyoung if (flag >= 'a' && flag <= 'z')
462 1.208 dyoung return 10 + flag - 'a';
463 1.208 dyoung if (flag >= 'A' && flag <= 'Z')
464 1.208 dyoung return 10 + 26 + flag - 'a';
465 1.208 dyoung
466 1.208 dyoung errno = EINVAL;
467 1.208 dyoung return -1;
468 1.208 dyoung }
469 1.208 dyoung
470 1.208 dyoung static bool
471 1.208 dyoung set_flag(int flag)
472 1.208 dyoung {
473 1.208 dyoung int idx;
474 1.208 dyoung
475 1.208 dyoung if ((idx = flag_index(flag)) == -1)
476 1.208 dyoung return false;
477 1.208 dyoung
478 1.208 dyoung return gflagset[idx] = true;
479 1.208 dyoung }
480 1.208 dyoung
481 1.208 dyoung bool
482 1.208 dyoung get_flag(int flag)
483 1.208 dyoung {
484 1.208 dyoung int idx;
485 1.208 dyoung
486 1.208 dyoung if ((idx = flag_index(flag)) == -1)
487 1.208 dyoung return false;
488 1.227 mbalmer
489 1.208 dyoung return gflagset[idx];
490 1.208 dyoung }
491 1.208 dyoung
492 1.187 dyoung static struct parser *
493 1.187 dyoung init_parser(void)
494 1.187 dyoung {
495 1.208 dyoung cmdloop_branch_t *b;
496 1.208 dyoung
497 1.187 dyoung if (parser_init(&iface_opt_family_only.pif_parser) == -1)
498 1.187 dyoung err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
499 1.187 dyoung if (parser_init(&iface_only.pif_parser) == -1)
500 1.187 dyoung err(EXIT_FAILURE, "parser_init(iface_only)");
501 1.187 dyoung if (parser_init(&iface_start.pif_parser) == -1)
502 1.187 dyoung err(EXIT_FAILURE, "parser_init(iface_start)");
503 1.227 mbalmer
504 1.208 dyoung SIMPLEQ_FOREACH(b, &cmdloop_branches, b_next)
505 1.208 dyoung pbranch_addbranch(&command_root, b->b_parser);
506 1.187 dyoung
507 1.187 dyoung return &iface_start.pif_parser;
508 1.187 dyoung }
509 1.187 dyoung
510 1.187 dyoung static int
511 1.211 dyoung no_cmds_exec(prop_dictionary_t env, prop_dictionary_t oenv)
512 1.187 dyoung {
513 1.187 dyoung const char *ifname;
514 1.187 dyoung unsigned short ignore;
515 1.187 dyoung
516 1.187 dyoung /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
517 1.187 dyoung if ((ifname = getifname(env)) == NULL)
518 1.187 dyoung ;
519 1.211 dyoung else if (getifflags(env, oenv, &ignore) == -1)
520 1.187 dyoung err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
521 1.187 dyoung
522 1.187 dyoung printall(ifname, env);
523 1.187 dyoung exit(EXIT_SUCCESS);
524 1.187 dyoung }
525 1.187 dyoung
526 1.187 dyoung static int
527 1.232 roy wait_dad_exec(prop_dictionary_t env, prop_dictionary_t oenv)
528 1.232 roy {
529 1.232 roy bool waiting;
530 1.232 roy struct ifaddrs *ifaddrs, *ifa;
531 1.232 roy const struct timespec ts = { .tv_sec = 0, .tv_nsec = WAIT_DAD };
532 1.236 roy struct timespec now, end_det, end;
533 1.234 roy const struct afswtch *afp;
534 1.232 roy
535 1.232 roy if (wflag_secs) {
536 1.236 roy const struct timespec tent =
537 1.236 roy { .tv_sec = wflag_secs, .tv_nsec = 0};
538 1.236 roy const struct timespec det =
539 1.236 roy { .tv_sec = Wflag_secs, .tv_nsec = 0};
540 1.236 roy
541 1.232 roy if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
542 1.232 roy err(EXIT_FAILURE, "clock_gettime");
543 1.236 roy timespecadd(&now, &tent, &end);
544 1.236 roy if (Wflag_secs)
545 1.236 roy timespecadd(&now, &det, &end_det);
546 1.236 roy else
547 1.236 roy timespecclear(&end_det);
548 1.236 roy } else {
549 1.236 roy timespecclear(&end_det);
550 1.236 roy timespecclear(&end);
551 1.232 roy }
552 1.232 roy
553 1.232 roy if (getifaddrs(&ifaddrs) == -1)
554 1.232 roy err(EXIT_FAILURE, "getifaddrs");
555 1.232 roy
556 1.232 roy for (;;) {
557 1.232 roy waiting = false;
558 1.232 roy for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
559 1.232 roy if (ifa->ifa_addr == NULL)
560 1.232 roy continue;
561 1.234 roy afp = lookup_af_bynum(ifa->ifa_addr->sa_family);
562 1.236 roy if (afp &&
563 1.236 roy ((afp->af_addr_tentative_or_detached &&
564 1.236 roy ifa->ifa_flags & IFF_UP &&
565 1.236 roy timespecisset(&end_det) &&
566 1.236 roy timespeccmp(&now, &end_det, <) &&
567 1.236 roy afp->af_addr_tentative_or_detached(ifa)) ||
568 1.236 roy (afp->af_addr_tentative &&
569 1.236 roy afp->af_addr_tentative(ifa))))
570 1.234 roy {
571 1.234 roy waiting = true;
572 1.234 roy break;
573 1.232 roy }
574 1.232 roy }
575 1.232 roy if (!waiting)
576 1.232 roy break;
577 1.232 roy nanosleep(&ts, NULL);
578 1.232 roy if (wflag_secs) {
579 1.232 roy if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
580 1.232 roy err(EXIT_FAILURE, "clock_gettime");
581 1.232 roy if (timespeccmp(&now, &end, >))
582 1.232 roy errx(EXIT_FAILURE, "timed out");
583 1.232 roy }
584 1.232 roy }
585 1.232 roy
586 1.232 roy freeifaddrs(ifaddrs);
587 1.232 roy exit(EXIT_SUCCESS);
588 1.232 roy }
589 1.232 roy
590 1.232 roy static int
591 1.211 dyoung media_status_exec(prop_dictionary_t env, prop_dictionary_t oenv)
592 1.187 dyoung {
593 1.187 dyoung const char *ifname;
594 1.187 dyoung unsigned short ignore;
595 1.187 dyoung
596 1.187 dyoung /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
597 1.187 dyoung if ((ifname = getifname(env)) == NULL)
598 1.187 dyoung ;
599 1.211 dyoung else if (getifflags(env, oenv, &ignore) == -1)
600 1.187 dyoung err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
601 1.187 dyoung
602 1.187 dyoung exit(carrier(env));
603 1.187 dyoung }
604 1.1 cgd
605 1.192 dyoung static void
606 1.201 dyoung do_setifcaps(prop_dictionary_t env)
607 1.192 dyoung {
608 1.192 dyoung struct ifcapreq ifcr;
609 1.192 dyoung prop_data_t d;
610 1.192 dyoung
611 1.193 dyoung d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
612 1.193 dyoung if (d == NULL)
613 1.192 dyoung return;
614 1.192 dyoung
615 1.193 dyoung assert(sizeof(ifcr) == prop_data_size(d));
616 1.193 dyoung
617 1.242 thorpej memcpy(&ifcr, prop_data_value(d), sizeof(ifcr));
618 1.201 dyoung if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
619 1.192 dyoung err(EXIT_FAILURE, "SIOCSIFCAP");
620 1.192 dyoung }
621 1.192 dyoung
622 1.16 cgd int
623 1.187 dyoung main(int argc, char **argv)
624 1.1 cgd {
625 1.187 dyoung const struct afswtch *afp;
626 1.236 roy int af, s, e;
627 1.208 dyoung bool aflag = false, Cflag = false;
628 1.187 dyoung struct match match[32];
629 1.187 dyoung size_t nmatch;
630 1.187 dyoung struct parser *start;
631 1.187 dyoung int ch, narg = 0, rc;
632 1.211 dyoung prop_dictionary_t env, oenv;
633 1.187 dyoung const char *ifname;
634 1.187 dyoung
635 1.187 dyoung memset(match, 0, sizeof(match));
636 1.187 dyoung
637 1.208 dyoung init_afs();
638 1.208 dyoung
639 1.187 dyoung start = init_parser();
640 1.24 thorpej
641 1.24 thorpej /* Parse command-line options */
642 1.227 mbalmer Nflag = vflag = zflag = false;
643 1.227 mbalmer aflag = argc == 1 ? true : false;
644 1.227 mbalmer if (aflag)
645 1.227 mbalmer start = &opt_family_only.pb_parser;
646 1.227 mbalmer
647 1.208 dyoung while ((ch = getopt(argc, argv, gflags)) != -1) {
648 1.24 thorpej switch (ch) {
649 1.49 christos case 'A':
650 1.107 itojun warnx("-A is deprecated");
651 1.49 christos break;
652 1.49 christos
653 1.24 thorpej case 'a':
654 1.208 dyoung aflag = true;
655 1.24 thorpej break;
656 1.24 thorpej
657 1.54 sommerfe case 'b':
658 1.208 dyoung bflag = true;
659 1.54 sommerfe break;
660 1.227 mbalmer
661 1.87 thorpej case 'C':
662 1.208 dyoung Cflag = true;
663 1.87 thorpej break;
664 1.87 thorpej
665 1.34 lukem case 'd':
666 1.208 dyoung dflag = true;
667 1.34 lukem break;
668 1.169 rpaulo case 'h':
669 1.208 dyoung hflag = true;
670 1.169 rpaulo break;
671 1.31 thorpej case 'l':
672 1.208 dyoung lflag = true;
673 1.24 thorpej break;
674 1.220 dyoung case 'N':
675 1.220 dyoung Nflag = true;
676 1.220 dyoung break;
677 1.1 cgd
678 1.54 sommerfe case 's':
679 1.208 dyoung sflag = true;
680 1.54 sommerfe break;
681 1.54 sommerfe
682 1.34 lukem case 'u':
683 1.208 dyoung uflag = true;
684 1.34 lukem break;
685 1.34 lukem
686 1.124 matt case 'v':
687 1.208 dyoung vflag = true;
688 1.124 matt break;
689 1.124 matt
690 1.232 roy case 'w':
691 1.232 roy wflag = true;
692 1.236 roy wflag_secs = strtoi(optarg, NULL, 10, 0, INT32_MAX, &e);
693 1.236 roy if (e)
694 1.236 roy errx(EXIT_FAILURE, "%s: not a number", optarg);
695 1.236 roy break;
696 1.236 roy
697 1.236 roy case 'W':
698 1.236 roy Wflag = true;
699 1.236 roy Wflag_secs = strtoi(optarg, NULL, 10, 0, INT32_MAX, &e);
700 1.236 roy if (e)
701 1.232 roy errx(EXIT_FAILURE, "%s: not a number", optarg);
702 1.232 roy break;
703 1.232 roy
704 1.134 perry case 'z':
705 1.208 dyoung zflag = true;
706 1.134 perry break;
707 1.134 perry
708 1.24 thorpej default:
709 1.208 dyoung if (!set_flag(ch))
710 1.208 dyoung usage();
711 1.208 dyoung break;
712 1.24 thorpej }
713 1.187 dyoung switch (ch) {
714 1.187 dyoung case 'a':
715 1.187 dyoung start = &opt_family_only.pb_parser;
716 1.187 dyoung break;
717 1.187 dyoung
718 1.187 dyoung case 'L':
719 1.187 dyoung case 'm':
720 1.187 dyoung case 'z':
721 1.187 dyoung if (start != &opt_family_only.pb_parser)
722 1.187 dyoung start = &iface_opt_family_only.pif_parser;
723 1.187 dyoung break;
724 1.187 dyoung case 'C':
725 1.187 dyoung start = &cloneterm.pt_parser;
726 1.187 dyoung break;
727 1.187 dyoung case 'l':
728 1.187 dyoung start = &no_cmds.pt_parser;
729 1.187 dyoung break;
730 1.187 dyoung case 's':
731 1.187 dyoung if (start != &no_cmds.pt_parser &&
732 1.187 dyoung start != &opt_family_only.pb_parser)
733 1.187 dyoung start = &iface_only.pif_parser;
734 1.187 dyoung break;
735 1.232 roy case 'w':
736 1.232 roy start = &wait_dad.pt_parser;
737 1.232 roy break;
738 1.187 dyoung default:
739 1.187 dyoung break;
740 1.187 dyoung }
741 1.1 cgd }
742 1.24 thorpej argc -= optind;
743 1.24 thorpej argv += optind;
744 1.24 thorpej
745 1.31 thorpej /*
746 1.232 roy * -l means "list all interfaces", and is mutually exclusive with
747 1.31 thorpej * all other flags/commands.
748 1.31 thorpej *
749 1.87 thorpej * -C means "list all names of cloners", and it mutually exclusive
750 1.87 thorpej * with all other flags/commands.
751 1.87 thorpej *
752 1.31 thorpej * -a means "print status of all interfaces".
753 1.232 roy *
754 1.232 roy * -w means "spin until DAD completes for all addreseses", and is
755 1.232 roy * mutually exclusivewith all other flags/commands.
756 1.31 thorpej */
757 1.232 roy if ((lflag || Cflag || wflag) &&
758 1.232 roy (aflag || get_flag('m') || vflag || zflag))
759 1.54 sommerfe usage();
760 1.232 roy if ((lflag || Cflag || wflag) && get_flag('L'))
761 1.31 thorpej usage();
762 1.232 roy if ((lflag && Cflag) || (lflag & wflag) || (Cflag && wflag))
763 1.87 thorpej usage();
764 1.24 thorpej
765 1.187 dyoung nmatch = __arraycount(match);
766 1.187 dyoung
767 1.187 dyoung rc = parse(argc, argv, start, match, &nmatch, &narg);
768 1.187 dyoung if (rc != 0)
769 1.24 thorpej usage();
770 1.24 thorpej
771 1.224 pooka if (prog_init && prog_init() == -1)
772 1.224 pooka err(1, "rump client init");
773 1.224 pooka
774 1.211 dyoung if ((oenv = prop_dictionary_create()) == NULL)
775 1.187 dyoung err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
776 1.187 dyoung
777 1.211 dyoung if (matches_exec(match, oenv, nmatch) == -1)
778 1.187 dyoung err(EXIT_FAILURE, "exec_matches");
779 1.81 thorpej
780 1.187 dyoung argc -= narg;
781 1.187 dyoung argv += narg;
782 1.24 thorpej
783 1.187 dyoung env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
784 1.187 dyoung if (env == NULL)
785 1.211 dyoung env = oenv;
786 1.229 apb else {
787 1.211 dyoung env = prop_dictionary_augment(env, oenv);
788 1.229 apb if (env == NULL)
789 1.229 apb err(EXIT_FAILURE, "%s: prop_dictionary_augment",
790 1.229 apb __func__);
791 1.229 apb }
792 1.24 thorpej
793 1.187 dyoung /* Process any media commands that may have been issued. */
794 1.187 dyoung process_media_commands(env);
795 1.24 thorpej
796 1.208 dyoung if ((af = getaf(env)) == -1)
797 1.187 dyoung af = AF_INET;
798 1.54 sommerfe
799 1.187 dyoung if ((s = getsock(af)) == -1)
800 1.187 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
801 1.51 thorpej
802 1.187 dyoung if ((ifname = getifname(env)) == NULL)
803 1.187 dyoung err(EXIT_FAILURE, "%s: getifname", __func__);
804 1.24 thorpej
805 1.187 dyoung if ((afp = lookup_af_bynum(af)) == NULL)
806 1.187 dyoung errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
807 1.1 cgd
808 1.208 dyoung assert(afp->af_addr_commit != NULL);
809 1.211 dyoung (*afp->af_addr_commit)(env, oenv);
810 1.21 gwr
811 1.187 dyoung do_setifpreference(env);
812 1.201 dyoung do_setifcaps(env);
813 1.238 msaitoh do_setethercaps(env);
814 1.108 thorpej
815 1.208 dyoung exit(EXIT_SUCCESS);
816 1.208 dyoung }
817 1.24 thorpej
818 1.208 dyoung static void
819 1.208 dyoung init_afs(void)
820 1.208 dyoung {
821 1.214 lukem size_t i;
822 1.208 dyoung const struct afswtch *afp;
823 1.208 dyoung struct kwinst kw = {.k_type = KW_T_INT};
824 1.208 dyoung
825 1.208 dyoung SIMPLEQ_FOREACH(afp, &aflist, af_next) {
826 1.208 dyoung kw.k_word = afp->af_name;
827 1.208 dyoung kw.k_int = afp->af_af;
828 1.208 dyoung for (i = 0; i < __arraycount(familykw); i++) {
829 1.208 dyoung if (familykw[i].k_word == NULL) {
830 1.208 dyoung familykw[i] = kw;
831 1.208 dyoung break;
832 1.208 dyoung }
833 1.208 dyoung }
834 1.208 dyoung }
835 1.24 thorpej }
836 1.24 thorpej
837 1.160 thorpej const struct afswtch *
838 1.158 thorpej lookup_af_bynum(int afnum)
839 1.158 thorpej {
840 1.208 dyoung const struct afswtch *afp;
841 1.158 thorpej
842 1.208 dyoung SIMPLEQ_FOREACH(afp, &aflist, af_next) {
843 1.208 dyoung if (afp->af_af == afnum)
844 1.208 dyoung break;
845 1.208 dyoung }
846 1.208 dyoung return afp;
847 1.158 thorpej }
848 1.158 thorpej
849 1.13 mycroft void
850 1.187 dyoung printall(const char *ifname, prop_dictionary_t env0)
851 1.5 deraadt {
852 1.78 itojun struct ifaddrs *ifap, *ifa;
853 1.187 dyoung struct ifreq ifr;
854 1.243 roy const struct sockaddr_dl *sdl = NULL;
855 1.189 dyoung prop_dictionary_t env, oenv;
856 1.78 itojun int idx;
857 1.78 itojun char *p;
858 1.78 itojun
859 1.187 dyoung if (env0 == NULL)
860 1.187 dyoung env = prop_dictionary_create();
861 1.187 dyoung else
862 1.187 dyoung env = prop_dictionary_copy_mutable(env0);
863 1.187 dyoung
864 1.189 dyoung oenv = prop_dictionary_create();
865 1.189 dyoung
866 1.189 dyoung if (env == NULL || oenv == NULL)
867 1.187 dyoung errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
868 1.187 dyoung
869 1.78 itojun if (getifaddrs(&ifap) != 0)
870 1.120 atatat err(EXIT_FAILURE, "getifaddrs");
871 1.78 itojun p = NULL;
872 1.78 itojun idx = 0;
873 1.78 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
874 1.187 dyoung memset(&ifr, 0, sizeof(ifr));
875 1.187 dyoung estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
876 1.187 dyoung if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
877 1.187 dyoung memcpy(&ifr.ifr_addr, ifa->ifa_addr,
878 1.78 itojun ifa->ifa_addr->sa_len);
879 1.78 itojun }
880 1.78 itojun
881 1.187 dyoung if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
882 1.107 itojun continue;
883 1.78 itojun if (ifa->ifa_addr->sa_family == AF_LINK)
884 1.243 roy sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
885 1.78 itojun if (p && strcmp(p, ifa->ifa_name) == 0)
886 1.78 itojun continue;
887 1.242 thorpej if (!prop_dictionary_set_string(env, "if", ifa->ifa_name))
888 1.140 itojun continue;
889 1.78 itojun p = ifa->ifa_name;
890 1.78 itojun
891 1.135 lukem if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
892 1.78 itojun continue;
893 1.78 itojun if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
894 1.78 itojun continue;
895 1.78 itojun if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
896 1.78 itojun continue;
897 1.78 itojun
898 1.187 dyoung if (sflag && carrier(env))
899 1.78 itojun continue;
900 1.78 itojun idx++;
901 1.78 itojun /*
902 1.78 itojun * Are we just listing the interfaces?
903 1.78 itojun */
904 1.78 itojun if (lflag) {
905 1.78 itojun if (idx > 1)
906 1.147 dsl printf(" ");
907 1.187 dyoung fputs(ifa->ifa_name, stdout);
908 1.78 itojun continue;
909 1.78 itojun }
910 1.78 itojun
911 1.189 dyoung status(sdl, env, oenv);
912 1.119 bjh21 sdl = NULL;
913 1.78 itojun }
914 1.78 itojun if (lflag)
915 1.147 dsl printf("\n");
916 1.187 dyoung prop_object_release((prop_object_t)env);
917 1.189 dyoung prop_object_release((prop_object_t)oenv);
918 1.78 itojun freeifaddrs(ifap);
919 1.87 thorpej }
920 1.87 thorpej
921 1.208 dyoung static int
922 1.187 dyoung list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
923 1.87 thorpej {
924 1.87 thorpej struct if_clonereq ifcr;
925 1.87 thorpej char *cp, *buf;
926 1.187 dyoung int idx, s;
927 1.87 thorpej
928 1.87 thorpej memset(&ifcr, 0, sizeof(ifcr));
929 1.87 thorpej
930 1.187 dyoung s = getsock(AF_INET);
931 1.87 thorpej
932 1.224 pooka if (prog_ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
933 1.120 atatat err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
934 1.87 thorpej
935 1.87 thorpej buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
936 1.87 thorpej if (buf == NULL)
937 1.120 atatat err(EXIT_FAILURE, "unable to allocate cloner name buffer");
938 1.87 thorpej
939 1.87 thorpej ifcr.ifcr_count = ifcr.ifcr_total;
940 1.87 thorpej ifcr.ifcr_buffer = buf;
941 1.87 thorpej
942 1.224 pooka if (prog_ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
943 1.120 atatat err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
944 1.87 thorpej
945 1.87 thorpej /*
946 1.87 thorpej * In case some disappeared in the mean time, clamp it down.
947 1.87 thorpej */
948 1.87 thorpej if (ifcr.ifcr_count > ifcr.ifcr_total)
949 1.87 thorpej ifcr.ifcr_count = ifcr.ifcr_total;
950 1.87 thorpej
951 1.87 thorpej for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
952 1.87 thorpej if (idx > 0)
953 1.147 dsl printf(" ");
954 1.87 thorpej printf("%s", cp);
955 1.87 thorpej }
956 1.87 thorpej
957 1.147 dsl printf("\n");
958 1.87 thorpej free(buf);
959 1.187 dyoung exit(EXIT_SUCCESS);
960 1.5 deraadt }
961 1.5 deraadt
962 1.187 dyoung static int
963 1.211 dyoung clone_command(prop_dictionary_t env, prop_dictionary_t oenv)
964 1.81 thorpej {
965 1.188 dyoung int64_t cmd;
966 1.187 dyoung
967 1.188 dyoung if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
968 1.187 dyoung errno = ENOENT;
969 1.187 dyoung return -1;
970 1.187 dyoung }
971 1.187 dyoung
972 1.201 dyoung if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
973 1.187 dyoung warn("%s", __func__);
974 1.187 dyoung return -1;
975 1.187 dyoung }
976 1.187 dyoung return 0;
977 1.81 thorpej }
978 1.81 thorpej
979 1.81 thorpej /*ARGSUSED*/
980 1.208 dyoung static int
981 1.211 dyoung setifaddr(prop_dictionary_t env, prop_dictionary_t oenv)
982 1.81 thorpej {
983 1.187 dyoung const struct paddr_prefix *pfx0;
984 1.187 dyoung struct paddr_prefix *pfx;
985 1.187 dyoung prop_data_t d;
986 1.208 dyoung int af;
987 1.187 dyoung
988 1.187 dyoung if ((af = getaf(env)) == -1)
989 1.187 dyoung af = AF_INET;
990 1.187 dyoung
991 1.187 dyoung d = (prop_data_t)prop_dictionary_get(env, "address");
992 1.187 dyoung assert(d != NULL);
993 1.242 thorpej pfx0 = prop_data_value(d);
994 1.81 thorpej
995 1.194 dyoung if (pfx0->pfx_len >= 0) {
996 1.187 dyoung pfx = prefixlen_to_mask(af, pfx0->pfx_len);
997 1.187 dyoung if (pfx == NULL)
998 1.187 dyoung err(EXIT_FAILURE, "prefixlen_to_mask");
999 1.187 dyoung free(pfx);
1000 1.187 dyoung }
1001 1.187 dyoung
1002 1.187 dyoung return 0;
1003 1.1 cgd }
1004 1.1 cgd
1005 1.208 dyoung static int
1006 1.211 dyoung setifnetmask(prop_dictionary_t env, prop_dictionary_t oenv)
1007 1.1 cgd {
1008 1.187 dyoung prop_data_t d;
1009 1.187 dyoung
1010 1.187 dyoung d = (prop_data_t)prop_dictionary_get(env, "dstormask");
1011 1.187 dyoung assert(d != NULL);
1012 1.187 dyoung
1013 1.211 dyoung if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
1014 1.187 dyoung return -1;
1015 1.187 dyoung
1016 1.187 dyoung return 0;
1017 1.1 cgd }
1018 1.1 cgd
1019 1.208 dyoung static int
1020 1.211 dyoung setifbroadaddr(prop_dictionary_t env, prop_dictionary_t oenv)
1021 1.1 cgd {
1022 1.187 dyoung prop_data_t d;
1023 1.187 dyoung unsigned short flags;
1024 1.187 dyoung
1025 1.211 dyoung if (getifflags(env, oenv, &flags) == -1)
1026 1.187 dyoung err(EXIT_FAILURE, "%s: getifflags", __func__);
1027 1.187 dyoung
1028 1.187 dyoung if ((flags & IFF_BROADCAST) == 0)
1029 1.187 dyoung errx(EXIT_FAILURE, "not a broadcast interface");
1030 1.187 dyoung
1031 1.187 dyoung d = (prop_data_t)prop_dictionary_get(env, "broadcast");
1032 1.187 dyoung assert(d != NULL);
1033 1.187 dyoung
1034 1.211 dyoung if (!prop_dictionary_set(oenv, "broadcast", (prop_object_t)d))
1035 1.187 dyoung return -1;
1036 1.187 dyoung
1037 1.187 dyoung return 0;
1038 1.1 cgd }
1039 1.1 cgd
1040 1.1 cgd /*ARGSUSED*/
1041 1.208 dyoung static int
1042 1.211 dyoung notrailers(prop_dictionary_t env, prop_dictionary_t oenv)
1043 1.13 mycroft {
1044 1.34 lukem puts("Note: trailers are no longer sent, but always received");
1045 1.187 dyoung return 0;
1046 1.13 mycroft }
1047 1.13 mycroft
1048 1.13 mycroft /*ARGSUSED*/
1049 1.208 dyoung static int
1050 1.211 dyoung setifdstormask(prop_dictionary_t env, prop_dictionary_t oenv)
1051 1.1 cgd {
1052 1.187 dyoung const char *key;
1053 1.187 dyoung prop_data_t d;
1054 1.187 dyoung unsigned short flags;
1055 1.187 dyoung
1056 1.211 dyoung if (getifflags(env, oenv, &flags) == -1)
1057 1.187 dyoung err(EXIT_FAILURE, "%s: getifflags", __func__);
1058 1.187 dyoung
1059 1.187 dyoung d = (prop_data_t)prop_dictionary_get(env, "dstormask");
1060 1.187 dyoung assert(d != NULL);
1061 1.187 dyoung
1062 1.187 dyoung if ((flags & IFF_BROADCAST) == 0) {
1063 1.187 dyoung key = "dst";
1064 1.187 dyoung } else {
1065 1.187 dyoung key = "netmask";
1066 1.187 dyoung }
1067 1.187 dyoung
1068 1.211 dyoung if (!prop_dictionary_set(oenv, key, (prop_object_t)d))
1069 1.187 dyoung return -1;
1070 1.187 dyoung
1071 1.187 dyoung return 0;
1072 1.1 cgd }
1073 1.1 cgd
1074 1.208 dyoung static int
1075 1.211 dyoung setifflags(prop_dictionary_t env, prop_dictionary_t oenv)
1076 1.1 cgd {
1077 1.201 dyoung struct ifreq ifr;
1078 1.189 dyoung int64_t ifflag;
1079 1.189 dyoung bool rc;
1080 1.187 dyoung
1081 1.189 dyoung rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
1082 1.189 dyoung assert(rc);
1083 1.79 enami
1084 1.240 msaitoh if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
1085 1.187 dyoung return -1;
1086 1.187 dyoung
1087 1.187 dyoung if (ifflag < 0) {
1088 1.187 dyoung ifflag = -ifflag;
1089 1.201 dyoung ifr.ifr_flags &= ~ifflag;
1090 1.1 cgd } else
1091 1.201 dyoung ifr.ifr_flags |= ifflag;
1092 1.187 dyoung
1093 1.201 dyoung if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
1094 1.187 dyoung return -1;
1095 1.187 dyoung
1096 1.227 mbalmer return 0;
1097 1.187 dyoung }
1098 1.187 dyoung
1099 1.187 dyoung static int
1100 1.187 dyoung getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
1101 1.187 dyoung {
1102 1.187 dyoung bool rc;
1103 1.187 dyoung struct ifcapreq ifcr;
1104 1.187 dyoung const struct ifcapreq *tmpifcr;
1105 1.187 dyoung prop_data_t capdata;
1106 1.187 dyoung
1107 1.187 dyoung capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
1108 1.187 dyoung
1109 1.187 dyoung if (capdata != NULL) {
1110 1.242 thorpej tmpifcr = prop_data_value(capdata);
1111 1.187 dyoung *oifcr = *tmpifcr;
1112 1.187 dyoung return 0;
1113 1.187 dyoung }
1114 1.187 dyoung
1115 1.201 dyoung (void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
1116 1.187 dyoung *oifcr = ifcr;
1117 1.187 dyoung
1118 1.242 thorpej capdata = prop_data_create_copy(&ifcr, sizeof(ifcr));
1119 1.187 dyoung
1120 1.187 dyoung rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1121 1.187 dyoung
1122 1.187 dyoung prop_object_release((prop_object_t)capdata);
1123 1.187 dyoung
1124 1.190 dyoung return rc ? 0 : -1;
1125 1.1 cgd }
1126 1.1 cgd
1127 1.187 dyoung static int
1128 1.187 dyoung setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
1129 1.108 thorpej {
1130 1.189 dyoung int64_t ifcap;
1131 1.189 dyoung bool rc;
1132 1.187 dyoung prop_data_t capdata;
1133 1.187 dyoung struct ifcapreq ifcr;
1134 1.187 dyoung
1135 1.189 dyoung rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
1136 1.189 dyoung assert(rc);
1137 1.189 dyoung
1138 1.187 dyoung if (getifcaps(env, oenv, &ifcr) == -1)
1139 1.187 dyoung return -1;
1140 1.187 dyoung
1141 1.187 dyoung if (ifcap < 0) {
1142 1.187 dyoung ifcap = -ifcap;
1143 1.187 dyoung ifcr.ifcr_capenable &= ~ifcap;
1144 1.108 thorpej } else
1145 1.187 dyoung ifcr.ifcr_capenable |= ifcap;
1146 1.187 dyoung
1147 1.242 thorpej if ((capdata = prop_data_create_copy(&ifcr, sizeof(ifcr))) == NULL)
1148 1.187 dyoung return -1;
1149 1.108 thorpej
1150 1.191 dyoung rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1151 1.190 dyoung prop_object_release((prop_object_t)capdata);
1152 1.187 dyoung
1153 1.190 dyoung return rc ? 0 : -1;
1154 1.108 thorpej }
1155 1.108 thorpej
1156 1.187 dyoung static int
1157 1.211 dyoung setifmetric(prop_dictionary_t env, prop_dictionary_t oenv)
1158 1.1 cgd {
1159 1.187 dyoung struct ifreq ifr;
1160 1.189 dyoung bool rc;
1161 1.189 dyoung int64_t metric;
1162 1.187 dyoung
1163 1.189 dyoung rc = prop_dictionary_get_int64(env, "metric", &metric);
1164 1.189 dyoung assert(rc);
1165 1.125 itojun
1166 1.189 dyoung ifr.ifr_metric = metric;
1167 1.201 dyoung if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
1168 1.13 mycroft warn("SIOCSIFMETRIC");
1169 1.187 dyoung return 0;
1170 1.1 cgd }
1171 1.1 cgd
1172 1.187 dyoung static void
1173 1.187 dyoung do_setifpreference(prop_dictionary_t env)
1174 1.178 dyoung {
1175 1.178 dyoung struct if_addrprefreq ifap;
1176 1.187 dyoung prop_data_t d;
1177 1.187 dyoung const struct paddr_prefix *pfx;
1178 1.187 dyoung
1179 1.189 dyoung memset(&ifap, 0, sizeof(ifap));
1180 1.189 dyoung
1181 1.189 dyoung if (!prop_dictionary_get_int16(env, "preference",
1182 1.189 dyoung &ifap.ifap_preference))
1183 1.227 mbalmer return;
1184 1.187 dyoung
1185 1.187 dyoung d = (prop_data_t)prop_dictionary_get(env, "address");
1186 1.187 dyoung assert(d != NULL);
1187 1.189 dyoung
1188 1.242 thorpej pfx = prop_data_value(d);
1189 1.187 dyoung
1190 1.187 dyoung memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
1191 1.187 dyoung MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
1192 1.201 dyoung if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
1193 1.178 dyoung warn("SIOCSIFADDRPREF");
1194 1.178 dyoung }
1195 1.178 dyoung
1196 1.187 dyoung static int
1197 1.211 dyoung setifmtu(prop_dictionary_t env, prop_dictionary_t oenv)
1198 1.33 is {
1199 1.189 dyoung int64_t mtu;
1200 1.189 dyoung bool rc;
1201 1.187 dyoung struct ifreq ifr;
1202 1.125 itojun
1203 1.189 dyoung rc = prop_dictionary_get_int64(env, "mtu", &mtu);
1204 1.189 dyoung assert(rc);
1205 1.187 dyoung
1206 1.189 dyoung ifr.ifr_mtu = mtu;
1207 1.201 dyoung if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
1208 1.33 is warn("SIOCSIFMTU");
1209 1.33 is
1210 1.187 dyoung return 0;
1211 1.88 onoe }
1212 1.88 onoe
1213 1.208 dyoung static int
1214 1.187 dyoung carrier(prop_dictionary_t env)
1215 1.54 sommerfe {
1216 1.244 roy struct ifmediareq ifmr = { .ifm_status = 0 };
1217 1.54 sommerfe
1218 1.201 dyoung if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
1219 1.54 sommerfe /*
1220 1.54 sommerfe * Interface doesn't support SIOC{G,S}IFMEDIA;
1221 1.244 roy * check link state.
1222 1.54 sommerfe */
1223 1.244 roy struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
1224 1.244 roy
1225 1.244 roy if (direct_ioctl(env, SIOCGIFDATA, &ifdr) == -1)
1226 1.244 roy return EXIT_FAILURE;
1227 1.244 roy if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_UP)
1228 1.244 roy return EXIT_SUCCESS;
1229 1.244 roy else
1230 1.244 roy return EXIT_FAILURE;
1231 1.54 sommerfe }
1232 1.54 sommerfe if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1233 1.54 sommerfe /*
1234 1.54 sommerfe * Interface doesn't report media-valid status.
1235 1.54 sommerfe * assume ok.
1236 1.54 sommerfe */
1237 1.187 dyoung return EXIT_SUCCESS;
1238 1.54 sommerfe }
1239 1.54 sommerfe /* otherwise, return ok for active, not-ok if not active. */
1240 1.187 dyoung if (ifmr.ifm_status & IFM_ACTIVE)
1241 1.187 dyoung return EXIT_SUCCESS;
1242 1.187 dyoung else
1243 1.187 dyoung return EXIT_FAILURE;
1244 1.54 sommerfe }
1245 1.54 sommerfe
1246 1.208 dyoung static void
1247 1.208 dyoung print_plural(const char *prefix, uint64_t n, const char *unit)
1248 1.208 dyoung {
1249 1.208 dyoung printf("%s%" PRIu64 " %s%s", prefix, n, unit, (n == 1) ? "" : "s");
1250 1.208 dyoung }
1251 1.63 thorpej
1252 1.208 dyoung static void
1253 1.208 dyoung print_human_bytes(bool humanize, uint64_t n)
1254 1.208 dyoung {
1255 1.208 dyoung char buf[5];
1256 1.208 dyoung
1257 1.208 dyoung if (humanize) {
1258 1.208 dyoung (void)humanize_number(buf, sizeof(buf),
1259 1.208 dyoung (int64_t)n, "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
1260 1.208 dyoung printf(", %s byte%s", buf, (atof(buf) == 1.0) ? "" : "s");
1261 1.208 dyoung } else
1262 1.208 dyoung print_plural(", ", n, "byte");
1263 1.208 dyoung }
1264 1.63 thorpej
1265 1.1 cgd /*
1266 1.1 cgd * Print the status of the interface. If an address family was
1267 1.1 cgd * specified, show it and it only; otherwise, show them all.
1268 1.1 cgd */
1269 1.228 pgoyette
1270 1.228 pgoyette #define MAX_PRINT_LEN 58 /* XXX need a better way to determine this! */
1271 1.228 pgoyette
1272 1.16 cgd void
1273 1.243 roy status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
1274 1.189 dyoung prop_dictionary_t oenv)
1275 1.1 cgd {
1276 1.208 dyoung const struct if_data *ifi;
1277 1.208 dyoung status_func_t *status_f;
1278 1.208 dyoung statistics_func_t *statistics_f;
1279 1.124 matt struct ifdatareq ifdr;
1280 1.187 dyoung struct ifreq ifr;
1281 1.223 pooka struct ifdrv ifdrv;
1282 1.130 christos char fbuf[BUFSIZ];
1283 1.228 pgoyette char *bp;
1284 1.187 dyoung int af, s;
1285 1.187 dyoung const char *ifname;
1286 1.187 dyoung struct ifcapreq ifcr;
1287 1.187 dyoung unsigned short flags;
1288 1.187 dyoung const struct afswtch *afp;
1289 1.239 ozaki char ifdescr[IFDESCRSIZE];
1290 1.187 dyoung
1291 1.188 dyoung if ((af = getaf(env)) == -1) {
1292 1.188 dyoung afp = NULL;
1293 1.188 dyoung af = AF_UNSPEC;
1294 1.188 dyoung } else
1295 1.188 dyoung afp = lookup_af_bynum(af);
1296 1.188 dyoung
1297 1.201 dyoung /* get out early if the family is unsupported by the kernel */
1298 1.188 dyoung if ((s = getsock(af)) == -1)
1299 1.187 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
1300 1.187 dyoung
1301 1.189 dyoung if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
1302 1.187 dyoung err(EXIT_FAILURE, "%s: getifinfo", __func__);
1303 1.1 cgd
1304 1.235 ozaki (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1305 1.235 ozaki printf("%s: flags=%s", ifname, fbuf);
1306 1.187 dyoung
1307 1.187 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1308 1.224 pooka if (prog_ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
1309 1.187 dyoung warn("SIOCGIFMETRIC %s", ifr.ifr_name);
1310 1.187 dyoung else if (ifr.ifr_metric != 0)
1311 1.187 dyoung printf(" metric %d", ifr.ifr_metric);
1312 1.187 dyoung
1313 1.187 dyoung estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1314 1.224 pooka if (prog_ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
1315 1.187 dyoung printf(" mtu %d", ifr.ifr_mtu);
1316 1.147 dsl printf("\n");
1317 1.108 thorpej
1318 1.189 dyoung if (getifcaps(env, oenv, &ifcr) == -1)
1319 1.187 dyoung err(EXIT_FAILURE, "%s: getifcaps", __func__);
1320 1.187 dyoung
1321 1.187 dyoung if (ifcr.ifcr_capabilities != 0) {
1322 1.228 pgoyette (void)snprintb_m(fbuf, sizeof(fbuf), IFCAPBITS,
1323 1.228 pgoyette ifcr.ifcr_capabilities, MAX_PRINT_LEN);
1324 1.228 pgoyette bp = fbuf;
1325 1.228 pgoyette while (*bp != '\0') {
1326 1.241 ryo printf("\tcapabilities=%s\n", bp);
1327 1.228 pgoyette bp += strlen(bp) + 1;
1328 1.228 pgoyette }
1329 1.228 pgoyette (void)snprintb_m(fbuf, sizeof(fbuf), IFCAPBITS,
1330 1.228 pgoyette ifcr.ifcr_capenable, MAX_PRINT_LEN);
1331 1.228 pgoyette bp = fbuf;
1332 1.228 pgoyette while (*bp != '\0') {
1333 1.241 ryo printf("\tenabled=%s\n", bp);
1334 1.228 pgoyette bp += strlen(bp) + 1;
1335 1.228 pgoyette }
1336 1.108 thorpej }
1337 1.65 thorpej
1338 1.208 dyoung SIMPLEQ_FOREACH(status_f, &status_funcs, f_next)
1339 1.208 dyoung (*status_f->f_func)(env, oenv);
1340 1.65 thorpej
1341 1.239 ozaki estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1342 1.239 ozaki ifr.ifr_buf = &ifdescr;
1343 1.239 ozaki ifr.ifr_buflen = sizeof(ifdescr);
1344 1.239 ozaki if (prog_ioctl(s, SIOCGIFDESCR, &ifr) == 0)
1345 1.239 ozaki printf("\tdescription: \"%s\"\n", (char *)ifr.ifr_buf);
1346 1.239 ozaki
1347 1.218 dyoung print_link_addresses(env, true);
1348 1.24 thorpej
1349 1.223 pooka estrlcpy(ifdrv.ifd_name, ifname, sizeof(ifdrv.ifd_name));
1350 1.223 pooka ifdrv.ifd_cmd = IFLINKSTR_QUERYLEN;
1351 1.223 pooka ifdrv.ifd_len = 0;
1352 1.223 pooka ifdrv.ifd_data = NULL;
1353 1.223 pooka /* interface supports linkstr? */
1354 1.224 pooka if (prog_ioctl(s, SIOCGLINKSTR, &ifdrv) != -1) {
1355 1.223 pooka char *p;
1356 1.223 pooka
1357 1.223 pooka p = malloc(ifdrv.ifd_len);
1358 1.223 pooka if (p == NULL)
1359 1.223 pooka err(EXIT_FAILURE, "malloc linkstr buf failed");
1360 1.223 pooka ifdrv.ifd_data = p;
1361 1.223 pooka ifdrv.ifd_cmd = 0;
1362 1.224 pooka if (prog_ioctl(s, SIOCGLINKSTR, &ifdrv) == -1)
1363 1.223 pooka err(EXIT_FAILURE, "failed to query linkstr");
1364 1.223 pooka printf("\tlinkstr: %s\n", (char *)ifdrv.ifd_data);
1365 1.223 pooka free(p);
1366 1.223 pooka }
1367 1.223 pooka
1368 1.187 dyoung estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
1369 1.124 matt
1370 1.224 pooka if (prog_ioctl(s, zflag ? SIOCZIFDATA : SIOCGIFDATA, &ifdr) == -1)
1371 1.134 perry err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1372 1.169 rpaulo
1373 1.208 dyoung ifi = &ifdr.ifdr_data;
1374 1.208 dyoung
1375 1.243 roy media_status(sdl->sdl_type, ifi->ifi_link_state, env, oenv);
1376 1.243 roy
1377 1.243 roy if (!vflag && !zflag)
1378 1.243 roy goto proto_status;
1379 1.243 roy
1380 1.208 dyoung print_plural("\tinput: ", ifi->ifi_ipackets, "packet");
1381 1.208 dyoung print_human_bytes(hflag, ifi->ifi_ibytes);
1382 1.208 dyoung if (ifi->ifi_imcasts)
1383 1.208 dyoung print_plural(", ", ifi->ifi_imcasts, "multicast");
1384 1.208 dyoung if (ifi->ifi_ierrors)
1385 1.208 dyoung print_plural(", ", ifi->ifi_ierrors, "error");
1386 1.208 dyoung if (ifi->ifi_iqdrops)
1387 1.208 dyoung print_plural(", ", ifi->ifi_iqdrops, "queue drop");
1388 1.208 dyoung if (ifi->ifi_noproto)
1389 1.208 dyoung printf(", %" PRIu64 " unknown protocol", ifi->ifi_noproto);
1390 1.208 dyoung print_plural("\n\toutput: ", ifi->ifi_opackets, "packet");
1391 1.208 dyoung print_human_bytes(hflag, ifi->ifi_obytes);
1392 1.208 dyoung if (ifi->ifi_omcasts)
1393 1.208 dyoung print_plural(", ", ifi->ifi_omcasts, "multicast");
1394 1.208 dyoung if (ifi->ifi_oerrors)
1395 1.208 dyoung print_plural(", ", ifi->ifi_oerrors, "error");
1396 1.208 dyoung if (ifi->ifi_collisions)
1397 1.208 dyoung print_plural(", ", ifi->ifi_collisions, "collision");
1398 1.208 dyoung printf("\n");
1399 1.124 matt
1400 1.208 dyoung SIMPLEQ_FOREACH(statistics_f, &statistics_funcs, f_next)
1401 1.208 dyoung (*statistics_f->f_func)(env);
1402 1.149 dyoung
1403 1.24 thorpej proto_status:
1404 1.187 dyoung
1405 1.187 dyoung if (afp != NULL)
1406 1.189 dyoung (*afp->af_status)(env, oenv, true);
1407 1.208 dyoung else SIMPLEQ_FOREACH(afp, &aflist, af_next)
1408 1.189 dyoung (*afp->af_status)(env, oenv, false);
1409 1.49 christos }
1410 1.49 christos
1411 1.208 dyoung static int
1412 1.211 dyoung setifprefixlen(prop_dictionary_t env, prop_dictionary_t oenv)
1413 1.53 itojun {
1414 1.189 dyoung bool rc;
1415 1.189 dyoung int64_t plen;
1416 1.187 dyoung int af;
1417 1.187 dyoung struct paddr_prefix *pfx;
1418 1.187 dyoung prop_data_t d;
1419 1.187 dyoung
1420 1.187 dyoung if ((af = getaf(env)) == -1)
1421 1.187 dyoung af = AF_INET;
1422 1.187 dyoung
1423 1.189 dyoung rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
1424 1.189 dyoung assert(rc);
1425 1.187 dyoung
1426 1.189 dyoung pfx = prefixlen_to_mask(af, plen);
1427 1.187 dyoung if (pfx == NULL)
1428 1.187 dyoung err(EXIT_FAILURE, "prefixlen_to_mask");
1429 1.187 dyoung
1430 1.242 thorpej d = prop_data_create_copy(pfx, paddr_prefix_size(pfx));
1431 1.187 dyoung if (d == NULL)
1432 1.242 thorpej err(EXIT_FAILURE, "%s: prop_data_create_copy", __func__);
1433 1.187 dyoung
1434 1.211 dyoung if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
1435 1.187 dyoung err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
1436 1.187 dyoung
1437 1.187 dyoung free(pfx);
1438 1.187 dyoung return 0;
1439 1.53 itojun }
1440 1.53 itojun
1441 1.223 pooka static int
1442 1.223 pooka setlinkstr(prop_dictionary_t env, prop_dictionary_t oenv)
1443 1.223 pooka {
1444 1.223 pooka struct ifdrv ifdrv;
1445 1.223 pooka size_t linkstrlen;
1446 1.223 pooka prop_data_t data;
1447 1.225 pooka char *linkstr;
1448 1.223 pooka
1449 1.223 pooka data = (prop_data_t)prop_dictionary_get(env, "linkstr");
1450 1.223 pooka if (data == NULL) {
1451 1.223 pooka errno = ENOENT;
1452 1.223 pooka return -1;
1453 1.223 pooka }
1454 1.223 pooka linkstrlen = prop_data_size(data)+1;
1455 1.225 pooka
1456 1.225 pooka linkstr = malloc(linkstrlen);
1457 1.225 pooka if (linkstr == NULL)
1458 1.225 pooka err(EXIT_FAILURE, "malloc linkstr space");
1459 1.225 pooka if (getargstr(env, "linkstr", linkstr, linkstrlen) == -1)
1460 1.225 pooka errx(EXIT_FAILURE, "getargstr linkstr failed");
1461 1.223 pooka
1462 1.223 pooka ifdrv.ifd_cmd = 0;
1463 1.223 pooka ifdrv.ifd_len = linkstrlen;
1464 1.223 pooka ifdrv.ifd_data = __UNCONST(linkstr);
1465 1.223 pooka
1466 1.223 pooka if (direct_ioctl(env, SIOCSLINKSTR, &ifdrv) == -1)
1467 1.223 pooka err(EXIT_FAILURE, "SIOCSLINKSTR");
1468 1.225 pooka free(linkstr);
1469 1.223 pooka
1470 1.223 pooka return 0;
1471 1.223 pooka }
1472 1.223 pooka
1473 1.223 pooka static int
1474 1.223 pooka unsetlinkstr(prop_dictionary_t env, prop_dictionary_t oenv)
1475 1.223 pooka {
1476 1.223 pooka struct ifdrv ifdrv;
1477 1.223 pooka
1478 1.223 pooka memset(&ifdrv, 0, sizeof(ifdrv));
1479 1.223 pooka ifdrv.ifd_cmd = IFLINKSTR_UNSET;
1480 1.223 pooka
1481 1.223 pooka if (direct_ioctl(env, SIOCSLINKSTR, &ifdrv) == -1)
1482 1.223 pooka err(EXIT_FAILURE, "SIOCSLINKSTR");
1483 1.223 pooka
1484 1.223 pooka return 0;
1485 1.223 pooka }
1486 1.223 pooka
1487 1.239 ozaki static int
1488 1.239 ozaki setifdescr(prop_dictionary_t env, prop_dictionary_t oenv)
1489 1.239 ozaki {
1490 1.239 ozaki struct ifreq ifr;
1491 1.239 ozaki size_t len;
1492 1.239 ozaki prop_data_t data;
1493 1.239 ozaki char *descr;
1494 1.239 ozaki
1495 1.240 msaitoh data = (prop_data_t)prop_dictionary_get(env, "descr");
1496 1.239 ozaki if (data == NULL) {
1497 1.239 ozaki errno = ENOENT;
1498 1.239 ozaki return -1;
1499 1.239 ozaki }
1500 1.239 ozaki len = prop_data_size(data) + 1;
1501 1.239 ozaki
1502 1.240 msaitoh if (len > IFDESCRSIZE)
1503 1.239 ozaki err(EXIT_FAILURE, "description too long");
1504 1.239 ozaki
1505 1.240 msaitoh descr = malloc(len);
1506 1.239 ozaki if (descr == NULL)
1507 1.239 ozaki err(EXIT_FAILURE, "malloc description space");
1508 1.239 ozaki if (getargstr(env, "descr", descr, len) == -1)
1509 1.239 ozaki errx(EXIT_FAILURE, "getargstr descr failed");
1510 1.239 ozaki
1511 1.239 ozaki
1512 1.240 msaitoh ifr.ifr_buf = descr;
1513 1.239 ozaki ifr.ifr_buflen = len;
1514 1.239 ozaki if (direct_ioctl(env, SIOCSIFDESCR, &ifr) != 0)
1515 1.239 ozaki err(EXIT_FAILURE, "SIOCSIFDESCR");
1516 1.239 ozaki
1517 1.240 msaitoh free(descr);
1518 1.239 ozaki
1519 1.240 msaitoh return 0;
1520 1.239 ozaki }
1521 1.239 ozaki
1522 1.239 ozaki static int
1523 1.239 ozaki unsetifdescr(prop_dictionary_t env, prop_dictionary_t oenv)
1524 1.239 ozaki {
1525 1.239 ozaki struct ifreq ifr;
1526 1.239 ozaki ifr.ifr_buf = NULL;
1527 1.239 ozaki ifr.ifr_buflen = 0;
1528 1.239 ozaki
1529 1.240 msaitoh if (direct_ioctl(env, SIOCSIFDESCR, &ifr) != 0)
1530 1.239 ozaki err(EXIT_FAILURE, "SIOCSIFDESCR");
1531 1.239 ozaki
1532 1.240 msaitoh return 0;
1533 1.239 ozaki }
1534 1.239 ozaki
1535 1.239 ozaki
1536 1.210 dyoung static void
1537 1.145 dsl usage(void)
1538 1.18 glass {
1539 1.99 cgd const char *progname = getprogname();
1540 1.209 dyoung usage_func_t *usage_f;
1541 1.209 dyoung prop_dictionary_t env;
1542 1.80 thorpej
1543 1.209 dyoung if ((env = prop_dictionary_create()) == NULL)
1544 1.209 dyoung err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
1545 1.209 dyoung
1546 1.209 dyoung fprintf(stderr, "usage: %s [-h] %s[-v] [-z] %sinterface\n"
1547 1.105 itojun "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1548 1.105 itojun "\t\t[ alias | -alias ] ]\n"
1549 1.209 dyoung "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n", progname,
1550 1.209 dyoung flag_is_registered(gflags, 'm') ? "[-m] " : "",
1551 1.209 dyoung flag_is_registered(gflags, 'L') ? "[-L] " : "");
1552 1.209 dyoung
1553 1.209 dyoung SIMPLEQ_FOREACH(usage_f, &usage_funcs, f_next)
1554 1.209 dyoung (*usage_f->f_func)(env);
1555 1.209 dyoung
1556 1.209 dyoung fprintf(stderr,
1557 1.80 thorpej "\t[ arp | -arp ]\n"
1558 1.178 dyoung "\t[ preference n ]\n"
1559 1.80 thorpej "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1560 1.237 ozaki "\t[ linkstr str | -linkstr ]\n"
1561 1.239 ozaki "\t[ description str | descr str | -description | -descr ]\n"
1562 1.209 dyoung " %s -a [-b] [-d] [-h] %s[-u] [-v] [-z] [ af ]\n"
1563 1.209 dyoung " %s -l [-b] [-d] [-s] [-u]\n"
1564 1.101 christos " %s -C\n"
1565 1.232 roy " %s -w n\n"
1566 1.81 thorpej " %s interface create\n"
1567 1.81 thorpej " %s interface destroy\n",
1568 1.209 dyoung progname, flag_is_registered(gflags, 'm') ? "[-m] " : "",
1569 1.232 roy progname, progname, progname, progname, progname);
1570 1.209 dyoung
1571 1.209 dyoung prop_object_release((prop_object_t)env);
1572 1.187 dyoung exit(EXIT_FAILURE);
1573 1.1 cgd }
1574