1 /* 2 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Copyright (c) 1983 Regents of the University of California. 6 * All rights reserved. The Berkeley software License Agreement 7 * specifies the terms and conditions for redistribution. 8 */ 9 10 #include "defs.h" 11 #include "strings.h" 12 #include "ifconfig.h" 13 #include <compat.h> 14 #include <libdlpi.h> 15 #include <libdllink.h> 16 #include <libdliptun.h> 17 #include <libdllink.h> 18 #include <inet/ip.h> 19 #include <inet/ipsec_impl.h> 20 #include <libipadm.h> 21 #include <ifaddrs.h> 22 #include <libsocket_priv.h> 23 24 #define LOOPBACK_IF "lo0" 25 #define NONE_STR "none" 26 #define ARP_MOD_NAME "arp" 27 #define LIFC_DEFAULT (LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\ 28 LIFC_UNDER_IPMP) 29 30 typedef struct if_flags { 31 uint64_t iff_value; 32 char *iff_name; 33 } if_flags_t; 34 35 static if_flags_t if_flags_tbl[] = { 36 { IFF_UP, "UP" }, 37 { IFF_BROADCAST, "BROADCAST" }, 38 { IFF_DEBUG, "DEBUG" }, 39 { IFF_LOOPBACK, "LOOPBACK" }, 40 { IFF_POINTOPOINT, "POINTOPOINT" }, 41 { IFF_NOTRAILERS, "NOTRAILERS" }, 42 { IFF_RUNNING, "RUNNING" }, 43 { IFF_NOARP, "NOARP" }, 44 { IFF_PROMISC, "PROMISC" }, 45 { IFF_ALLMULTI, "ALLMULTI" }, 46 { IFF_INTELLIGENT, "INTELLIGENT" }, 47 { IFF_MULTICAST, "MULTICAST" }, 48 { IFF_MULTI_BCAST, "MULTI_BCAST" }, 49 { IFF_UNNUMBERED, "UNNUMBERED" }, 50 { IFF_DHCPRUNNING, "DHCP" }, 51 { IFF_PRIVATE, "PRIVATE" }, 52 { IFF_NOXMIT, "NOXMIT" }, 53 { IFF_NOLOCAL, "NOLOCAL" }, 54 { IFF_DEPRECATED, "DEPRECATED" }, 55 { IFF_ADDRCONF, "ADDRCONF" }, 56 { IFF_ROUTER, "ROUTER" }, 57 { IFF_NONUD, "NONUD" }, 58 { IFF_ANYCAST, "ANYCAST" }, 59 { IFF_NORTEXCH, "NORTEXCH" }, 60 { IFF_IPV4, "IPv4" }, 61 { IFF_IPV6, "IPv6" }, 62 { IFF_NOFAILOVER, "NOFAILOVER" }, 63 { IFF_FAILED, "FAILED" }, 64 { IFF_STANDBY, "STANDBY" }, 65 { IFF_INACTIVE, "INACTIVE" }, 66 { IFF_OFFLINE, "OFFLINE" }, 67 { IFF_XRESOLV, "XRESOLV" }, 68 { IFF_COS_ENABLED, "CoS" }, 69 { IFF_PREFERRED, "PREFERRED" }, 70 { IFF_TEMPORARY, "TEMPORARY" }, 71 { IFF_FIXEDMTU, "FIXEDMTU" }, 72 { IFF_VIRTUAL, "VIRTUAL" }, 73 { IFF_DUPLICATE, "DUPLICATE" }, 74 { IFF_IPMP, "IPMP"}, 75 { IFF_VRRP, "VRRP"}, 76 { IFF_NOACCEPT, "NOACCEPT"} 77 }; 78 79 typedef struct { 80 const char *ia_app; 81 uint64_t ia_flag; 82 uint_t ia_tries; 83 } if_appflags_t; 84 85 static const if_appflags_t if_appflags_tbl[] = { 86 { "dhcpagent(1M)", IFF_DHCPRUNNING, 1 }, 87 { "in.ndpd(1M)", IFF_ADDRCONF, 3 }, 88 { NULL, 0, 0 } 89 }; 90 91 static dladm_handle_t dlh; 92 boolean_t dlh_opened; 93 static struct lifreq lifr; 94 /* current interface name a particular function is accessing */ 95 static char name[LIFNAMSIZ]; 96 /* foreach interface saved name */ 97 static char origname[LIFNAMSIZ]; 98 static int setaddr; 99 static boolean_t setaddr_done = _B_FALSE; 100 static boolean_t ipsec_policy_set; 101 static boolean_t ipsec_auth_covered; 102 static ipadm_handle_t iph; 103 static ipadm_addrobj_t ipaddr; 104 105 /* 106 * Make sure the algorithm variables hold more than the sizeof an algorithm 107 * in PF_KEY. (For now, more than a uint8_t.) The NO_***_?ALG indicates that 108 * there was no algorithm requested, and in the ipsec_req that service should 109 * be disabled. (E.g. if ah_aalg remains NO_AH_AALG, then AH will be 110 * disabled on that tunnel.) 111 */ 112 #define NO_AH_AALG 256 113 #define NO_ESP_AALG 256 114 #define NO_ESP_EALG 256 115 116 int s, s4, s6; 117 int af = AF_INET; /* default address family */ 118 int debug = 0; 119 int all = 0; /* setifdhcp() needs to know this */ 120 int verbose = 0; 121 int v4compat = 0; /* Compatible printing format */ 122 123 /* 124 * Function prototypes for command functions. 125 */ 126 static int addif(char *arg, int64_t param); 127 static int inetipmp(char *arg, int64_t param); 128 static int inetplumb(char *arg, int64_t param); 129 static int inetunplumb(char *arg, int64_t param); 130 static int removeif(char *arg, int64_t param); 131 static int setdebugflag(char *arg, int64_t param); 132 static int setifaddr(char *arg, int64_t param); 133 static int setifbroadaddr(char *arg, int64_t param); 134 static int setifdstaddr(char *arg, int64_t param); 135 static int setifether(char *arg, int64_t param); 136 static int setifflags(char *arg, int64_t param); 137 static int setifindex(char *arg, int64_t param); 138 static int setifmetric(char *arg, int64_t param); 139 static int setifmtu(char *arg, int64_t param); 140 static int setifnetmask(char *arg, int64_t param); 141 static int setifprefixlen(char *arg, int64_t param); 142 static int setifrevarp(char *arg, int64_t param); 143 static int setifsubnet(char *arg, int64_t param); 144 static int setiftdst(char *arg, int64_t param); 145 static int setiftoken(char *arg, int64_t param); 146 static int setiftsrc(char *arg, int64_t param); 147 static int setverboseflag(char *arg, int64_t param); 148 static int set_tun_ah_alg(char *arg, int64_t param); 149 static int set_tun_esp_auth_alg(char *arg, int64_t param); 150 static int set_tun_esp_encr_alg(char *arg, int64_t param); 151 static int modlist(char *arg, int64_t param); 152 static int modinsert(char *arg, int64_t param); 153 static int modremove(char *arg, int64_t param); 154 static int setifgroupname(char *arg, int64_t param); 155 static int configinfo(char *arg, int64_t param); 156 static void print_config_flags(int af, uint64_t flags); 157 static void print_flags(uint64_t flags); 158 static void print_ifether(const char *ifname); 159 static int set_tun_encap_limit(char *arg, int64_t param); 160 static int clr_tun_encap_limit(char *arg, int64_t param); 161 static int set_tun_hop_limit(char *arg, int64_t param); 162 static int setzone(char *arg, int64_t param); 163 static int setallzones(char *arg, int64_t param); 164 static int setifsrc(char *arg, int64_t param); 165 static int lifnum(const char *ifname); 166 static void plumball(int, char **, int64_t, int64_t, int64_t); 167 168 /* 169 * Address family specific function prototypes. 170 */ 171 static void in_getaddr(char *s, struct sockaddr *saddr, int *plenp); 172 static void in_status(int force, uint64_t flags); 173 static void in_configinfo(int force, uint64_t flags); 174 static void in6_getaddr(char *s, struct sockaddr *saddr, int *plenp); 175 static void in6_status(int force, uint64_t flags); 176 static void in6_configinfo(int force, uint64_t flags); 177 178 /* 179 * Misc support functions 180 */ 181 static boolean_t ni_entry(const char *, void *); 182 static void foreachinterface(int argc, char *argv[], 183 int af, int64_t onflags, int64_t offflags, 184 int64_t lifc_flags); 185 static void ifconfig(int argc, char *argv[], int af, 186 struct ifaddrs *ifa); 187 static boolean_t in_getmask(struct sockaddr_in *saddr, 188 boolean_t addr_set); 189 static int in_getprefixlen(char *addr, boolean_t slash, int plen); 190 static boolean_t in_prefixlentomask(int prefixlen, int maxlen, 191 uchar_t *mask); 192 static void status(void); 193 static void ifstatus(const char *ifname); 194 static void tun_status(datalink_id_t); 195 static void usage(void); 196 static int setifdhcp(const char *caller, const char *ifname, 197 int argc, char *argv[]); 198 static int ip_domux2fd(int *, int *, int *, int *, int *); 199 static int ip_plink(int, int, int, int, int); 200 static int modop(char *arg, char op); 201 static int find_all_interfaces(struct lifconf *lifcp, char **buf, 202 int64_t lifc_flags); 203 static int create_ipmp(const char *grname, int af, 204 const char *ifname, boolean_t implicit); 205 static void start_ipmp_daemon(void); 206 static boolean_t ifaddr_up(ifaddrlistx_t *ifaddrp); 207 static boolean_t ifaddr_down(ifaddrlistx_t *ifaddrp); 208 static dladm_status_t ifconfig_dladm_open(const char *, datalink_class_t, 209 datalink_id_t *); 210 static void dladmerr_exit(dladm_status_t status, const char *str); 211 static void ipadmerr_exit(ipadm_status_t status, const char *str); 212 static boolean_t ifconfig_use_libipadm(int, const char *); 213 214 #define max(a, b) ((a) < (b) ? (b) : (a)) 215 216 /* 217 * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there 218 * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep 219 * on going rather than exit with an error. 220 */ 221 222 #define DHCP_EXIT_IF_FAILURE -1 223 224 #define NEXTARG 0xffffff /* command takes an argument */ 225 #define OPTARG 0xfffffe /* command takes an optional argument */ 226 #define AF_ANY (-1) 227 228 /* Refer to the comments in ifconfig() on the netmask "hack" */ 229 #define NETMASK_CMD "netmask" 230 struct sockaddr_storage g_netmask; 231 enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET } 232 g_netmask_set = G_NETMASK_NIL; 233 234 struct cmd { 235 char *c_name; 236 int64_t c_parameter; /* NEXTARG means next argv */ 237 int (*c_func)(char *, int64_t); 238 int c_abortonfail; /* don't continue parsing args */ 239 /* for the current interface */ 240 int c_af; /* address family restrictions */ 241 } cmds[] = { 242 { "up", IFF_UP, setifflags, 0, AF_ANY }, 243 { "down", -IFF_UP, setifflags, 0, AF_ANY }, 244 { "trailers", -IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 245 { "-trailers", IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 246 { "arp", -IFF_NOARP, setifflags, 0, AF_INET }, 247 { "-arp", IFF_NOARP, setifflags, 0, AF_INET }, 248 { "router", IFF_ROUTER, setifflags, 0, AF_ANY }, 249 { "-router", -IFF_ROUTER, setifflags, 0, AF_ANY }, 250 { "private", IFF_PRIVATE, setifflags, 0, AF_ANY }, 251 { "-private", -IFF_PRIVATE, setifflags, 0, AF_ANY }, 252 { "xmit", -IFF_NOXMIT, setifflags, 0, AF_ANY }, 253 { "-xmit", IFF_NOXMIT, setifflags, 0, AF_ANY }, 254 { "-nud", IFF_NONUD, setifflags, 0, AF_INET6 }, 255 { "nud", -IFF_NONUD, setifflags, 0, AF_INET6 }, 256 { "anycast", IFF_ANYCAST, setifflags, 0, AF_ANY }, 257 { "-anycast", -IFF_ANYCAST, setifflags, 0, AF_ANY }, 258 { "local", -IFF_NOLOCAL, setifflags, 0, AF_ANY }, 259 { "-local", IFF_NOLOCAL, setifflags, 0, AF_ANY }, 260 { "deprecated", IFF_DEPRECATED, setifflags, 0, AF_ANY }, 261 { "-deprecated", -IFF_DEPRECATED, setifflags, 0, AF_ANY }, 262 { "preferred", IFF_PREFERRED, setifflags, 0, AF_INET6 }, 263 { "-preferred", -IFF_PREFERRED, setifflags, 0, AF_INET6 }, 264 { "debug", 0, setdebugflag, 0, AF_ANY }, 265 { "verbose", 0, setverboseflag, 0, AF_ANY }, 266 { NETMASK_CMD, NEXTARG, setifnetmask, 0, AF_INET }, 267 { "metric", NEXTARG, setifmetric, 0, AF_ANY }, 268 { "mtu", NEXTARG, setifmtu, 0, AF_ANY }, 269 { "index", NEXTARG, setifindex, 0, AF_ANY }, 270 { "broadcast", NEXTARG, setifbroadaddr, 0, AF_INET }, 271 { "auto-revarp", 0, setifrevarp, 1, AF_INET }, 272 { "ipmp", 0, inetipmp, 1, AF_ANY }, 273 { "plumb", 0, inetplumb, 1, AF_ANY }, 274 { "unplumb", 0, inetunplumb, 0, AF_ANY }, 275 { "subnet", NEXTARG, setifsubnet, 0, AF_ANY }, 276 { "token", NEXTARG, setiftoken, 0, AF_INET6 }, 277 { "tsrc", NEXTARG, setiftsrc, 0, AF_ANY }, 278 { "tdst", NEXTARG, setiftdst, 0, AF_ANY }, 279 { "encr_auth_algs", NEXTARG, set_tun_esp_auth_alg, 0, AF_ANY }, 280 { "encr_algs", NEXTARG, set_tun_esp_encr_alg, 0, AF_ANY }, 281 { "auth_algs", NEXTARG, set_tun_ah_alg, 0, AF_ANY }, 282 { "addif", NEXTARG, addif, 1, AF_ANY }, 283 { "removeif", NEXTARG, removeif, 1, AF_ANY }, 284 { "modlist", 0, modlist, 1, AF_ANY }, 285 { "modinsert", NEXTARG, modinsert, 1, AF_ANY }, 286 { "modremove", NEXTARG, modremove, 1, AF_ANY }, 287 { "failover", -IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 288 { "-failover", IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 289 { "standby", IFF_STANDBY, setifflags, 1, AF_ANY }, 290 { "-standby", -IFF_STANDBY, setifflags, 1, AF_ANY }, 291 { "failed", IFF_FAILED, setifflags, 1, AF_ANY }, 292 { "-failed", -IFF_FAILED, setifflags, 1, AF_ANY }, 293 { "group", NEXTARG, setifgroupname, 1, AF_ANY }, 294 { "configinfo", 0, configinfo, 1, AF_ANY }, 295 { "encaplimit", NEXTARG, set_tun_encap_limit, 0, AF_ANY }, 296 { "-encaplimit", 0, clr_tun_encap_limit, 0, AF_ANY }, 297 { "thoplimit", NEXTARG, set_tun_hop_limit, 0, AF_ANY }, 298 { "set", NEXTARG, setifaddr, 0, AF_ANY }, 299 { "destination", NEXTARG, setifdstaddr, 0, AF_ANY }, 300 { "zone", NEXTARG, setzone, 0, AF_ANY }, 301 { "-zone", 0, setzone, 0, AF_ANY }, 302 { "all-zones", 0, setallzones, 0, AF_ANY }, 303 { "ether", OPTARG, setifether, 0, AF_ANY }, 304 { "usesrc", NEXTARG, setifsrc, 0, AF_ANY }, 305 306 /* 307 * NOTE: any additions to this table must also be applied to ifparse 308 * (usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c) 309 */ 310 311 { 0, 0, setifaddr, 0, AF_ANY }, 312 { 0, 0, setifdstaddr, 0, AF_ANY }, 313 { 0, 0, 0, 0, 0 }, 314 }; 315 316 317 typedef struct if_config_cmd { 318 uint64_t iff_flag; 319 int iff_af; 320 char *iff_name; 321 } if_config_cmd_t; 322 323 /* 324 * NOTE: print_config_flags() processes this table in order, so we put "up" 325 * last so that we can be sure "-failover" will take effect first. Otherwise, 326 * IPMP test addresses will erroneously migrate to the IPMP interface. 327 */ 328 static if_config_cmd_t if_config_cmd_tbl[] = { 329 { IFF_NOTRAILERS, AF_UNSPEC, "-trailers" }, 330 { IFF_PRIVATE, AF_UNSPEC, "private" }, 331 { IFF_NOXMIT, AF_UNSPEC, "-xmit" }, 332 { IFF_ANYCAST, AF_INET6, "anycast" }, 333 { IFF_NOLOCAL, AF_UNSPEC, "-local" }, 334 { IFF_DEPRECATED, AF_UNSPEC, "deprecated" }, 335 { IFF_NOFAILOVER, AF_UNSPEC, "-failover" }, 336 { IFF_STANDBY, AF_UNSPEC, "standby" }, 337 { IFF_FAILED, AF_UNSPEC, "failed" }, 338 { IFF_PREFERRED, AF_UNSPEC, "preferred" }, 339 { IFF_NONUD, AF_INET6, "-nud" }, 340 { IFF_NOARP, AF_INET, "-arp" }, 341 { IFF_UP, AF_UNSPEC, "up" }, 342 { 0, 0, NULL }, 343 }; 344 345 typedef struct ni { 346 char ni_name[LIFNAMSIZ]; 347 struct ni *ni_next; 348 } ni_t; 349 350 static ni_t *ni_list = NULL; 351 static int num_ni = 0; 352 353 /* End defines and structure definitions for ifconfig -a plumb */ 354 355 /* Known address families */ 356 struct afswtch { 357 char *af_name; 358 short af_af; 359 void (*af_status)(); 360 void (*af_getaddr)(); 361 void (*af_configinfo)(); 362 } afs[] = { 363 { "inet", AF_INET, in_status, in_getaddr, in_configinfo }, 364 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo }, 365 { 0, 0, 0, 0, 0 } 366 }; 367 368 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 369 370 struct afswtch *afp; /* the address family being set or asked about */ 371 372 int 373 main(int argc, char *argv[]) 374 { 375 int64_t lifc_flags; 376 char *default_ip_str; 377 ipadm_status_t istatus; 378 379 lifc_flags = LIFC_DEFAULT; 380 381 if (argc < 2) { 382 usage(); 383 exit(1); 384 } 385 argc--, argv++; 386 if (strlen(*argv) > sizeof (name) - 1) { 387 (void) fprintf(stderr, "%s: interface name too long\n", *argv); 388 exit(1); 389 } 390 (void) strncpy(name, *argv, sizeof (name)); 391 name[sizeof (name) - 1] = '\0'; 392 (void) strncpy(origname, name, sizeof (origname)); /* For addif */ 393 default_ip_str = NULL; 394 v4compat = get_compat_flag(&default_ip_str); 395 if (v4compat == DEFAULT_PROT_BAD_VALUE) { 396 (void) fprintf(stderr, 397 "ifconfig: %s: Bad value for %s in %s\n", default_ip_str, 398 DEFAULT_IP, INET_DEFAULT_FILE); 399 free(default_ip_str); 400 exit(2); 401 } 402 free(default_ip_str); 403 argc--, argv++; 404 if (argc > 0) { 405 struct afswtch *myafp; 406 407 for (myafp = afp = afs; myafp->af_name; myafp++) { 408 if (strcmp(myafp->af_name, *argv) == 0) { 409 afp = myafp; argc--; argv++; 410 break; 411 } 412 } 413 af = lifr.lifr_addr.ss_family = afp->af_af; 414 if (af == AF_INET6) { 415 v4compat = 0; 416 } 417 } 418 419 s = socket(SOCKET_AF(af), SOCK_DGRAM, 0); 420 s4 = socket(AF_INET, SOCK_DGRAM, 0); 421 s6 = socket(AF_INET6, SOCK_DGRAM, 0); 422 if (s == -1 || s4 == -1 || s6 == -1) 423 Perror0_exit("socket"); 424 /* 425 * Open the global libipadm handle. The flag IPH_LEGACY has to 426 * be specified to indicate that logical interface names will 427 * be used during interface creation and address creation. 428 */ 429 if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS) 430 ipadmerr_exit(istatus, "unable to open handle to libipadm"); 431 432 /* 433 * Special interface names is any combination of these flags. 434 * Note that due to the ifconfig syntax they have to be combined 435 * as a single '-' option. 436 * -a All interfaces 437 * -u "up" interfaces 438 * -d "down" interfaces 439 * -D Interfaces not controlled by DHCP 440 * -4 IPv4 interfaces 441 * -6 IPv6 interfaces 442 * -X Turn on debug (not documented) 443 * -v Turn on verbose 444 * -Z Only interfaces in caller's zone 445 */ 446 447 if (name[0] == '-') { 448 /* One or more options */ 449 int64_t onflags = 0; 450 int64_t offflags = 0; 451 int c; 452 char *av[2] = { "ifconfig", name }; 453 454 while ((c = getopt(2, av, "audDXZ46v")) != -1) { 455 switch ((char)c) { 456 case 'a': 457 all = 1; 458 break; 459 case 'u': 460 onflags |= IFF_UP; 461 break; 462 case 'd': 463 offflags |= IFF_UP; 464 break; 465 case 'D': 466 offflags |= IFF_DHCPRUNNING; 467 break; 468 case 'X': 469 debug += 3; 470 break; 471 case 'Z': 472 lifc_flags &= ~LIFC_ALLZONES; 473 break; 474 case '4': 475 /* 476 * -4 is not a compatable flag, therefore 477 * we assume they want v4compat turned off 478 */ 479 v4compat = 0; 480 onflags |= IFF_IPV4; 481 break; 482 case '6': 483 /* 484 * If they want IPv6, well then we'll assume 485 * they don't want IPv4 compat 486 */ 487 v4compat = 0; 488 onflags |= IFF_IPV6; 489 break; 490 case 'v': 491 verbose = 1; 492 break; 493 case '?': 494 usage(); 495 exit(1); 496 } 497 } 498 if (!all) { 499 (void) fprintf(stderr, 500 "ifconfig: %s: no such interface\n", name); 501 exit(1); 502 } 503 foreachinterface(argc, argv, af, onflags, offflags, 504 lifc_flags); 505 } else { 506 ifconfig(argc, argv, af, NULL); 507 } 508 ipadm_close(iph); 509 return (0); 510 } 511 512 /* 513 * For each interface, call ifconfig(argc, argv, af, ifa). 514 * Only call function if onflags and offflags are set or clear, respectively, 515 * in the interfaces flags field. 516 */ 517 static void 518 foreachinterface(int argc, char *argv[], int af, 519 int64_t onflags, int64_t offflags, int64_t lifc_flags) 520 { 521 ipadm_addr_info_t *ainfo, *ainfop; 522 struct ifaddrs *ifa; 523 ipadm_status_t istatus; 524 525 /* 526 * Special case: 527 * ifconfig -a plumb should find all network interfaces in the current 528 * zone. 529 */ 530 if (argc > 0 && (strcmp(*argv, "plumb") == 0)) { 531 plumball(argc, argv, onflags, offflags, lifc_flags); 532 return; 533 } 534 /* Get all addresses in kernel including addresses that are zero. */ 535 istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR, 536 lifc_flags); 537 if (istatus != IPADM_SUCCESS) 538 ipadmerr_exit(istatus, "could not get addresses from kernel"); 539 540 /* 541 * For each logical interface, call ifconfig() with the 542 * given arguments. 543 */ 544 for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) { 545 if (ainfop->ia_state == IFA_DISABLED) 546 continue; 547 ifa = &ainfop->ia_ifa; 548 if (onflags || offflags) { 549 if ((ifa->ifa_flags & onflags) != onflags) 550 continue; 551 if ((~ifa->ifa_flags & offflags) != offflags) 552 continue; 553 } 554 s = (ifa->ifa_addr->ss_family == AF_INET ? s4 : s6); 555 (void) strncpy(name, ifa->ifa_name, sizeof (name)); 556 (void) strncpy(origname, name, sizeof (origname)); 557 ifconfig(argc, argv, af, ifa); 558 } 559 ipadm_free_addr_info(ainfo); 560 } 561 562 /* 563 * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface. 564 */ 565 static void 566 plumball(int argc, char *argv[], int64_t onflags, int64_t offflags, 567 int64_t lifc_flags) 568 { 569 int n; 570 struct lifreq *lifrp; 571 struct lifconf lifc; 572 char *buf; 573 574 if (onflags != 0 || offflags != 0) { 575 (void) fprintf(stderr, "ifconfig: invalid syntax used to " 576 "plumb all interfaces.\n"); 577 exit(1); 578 } 579 580 if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 || 581 lifc.lifc_len == 0) 582 return; 583 584 lifrp = lifc.lifc_req; 585 for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) { 586 /* 587 * Reset global state 588 * setaddr: Used by parser to tear apart source and dest 589 * name and origname contain the name of the 'current' 590 * interface. 591 */ 592 setaddr = 0; 593 (void) strncpy(name, lifrp->lifr_name, sizeof (name)); 594 (void) strncpy(origname, name, sizeof (origname)); 595 ifconfig(argc, argv, af, NULL); 596 } 597 } 598 599 /* 600 * Parses the interface name and the command in argv[]. Calls the 601 * appropriate callback function for the given command from `cmds[]' 602 * table. 603 * If there is no command specified, it prints all addresses. 604 */ 605 static void 606 ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa) 607 { 608 static boolean_t scan_netmask = _B_FALSE; 609 int ret; 610 ipadm_status_t istatus; 611 struct lifreq lifr; 612 613 if (argc == 0) { 614 status(); 615 return; 616 } 617 618 if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) { 619 /* 620 * Some errors are ignored in the case where more than one 621 * interface is being operated on. 622 */ 623 ret = setifdhcp("ifconfig", name, argc, argv); 624 if (ret == DHCP_EXIT_IF_FAILURE) { 625 if (!all) 626 exit(DHCP_EXIT_FAILURE); 627 } else if (ret != DHCP_EXIT_SUCCESS) { 628 exit(ret); 629 } 630 return; 631 } 632 633 /* 634 * The following is a "hack" to get around the existing interface 635 * setting mechanism. Currently, each interface attribute, 636 * such as address, netmask, broadcast, ... is set separately. But 637 * sometimes two or more attributes must be set together. For 638 * example, setting an address without a netmask does not make sense. 639 * Yet they can be set separately for IPv4 address using the current 640 * ifconfig(1M) syntax. The kernel then "infers" the correct netmask 641 * using the deprecated "IP address classes." This is simply not 642 * correct. 643 * 644 * The "hack" below is to go thru the whole command list looking for 645 * the netmask command first. Then use this netmask to set the 646 * address. This does not provide an extensible way to accommodate 647 * future need for setting more than one attributes together. 648 * 649 * Note that if the "netmask" command argument is a "+", we need 650 * to save this info and do the query after we know the address to 651 * be set. The reason is that if "addif" is used, the working 652 * interface name will be changed later when the logical interface 653 * is created. In in_getmask(), if an address is not provided, 654 * it will use the working interface's address to do the query. 655 * It will be wrong now as we don't know the logical interface's name. 656 * 657 * ifconfig(1M) is too overloaded and the code is so convoluted 658 * that it is "safer" not to re-architect the code to fix the above 659 * issue, hence this "hack." We may be better off to have a new 660 * command with better syntax for configuring network interface 661 * parameters... 662 */ 663 if (!scan_netmask && afp->af_af == AF_INET) { 664 int largc; 665 char **largv; 666 667 /* Only go thru the command list once to find the netmask. */ 668 scan_netmask = _B_TRUE; 669 670 /* 671 * Currently, if multiple netmask commands are specified, the 672 * last one will be used as the final netmask. So we need 673 * to scan the whole list to preserve this behavior. 674 */ 675 for (largc = argc, largv = argv; largc > 0; largc--, largv++) { 676 if (strcmp(*largv, NETMASK_CMD) == 0) { 677 if (--largc == 0) 678 break; 679 largv++; 680 if (strcmp(*largv, "+") == 0) { 681 g_netmask_set = G_NETMASK_PENDING; 682 } else { 683 in_getaddr(*largv, (struct sockaddr *) 684 &g_netmask, NULL); 685 g_netmask_set = G_NETMASK_SET; 686 } 687 /* Continue the scan. */ 688 } 689 } 690 } 691 692 while (argc > 0) { 693 struct cmd *p; 694 boolean_t found_cmd; 695 696 if (debug) 697 (void) printf("ifconfig: argv %s\n", *argv); 698 699 found_cmd = _B_FALSE; 700 for (p = cmds; p->c_func; p++) { 701 if (p->c_name) { 702 if (strcmp(*argv, p->c_name) == 0) { 703 /* 704 * indicate that the command was 705 * found and check to see if 706 * the address family is valid 707 */ 708 found_cmd = _B_TRUE; 709 if (p->c_af == AF_ANY || 710 af == p->c_af) 711 break; 712 } 713 } else { 714 if (p->c_af == AF_ANY || 715 af == p->c_af) 716 break; 717 } 718 } 719 /* 720 * If we found the keyword, but the address family 721 * did not match spit out an error 722 */ 723 if (found_cmd && p->c_name == 0) { 724 (void) fprintf(stderr, "ifconfig: Operation %s not" 725 " supported for %s\n", *argv, afp->af_name); 726 exit(1); 727 } 728 /* 729 * else (no keyword found), we assume it's an address 730 * of some sort 731 */ 732 if (setaddr && ipaddr != NULL) { 733 /* 734 * We must have already filled in a source address in 735 * `ipaddr' and we now got a destination address. 736 * Fill it in `ipaddr' and call libipadm to create 737 * the static address. 738 */ 739 if (p->c_name == 0) { 740 istatus = ipadm_set_dst_addr(ipaddr, *argv, 741 (p->c_af == AF_ANY ? AF_UNSPEC : af)); 742 if (istatus != IPADM_SUCCESS) { 743 ipadmerr_exit(istatus, "could not " 744 "set destination address"); 745 } 746 /* 747 * finished processing dstaddr, so reset setaddr 748 */ 749 setaddr = 0; 750 } 751 /* 752 * Both source and destination address are in `ipaddr'. 753 * Add the address by calling libipadm. 754 */ 755 istatus = ipadm_create_addr(iph, ipaddr, 756 IPADM_OPT_ACTIVE); 757 if (istatus != IPADM_SUCCESS) 758 goto createfailed; 759 ipadm_destroy_addrobj(ipaddr); 760 ipaddr = NULL; 761 setaddr_done = _B_TRUE; 762 if (p->c_name == 0) { 763 /* move parser along */ 764 argc--, argv++; 765 continue; 766 } 767 } 768 if (p->c_name == 0 && setaddr_done) { 769 /* 770 * catch odd commands like 771 * "ifconfig <intf> addr1 addr2 addr3 addr4 up" 772 */ 773 (void) fprintf(stderr, "%s", 774 "ifconfig: cannot configure more than two " 775 "addresses in one command\n"); 776 exit(1); 777 } 778 if (p->c_func) { 779 if (p->c_af == AF_INET6) { 780 v4compat = 0; 781 } 782 if (p->c_parameter == NEXTARG || 783 p->c_parameter == OPTARG) { 784 argc--, argv++; 785 if (argc == 0 && p->c_parameter == NEXTARG) { 786 (void) fprintf(stderr, 787 "ifconfig: no argument for %s\n", 788 p->c_name); 789 exit(1); 790 } 791 } 792 /* 793 * Call the function if: 794 * 795 * there's no address family 796 * restriction 797 * OR 798 * we don't know the address yet 799 * (because we were called from 800 * main) 801 * OR 802 * there is a restriction AND 803 * the address families match 804 */ 805 if ((p->c_af == AF_ANY) || 806 (ifa == NULL) || 807 (ifa->ifa_addr->ss_family == p->c_af)) { 808 ret = (*p->c_func)(*argv, p->c_parameter); 809 /* 810 * If c_func failed and we should 811 * abort processing for this 812 * interface on failure, return 813 * now rather than going on to 814 * process other commands for 815 * the same interface. 816 */ 817 if (ret != 0 && p->c_abortonfail) 818 return; 819 } 820 } 821 argc--, argv++; 822 } 823 824 if (setaddr && ipaddr != NULL) { 825 /* 826 * Only the source address was provided, which was already 827 * set in `ipaddr'. Add the address by calling libipadm. 828 */ 829 istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE); 830 if (istatus != IPADM_SUCCESS) 831 goto createfailed; 832 ipadm_destroy_addrobj(ipaddr); 833 ipaddr = NULL; 834 setaddr_done = _B_TRUE; 835 } 836 837 /* Check to see if there's a security hole in the tunnel setup. */ 838 if (ipsec_policy_set && !ipsec_auth_covered) { 839 (void) fprintf(stderr, "ifconfig: WARNING: tunnel with only " 840 "ESP and no authentication.\n"); 841 } 842 return; 843 844 createfailed: 845 (void) fprintf(stderr, "ifconfig: could not create address:% s\n", 846 ipadm_status2str(istatus)); 847 /* Remove the newly created logical interface. */ 848 if (strcmp(name, origname) != 0) { 849 assert(strchr(name, ':') != NULL); 850 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 851 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 852 } 853 exit(1); 854 } 855 856 /* ARGSUSED */ 857 static int 858 setdebugflag(char *val, int64_t arg) 859 { 860 debug++; 861 return (0); 862 } 863 864 /* ARGSUSED */ 865 static int 866 setverboseflag(char *val, int64_t arg) 867 { 868 verbose++; 869 return (0); 870 } 871 872 /* 873 * This function fills in the given lifreq's lifr_addr field based on 874 * g_netmask_set. 875 */ 876 static void 877 set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr, 878 struct sockaddr_storage *mask) 879 { 880 assert(addr != NULL); 881 assert(mask != NULL); 882 883 switch (g_netmask_set) { 884 case G_NETMASK_SET: 885 lifr->lifr_addr = g_netmask; 886 break; 887 888 case G_NETMASK_PENDING: 889 /* 890 * "+" is used as the argument to "netmask" command. Query 891 * the database on the correct netmask based on the address to 892 * be set. 893 */ 894 assert(afp->af_af == AF_INET); 895 g_netmask = *addr; 896 if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) { 897 lifr->lifr_addr = *mask; 898 g_netmask_set = G_NETMASK_NIL; 899 } else { 900 lifr->lifr_addr = g_netmask; 901 g_netmask_set = G_NETMASK_SET; 902 } 903 break; 904 905 case G_NETMASK_NIL: 906 default: 907 lifr->lifr_addr = *mask; 908 break; 909 } 910 } 911 912 /* 913 * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n> 914 * syntax for setting the address, the address plus netmask, and just 915 * the netmask respectively. 916 */ 917 /* ARGSUSED */ 918 static int 919 setifaddr(char *addr, int64_t param) 920 { 921 ipadm_status_t istatus; 922 int prefixlen = 0; 923 struct lifreq lifr1; 924 struct sockaddr_storage laddr; 925 struct sockaddr_storage netmask; 926 struct sockaddr_in6 *sin6; 927 struct sockaddr_in *sin; 928 struct sockaddr_storage sav_netmask; 929 char cidraddr[BUFSIZ]; 930 931 if (addr[0] == '/') 932 return (setifprefixlen(addr, 0)); 933 934 (*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen); 935 936 (void) memset(&netmask, 0, sizeof (netmask)); 937 netmask.ss_family = afp->af_af; 938 switch (prefixlen) { 939 case NO_PREFIX: 940 /* Nothing there - ok */ 941 break; 942 case BAD_ADDR: 943 (void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n", 944 addr); 945 exit(1); 946 default: 947 if (afp->af_af == AF_INET6) { 948 sin6 = (struct sockaddr_in6 *)&netmask; 949 if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 950 (uchar_t *)&sin6->sin6_addr)) { 951 (void) fprintf(stderr, "ifconfig: " 952 "Bad prefix length: %d\n", 953 prefixlen); 954 exit(1); 955 } 956 } else { 957 sin = (struct sockaddr_in *)&netmask; 958 if (!in_prefixlentomask(prefixlen, IP_ABITS, 959 (uchar_t *)&sin->sin_addr)) { 960 (void) fprintf(stderr, "ifconfig: " 961 "Bad prefix length: %d\n", 962 prefixlen); 963 exit(1); 964 } 965 } 966 /* 967 * Just in case of funny setting of both prefix and netmask, 968 * prefix should override the netmask command. 969 */ 970 g_netmask_set = G_NETMASK_NIL; 971 break; 972 } 973 974 /* 975 * Check and see if any "netmask" command is used and perform the 976 * necessary operation. 977 */ 978 set_mask_lifreq(&lifr, &laddr, &netmask); 979 980 /* This check is temporary until libipadm supports IPMP interfaces. */ 981 if (ifconfig_use_libipadm(s, name)) { 982 char addrstr[INET6_ADDRSTRLEN]; 983 984 if (af == AF_INET) { 985 sin = (struct sockaddr_in *)&laddr; 986 (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr, 987 sizeof (addrstr)); 988 } else { 989 sin6 = (struct sockaddr_in6 *)&laddr; 990 (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 991 sizeof (addrstr)); 992 } 993 istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 994 &ipaddr); 995 if (istatus != IPADM_SUCCESS) 996 ipadmerr_exit(istatus, "setifaddr"); 997 998 /* 999 * lifr.lifr_addr, which is updated by set_mask_lifreq() 1000 * will contain the right mask to use. 1001 */ 1002 prefixlen = mask2plen(&lifr.lifr_addr); 1003 (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 1004 addrstr, prefixlen); 1005 1006 istatus = ipadm_set_addr(ipaddr, cidraddr, af); 1007 if (istatus != IPADM_SUCCESS) 1008 ipadmerr_exit(istatus, "could not set address"); 1009 /* 1010 * let parser know we got a source. 1011 * Next address, if given, should be dest 1012 */ 1013 setaddr++; 1014 1015 /* 1016 * address will be set by the parser after nextarg has 1017 * been scanned 1018 */ 1019 return (0); 1020 } 1021 1022 /* Tell parser that an address was set */ 1023 setaddr++; 1024 /* save copy of netmask to restore in case of error */ 1025 (void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name)); 1026 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0) 1027 Perror0_exit("SIOCGLIFNETMASK"); 1028 sav_netmask = lifr1.lifr_addr; 1029 1030 /* 1031 * If setting the address and not the mask, clear any existing mask 1032 * and the kernel will then assign the default (netmask has been set 1033 * to 0 in this case). If setting both (either by using a prefix or 1034 * using the netmask command), set the mask first, so the address will 1035 * be interpreted correctly. 1036 */ 1037 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1038 /* lifr.lifr_addr already contains netmask from set_mask_lifreq() */ 1039 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 1040 Perror0_exit("SIOCSLIFNETMASK"); 1041 1042 if (debug) { 1043 char abuf[INET6_ADDRSTRLEN]; 1044 void *addr = (afp->af_af == AF_INET) ? 1045 (void *)&((struct sockaddr_in *)&laddr)->sin_addr : 1046 (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr; 1047 1048 (void) printf("Setting %s af %d addr %s\n", 1049 lifr.lifr_name, afp->af_af, 1050 inet_ntop(afp->af_af, addr, abuf, sizeof (abuf))); 1051 } 1052 lifr.lifr_addr = laddr; 1053 lifr.lifr_addr.ss_family = afp->af_af; 1054 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 1055 /* 1056 * Restore the netmask 1057 */ 1058 int saverr = errno; 1059 1060 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1061 lifr.lifr_addr = sav_netmask; 1062 (void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr); 1063 errno = saverr; 1064 Perror0_exit("SIOCSLIFADDR"); 1065 } 1066 1067 return (0); 1068 } 1069 1070 /* 1071 * The following functions are stolen from the ipseckey(1m) program. 1072 * Perhaps they should be somewhere common, but for now, we just maintain 1073 * two versions. We do this because of the different semantics for which 1074 * algorithms we select ("requested" for ifconfig vs. "actual" for key). 1075 */ 1076 1077 static ulong_t 1078 parsenum(char *num) 1079 { 1080 ulong_t rc; 1081 char *end = NULL; 1082 1083 errno = 0; 1084 rc = strtoul(num, &end, 0); 1085 if (errno != 0 || end == num || *end != '\0') { 1086 rc = (ulong_t)-1; 1087 } 1088 1089 return (rc); 1090 } 1091 1092 /* 1093 * Parse and reverse parse possible algorithm values, include numbers. 1094 * Mostly stolen from ipseckey.c. See the comments above parsenum() for why 1095 * this isn't common to ipseckey.c. 1096 * 1097 * NOTE: Static buffer in this function for the return value. Since ifconfig 1098 * isn't multithreaded, this isn't a huge problem. 1099 */ 1100 1101 #define NBUF_SIZE 20 /* Enough to print a large integer. */ 1102 1103 static char * 1104 rparsealg(uint8_t alg_value, int proto_num) 1105 { 1106 struct ipsecalgent *alg; 1107 static char numprint[128]; /* Enough to hold an algorithm name. */ 1108 1109 /* 1110 * Special cases for "any" and "none" 1111 * The kernel needs to be able to distinguish between "any" 1112 * and "none" and the APIs are underdefined in this area for auth. 1113 */ 1114 if (proto_num == IPSEC_PROTO_AH) { 1115 if (alg_value == SADB_AALG_NONE) 1116 return ("none"); 1117 if (alg_value == SADB_AALG_ANY) 1118 return ("any"); 1119 } 1120 1121 alg = getipsecalgbynum(alg_value, proto_num, NULL); 1122 if (alg != NULL) { 1123 (void) strlcpy(numprint, alg->a_names[0], sizeof (numprint)); 1124 freeipsecalgent(alg); 1125 } else { 1126 (void) snprintf(numprint, sizeof (numprint), "%d", alg_value); 1127 } 1128 1129 return (numprint); 1130 } 1131 1132 static uint_t 1133 parsealg(char *algname, int proto_num) 1134 { 1135 struct ipsecalgent *alg; 1136 ulong_t invalue; 1137 1138 if (algname == NULL) { 1139 (void) fprintf(stderr, "ifconfig: Unexpected end of command " 1140 "line.\n"); 1141 exit(1); 1142 } 1143 1144 /* 1145 * Special-case "none" and "any". 1146 * Use strcasecmp because its length is bounded. 1147 */ 1148 if (strcasecmp("none", algname) == 0) { 1149 return ((proto_num == IPSEC_PROTO_ESP) ? 1150 NO_ESP_EALG : NO_ESP_AALG); 1151 } 1152 if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH)) 1153 return (SADB_AALG_ANY); 1154 1155 alg = getipsecalgbyname(algname, proto_num, NULL); 1156 if (alg != NULL) { 1157 invalue = alg->a_alg_num; 1158 freeipsecalgent(alg); 1159 return ((uint_t)invalue); 1160 } 1161 1162 /* 1163 * Since algorithms can be loaded during kernel run-time, check for 1164 * numeric algorithm values too. 1165 */ 1166 invalue = parsenum(algname); 1167 if ((invalue & (ulong_t)0xff) == invalue) 1168 return ((uint_t)invalue); 1169 1170 (void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n", 1171 (proto_num == IPSEC_PROTO_ESP) ? 1172 "Encryption" : "Authentication", algname); 1173 exit(1); 1174 /* NOTREACHED */ 1175 } 1176 1177 /* 1178 * Actual ifconfig functions to set tunnel security properties. 1179 */ 1180 1181 enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG }; 1182 1183 static int 1184 set_tun_algs(int which_alg, int alg) 1185 { 1186 boolean_t encr_alg_set = _B_FALSE; 1187 iptun_params_t params; 1188 dladm_status_t status; 1189 ipsec_req_t *ipsr; 1190 1191 if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 1192 ¶ms.iptun_param_linkid)) != DLADM_STATUS_OK) 1193 goto done; 1194 1195 status = dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE); 1196 if (status != DLADM_STATUS_OK) 1197 goto done; 1198 1199 ipsr = ¶ms.iptun_param_secinfo; 1200 1201 /* 1202 * If I'm just starting off this ifconfig, I want a clean slate, 1203 * otherwise, I've captured the current tunnel security settings. 1204 * In the case of continuation, I merely add to the settings. 1205 */ 1206 if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO)) 1207 (void) memset(ipsr, 0, sizeof (*ipsr)); 1208 1209 /* We're only modifying the IPsec information */ 1210 params.iptun_param_flags = IPTUN_PARAM_SECINFO; 1211 1212 switch (which_alg) { 1213 case ESP_ENCR_ALG: 1214 if (alg == NO_ESP_EALG) { 1215 if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE) 1216 ipsr->ipsr_esp_req = 0; 1217 ipsr->ipsr_esp_alg = SADB_EALG_NONE; 1218 1219 /* Let the user specify NULL encryption implicitly. */ 1220 if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) { 1221 encr_alg_set = _B_TRUE; 1222 ipsr->ipsr_esp_alg = SADB_EALG_NULL; 1223 } 1224 } else { 1225 encr_alg_set = _B_TRUE; 1226 ipsr->ipsr_esp_req = 1227 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 1228 ipsr->ipsr_esp_alg = alg; 1229 } 1230 break; 1231 case ESP_AUTH_ALG: 1232 if (alg == NO_ESP_AALG) { 1233 if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE || 1234 ipsr->ipsr_esp_alg == SADB_EALG_NULL) && 1235 !encr_alg_set) 1236 ipsr->ipsr_esp_req = 0; 1237 ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE; 1238 } else { 1239 ipsr->ipsr_esp_req = 1240 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 1241 ipsr->ipsr_esp_auth_alg = alg; 1242 1243 /* Let the user specify NULL encryption implicitly. */ 1244 if (ipsr->ipsr_esp_alg == SADB_EALG_NONE && 1245 !encr_alg_set) 1246 ipsr->ipsr_esp_alg = SADB_EALG_NULL; 1247 } 1248 break; 1249 case AH_AUTH_ALG: 1250 if (alg == NO_AH_AALG) { 1251 ipsr->ipsr_ah_req = 0; 1252 ipsr->ipsr_auth_alg = SADB_AALG_NONE; 1253 } else { 1254 ipsr->ipsr_ah_req = 1255 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 1256 ipsr->ipsr_auth_alg = alg; 1257 } 1258 break; 1259 /* Will never hit DEFAULT */ 1260 } 1261 1262 status = dladm_iptun_modify(dlh, ¶ms, DLADM_OPT_ACTIVE); 1263 1264 done: 1265 if (status != DLADM_STATUS_OK) 1266 dladmerr_exit(status, name); 1267 else { 1268 ipsec_policy_set = _B_TRUE; 1269 if ((ipsr->ipsr_esp_req != 0 && 1270 ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) || 1271 (ipsr->ipsr_ah_req != 0 && 1272 ipsr->ipsr_auth_alg != SADB_AALG_NONE)) 1273 ipsec_auth_covered = _B_TRUE; 1274 } 1275 return (0); 1276 } 1277 1278 /* ARGSUSED */ 1279 static int 1280 set_tun_esp_encr_alg(char *addr, int64_t param) 1281 { 1282 return (set_tun_algs(ESP_ENCR_ALG, 1283 parsealg(addr, IPSEC_PROTO_ESP))); 1284 } 1285 1286 /* ARGSUSED */ 1287 static int 1288 set_tun_esp_auth_alg(char *addr, int64_t param) 1289 { 1290 return (set_tun_algs(ESP_AUTH_ALG, 1291 parsealg(addr, IPSEC_PROTO_AH))); 1292 } 1293 1294 /* ARGSUSED */ 1295 static int 1296 set_tun_ah_alg(char *addr, int64_t param) 1297 { 1298 return (set_tun_algs(AH_AUTH_ALG, 1299 parsealg(addr, IPSEC_PROTO_AH))); 1300 } 1301 1302 /* ARGSUSED */ 1303 static int 1304 setifrevarp(char *arg, int64_t param) 1305 { 1306 struct sockaddr_in laddr; 1307 1308 if (afp->af_af == AF_INET6) { 1309 (void) fprintf(stderr, 1310 "ifconfig: revarp not possible on IPv6 interface %s\n", 1311 name); 1312 exit(1); 1313 } 1314 if (doifrevarp(name, &laddr)) { 1315 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1316 laddr.sin_family = AF_INET; 1317 (void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr)); 1318 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 1319 Perror0_exit("SIOCSLIFADDR"); 1320 } 1321 return (0); 1322 } 1323 1324 /* ARGSUSED */ 1325 static int 1326 setifsubnet(char *addr, int64_t param) 1327 { 1328 int prefixlen = 0; 1329 struct sockaddr_storage subnet; 1330 1331 (*afp->af_getaddr)(addr, &subnet, &prefixlen); 1332 1333 switch (prefixlen) { 1334 case NO_PREFIX: 1335 (void) fprintf(stderr, 1336 "ifconfig: Missing prefix length in subnet %s\n", addr); 1337 exit(1); 1338 /* NOTREACHED */ 1339 case BAD_ADDR: 1340 (void) fprintf(stderr, 1341 "ifconfig: Bad prefix length in %s\n", addr); 1342 exit(1); 1343 default: 1344 break; 1345 } 1346 1347 lifr.lifr_addr = subnet; 1348 lifr.lifr_addrlen = prefixlen; 1349 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1350 if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0) 1351 Perror0_exit("SIOCSLIFSUBNET"); 1352 1353 return (0); 1354 } 1355 1356 /* ARGSUSED */ 1357 static int 1358 setifnetmask(char *addr, int64_t param) 1359 { 1360 struct sockaddr_in netmask; 1361 1362 assert(afp->af_af != AF_INET6); 1363 1364 if (strcmp(addr, "+") == 0) { 1365 if (!in_getmask(&netmask, _B_FALSE)) 1366 return (0); 1367 (void) printf("Setting netmask of %s to %s\n", name, 1368 inet_ntoa(netmask.sin_addr)); 1369 } else { 1370 in_getaddr(addr, (struct sockaddr *)&netmask, NULL); 1371 } 1372 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1373 (void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask)); 1374 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 1375 Perror0_exit("SIOCSLIFNETMASK"); 1376 return (0); 1377 } 1378 1379 /* 1380 * Parse '/<n>' as a netmask. 1381 */ 1382 /* ARGSUSED */ 1383 static int 1384 setifprefixlen(char *addr, int64_t param) 1385 { 1386 int prefixlen; 1387 int af = afp->af_af; 1388 1389 prefixlen = in_getprefixlen(addr, _B_TRUE, 1390 (af == AF_INET) ? IP_ABITS : IPV6_ABITS); 1391 if (prefixlen < 0) { 1392 (void) fprintf(stderr, 1393 "ifconfig: Bad prefix length in %s\n", addr); 1394 exit(1); 1395 } 1396 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 1397 lifr.lifr_addr.ss_family = af; 1398 if (af == AF_INET6) { 1399 struct sockaddr_in6 *sin6; 1400 1401 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 1402 if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 1403 (uchar_t *)&sin6->sin6_addr)) { 1404 (void) fprintf(stderr, "ifconfig: " 1405 "Bad prefix length: %d\n", 1406 prefixlen); 1407 exit(1); 1408 } 1409 } else if (af == AF_INET) { 1410 struct sockaddr_in *sin; 1411 1412 sin = (struct sockaddr_in *)&lifr.lifr_addr; 1413 if (!in_prefixlentomask(prefixlen, IP_ABITS, 1414 (uchar_t *)&sin->sin_addr)) { 1415 (void) fprintf(stderr, "ifconfig: " 1416 "Bad prefix length: %d\n", 1417 prefixlen); 1418 exit(1); 1419 } 1420 } else { 1421 (void) fprintf(stderr, "ifconfig: setting prefix only supported" 1422 " for address family inet or inet6\n"); 1423 exit(1); 1424 } 1425 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1426 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 1427 Perror0_exit("SIOCSLIFNETMASK"); 1428 return (0); 1429 } 1430 1431 /* ARGSUSED */ 1432 static int 1433 setifbroadaddr(char *addr, int64_t param) 1434 { 1435 struct sockaddr_in broadaddr; 1436 1437 assert(afp->af_af != AF_INET6); 1438 1439 if (strcmp(addr, "+") == 0) { 1440 /* 1441 * This doesn't set the broadcast address at all. Rather, it 1442 * gets, then sets the interface's address, relying on the fact 1443 * that resetting the address will reset the broadcast address. 1444 */ 1445 (void) strncpy(lifr.lifr_name, name, 1446 sizeof (lifr.lifr_name)); 1447 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 1448 if (errno != EADDRNOTAVAIL) 1449 Perror0_exit("SIOCGLIFADDR"); 1450 return (0); 1451 } 1452 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 1453 Perror0_exit("SIOCGLIFADDR"); 1454 1455 return (0); 1456 } 1457 in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL); 1458 1459 (void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr)); 1460 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1461 if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0) 1462 Perror0_exit("SIOCSLIFBRDADDR"); 1463 return (0); 1464 } 1465 1466 /* 1467 * set interface destination address 1468 */ 1469 /* ARGSUSED */ 1470 static int 1471 setifdstaddr(char *addr, int64_t param) 1472 { 1473 (*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL); 1474 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1475 if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0) 1476 Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR"); 1477 return (0); 1478 } 1479 1480 /* ARGSUSED */ 1481 static int 1482 setifflags(char *val, int64_t value) 1483 { 1484 struct lifreq lifrl; /* local lifreq struct */ 1485 boolean_t bringup = _B_FALSE; 1486 1487 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1488 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) 1489 Perror0_exit("setifflags: SIOCGLIFFLAGS"); 1490 1491 if (value < 0) { 1492 value = -value; 1493 1494 if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) { 1495 /* 1496 * The kernel does not allow administratively up test 1497 * addresses to be converted to data addresses. Bring 1498 * the address down first, then bring it up after it's 1499 * been converted to a data address. 1500 */ 1501 lifr.lifr_flags &= ~IFF_UP; 1502 (void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr); 1503 bringup = _B_TRUE; 1504 } 1505 1506 lifr.lifr_flags &= ~value; 1507 if ((value & (IFF_UP | IFF_NOFAILOVER)) && 1508 (lifr.lifr_flags & IFF_DUPLICATE)) { 1509 /* 1510 * If the user is trying to mark an interface with a 1511 * duplicate address as "down," or convert a duplicate 1512 * test address to a data address, then fetch the 1513 * address and set it. This will cause IP to clear 1514 * the IFF_DUPLICATE flag and stop the automatic 1515 * recovery timer. 1516 */ 1517 value = lifr.lifr_flags; 1518 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) 1519 (void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr); 1520 lifr.lifr_flags = value; 1521 } 1522 } else { 1523 lifr.lifr_flags |= value; 1524 } 1525 1526 /* 1527 * If we're about to bring up an underlying physical IPv6 interface in 1528 * an IPMP group, ensure the IPv6 IPMP interface is also up. This is 1529 * for backward compatibility with legacy configurations in which 1530 * there are no explicit hostname files for IPMP interfaces. (For 1531 * IPv4, this is automatically handled by the kernel when migrating 1532 * the underlying interface's data address to the IPMP interface.) 1533 */ 1534 (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 1535 1536 if (lifnum(lifr.lifr_name) == 0 && 1537 (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) && 1538 ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 && 1539 lifrl.lifr_groupname[0] != '\0') { 1540 lifgroupinfo_t lifgr; 1541 1542 (void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname, 1543 LIFGRNAMSIZ); 1544 if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) 1545 Perror0_exit("setifflags: SIOCGLIFGROUPINFO"); 1546 1547 (void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ); 1548 if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1) 1549 Perror0_exit("setifflags: SIOCGLIFFLAGS"); 1550 if (!(lifrl.lifr_flags & IFF_UP)) { 1551 lifrl.lifr_flags |= IFF_UP; 1552 if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1) 1553 Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1554 } 1555 } 1556 1557 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1558 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 1559 Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1560 1561 if (bringup) { 1562 lifr.lifr_flags |= IFF_UP; 1563 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 1564 Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP"); 1565 } 1566 1567 return (0); 1568 } 1569 1570 /* ARGSUSED */ 1571 static int 1572 setifmetric(char *val, int64_t param) 1573 { 1574 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1575 lifr.lifr_metric = atoi(val); 1576 if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0) 1577 Perror0_exit("setifmetric: SIOCSLIFMETRIC"); 1578 return (0); 1579 } 1580 1581 /* ARGSUSED */ 1582 static int 1583 setifmtu(char *val, int64_t param) 1584 { 1585 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1586 lifr.lifr_mtu = atoi(val); 1587 if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0) 1588 Perror0_exit("setifmtu: SIOCSLIFMTU"); 1589 return (0); 1590 } 1591 1592 /* ARGSUSED */ 1593 static int 1594 setifindex(char *val, int64_t param) 1595 { 1596 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1597 lifr.lifr_index = atoi(val); 1598 if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0) 1599 Perror0_exit("setifindex: SIOCSLIFINDEX"); 1600 return (0); 1601 } 1602 1603 /* ARGSUSED */ 1604 static void 1605 notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg) 1606 { 1607 } 1608 1609 /* ARGSUSED */ 1610 static int 1611 setifether(char *addr, int64_t param) 1612 { 1613 uchar_t *hwaddr; 1614 int hwaddrlen; 1615 int retval; 1616 ifaddrlistx_t *ifaddrp, *ifaddrs = NULL; 1617 dlpi_handle_t dh; 1618 dlpi_notifyid_t id; 1619 1620 if (addr == NULL) { 1621 ifstatus(name); 1622 print_ifether(name); 1623 return (0); 1624 } 1625 1626 /* 1627 * if the IP interface in the arguments is a logical 1628 * interface, exit with an error now. 1629 */ 1630 if (strchr(name, ':') != NULL) { 1631 (void) fprintf(stderr, "ifconfig: cannot change" 1632 " ethernet address of a logical interface\n"); 1633 exit(1); 1634 } 1635 1636 if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) { 1637 if (hwaddrlen == -1) 1638 (void) fprintf(stderr, 1639 "ifconfig: bad ethernet address\n"); 1640 else 1641 (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 1642 exit(1); 1643 } 1644 1645 if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS) 1646 Perrdlpi_exit("cannot dlpi_open() link", name, retval); 1647 1648 retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id); 1649 if (retval == DLPI_SUCCESS) { 1650 (void) dlpi_disabnotify(dh, id, NULL); 1651 } else { 1652 /* 1653 * This link does not support DL_NOTE_PHYS_ADDR: bring down 1654 * all of the addresses to flush the old hardware address 1655 * information out of IP. 1656 * 1657 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is 1658 * more than an optimization: in.mpathd will set IFF_OFFLINE 1659 * if it's notified and the new address is a duplicate of 1660 * another in the group -- but the flags manipulation in 1661 * ifaddr_{down,up}() cannot be atomic and thus might clobber 1662 * IFF_OFFLINE, confusing in.mpathd. 1663 */ 1664 if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1) 1665 Perror2_exit(name, "cannot get address list"); 1666 1667 ifaddrp = ifaddrs; 1668 for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1669 if (!ifaddr_down(ifaddrp)) { 1670 Perror2_exit(ifaddrp->ia_name, 1671 "cannot bring down"); 1672 } 1673 } 1674 } 1675 1676 /* 1677 * Change the hardware address. 1678 */ 1679 retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen); 1680 if (retval != DLPI_SUCCESS) { 1681 (void) fprintf(stderr, 1682 "ifconfig: failed setting mac address on %s\n", name); 1683 } 1684 dlpi_close(dh); 1685 1686 /* 1687 * If any addresses were brought down before changing the hardware 1688 * address, bring them up again. 1689 */ 1690 for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1691 if (!ifaddr_up(ifaddrp)) 1692 Perror2_exit(ifaddrp->ia_name, "cannot bring up"); 1693 } 1694 ifaddrlistx_free(ifaddrs); 1695 1696 return (0); 1697 } 1698 1699 /* 1700 * Print an interface's Ethernet address, if it has one. 1701 */ 1702 static void 1703 print_ifether(const char *ifname) 1704 { 1705 int fd; 1706 1707 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1708 1709 fd = socket(AF_INET, SOCK_DGRAM, 0); 1710 if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 1711 /* 1712 * It's possible the interface is only configured for 1713 * IPv6; check again with AF_INET6. 1714 */ 1715 (void) close(fd); 1716 fd = socket(AF_INET6, SOCK_DGRAM, 0); 1717 if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 1718 (void) close(fd); 1719 return; 1720 } 1721 } 1722 (void) close(fd); 1723 1724 /* VNI and IPMP interfaces don't have MAC addresses */ 1725 if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) 1726 return; 1727 1728 /* IP tunnels also don't have Ethernet-like MAC addresses */ 1729 if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) == 1730 DLADM_STATUS_OK) 1731 return; 1732 1733 dlpi_print_address(ifname); 1734 } 1735 1736 /* 1737 * static int find_all_interfaces(struct lifconf *lifcp, char **buf, 1738 * int64_t lifc_flags) 1739 * 1740 * It finds all active data links. 1741 * 1742 * It takes in input a pointer to struct lifconf to receive interfaces 1743 * informations, a **char to hold allocated buffer, and a lifc_flags. 1744 * 1745 * Return values: 1746 * 0 = everything OK 1747 * -1 = problem 1748 */ 1749 static int 1750 find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags) 1751 { 1752 unsigned bufsize; 1753 int n; 1754 ni_t *nip; 1755 struct lifreq *lifrp; 1756 dladm_status_t status; 1757 1758 if (!dlh_opened) { 1759 status = ifconfig_dladm_open(NULL, 0, NULL); 1760 if (status != DLADM_STATUS_OK) 1761 dladmerr_exit(status, "unable to open dladm handle"); 1762 } 1763 1764 (void) dlpi_walk(ni_entry, dlh, 0); 1765 1766 /* Now, translate the linked list into a struct lifreq buffer */ 1767 if (num_ni == 0) { 1768 lifcp->lifc_family = AF_UNSPEC; 1769 lifcp->lifc_flags = lifc_flags; 1770 lifcp->lifc_len = 0; 1771 lifcp->lifc_buf = NULL; 1772 return (0); 1773 } 1774 1775 bufsize = num_ni * sizeof (struct lifreq); 1776 if ((*buf = malloc(bufsize)) == NULL) 1777 Perror0_exit("find_all_interfaces: malloc failed"); 1778 1779 lifcp->lifc_family = AF_UNSPEC; 1780 lifcp->lifc_flags = lifc_flags; 1781 lifcp->lifc_len = bufsize; 1782 lifcp->lifc_buf = *buf; 1783 1784 for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) { 1785 nip = ni_list; 1786 (void) strncpy(lifrp->lifr_name, nip->ni_name, 1787 sizeof (lifr.lifr_name)); 1788 ni_list = nip->ni_next; 1789 free(nip); 1790 } 1791 return (0); 1792 } 1793 1794 /* 1795 * Create the next unused logical interface using the original name 1796 * and assign the address (and mask if '/<n>' is part of the address). 1797 * Use the new logical interface for subsequent subcommands by updating 1798 * the name variable. 1799 * 1800 * This allows syntax like: 1801 * ifconfig le0 addif 109.106.86.130 netmask + up \ 1802 * addif 109.106.86.131 netmask + up 1803 */ 1804 /* ARGSUSED */ 1805 static int 1806 addif(char *str, int64_t param) 1807 { 1808 int prefixlen = 0; 1809 struct sockaddr_storage laddr; 1810 struct sockaddr_storage mask; 1811 ipadm_status_t istatus; 1812 char cidraddr[BUFSIZ]; 1813 1814 (void) strncpy(name, origname, sizeof (name)); 1815 1816 if (strchr(name, ':') != NULL) { 1817 (void) fprintf(stderr, 1818 "ifconfig: addif: bad physical interface name %s\n", 1819 name); 1820 exit(1); 1821 } 1822 1823 /* 1824 * clear so parser will interpret next address as source followed 1825 * by possible dest 1826 */ 1827 setaddr = 0; 1828 (*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen); 1829 1830 switch (prefixlen) { 1831 case NO_PREFIX: 1832 /* Nothing there - ok */ 1833 break; 1834 case BAD_ADDR: 1835 (void) fprintf(stderr, 1836 "ifconfig: Bad prefix length in %s\n", str); 1837 exit(1); 1838 default: 1839 (void) memset(&mask, 0, sizeof (mask)); 1840 mask.ss_family = afp->af_af; 1841 if (afp->af_af == AF_INET6) { 1842 struct sockaddr_in6 *sin6; 1843 sin6 = (struct sockaddr_in6 *)&mask; 1844 if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 1845 (uchar_t *)&sin6->sin6_addr)) { 1846 (void) fprintf(stderr, "ifconfig: " 1847 "Bad prefix length: %d\n", 1848 prefixlen); 1849 exit(1); 1850 } 1851 } else { 1852 struct sockaddr_in *sin; 1853 1854 sin = (struct sockaddr_in *)&mask; 1855 if (!in_prefixlentomask(prefixlen, IP_ABITS, 1856 (uchar_t *)&sin->sin_addr)) { 1857 (void) fprintf(stderr, "ifconfig: " 1858 "Bad prefix length: %d\n", 1859 prefixlen); 1860 exit(1); 1861 } 1862 } 1863 g_netmask_set = G_NETMASK_NIL; 1864 break; 1865 } 1866 1867 /* 1868 * This is a "hack" to get around the problem of SIOCLIFADDIF. The 1869 * problem is that this ioctl does not include the netmask when 1870 * adding a logical interface. This is the same problem described 1871 * in the ifconfig() comments. To get around this problem, we first 1872 * add the logical interface with a 0 address. After that, we set 1873 * the netmask if provided. Finally we set the interface address. 1874 */ 1875 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1876 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 1877 1878 /* Note: no need to do DAD here since the interface isn't up yet. */ 1879 1880 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) 1881 Perror0_exit("addif: SIOCLIFADDIF"); 1882 1883 (void) printf("Created new logical interface %s\n", 1884 lifr.lifr_name); 1885 (void) strncpy(name, lifr.lifr_name, sizeof (name)); 1886 1887 /* 1888 * Check and see if any "netmask" command is used and perform the 1889 * necessary operation. 1890 */ 1891 set_mask_lifreq(&lifr, &laddr, &mask); 1892 1893 /* This check is temporary until libipadm supports IPMP interfaces. */ 1894 if (ifconfig_use_libipadm(s, name)) { 1895 /* 1896 * We added the logical interface above before calling 1897 * ipadm_create_addr(), because, with IPH_LEGACY, we need 1898 * to do an addif for `ifconfig ce0 addif <addr>' but not for 1899 * `ifconfig ce0 <addr>'. libipadm does not have a flag to 1900 * to differentiate between these two cases. To keep it simple, 1901 * we always create the logical interface and pass it to 1902 * libipadm instead of requiring libipadm to addif for some 1903 * cases and not do addif for other cases. 1904 */ 1905 istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 1906 &ipaddr); 1907 if (istatus != IPADM_SUCCESS) 1908 ipadmerr_exit(istatus, "addif"); 1909 1910 if (strchr(str, '/') == NULL) { 1911 /* 1912 * lifr.lifr_addr, which is updated by set_mask_lifreq() 1913 * will contain the right mask to use. 1914 */ 1915 prefixlen = mask2plen(&lifr.lifr_addr); 1916 (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 1917 str, prefixlen); 1918 str = cidraddr; 1919 } 1920 istatus = ipadm_set_addr(ipaddr, str, af); 1921 if (istatus != IPADM_SUCCESS) 1922 ipadmerr_exit(istatus, "could not set address"); 1923 setaddr++; 1924 /* 1925 * address will be set by the parser after nextarg 1926 * has been scanned 1927 */ 1928 return (0); 1929 } 1930 1931 /* 1932 * Only set the netmask if "netmask" command is used or a prefix is 1933 * provided. 1934 */ 1935 if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) { 1936 /* 1937 * lifr.lifr_addr already contains netmask from 1938 * set_mask_lifreq(). 1939 */ 1940 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 1941 Perror0_exit("addif: SIOCSLIFNETMASK"); 1942 } 1943 1944 /* Finally, we set the interface address. */ 1945 lifr.lifr_addr = laddr; 1946 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 1947 Perror0_exit("SIOCSLIFADDR"); 1948 1949 /* 1950 * let parser know we got a source. 1951 * Next address, if given, should be dest 1952 */ 1953 setaddr++; 1954 return (0); 1955 } 1956 1957 /* 1958 * Remove a logical interface based on its IP address. Unlike addif 1959 * there is no '/<n>' here. 1960 * Verifies that the interface is down before it is removed. 1961 */ 1962 /* ARGSUSED */ 1963 static int 1964 removeif(char *str, int64_t param) 1965 { 1966 struct sockaddr_storage laddr; 1967 ipadm_status_t istatus; 1968 ipadm_addr_info_t *ainfo, *ainfop; 1969 1970 if (strchr(name, ':') != NULL) { 1971 (void) fprintf(stderr, 1972 "ifconfig: removeif: bad physical interface name %s\n", 1973 name); 1974 exit(1); 1975 } 1976 1977 (*afp->af_getaddr)(str, &laddr, NULL); 1978 1979 /* 1980 * Following check is temporary until libipadm supports 1981 * IPMP interfaces. 1982 */ 1983 if (!ifconfig_use_libipadm(s, name)) 1984 goto delete; 1985 1986 /* 1987 * Get all addresses and search this address among the active 1988 * addresses. If an address object was found, delete using 1989 * ipadm_delete_addr(). 1990 */ 1991 istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT); 1992 if (istatus != IPADM_SUCCESS) 1993 ipadmerr_exit(istatus, "removeif"); 1994 1995 for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) 1996 if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr)) 1997 break; 1998 1999 if (ainfop != NULL) { 2000 if (strchr(ainfop->ia_ifa.ifa_name, ':') == NULL) { 2001 (void) fprintf(stderr, 2002 "ifconfig: removeif: cannot remove interface: %s\n", 2003 name); 2004 exit(1); 2005 } 2006 if (ainfop->ia_aobjname[0] != '\0') { 2007 istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname, 2008 IPADM_OPT_ACTIVE); 2009 if (istatus != IPADM_SUCCESS) { 2010 ipadmerr_exit(istatus, 2011 "could not delete address"); 2012 } 2013 ipadm_free_addr_info(ainfo); 2014 return (0); 2015 } 2016 } 2017 ipadm_free_addr_info(ainfo); 2018 2019 delete: 2020 /* 2021 * An address object for this address was not found in ipadm. 2022 * Delete with SIOCLIFREMOVEIF. 2023 */ 2024 lifr.lifr_addr = laddr; 2025 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2026 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) { 2027 if (errno == EBUSY) { 2028 /* This can only happen if ipif_id = 0 */ 2029 (void) fprintf(stderr, 2030 "ifconfig: removeif: cannot remove interface: %s\n", 2031 name); 2032 exit(1); 2033 } 2034 Perror0_exit("removeif: SIOCLIFREMOVEIF"); 2035 } 2036 return (0); 2037 } 2038 2039 /* 2040 * Set the address token for IPv6. 2041 */ 2042 /* ARGSUSED */ 2043 static int 2044 setiftoken(char *addr, int64_t param) 2045 { 2046 int prefixlen = 0; 2047 struct sockaddr_in6 token; 2048 2049 in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen); 2050 switch (prefixlen) { 2051 case NO_PREFIX: 2052 (void) fprintf(stderr, 2053 "ifconfig: Missing prefix length in subnet %s\n", addr); 2054 exit(1); 2055 /* NOTREACHED */ 2056 case BAD_ADDR: 2057 (void) fprintf(stderr, 2058 "ifconfig: Bad prefix length in %s\n", addr); 2059 exit(1); 2060 default: 2061 break; 2062 } 2063 (void) memcpy(&lifr.lifr_addr, &token, sizeof (token)); 2064 lifr.lifr_addrlen = prefixlen; 2065 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2066 if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0) { 2067 Perror0_exit("setiftoken: SIOCSLIFTOKEN"); 2068 } 2069 return (0); 2070 } 2071 2072 /* ARGSUSED */ 2073 static int 2074 setifgroupname(char *grname, int64_t param) 2075 { 2076 lifgroupinfo_t lifgr; 2077 struct lifreq lifrl; 2078 ifaddrlistx_t *ifaddrp, *nextifaddrp; 2079 ifaddrlistx_t *ifaddrs = NULL, *downaddrs = NULL; 2080 int af; 2081 2082 if (debug) { 2083 (void) printf("Setting groupname %s on interface %s\n", 2084 grname, name); 2085 } 2086 2087 (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 2088 (void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ); 2089 2090 while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2091 switch (errno) { 2092 case ENOENT: 2093 /* 2094 * The group doesn't yet exist; create it and repeat. 2095 */ 2096 af = afp->af_af; 2097 if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) { 2098 if (errno == EEXIST) 2099 continue; 2100 2101 Perror2(grname, "cannot create IPMP group"); 2102 goto fail; 2103 } 2104 continue; 2105 2106 case EALREADY: 2107 /* 2108 * The interface is already in another group; must 2109 * remove existing membership first. 2110 */ 2111 lifrl.lifr_groupname[0] = '\0'; 2112 if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2113 Perror2(name, "cannot remove existing " 2114 "IPMP group membership"); 2115 goto fail; 2116 } 2117 (void) strlcpy(lifrl.lifr_groupname, grname, 2118 LIFGRNAMSIZ); 2119 continue; 2120 2121 case EAFNOSUPPORT: 2122 /* 2123 * The group exists, but it's not configured with the 2124 * address families the interface needs. Since only 2125 * two address families are currently supported, just 2126 * configure the "other" address family. Note that we 2127 * may race with group deletion or creation by another 2128 * process (ENOENT or EEXIST); in such cases we repeat 2129 * our original SIOCSLIFGROUPNAME. 2130 */ 2131 (void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ); 2132 if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) { 2133 if (errno == ENOENT) 2134 continue; 2135 2136 Perror2(grname, "SIOCGLIFGROUPINFO"); 2137 goto fail; 2138 } 2139 2140 af = lifgr.gi_v4 ? AF_INET6 : AF_INET; 2141 if (create_ipmp(grname, af, lifgr.gi_grifname, 2142 _B_TRUE) == -1) { 2143 if (errno == EEXIST) 2144 continue; 2145 2146 Perror2(grname, "cannot configure IPMP group"); 2147 goto fail; 2148 } 2149 continue; 2150 2151 case EADDRINUSE: 2152 /* 2153 * Some addresses are in-use (or under control of DAD). 2154 * Bring them down and retry the group join operation. 2155 * We will bring them back up after the interface has 2156 * been placed in the group. 2157 */ 2158 if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE, 2159 0, &ifaddrs) == -1) { 2160 Perror2(grname, "cannot get address list"); 2161 goto fail; 2162 } 2163 2164 ifaddrp = ifaddrs; 2165 for (; ifaddrp != NULL; ifaddrp = nextifaddrp) { 2166 if (!ifaddr_down(ifaddrp)) { 2167 ifaddrs = ifaddrp; 2168 goto fail; 2169 } 2170 nextifaddrp = ifaddrp->ia_next; 2171 ifaddrp->ia_next = downaddrs; 2172 downaddrs = ifaddrp; 2173 } 2174 ifaddrs = NULL; 2175 continue; 2176 2177 case EADDRNOTAVAIL: { 2178 /* 2179 * Some data addresses are under application control. 2180 * For some of these (e.g., ADDRCONF), the application 2181 * should remove the address, in which case we retry a 2182 * few times (since the application's action is not 2183 * atomic with respect to us) before bailing out and 2184 * informing the user. 2185 */ 2186 int ntries, nappaddr = 0; 2187 const if_appflags_t *iap = if_appflags_tbl; 2188 2189 for (; iap->ia_app != NULL; iap++) { 2190 ntries = 0; 2191 again: 2192 if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag, 2193 IFF_NOFAILOVER, &ifaddrs) == -1) { 2194 (void) fprintf(stderr, "ifconfig: %s: " 2195 "cannot get data addresses managed " 2196 "by %s\n", lifrl.lifr_name, 2197 iap->ia_app); 2198 goto fail; 2199 } 2200 2201 if (ifaddrs == NULL) 2202 continue; 2203 2204 ifaddrlistx_free(ifaddrs); 2205 ifaddrs = NULL; 2206 2207 if (++ntries < iap->ia_tries) { 2208 (void) poll(NULL, 0, 100); 2209 goto again; 2210 } 2211 2212 (void) fprintf(stderr, "ifconfig: cannot join " 2213 "IPMP group: %s has data addresses managed " 2214 "by %s\n", lifrl.lifr_name, iap->ia_app); 2215 nappaddr++; 2216 } 2217 if (nappaddr > 0) 2218 goto fail; 2219 continue; 2220 } 2221 default: 2222 Perror2(name, "SIOCSLIFGROUPNAME"); 2223 goto fail; 2224 } 2225 } 2226 2227 /* 2228 * If the interface being moved is under the control of `ipmgmtd(1M)' 2229 * dameon then we should inform the daemon about this move, so that 2230 * the daemon can delete the state associated with this interface. 2231 * 2232 * This workaround is needed until the IPMP support in ipadm(1M). 2233 */ 2234 ipadm_if_move(iph, name); 2235 2236 /* 2237 * If there were addresses that we had to bring down, it's time to 2238 * bring them up again. As part of bringing them up, the kernel will 2239 * automatically move them to the new IPMP interface. 2240 */ 2241 for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2242 if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2243 (void) fprintf(stderr, "ifconfig: cannot bring back up " 2244 "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2245 } 2246 } 2247 ifaddrlistx_free(downaddrs); 2248 return (0); 2249 fail: 2250 /* 2251 * Attempt to bring back up any interfaces that we downed. 2252 */ 2253 for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2254 if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2255 (void) fprintf(stderr, "ifconfig: cannot bring back up " 2256 "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2257 } 2258 } 2259 ifaddrlistx_free(downaddrs); 2260 ifaddrlistx_free(ifaddrs); 2261 2262 /* 2263 * We'd return -1, but foreachinterface() doesn't propagate the error 2264 * into the exit status, so we're forced to explicitly exit(). 2265 */ 2266 exit(1); 2267 /* NOTREACHED */ 2268 } 2269 2270 static boolean_t 2271 modcheck(const char *ifname) 2272 { 2273 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2274 2275 if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) { 2276 Perror0("SIOCGLIFFLAGS"); 2277 return (_B_FALSE); 2278 } 2279 2280 if (lifr.lifr_flags & IFF_IPMP) { 2281 (void) fprintf(stderr, "ifconfig: %s: module operations not" 2282 " supported on IPMP interfaces\n", ifname); 2283 return (_B_FALSE); 2284 } 2285 if (lifr.lifr_flags & IFF_VIRTUAL) { 2286 (void) fprintf(stderr, "ifconfig: %s: module operations not" 2287 " supported on virtual IP interfaces\n", ifname); 2288 return (_B_FALSE); 2289 } 2290 return (_B_TRUE); 2291 } 2292 2293 /* 2294 * To list all the modules above a given network interface. 2295 */ 2296 /* ARGSUSED */ 2297 static int 2298 modlist(char *null, int64_t param) 2299 { 2300 int muxid_fd; 2301 int muxfd; 2302 int ipfd_lowstr; 2303 int arpfd_lowstr; 2304 int num_mods; 2305 int i; 2306 struct str_list strlist; 2307 int orig_arpid; 2308 2309 /* 2310 * We'd return -1, but foreachinterface() doesn't propagate the error 2311 * into the exit status, so we're forced to explicitly exit(). 2312 */ 2313 if (!modcheck(name)) 2314 exit(1); 2315 2316 if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 2317 &orig_arpid) < 0) { 2318 return (-1); 2319 } 2320 if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) { 2321 Perror0("cannot I_LIST to get the number of modules"); 2322 } else { 2323 if (debug > 0) { 2324 (void) printf("Listing (%d) modules above %s\n", 2325 num_mods, name); 2326 } 2327 2328 strlist.sl_nmods = num_mods; 2329 strlist.sl_modlist = malloc(sizeof (struct str_mlist) * 2330 num_mods); 2331 if (strlist.sl_modlist == NULL) { 2332 Perror0("cannot malloc"); 2333 } else { 2334 if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) { 2335 Perror0("cannot I_LIST for module names"); 2336 } else { 2337 for (i = 0; i < strlist.sl_nmods; i++) { 2338 (void) printf("%d %s\n", i, 2339 strlist.sl_modlist[i].l_name); 2340 } 2341 } 2342 free(strlist.sl_modlist); 2343 } 2344 } 2345 return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2346 orig_arpid)); 2347 } 2348 2349 #define MODINSERT_OP 'i' 2350 #define MODREMOVE_OP 'r' 2351 2352 /* 2353 * To insert a module to the stream of the interface. It is just a 2354 * wrapper. The real function is modop(). 2355 */ 2356 /* ARGSUSED */ 2357 static int 2358 modinsert(char *arg, int64_t param) 2359 { 2360 return (modop(arg, MODINSERT_OP)); 2361 } 2362 2363 /* 2364 * To remove a module from the stream of the interface. It is just a 2365 * wrapper. The real function is modop(). 2366 */ 2367 /* ARGSUSED */ 2368 static int 2369 modremove(char *arg, int64_t param) 2370 { 2371 return (modop(arg, MODREMOVE_OP)); 2372 } 2373 2374 /* 2375 * Helper function for mod*() functions. It gets a fd to the lower IP 2376 * stream and I_PUNLINK's the lower stream. It also initializes the 2377 * global variable lifr. 2378 * 2379 * Param: 2380 * int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2381 * int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2382 * int *ipfd_lowstr: fd to the lower IP stream. 2383 * int *arpfd_lowstr: fd to the lower ARP stream. 2384 * 2385 * Return: 2386 * -1 if operation fails, 0 otherwise. 2387 * 2388 * Please see the big block comment above ifplumb() for the logic of the 2389 * PLINK/PUNLINK 2390 */ 2391 static int 2392 ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr, 2393 int *orig_arpid) 2394 { 2395 uint64_t flags; 2396 char *udp_dev_name; 2397 2398 *orig_arpid = 0; 2399 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2400 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2401 Perror0_exit("status: SIOCGLIFFLAGS"); 2402 } 2403 flags = lifr.lifr_flags; 2404 if (flags & IFF_IPV4) { 2405 udp_dev_name = UDP_DEV_NAME; 2406 } else if (flags & IFF_IPV6) { 2407 udp_dev_name = UDP6_DEV_NAME; 2408 } else { 2409 return (-1); 2410 } 2411 2412 if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) { 2413 Perror2("open", udp_dev_name); 2414 return (-1); 2415 } 2416 if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) { 2417 Perror2("SIOCGLIFMUXID", udp_dev_name); 2418 return (-1); 2419 } 2420 if (debug > 0) { 2421 (void) printf("ARP_muxid %d IP_muxid %d\n", 2422 lifr.lifr_arp_muxid, lifr.lifr_ip_muxid); 2423 } 2424 2425 /* 2426 * Use /dev/udp{,6} as the mux to avoid linkcycles. 2427 */ 2428 if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS) 2429 return (-1); 2430 2431 if (lifr.lifr_arp_muxid != 0) { 2432 if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 2433 lifr.lifr_arp_muxid)) < 0) { 2434 if ((errno == EINVAL) && 2435 (flags & (IFF_NOARP | IFF_IPV6))) { 2436 /* 2437 * Some plumbing utilities set the muxid to 2438 * -1 or some invalid value to signify that 2439 * there is no arp stream. Set the muxid to 0 2440 * before trying to unplumb the IP stream. 2441 * IP does not allow the IP stream to be 2442 * unplumbed if it sees a non-null arp muxid, 2443 * for consistency of IP-ARP streams. 2444 */ 2445 *orig_arpid = lifr.lifr_arp_muxid; 2446 lifr.lifr_arp_muxid = 0; 2447 (void) ioctl(*muxid_fd, SIOCSLIFMUXID, 2448 (caddr_t)&lifr); 2449 *arpfd_lowstr = -1; 2450 } else { 2451 Perror0("_I_MUXID2FD"); 2452 return (-1); 2453 } 2454 } else if (ioctl(*muxfd, I_PUNLINK, 2455 lifr.lifr_arp_muxid) < 0) { 2456 Perror2("I_PUNLINK", udp_dev_name); 2457 return (-1); 2458 } 2459 } else { 2460 *arpfd_lowstr = -1; 2461 } 2462 2463 if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 2464 lifr.lifr_ip_muxid)) < 0) { 2465 Perror0("_I_MUXID2FD"); 2466 /* Undo any changes we made */ 2467 if (*orig_arpid != 0) { 2468 lifr.lifr_arp_muxid = *orig_arpid; 2469 (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 2470 } 2471 return (-1); 2472 } 2473 if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) { 2474 Perror2("I_PUNLINK", udp_dev_name); 2475 /* Undo any changes we made */ 2476 if (*orig_arpid != 0) { 2477 lifr.lifr_arp_muxid = *orig_arpid; 2478 (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 2479 } 2480 return (-1); 2481 } 2482 return (0); 2483 } 2484 2485 /* 2486 * Helper function for mod*() functions. It I_PLINK's back the upper and 2487 * lower IP streams. Note that this function must be called after 2488 * ip_domux2fd(). In ip_domux2fd(), the global variable lifr is initialized 2489 * and ip_plink() needs information in lifr. So ip_domux2fd() and ip_plink() 2490 * must be called in pairs. 2491 * 2492 * Param: 2493 * int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2494 * int muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2495 * int ipfd_lowstr: fd to the lower IP stream. 2496 * int arpfd_lowstr: fd to the lower ARP stream. 2497 * 2498 * Return: 2499 * -1 if operation fails, 0 otherwise. 2500 * 2501 * Please see the big block comment above ifplumb() for the logic of the 2502 * PLINK/PUNLINK 2503 */ 2504 static int 2505 ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr, 2506 int orig_arpid) 2507 { 2508 int ip_muxid; 2509 2510 ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr); 2511 if (ip_muxid < 0) { 2512 Perror2("I_PLINK", UDP_DEV_NAME); 2513 return (-1); 2514 } 2515 2516 /* 2517 * If there is an arp stream, plink it. If there is no 2518 * arp stream, then it is possible that the plumbing 2519 * utility could have stored any value in the arp_muxid. 2520 * If so, restore it from orig_arpid. 2521 */ 2522 if (arpfd_lowstr != -1) { 2523 if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) { 2524 Perror2("I_PLINK", UDP_DEV_NAME); 2525 return (-1); 2526 } 2527 } else if (orig_arpid != 0) { 2528 /* Undo the changes we did in ip_domux2fd */ 2529 lifr.lifr_arp_muxid = orig_arpid; 2530 lifr.lifr_ip_muxid = ip_muxid; 2531 (void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 2532 } 2533 2534 (void) close(muxfd); 2535 (void) close(muxid_fd); 2536 return (0); 2537 } 2538 2539 /* 2540 * The real function to perform module insertion/removal. 2541 * 2542 * Param: 2543 * char *arg: the argument string module_name@position 2544 * char op: operation, either MODINSERT_OP or MODREMOVE_OP. 2545 * 2546 * Return: 2547 * Before doing ip_domux2fd(), this function calls exit(1) in case of 2548 * error. After ip_domux2fd() is done, it returns -1 for error, 0 2549 * otherwise. 2550 */ 2551 static int 2552 modop(char *arg, char op) 2553 { 2554 char *pos_p; 2555 int muxfd; 2556 int muxid_fd; 2557 int ipfd_lowstr; /* IP stream (lower stream of mux) to be plinked */ 2558 int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */ 2559 struct strmodconf mod; 2560 char *at_char = "@"; 2561 char *arg_str; 2562 int orig_arpid; 2563 2564 /* 2565 * We'd return -1, but foreachinterface() doesn't propagate the error 2566 * into the exit status, so we're forced to explicitly exit(). 2567 */ 2568 if (!modcheck(name)) 2569 exit(1); 2570 2571 /* Need to save the original string for -a option. */ 2572 if ((arg_str = malloc(strlen(arg) + 1)) == NULL) { 2573 Perror0("cannot malloc"); 2574 return (-1); 2575 } 2576 (void) strcpy(arg_str, arg); 2577 2578 if (*arg_str == *at_char) { 2579 (void) fprintf(stderr, 2580 "ifconfig: must supply a module name\n"); 2581 exit(1); 2582 } 2583 mod.mod_name = strtok(arg_str, at_char); 2584 if (strlen(mod.mod_name) > FMNAMESZ) { 2585 (void) fprintf(stderr, "ifconfig: module name too long: %s\n", 2586 mod.mod_name); 2587 exit(1); 2588 } 2589 2590 /* 2591 * Need to make sure that the core TCP/IP stack modules are not 2592 * removed. Otherwise, "bad" things can happen. If a module 2593 * is removed and inserted back, it loses its old state. But 2594 * the modules above it still have the old state. E.g. IP assumes 2595 * fast data path while tunnel after re-inserted assumes that it can 2596 * receive M_DATA only in fast data path for which it does not have 2597 * any state. This is a general caveat of _I_REMOVE/_I_INSERT. 2598 */ 2599 if (op == MODREMOVE_OP && 2600 (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 || 2601 strcmp(mod.mod_name, IP_MOD_NAME) == 0)) { 2602 (void) fprintf(stderr, "ifconfig: cannot remove %s\n", 2603 mod.mod_name); 2604 exit(1); 2605 } 2606 2607 if ((pos_p = strtok(NULL, at_char)) == NULL) { 2608 (void) fprintf(stderr, "ifconfig: must supply a position\n"); 2609 exit(1); 2610 } 2611 mod.pos = atoi(pos_p); 2612 2613 if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 2614 &orig_arpid) < 0) { 2615 free(arg_str); 2616 return (-1); 2617 } 2618 switch (op) { 2619 case MODINSERT_OP: 2620 if (debug > 0) { 2621 (void) printf("Inserting module %s at %d\n", 2622 mod.mod_name, mod.pos); 2623 } 2624 if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) { 2625 Perror2("fail to insert module", mod.mod_name); 2626 } 2627 break; 2628 case MODREMOVE_OP: 2629 if (debug > 0) { 2630 (void) printf("Removing module %s at %d\n", 2631 mod.mod_name, mod.pos); 2632 } 2633 if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) { 2634 Perror2("fail to remove module", mod.mod_name); 2635 } 2636 break; 2637 default: 2638 /* Should never get to here. */ 2639 (void) fprintf(stderr, "Unknown operation\n"); 2640 break; 2641 } 2642 free(arg_str); 2643 return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2644 orig_arpid)); 2645 } 2646 2647 static int 2648 modify_tun(iptun_params_t *params) 2649 { 2650 dladm_status_t status; 2651 2652 if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 2653 ¶ms->iptun_param_linkid)) == DLADM_STATUS_OK) 2654 status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE); 2655 if (status != DLADM_STATUS_OK) 2656 dladmerr_exit(status, name); 2657 return (0); 2658 } 2659 2660 /* 2661 * Set tunnel source address 2662 */ 2663 /* ARGSUSED */ 2664 static int 2665 setiftsrc(char *addr, int64_t param) 2666 { 2667 iptun_params_t params; 2668 2669 params.iptun_param_flags = IPTUN_PARAM_LADDR; 2670 (void) strlcpy(params.iptun_param_laddr, addr, 2671 sizeof (params.iptun_param_laddr)); 2672 return (modify_tun(¶ms)); 2673 } 2674 2675 /* 2676 * Set tunnel destination address 2677 */ 2678 /* ARGSUSED */ 2679 static int 2680 setiftdst(char *addr, int64_t param) 2681 { 2682 iptun_params_t params; 2683 2684 params.iptun_param_flags = IPTUN_PARAM_RADDR; 2685 (void) strlcpy(params.iptun_param_raddr, addr, 2686 sizeof (params.iptun_param_raddr)); 2687 return (modify_tun(¶ms)); 2688 } 2689 2690 static int 2691 set_tun_prop(const char *propname, char *value) 2692 { 2693 dladm_status_t status; 2694 datalink_id_t linkid; 2695 2696 status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid); 2697 if (status == DLADM_STATUS_OK) { 2698 status = dladm_set_linkprop(dlh, linkid, propname, &value, 1, 2699 DLADM_OPT_ACTIVE); 2700 } 2701 if (status != DLADM_STATUS_OK) 2702 dladmerr_exit(status, name); 2703 return (0); 2704 } 2705 2706 /* Set tunnel encapsulation limit. */ 2707 /* ARGSUSED */ 2708 static int 2709 set_tun_encap_limit(char *arg, int64_t param) 2710 { 2711 return (set_tun_prop("encaplimit", arg)); 2712 } 2713 2714 /* Disable encapsulation limit. */ 2715 /* ARGSUSED */ 2716 static int 2717 clr_tun_encap_limit(char *arg, int64_t param) 2718 { 2719 return (set_tun_encap_limit("-1", 0)); 2720 } 2721 2722 /* Set tunnel hop limit. */ 2723 /* ARGSUSED */ 2724 static int 2725 set_tun_hop_limit(char *arg, int64_t param) 2726 { 2727 return (set_tun_prop("hoplimit", arg)); 2728 } 2729 2730 /* Set zone ID */ 2731 static int 2732 setzone(char *arg, int64_t param) 2733 { 2734 zoneid_t zoneid = GLOBAL_ZONEID; 2735 2736 if (param == NEXTARG) { 2737 /* zone must be active */ 2738 if ((zoneid = getzoneidbyname(arg)) == -1) { 2739 (void) fprintf(stderr, 2740 "ifconfig: unknown zone '%s'\n", arg); 2741 exit(1); 2742 } 2743 } 2744 (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2745 lifr.lifr_zoneid = zoneid; 2746 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 2747 Perror0_exit("SIOCSLIFZONE"); 2748 return (0); 2749 } 2750 2751 /* Put interface into all zones */ 2752 /* ARGSUSED */ 2753 static int 2754 setallzones(char *arg, int64_t param) 2755 { 2756 (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2757 lifr.lifr_zoneid = ALL_ZONES; 2758 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 2759 Perror0_exit("SIOCSLIFZONE"); 2760 return (0); 2761 } 2762 2763 /* Set source address to use */ 2764 /* ARGSUSED */ 2765 static int 2766 setifsrc(char *arg, int64_t param) 2767 { 2768 uint_t ifindex = 0; 2769 int rval; 2770 2771 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2772 2773 /* 2774 * Argument can be either an interface name or "none". The latter means 2775 * that any previous selection is cleared. 2776 */ 2777 2778 if (strchr(arg, ':') != NULL) { 2779 (void) fprintf(stderr, 2780 "ifconfig: Cannot specify logical interface for usesrc \n"); 2781 exit(1); 2782 } 2783 2784 rval = strcmp(arg, NONE_STR); 2785 if (rval != 0) { 2786 if ((ifindex = if_nametoindex(arg)) == 0) { 2787 (void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ); 2788 Perror0_exit("Could not get interface index"); 2789 } 2790 lifr.lifr_index = ifindex; 2791 } else { 2792 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0) 2793 Perror0_exit("Not a valid usesrc consumer"); 2794 lifr.lifr_index = 0; 2795 } 2796 2797 if (debug) 2798 (void) printf("setifsrc: lifr_name %s, lifr_index %d\n", 2799 lifr.lifr_name, lifr.lifr_index); 2800 2801 if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) { 2802 if (rval == 0) 2803 Perror0_exit("Cannot reset usesrc group"); 2804 else 2805 Perror0_exit("Could not set source interface"); 2806 } 2807 2808 return (0); 2809 } 2810 2811 /* 2812 * Print the interface status line associated with `ifname' 2813 */ 2814 static void 2815 ifstatus(const char *ifname) 2816 { 2817 uint64_t flags; 2818 char if_usesrc_name[LIFNAMSIZ]; 2819 char *newbuf; 2820 int n, numifs, rval = 0; 2821 struct lifreq *lifrp; 2822 struct lifsrcof lifs; 2823 2824 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2825 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2826 Perror0_exit("status: SIOCGLIFFLAGS"); 2827 } 2828 flags = lifr.lifr_flags; 2829 2830 /* 2831 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or 2832 * interfaces with IFF_IPV6 set. 2833 */ 2834 if (v4compat) { 2835 flags &= ~IFF_IPV4; 2836 if (flags & IFF_IPV6) 2837 return; 2838 } 2839 2840 (void) printf("%s: ", ifname); 2841 print_flags(flags); 2842 2843 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2844 if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 2845 Perror0_exit("status: SIOCGLIFMETRIC"); 2846 } else { 2847 if (lifr.lifr_metric) 2848 (void) printf(" metric %d", lifr.lifr_metric); 2849 } 2850 if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 2851 (void) printf(" mtu %u", lifr.lifr_mtu); 2852 2853 /* don't print index or zone when in compatibility mode */ 2854 if (!v4compat) { 2855 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 2856 (void) printf(" index %d", lifr.lifr_index); 2857 /* 2858 * Stack instances use GLOBAL_ZONEID for IP data structures 2859 * even in the non-global zone. 2860 */ 2861 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 && 2862 lifr.lifr_zoneid != getzoneid() && 2863 lifr.lifr_zoneid != GLOBAL_ZONEID) { 2864 char zone_name[ZONENAME_MAX]; 2865 2866 if (lifr.lifr_zoneid == ALL_ZONES) { 2867 (void) printf("\n\tall-zones"); 2868 } else if (getzonenamebyid(lifr.lifr_zoneid, zone_name, 2869 sizeof (zone_name)) < 0) { 2870 (void) printf("\n\tzone %d", lifr.lifr_zoneid); 2871 } else { 2872 (void) printf("\n\tzone %s", zone_name); 2873 } 2874 } 2875 } 2876 2877 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) { 2878 lifs.lifs_ifindex = lifr.lifr_index; 2879 2880 /* 2881 * Find the number of interfaces that use this interfaces' 2882 * address as a source address 2883 */ 2884 lifs.lifs_buf = NULL; 2885 lifs.lifs_maxlen = 0; 2886 for (;;) { 2887 /* The first pass will give the bufsize we need */ 2888 rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs); 2889 if (rval < 0) { 2890 if (lifs.lifs_buf != NULL) { 2891 free(lifs.lifs_buf); 2892 lifs.lifs_buf = NULL; 2893 } 2894 lifs.lifs_len = 0; 2895 break; 2896 } 2897 if (lifs.lifs_len <= lifs.lifs_maxlen) 2898 break; 2899 /* Use kernel's size + a small margin to avoid loops */ 2900 lifs.lifs_maxlen = lifs.lifs_len + 2901 5 * sizeof (struct lifreq); 2902 /* For the first pass, realloc acts like malloc */ 2903 newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen); 2904 if (newbuf == NULL) { 2905 if (lifs.lifs_buf != NULL) { 2906 free(lifs.lifs_buf); 2907 lifs.lifs_buf = NULL; 2908 } 2909 lifs.lifs_len = 0; 2910 break; 2911 } 2912 lifs.lifs_buf = newbuf; 2913 } 2914 2915 2916 numifs = lifs.lifs_len / sizeof (struct lifreq); 2917 if (numifs > 0) { 2918 lifrp = lifs.lifs_req; 2919 (void) printf("\n\tsrcof"); 2920 for (n = numifs; n > 0; n--, lifrp++) { 2921 (void) printf(" %s", lifrp->lifr_name); 2922 } 2923 } 2924 2925 if (lifs.lifs_buf != NULL) 2926 free(lifs.lifs_buf); 2927 } 2928 2929 /* Find the interface whose source address this interface uses */ 2930 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 2931 if (lifr.lifr_index != 0) { 2932 if (if_indextoname(lifr.lifr_index, 2933 if_usesrc_name) == NULL) { 2934 (void) printf("\n\tusesrc ifIndex %d", 2935 lifr.lifr_index); 2936 } else { 2937 (void) printf("\n\tusesrc %s", if_usesrc_name); 2938 } 2939 } 2940 } 2941 2942 (void) putchar('\n'); 2943 } 2944 2945 /* 2946 * Print the status of the interface. If an address family was 2947 * specified, show it and it only; otherwise, show them all. 2948 */ 2949 static void 2950 status(void) 2951 { 2952 struct afswtch *p = afp; 2953 uint64_t flags; 2954 datalink_id_t linkid; 2955 2956 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 2957 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2958 Perror0_exit("status: SIOCGLIFFLAGS"); 2959 } 2960 2961 flags = lifr.lifr_flags; 2962 2963 /* 2964 * Only print the interface status if the address family matches 2965 * the interface family flag. 2966 */ 2967 if (p != NULL) { 2968 if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) || 2969 ((p->af_af == AF_INET) && (flags & IFF_IPV6))) 2970 return; 2971 } 2972 2973 /* 2974 * In V4 compatibility mode, don't print IFF_IPV6 interfaces. 2975 */ 2976 if (v4compat && (flags & IFF_IPV6)) 2977 return; 2978 2979 ifstatus(name); 2980 2981 if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) == 2982 DLADM_STATUS_OK) 2983 tun_status(linkid); 2984 2985 if (p != NULL) { 2986 (*p->af_status)(1, flags); 2987 } else { 2988 for (p = afs; p->af_name; p++) { 2989 /* set global af for use in p->af_status */ 2990 af = p->af_af; 2991 (*p->af_status)(0, flags); 2992 } 2993 2994 /* 2995 * Historically, 'ether' has been an address family, 2996 * so print it here. 2997 */ 2998 print_ifether(name); 2999 } 3000 } 3001 3002 /* 3003 * Print the status of the interface in a format that can be used to 3004 * reconfigure the interface later. Code stolen from status() above. 3005 */ 3006 /* ARGSUSED */ 3007 static int 3008 configinfo(char *null, int64_t param) 3009 { 3010 char *cp; 3011 struct afswtch *p = afp; 3012 uint64_t flags; 3013 char lifname[LIFNAMSIZ]; 3014 char if_usesrc_name[LIFNAMSIZ]; 3015 3016 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3017 3018 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 3019 Perror0_exit("status: SIOCGLIFFLAGS"); 3020 } 3021 flags = lifr.lifr_flags; 3022 3023 if (debug) { 3024 (void) printf("configinfo: name %s flags 0x%llx af_af %d\n", 3025 name, flags, p != NULL ? p->af_af : -1); 3026 } 3027 3028 /* 3029 * Build the interface name to print (we cannot directly use `name' 3030 * because one cannot "plumb" ":0" interfaces). 3031 */ 3032 (void) strlcpy(lifname, name, LIFNAMSIZ); 3033 if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0) 3034 *cp = '\0'; 3035 3036 /* 3037 * if the interface is IPv4 3038 * if we have a IPv6 address family restriction return 3039 * so it won't print 3040 * if we are in IPv4 compatibility mode, clear out IFF_IPV4 3041 * so we don't print it. 3042 */ 3043 if (flags & IFF_IPV4) { 3044 if (p && p->af_af == AF_INET6) 3045 return (-1); 3046 if (v4compat) 3047 flags &= ~IFF_IPV4; 3048 3049 (void) printf("%s inet plumb", lifname); 3050 } else if (flags & IFF_IPV6) { 3051 /* 3052 * else if the interface is IPv6 3053 * if we have a IPv4 address family restriction return 3054 * or we are in IPv4 compatibiltiy mode, return. 3055 */ 3056 if (p && p->af_af == AF_INET) 3057 return (-1); 3058 if (v4compat) 3059 return (-1); 3060 3061 (void) printf("%s inet6 plumb", lifname); 3062 } 3063 3064 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3065 if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 3066 Perror0_exit("configinfo: SIOCGLIFMETRIC"); 3067 } else { 3068 if (lifr.lifr_metric) 3069 (void) printf(" metric %d ", lifr.lifr_metric); 3070 } 3071 if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) && 3072 ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 3073 (void) printf(" mtu %d", lifr.lifr_metric); 3074 3075 /* Index only applies to the zeroth interface */ 3076 if (lifnum(name) == 0) { 3077 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 3078 (void) printf(" index %d", lifr.lifr_index); 3079 } 3080 3081 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 3082 if (lifr.lifr_index != 0) { 3083 if (if_indextoname(lifr.lifr_index, 3084 if_usesrc_name) != NULL) { 3085 (void) printf(" usesrc %s", if_usesrc_name); 3086 } 3087 } 3088 } 3089 3090 if (p != NULL) { 3091 (*p->af_configinfo)(1, flags); 3092 } else { 3093 for (p = afs; p->af_name; p++) { 3094 (void) close(s); 3095 s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0); 3096 /* set global af for use in p->af_configinfo */ 3097 af = p->af_af; 3098 if (s == -1) { 3099 Perror0_exit("socket"); 3100 } 3101 (*p->af_configinfo)(0, flags); 3102 } 3103 } 3104 3105 (void) putchar('\n'); 3106 return (0); 3107 } 3108 3109 static void 3110 print_tsec(iptun_params_t *params) 3111 { 3112 ipsec_req_t *ipsr; 3113 3114 (void) printf("\ttunnel security settings "); 3115 if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) { 3116 (void) printf("--> use 'ipsecconf -ln -i %s'", name); 3117 } else { 3118 ipsr = ¶ms->iptun_param_secinfo; 3119 if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) { 3120 (void) printf("ah (%s) ", 3121 rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH)); 3122 } 3123 if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) { 3124 (void) printf("esp (%s", 3125 rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP)); 3126 (void) printf("/%s)", 3127 rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH)); 3128 } 3129 } 3130 (void) printf("\n"); 3131 } 3132 3133 static void 3134 tun_status(datalink_id_t linkid) 3135 { 3136 iptun_params_t params; 3137 char propval[DLADM_PROP_VAL_MAX]; 3138 char *valptr[1]; 3139 uint_t valcnt = 1; 3140 boolean_t tabbed = _B_FALSE; 3141 3142 params.iptun_param_linkid = linkid; 3143 3144 /* If dladm_iptun_getparams() fails, assume we are not a tunnel. */ 3145 assert(dlh_opened); 3146 if (dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE) != 3147 DLADM_STATUS_OK) 3148 return; 3149 3150 switch (params.iptun_param_type) { 3151 case IPTUN_TYPE_IPV4: 3152 case IPTUN_TYPE_6TO4: 3153 (void) printf("\tinet"); 3154 break; 3155 case IPTUN_TYPE_IPV6: 3156 (void) printf("\tinet6"); 3157 break; 3158 default: 3159 dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name); 3160 break; 3161 } 3162 3163 /* 3164 * There is always a source address. If it hasn't been explicitly 3165 * set, the API will pass back a buffer containing the unspecified 3166 * address. 3167 */ 3168 (void) printf(" tunnel src %s ", params.iptun_param_laddr); 3169 3170 if (params.iptun_param_flags & IPTUN_PARAM_RADDR) 3171 (void) printf("tunnel dst %s\n", params.iptun_param_raddr); 3172 else 3173 (void) putchar('\n'); 3174 3175 if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL) 3176 print_tsec(¶ms); 3177 3178 valptr[0] = propval; 3179 if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit", 3180 (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 3181 (void) printf("\ttunnel hop limit %s ", propval); 3182 tabbed = _B_TRUE; 3183 } 3184 3185 if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, 3186 "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 3187 uint32_t elim; 3188 3189 if (!tabbed) { 3190 (void) putchar('\t'); 3191 tabbed = _B_TRUE; 3192 } 3193 elim = strtol(propval, NULL, 10); 3194 if (elim > 0) 3195 (void) printf("tunnel encapsulation limit %s", propval); 3196 else 3197 (void) printf("tunnel encapsulation limit disabled"); 3198 } 3199 3200 if (tabbed) 3201 (void) putchar('\n'); 3202 } 3203 3204 static void 3205 in_status(int force, uint64_t flags) 3206 { 3207 struct sockaddr_in *sin, *laddr; 3208 struct sockaddr_in netmask = { AF_INET }; 3209 3210 if (debug) 3211 (void) printf("in_status(%s) flags 0x%llx\n", name, flags); 3212 3213 /* only print status for IPv4 interfaces */ 3214 if (!(flags & IFF_IPV4)) 3215 return; 3216 3217 if (!(flags & IFF_NOLOCAL)) { 3218 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3219 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 3220 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3221 errno == ENXIO) { 3222 if (!force) 3223 return; 3224 (void) memset(&lifr.lifr_addr, 0, 3225 sizeof (lifr.lifr_addr)); 3226 } else 3227 Perror0_exit("in_status: SIOCGLIFADDR"); 3228 } 3229 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3230 (void) printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 3231 laddr = sin; 3232 } else { 3233 (void) printf("\tinet "); 3234 } 3235 3236 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3237 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 3238 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3239 errno == ENXIO) { 3240 if (!force) 3241 return; 3242 (void) memset(&lifr.lifr_addr, 0, 3243 sizeof (lifr.lifr_addr)); 3244 } else { 3245 Perror0_exit("in_status: SIOCGLIFSUBNET"); 3246 } 3247 } 3248 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3249 if ((flags & IFF_NOLOCAL) || 3250 sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 3251 (void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr), 3252 lifr.lifr_addrlen); 3253 } 3254 if (sin->sin_family != AF_INET) { 3255 (void) printf("Wrong family: %d\n", sin->sin_family); 3256 } 3257 3258 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3259 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 3260 if (errno != EADDRNOTAVAIL) 3261 Perror0_exit("in_status: SIOCGLIFNETMASK"); 3262 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 3263 } else 3264 netmask.sin_addr = 3265 ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 3266 if (flags & IFF_POINTOPOINT) { 3267 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3268 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 3269 if (errno == EADDRNOTAVAIL) 3270 (void) memset(&lifr.lifr_addr, 0, 3271 sizeof (lifr.lifr_addr)); 3272 else 3273 Perror0_exit("in_status: SIOCGLIFDSTADDR"); 3274 } 3275 sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 3276 (void) printf("--> %s ", inet_ntoa(sin->sin_addr)); 3277 } 3278 (void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr)); 3279 if (flags & IFF_BROADCAST) { 3280 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3281 if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 3282 if (errno == EADDRNOTAVAIL) 3283 (void) memset(&lifr.lifr_addr, 0, 3284 sizeof (lifr.lifr_addr)); 3285 else 3286 Perror0_exit("in_status: SIOCGLIFBRDADDR"); 3287 } 3288 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3289 if (sin->sin_addr.s_addr != 0) { 3290 (void) printf("broadcast %s", 3291 inet_ntoa(sin->sin_addr)); 3292 } 3293 } 3294 /* If there is a groupname, print it for only the physical interface */ 3295 if (strchr(name, ':') == NULL) { 3296 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3297 lifr.lifr_groupname[0] != '\0') { 3298 (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 3299 } 3300 } 3301 (void) putchar('\n'); 3302 } 3303 3304 static void 3305 in6_status(int force, uint64_t flags) 3306 { 3307 char abuf[INET6_ADDRSTRLEN]; 3308 struct sockaddr_in6 *sin6, *laddr6; 3309 3310 if (debug) 3311 (void) printf("in6_status(%s) flags 0x%llx\n", name, flags); 3312 3313 if (!(flags & IFF_IPV6)) 3314 return; 3315 3316 if (!(flags & IFF_NOLOCAL)) { 3317 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3318 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 3319 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3320 errno == ENXIO) { 3321 if (!force) 3322 return; 3323 (void) memset(&lifr.lifr_addr, 0, 3324 sizeof (lifr.lifr_addr)); 3325 } else 3326 Perror0_exit("in_status6: SIOCGLIFADDR"); 3327 } 3328 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3329 (void) printf("\tinet6 %s/%d ", 3330 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3331 abuf, sizeof (abuf)), 3332 lifr.lifr_addrlen); 3333 laddr6 = sin6; 3334 } else { 3335 (void) printf("\tinet6 "); 3336 } 3337 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3338 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 3339 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3340 errno == ENXIO) { 3341 if (!force) 3342 return; 3343 (void) memset(&lifr.lifr_addr, 0, 3344 sizeof (lifr.lifr_addr)); 3345 } else 3346 Perror0_exit("in_status6: SIOCGLIFSUBNET"); 3347 } 3348 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3349 if ((flags & IFF_NOLOCAL) || 3350 !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 3351 (void) printf("subnet %s/%d ", 3352 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3353 abuf, sizeof (abuf)), 3354 lifr.lifr_addrlen); 3355 } 3356 if (sin6->sin6_family != AF_INET6) { 3357 (void) printf("Wrong family: %d\n", sin6->sin6_family); 3358 } 3359 if (flags & IFF_POINTOPOINT) { 3360 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3361 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 3362 if (errno == EADDRNOTAVAIL) 3363 (void) memset(&lifr.lifr_addr, 0, 3364 sizeof (lifr.lifr_addr)); 3365 else 3366 Perror0_exit("in_status6: SIOCGLIFDSTADDR"); 3367 } 3368 sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 3369 (void) printf("--> %s ", 3370 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3371 abuf, sizeof (abuf))); 3372 } 3373 if (verbose) { 3374 (void) putchar('\n'); 3375 (void) putchar('\t'); 3376 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3377 if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 3378 if (errno == EADDRNOTAVAIL || errno == EINVAL) 3379 (void) memset(&lifr.lifr_addr, 0, 3380 sizeof (lifr.lifr_addr)); 3381 else 3382 Perror0_exit("in_status6: SIOCGLIFTOKEN"); 3383 } else { 3384 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3385 (void) printf("token %s/%d ", 3386 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3387 abuf, sizeof (abuf)), 3388 lifr.lifr_addrlen); 3389 } 3390 if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) { 3391 if (errno != EINVAL) { 3392 Perror0_exit("in_status6: SIOCGLIFLNKINFO"); 3393 } 3394 } else { 3395 (void) printf("maxhops %u, reachtime %u ms, " 3396 "reachretrans %u ms, maxmtu %u ", 3397 lifr.lifr_ifinfo.lir_maxhops, 3398 lifr.lifr_ifinfo.lir_reachtime, 3399 lifr.lifr_ifinfo.lir_reachretrans, 3400 lifr.lifr_ifinfo.lir_maxmtu); 3401 } 3402 } 3403 /* If there is a groupname, print it for only the physical interface */ 3404 if (strchr(name, ':') == NULL) { 3405 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3406 lifr.lifr_groupname[0] != '\0') { 3407 (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 3408 } 3409 } 3410 (void) putchar('\n'); 3411 } 3412 3413 static void 3414 in_configinfo(int force, uint64_t flags) 3415 { 3416 struct sockaddr_in *sin, *laddr; 3417 struct sockaddr_in netmask = { AF_INET }; 3418 3419 if (debug) 3420 (void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags); 3421 3422 /* only configinfo info for IPv4 interfaces */ 3423 if (!(flags & IFF_IPV4)) 3424 return; 3425 3426 if (!(flags & IFF_NOLOCAL)) { 3427 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3428 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 3429 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3430 errno == ENXIO) { 3431 if (!force) 3432 return; 3433 (void) memset(&lifr.lifr_addr, 0, 3434 sizeof (lifr.lifr_addr)); 3435 } else 3436 Perror0_exit("in_configinfo: SIOCGLIFADDR"); 3437 } 3438 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3439 (void) printf(" set %s ", inet_ntoa(sin->sin_addr)); 3440 laddr = sin; 3441 } 3442 3443 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3444 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 3445 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3446 errno == ENXIO) { 3447 if (!force) 3448 return; 3449 (void) memset(&lifr.lifr_addr, 0, 3450 sizeof (lifr.lifr_addr)); 3451 } else { 3452 Perror0_exit("in_configinfo: SIOCGLIFSUBNET"); 3453 } 3454 } 3455 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3456 3457 if ((flags & IFF_NOLOCAL) || 3458 sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 3459 (void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr), 3460 lifr.lifr_addrlen); 3461 } 3462 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3463 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 3464 if (errno != EADDRNOTAVAIL) 3465 Perror0_exit("in_configinfo: SIOCGLIFNETMASK"); 3466 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 3467 } else 3468 netmask.sin_addr = 3469 ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 3470 if (flags & IFF_POINTOPOINT) { 3471 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3472 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 3473 if (errno == EADDRNOTAVAIL) 3474 (void) memset(&lifr.lifr_addr, 0, 3475 sizeof (lifr.lifr_addr)); 3476 else 3477 Perror0_exit("in_configinfo: SIOCGLIFDSTADDR"); 3478 } 3479 sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 3480 (void) printf(" destination %s ", inet_ntoa(sin->sin_addr)); 3481 } 3482 (void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr)); 3483 if (flags & IFF_BROADCAST) { 3484 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3485 if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 3486 if (errno == EADDRNOTAVAIL) 3487 (void) memset(&lifr.lifr_addr, 0, 3488 sizeof (lifr.lifr_addr)); 3489 else 3490 Perror0_exit("in_configinfo: SIOCGLIFBRDADDR"); 3491 } 3492 sin = (struct sockaddr_in *)&lifr.lifr_addr; 3493 if (sin->sin_addr.s_addr != 0) { 3494 (void) printf(" broadcast %s ", 3495 inet_ntoa(sin->sin_addr)); 3496 } 3497 } 3498 3499 /* If there is a groupname, print it for only the zeroth interface */ 3500 if (lifnum(name) == 0) { 3501 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3502 lifr.lifr_groupname[0] != '\0') { 3503 (void) printf(" group %s ", lifr.lifr_groupname); 3504 } 3505 } 3506 3507 /* Print flags to configure */ 3508 print_config_flags(AF_INET, flags); 3509 } 3510 3511 static void 3512 in6_configinfo(int force, uint64_t flags) 3513 { 3514 char abuf[INET6_ADDRSTRLEN]; 3515 struct sockaddr_in6 *sin6, *laddr6; 3516 3517 if (debug) 3518 (void) printf("in6_configinfo(%s) flags 0x%llx\n", name, 3519 flags); 3520 3521 if (!(flags & IFF_IPV6)) 3522 return; 3523 3524 if (!(flags & IFF_NOLOCAL)) { 3525 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3526 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 3527 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3528 errno == ENXIO) { 3529 if (!force) 3530 return; 3531 (void) memset(&lifr.lifr_addr, 0, 3532 sizeof (lifr.lifr_addr)); 3533 } else 3534 Perror0_exit("in6_configinfo: SIOCGLIFADDR"); 3535 } 3536 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3537 (void) printf(" set %s/%d ", 3538 inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)), 3539 lifr.lifr_addrlen); 3540 laddr6 = sin6; 3541 } 3542 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3543 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 3544 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 3545 errno == ENXIO) { 3546 if (!force) 3547 return; 3548 (void) memset(&lifr.lifr_addr, 0, 3549 sizeof (lifr.lifr_addr)); 3550 } else 3551 Perror0_exit("in6_configinfo: SIOCGLIFSUBNET"); 3552 } 3553 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3554 if ((flags & IFF_NOLOCAL) || 3555 !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 3556 (void) printf(" subnet %s/%d ", 3557 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3558 abuf, sizeof (abuf)), 3559 lifr.lifr_addrlen); 3560 } 3561 3562 if (flags & IFF_POINTOPOINT) { 3563 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3564 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 3565 if (errno == EADDRNOTAVAIL) 3566 (void) memset(&lifr.lifr_addr, 0, 3567 sizeof (lifr.lifr_addr)); 3568 else 3569 Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR"); 3570 } 3571 sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 3572 (void) printf(" destination %s ", 3573 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3574 abuf, sizeof (abuf))); 3575 } 3576 3577 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3578 if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 3579 if (errno == EADDRNOTAVAIL || errno == EINVAL) 3580 (void) memset(&lifr.lifr_addr, 0, 3581 sizeof (lifr.lifr_addr)); 3582 else 3583 Perror0_exit("in6_configinfo: SIOCGLIFTOKEN"); 3584 } else { 3585 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3586 (void) printf(" token %s/%d ", 3587 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 3588 abuf, sizeof (abuf)), 3589 lifr.lifr_addrlen); 3590 } 3591 3592 /* If there is a groupname, print it for only the zeroth interface */ 3593 if (lifnum(name) == 0) { 3594 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3595 lifr.lifr_groupname[0] != '\0') { 3596 (void) printf(" group %s ", lifr.lifr_groupname); 3597 } 3598 } 3599 3600 /* Print flags to configure */ 3601 print_config_flags(AF_INET6, flags); 3602 } 3603 3604 /* 3605 * If this is a physical interface then remove it. 3606 * If it is a logical interface name use SIOCLIFREMOVEIF to 3607 * remove it. In both cases fail if it doesn't exist. 3608 */ 3609 /* ARGSUSED */ 3610 static int 3611 inetunplumb(char *arg, int64_t param) 3612 { 3613 ipadm_status_t istatus; 3614 3615 istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 3616 if (istatus != IPADM_SUCCESS) { 3617 (void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n", 3618 name, ipadm_status2str(istatus)); 3619 exit(1); 3620 } 3621 3622 return (0); 3623 } 3624 3625 /* 3626 * Create the interface in `name', using ipadm_create_if(). If `name' is a 3627 * logical interface or loopback interface, ipadm_create_if() uses 3628 * SIOCLIFADDIF to create it. 3629 */ 3630 /* ARGSUSED */ 3631 static int 3632 inetplumb(char *arg, int64_t param) 3633 { 3634 ipadm_status_t istatus; 3635 3636 istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 3637 if (istatus != IPADM_SUCCESS) { 3638 (void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n", 3639 name, ipadm_status2str(istatus)); 3640 if (istatus != IPADM_IF_EXISTS) 3641 exit(1); 3642 } 3643 return (0); 3644 } 3645 3646 /* ARGSUSED */ 3647 static int 3648 inetipmp(char *arg, int64_t param) 3649 { 3650 int retval; 3651 3652 /* 3653 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb". 3654 * Otherwise, try to create the requested IPMP interface. 3655 */ 3656 if (strchr(name, ':') != NULL) 3657 retval = inetplumb(arg, param); 3658 else 3659 retval = create_ipmp(name, afp->af_af, name, _B_FALSE); 3660 3661 /* 3662 * We'd return -1, but foreachinterface() doesn't propagate the error 3663 * into the exit status, so we're forced to explicitly exit(). 3664 */ 3665 if (retval == -1) 3666 exit(1); 3667 return (0); 3668 } 3669 3670 /* 3671 * Create an IPMP group `grname' with address family `af'. If `ifname' is 3672 * non-NULL, it specifies the interface name to use. Otherwise, use the name 3673 * ipmpN, where N corresponds to the lowest available integer. If `implicit' 3674 * is set, then the group is being created as a side-effect of placing an 3675 * underlying interface in a group. Also start in.mpathd if necessary. 3676 */ 3677 static int 3678 create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit) 3679 { 3680 static int ipmp_daemon_started; 3681 uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE; 3682 ipadm_status_t istatus; 3683 3684 if (debug) { 3685 (void) printf("create_ipmp: ifname %s grname %s af %d\n", 3686 ifname != NULL ? ifname : "NULL", grname, af); 3687 } 3688 3689 /* 3690 * ipadm_create_if() creates the IPMP interface and fills in the 3691 * ppa in lifr.lifr_name, if `ifname'="ipmp". 3692 */ 3693 (void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"), 3694 sizeof (lifr.lifr_name)); 3695 if (ifname == NULL) 3696 flags |= IPADM_OPT_GENPPA; 3697 istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags); 3698 if (istatus != IPADM_SUCCESS) { 3699 (void) fprintf(stderr, "ifconfig: cannot create IPMP interface " 3700 "%s: %s\n", grname, ipadm_status2str(istatus)); 3701 return (-1); 3702 } 3703 3704 /* 3705 * To preserve backward-compatibility, always bring up the link-local 3706 * address for implicitly-created IPv6 IPMP interfaces. 3707 */ 3708 if (implicit && af == AF_INET6) { 3709 if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) { 3710 lifr.lifr_flags |= IFF_UP; 3711 (void) ioctl(s6, SIOCSLIFFLAGS, &lifr); 3712 } 3713 } 3714 3715 /* 3716 * If the caller requested a different group name, issue a 3717 * SIOCSLIFGROUPNAME on the new IPMP interface. 3718 */ 3719 if (strcmp(lifr.lifr_name, grname) != 0) { 3720 (void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ); 3721 if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) { 3722 Perror0("SIOCSLIFGROUPNAME"); 3723 return (-1); 3724 } 3725 } 3726 3727 /* 3728 * If we haven't done so yet, ensure in.mpathd is started. 3729 */ 3730 if (ipmp_daemon_started++ == 0) 3731 start_ipmp_daemon(); 3732 3733 return (0); 3734 } 3735 3736 /* 3737 * Start in.mpathd if it's not already running. 3738 */ 3739 static void 3740 start_ipmp_daemon(void) 3741 { 3742 int retval; 3743 ipmp_handle_t ipmp_handle; 3744 3745 /* 3746 * Ping in.mpathd to see if it's running already. 3747 */ 3748 if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) { 3749 (void) fprintf(stderr, "ifconfig: cannot create IPMP handle: " 3750 "%s\n", ipmp_errmsg(retval)); 3751 return; 3752 } 3753 3754 retval = ipmp_ping_daemon(ipmp_handle); 3755 ipmp_close(ipmp_handle); 3756 3757 switch (retval) { 3758 case IPMP_ENOMPATHD: 3759 break; 3760 case IPMP_SUCCESS: 3761 return; 3762 default: 3763 (void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n", 3764 ipmp_errmsg(retval)); 3765 break; 3766 } 3767 3768 /* 3769 * Start in.mpathd. Note that in.mpathd will handle multiple 3770 * incarnations (ipmp_ping_daemon() is just an optimization) so we 3771 * don't need to worry about racing with another ifconfig process. 3772 */ 3773 switch (fork()) { 3774 case -1: 3775 Perror0_exit("start_ipmp_daemon: fork"); 3776 /* NOTREACHED */ 3777 case 0: 3778 (void) execl(MPATHD_PATH, MPATHD_PATH, NULL); 3779 _exit(1); 3780 /* NOTREACHED */ 3781 default: 3782 break; 3783 } 3784 } 3785 3786 /* 3787 * Bring the address named by `ifaddrp' up or down. Doesn't trust any mutable 3788 * values in ia_flags since they may be stale. 3789 */ 3790 static boolean_t 3791 ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up) 3792 { 3793 struct lifreq lifrl; /* Local lifreq struct */ 3794 int fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6; 3795 3796 (void) memset(&lifrl, 0, sizeof (lifrl)); 3797 (void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ); 3798 if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1) 3799 return (_B_FALSE); 3800 3801 if (up) 3802 lifrl.lifr_flags |= IFF_UP; 3803 else 3804 lifrl.lifr_flags &= ~IFF_UP; 3805 3806 if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1) 3807 return (_B_FALSE); 3808 3809 /* 3810 * If we're trying to bring the address down, ensure that DAD activity 3811 * (observable by IFF_DUPLICATE) has also been stopped. 3812 */ 3813 if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 && 3814 lifrl.lifr_flags & IFF_DUPLICATE) { 3815 if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 || 3816 ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) { 3817 return (_B_FALSE); 3818 } 3819 } 3820 return (_B_TRUE); 3821 } 3822 3823 static boolean_t 3824 ifaddr_up(ifaddrlistx_t *ifaddrp) 3825 { 3826 return (ifaddr_op(ifaddrp, _B_TRUE)); 3827 } 3828 3829 static boolean_t 3830 ifaddr_down(ifaddrlistx_t *ifaddrp) 3831 { 3832 return (ifaddr_op(ifaddrp, _B_FALSE)); 3833 } 3834 3835 /* 3836 * Open the global libdladm handle "dlh" if it isn't already opened. The 3837 * caller may optionally supply a link name to obtain its linkid. If a link 3838 * of a specific class or classes is required, reqclass specifies the class 3839 * mask. 3840 */ 3841 static dladm_status_t 3842 ifconfig_dladm_open(const char *name, datalink_class_t reqclass, 3843 datalink_id_t *linkid) 3844 { 3845 dladm_status_t status = DLADM_STATUS_OK; 3846 datalink_class_t class; 3847 3848 if (!dlh_opened) { 3849 if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK) 3850 return (status); 3851 dlh_opened = _B_TRUE; 3852 } 3853 if (name != NULL) { 3854 status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL); 3855 if (status == DLADM_STATUS_OK) { 3856 if (!(class & reqclass)) 3857 status = DLADM_STATUS_LINKINVAL; 3858 } 3859 } 3860 return (status); 3861 } 3862 3863 /* 3864 * This function checks if we can use libipadm API's. We will only 3865 * call libipadm functions for non-IPMP interfaces. This check is 3866 * temporary until libipadm supports IPMP interfaces. 3867 */ 3868 static boolean_t 3869 ifconfig_use_libipadm(int s, const char *lifname) 3870 { 3871 struct lifreq lifr1; 3872 3873 (void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name)); 3874 if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) { 3875 (void) strncpy(lifr.lifr_name, lifname, 3876 sizeof (lifr.lifr_name)); 3877 Perror0_exit("error"); 3878 } 3879 3880 return (lifr1.lifr_groupname[0] == '\0'); 3881 } 3882 3883 static void 3884 ipadmerr_exit(ipadm_status_t status, const char *str) 3885 { 3886 (void) fprintf(stderr, "ifconfig: %s: %s\n", str, 3887 ipadm_status2str(status)); 3888 exit(1); 3889 } 3890 3891 static void 3892 dladmerr_exit(dladm_status_t status, const char *str) 3893 { 3894 char errstr[DLADM_STRSIZE]; 3895 3896 (void) fprintf(stderr, "%s: %s\n", str, 3897 dladm_status2str(status, errstr)); 3898 exit(1); 3899 } 3900 3901 void 3902 Perror0(const char *cmd) 3903 { 3904 Perror2(cmd, lifr.lifr_name); 3905 } 3906 3907 void 3908 Perror0_exit(const char *cmd) 3909 { 3910 Perror0(cmd); 3911 exit(1); 3912 } 3913 3914 void 3915 Perror2(const char *cmd, const char *str) 3916 { 3917 int error = errno; 3918 3919 (void) fprintf(stderr, "ifconfig: %s: ", cmd); 3920 3921 switch (error) { 3922 case ENXIO: 3923 (void) fprintf(stderr, "%s: no such interface\n", str); 3924 break; 3925 case EPERM: 3926 (void) fprintf(stderr, "%s: permission denied\n", str); 3927 break; 3928 case EEXIST: 3929 (void) fprintf(stderr, "%s: already exists\n", str); 3930 break; 3931 case ENAMETOOLONG: 3932 (void) fprintf(stderr, "%s: interface name too long\n", str); 3933 break; 3934 case ERANGE: 3935 (void) fprintf(stderr, "%s: logical interface id is outside " 3936 "allowed range\n", str); 3937 break; 3938 default: 3939 errno = error; 3940 perror(str); 3941 } 3942 } 3943 3944 /* 3945 * Print out error message (Perror2()) and exit 3946 */ 3947 void 3948 Perror2_exit(const char *cmd, const char *str) 3949 { 3950 Perror2(cmd, str); 3951 exit(1); 3952 /* NOTREACHED */ 3953 } 3954 3955 void 3956 Perrdlpi(const char *cmd, const char *linkname, int err) 3957 { 3958 (void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd, 3959 linkname, dlpi_strerror(err)); 3960 } 3961 3962 /* 3963 * Print out error message (Perrdlpi()) and exit 3964 */ 3965 void 3966 Perrdlpi_exit(const char *cmd, const char *linkname, int err) 3967 { 3968 Perrdlpi(cmd, linkname, err); 3969 exit(1); 3970 } 3971 3972 /* 3973 * If the last argument is non-NULL allow a <addr>/<n> syntax and 3974 * pass out <n> in *plenp. 3975 * If <n> doesn't parse return BAD_ADDR as *plenp. 3976 * If no /<n> is present return NO_PREFIX as *plenp. 3977 */ 3978 static void 3979 in_getaddr(char *s, struct sockaddr *saddr, int *plenp) 3980 { 3981 /* LINTED: alignment */ 3982 struct sockaddr_in *sin = (struct sockaddr_in *)saddr; 3983 struct hostent *hp; 3984 struct netent *np; 3985 char str[BUFSIZ]; 3986 int error_num; 3987 3988 (void) strncpy(str, s, sizeof (str)); 3989 3990 /* 3991 * Look for '/'<n> is plenp 3992 */ 3993 if (plenp != NULL) { 3994 char *cp; 3995 3996 *plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS); 3997 if (*plenp == BAD_ADDR) 3998 return; 3999 cp = strchr(str, '/'); 4000 if (cp != NULL) 4001 *cp = '\0'; 4002 } else if (strchr(str, '/') != NULL) { 4003 (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 4004 exit(1); 4005 } 4006 4007 (void) memset(sin, 0, sizeof (*sin)); 4008 4009 /* 4010 * Try to catch attempts to set the broadcast address to all 1's. 4011 */ 4012 if (strcmp(str, "255.255.255.255") == 0 || 4013 (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) { 4014 sin->sin_family = AF_INET; 4015 sin->sin_addr.s_addr = 0xffffffff; 4016 return; 4017 } 4018 4019 hp = getipnodebyname(str, AF_INET, 0, &error_num); 4020 if (hp) { 4021 sin->sin_family = hp->h_addrtype; 4022 (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); 4023 freehostent(hp); 4024 return; 4025 } 4026 np = getnetbyname(str); 4027 if (np) { 4028 sin->sin_family = np->n_addrtype; 4029 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 4030 return; 4031 } 4032 if (error_num == TRY_AGAIN) { 4033 (void) fprintf(stderr, "ifconfig: %s: bad address " 4034 "(try again later)\n", s); 4035 } else { 4036 (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 4037 } 4038 exit(1); 4039 } 4040 4041 /* 4042 * If the last argument is non-NULL allow a <addr>/<n> syntax and 4043 * pass out <n> in *plenp. 4044 * If <n> doesn't parse return BAD_ADDR as *plenp. 4045 * If no /<n> is present return NO_PREFIX as *plenp. 4046 */ 4047 static void 4048 in6_getaddr(char *s, struct sockaddr *saddr, int *plenp) 4049 { 4050 /* LINTED: alignment */ 4051 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr; 4052 struct hostent *hp; 4053 char str[BUFSIZ]; 4054 int error_num; 4055 4056 (void) strncpy(str, s, sizeof (str)); 4057 4058 /* 4059 * Look for '/'<n> is plenp 4060 */ 4061 if (plenp != NULL) { 4062 char *cp; 4063 4064 *plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS); 4065 if (*plenp == BAD_ADDR) 4066 return; 4067 cp = strchr(str, '/'); 4068 if (cp != NULL) 4069 *cp = '\0'; 4070 } else if (strchr(str, '/') != NULL) { 4071 (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 4072 exit(1); 4073 } 4074 4075 (void) memset(sin6, 0, sizeof (*sin6)); 4076 4077 hp = getipnodebyname(str, AF_INET6, 0, &error_num); 4078 if (hp) { 4079 sin6->sin6_family = hp->h_addrtype; 4080 (void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length); 4081 freehostent(hp); 4082 return; 4083 } 4084 if (error_num == TRY_AGAIN) { 4085 (void) fprintf(stderr, "ifconfig: %s: bad address " 4086 "(try again later)\n", s); 4087 } else { 4088 (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 4089 } 4090 exit(1); 4091 } 4092 4093 /* 4094 * If "slash" is zero this parses the whole string as 4095 * an integer. With "slash" non zero it parses the tail part as an integer. 4096 * 4097 * If it is not a valid integer this returns BAD_ADDR. 4098 * If there is /<n> present this returns NO_PREFIX. 4099 */ 4100 static int 4101 in_getprefixlen(char *addr, boolean_t slash, int max_plen) 4102 { 4103 int prefixlen; 4104 char *str, *end; 4105 4106 if (slash) { 4107 str = strchr(addr, '/'); 4108 if (str == NULL) 4109 return (NO_PREFIX); 4110 str++; 4111 } else 4112 str = addr; 4113 4114 prefixlen = strtol(str, &end, 10); 4115 if (prefixlen < 0) 4116 return (BAD_ADDR); 4117 if (str == end) 4118 return (BAD_ADDR); 4119 if (max_plen != 0 && max_plen < prefixlen) 4120 return (BAD_ADDR); 4121 return (prefixlen); 4122 } 4123 4124 /* 4125 * Convert a prefix length to a mask. 4126 * Returns 1 if ok. 0 otherwise. 4127 * Assumes the mask array is zero'ed by the caller. 4128 */ 4129 static boolean_t 4130 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask) 4131 { 4132 if (prefixlen < 0 || prefixlen > maxlen) 4133 return (0); 4134 4135 while (prefixlen > 0) { 4136 if (prefixlen >= 8) { 4137 *mask++ = 0xFF; 4138 prefixlen -= 8; 4139 continue; 4140 } 4141 *mask |= 1 << (8 - prefixlen); 4142 prefixlen--; 4143 } 4144 return (1); 4145 } 4146 4147 static void 4148 print_flags(uint64_t flags) 4149 { 4150 boolean_t first = _B_TRUE; 4151 int cnt, i; 4152 4153 (void) printf("flags=%llx", flags); 4154 cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t); 4155 for (i = 0; i < cnt; i++) { 4156 if (flags & if_flags_tbl[i].iff_value) { 4157 if (first) { 4158 (void) printf("<"); 4159 first = _B_FALSE; 4160 } else { 4161 /* 4162 * It has to be here and not with the 4163 * printf below because for the last one, 4164 * we don't want a comma before the ">". 4165 */ 4166 (void) printf(","); 4167 } 4168 (void) printf("%s", if_flags_tbl[i].iff_name); 4169 } 4170 } 4171 if (!first) 4172 (void) printf(">"); 4173 } 4174 4175 static void 4176 print_config_flags(int af, uint64_t flags) 4177 { 4178 if_config_cmd_t *cmdp; 4179 4180 for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) { 4181 if ((flags & cmdp->iff_flag) && 4182 (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) { 4183 (void) printf("%s ", cmdp->iff_name); 4184 } 4185 } 4186 } 4187 4188 /* 4189 * Use the configured directory lookup mechanism (e.g. files/NIS/...) 4190 * to find the network mask. Returns true if we found one to set. 4191 * 4192 * The parameter addr_set controls whether we should get the address of 4193 * the working interface for the netmask query. If addr_set is true, 4194 * we will use the address provided. Otherwise, we will find the working 4195 * interface's address and use it instead. 4196 */ 4197 static boolean_t 4198 in_getmask(struct sockaddr_in *saddr, boolean_t addr_set) 4199 { 4200 struct sockaddr_in ifaddr; 4201 4202 /* 4203 * Read the address from the interface if it is not passed in. 4204 */ 4205 if (!addr_set) { 4206 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 4207 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 4208 if (errno != EADDRNOTAVAIL) { 4209 (void) fprintf(stderr, "Need net number for " 4210 "mask\n"); 4211 } 4212 return (_B_FALSE); 4213 } 4214 ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr); 4215 } else { 4216 ifaddr.sin_addr = saddr->sin_addr; 4217 } 4218 if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) { 4219 saddr->sin_family = AF_INET; 4220 return (_B_TRUE); 4221 } 4222 return (_B_FALSE); 4223 } 4224 4225 static int 4226 lifnum(const char *ifname) 4227 { 4228 const char *cp; 4229 4230 if ((cp = strchr(ifname, ':')) == NULL) 4231 return (0); 4232 else 4233 return (atoi(cp + 1)); 4234 } 4235 4236 static void 4237 add_ni(const char *name) 4238 { 4239 ni_t **pp; 4240 ni_t *p; 4241 4242 for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) { 4243 if (strcmp(p->ni_name, name) == 0) { 4244 if (debug > 2) 4245 (void) fprintf(stderr, "'%s' is a duplicate\n", 4246 name); 4247 return; 4248 } 4249 } 4250 4251 if (debug > 2) 4252 (void) fprintf(stderr, "adding '%s'\n", 4253 name); 4254 4255 if ((p = malloc(sizeof (ni_t))) == NULL) 4256 return; 4257 4258 (void) strlcpy(p->ni_name, name, sizeof (p->ni_name)); 4259 p->ni_next = NULL; 4260 4261 *pp = p; 4262 num_ni++; 4263 } 4264 4265 static boolean_t 4266 ni_entry(const char *linkname, void *arg) 4267 { 4268 dlpi_handle_t dh; 4269 datalink_class_t class; 4270 4271 (void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL); 4272 4273 if (class == DATALINK_CLASS_ETHERSTUB) 4274 return (_B_FALSE); 4275 if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS) 4276 return (_B_FALSE); 4277 4278 add_ni(linkname); 4279 4280 dlpi_close(dh); 4281 return (_B_FALSE); 4282 } 4283 4284 /* 4285 * dhcp-related routines 4286 */ 4287 4288 static int 4289 setifdhcp(const char *caller, const char *ifname, int argc, char *argv[]) 4290 { 4291 dhcp_ipc_request_t *request; 4292 dhcp_ipc_reply_t *reply = NULL; 4293 int timeout = DHCP_IPC_WAIT_DEFAULT; 4294 dhcp_ipc_type_t type = DHCP_START; 4295 int error; 4296 boolean_t is_primary = _B_FALSE; 4297 boolean_t started = _B_FALSE; 4298 4299 for (argv++; --argc > 0; argv++) { 4300 4301 if (strcmp(*argv, "primary") == 0) { 4302 is_primary = _B_TRUE; 4303 continue; 4304 } 4305 4306 if (strcmp(*argv, "wait") == 0) { 4307 if (--argc <= 0) { 4308 usage(); 4309 return (DHCP_EXIT_BADARGS); 4310 } 4311 argv++; 4312 4313 if (strcmp(*argv, "forever") == 0) { 4314 timeout = DHCP_IPC_WAIT_FOREVER; 4315 continue; 4316 } 4317 4318 if (sscanf(*argv, "%d", &timeout) != 1) { 4319 usage(); 4320 return (DHCP_EXIT_BADARGS); 4321 } 4322 4323 if (timeout < 0) { 4324 usage(); 4325 return (DHCP_EXIT_BADARGS); 4326 } 4327 continue; 4328 } 4329 4330 type = dhcp_string_to_request(*argv); 4331 if (type == -1) { 4332 usage(); 4333 return (DHCP_EXIT_BADARGS); 4334 } 4335 } 4336 4337 /* 4338 * Only try to start agent on start or inform; in all other cases it 4339 * has to already be running for anything to make sense. 4340 */ 4341 if (type == DHCP_START || type == DHCP_INFORM) { 4342 if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) { 4343 (void) fprintf(stderr, "%s: unable to start %s\n", 4344 caller, DHCP_AGENT_PATH); 4345 return (DHCP_EXIT_FAILURE); 4346 } 4347 started = _B_TRUE; 4348 } 4349 4350 if (is_primary) 4351 type |= DHCP_PRIMARY; 4352 4353 if (af != AF_INET) 4354 type |= DHCP_V6; 4355 4356 request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE); 4357 if (request == NULL) { 4358 (void) fprintf(stderr, "%s: out of memory\n", caller); 4359 return (DHCP_EXIT_SYSTEM); 4360 } 4361 4362 error = dhcp_ipc_make_request(request, &reply, timeout); 4363 if (error != 0) { 4364 free(request); 4365 /* 4366 * Re-map connect error to not under control if we didn't try a 4367 * start operation, as this has to be true and results in a 4368 * clearer message, not to mention preserving compatibility 4369 * with the days when we always started dhcpagent for every 4370 * request. 4371 */ 4372 if (error == DHCP_IPC_E_CONNECT && !started) 4373 error = DHCP_IPC_E_UNKIF; 4374 (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 4375 dhcp_ipc_strerror(error)); 4376 return (DHCP_EXIT_FAILURE); 4377 } 4378 4379 error = reply->return_code; 4380 if (error != 0) { 4381 free(request); 4382 free(reply); 4383 4384 if (error == DHCP_IPC_E_TIMEOUT && timeout == 0) 4385 return (DHCP_EXIT_SUCCESS); 4386 4387 (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 4388 dhcp_ipc_strerror(error)); 4389 4390 if (error == DHCP_IPC_E_TIMEOUT) 4391 return (DHCP_EXIT_TIMEOUT); 4392 else 4393 return (DHCP_EXIT_IF_FAILURE); 4394 } 4395 4396 if (DHCP_IPC_CMD(type) == DHCP_STATUS) { 4397 (void) printf("%s", dhcp_status_hdr_string()); 4398 (void) printf("%s", dhcp_status_reply_to_string(reply)); 4399 } 4400 4401 free(request); 4402 free(reply); 4403 return (DHCP_EXIT_SUCCESS); 4404 } 4405 4406 static void 4407 usage(void) 4408 { 4409 (void) fprintf(stderr, 4410 "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n"); 4411 4412 (void) fprintf(stderr, "%s", 4413 "\t[ <addr_family> ]\n" 4414 "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n" 4415 "\t[ set [ <address>][/<prefix_length>] ]" 4416 " [ <address>/<prefix_length>] ]\n" 4417 "\t[ destination <dest_address> ]\n" 4418 "\t[ addif <address>[/<prefix_length>]" 4419 " [ <dest_address> ] ]\n" 4420 "\t[ removeif <address>[/<prefix_length>] ]\n" 4421 "\t[ arp | -arp ]\n" 4422 "\t[ auto-revarp ]\n" 4423 "\t[ broadcast <broad_addr> ]\n" 4424 "\t[ index <if_index> ]\n" 4425 "\t[ metric <n> ] [ mtu <n> ]\n" 4426 "\t[ netmask <mask> ]\n" 4427 "\t[ plumb ] [ unplumb ]\n" 4428 "\t[ preferred | -preferred ]\n" 4429 "\t[ private | -private ]\n" 4430 "\t[ local | -local ]\n" 4431 "\t[ router | -router ]\n" 4432 "\t[ subnet <subnet_address>]\n" 4433 "\t[ trailers | -trailers ]\n" 4434 "\t[ token <address>/<prefix_length> ]\n" 4435 "\t[ tsrc <tunnel_src_address> ]\n" 4436 "\t[ tdst <tunnel_dest_address> ]\n" 4437 "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n" 4438 "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n" 4439 "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n" 4440 "\t[ up ] [ down ]\n" 4441 "\t[ xmit | -xmit ]\n" 4442 "\t[ modlist ]\n" 4443 "\t[ modinsert <module_name@position> ]\n" 4444 "\t[ modremove <module_name@position> ]\n" 4445 "\t[ ipmp ]\n" 4446 "\t[ group <groupname>] | [ group \"\"]\n" 4447 "\t[ deprecated | -deprecated ]\n" 4448 "\t[ standby | -standby ]\n" 4449 "\t[ failover | -failover ]\n" 4450 "\t[ zone <zonename> | -zone ]\n" 4451 "\t[ usesrc <interface> ]\n" 4452 "\t[ all-zones ]\n"); 4453 4454 (void) fprintf(stderr, "or\n"); 4455 (void) fprintf(stderr, 4456 "\tifconfig <interface> | -a[ 4 | 6 | D ] [ u | d ]\n"); 4457 4458 (void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n" 4459 "\t[ wait <time> | forever ]\n\t[ primary ]\n" 4460 "\tstart | drop | ping | release | status | inform\n"); 4461 } 4462