ifconfig.c revision 1.191 1 /* $NetBSD: ifconfig.c,v 1.191 2008/05/06 18:16:34 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.191 2008/05/06 18:16:34 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 prop_dictionary_t);
175 void usage(void);
176
177 void print_media_word(int, const char *);
178 void process_media_commands(prop_dictionary_t);
179 void init_current_media(prop_dictionary_t, prop_dictionary_t);
180
181 /* Known address families */
182 static 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
217 struct 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
227 struct 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
247 extern struct pbranch command_root;
248 extern struct pbranch opt_command;
249 extern struct pbranch opt_family, opt_silent_family;
250 extern struct pkw cloning, silent_family, family, ia6flags, ia6lifetime,
251 ieee80211bool, ifcaps, ifflags, lists, misc;
252
253 struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
254 setifmetric, "metric", &command_root.pb_parser);
255
256 struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
257 setifmtu, "mtu", &command_root.pb_parser);
258
259 struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
260 "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
261
262 struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
263 "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
264 &command_root.pb_parser);
265
266 struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
267 setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
268
269 struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
270 "broadcast address",
271 setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
272
273 struct pstr mediamode = PSTR_INITIALIZER(&mediamode, "mediamode",
274 setmediamode, "mediamode", &command_root.pb_parser);
275
276 struct pinteger mediainst = PINTEGER_INITIALIZER1(&mediainst, "mediainst",
277 0, IFM_INST_MAX, 10, setmediainst, "mediainst", &command_root.pb_parser);
278
279 struct pstr unmediaopt = PSTR_INITIALIZER(&unmediaopt, "-mediaopt",
280 unsetmediaopt, "unmediaopt", &command_root.pb_parser);
281
282 struct pstr mediaopt = PSTR_INITIALIZER(&mediaopt, "mediaopt",
283 setmediaopt, "mediaopt", &command_root.pb_parser);
284
285 struct pstr media = PSTR_INITIALIZER(&media, "media",
286 setmedia, "media", &command_root.pb_parser);
287
288 struct 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 */
376 struct 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
382 struct 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
397 struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
398 list_cloners, "none");
399
400 struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
401 "none");
402
403 struct pkw family_only =
404 PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
405 __arraycount(familykw), &no_cmds.pt_parser);
406
407 struct paddr address = PADDR_INITIALIZER(&address,
408 "local address (address 1)",
409 setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
410
411 struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
412 "destination/netmask (address 2)",
413 setifdstormask, "dstormask", NULL, "address", "dstormask",
414 &command_root.pb_parser);
415
416 struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
417 "broadcast address (address 3)",
418 setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
419 &command_root.pb_parser);
420
421 struct 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
448 struct branch opt_family_only_brs[] = {
449 {.b_nextparser = &no_cmds.pt_parser}
450 , {.b_nextparser = &family_only.pk_parser}
451 };
452 struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
453 "opt-family-only", opt_family_only_brs,
454 __arraycount(opt_family_only_brs), true);
455 struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
456 "optional command",
457 opt_command_brs, __arraycount(opt_command_brs), true);
458
459 struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
460 "command-root", command_root_brs, __arraycount(command_root_brs), true);
461
462 struct 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
466 struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
467 familykw, __arraycount(familykw), &opt_command.pb_parser);
468
469 struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
470 NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
471
472 #ifdef INET6
473 struct kwinst ia6flagskw[] = {
474 IFKW("anycast", IN6_IFF_ANYCAST)
475 , IFKW("tentative", IN6_IFF_TENTATIVE)
476 , IFKW("deprecated", IN6_IFF_DEPRECATED)
477 };
478
479 struct pinteger pltime = PINTEGER_INITIALIZER(&pltime, "pltime", 0,
480 setia6pltime, "pltime", &command_root.pb_parser);
481
482 struct pinteger vltime = PINTEGER_INITIALIZER(&vltime, "vltime", 0,
483 setia6vltime, "vltime", &command_root.pb_parser);
484
485 struct kwinst ia6lifetimekw[] = {
486 {.k_word = "pltime", .k_nextparser = &pltime.pi_parser}
487 , {.k_word = "vltime", .k_nextparser = &vltime.pi_parser}
488 };
489
490 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", setia6flags,
491 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
492 struct pkw ia6lifetime = PKW_INITIALIZER(&ia6lifetime, "ia6lifetime", NULL,
493 NULL, ia6lifetimekw, __arraycount(ia6lifetimekw), NULL);
494 #endif /*INET6*/
495
496 struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
497 "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
498
499 struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
500 "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
501
502 struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
503 "clonecmd", clonekw, __arraycount(clonekw), NULL);
504
505 struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
506 misckw, __arraycount(misckw), NULL);
507
508 struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
509 "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
510
511 struct 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
515 struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
516 "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
517
518 struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
519 "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
520
521 struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
522 media_status_exec, "if", NULL);
523
524 static struct parser *
525 init_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
537 static int
538 no_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
553 static int
554 media_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
568 int
569 main(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
778 const struct afswtch *
779 lookup_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
789 void
790 printall(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
861 int
862 list_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
902 static int
903 clone_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*/
930 int
931 setifaddr(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
1003 int
1004 setifnetmask(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
1029 int
1030 setifbroadaddr(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
1065 int
1066 notealias(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*/
1100 int
1101 notrailers(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*/
1108 int
1109 setifdstormask(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
1147 void
1148 check_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
1165 int
1166 setifflags(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
1199 static int
1200 getifcaps(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 return rc ? 0 : -1;
1234 }
1235
1236 static int
1237 setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
1238 {
1239 int64_t ifcap;
1240 int s;
1241 bool rc;
1242 prop_data_t capdata;
1243 const char *ifname;
1244 struct ifcapreq ifcr;
1245
1246 s = getsock(AF_INET);
1247
1248 rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
1249 assert(rc);
1250
1251 if ((ifname = getifname(env)) == NULL)
1252 return -1;
1253
1254 if (getifcaps(env, oenv, &ifcr) == -1)
1255 return -1;
1256
1257 if (ifcap < 0) {
1258 ifcap = -ifcap;
1259 ifcr.ifcr_capenable &= ~ifcap;
1260 } else
1261 ifcr.ifcr_capenable |= ifcap;
1262
1263 if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
1264 return -1;
1265
1266 rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1267 prop_object_release((prop_object_t)capdata);
1268
1269 return rc ? 0 : -1;
1270 }
1271
1272 static int
1273 setifmetric(prop_dictionary_t env, prop_dictionary_t xenv)
1274 {
1275 struct ifreq ifr;
1276 bool rc;
1277 const char *ifname;
1278 int s;
1279 int64_t metric;
1280
1281 if ((s = getsock(AF_UNSPEC)) == -1)
1282 return -1;
1283
1284 if ((ifname = getifname(env)) == NULL)
1285 return -1;
1286
1287 rc = prop_dictionary_get_int64(env, "metric", &metric);
1288 assert(rc);
1289
1290 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1291 ifr.ifr_metric = metric;
1292 if (ioctl(s, SIOCSIFMETRIC, &ifr) == -1)
1293 warn("SIOCSIFMETRIC");
1294 return 0;
1295 }
1296
1297 static void
1298 do_setifpreference(prop_dictionary_t env)
1299 {
1300 struct if_addrprefreq ifap;
1301 int af, s;
1302 const char *ifname;
1303 const struct afswtch *afp;
1304 prop_data_t d;
1305 const struct paddr_prefix *pfx;
1306
1307 if ((af = getaf(env)) == -1)
1308 af = AF_INET;
1309
1310 if ((afp = lookup_af_bynum(af)) == NULL)
1311 errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
1312
1313 memset(&ifap, 0, sizeof(ifap));
1314
1315 if (!prop_dictionary_get_int16(env, "preference",
1316 &ifap.ifap_preference))
1317 return;
1318
1319 d = (prop_data_t)prop_dictionary_get(env, "address");
1320 assert(d != NULL);
1321
1322 pfx = prop_data_data_nocopy(d);
1323
1324 s = getsock(AF_UNSPEC);
1325
1326 if ((ifname = getifname(env)) == NULL)
1327 err(EXIT_FAILURE, "%s: getifname", __func__);
1328
1329 strlcpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
1330 memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
1331 MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
1332 if (ioctl(s, SIOCSIFADDRPREF, &ifap) == -1)
1333 warn("SIOCSIFADDRPREF");
1334 }
1335
1336 static int
1337 setifmtu(prop_dictionary_t env, prop_dictionary_t xenv)
1338 {
1339 int64_t mtu;
1340 bool rc;
1341 const char *ifname;
1342 struct ifreq ifr;
1343 int s;
1344
1345 if ((s = getsock(AF_UNSPEC)) == -1)
1346 return -1;
1347
1348 if ((ifname = getifname(env)) == NULL)
1349 return -1;
1350
1351 rc = prop_dictionary_get_int64(env, "mtu", &mtu);
1352 assert(rc);
1353
1354 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1355 ifr.ifr_mtu = mtu;
1356 if (ioctl(s, SIOCSIFMTU, &ifr) == -1)
1357 warn("SIOCSIFMTU");
1358
1359 return 0;
1360 }
1361
1362 static void
1363 media_error(int type, const char *val, const char *opt)
1364 {
1365 errx(EXIT_FAILURE, "unknown %s media %s: %s",
1366 get_media_type_string(type), opt, val);
1367 }
1368
1369 void
1370 init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
1371 {
1372 struct ifmediareq ifmr;
1373 int s;
1374 const char *ifname;
1375
1376 if ((s = getsock(AF_UNSPEC)) == -1)
1377 err(EXIT_FAILURE, "%s: getsock", __func__);
1378
1379 if ((ifname = getifname(env)) == NULL)
1380 err(EXIT_FAILURE, "%s: getifname", __func__);
1381
1382 /*
1383 * If we have not yet done so, grab the currently-selected
1384 * media.
1385 */
1386
1387 if (prop_dictionary_get(env, "initmedia") == NULL) {
1388 memset(&ifmr, 0, sizeof(ifmr));
1389 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1390
1391 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1392 /*
1393 * If we get E2BIG, the kernel is telling us
1394 * that there are more, so we can ignore it.
1395 */
1396 if (errno != E2BIG)
1397 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1398 }
1399
1400 if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
1401 err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
1402 __func__);
1403 }
1404 media_current = ifmr.ifm_current;
1405 }
1406
1407 /* Sanity. */
1408 if (IFM_TYPE(media_current) == 0)
1409 errx(EXIT_FAILURE, "%s: no link type?", ifname);
1410 }
1411
1412 void
1413 process_media_commands(prop_dictionary_t env)
1414 {
1415 struct ifreq ifr;
1416 const char *ifname;
1417 int s;
1418
1419 if ((s = getsock(AF_UNSPEC)) == -1)
1420 err(EXIT_FAILURE, "%s: getsock", __func__);
1421
1422 if (prop_dictionary_get(env, "media") == NULL &&
1423 prop_dictionary_get(env, "mediaopt") == NULL &&
1424 prop_dictionary_get(env, "unmediaopt") == NULL &&
1425 prop_dictionary_get(env, "mediamode") == NULL) {
1426 /* Nothing to do. */
1427 return;
1428 }
1429
1430 if ((ifname = getifname(env)) == NULL)
1431 err(EXIT_FAILURE, "%s: getifname", __func__);
1432
1433 /*
1434 * Media already set up, and commands sanity-checked. Set/clear
1435 * any options, and we're ready to go.
1436 */
1437 media_current |= mediaopt_set;
1438 media_current &= ~mediaopt_clear;
1439
1440 memset(&ifr, 0, sizeof(ifr));
1441 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1442 ifr.ifr_media = media_current;
1443
1444 if (ioctl(s, SIOCSIFMEDIA, &ifr) == -1)
1445 err(EXIT_FAILURE, "SIOCSIFMEDIA");
1446 }
1447
1448 int
1449 setmedia(prop_dictionary_t env, prop_dictionary_t xenv)
1450 {
1451 int type, subtype, inst;
1452 prop_data_t data;
1453 char *val;
1454
1455 init_current_media(env, xenv);
1456
1457 data = (prop_data_t)prop_dictionary_get(env, "media");
1458 assert(data != NULL);
1459
1460 /* Only one media command may be given. */
1461 /* Must not come after mode commands */
1462 /* Must not come after mediaopt commands */
1463
1464 /*
1465 * No need to check if `instance' has been issued; setmediainst()
1466 * craps out if `media' has not been specified.
1467 */
1468
1469 type = IFM_TYPE(media_current);
1470 inst = IFM_INST(media_current);
1471
1472 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1473 if (val == NULL)
1474 return -1;
1475
1476 /* Look up the subtype. */
1477 subtype = get_media_subtype(type, val);
1478 if (subtype == -1)
1479 media_error(type, val, "subtype");
1480
1481 /* Build the new current media word. */
1482 media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1483
1484 /* Media will be set after other processing is complete. */
1485 return 0;
1486 }
1487
1488 int
1489 setmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1490 {
1491 char *invalid;
1492 prop_data_t data;
1493 char *val;
1494
1495 init_current_media(env, xenv);
1496
1497 data = (prop_data_t)prop_dictionary_get(env, "mediaopt");
1498 assert(data != NULL);
1499
1500 /* Can only issue `mediaopt' once. */
1501 /* Can't issue `mediaopt' if `instance' has already been issued. */
1502
1503 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1504 if (val == NULL)
1505 return -1;
1506
1507 mediaopt_set = get_media_options(media_current, val, &invalid);
1508 free(val);
1509 if (mediaopt_set == -1)
1510 media_error(media_current, invalid, "option");
1511
1512 /* Media will be set after other processing is complete. */
1513 return 0;
1514 }
1515
1516 int
1517 unsetmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1518 {
1519 char *invalid, *val;
1520 prop_data_t data;
1521
1522 init_current_media(env, xenv);
1523
1524 data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
1525 if (data == NULL) {
1526 errno = ENOENT;
1527 return -1;
1528 }
1529
1530 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1531 if (val == NULL)
1532 return -1;
1533
1534 /*
1535 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1536 * implicitly checks for A_MEDIAINST.
1537 */
1538
1539 mediaopt_clear = get_media_options(media_current, val, &invalid);
1540 free(val);
1541 if (mediaopt_clear == -1)
1542 media_error(media_current, invalid, "option");
1543
1544 /* Media will be set after other processing is complete. */
1545 return 0;
1546 }
1547
1548 int
1549 setmediainst(prop_dictionary_t env, prop_dictionary_t xenv)
1550 {
1551 int type, subtype, options;
1552 int64_t inst;
1553 bool rc;
1554
1555 init_current_media(env, xenv);
1556
1557 rc = prop_dictionary_get_int64(env, "mediainst", &inst);
1558 assert(rc);
1559
1560 /* Can only issue `instance' once. */
1561 /* Must have already specified `media' */
1562
1563 type = IFM_TYPE(media_current);
1564 subtype = IFM_SUBTYPE(media_current);
1565 options = IFM_OPTIONS(media_current);
1566
1567 media_current = IFM_MAKEWORD(type, subtype, options, inst);
1568
1569 /* Media will be set after other processing is complete. */
1570 return 0;
1571 }
1572
1573 int
1574 setmediamode(prop_dictionary_t env, prop_dictionary_t xenv)
1575 {
1576 int type, subtype, options, inst, mode;
1577 prop_data_t data;
1578 char *val;
1579
1580 init_current_media(env, xenv);
1581
1582 data = (prop_data_t)prop_dictionary_get(env, "mediamode");
1583 assert(data != NULL);
1584
1585 type = IFM_TYPE(media_current);
1586 subtype = IFM_SUBTYPE(media_current);
1587 options = IFM_OPTIONS(media_current);
1588 inst = IFM_INST(media_current);
1589
1590 val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1591 if (val == NULL)
1592 return -1;
1593
1594 mode = get_media_mode(type, val);
1595 if (mode == -1)
1596 media_error(type, val, "mode");
1597
1598 free(val);
1599
1600 media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1601
1602 /* Media will be set after other processing is complete. */
1603 return 0;
1604 }
1605
1606 void
1607 print_media_word(int ifmw, const char *opt_sep)
1608 {
1609 const char *str;
1610
1611 printf("%s", get_media_subtype_string(ifmw));
1612
1613 /* Find mode. */
1614 if (IFM_MODE(ifmw) != 0) {
1615 str = get_media_mode_string(ifmw);
1616 if (str != NULL)
1617 printf(" mode %s", str);
1618 }
1619
1620 /* Find options. */
1621 for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1622 printf("%s%s", opt_sep, str);
1623
1624 if (IFM_INST(ifmw) != 0)
1625 printf(" instance %d", IFM_INST(ifmw));
1626 }
1627
1628 int
1629 carrier(prop_dictionary_t env)
1630 {
1631 struct ifmediareq ifmr;
1632 int s;
1633 const char *ifname;
1634
1635 if ((s = getsock(AF_UNSPEC)) == -1)
1636 err(EXIT_FAILURE, "%s: getsock", __func__);
1637
1638 if ((ifname = getifname(env)) == NULL)
1639 err(EXIT_FAILURE, "%s: getifname", __func__);
1640
1641 (void) memset(&ifmr, 0, sizeof(ifmr));
1642 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1643
1644 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1645 /*
1646 * Interface doesn't support SIOC{G,S}IFMEDIA;
1647 * assume ok.
1648 */
1649 return EXIT_SUCCESS;
1650 }
1651 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1652 /*
1653 * Interface doesn't report media-valid status.
1654 * assume ok.
1655 */
1656 return EXIT_SUCCESS;
1657 }
1658 /* otherwise, return ok for active, not-ok if not active. */
1659 if (ifmr.ifm_status & IFM_ACTIVE)
1660 return EXIT_SUCCESS;
1661 else
1662 return EXIT_FAILURE;
1663 }
1664
1665 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1666
1667 const struct ifmedia_status_description ifm_status_descriptions[] =
1668 IFM_STATUS_DESCRIPTIONS;
1669
1670 /*
1671 * Print the status of the interface. If an address family was
1672 * specified, show it and it only; otherwise, show them all.
1673 */
1674 void
1675 status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
1676 prop_dictionary_t oenv)
1677 {
1678 struct ifmediareq ifmr;
1679 struct ifdatareq ifdr;
1680 struct ifreq ifr;
1681 int *media_list, i;
1682 char hbuf[NI_MAXHOST];
1683 char fbuf[BUFSIZ];
1684 int af, s;
1685 const char *ifname;
1686 struct ifcapreq ifcr;
1687 unsigned short flags;
1688 const struct afswtch *afp;
1689
1690 if ((af = getaf(env)) == -1) {
1691 afp = NULL;
1692 af = AF_UNSPEC;
1693 } else
1694 afp = lookup_af_bynum(af);
1695
1696 if ((s = getsock(af)) == -1)
1697 err(EXIT_FAILURE, "%s: getsock", __func__);
1698
1699 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
1700 err(EXIT_FAILURE, "%s: getifinfo", __func__);
1701
1702 (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1703 printf("%s: flags=%s", ifname, &fbuf[2]);
1704
1705 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1706 if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
1707 warn("SIOCGIFMETRIC %s", ifr.ifr_name);
1708 else if (ifr.ifr_metric != 0)
1709 printf(" metric %d", ifr.ifr_metric);
1710
1711 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1712 if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
1713 printf(" mtu %d", ifr.ifr_mtu);
1714 printf("\n");
1715
1716 if (getifcaps(env, oenv, &ifcr) == -1)
1717 err(EXIT_FAILURE, "%s: getifcaps", __func__);
1718
1719 if (ifcr.ifcr_capabilities != 0) {
1720 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1721 ifcr.ifcr_capabilities);
1722 printf("\tcapabilities=%s\n", &fbuf[2]);
1723 (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1724 ifcr.ifcr_capenable);
1725 printf("\tenabled=%s\n", &fbuf[2]);
1726 }
1727
1728 ieee80211_status(env, oenv);
1729 vlan_status(env, oenv);
1730 #ifndef INET_ONLY
1731 carp_status(env, oenv);
1732 #endif
1733 tunnel_status(env, oenv);
1734 agr_status(env, oenv);
1735
1736 if (sdl != NULL &&
1737 getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1738 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1739 hbuf[0] != '\0')
1740 printf("\taddress: %s\n", hbuf);
1741
1742 (void) memset(&ifmr, 0, sizeof(ifmr));
1743 estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1744
1745 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1746 /*
1747 * Interface doesn't support SIOC{G,S}IFMEDIA.
1748 */
1749 goto iface_stats;
1750 }
1751
1752 if (ifmr.ifm_count == 0) {
1753 warnx("%s: no media types?", ifname);
1754 goto iface_stats;
1755 }
1756
1757 media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1758 if (media_list == NULL)
1759 err(EXIT_FAILURE, "malloc");
1760 ifmr.ifm_ulist = media_list;
1761
1762 if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1763 err(EXIT_FAILURE, "SIOCGIFMEDIA");
1764
1765 printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1766 print_media_word(ifmr.ifm_current, " ");
1767 if (ifmr.ifm_active != ifmr.ifm_current) {
1768 printf(" (");
1769 print_media_word(ifmr.ifm_active, " ");
1770 printf(")");
1771 }
1772 printf("\n");
1773
1774 if (ifmr.ifm_status & IFM_STATUS_VALID) {
1775 const struct ifmedia_status_description *ifms;
1776 int bitno, found = 0;
1777
1778 printf("\tstatus: ");
1779 for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1780 for (ifms = ifm_status_descriptions;
1781 ifms->ifms_valid != 0; ifms++) {
1782 if (ifms->ifms_type !=
1783 IFM_TYPE(ifmr.ifm_current) ||
1784 ifms->ifms_valid !=
1785 ifm_status_valid_list[bitno])
1786 continue;
1787 printf("%s%s", found ? ", " : "",
1788 IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1789 found = 1;
1790
1791 /*
1792 * For each valid indicator bit, there's
1793 * only one entry for each media type, so
1794 * terminate the inner loop now.
1795 */
1796 break;
1797 }
1798 }
1799
1800 if (found == 0)
1801 printf("unknown");
1802 printf("\n");
1803 }
1804
1805 if (mflag) {
1806 int type, printed_type;
1807
1808 for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1809 for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1810 if (IFM_TYPE(media_list[i]) != type)
1811 continue;
1812 if (printed_type == 0) {
1813 printf("\tsupported %s media:\n",
1814 get_media_type_string(type));
1815 printed_type = 1;
1816 }
1817 printf("\t\tmedia ");
1818 print_media_word(media_list[i], " mediaopt ");
1819 printf("\n");
1820 }
1821 }
1822 }
1823
1824 free(media_list);
1825
1826 iface_stats:
1827 if (!vflag && !zflag)
1828 goto proto_status;
1829
1830 estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
1831
1832 if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1833 err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1834 } else {
1835 struct if_data * const ifi = &ifdr.ifdr_data;
1836 char buf[5];
1837
1838 #define PLURAL(n) ((n) == 1 ? "" : "s")
1839 #define PLURALSTR(s) ((atof(s)) == 1.0 ? "" : "s")
1840 printf("\tinput: %llu packet%s, ",
1841 (unsigned long long) ifi->ifi_ipackets,
1842 PLURAL(ifi->ifi_ipackets));
1843 if (hflag) {
1844 (void) humanize_number(buf, sizeof(buf),
1845 (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
1846 HN_NOSPACE | HN_DECIMAL);
1847 printf("%s byte%s", buf,
1848 PLURALSTR(buf));
1849 } else
1850 printf("%llu byte%s",
1851 (unsigned long long) ifi->ifi_ibytes,
1852 PLURAL(ifi->ifi_ibytes));
1853 if (ifi->ifi_imcasts)
1854 printf(", %llu multicast%s",
1855 (unsigned long long) ifi->ifi_imcasts,
1856 PLURAL(ifi->ifi_imcasts));
1857 if (ifi->ifi_ierrors)
1858 printf(", %llu error%s",
1859 (unsigned long long) ifi->ifi_ierrors,
1860 PLURAL(ifi->ifi_ierrors));
1861 if (ifi->ifi_iqdrops)
1862 printf(", %llu queue drop%s",
1863 (unsigned long long) ifi->ifi_iqdrops,
1864 PLURAL(ifi->ifi_iqdrops));
1865 if (ifi->ifi_noproto)
1866 printf(", %llu unknown protocol",
1867 (unsigned long long) ifi->ifi_noproto);
1868 printf("\n\toutput: %llu packet%s, ",
1869 (unsigned long long) ifi->ifi_opackets,
1870 PLURAL(ifi->ifi_opackets));
1871 if (hflag) {
1872 (void) humanize_number(buf, sizeof(buf),
1873 (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
1874 HN_NOSPACE | HN_DECIMAL);
1875 printf("%s byte%s", buf,
1876 PLURALSTR(buf));
1877 } else
1878 printf("%llu byte%s",
1879 (unsigned long long) ifi->ifi_obytes,
1880 PLURAL(ifi->ifi_obytes));
1881 if (ifi->ifi_omcasts)
1882 printf(", %llu multicast%s",
1883 (unsigned long long) ifi->ifi_omcasts,
1884 PLURAL(ifi->ifi_omcasts));
1885 if (ifi->ifi_oerrors)
1886 printf(", %llu error%s",
1887 (unsigned long long) ifi->ifi_oerrors,
1888 PLURAL(ifi->ifi_oerrors));
1889 if (ifi->ifi_collisions)
1890 printf(", %llu collision%s",
1891 (unsigned long long) ifi->ifi_collisions,
1892 PLURAL(ifi->ifi_collisions));
1893 printf("\n");
1894 #undef PLURAL
1895 #undef PLURALSTR
1896 }
1897
1898 ieee80211_statistics(env);
1899
1900 proto_status:
1901
1902 if (afp != NULL)
1903 (*afp->af_status)(env, oenv, true);
1904 else for (afp = afs; afp->af_name != NULL; afp++)
1905 (*afp->af_status)(env, oenv, false);
1906 }
1907
1908 int
1909 setifprefixlen(prop_dictionary_t env, prop_dictionary_t xenv)
1910 {
1911 const char *ifname;
1912 bool rc;
1913 int64_t plen;
1914 int af;
1915 const struct afswtch *afp;
1916 unsigned short flags;
1917 struct paddr_prefix *pfx;
1918 prop_data_t d;
1919
1920 if ((af = getaf(env)) == -1)
1921 af = AF_INET;
1922
1923 if ((afp = lookup_af_bynum(af)) == NULL)
1924 return -1;
1925
1926 if ((ifname = getifinfo(env, xenv, &flags)) == NULL)
1927 err(EXIT_FAILURE, "getifinfo");
1928
1929 rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
1930 assert(rc);
1931
1932 pfx = prefixlen_to_mask(af, plen);
1933 if (pfx == NULL)
1934 err(EXIT_FAILURE, "prefixlen_to_mask");
1935
1936 d = prop_data_create_data(pfx,
1937 offsetof(struct paddr_prefix, pfx_addr) + pfx->pfx_addr.sa_len);
1938 if (d == NULL)
1939 err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
1940
1941 if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
1942 err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
1943
1944 if (afp->af_getaddr != NULL)
1945 (*afp->af_getaddr)(pfx, MASK);
1946
1947 free(pfx);
1948 return 0;
1949 }
1950
1951 void
1952 usage(void)
1953 {
1954 const char *progname = getprogname();
1955
1956 fprintf(stderr,
1957 "usage: %s [-h] [-m] [-v] [-z] "
1958 #ifdef INET6
1959 "[-L] "
1960 #endif
1961 "interface\n"
1962 "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1963 "\t\t[ alias | -alias ] ]\n"
1964 "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
1965 "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
1966 "\t[ list scan ]\n"
1967 "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
1968 "\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
1969 "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
1970 "\t[ arp | -arp ]\n"
1971 "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
1972 "[ instance minst ]\n"
1973 "\t[ preference n ]\n"
1974 "\t[ vlan n vlanif i ]\n"
1975 "\t[ agrport i ] [ -agrport i ]\n"
1976 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
1977 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
1978 "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1979 " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
1980 " %s -l [-b] [-d] [-u] [-s]\n"
1981 " %s -C\n"
1982 " %s interface create\n"
1983 " %s interface destroy\n",
1984 progname, progname, progname, progname, progname, progname);
1985 exit(EXIT_FAILURE);
1986 }
1987