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