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