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