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