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