ifconfig.c revision 1.195 1 /* $NetBSD: ifconfig.c,v 1.195 2008/05/07 19:55:24 dyoung 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\n\
65 The Regents of the University of California. All rights reserved.\n");
66 #endif /* not lint */
67
68 #ifndef lint
69 #if 0
70 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
71 #else
72 __RCSID("$NetBSD: ifconfig.c,v 1.195 2008/05/07 19:55:24 dyoung Exp $");
73 #endif
74 #endif /* not lint */
75
76 #include <sys/param.h>
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79
80 #include <net/if.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83 #include <net/if_ether.h>
84 #include <netinet/in.h> /* XXX */
85 #include <netinet/in_var.h> /* XXX */
86
87 #include <netdb.h>
88
89 #include <sys/protosw.h>
90
91 #include <assert.h>
92 #include <ctype.h>
93 #include <err.h>
94 #include <errno.h>
95 #include <stdbool.h>
96 #include <stddef.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <ifaddrs.h>
102 #include <util.h>
103
104 #include "extern.h"
105
106 #ifndef INET_ONLY
107 #include "af_atalk.h"
108 #include "af_iso.h"
109 #endif /* ! INET_ONLY */
110 #include "af_inet.h"
111 #ifdef INET6
112 #include "af_inet6.h"
113 #endif /* INET6 */
114
115 #include "agr.h"
116 #include "carp.h"
117 #include "ieee80211.h"
118 #include "tunnel.h"
119 #include "vlan.h"
120 #include "parse.h"
121 #include "env.h"
122
123 int setaddr, doalias;
124 int clearaddr;
125 int newaddr = -1;
126 int check_up_state = -1;
127 int bflag, dflag, hflag, lflag, mflag, sflag, uflag, vflag, zflag;
128 #ifdef INET6
129 int Lflag;
130 #endif
131
132 /*
133 * Media stuff. Whenever a media command is first performed, the
134 * currently select media is grabbed for this interface. If `media'
135 * is given, the current media word is modifed. `mediaopt' commands
136 * only modify the set and clear words. They then operate on the
137 * current media word later.
138 */
139 int media_current;
140 int mediaopt_set;
141 int mediaopt_clear;
142
143 void check_ifflags_up(const char *);
144
145 int notealias(prop_dictionary_t, prop_dictionary_t);
146 int notrailers(prop_dictionary_t, prop_dictionary_t);
147 int setifaddr(prop_dictionary_t, prop_dictionary_t);
148 int setifdstormask(prop_dictionary_t, prop_dictionary_t);
149 int setifflags(prop_dictionary_t, prop_dictionary_t);
150 static int setifcaps(prop_dictionary_t, prop_dictionary_t);
151 int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
152 static int setifmetric(prop_dictionary_t, prop_dictionary_t);
153 static int setifmtu(prop_dictionary_t, prop_dictionary_t);
154 int setifnetmask(prop_dictionary_t, prop_dictionary_t);
155 int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
156 int setmedia(prop_dictionary_t, prop_dictionary_t);
157 int setmediamode(prop_dictionary_t, prop_dictionary_t);
158 int setmediaopt(prop_dictionary_t, prop_dictionary_t);
159 int unsetmediaopt(prop_dictionary_t, prop_dictionary_t);
160 int setmediainst(prop_dictionary_t, prop_dictionary_t);
161 static int clone_command(prop_dictionary_t, prop_dictionary_t);
162 static void do_setifpreference(prop_dictionary_t);
163
164 static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
165 static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
166
167 int carrier(prop_dictionary_t);
168 void printall(const char *, prop_dictionary_t);
169 int list_cloners(prop_dictionary_t, prop_dictionary_t);
170 void status(const struct sockaddr_dl *, prop_dictionary_t,
171 prop_dictionary_t);
172 void usage(void);
173
174 void print_media_word(int, const char *);
175 void process_media_commands(prop_dictionary_t);
176 void init_current_media(prop_dictionary_t, prop_dictionary_t);
177
178 /* Known address families */
179 static const struct afswtch afs[] = {
180 {.af_name = "inet", .af_af = AF_INET, .af_status = in_status,
181 .af_addr_commit = commit_address}
182
183 #ifdef INET6
184 , {.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
185 .af_getaddr = in6_getaddr, .af_getprefix = in6_getprefix,
186 .af_difaddr = SIOCDIFADDR_IN6, .af_aifaddr = SIOCAIFADDR_IN6,
187 /* Deleting the first address before setting new one is
188 * not prefered way in this protocol.
189 */
190 .af_gifaddr = 0, .af_ridreq = &in6_ridreq, .af_addreq = &in6_addreq}
191 #endif
192
193 #ifndef INET_ONLY /* small version, for boot media */
194 , {.af_name = "atalk", .af_af = AF_APPLETALK, .af_status = at_status,
195 .af_getaddr = at_getaddr, NULL, .af_difaddr = SIOCDIFADDR,
196 .af_aifaddr = SIOCAIFADDR, .af_gifaddr = SIOCGIFADDR,
197 .af_ridreq = &at_addreq, .af_addreq = &at_addreq}
198 , {.af_name = "iso", .af_af = AF_ISO, .af_status = iso_status,
199 .af_getaddr = iso_getaddr, NULL, .af_difaddr = SIOCDIFADDR_ISO,
200 .af_aifaddr = SIOCAIFADDR_ISO, .af_gifaddr = SIOCGIFADDR_ISO,
201 .af_ridreq = &iso_ridreq, .af_addreq = &iso_addreq}
202 #endif /* INET_ONLY */
203
204 , {.af_name = NULL} /* sentinel */
205 };
206
207 #define IFKW(__word, __flag) \
208 { \
209 .k_word = (__word), .k_neg = true, .k_type = KW_T_NUM, \
210 .k_num = (__flag), \
211 .k_negnum = -(__flag) \
212 }
213
214 struct kwinst ifflagskw[] = {
215 IFKW("arp", IFF_NOARP)
216 , IFKW("debug", IFF_DEBUG)
217 , IFKW("link0", IFF_LINK0)
218 , IFKW("link1", IFF_LINK1)
219 , IFKW("link2", IFF_LINK2)
220 , {.k_word = "down", .k_type = KW_T_NUM, .k_num = -IFF_UP}
221 , {.k_word = "up", .k_type = KW_T_NUM, .k_num = IFF_UP}
222 };
223
224 struct kwinst ifcapskw[] = {
225 IFKW("ip4csum-tx", IFCAP_CSUM_IPv4_Tx)
226 , IFKW("ip4csum-rx", IFCAP_CSUM_IPv4_Rx)
227 , IFKW("tcp4csum-tx", IFCAP_CSUM_TCPv4_Tx)
228 , IFKW("tcp4csum-rx", IFCAP_CSUM_TCPv4_Rx)
229 , IFKW("udp4csum-tx", IFCAP_CSUM_UDPv4_Tx)
230 , IFKW("udp4csum-rx", IFCAP_CSUM_UDPv4_Rx)
231 , IFKW("tcp6csum-tx", IFCAP_CSUM_TCPv6_Tx)
232 , IFKW("tcp6csum-rx", IFCAP_CSUM_TCPv6_Rx)
233 , IFKW("udp6csum-tx", IFCAP_CSUM_UDPv6_Tx)
234 , IFKW("udp6csum-rx", IFCAP_CSUM_UDPv6_Rx)
235 , IFKW("ip4csum", IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
236 , IFKW("tcp4csum", IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
237 , IFKW("udp4csum", IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
238 , IFKW("tcp6csum", IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
239 , IFKW("udp6csum", IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
240 , IFKW("tso4", IFCAP_TSOv4)
241 , IFKW("tso6", IFCAP_TSOv6)
242 };
243
244 extern struct pbranch command_root;
245 extern struct pbranch opt_command;
246 extern struct pbranch opt_family, opt_silent_family;
247 extern struct pkw cloning, silent_family, family, ia6flags, ia6lifetime,
248 ifcaps, ifflags, lists, misc;
249
250 struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
251 setifmetric, "metric", &command_root.pb_parser);
252
253 struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
254 setifmtu, "mtu", &command_root.pb_parser);
255
256 struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
257 "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
258
259 struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
260 "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
261 &command_root.pb_parser);
262
263 struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
264 setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
265
266 struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
267 "broadcast address",
268 setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
269
270 struct pstr mediamode = PSTR_INITIALIZER(&mediamode, "mediamode",
271 setmediamode, "mediamode", &command_root.pb_parser);
272
273 struct pinteger mediainst = PINTEGER_INITIALIZER1(&mediainst, "mediainst",
274 0, IFM_INST_MAX, 10, setmediainst, "mediainst", &command_root.pb_parser);
275
276 struct pstr unmediaopt = PSTR_INITIALIZER(&unmediaopt, "-mediaopt",
277 unsetmediaopt, "unmediaopt", &command_root.pb_parser);
278
279 struct pstr mediaopt = PSTR_INITIALIZER(&mediaopt, "mediaopt",
280 setmediaopt, "mediaopt", &command_root.pb_parser);
281
282 struct pstr media = PSTR_INITIALIZER(&media, "media",
283 setmedia, "media", &command_root.pb_parser);
284
285 struct kwinst misckw[] = {
286 {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
287 .k_type = KW_T_BOOL, .k_neg = true,
288 .k_bool = true, .k_negbool = false,
289 .k_exec = notealias, .k_nextparser = &command_root.pb_parser}
290 , {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
291 , {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
292 .k_type = KW_T_BOOL, .k_bool = false, .k_exec = notealias,
293 .k_nextparser = &command_root.pb_parser}
294 , {.k_word = "deletetunnel", .k_exec = deletetunnel,
295 .k_nextparser = &command_root.pb_parser}
296 , {.k_word = "instance", .k_key = "anymedia", .k_type = KW_T_BOOL,
297 .k_bool = true, .k_act = "media", .k_deact = "mediainst",
298 .k_nextparser = &mediainst.pi_parser}
299 , {.k_word = "inst", .k_key = "anymedia", .k_type = KW_T_BOOL,
300 .k_bool = true, .k_act = "media", .k_deact = "mediainst",
301 .k_nextparser = &mediainst.pi_parser}
302 , {.k_word = "list", .k_nextparser = &lists.pk_parser}
303 , {.k_word = "media", .k_key = "anymedia", .k_type = KW_T_BOOL,
304 .k_bool = true, .k_deact = "media", .k_altdeact = "anymedia",
305 .k_nextparser = &media.ps_parser}
306 , {.k_word = "mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
307 .k_bool = true, .k_deact = "mediaopt", .k_altdeact = "instance",
308 .k_nextparser = &mediaopt.ps_parser}
309 , {.k_word = "-mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
310 .k_bool = true, .k_deact = "unmediaopt", .k_altdeact = "media",
311 .k_nextparser = &unmediaopt.ps_parser}
312 , {.k_word = "mode", .k_key = "anymedia", .k_type = KW_T_BOOL,
313 .k_bool = true, .k_deact = "mode",
314 .k_nextparser = &mediamode.ps_parser}
315 , {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
316 , {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
317 , {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
318 , {.k_word = "preference", .k_act = "address",
319 .k_nextparser = &parse_preference.pi_parser}
320 , {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
321 , {.k_word = "trailers", .k_neg = true,
322 .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
323 , {.k_word = "tunnel", .k_nextparser = &tunsrc.pa_parser}
324 , {.k_word = "vlan", .k_nextparser = &vlan.pi_parser}
325 , {.k_word = "vlanif", .k_act = "vlan",
326 .k_nextparser = &vlanif.pif_parser}
327 , {.k_word = "-vlanif", .k_key = "vlanif", .k_type = KW_T_STR,
328 .k_str = "", .k_exec = setvlanif}
329 #ifndef INET_ONLY
330 , {.k_word = "phase", .k_nextparser = &phase.pi_parser}
331 , {.k_word = "range", .k_nextparser = &parse_range.ps_parser}
332 , {.k_word = "nsellength", .k_nextparser = &parse_nsellength.pi_parser}
333 , {.k_word = "snpaoffset", .k_nextparser = &parse_snpaoffset.pi_parser}
334 /* CARP */
335 , {.k_word = "advbase", .k_nextparser = &parse_advbase.pi_parser}
336 , {.k_word = "advskew", .k_nextparser = &parse_advskew.pi_parser}
337 , {.k_word = "carpdev", .k_nextparser = &carpdev.pif_parser}
338 , {.k_word = "-carpdev", .k_key = "carpdev", .k_type = KW_T_STR,
339 .k_str = "", .k_exec = setcarpdev,
340 .k_nextparser = &command_root.pb_parser}
341 , {.k_word = "pass", .k_nextparser = &pass.ps_parser}
342 , {.k_word = "state", .k_nextparser = &carpstate.pk_parser}
343 , {.k_word = "vhid", .k_nextparser = &parse_vhid.pi_parser}
344 #endif
345 #ifdef INET6
346 , {.k_word = "eui64", .k_exec = setia6eui64,
347 .k_nextparser = &command_root.pb_parser}
348 #endif /*INET6*/
349 };
350
351 /* key: clonecmd */
352 struct kwinst clonekw[] = {
353 {.k_word = "create", .k_type = KW_T_NUM, .k_num = SIOCIFCREATE,
354 .k_nextparser = &opt_silent_family.pb_parser},
355 {.k_word = "destroy", .k_type = KW_T_NUM, .k_num = SIOCIFDESTROY}
356 };
357
358 struct kwinst familykw[] = {
359 {.k_word = "inet", .k_type = KW_T_NUM, .k_num = AF_INET,
360 .k_nextparser = NULL}
361 #ifdef INET6
362 , {.k_word = "inet6", .k_type = KW_T_NUM, .k_num = AF_INET6,
363 .k_nextparser = NULL}
364 #endif
365 #ifndef INET_ONLY /* small version, for boot media */
366 , {.k_word = "atalk", .k_type = KW_T_NUM, .k_num = AF_APPLETALK,
367 .k_nextparser = NULL}
368 , {.k_word = "iso", .k_type = KW_T_NUM, .k_num = AF_ISO,
369 .k_nextparser = NULL}
370 #endif /* INET_ONLY */
371 };
372
373 struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
374 list_cloners, "none");
375
376 struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
377 "none");
378
379 struct pkw family_only =
380 PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
381 __arraycount(familykw), &no_cmds.pt_parser);
382
383 struct paddr address = PADDR_INITIALIZER(&address,
384 "local address (address 1)",
385 setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
386
387 struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
388 "destination/netmask (address 2)",
389 setifdstormask, "dstormask", NULL, "address", "dstormask",
390 &command_root.pb_parser);
391
392 struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
393 "broadcast address (address 3)",
394 setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
395 &command_root.pb_parser);
396
397 struct branch opt_clone_brs[] = {
398 {.b_nextparser = &cloning.pk_parser}
399 , {.b_nextparser = &opt_family.pb_parser}
400 }, opt_silent_family_brs[] = {
401 {.b_nextparser = &silent_family.pk_parser}
402 , {.b_nextparser = &command_root.pb_parser}
403 }, opt_family_brs[] = {
404 {.b_nextparser = &family.pk_parser}
405 , {.b_nextparser = &opt_command.pb_parser}
406 }, command_root_brs[] = {
407 {.b_nextparser = &ieee80211bool.pk_parser}
408 , {.b_nextparser = &ifflags.pk_parser}
409 , {.b_nextparser = &ifcaps.pk_parser}
410 #ifdef INET6
411 , {.b_nextparser = &ia6flags.pk_parser}
412 , {.b_nextparser = &ia6lifetime.pk_parser}
413 #endif /*INET6*/
414 , {.b_nextparser = &misc.pk_parser}
415 , {.b_nextparser = &agr.pk_parser}
416 , {.b_nextparser = &kw80211.pk_parser}
417 , {.b_nextparser = &address.pa_parser}
418 , {.b_nextparser = &dstormask.pa_parser}
419 , {.b_nextparser = &broadcast.pa_parser}
420 , {.b_nextparser = NULL}
421 }, opt_command_brs[] = {
422 {.b_nextparser = &no_cmds.pt_parser}
423 , {.b_nextparser = &command_root.pb_parser}
424 };
425
426 struct branch opt_family_only_brs[] = {
427 {.b_nextparser = &no_cmds.pt_parser}
428 , {.b_nextparser = &family_only.pk_parser}
429 };
430 struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
431 "opt-family-only", opt_family_only_brs,
432 __arraycount(opt_family_only_brs), true);
433 struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
434 "optional command",
435 opt_command_brs, __arraycount(opt_command_brs), true);
436
437 struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
438 "command-root", command_root_brs, __arraycount(command_root_brs), true);
439
440 struct piface iface_opt_family_only =
441 PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
442 NULL, "if", &opt_family_only.pb_parser);
443
444 struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
445 familykw, __arraycount(familykw), &opt_command.pb_parser);
446
447 struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
448 NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
449
450 #ifdef INET6
451 struct kwinst ia6flagskw[] = {
452 IFKW("anycast", IN6_IFF_ANYCAST)
453 , IFKW("tentative", IN6_IFF_TENTATIVE)
454 , IFKW("deprecated", IN6_IFF_DEPRECATED)
455 };
456
457 struct pinteger pltime = PINTEGER_INITIALIZER(&pltime, "pltime", 0,
458 setia6pltime, "pltime", &command_root.pb_parser);
459
460 struct pinteger vltime = PINTEGER_INITIALIZER(&vltime, "vltime", 0,
461 setia6vltime, "vltime", &command_root.pb_parser);
462
463 struct kwinst ia6lifetimekw[] = {
464 {.k_word = "pltime", .k_nextparser = &pltime.pi_parser}
465 , {.k_word = "vltime", .k_nextparser = &vltime.pi_parser}
466 };
467
468 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", setia6flags,
469 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
470 struct pkw ia6lifetime = PKW_INITIALIZER(&ia6lifetime, "ia6lifetime", NULL,
471 NULL, ia6lifetimekw, __arraycount(ia6lifetimekw), NULL);
472 #endif /*INET6*/
473
474 struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
475 "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
476
477 struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
478 "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
479
480 struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
481 "clonecmd", clonekw, __arraycount(clonekw), NULL);
482
483 struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
484 misckw, __arraycount(misckw), NULL);
485
486 struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
487 "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
488
489 struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
490 "optional silent family", opt_silent_family_brs,
491 __arraycount(opt_silent_family_brs), true);
492
493 struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
494 "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
495
496 struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
497 "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
498
499 struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
500 media_status_exec, "if", NULL);
501
502 static struct parser *
503 init_parser(void)
504 {
505 if (parser_init(&iface_opt_family_only.pif_parser) == -1)
506 err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
507 if (parser_init(&iface_only.pif_parser) == -1)
508 err(EXIT_FAILURE, "parser_init(iface_only)");
509 if (parser_init(&iface_start.pif_parser) == -1)
510 err(EXIT_FAILURE, "parser_init(iface_start)");
511
512 return &iface_start.pif_parser;
513 }
514
515 static int
516 no_cmds_exec(prop_dictionary_t env, prop_dictionary_t xenv)
517 {
518 const char *ifname;
519 unsigned short ignore;
520
521 /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
522 if ((ifname = getifname(env)) == NULL)
523 ;
524 else if (getifflags(env, xenv, &ignore) == -1)
525 err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
526
527 printall(ifname, env);
528 exit(EXIT_SUCCESS);
529 }
530
531 static int
532 media_status_exec(prop_dictionary_t env, prop_dictionary_t xenv)
533 {
534 const char *ifname;
535 unsigned short ignore;
536
537 /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
538 if ((ifname = getifname(env)) == NULL)
539 ;
540 else if (getifflags(env, xenv, &ignore) == -1)
541 err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
542
543 exit(carrier(env));
544 }
545
546 static void
547 do_setifcaps(int s, const char *ifname, prop_dictionary_t env)
548 {
549 struct ifcapreq ifcr;
550 prop_data_t d;
551
552 d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
553 if (d == NULL)
554 return;
555
556 assert(sizeof(ifcr) == prop_data_size(d));
557
558 memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
559 estrlcpy(ifcr.ifcr_name, ifname, sizeof(ifcr.ifcr_name));
560 if (ioctl(s, SIOCSIFCAP, &ifcr) == -1)
561 err(EXIT_FAILURE, "SIOCSIFCAP");
562 }
563
564 int
565 main(int argc, char **argv)
566 {
567 const struct afswtch *afp;
568 int af, aflag = 0, Cflag = 0, s;
569 struct match match[32];
570 size_t nmatch;
571 struct parser *start;
572 int ch, narg = 0, rc;
573 prop_dictionary_t env, xenv;
574 const char *ifname;
575
576 memset(match, 0, sizeof(match));
577
578 start = init_parser();
579
580 /* Parse command-line options */
581 aflag = mflag = vflag = zflag = 0;
582 while ((ch = getopt(argc, argv, "AabCdhlmsuvz"
583 #ifdef INET6
584 "L"
585 #endif
586 )) != -1) {
587 switch (ch) {
588 case 'A':
589 warnx("-A is deprecated");
590 break;
591
592 case 'a':
593 aflag = 1;
594 break;
595
596 case 'b':
597 bflag = 1;
598 break;
599
600 case 'C':
601 Cflag = 1;
602 break;
603
604 case 'd':
605 dflag = 1;
606 break;
607 case 'h':
608 hflag = 1;
609 break;
610 #ifdef INET6
611 case 'L':
612 Lflag = 1;
613 break;
614 #endif
615
616 case 'l':
617 lflag = 1;
618 break;
619
620 case 'm':
621 mflag = 1;
622 break;
623
624 case 's':
625 sflag = 1;
626 break;
627
628 case 'u':
629 uflag = 1;
630 break;
631
632 case 'v':
633 vflag = 1;
634 break;
635
636 case 'z':
637 zflag = 1;
638 break;
639
640
641 default:
642 usage();
643 /* NOTREACHED */
644 }
645 switch (ch) {
646 case 'a':
647 start = &opt_family_only.pb_parser;
648 break;
649
650 #ifdef INET6
651 case 'L':
652 #endif
653 case 'm':
654 case 'v':
655 case 'z':
656 if (start != &opt_family_only.pb_parser)
657 start = &iface_opt_family_only.pif_parser;
658 break;
659 case 'C':
660 start = &cloneterm.pt_parser;
661 break;
662 case 'l':
663 start = &no_cmds.pt_parser;
664 break;
665 case 's':
666 if (start != &no_cmds.pt_parser &&
667 start != &opt_family_only.pb_parser)
668 start = &iface_only.pif_parser;
669 break;
670 default:
671 break;
672 }
673 }
674 argc -= optind;
675 argv += optind;
676
677 /*
678 * -l means "list all interfaces", and is mutally exclusive with
679 * all other flags/commands.
680 *
681 * -C means "list all names of cloners", and it mutually exclusive
682 * with all other flags/commands.
683 *
684 * -a means "print status of all interfaces".
685 */
686 if ((lflag || Cflag) && (aflag || mflag || vflag || zflag))
687 usage();
688 #ifdef INET6
689 if ((lflag || Cflag) && Lflag)
690 usage();
691 #endif
692 if (lflag && Cflag)
693 usage();
694
695 nmatch = __arraycount(match);
696
697 rc = parse(argc, argv, start, match, &nmatch, &narg);
698 if (rc != 0)
699 usage();
700
701 if ((xenv = prop_dictionary_create()) == NULL)
702 err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
703
704 if (matches_exec(match, xenv, nmatch) == -1)
705 err(EXIT_FAILURE, "exec_matches");
706
707 argc -= narg;
708 argv += narg;
709
710 env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
711 if (env == NULL)
712 env = xenv;
713 else
714 env = prop_dictionary_augment(env, xenv);
715
716 /* Process any media commands that may have been issued. */
717 process_media_commands(env);
718
719 af = getaf(env);
720 switch (af) {
721 #ifndef INET_ONLY
722 case AF_APPLETALK:
723 checkatrange(&at_addreq.ifra_addr);
724 break;
725 #endif /* INET_ONLY */
726 default:
727 break;
728 case -1:
729 af = AF_INET;
730 break;
731 }
732
733 if ((s = getsock(af)) == -1)
734 err(EXIT_FAILURE, "%s: getsock", __func__);
735
736 if ((ifname = getifname(env)) == NULL)
737 err(EXIT_FAILURE, "%s: getifname", __func__);
738
739 if ((afp = lookup_af_bynum(af)) == NULL)
740 errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
741
742 if (afp->af_addr_commit != NULL) {
743 (*afp->af_addr_commit)(env, xenv);
744 } else {
745 if (clearaddr) {
746 estrlcpy(afp->af_ridreq, ifname, IFNAMSIZ);
747 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
748 err(EXIT_FAILURE, "SIOCDIFADDR");
749 }
750 if (newaddr > 0) {
751 estrlcpy(afp->af_addreq, ifname, IFNAMSIZ);
752 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
753 warn("SIOCAIFADDR");
754 else if (check_up_state < 0)
755 check_up_state = 1;
756 }
757 }
758
759 do_setifpreference(env);
760 do_setifcaps(s, ifname, env);
761
762 if (check_up_state == 1)
763 check_ifflags_up(ifname);
764
765 exit(EXIT_SUCCESS);
766 }
767
768 const struct afswtch *
769 lookup_af_bynum(int afnum)
770 {
771 const struct afswtch *a;
772
773 for (a = afs; a->af_name != NULL; a++)
774 if (a->af_af == afnum)
775 return (a);
776 return (NULL);
777 }
778
779 void
780 printall(const char *ifname, prop_dictionary_t env0)
781 {
782 struct ifaddrs *ifap, *ifa;
783 struct ifreq ifr;
784 const struct sockaddr_dl *sdl = NULL;
785 prop_dictionary_t env, oenv;
786 int idx;
787 char *p;
788
789 if (env0 == NULL)
790 env = prop_dictionary_create();
791 else
792 env = prop_dictionary_copy_mutable(env0);
793
794 oenv = prop_dictionary_create();
795
796 if (env == NULL || oenv == NULL)
797 errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
798
799 if (getifaddrs(&ifap) != 0)
800 err(EXIT_FAILURE, "getifaddrs");
801 p = NULL;
802 idx = 0;
803 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
804 memset(&ifr, 0, sizeof(ifr));
805 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
806 if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
807 memcpy(&ifr.ifr_addr, ifa->ifa_addr,
808 ifa->ifa_addr->sa_len);
809 }
810
811 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
812 continue;
813 if (ifa->ifa_addr->sa_family == AF_LINK)
814 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
815 if (p && strcmp(p, ifa->ifa_name) == 0)
816 continue;
817 if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
818 continue;
819 p = ifa->ifa_name;
820
821 if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
822 continue;
823 if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
824 continue;
825 if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
826 continue;
827
828 if (sflag && carrier(env))
829 continue;
830 idx++;
831 /*
832 * Are we just listing the interfaces?
833 */
834 if (lflag) {
835 if (idx > 1)
836 printf(" ");
837 fputs(ifa->ifa_name, stdout);
838 continue;
839 }
840
841 status(sdl, env, oenv);
842 sdl = NULL;
843 }
844 if (lflag)
845 printf("\n");
846 prop_object_release((prop_object_t)env);
847 prop_object_release((prop_object_t)oenv);
848 freeifaddrs(ifap);
849 }
850
851 int
852 list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
853 {
854 struct if_clonereq ifcr;
855 char *cp, *buf;
856 int idx, s;
857
858 memset(&ifcr, 0, sizeof(ifcr));
859
860 s = getsock(AF_INET);
861
862 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
863 err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
864
865 buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
866 if (buf == NULL)
867 err(EXIT_FAILURE, "unable to allocate cloner name buffer");
868
869 ifcr.ifcr_count = ifcr.ifcr_total;
870 ifcr.ifcr_buffer = buf;
871
872 if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
873 err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
874
875 /*
876 * In case some disappeared in the mean time, clamp it down.
877 */
878 if (ifcr.ifcr_count > ifcr.ifcr_total)
879 ifcr.ifcr_count = ifcr.ifcr_total;
880
881 for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
882 if (idx > 0)
883 printf(" ");
884 printf("%s", cp);
885 }
886
887 printf("\n");
888 free(buf);
889 exit(EXIT_SUCCESS);
890 }
891
892 static int
893 clone_command(prop_dictionary_t env, prop_dictionary_t xenv)
894 {
895 struct ifreq ifreq;
896 int s;
897 int64_t cmd;
898 const char *ifname;
899
900 if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
901 errno = ENOENT;
902 return -1;
903 }
904
905 if ((ifname = getifname(env)) == NULL)
906 return -1;
907
908 /* We're called early... */
909 s = getsock(AF_INET);
910
911 estrlcpy(ifreq.ifr_name, ifname, sizeof(ifreq.ifr_name));
912 if (ioctl(s, (unsigned long)cmd, &ifreq) == -1) {
913 warn("%s", __func__);
914 return -1;
915 }
916 return 0;
917 }
918
919 /*ARGSUSED*/
920 int
921 setifaddr(prop_dictionary_t env, prop_dictionary_t xenv)
922 {
923 struct ifreq *ifr; /* XXX */
924 const struct paddr_prefix *pfx0;
925 struct paddr_prefix *pfx;
926 prop_data_t d;
927 const char *ifname;
928 int af, s;
929 const struct afswtch *afp;
930
931 if ((af = getaf(env)) == -1)
932 af = AF_INET;
933
934 if ((afp = lookup_af_bynum(af)) == NULL)
935 return -1;
936
937 d = (prop_data_t)prop_dictionary_get(env, "address");
938 assert(d != NULL);
939 pfx0 = prop_data_data_nocopy(d);
940
941 if (pfx0->pfx_len >= 0) {
942 pfx = prefixlen_to_mask(af, pfx0->pfx_len);
943 if (pfx == NULL)
944 err(EXIT_FAILURE, "prefixlen_to_mask");
945
946 if (afp->af_getaddr != NULL)
947 (*afp->af_getaddr)(pfx, MASK);
948 free(pfx);
949 }
950
951 if (afp->af_addr_commit != NULL)
952 return 0;
953
954 if ((ifname = getifname(env)) == NULL)
955 return -1;
956
957 /*
958 * Delay the ioctl to set the interface addr until flags are all set.
959 * The address interpretation may depend on the flags,
960 * and the flags may change when the address is set.
961 */
962 setaddr++;
963 if (newaddr == -1)
964 newaddr = 1;
965 if (doalias == 0 && afp->af_gifaddr != 0) {
966 ifr = (struct ifreq *)afp->af_ridreq;
967 estrlcpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
968 ifr->ifr_addr.sa_family = af;
969
970 if ((s = getsock(af)) == -1)
971 err(EXIT_FAILURE, "getsock");
972
973 if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
974 clearaddr = 1;
975 else if (errno == EADDRNOTAVAIL)
976 /* No address was assigned yet. */
977 ;
978 else
979 err(EXIT_FAILURE, "SIOCGIFADDR");
980 }
981
982 #if 0
983 int i;
984 for (i = 0; i < pfx0->pfx_addr.sa_len; i++)
985 printf(" %02x", ((const uint8_t *)&pfx->pfx_addr)[i]);
986 printf("\n");
987 #endif
988 if (afp->af_getaddr != NULL)
989 (*afp->af_getaddr)(pfx0, (doalias >= 0 ? ADDR : RIDADDR));
990 return 0;
991 }
992
993 int
994 setifnetmask(prop_dictionary_t env, prop_dictionary_t xenv)
995 {
996 const struct paddr_prefix *pfx;
997 prop_data_t d;
998 int af;
999 const struct afswtch *afp;
1000
1001 if ((af = getaf(env)) == -1)
1002 af = AF_INET;
1003
1004 if ((afp = lookup_af_bynum(af)) == NULL)
1005 return -1;
1006
1007 d = (prop_data_t)prop_dictionary_get(env, "dstormask");
1008 assert(d != NULL);
1009 pfx = prop_data_data_nocopy(d);
1010
1011 if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
1012 return -1;
1013
1014 if (afp->af_getaddr != NULL)
1015 (*afp->af_getaddr)(pfx, MASK);
1016 return 0;
1017 }
1018
1019 int
1020 setifbroadaddr(prop_dictionary_t env, prop_dictionary_t xenv)
1021 {
1022 const struct paddr_prefix *pfx;
1023 prop_data_t d;
1024 int af;
1025 const struct afswtch *afp;
1026 unsigned short flags;
1027
1028 if ((af = getaf(env)) == -1)
1029 af = AF_INET;
1030
1031 if ((afp = lookup_af_bynum(af)) == NULL)
1032 return -1;
1033
1034 if (getifflags(env, xenv, &flags) == -1)
1035 err(EXIT_FAILURE, "%s: getifflags", __func__);
1036
1037 if ((flags & IFF_BROADCAST) == 0)
1038 errx(EXIT_FAILURE, "not a broadcast interface");
1039
1040 d = (prop_data_t)prop_dictionary_get(env, "broadcast");
1041 assert(d != NULL);
1042 pfx = prop_data_data_nocopy(d);
1043
1044 if (!prop_dictionary_set(xenv, "broadcast", (prop_object_t)d))
1045 return -1;
1046
1047 if (afp->af_getaddr != NULL)
1048 (*afp->af_getaddr)(pfx, DSTADDR);
1049
1050 return 0;
1051 }
1052
1053 #define rqtosa(__afp, __x) (&(((struct ifreq *)(__afp->__x))->ifr_addr))
1054
1055 int
1056 notealias(prop_dictionary_t env, prop_dictionary_t xenv)
1057 {
1058 bool alias, delete;
1059 int af;
1060 const struct afswtch *afp;
1061
1062 if ((af = getaf(env)) == -1)
1063 af = AF_INET;
1064
1065 if ((afp = lookup_af_bynum(af)) == NULL)
1066 return -1;
1067
1068 if (afp->af_addr_commit != NULL)
1069 return 0;
1070
1071 if (!prop_dictionary_get_bool(env, "alias", &alias)) {
1072 errno = ENOENT;
1073 return -1;
1074 }
1075 delete = !alias;
1076 if (setaddr && doalias == 0 && delete)
1077 memcpy(rqtosa(afp, af_ridreq), rqtosa(afp, af_addreq),
1078 rqtosa(afp, af_addreq)->sa_len);
1079 doalias = delete ? -1 : 1;
1080 if (delete) {
1081 clearaddr = 1;
1082 newaddr = 0;
1083 } else {
1084 clearaddr = 0;
1085 }
1086 return 0;
1087 }
1088
1089 /*ARGSUSED*/
1090 int
1091 notrailers(prop_dictionary_t env, prop_dictionary_t xenv)
1092 {
1093 puts("Note: trailers are no longer sent, but always received");
1094 return 0;
1095 }
1096
1097 /*ARGSUSED*/
1098 int
1099 setifdstormask(prop_dictionary_t env, prop_dictionary_t xenv)
1100 {
1101 const char *key;
1102 const struct paddr_prefix *pfx;
1103 prop_data_t d;
1104 int af, which;
1105 const struct afswtch *afp;
1106 unsigned short flags;
1107
1108 if ((af = getaf(env)) == -1)
1109 af = AF_INET;
1110
1111 if ((afp = lookup_af_bynum(af)) == NULL)
1112 return -1;
1113
1114 if (getifflags(env, xenv, &flags) == -1)
1115 err(EXIT_FAILURE, "%s: getifflags", __func__);
1116
1117 d = (prop_data_t)prop_dictionary_get(env, "dstormask");
1118 assert(d != NULL);
1119 pfx = prop_data_data_nocopy(d);
1120
1121 if ((flags & IFF_BROADCAST) == 0) {
1122 key = "dst";
1123 which = DSTADDR;
1124 } else {
1125 key = "netmask";
1126 which = MASK;
1127 }
1128
1129 if (!prop_dictionary_set(xenv, key, (prop_object_t)d))
1130 return -1;
1131
1132 if (afp->af_getaddr != NULL)
1133 (*afp->af_getaddr)(pfx, which);
1134 return 0;
1135 }
1136
1137 void
1138 check_ifflags_up(const char *ifname)
1139 {
1140 struct ifreq ifreq;
1141 int s;
1142
1143 s = getsock(AF_UNSPEC);
1144
1145 estrlcpy(ifreq.ifr_name, ifname, sizeof(ifreq.ifr_name));
1146 if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
1147 err(EXIT_FAILURE, "SIOCGIFFLAGS");
1148 if (ifreq.ifr_flags & IFF_UP)
1149 return;
1150 ifreq.ifr_flags |= IFF_UP;
1151 if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
1152 err(EXIT_FAILURE, "SIOCSIFFLAGS");
1153 }
1154
1155 int
1156 setifflags(prop_dictionary_t env, prop_dictionary_t xenv)
1157 {
1158 struct ifreq ifreq;
1159 int64_t ifflag;
1160 int s;
1161 bool rc;
1162 const char *ifname;
1163
1164 s = getsock(AF_INET);
1165
1166 rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
1167 assert(rc);
1168 if ((ifname = getifname(env)) == NULL)
1169 return -1;
1170
1171 estrlcpy(ifreq.ifr_name, ifname, sizeof(ifreq.ifr_name));
1172 if (ioctl(s, SIOCGIFFLAGS, &ifreq) == -1)
1173 return -1;
1174
1175 if (ifflag < 0) {
1176 ifflag = -ifflag;
1177 if (ifflag == IFF_UP)
1178 check_up_state = 0;
1179 ifreq.ifr_flags &= ~ifflag;
1180 } else
1181 ifreq.ifr_flags |= ifflag;
1182
1183 if (ioctl(s, SIOCSIFFLAGS, &ifreq) == -1)
1184 return -1;
1185
1186 return 0;
1187 }
1188
1189 static int
1190 getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
1191 {
1192 const char *ifname;
1193 bool rc;
1194 int s;
1195 struct ifcapreq ifcr;
1196 const struct ifcapreq *tmpifcr;
1197 prop_data_t capdata;
1198
1199 if ((s = getsock(AF_UNSPEC)) == -1)
1200 return -1;
1201
1202 if ((ifname = getifname(env)) == NULL)
1203 return -1;
1204
1205 capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
1206
1207 if (capdata != NULL) {
1208 tmpifcr = prop_data_data_nocopy(capdata);
1209 *oifcr = *tmpifcr;
1210 return 0;
1211 }
1212
1213 estrlcpy(ifcr.ifcr_name, ifname, sizeof(ifcr.ifcr_name));
1214 (void)ioctl(s, SIOCGIFCAP, &ifcr);
1215 *oifcr = ifcr;
1216
1217 capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
1218
1219 rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1220
1221 prop_object_release((prop_object_t)capdata);
1222
1223 return rc ? 0 : -1;
1224 }
1225
1226 static int
1227 setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
1228 {
1229 int64_t ifcap;
1230 int s;
1231 bool rc;
1232 prop_data_t capdata;
1233 const char *ifname;
1234 struct ifcapreq ifcr;
1235
1236 s = getsock(AF_INET);
1237
1238 rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
1239 assert(rc);
1240
1241 if ((ifname = getifname(env)) == NULL)
1242 return -1;
1243
1244 if (getifcaps(env, oenv, &ifcr) == -1)
1245 return -1;
1246
1247 if (ifcap < 0) {
1248 ifcap = -ifcap;
1249 ifcr.ifcr_capenable &= ~ifcap;
1250 } else
1251 ifcr.ifcr_capenable |= ifcap;
1252
1253 if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
1254 return -1;
1255
1256 rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1257 prop_object_release((prop_object_t)capdata);
1258
1259 return rc ? 0 : -1;
1260 }
1261
1262 static int
1263 setifmetric(prop_dictionary_t env, prop_dictionary_t xenv)
1264 {
1265 struct ifreq ifr;
1266 bool rc;
1267 const char *ifname;
1268 int s;
1269 int64_t metric;
1270
1271 if ((s = getsock(AF_UNSPEC)) == -1)
1272 return -1;
1273
1274 if ((ifname = getifname(env)) == NULL)
1275 return -1;
1276
1277 rc = prop_dictionary_get_int64(env, "metric", &metric);
1278 assert(rc);
1279
1280 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1281 ifr.ifr_metric = metric;
1282 if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
1283 warn("SIOCSIFMETRIC");
1284 return 0;
1285 }
1286
1287 static void
1288 do_setifpreference(prop_dictionary_t env)
1289 {
1290 struct if_addrprefreq ifap;
1291 int af, s;
1292 const char *ifname;
1293 const struct afswtch *afp;
1294 prop_data_t d;
1295 const struct paddr_prefix *pfx;
1296
1297 if ((af = getaf(env)) == -1)
1298 af = AF_INET;
1299
1300 if ((afp = lookup_af_bynum(af)) == NULL)
1301 errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
1302
1303 memset(&ifap, 0, sizeof(ifap));
1304
1305 if (!prop_dictionary_get_int16(env, "preference",
1306 &ifap.ifap_preference))
1307 return;
1308
1309 d = (prop_data_t)prop_dictionary_get(env, "address");
1310 assert(d != NULL);
1311
1312 pfx = prop_data_data_nocopy(d);
1313
1314 s = getsock(AF_UNSPEC);
1315
1316 if ((ifname = getifname(env)) == NULL)
1317 err(EXIT_FAILURE, "%s: getifname", __func__);
1318
1319 strlcpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
1320 memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
1321 MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
1322 if (ioctl(s, SIOCSIFADDRPREF, &ifap) == -1)
1323 warn("SIOCSIFADDRPREF");
1324 }
1325
1326 static int
1327 setifmtu(prop_dictionary_t env, prop_dictionary_t xenv)
1328 {
1329 int64_t mtu;
1330 bool rc;
1331 const char *ifname;
1332 struct ifreq ifr;
1333 int s;
1334
1335 if ((s = getsock(AF_UNSPEC)) == -1)
1336 return -1;
1337
1338 if ((ifname = getifname(env)) == NULL)
1339 return -1;
1340
1341 rc = prop_dictionary_get_int64(env, "mtu", &mtu);
1342 assert(rc);
1343
1344 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1345 ifr.ifr_mtu = mtu;
1346 if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
1347 warn("SIOCSIFMTU");
1348
1349 return 0;
1350 }
1351
1352 static void
1353 media_error(int type, const char *val, const char *opt)
1354 {
1355 errx(EXIT_FAILURE, "unknown %s media %s: %s",
1356 get_media_type_string(type), opt, val);
1357 }
1358
1359 void
1360 init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
1361 {
1362 struct ifmediareq ifmr;
1363 int s;
1364 const char *ifname;
1365
1366 if ((s = getsock(AF_UNSPEC)) == -1)
1367 err(EXIT_FAILURE, "%s: getsock", __func__);
1368
1369 if ((ifname = getifname(env)) == NULL)
1370 err(EXIT_FAILURE, "%s: getifname", __func__);
1371
1372 /*
1373 * If we have not yet done so, grab the currently-selected
1374 * media.
1375 */
1376
1377 if (prop_dictionary_get(env, "initmedia") == NULL) {
1378 memset(&ifmr, 0, sizeof(ifmr));
1379 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1380
1381 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1382 /*
1383 * If we get E2BIG, the kernel is telling us
1384 * that there are more, so we can ignore it.
1385 */
1386 if (errno != E2BIG)
1387 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1388 }
1389
1390 if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
1391 err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
1392 __func__);
1393 }
1394 media_current = ifmr.ifm_current;
1395 }
1396
1397 /* Sanity. */
1398 if (IFM_TYPE(media_current) == 0)
1399 errx(EXIT_FAILURE, "%s: no link type?", ifname);
1400 }
1401
1402 void
1403 process_media_commands(prop_dictionary_t env)
1404 {
1405 struct ifreq ifr;
1406 const char *ifname;
1407 int s;
1408
1409 if ((s = getsock(AF_UNSPEC)) == -1)
1410 err(EXIT_FAILURE, "%s: getsock", __func__);
1411
1412 if (prop_dictionary_get(env, "media") == NULL &&
1413 prop_dictionary_get(env, "mediaopt") == NULL &&
1414 prop_dictionary_get(env, "unmediaopt") == NULL &&
1415 prop_dictionary_get(env, "mediamode") == NULL) {
1416 /* Nothing to do. */
1417 return;
1418 }
1419
1420 if ((ifname = getifname(env)) == NULL)
1421 err(EXIT_FAILURE, "%s: getifname", __func__);
1422
1423 /*
1424 * Media already set up, and commands sanity-checked. Set/clear
1425 * any options, and we're ready to go.
1426 */
1427 media_current |= mediaopt_set;
1428 media_current &= ~mediaopt_clear;
1429
1430 memset(&ifr, 0, sizeof(ifr));
1431 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1432 ifr.ifr_media = media_current;
1433
1434 if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
1435 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1436 }
1437
1438 int
1439 setmedia(prop_dictionary_t env, prop_dictionary_t xenv)
1440 {
1441 int type, subtype, inst;
1442 prop_data_t data;
1443 char *val;
1444
1445 init_current_media(env, xenv);
1446
1447 data = (prop_data_t)prop_dictionary_get(env, "media");
1448 assert(data != NULL);
1449
1450 /* Only one media command may be given. */
1451 /* Must not come after mode commands */
1452 /* Must not come after mediaopt commands */
1453
1454 /*
1455 * No need to check if `instance' has been issued; setmediainst()
1456 * craps out if `media' has not been specified.
1457 */
1458
1459 type = IFM_TYPE(media_current);
1460 inst = IFM_INST(media_current);
1461
1462 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1463 if (val == NULL)
1464 return -1;
1465
1466 /* Look up the subtype. */
1467 subtype = get_media_subtype(type, val);
1468 if (subtype == -1)
1469 media_error(type, val, "subtype");
1470
1471 /* Build the new current media word. */
1472 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1473
1474 /* Media will be set after other processing is complete. */
1475 return 0;
1476 }
1477
1478 int
1479 setmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1480 {
1481 char *invalid;
1482 prop_data_t data;
1483 char *val;
1484
1485 init_current_media(env, xenv);
1486
1487 data = (prop_data_t)prop_dictionary_get(env, "mediaopt");
1488 assert(data != NULL);
1489
1490 /* Can only issue `mediaopt' once. */
1491 /* Can't issue `mediaopt' if `instance' has already been issued. */
1492
1493 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1494 if (val == NULL)
1495 return -1;
1496
1497 mediaopt_set = get_media_options(media_current, val, &invalid);
1498 free(val);
1499 if (mediaopt_set == -1)
1500 media_error(media_current, invalid, "option");
1501
1502 /* Media will be set after other processing is complete. */
1503 return 0;
1504 }
1505
1506 int
1507 unsetmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1508 {
1509 char *invalid, *val;
1510 prop_data_t data;
1511
1512 init_current_media(env, xenv);
1513
1514 data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
1515 if (data == NULL) {
1516 errno = ENOENT;
1517 return -1;
1518 }
1519
1520 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1521 if (val == NULL)
1522 return -1;
1523
1524 /*
1525 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1526 * implicitly checks for A_MEDIAINST.
1527 */
1528
1529 mediaopt_clear = get_media_options(media_current, val, &invalid);
1530 free(val);
1531 if (mediaopt_clear == -1)
1532 media_error(media_current, invalid, "option");
1533
1534 /* Media will be set after other processing is complete. */
1535 return 0;
1536 }
1537
1538 int
1539 setmediainst(prop_dictionary_t env, prop_dictionary_t xenv)
1540 {
1541 int type, subtype, options;
1542 int64_t inst;
1543 bool rc;
1544
1545 init_current_media(env, xenv);
1546
1547 rc = prop_dictionary_get_int64(env, "mediainst", &inst);
1548 assert(rc);
1549
1550 /* Can only issue `instance' once. */
1551 /* Must have already specified `media' */
1552
1553 type = IFM_TYPE(media_current);
1554 subtype = IFM_SUBTYPE(media_current);
1555 options = IFM_OPTIONS(media_current);
1556
1557 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1558
1559 /* Media will be set after other processing is complete. */
1560 return 0;
1561 }
1562
1563 int
1564 setmediamode(prop_dictionary_t env, prop_dictionary_t xenv)
1565 {
1566 int type, subtype, options, inst, mode;
1567 prop_data_t data;
1568 char *val;
1569
1570 init_current_media(env, xenv);
1571
1572 data = (prop_data_t)prop_dictionary_get(env, "mediamode");
1573 assert(data != NULL);
1574
1575 type = IFM_TYPE(media_current);
1576 subtype = IFM_SUBTYPE(media_current);
1577 options = IFM_OPTIONS(media_current);
1578 inst = IFM_INST(media_current);
1579
1580 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1581 if (val == NULL)
1582 return -1;
1583
1584 mode = get_media_mode(type, val);
1585 if (mode == -1)
1586 media_error(type, val, "mode");
1587
1588 free(val);
1589
1590 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1591
1592 /* Media will be set after other processing is complete. */
1593 return 0;
1594 }
1595
1596 void
1597 print_media_word(int ifmw, const char *opt_sep)
1598 {
1599 const char *str;
1600
1601 printf("%s", get_media_subtype_string(ifmw));
1602
1603 /* Find mode. */
1604 if (IFM_MODE(ifmw) != 0) {
1605 str = get_media_mode_string(ifmw);
1606 if (str != NULL)
1607 printf(" mode %s", str);
1608 }
1609
1610 /* Find options. */
1611 for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1612 printf("%s%s", opt_sep, str);
1613
1614 if (IFM_INST(ifmw) != 0)
1615 printf(" instance %d", IFM_INST(ifmw));
1616 }
1617
1618 int
1619 carrier(prop_dictionary_t env)
1620 {
1621 struct ifmediareq ifmr;
1622 int s;
1623 const char *ifname;
1624
1625 if ((s = getsock(AF_UNSPEC)) == -1)
1626 err(EXIT_FAILURE, "%s: getsock", __func__);
1627
1628 if ((ifname = getifname(env)) == NULL)
1629 err(EXIT_FAILURE, "%s: getifname", __func__);
1630
1631 (void) memset(&ifmr, 0, sizeof(ifmr));
1632 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1633
1634 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1635 /*
1636 * Interface doesn't support SIOC{G,S}IFMEDIA;
1637 * assume ok.
1638 */
1639 return EXIT_SUCCESS;
1640 }
1641 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1642 /*
1643 * Interface doesn't report media-valid status.
1644 * assume ok.
1645 */
1646 return EXIT_SUCCESS;
1647 }
1648 /* otherwise, return ok for active, not-ok if not active. */
1649 if (ifmr.ifm_status & IFM_ACTIVE)
1650 return EXIT_SUCCESS;
1651 else
1652 return EXIT_FAILURE;
1653 }
1654
1655 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1656
1657 const struct ifmedia_status_description ifm_status_descriptions[] =
1658 IFM_STATUS_DESCRIPTIONS;
1659
1660 /*
1661 * Print the status of the interface. If an address family was
1662 * specified, show it and it only; otherwise, show them all.
1663 */
1664 void
1665 status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
1666 prop_dictionary_t oenv)
1667 {
1668 struct ifmediareq ifmr;
1669 struct ifdatareq ifdr;
1670 struct ifreq ifr;
1671 int *media_list, i;
1672 char hbuf[NI_MAXHOST];
1673 char fbuf[BUFSIZ];
1674 int af, s;
1675 const char *ifname;
1676 struct ifcapreq ifcr;
1677 unsigned short flags;
1678 const struct afswtch *afp;
1679
1680 if ((af = getaf(env)) == -1) {
1681 afp = NULL;
1682 af = AF_UNSPEC;
1683 } else
1684 afp = lookup_af_bynum(af);
1685
1686 if ((s = getsock(af)) == -1)
1687 err(EXIT_FAILURE, "%s: getsock", __func__);
1688
1689 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
1690 err(EXIT_FAILURE, "%s: getifinfo", __func__);
1691
1692 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1693 printf("%s: flags=%s", ifname, &fbuf[2]);
1694
1695 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1696 if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
1697 warn("SIOCGIFMETRIC %s", ifr.ifr_name);
1698 else if (ifr.ifr_metric != 0)
1699 printf(" metric %d", ifr.ifr_metric);
1700
1701 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1702 if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
1703 printf(" mtu %d", ifr.ifr_mtu);
1704 printf("\n");
1705
1706 if (getifcaps(env, oenv, &ifcr) == -1)
1707 err(EXIT_FAILURE, "%s: getifcaps", __func__);
1708
1709 if (ifcr.ifcr_capabilities != 0) {
1710 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1711 ifcr.ifcr_capabilities);
1712 printf("\tcapabilities=%s\n", &fbuf[2]);
1713 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1714 ifcr.ifcr_capenable);
1715 printf("\tenabled=%s\n", &fbuf[2]);
1716 }
1717
1718 ieee80211_status(env, oenv);
1719 vlan_status(env, oenv);
1720 #ifndef INET_ONLY
1721 carp_status(env, oenv);
1722 #endif
1723 tunnel_status(env, oenv);
1724 agr_status(env, oenv);
1725
1726 if (sdl != NULL &&
1727 getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1728 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1729 hbuf[0] != '\0')
1730 printf("\taddress: %s\n", hbuf);
1731
1732 (void) memset(&ifmr, 0, sizeof(ifmr));
1733 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1734
1735 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1736 /*
1737 * Interface doesn't support SIOC{G,S}IFMEDIA.
1738 */
1739 goto iface_stats;
1740 }
1741
1742 if (ifmr.ifm_count == 0) {
1743 warnx("%s: no media types?", ifname);
1744 goto iface_stats;
1745 }
1746
1747 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1748 if (media_list == NULL)
1749 err(EXIT_FAILURE, "malloc");
1750 ifmr.ifm_ulist = media_list;
1751
1752 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1753 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1754
1755 printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1756 print_media_word(ifmr.ifm_current, " ");
1757 if (ifmr.ifm_active != ifmr.ifm_current) {
1758 printf(" (");
1759 print_media_word(ifmr.ifm_active, " ");
1760 printf(")");
1761 }
1762 printf("\n");
1763
1764 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1765 const struct ifmedia_status_description *ifms;
1766 int bitno, found = 0;
1767
1768 printf("\tstatus: ");
1769 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1770 for (ifms = ifm_status_descriptions;
1771 ifms->ifms_valid != 0; ifms++) {
1772 if (ifms->ifms_type !=
1773 IFM_TYPE(ifmr.ifm_current) ||
1774 ifms->ifms_valid !=
1775 ifm_status_valid_list[bitno])
1776 continue;
1777 printf("%s%s", found ? ", " : "",
1778 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1779 found = 1;
1780
1781 /*
1782 * For each valid indicator bit, there's
1783 * only one entry for each media type, so
1784 * terminate the inner loop now.
1785 */
1786 break;
1787 }
1788 }
1789
1790 if (found == 0)
1791 printf("unknown");
1792 printf("\n");
1793 }
1794
1795 if (mflag) {
1796 int type, printed_type;
1797
1798 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1799 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1800 if (IFM_TYPE(media_list[i]) != type)
1801 continue;
1802 if (printed_type == 0) {
1803 printf("\tsupported %s media:\n",
1804 get_media_type_string(type));
1805 printed_type = 1;
1806 }
1807 printf("\t\tmedia ");
1808 print_media_word(media_list[i], " mediaopt ");
1809 printf("\n");
1810 }
1811 }
1812 }
1813
1814 free(media_list);
1815
1816 iface_stats:
1817 if (!vflag && !zflag)
1818 goto proto_status;
1819
1820 estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
1821
1822 if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1823 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1824 } else {
1825 struct if_data * const ifi = &ifdr.ifdr_data;
1826 char buf[5];
1827
1828 #define PLURAL(n) ((n) == 1 ? "" : "s")
1829 #define PLURALSTR(s) ((atof(s)) == 1.0 ? "" : "s")
1830 printf("\tinput: %llu packet%s, ",
1831 (unsigned long long) ifi->ifi_ipackets,
1832 PLURAL(ifi->ifi_ipackets));
1833 if (hflag) {
1834 (void) humanize_number(buf, sizeof(buf),
1835 (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
1836 HN_NOSPACE | HN_DECIMAL);
1837 printf("%s byte%s", buf,
1838 PLURALSTR(buf));
1839 } else
1840 printf("%llu byte%s",
1841 (unsigned long long) ifi->ifi_ibytes,
1842 PLURAL(ifi->ifi_ibytes));
1843 if (ifi->ifi_imcasts)
1844 printf(", %llu multicast%s",
1845 (unsigned long long) ifi->ifi_imcasts,
1846 PLURAL(ifi->ifi_imcasts));
1847 if (ifi->ifi_ierrors)
1848 printf(", %llu error%s",
1849 (unsigned long long) ifi->ifi_ierrors,
1850 PLURAL(ifi->ifi_ierrors));
1851 if (ifi->ifi_iqdrops)
1852 printf(", %llu queue drop%s",
1853 (unsigned long long) ifi->ifi_iqdrops,
1854 PLURAL(ifi->ifi_iqdrops));
1855 if (ifi->ifi_noproto)
1856 printf(", %llu unknown protocol",
1857 (unsigned long long) ifi->ifi_noproto);
1858 printf("\n\toutput: %llu packet%s, ",
1859 (unsigned long long) ifi->ifi_opackets,
1860 PLURAL(ifi->ifi_opackets));
1861 if (hflag) {
1862 (void) humanize_number(buf, sizeof(buf),
1863 (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
1864 HN_NOSPACE | HN_DECIMAL);
1865 printf("%s byte%s", buf,
1866 PLURALSTR(buf));
1867 } else
1868 printf("%llu byte%s",
1869 (unsigned long long) ifi->ifi_obytes,
1870 PLURAL(ifi->ifi_obytes));
1871 if (ifi->ifi_omcasts)
1872 printf(", %llu multicast%s",
1873 (unsigned long long) ifi->ifi_omcasts,
1874 PLURAL(ifi->ifi_omcasts));
1875 if (ifi->ifi_oerrors)
1876 printf(", %llu error%s",
1877 (unsigned long long) ifi->ifi_oerrors,
1878 PLURAL(ifi->ifi_oerrors));
1879 if (ifi->ifi_collisions)
1880 printf(", %llu collision%s",
1881 (unsigned long long) ifi->ifi_collisions,
1882 PLURAL(ifi->ifi_collisions));
1883 printf("\n");
1884 #undef PLURAL
1885 #undef PLURALSTR
1886 }
1887
1888 ieee80211_statistics(env);
1889
1890 proto_status:
1891
1892 if (afp != NULL)
1893 (*afp->af_status)(env, oenv, true);
1894 else for (afp = afs; afp->af_name != NULL; afp++)
1895 (*afp->af_status)(env, oenv, false);
1896 }
1897
1898 int
1899 setifprefixlen(prop_dictionary_t env, prop_dictionary_t xenv)
1900 {
1901 const char *ifname;
1902 bool rc;
1903 int64_t plen;
1904 int af;
1905 const struct afswtch *afp;
1906 unsigned short flags;
1907 struct paddr_prefix *pfx;
1908 prop_data_t d;
1909
1910 if ((af = getaf(env)) == -1)
1911 af = AF_INET;
1912
1913 if ((afp = lookup_af_bynum(af)) == NULL)
1914 return -1;
1915
1916 if ((ifname = getifinfo(env, xenv, &flags)) == NULL)
1917 err(EXIT_FAILURE, "getifinfo");
1918
1919 rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
1920 assert(rc);
1921
1922 pfx = prefixlen_to_mask(af, plen);
1923 if (pfx == NULL)
1924 err(EXIT_FAILURE, "prefixlen_to_mask");
1925
1926 d = prop_data_create_data(pfx,
1927 offsetof(struct paddr_prefix, pfx_addr) + pfx->pfx_addr.sa_len);
1928 if (d == NULL)
1929 err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
1930
1931 if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
1932 err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
1933
1934 if (afp->af_getaddr != NULL)
1935 (*afp->af_getaddr)(pfx, MASK);
1936
1937 free(pfx);
1938 return 0;
1939 }
1940
1941 void
1942 usage(void)
1943 {
1944 const char *progname = getprogname();
1945
1946 fprintf(stderr,
1947 "usage: %s [-h] [-m] [-v] [-z] "
1948 #ifdef INET6
1949 "[-L] "
1950 #endif
1951 "interface\n"
1952 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1953 "\t\t[ alias | -alias ] ]\n"
1954 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
1955 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
1956 "\t[ list scan ]\n"
1957 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
1958 "\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
1959 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
1960 "\t[ arp | -arp ]\n"
1961 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
1962 "[ instance minst ]\n"
1963 "\t[ preference n ]\n"
1964 "\t[ vlan n vlanif i ]\n"
1965 "\t[ agrport i ] [ -agrport i ]\n"
1966 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
1967 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
1968 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1969 " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
1970 " %s -l [-b] [-d] [-u] [-s]\n"
1971 " %s -C\n"
1972 " %s interface create\n"
1973 " %s interface destroy\n",
1974 progname, progname, progname, progname, progname, progname);
1975 exit(EXIT_FAILURE);
1976 }
1977