No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / ifconfig / ifconfig.c
blobeffbfabf1a772b8ddb807391c36c1e0876fcf3b7
1 /* $NetBSD: ifconfig.c,v 1.219 2009/05/26 16:03:24 pooka Exp $ */
3 /*-
4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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.
34 * Copyright (c) 1983, 1993
35 * The Regents of the University of California. All rights reserved.
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.
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.
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.219 2009/05/26 16:03:24 pooka Exp $");
67 #endif /* not lint */
69 #include <sys/param.h>
70 #include <sys/queue.h>
71 #include <sys/socket.h>
72 #include <sys/ioctl.h>
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 */
81 #include <netdb.h>
83 #include <sys/protosw.h>
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>
98 #include "extern.h"
100 #include "media.h"
101 #include "parse.h"
102 #include "env.h"
104 static bool bflag, dflag, hflag, sflag, uflag;
105 bool lflag, Nflag, vflag, zflag;
107 static char gflags[10 + 26 * 2 + 1] = "AabCdhlNsuvz";
108 bool gflagset[10 + 26 * 2];
110 static int carrier(prop_dictionary_t);
111 static int clone_command(prop_dictionary_t, prop_dictionary_t);
112 static void do_setifpreference(prop_dictionary_t);
113 static int flag_index(int);
114 static void init_afs(void);
115 static int list_cloners(prop_dictionary_t, prop_dictionary_t);
116 static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
117 static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
118 static int notrailers(prop_dictionary_t, prop_dictionary_t);
119 static void printall(const char *, prop_dictionary_t);
120 static int setifaddr(prop_dictionary_t, prop_dictionary_t);
121 static int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
122 static int setifcaps(prop_dictionary_t, prop_dictionary_t);
123 static int setifdstormask(prop_dictionary_t, prop_dictionary_t);
124 static int setifflags(prop_dictionary_t, prop_dictionary_t);
125 static int setifmetric(prop_dictionary_t, prop_dictionary_t);
126 static int setifmtu(prop_dictionary_t, prop_dictionary_t);
127 static int setifnetmask(prop_dictionary_t, prop_dictionary_t);
128 static int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
129 static void status(const struct sockaddr *, prop_dictionary_t,
130 prop_dictionary_t);
131 static void usage(void);
133 static const struct kwinst ifflagskw[] = {
134 IFKW("arp", -IFF_NOARP)
135 , IFKW("debug", IFF_DEBUG)
136 , IFKW("link0", IFF_LINK0)
137 , IFKW("link1", IFF_LINK1)
138 , IFKW("link2", IFF_LINK2)
139 , {.k_word = "down", .k_type = KW_T_INT, .k_int = -IFF_UP}
140 , {.k_word = "up", .k_type = KW_T_INT, .k_int = IFF_UP}
143 static const struct kwinst ifcapskw[] = {
144 IFKW("ip4csum-tx", IFCAP_CSUM_IPv4_Tx)
145 , IFKW("ip4csum-rx", IFCAP_CSUM_IPv4_Rx)
146 , IFKW("tcp4csum-tx", IFCAP_CSUM_TCPv4_Tx)
147 , IFKW("tcp4csum-rx", IFCAP_CSUM_TCPv4_Rx)
148 , IFKW("udp4csum-tx", IFCAP_CSUM_UDPv4_Tx)
149 , IFKW("udp4csum-rx", IFCAP_CSUM_UDPv4_Rx)
150 , IFKW("tcp6csum-tx", IFCAP_CSUM_TCPv6_Tx)
151 , IFKW("tcp6csum-rx", IFCAP_CSUM_TCPv6_Rx)
152 , IFKW("udp6csum-tx", IFCAP_CSUM_UDPv6_Tx)
153 , IFKW("udp6csum-rx", IFCAP_CSUM_UDPv6_Rx)
154 , IFKW("ip4csum", IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
155 , IFKW("tcp4csum", IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
156 , IFKW("udp4csum", IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
157 , IFKW("tcp6csum", IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
158 , IFKW("udp6csum", IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
159 , IFKW("tso4", IFCAP_TSOv4)
160 , IFKW("tso6", IFCAP_TSOv6)
163 extern struct pbranch command_root;
164 extern struct pbranch opt_command;
165 extern struct pbranch opt_family, opt_silent_family;
166 extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
168 struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
169 setifmetric, "metric", &command_root.pb_parser);
171 struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
172 setifmtu, "mtu", &command_root.pb_parser);
174 struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
175 "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
177 struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
178 "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
179 &command_root.pb_parser);
181 struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
182 setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
184 struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
185 "broadcast address",
186 setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
188 static const struct kwinst misckw[] = {
189 {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
190 .k_type = KW_T_BOOL, .k_neg = true,
191 .k_bool = true, .k_negbool = false,
192 .k_nextparser = &command_root.pb_parser}
193 , {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
194 , {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
195 .k_type = KW_T_BOOL, .k_bool = false,
196 .k_nextparser = &command_root.pb_parser}
197 , {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
198 , {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
199 , {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
200 , {.k_word = "preference", .k_act = "address",
201 .k_nextparser = &parse_preference.pi_parser}
202 , {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
203 , {.k_word = "trailers", .k_neg = true,
204 .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
207 /* key: clonecmd */
208 static const struct kwinst clonekw[] = {
209 {.k_word = "create", .k_type = KW_T_INT, .k_int = SIOCIFCREATE,
210 .k_nextparser = &opt_silent_family.pb_parser},
211 {.k_word = "destroy", .k_type = KW_T_INT, .k_int = SIOCIFDESTROY}
214 static struct kwinst familykw[24];
216 struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
217 list_cloners, "none");
219 struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
220 "none");
222 struct pkw family_only =
223 PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
224 __arraycount(familykw), &no_cmds.pt_parser);
226 struct paddr address = PADDR_INITIALIZER(&address,
227 "local address (address 1)",
228 setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
230 struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
231 "destination/netmask (address 2)",
232 setifdstormask, "dstormask", NULL, "address", "dstormask",
233 &command_root.pb_parser);
235 struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
236 "broadcast address (address 3)",
237 setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
238 &command_root.pb_parser);
240 static SIMPLEQ_HEAD(, afswtch) aflist = SIMPLEQ_HEAD_INITIALIZER(aflist);
242 static SIMPLEQ_HEAD(, usage_func) usage_funcs =
243 SIMPLEQ_HEAD_INITIALIZER(usage_funcs);
244 static SIMPLEQ_HEAD(, status_func) status_funcs =
245 SIMPLEQ_HEAD_INITIALIZER(status_funcs);
246 static SIMPLEQ_HEAD(, statistics_func) statistics_funcs =
247 SIMPLEQ_HEAD_INITIALIZER(statistics_funcs);
248 static SIMPLEQ_HEAD(, cmdloop_branch) cmdloop_branches =
249 SIMPLEQ_HEAD_INITIALIZER(cmdloop_branches);
251 struct branch opt_clone_brs[] = {
252 {.b_nextparser = &cloning.pk_parser}
253 , {.b_nextparser = &opt_family.pb_parser}
254 }, opt_silent_family_brs[] = {
255 {.b_nextparser = &silent_family.pk_parser}
256 , {.b_nextparser = &command_root.pb_parser}
257 }, opt_family_brs[] = {
258 {.b_nextparser = &family.pk_parser}
259 , {.b_nextparser = &opt_command.pb_parser}
260 }, command_root_brs[] = {
261 {.b_nextparser = &ifflags.pk_parser}
262 , {.b_nextparser = &ifcaps.pk_parser}
263 , {.b_nextparser = &kwmedia.pk_parser}
264 , {.b_nextparser = &misc.pk_parser}
265 , {.b_nextparser = &address.pa_parser}
266 , {.b_nextparser = &dstormask.pa_parser}
267 , {.b_nextparser = &broadcast.pa_parser}
268 , {.b_nextparser = NULL}
269 }, opt_command_brs[] = {
270 {.b_nextparser = &no_cmds.pt_parser}
271 , {.b_nextparser = &command_root.pb_parser}
274 struct branch opt_family_only_brs[] = {
275 {.b_nextparser = &no_cmds.pt_parser}
276 , {.b_nextparser = &family_only.pk_parser}
278 struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
279 "opt-family-only", opt_family_only_brs,
280 __arraycount(opt_family_only_brs), true);
281 struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
282 "optional command",
283 opt_command_brs, __arraycount(opt_command_brs), true);
285 struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
286 "command-root", command_root_brs, __arraycount(command_root_brs), true);
288 struct piface iface_opt_family_only =
289 PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
290 NULL, "if", &opt_family_only.pb_parser);
292 struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
293 familykw, __arraycount(familykw), &opt_command.pb_parser);
295 struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
296 NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
298 struct pkw *family_users[] = {&family_only, &family, &silent_family};
300 struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
301 "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
303 struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
304 "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
306 struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
307 "clonecmd", clonekw, __arraycount(clonekw), NULL);
309 struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
310 misckw, __arraycount(misckw), NULL);
312 struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
313 "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
315 struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
316 "optional silent family", opt_silent_family_brs,
317 __arraycount(opt_silent_family_brs), true);
319 struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
320 "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
322 struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
323 "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
325 struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
326 media_status_exec, "if", NULL);
328 static bool
329 flag_is_registered(const char *flags, int flag)
331 return flags != NULL && strchr(flags, flag) != NULL;
334 static int
335 check_flag(const char *flags, int flag)
337 if (flag_is_registered(flags, flag)) {
338 errno = EEXIST;
339 return -1;
342 if (flag >= '0' && flag <= '9')
343 return 0;
344 if (flag >= 'a' && flag <= 'z')
345 return 0;
346 if (flag >= 'A' && flag <= 'Z')
347 return 0;
349 errno = EINVAL;
350 return -1;
353 void
354 cmdloop_branch_init(cmdloop_branch_t *b, struct parser *p)
356 b->b_parser = p;
359 void
360 statistics_func_init(statistics_func_t *f, statistics_cb_t func)
362 f->f_func = func;
365 void
366 status_func_init(status_func_t *f, status_cb_t func)
368 f->f_func = func;
371 void
372 usage_func_init(usage_func_t *f, usage_cb_t func)
374 f->f_func = func;
378 register_cmdloop_branch(cmdloop_branch_t *b)
380 SIMPLEQ_INSERT_TAIL(&cmdloop_branches, b, b_next);
381 return 0;
385 register_statistics(statistics_func_t *f)
387 SIMPLEQ_INSERT_TAIL(&statistics_funcs, f, f_next);
388 return 0;
392 register_status(status_func_t *f)
394 SIMPLEQ_INSERT_TAIL(&status_funcs, f, f_next);
395 return 0;
399 register_usage(usage_func_t *f)
401 SIMPLEQ_INSERT_TAIL(&usage_funcs, f, f_next);
402 return 0;
406 register_family(struct afswtch *af)
408 SIMPLEQ_INSERT_TAIL(&aflist, af, af_next);
409 return 0;
413 register_flag(int flag)
415 if (check_flag(gflags, flag) == -1)
416 return -1;
418 if (strlen(gflags) + 1 >= sizeof(gflags)) {
419 errno = ENOMEM;
420 return -1;
423 gflags[strlen(gflags)] = flag;
425 return 0;
428 static int
429 flag_index(int flag)
431 if (flag >= '0' && flag <= '9')
432 return flag - '0';
433 if (flag >= 'a' && flag <= 'z')
434 return 10 + flag - 'a';
435 if (flag >= 'A' && flag <= 'Z')
436 return 10 + 26 + flag - 'a';
438 errno = EINVAL;
439 return -1;
442 static bool
443 set_flag(int flag)
445 int idx;
447 if ((idx = flag_index(flag)) == -1)
448 return false;
450 return gflagset[idx] = true;
453 bool
454 get_flag(int flag)
456 int idx;
458 if ((idx = flag_index(flag)) == -1)
459 return false;
461 return gflagset[idx];
464 static struct parser *
465 init_parser(void)
467 cmdloop_branch_t *b;
469 if (parser_init(&iface_opt_family_only.pif_parser) == -1)
470 err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
471 if (parser_init(&iface_only.pif_parser) == -1)
472 err(EXIT_FAILURE, "parser_init(iface_only)");
473 if (parser_init(&iface_start.pif_parser) == -1)
474 err(EXIT_FAILURE, "parser_init(iface_start)");
476 SIMPLEQ_FOREACH(b, &cmdloop_branches, b_next)
477 pbranch_addbranch(&command_root, b->b_parser);
479 return &iface_start.pif_parser;
482 static int
483 no_cmds_exec(prop_dictionary_t env, prop_dictionary_t oenv)
485 const char *ifname;
486 unsigned short ignore;
488 /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
489 if ((ifname = getifname(env)) == NULL)
491 else if (getifflags(env, oenv, &ignore) == -1)
492 err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
494 printall(ifname, env);
495 exit(EXIT_SUCCESS);
498 static int
499 media_status_exec(prop_dictionary_t env, prop_dictionary_t oenv)
501 const char *ifname;
502 unsigned short ignore;
504 /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
505 if ((ifname = getifname(env)) == NULL)
507 else if (getifflags(env, oenv, &ignore) == -1)
508 err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
510 exit(carrier(env));
513 static void
514 do_setifcaps(prop_dictionary_t env)
516 struct ifcapreq ifcr;
517 prop_data_t d;
519 d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
520 if (d == NULL)
521 return;
523 assert(sizeof(ifcr) == prop_data_size(d));
525 memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
526 if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
527 err(EXIT_FAILURE, "SIOCSIFCAP");
531 main(int argc, char **argv)
533 const struct afswtch *afp;
534 int af, s;
535 bool aflag = false, Cflag = false;
536 struct match match[32];
537 size_t nmatch;
538 struct parser *start;
539 int ch, narg = 0, rc;
540 prop_dictionary_t env, oenv;
541 const char *ifname;
543 #ifdef RUMP_ACTION
544 rump_init();
545 #endif
546 memset(match, 0, sizeof(match));
548 init_afs();
550 start = init_parser();
552 /* Parse command-line options */
553 aflag = Nflag = vflag = zflag = false;
554 while ((ch = getopt(argc, argv, gflags)) != -1) {
555 switch (ch) {
556 case 'A':
557 warnx("-A is deprecated");
558 break;
560 case 'a':
561 aflag = true;
562 break;
564 case 'b':
565 bflag = true;
566 break;
568 case 'C':
569 Cflag = true;
570 break;
572 case 'd':
573 dflag = true;
574 break;
575 case 'h':
576 hflag = true;
577 break;
578 case 'l':
579 lflag = true;
580 break;
581 case 'N':
582 Nflag = true;
583 break;
585 case 's':
586 sflag = true;
587 break;
589 case 'u':
590 uflag = true;
591 break;
593 case 'v':
594 vflag = true;
595 break;
597 case 'z':
598 zflag = true;
599 break;
601 default:
602 if (!set_flag(ch))
603 usage();
604 break;
606 switch (ch) {
607 case 'a':
608 start = &opt_family_only.pb_parser;
609 break;
611 case 'L':
612 case 'm':
613 case 'v':
614 case 'z':
615 if (start != &opt_family_only.pb_parser)
616 start = &iface_opt_family_only.pif_parser;
617 break;
618 case 'C':
619 start = &cloneterm.pt_parser;
620 break;
621 case 'l':
622 start = &no_cmds.pt_parser;
623 break;
624 case 's':
625 if (start != &no_cmds.pt_parser &&
626 start != &opt_family_only.pb_parser)
627 start = &iface_only.pif_parser;
628 break;
629 default:
630 break;
633 argc -= optind;
634 argv += optind;
637 * -l means "list all interfaces", and is mutally exclusive with
638 * all other flags/commands.
640 * -C means "list all names of cloners", and it mutually exclusive
641 * with all other flags/commands.
643 * -a means "print status of all interfaces".
645 if ((lflag || Cflag) && (aflag || get_flag('m') || vflag || zflag))
646 usage();
647 if ((lflag || Cflag) && get_flag('L'))
648 usage();
649 if (lflag && Cflag)
650 usage();
652 nmatch = __arraycount(match);
654 rc = parse(argc, argv, start, match, &nmatch, &narg);
655 if (rc != 0)
656 usage();
658 if ((oenv = prop_dictionary_create()) == NULL)
659 err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
661 if (matches_exec(match, oenv, nmatch) == -1)
662 err(EXIT_FAILURE, "exec_matches");
664 argc -= narg;
665 argv += narg;
667 env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
668 if (env == NULL)
669 env = oenv;
670 else
671 env = prop_dictionary_augment(env, oenv);
673 /* Process any media commands that may have been issued. */
674 process_media_commands(env);
676 if ((af = getaf(env)) == -1)
677 af = AF_INET;
679 if ((s = getsock(af)) == -1)
680 err(EXIT_FAILURE, "%s: getsock", __func__);
682 if ((ifname = getifname(env)) == NULL)
683 err(EXIT_FAILURE, "%s: getifname", __func__);
685 if ((afp = lookup_af_bynum(af)) == NULL)
686 errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
688 assert(afp->af_addr_commit != NULL);
689 (*afp->af_addr_commit)(env, oenv);
691 do_setifpreference(env);
692 do_setifcaps(env);
694 exit(EXIT_SUCCESS);
697 static void
698 init_afs(void)
700 size_t i;
701 const struct afswtch *afp;
702 struct kwinst kw = {.k_type = KW_T_INT};
704 SIMPLEQ_FOREACH(afp, &aflist, af_next) {
705 kw.k_word = afp->af_name;
706 kw.k_int = afp->af_af;
707 for (i = 0; i < __arraycount(familykw); i++) {
708 if (familykw[i].k_word == NULL) {
709 familykw[i] = kw;
710 break;
716 const struct afswtch *
717 lookup_af_bynum(int afnum)
719 const struct afswtch *afp;
721 SIMPLEQ_FOREACH(afp, &aflist, af_next) {
722 if (afp->af_af == afnum)
723 break;
725 return afp;
728 void
729 printall(const char *ifname, prop_dictionary_t env0)
731 struct ifaddrs *ifap, *ifa;
732 struct ifreq ifr;
733 const struct sockaddr *sdl = NULL;
734 prop_dictionary_t env, oenv;
735 int idx;
736 char *p;
738 if (env0 == NULL)
739 env = prop_dictionary_create();
740 else
741 env = prop_dictionary_copy_mutable(env0);
743 oenv = prop_dictionary_create();
745 if (env == NULL || oenv == NULL)
746 errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
748 if (getifaddrs(&ifap) != 0)
749 err(EXIT_FAILURE, "getifaddrs");
750 p = NULL;
751 idx = 0;
752 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
753 memset(&ifr, 0, sizeof(ifr));
754 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
755 if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
756 memcpy(&ifr.ifr_addr, ifa->ifa_addr,
757 ifa->ifa_addr->sa_len);
760 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
761 continue;
762 if (ifa->ifa_addr->sa_family == AF_LINK)
763 sdl = ifa->ifa_addr;
764 if (p && strcmp(p, ifa->ifa_name) == 0)
765 continue;
766 if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
767 continue;
768 p = ifa->ifa_name;
770 if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
771 continue;
772 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
773 continue;
774 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
775 continue;
777 if (sflag && carrier(env))
778 continue;
779 idx++;
781 * Are we just listing the interfaces?
783 if (lflag) {
784 if (idx > 1)
785 printf(" ");
786 fputs(ifa->ifa_name, stdout);
787 continue;
790 status(sdl, env, oenv);
791 sdl = NULL;
793 if (lflag)
794 printf("\n");
795 prop_object_release((prop_object_t)env);
796 prop_object_release((prop_object_t)oenv);
797 freeifaddrs(ifap);
800 static int
801 list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
803 struct if_clonereq ifcr;
804 char *cp, *buf;
805 int idx, s;
807 memset(&ifcr, 0, sizeof(ifcr));
809 s = getsock(AF_INET);
811 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
812 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
814 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
815 if (buf == NULL)
816 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
818 ifcr.ifcr_count = ifcr.ifcr_total;
819 ifcr.ifcr_buffer = buf;
821 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
822 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
825 * In case some disappeared in the mean time, clamp it down.
827 if (ifcr.ifcr_count > ifcr.ifcr_total)
828 ifcr.ifcr_count = ifcr.ifcr_total;
830 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
831 if (idx > 0)
832 printf(" ");
833 printf("%s", cp);
836 printf("\n");
837 free(buf);
838 exit(EXIT_SUCCESS);
841 static int
842 clone_command(prop_dictionary_t env, prop_dictionary_t oenv)
844 int64_t cmd;
846 if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
847 errno = ENOENT;
848 return -1;
851 if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
852 warn("%s", __func__);
853 return -1;
855 return 0;
858 /*ARGSUSED*/
859 static int
860 setifaddr(prop_dictionary_t env, prop_dictionary_t oenv)
862 const struct paddr_prefix *pfx0;
863 struct paddr_prefix *pfx;
864 prop_data_t d;
865 int af;
867 if ((af = getaf(env)) == -1)
868 af = AF_INET;
870 d = (prop_data_t)prop_dictionary_get(env, "address");
871 assert(d != NULL);
872 pfx0 = prop_data_data_nocopy(d);
874 if (pfx0->pfx_len >= 0) {
875 pfx = prefixlen_to_mask(af, pfx0->pfx_len);
876 if (pfx == NULL)
877 err(EXIT_FAILURE, "prefixlen_to_mask");
878 free(pfx);
881 return 0;
884 static int
885 setifnetmask(prop_dictionary_t env, prop_dictionary_t oenv)
887 const struct paddr_prefix *pfx;
888 prop_data_t d;
890 d = (prop_data_t)prop_dictionary_get(env, "dstormask");
891 assert(d != NULL);
892 pfx = prop_data_data_nocopy(d);
894 if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
895 return -1;
897 return 0;
900 static int
901 setifbroadaddr(prop_dictionary_t env, prop_dictionary_t oenv)
903 const struct paddr_prefix *pfx;
904 prop_data_t d;
905 unsigned short flags;
907 if (getifflags(env, oenv, &flags) == -1)
908 err(EXIT_FAILURE, "%s: getifflags", __func__);
910 if ((flags & IFF_BROADCAST) == 0)
911 errx(EXIT_FAILURE, "not a broadcast interface");
913 d = (prop_data_t)prop_dictionary_get(env, "broadcast");
914 assert(d != NULL);
915 pfx = prop_data_data_nocopy(d);
917 if (!prop_dictionary_set(oenv, "broadcast", (prop_object_t)d))
918 return -1;
920 return 0;
923 /*ARGSUSED*/
924 static int
925 notrailers(prop_dictionary_t env, prop_dictionary_t oenv)
927 puts("Note: trailers are no longer sent, but always received");
928 return 0;
931 /*ARGSUSED*/
932 static int
933 setifdstormask(prop_dictionary_t env, prop_dictionary_t oenv)
935 const char *key;
936 const struct paddr_prefix *pfx;
937 prop_data_t d;
938 unsigned short flags;
940 if (getifflags(env, oenv, &flags) == -1)
941 err(EXIT_FAILURE, "%s: getifflags", __func__);
943 d = (prop_data_t)prop_dictionary_get(env, "dstormask");
944 assert(d != NULL);
945 pfx = prop_data_data_nocopy(d);
947 if ((flags & IFF_BROADCAST) == 0) {
948 key = "dst";
949 } else {
950 key = "netmask";
953 if (!prop_dictionary_set(oenv, key, (prop_object_t)d))
954 return -1;
956 return 0;
959 static int
960 setifflags(prop_dictionary_t env, prop_dictionary_t oenv)
962 struct ifreq ifr;
963 int64_t ifflag;
964 bool rc;
966 rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
967 assert(rc);
969 if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
970 return -1;
972 if (ifflag < 0) {
973 ifflag = -ifflag;
974 ifr.ifr_flags &= ~ifflag;
975 } else
976 ifr.ifr_flags |= ifflag;
978 if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
979 return -1;
981 return 0;
984 static int
985 getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
987 bool rc;
988 struct ifcapreq ifcr;
989 const struct ifcapreq *tmpifcr;
990 prop_data_t capdata;
992 capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
994 if (capdata != NULL) {
995 tmpifcr = prop_data_data_nocopy(capdata);
996 *oifcr = *tmpifcr;
997 return 0;
1000 (void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
1001 *oifcr = ifcr;
1003 capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
1005 rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1007 prop_object_release((prop_object_t)capdata);
1009 return rc ? 0 : -1;
1012 static int
1013 setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
1015 int64_t ifcap;
1016 int s;
1017 bool rc;
1018 prop_data_t capdata;
1019 struct ifcapreq ifcr;
1021 s = getsock(AF_INET);
1023 rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
1024 assert(rc);
1026 if (getifcaps(env, oenv, &ifcr) == -1)
1027 return -1;
1029 if (ifcap < 0) {
1030 ifcap = -ifcap;
1031 ifcr.ifcr_capenable &= ~ifcap;
1032 } else
1033 ifcr.ifcr_capenable |= ifcap;
1035 if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
1036 return -1;
1038 rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1039 prop_object_release((prop_object_t)capdata);
1041 return rc ? 0 : -1;
1044 static int
1045 setifmetric(prop_dictionary_t env, prop_dictionary_t oenv)
1047 struct ifreq ifr;
1048 bool rc;
1049 int64_t metric;
1051 rc = prop_dictionary_get_int64(env, "metric", &metric);
1052 assert(rc);
1054 ifr.ifr_metric = metric;
1055 if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
1056 warn("SIOCSIFMETRIC");
1057 return 0;
1060 static void
1061 do_setifpreference(prop_dictionary_t env)
1063 struct if_addrprefreq ifap;
1064 prop_data_t d;
1065 const struct paddr_prefix *pfx;
1067 memset(&ifap, 0, sizeof(ifap));
1069 if (!prop_dictionary_get_int16(env, "preference",
1070 &ifap.ifap_preference))
1071 return;
1073 d = (prop_data_t)prop_dictionary_get(env, "address");
1074 assert(d != NULL);
1076 pfx = prop_data_data_nocopy(d);
1078 memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
1079 MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
1080 if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
1081 warn("SIOCSIFADDRPREF");
1084 static int
1085 setifmtu(prop_dictionary_t env, prop_dictionary_t oenv)
1087 int64_t mtu;
1088 bool rc;
1089 struct ifreq ifr;
1091 rc = prop_dictionary_get_int64(env, "mtu", &mtu);
1092 assert(rc);
1094 ifr.ifr_mtu = mtu;
1095 if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
1096 warn("SIOCSIFMTU");
1098 return 0;
1101 static int
1102 carrier(prop_dictionary_t env)
1104 struct ifmediareq ifmr;
1106 memset(&ifmr, 0, sizeof(ifmr));
1108 if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
1110 * Interface doesn't support SIOC{G,S}IFMEDIA;
1111 * assume ok.
1113 return EXIT_SUCCESS;
1115 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1117 * Interface doesn't report media-valid status.
1118 * assume ok.
1120 return EXIT_SUCCESS;
1122 /* otherwise, return ok for active, not-ok if not active. */
1123 if (ifmr.ifm_status & IFM_ACTIVE)
1124 return EXIT_SUCCESS;
1125 else
1126 return EXIT_FAILURE;
1129 static void
1130 print_plural(const char *prefix, uint64_t n, const char *unit)
1132 printf("%s%" PRIu64 " %s%s", prefix, n, unit, (n == 1) ? "" : "s");
1135 static void
1136 print_human_bytes(bool humanize, uint64_t n)
1138 char buf[5];
1140 if (humanize) {
1141 (void)humanize_number(buf, sizeof(buf),
1142 (int64_t)n, "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
1143 printf(", %s byte%s", buf, (atof(buf) == 1.0) ? "" : "s");
1144 } else
1145 print_plural(", ", n, "byte");
1149 * Print the status of the interface. If an address family was
1150 * specified, show it and it only; otherwise, show them all.
1152 void
1153 status(const struct sockaddr *sdl, prop_dictionary_t env,
1154 prop_dictionary_t oenv)
1156 const struct if_data *ifi;
1157 status_func_t *status_f;
1158 statistics_func_t *statistics_f;
1159 struct ifdatareq ifdr;
1160 struct ifreq ifr;
1161 char fbuf[BUFSIZ];
1162 int af, s;
1163 const char *ifname;
1164 struct ifcapreq ifcr;
1165 unsigned short flags;
1166 const struct afswtch *afp;
1168 if ((af = getaf(env)) == -1) {
1169 afp = NULL;
1170 af = AF_UNSPEC;
1171 } else
1172 afp = lookup_af_bynum(af);
1174 /* get out early if the family is unsupported by the kernel */
1175 if ((s = getsock(af)) == -1)
1176 err(EXIT_FAILURE, "%s: getsock", __func__);
1178 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
1179 err(EXIT_FAILURE, "%s: getifinfo", __func__);
1181 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1182 printf("%s: flags=%s", ifname, &fbuf[2]);
1184 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1185 if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
1186 warn("SIOCGIFMETRIC %s", ifr.ifr_name);
1187 else if (ifr.ifr_metric != 0)
1188 printf(" metric %d", ifr.ifr_metric);
1190 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1191 if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
1192 printf(" mtu %d", ifr.ifr_mtu);
1193 printf("\n");
1195 if (getifcaps(env, oenv, &ifcr) == -1)
1196 err(EXIT_FAILURE, "%s: getifcaps", __func__);
1198 if (ifcr.ifcr_capabilities != 0) {
1199 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1200 ifcr.ifcr_capabilities);
1201 printf("\tcapabilities=%s\n", &fbuf[2]);
1202 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1203 ifcr.ifcr_capenable);
1204 printf("\tenabled=%s\n", &fbuf[2]);
1207 SIMPLEQ_FOREACH(status_f, &status_funcs, f_next)
1208 (*status_f->f_func)(env, oenv);
1210 print_link_addresses(env, true);
1212 media_status(env, oenv);
1214 if (!vflag && !zflag)
1215 goto proto_status;
1217 estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
1219 if (ioctl(s, zflag ? SIOCZIFDATA : SIOCGIFDATA, &ifdr) == -1)
1220 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1222 ifi = &ifdr.ifdr_data;
1224 print_plural("\tinput: ", ifi->ifi_ipackets, "packet");
1225 print_human_bytes(hflag, ifi->ifi_ibytes);
1226 if (ifi->ifi_imcasts)
1227 print_plural(", ", ifi->ifi_imcasts, "multicast");
1228 if (ifi->ifi_ierrors)
1229 print_plural(", ", ifi->ifi_ierrors, "error");
1230 if (ifi->ifi_iqdrops)
1231 print_plural(", ", ifi->ifi_iqdrops, "queue drop");
1232 if (ifi->ifi_noproto)
1233 printf(", %" PRIu64 " unknown protocol", ifi->ifi_noproto);
1234 print_plural("\n\toutput: ", ifi->ifi_opackets, "packet");
1235 print_human_bytes(hflag, ifi->ifi_obytes);
1236 if (ifi->ifi_omcasts)
1237 print_plural(", ", ifi->ifi_omcasts, "multicast");
1238 if (ifi->ifi_oerrors)
1239 print_plural(", ", ifi->ifi_oerrors, "error");
1240 if (ifi->ifi_collisions)
1241 print_plural(", ", ifi->ifi_collisions, "collision");
1242 printf("\n");
1244 SIMPLEQ_FOREACH(statistics_f, &statistics_funcs, f_next)
1245 (*statistics_f->f_func)(env);
1247 proto_status:
1249 if (afp != NULL)
1250 (*afp->af_status)(env, oenv, true);
1251 else SIMPLEQ_FOREACH(afp, &aflist, af_next)
1252 (*afp->af_status)(env, oenv, false);
1255 static int
1256 setifprefixlen(prop_dictionary_t env, prop_dictionary_t oenv)
1258 bool rc;
1259 int64_t plen;
1260 int af;
1261 struct paddr_prefix *pfx;
1262 prop_data_t d;
1264 if ((af = getaf(env)) == -1)
1265 af = AF_INET;
1267 rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
1268 assert(rc);
1270 pfx = prefixlen_to_mask(af, plen);
1271 if (pfx == NULL)
1272 err(EXIT_FAILURE, "prefixlen_to_mask");
1274 d = prop_data_create_data(pfx, paddr_prefix_size(pfx));
1275 if (d == NULL)
1276 err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
1278 if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
1279 err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
1281 free(pfx);
1282 return 0;
1285 static void
1286 usage(void)
1288 const char *progname = getprogname();
1289 usage_func_t *usage_f;
1290 prop_dictionary_t env;
1292 if ((env = prop_dictionary_create()) == NULL)
1293 err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
1295 fprintf(stderr, "usage: %s [-h] %s[-v] [-z] %sinterface\n"
1296 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1297 "\t\t[ alias | -alias ] ]\n"
1298 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n", progname,
1299 flag_is_registered(gflags, 'm') ? "[-m] " : "",
1300 flag_is_registered(gflags, 'L') ? "[-L] " : "");
1302 SIMPLEQ_FOREACH(usage_f, &usage_funcs, f_next)
1303 (*usage_f->f_func)(env);
1305 fprintf(stderr,
1306 "\t[ arp | -arp ]\n"
1307 "\t[ preference n ]\n"
1308 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1309 " %s -a [-b] [-d] [-h] %s[-u] [-v] [-z] [ af ]\n"
1310 " %s -l [-b] [-d] [-s] [-u]\n"
1311 " %s -C\n"
1312 " %s interface create\n"
1313 " %s interface destroy\n",
1314 progname, flag_is_registered(gflags, 'm') ? "[-m] " : "",
1315 progname, progname, progname, progname);
1317 prop_object_release((prop_object_t)env);
1318 exit(EXIT_FAILURE);