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