17c478bd9Sstevel@tonic-gate /* 236b41818SGirish Moodalbail * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 37c478bd9Sstevel@tonic-gate */ 47c478bd9Sstevel@tonic-gate /* 57c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California. 67c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 77c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #include "defs.h" 117c478bd9Sstevel@tonic-gate #include "strings.h" 127c478bd9Sstevel@tonic-gate #include "ifconfig.h" 137c478bd9Sstevel@tonic-gate #include <compat.h> 147c478bd9Sstevel@tonic-gate #include <libdlpi.h> 15da14cebeSEric Cheng #include <libdllink.h> 162b24ab6bSSebastien Roy #include <libdliptun.h> 172b24ab6bSSebastien Roy #include <libdllink.h> 18ff550d0eSmasputra #include <inet/ip.h> 19d2f8a3dfSpwernau #include <inet/ipsec_impl.h> 206e91bba0SGirish Moodalbail #include <libipadm.h> 216e91bba0SGirish Moodalbail #include <ifaddrs.h> 226e91bba0SGirish Moodalbail #include <libsocket_priv.h> 23ff550d0eSmasputra 247c478bd9Sstevel@tonic-gate #define LOOPBACK_IF "lo0" 257c478bd9Sstevel@tonic-gate #define NONE_STR "none" 267c478bd9Sstevel@tonic-gate #define ARP_MOD_NAME "arp" 276e91bba0SGirish Moodalbail #define LIFC_DEFAULT (LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\ 286e91bba0SGirish Moodalbail LIFC_UNDER_IPMP) 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate typedef struct if_flags { 317c478bd9Sstevel@tonic-gate uint64_t iff_value; 327c478bd9Sstevel@tonic-gate char *iff_name; 337c478bd9Sstevel@tonic-gate } if_flags_t; 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate static if_flags_t if_flags_tbl[] = { 367c478bd9Sstevel@tonic-gate { IFF_UP, "UP" }, 377c478bd9Sstevel@tonic-gate { IFF_BROADCAST, "BROADCAST" }, 387c478bd9Sstevel@tonic-gate { IFF_DEBUG, "DEBUG" }, 397c478bd9Sstevel@tonic-gate { IFF_LOOPBACK, "LOOPBACK" }, 407c478bd9Sstevel@tonic-gate { IFF_POINTOPOINT, "POINTOPOINT" }, 417c478bd9Sstevel@tonic-gate { IFF_NOTRAILERS, "NOTRAILERS" }, 427c478bd9Sstevel@tonic-gate { IFF_RUNNING, "RUNNING" }, 437c478bd9Sstevel@tonic-gate { IFF_NOARP, "NOARP" }, 447c478bd9Sstevel@tonic-gate { IFF_PROMISC, "PROMISC" }, 457c478bd9Sstevel@tonic-gate { IFF_ALLMULTI, "ALLMULTI" }, 467c478bd9Sstevel@tonic-gate { IFF_INTELLIGENT, "INTELLIGENT" }, 477c478bd9Sstevel@tonic-gate { IFF_MULTICAST, "MULTICAST" }, 487c478bd9Sstevel@tonic-gate { IFF_MULTI_BCAST, "MULTI_BCAST" }, 497c478bd9Sstevel@tonic-gate { IFF_UNNUMBERED, "UNNUMBERED" }, 507c478bd9Sstevel@tonic-gate { IFF_DHCPRUNNING, "DHCP" }, 517c478bd9Sstevel@tonic-gate { IFF_PRIVATE, "PRIVATE" }, 527c478bd9Sstevel@tonic-gate { IFF_NOXMIT, "NOXMIT" }, 537c478bd9Sstevel@tonic-gate { IFF_NOLOCAL, "NOLOCAL" }, 547c478bd9Sstevel@tonic-gate { IFF_DEPRECATED, "DEPRECATED" }, 557c478bd9Sstevel@tonic-gate { IFF_ADDRCONF, "ADDRCONF" }, 567c478bd9Sstevel@tonic-gate { IFF_ROUTER, "ROUTER" }, 577c478bd9Sstevel@tonic-gate { IFF_NONUD, "NONUD" }, 587c478bd9Sstevel@tonic-gate { IFF_ANYCAST, "ANYCAST" }, 597c478bd9Sstevel@tonic-gate { IFF_NORTEXCH, "NORTEXCH" }, 607c478bd9Sstevel@tonic-gate { IFF_IPV4, "IPv4" }, 617c478bd9Sstevel@tonic-gate { IFF_IPV6, "IPv6" }, 627c478bd9Sstevel@tonic-gate { IFF_NOFAILOVER, "NOFAILOVER" }, 637c478bd9Sstevel@tonic-gate { IFF_FAILED, "FAILED" }, 647c478bd9Sstevel@tonic-gate { IFF_STANDBY, "STANDBY" }, 657c478bd9Sstevel@tonic-gate { IFF_INACTIVE, "INACTIVE" }, 667c478bd9Sstevel@tonic-gate { IFF_OFFLINE, "OFFLINE" }, 677c478bd9Sstevel@tonic-gate { IFF_XRESOLV, "XRESOLV" }, 687c478bd9Sstevel@tonic-gate { IFF_COS_ENABLED, "CoS" }, 697c478bd9Sstevel@tonic-gate { IFF_PREFERRED, "PREFERRED" }, 707c478bd9Sstevel@tonic-gate { IFF_TEMPORARY, "TEMPORARY" }, 717c478bd9Sstevel@tonic-gate { IFF_FIXEDMTU, "FIXEDMTU" }, 7269bb4bb4Scarlsonj { IFF_VIRTUAL, "VIRTUAL" }, 73e11c3f44Smeem { IFF_DUPLICATE, "DUPLICATE" }, 741cb875aeSCathy Zhou { IFF_IPMP, "IPMP"}, 751cb875aeSCathy Zhou { IFF_VRRP, "VRRP"}, 76*550b6e40SSowmini Varadhan { IFF_NOACCEPT, "NOACCEPT"}, 77*550b6e40SSowmini Varadhan { IFF_L3PROTECT, "L3PROTECT"} 78e11c3f44Smeem }; 79e11c3f44Smeem 80e11c3f44Smeem typedef struct { 81e11c3f44Smeem const char *ia_app; 82e11c3f44Smeem uint64_t ia_flag; 83e11c3f44Smeem uint_t ia_tries; 84e11c3f44Smeem } if_appflags_t; 85e11c3f44Smeem 86e11c3f44Smeem static const if_appflags_t if_appflags_tbl[] = { 87e11c3f44Smeem { "dhcpagent(1M)", IFF_DHCPRUNNING, 1 }, 88e11c3f44Smeem { "in.ndpd(1M)", IFF_ADDRCONF, 3 }, 89e11c3f44Smeem { NULL, 0, 0 } 907c478bd9Sstevel@tonic-gate }; 917c478bd9Sstevel@tonic-gate 922b24ab6bSSebastien Roy static dladm_handle_t dlh; 932b24ab6bSSebastien Roy boolean_t dlh_opened; 942b24ab6bSSebastien Roy static struct lifreq lifr; 957906a3e0Smeem /* current interface name a particular function is accessing */ 962b24ab6bSSebastien Roy static char name[LIFNAMSIZ]; 977c478bd9Sstevel@tonic-gate /* foreach interface saved name */ 982b24ab6bSSebastien Roy static char origname[LIFNAMSIZ]; 992b24ab6bSSebastien Roy static int setaddr; 1006e91bba0SGirish Moodalbail static boolean_t setaddr_done = _B_FALSE; 1012b24ab6bSSebastien Roy static boolean_t ipsec_policy_set; 1022b24ab6bSSebastien Roy static boolean_t ipsec_auth_covered; 1036e91bba0SGirish Moodalbail static ipadm_handle_t iph; 1046e91bba0SGirish Moodalbail static ipadm_addrobj_t ipaddr; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * Make sure the algorithm variables hold more than the sizeof an algorithm 1087c478bd9Sstevel@tonic-gate * in PF_KEY. (For now, more than a uint8_t.) The NO_***_?ALG indicates that 1097c478bd9Sstevel@tonic-gate * there was no algorithm requested, and in the ipsec_req that service should 1107c478bd9Sstevel@tonic-gate * be disabled. (E.g. if ah_aalg remains NO_AH_AALG, then AH will be 1117c478bd9Sstevel@tonic-gate * disabled on that tunnel.) 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate #define NO_AH_AALG 256 1147c478bd9Sstevel@tonic-gate #define NO_ESP_AALG 256 1157c478bd9Sstevel@tonic-gate #define NO_ESP_EALG 256 1167c478bd9Sstevel@tonic-gate 117e11c3f44Smeem int s, s4, s6; 1187c478bd9Sstevel@tonic-gate int af = AF_INET; /* default address family */ 1197c478bd9Sstevel@tonic-gate int debug = 0; 1207c478bd9Sstevel@tonic-gate int all = 0; /* setifdhcp() needs to know this */ 1217c478bd9Sstevel@tonic-gate int verbose = 0; 1227c478bd9Sstevel@tonic-gate int v4compat = 0; /* Compatible printing format */ 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * Function prototypes for command functions. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate static int addif(char *arg, int64_t param); 128e11c3f44Smeem static int inetipmp(char *arg, int64_t param); 1297c478bd9Sstevel@tonic-gate static int inetplumb(char *arg, int64_t param); 1307c478bd9Sstevel@tonic-gate static int inetunplumb(char *arg, int64_t param); 1317c478bd9Sstevel@tonic-gate static int removeif(char *arg, int64_t param); 1327c478bd9Sstevel@tonic-gate static int setdebugflag(char *arg, int64_t param); 1337c478bd9Sstevel@tonic-gate static int setifaddr(char *arg, int64_t param); 1347c478bd9Sstevel@tonic-gate static int setifbroadaddr(char *arg, int64_t param); 1357c478bd9Sstevel@tonic-gate static int setifdstaddr(char *arg, int64_t param); 1367c478bd9Sstevel@tonic-gate static int setifether(char *arg, int64_t param); 1377c478bd9Sstevel@tonic-gate static int setifflags(char *arg, int64_t param); 1387c478bd9Sstevel@tonic-gate static int setifindex(char *arg, int64_t param); 1397c478bd9Sstevel@tonic-gate static int setifmetric(char *arg, int64_t param); 1407c478bd9Sstevel@tonic-gate static int setifmtu(char *arg, int64_t param); 1417c478bd9Sstevel@tonic-gate static int setifnetmask(char *arg, int64_t param); 1427c478bd9Sstevel@tonic-gate static int setifprefixlen(char *arg, int64_t param); 1437c478bd9Sstevel@tonic-gate static int setifrevarp(char *arg, int64_t param); 1447c478bd9Sstevel@tonic-gate static int setifsubnet(char *arg, int64_t param); 1457c478bd9Sstevel@tonic-gate static int setiftdst(char *arg, int64_t param); 1467c478bd9Sstevel@tonic-gate static int setiftoken(char *arg, int64_t param); 1477c478bd9Sstevel@tonic-gate static int setiftsrc(char *arg, int64_t param); 1487c478bd9Sstevel@tonic-gate static int setverboseflag(char *arg, int64_t param); 1497c478bd9Sstevel@tonic-gate static int set_tun_ah_alg(char *arg, int64_t param); 1507c478bd9Sstevel@tonic-gate static int set_tun_esp_auth_alg(char *arg, int64_t param); 1517c478bd9Sstevel@tonic-gate static int set_tun_esp_encr_alg(char *arg, int64_t param); 1527c478bd9Sstevel@tonic-gate static int modlist(char *arg, int64_t param); 1537c478bd9Sstevel@tonic-gate static int modinsert(char *arg, int64_t param); 1547c478bd9Sstevel@tonic-gate static int modremove(char *arg, int64_t param); 1557c478bd9Sstevel@tonic-gate static int setifgroupname(char *arg, int64_t param); 1567c478bd9Sstevel@tonic-gate static int configinfo(char *arg, int64_t param); 157e11c3f44Smeem static void print_config_flags(int af, uint64_t flags); 1587c478bd9Sstevel@tonic-gate static void print_flags(uint64_t flags); 1596e91bba0SGirish Moodalbail static void print_ifether(const char *ifname); 1607c478bd9Sstevel@tonic-gate static int set_tun_encap_limit(char *arg, int64_t param); 1617c478bd9Sstevel@tonic-gate static int clr_tun_encap_limit(char *arg, int64_t param); 1627c478bd9Sstevel@tonic-gate static int set_tun_hop_limit(char *arg, int64_t param); 1637c478bd9Sstevel@tonic-gate static int setzone(char *arg, int64_t param); 16445916cd2Sjpk static int setallzones(char *arg, int64_t param); 1657c478bd9Sstevel@tonic-gate static int setifsrc(char *arg, int64_t param); 166e11c3f44Smeem static int lifnum(const char *ifname); 1676e91bba0SGirish Moodalbail static void plumball(int, char **, int64_t, int64_t, int64_t); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Address family specific function prototypes. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate static void in_getaddr(char *s, struct sockaddr *saddr, int *plenp); 1737c478bd9Sstevel@tonic-gate static void in_status(int force, uint64_t flags); 1747c478bd9Sstevel@tonic-gate static void in_configinfo(int force, uint64_t flags); 1757c478bd9Sstevel@tonic-gate static void in6_getaddr(char *s, struct sockaddr *saddr, int *plenp); 1767c478bd9Sstevel@tonic-gate static void in6_status(int force, uint64_t flags); 1777c478bd9Sstevel@tonic-gate static void in6_configinfo(int force, uint64_t flags); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * Misc support functions 1817c478bd9Sstevel@tonic-gate */ 182d62bc4baSyz static boolean_t ni_entry(const char *, void *); 1836e91bba0SGirish Moodalbail static void foreachinterface(int argc, char *argv[], 1846e91bba0SGirish Moodalbail int af, int64_t onflags, int64_t offflags, 1856e91bba0SGirish Moodalbail int64_t lifc_flags); 1866e91bba0SGirish Moodalbail static void ifconfig(int argc, char *argv[], int af, 1876e91bba0SGirish Moodalbail struct ifaddrs *ifa); 188dd7a6f5fSkcpoon static boolean_t in_getmask(struct sockaddr_in *saddr, 189dd7a6f5fSkcpoon boolean_t addr_set); 1906e91bba0SGirish Moodalbail static int in_getprefixlen(char *addr, boolean_t slash, int plen); 1917c478bd9Sstevel@tonic-gate static boolean_t in_prefixlentomask(int prefixlen, int maxlen, 1927c478bd9Sstevel@tonic-gate uchar_t *mask); 1936e91bba0SGirish Moodalbail static void status(void); 1946e91bba0SGirish Moodalbail static void ifstatus(const char *ifname); 1956e91bba0SGirish Moodalbail static void tun_status(datalink_id_t); 1966e91bba0SGirish Moodalbail static void usage(void); 1976e91bba0SGirish Moodalbail static int setifdhcp(const char *caller, const char *ifname, 1986e91bba0SGirish Moodalbail int argc, char *argv[]); 1996e91bba0SGirish Moodalbail static int ip_domux2fd(int *, int *, int *, int *, int *); 2006e91bba0SGirish Moodalbail static int ip_plink(int, int, int, int, int); 2016e91bba0SGirish Moodalbail static int modop(char *arg, char op); 2026e91bba0SGirish Moodalbail static int find_all_interfaces(struct lifconf *lifcp, char **buf, 2036e91bba0SGirish Moodalbail int64_t lifc_flags); 2046e91bba0SGirish Moodalbail static int create_ipmp(const char *grname, int af, 2056e91bba0SGirish Moodalbail const char *ifname, boolean_t implicit); 2066e91bba0SGirish Moodalbail static void start_ipmp_daemon(void); 2076e91bba0SGirish Moodalbail static boolean_t ifaddr_up(ifaddrlistx_t *ifaddrp); 2086e91bba0SGirish Moodalbail static boolean_t ifaddr_down(ifaddrlistx_t *ifaddrp); 2092b24ab6bSSebastien Roy static dladm_status_t ifconfig_dladm_open(const char *, datalink_class_t, 2106e91bba0SGirish Moodalbail datalink_id_t *); 2116e91bba0SGirish Moodalbail static void dladmerr_exit(dladm_status_t status, const char *str); 2126e91bba0SGirish Moodalbail static void ipadmerr_exit(ipadm_status_t status, const char *str); 2136e91bba0SGirish Moodalbail static boolean_t ifconfig_use_libipadm(int, const char *); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a)) 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there 2197c478bd9Sstevel@tonic-gate * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep 2207c478bd9Sstevel@tonic-gate * on going rather than exit with an error. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #define DHCP_EXIT_IF_FAILURE -1 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate #define NEXTARG 0xffffff /* command takes an argument */ 2267c478bd9Sstevel@tonic-gate #define OPTARG 0xfffffe /* command takes an optional argument */ 2277c478bd9Sstevel@tonic-gate #define AF_ANY (-1) 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */ 2307c478bd9Sstevel@tonic-gate #define NETMASK_CMD "netmask" 2317c478bd9Sstevel@tonic-gate struct sockaddr_storage g_netmask; 232dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET } 233dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate struct cmd { 2367c478bd9Sstevel@tonic-gate char *c_name; 2377c478bd9Sstevel@tonic-gate int64_t c_parameter; /* NEXTARG means next argv */ 2387c478bd9Sstevel@tonic-gate int (*c_func)(char *, int64_t); 2397c478bd9Sstevel@tonic-gate int c_abortonfail; /* don't continue parsing args */ 2407c478bd9Sstevel@tonic-gate /* for the current interface */ 2417c478bd9Sstevel@tonic-gate int c_af; /* address family restrictions */ 2427c478bd9Sstevel@tonic-gate } cmds[] = { 2437c478bd9Sstevel@tonic-gate { "up", IFF_UP, setifflags, 0, AF_ANY }, 2447c478bd9Sstevel@tonic-gate { "down", -IFF_UP, setifflags, 0, AF_ANY }, 2457c478bd9Sstevel@tonic-gate { "trailers", -IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 2467c478bd9Sstevel@tonic-gate { "-trailers", IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 2477c478bd9Sstevel@tonic-gate { "arp", -IFF_NOARP, setifflags, 0, AF_INET }, 2487c478bd9Sstevel@tonic-gate { "-arp", IFF_NOARP, setifflags, 0, AF_INET }, 2497c478bd9Sstevel@tonic-gate { "router", IFF_ROUTER, setifflags, 0, AF_ANY }, 2507c478bd9Sstevel@tonic-gate { "-router", -IFF_ROUTER, setifflags, 0, AF_ANY }, 2517c478bd9Sstevel@tonic-gate { "private", IFF_PRIVATE, setifflags, 0, AF_ANY }, 2527c478bd9Sstevel@tonic-gate { "-private", -IFF_PRIVATE, setifflags, 0, AF_ANY }, 2537c478bd9Sstevel@tonic-gate { "xmit", -IFF_NOXMIT, setifflags, 0, AF_ANY }, 2547c478bd9Sstevel@tonic-gate { "-xmit", IFF_NOXMIT, setifflags, 0, AF_ANY }, 2557c478bd9Sstevel@tonic-gate { "-nud", IFF_NONUD, setifflags, 0, AF_INET6 }, 2567c478bd9Sstevel@tonic-gate { "nud", -IFF_NONUD, setifflags, 0, AF_INET6 }, 2577c478bd9Sstevel@tonic-gate { "anycast", IFF_ANYCAST, setifflags, 0, AF_ANY }, 2587c478bd9Sstevel@tonic-gate { "-anycast", -IFF_ANYCAST, setifflags, 0, AF_ANY }, 2597c478bd9Sstevel@tonic-gate { "local", -IFF_NOLOCAL, setifflags, 0, AF_ANY }, 2607c478bd9Sstevel@tonic-gate { "-local", IFF_NOLOCAL, setifflags, 0, AF_ANY }, 2617c478bd9Sstevel@tonic-gate { "deprecated", IFF_DEPRECATED, setifflags, 0, AF_ANY }, 2627c478bd9Sstevel@tonic-gate { "-deprecated", -IFF_DEPRECATED, setifflags, 0, AF_ANY }, 2637c478bd9Sstevel@tonic-gate { "preferred", IFF_PREFERRED, setifflags, 0, AF_INET6 }, 2647c478bd9Sstevel@tonic-gate { "-preferred", -IFF_PREFERRED, setifflags, 0, AF_INET6 }, 2657c478bd9Sstevel@tonic-gate { "debug", 0, setdebugflag, 0, AF_ANY }, 2667c478bd9Sstevel@tonic-gate { "verbose", 0, setverboseflag, 0, AF_ANY }, 2677c478bd9Sstevel@tonic-gate { NETMASK_CMD, NEXTARG, setifnetmask, 0, AF_INET }, 2687c478bd9Sstevel@tonic-gate { "metric", NEXTARG, setifmetric, 0, AF_ANY }, 2697c478bd9Sstevel@tonic-gate { "mtu", NEXTARG, setifmtu, 0, AF_ANY }, 2707c478bd9Sstevel@tonic-gate { "index", NEXTARG, setifindex, 0, AF_ANY }, 2717c478bd9Sstevel@tonic-gate { "broadcast", NEXTARG, setifbroadaddr, 0, AF_INET }, 2727c478bd9Sstevel@tonic-gate { "auto-revarp", 0, setifrevarp, 1, AF_INET }, 273e11c3f44Smeem { "ipmp", 0, inetipmp, 1, AF_ANY }, 2747c478bd9Sstevel@tonic-gate { "plumb", 0, inetplumb, 1, AF_ANY }, 2757c478bd9Sstevel@tonic-gate { "unplumb", 0, inetunplumb, 0, AF_ANY }, 2767c478bd9Sstevel@tonic-gate { "subnet", NEXTARG, setifsubnet, 0, AF_ANY }, 2777c478bd9Sstevel@tonic-gate { "token", NEXTARG, setiftoken, 0, AF_INET6 }, 2787c478bd9Sstevel@tonic-gate { "tsrc", NEXTARG, setiftsrc, 0, AF_ANY }, 2797c478bd9Sstevel@tonic-gate { "tdst", NEXTARG, setiftdst, 0, AF_ANY }, 2807c478bd9Sstevel@tonic-gate { "encr_auth_algs", NEXTARG, set_tun_esp_auth_alg, 0, AF_ANY }, 2817c478bd9Sstevel@tonic-gate { "encr_algs", NEXTARG, set_tun_esp_encr_alg, 0, AF_ANY }, 2827c478bd9Sstevel@tonic-gate { "auth_algs", NEXTARG, set_tun_ah_alg, 0, AF_ANY }, 2837c478bd9Sstevel@tonic-gate { "addif", NEXTARG, addif, 1, AF_ANY }, 2847c478bd9Sstevel@tonic-gate { "removeif", NEXTARG, removeif, 1, AF_ANY }, 2857c478bd9Sstevel@tonic-gate { "modlist", 0, modlist, 1, AF_ANY }, 2867c478bd9Sstevel@tonic-gate { "modinsert", NEXTARG, modinsert, 1, AF_ANY }, 2877c478bd9Sstevel@tonic-gate { "modremove", NEXTARG, modremove, 1, AF_ANY }, 2887c478bd9Sstevel@tonic-gate { "failover", -IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 2897c478bd9Sstevel@tonic-gate { "-failover", IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 2907c478bd9Sstevel@tonic-gate { "standby", IFF_STANDBY, setifflags, 1, AF_ANY }, 2917c478bd9Sstevel@tonic-gate { "-standby", -IFF_STANDBY, setifflags, 1, AF_ANY }, 2927c478bd9Sstevel@tonic-gate { "failed", IFF_FAILED, setifflags, 1, AF_ANY }, 2937c478bd9Sstevel@tonic-gate { "-failed", -IFF_FAILED, setifflags, 1, AF_ANY }, 2947c478bd9Sstevel@tonic-gate { "group", NEXTARG, setifgroupname, 1, AF_ANY }, 2957c478bd9Sstevel@tonic-gate { "configinfo", 0, configinfo, 1, AF_ANY }, 2967906a3e0Smeem { "encaplimit", NEXTARG, set_tun_encap_limit, 0, AF_ANY }, 2977906a3e0Smeem { "-encaplimit", 0, clr_tun_encap_limit, 0, AF_ANY }, 2987906a3e0Smeem { "thoplimit", NEXTARG, set_tun_hop_limit, 0, AF_ANY }, 2997c478bd9Sstevel@tonic-gate { "set", NEXTARG, setifaddr, 0, AF_ANY }, 3007c478bd9Sstevel@tonic-gate { "destination", NEXTARG, setifdstaddr, 0, AF_ANY }, 3017c478bd9Sstevel@tonic-gate { "zone", NEXTARG, setzone, 0, AF_ANY }, 3027c478bd9Sstevel@tonic-gate { "-zone", 0, setzone, 0, AF_ANY }, 30345916cd2Sjpk { "all-zones", 0, setallzones, 0, AF_ANY }, 3047c478bd9Sstevel@tonic-gate { "ether", OPTARG, setifether, 0, AF_ANY }, 3057c478bd9Sstevel@tonic-gate { "usesrc", NEXTARG, setifsrc, 0, AF_ANY }, 306f7d61273Smeem 307f7d61273Smeem /* 308f7d61273Smeem * NOTE: any additions to this table must also be applied to ifparse 309f7d61273Smeem * (usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c) 310f7d61273Smeem */ 311f7d61273Smeem 3127c478bd9Sstevel@tonic-gate { 0, 0, setifaddr, 0, AF_ANY }, 3137c478bd9Sstevel@tonic-gate { 0, 0, setifdstaddr, 0, AF_ANY }, 3147c478bd9Sstevel@tonic-gate { 0, 0, 0, 0, 0 }, 3157c478bd9Sstevel@tonic-gate }; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate typedef struct if_config_cmd { 3197c478bd9Sstevel@tonic-gate uint64_t iff_flag; 320e11c3f44Smeem int iff_af; 3217c478bd9Sstevel@tonic-gate char *iff_name; 3227c478bd9Sstevel@tonic-gate } if_config_cmd_t; 3237c478bd9Sstevel@tonic-gate 324e11c3f44Smeem /* 325e11c3f44Smeem * NOTE: print_config_flags() processes this table in order, so we put "up" 326e11c3f44Smeem * last so that we can be sure "-failover" will take effect first. Otherwise, 327e11c3f44Smeem * IPMP test addresses will erroneously migrate to the IPMP interface. 328e11c3f44Smeem */ 3297c478bd9Sstevel@tonic-gate static if_config_cmd_t if_config_cmd_tbl[] = { 330e11c3f44Smeem { IFF_NOTRAILERS, AF_UNSPEC, "-trailers" }, 331e11c3f44Smeem { IFF_PRIVATE, AF_UNSPEC, "private" }, 332e11c3f44Smeem { IFF_NOXMIT, AF_UNSPEC, "-xmit" }, 333e11c3f44Smeem { IFF_ANYCAST, AF_INET6, "anycast" }, 334e11c3f44Smeem { IFF_NOLOCAL, AF_UNSPEC, "-local" }, 335e11c3f44Smeem { IFF_DEPRECATED, AF_UNSPEC, "deprecated" }, 336e11c3f44Smeem { IFF_NOFAILOVER, AF_UNSPEC, "-failover" }, 337e11c3f44Smeem { IFF_STANDBY, AF_UNSPEC, "standby" }, 338e11c3f44Smeem { IFF_FAILED, AF_UNSPEC, "failed" }, 339e11c3f44Smeem { IFF_PREFERRED, AF_UNSPEC, "preferred" }, 340e11c3f44Smeem { IFF_NONUD, AF_INET6, "-nud" }, 341e11c3f44Smeem { IFF_NOARP, AF_INET, "-arp" }, 342e11c3f44Smeem { IFF_UP, AF_UNSPEC, "up" }, 343e11c3f44Smeem { 0, 0, NULL }, 3447c478bd9Sstevel@tonic-gate }; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate typedef struct ni { 3477c478bd9Sstevel@tonic-gate char ni_name[LIFNAMSIZ]; 3487c478bd9Sstevel@tonic-gate struct ni *ni_next; 3497c478bd9Sstevel@tonic-gate } ni_t; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate static ni_t *ni_list = NULL; 3527c478bd9Sstevel@tonic-gate static int num_ni = 0; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */ 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* Known address families */ 3577c478bd9Sstevel@tonic-gate struct afswtch { 3587c478bd9Sstevel@tonic-gate char *af_name; 3597c478bd9Sstevel@tonic-gate short af_af; 3607c478bd9Sstevel@tonic-gate void (*af_status)(); 3617c478bd9Sstevel@tonic-gate void (*af_getaddr)(); 3627c478bd9Sstevel@tonic-gate void (*af_configinfo)(); 3637c478bd9Sstevel@tonic-gate } afs[] = { 3647c478bd9Sstevel@tonic-gate { "inet", AF_INET, in_status, in_getaddr, in_configinfo }, 3657c478bd9Sstevel@tonic-gate { "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo }, 3667c478bd9Sstevel@tonic-gate { 0, 0, 0, 0, 0 } 3677c478bd9Sstevel@tonic-gate }; 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate struct afswtch *afp; /* the address family being set or asked about */ 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate int 3747c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3757c478bd9Sstevel@tonic-gate { 3762b24ab6bSSebastien Roy int64_t lifc_flags; 3772b24ab6bSSebastien Roy char *default_ip_str; 3786e91bba0SGirish Moodalbail ipadm_status_t istatus; 3797c478bd9Sstevel@tonic-gate 3806e91bba0SGirish Moodalbail lifc_flags = LIFC_DEFAULT; 381e11c3f44Smeem 3827c478bd9Sstevel@tonic-gate if (argc < 2) { 3837c478bd9Sstevel@tonic-gate usage(); 3847c478bd9Sstevel@tonic-gate exit(1); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate argc--, argv++; 3877c478bd9Sstevel@tonic-gate if (strlen(*argv) > sizeof (name) - 1) { 3887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: interface name too long\n", *argv); 3897c478bd9Sstevel@tonic-gate exit(1); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate (void) strncpy(name, *argv, sizeof (name)); 3927c478bd9Sstevel@tonic-gate name[sizeof (name) - 1] = '\0'; 3937c478bd9Sstevel@tonic-gate (void) strncpy(origname, name, sizeof (origname)); /* For addif */ 3947c478bd9Sstevel@tonic-gate default_ip_str = NULL; 3957c478bd9Sstevel@tonic-gate v4compat = get_compat_flag(&default_ip_str); 3967c478bd9Sstevel@tonic-gate if (v4compat == DEFAULT_PROT_BAD_VALUE) { 3977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3987c478bd9Sstevel@tonic-gate "ifconfig: %s: Bad value for %s in %s\n", default_ip_str, 3997c478bd9Sstevel@tonic-gate DEFAULT_IP, INET_DEFAULT_FILE); 4007c478bd9Sstevel@tonic-gate free(default_ip_str); 4017c478bd9Sstevel@tonic-gate exit(2); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate free(default_ip_str); 4047c478bd9Sstevel@tonic-gate argc--, argv++; 4057c478bd9Sstevel@tonic-gate if (argc > 0) { 4067c478bd9Sstevel@tonic-gate struct afswtch *myafp; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate for (myafp = afp = afs; myafp->af_name; myafp++) { 4097c478bd9Sstevel@tonic-gate if (strcmp(myafp->af_name, *argv) == 0) { 4107c478bd9Sstevel@tonic-gate afp = myafp; argc--; argv++; 4117c478bd9Sstevel@tonic-gate break; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family = afp->af_af; 4157c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 4167c478bd9Sstevel@tonic-gate v4compat = 0; 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate s = socket(SOCKET_AF(af), SOCK_DGRAM, 0); 421e11c3f44Smeem s4 = socket(AF_INET, SOCK_DGRAM, 0); 422e11c3f44Smeem s6 = socket(AF_INET6, SOCK_DGRAM, 0); 423e11c3f44Smeem if (s == -1 || s4 == -1 || s6 == -1) 4247c478bd9Sstevel@tonic-gate Perror0_exit("socket"); 4256e91bba0SGirish Moodalbail /* 4266e91bba0SGirish Moodalbail * Open the global libipadm handle. The flag IPH_LEGACY has to 4276e91bba0SGirish Moodalbail * be specified to indicate that logical interface names will 4286e91bba0SGirish Moodalbail * be used during interface creation and address creation. 4296e91bba0SGirish Moodalbail */ 4306e91bba0SGirish Moodalbail if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS) 4316e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "unable to open handle to libipadm"); 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Special interface names is any combination of these flags. 4357c478bd9Sstevel@tonic-gate * Note that due to the ifconfig syntax they have to be combined 4367c478bd9Sstevel@tonic-gate * as a single '-' option. 4377c478bd9Sstevel@tonic-gate * -a All interfaces 4387c478bd9Sstevel@tonic-gate * -u "up" interfaces 4397c478bd9Sstevel@tonic-gate * -d "down" interfaces 4407c478bd9Sstevel@tonic-gate * -D Interfaces not controlled by DHCP 4417c478bd9Sstevel@tonic-gate * -4 IPv4 interfaces 4427c478bd9Sstevel@tonic-gate * -6 IPv6 interfaces 4437c478bd9Sstevel@tonic-gate * -X Turn on debug (not documented) 4447c478bd9Sstevel@tonic-gate * -v Turn on verbose 4457c478bd9Sstevel@tonic-gate * -Z Only interfaces in caller's zone 4467c478bd9Sstevel@tonic-gate */ 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if (name[0] == '-') { 4497c478bd9Sstevel@tonic-gate /* One or more options */ 4507c478bd9Sstevel@tonic-gate int64_t onflags = 0; 4517c478bd9Sstevel@tonic-gate int64_t offflags = 0; 4527c478bd9Sstevel@tonic-gate int c; 4537c478bd9Sstevel@tonic-gate char *av[2] = { "ifconfig", name }; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate while ((c = getopt(2, av, "audDXZ46v")) != -1) { 4567c478bd9Sstevel@tonic-gate switch ((char)c) { 4577c478bd9Sstevel@tonic-gate case 'a': 4587c478bd9Sstevel@tonic-gate all = 1; 4597c478bd9Sstevel@tonic-gate break; 4607c478bd9Sstevel@tonic-gate case 'u': 4617c478bd9Sstevel@tonic-gate onflags |= IFF_UP; 4627c478bd9Sstevel@tonic-gate break; 4637c478bd9Sstevel@tonic-gate case 'd': 4647c478bd9Sstevel@tonic-gate offflags |= IFF_UP; 4657c478bd9Sstevel@tonic-gate break; 4667c478bd9Sstevel@tonic-gate case 'D': 4677c478bd9Sstevel@tonic-gate offflags |= IFF_DHCPRUNNING; 4687c478bd9Sstevel@tonic-gate break; 4697c478bd9Sstevel@tonic-gate case 'X': 4707c478bd9Sstevel@tonic-gate debug += 3; 4717c478bd9Sstevel@tonic-gate break; 4727c478bd9Sstevel@tonic-gate case 'Z': 4737c478bd9Sstevel@tonic-gate lifc_flags &= ~LIFC_ALLZONES; 4747c478bd9Sstevel@tonic-gate break; 4757c478bd9Sstevel@tonic-gate case '4': 4767c478bd9Sstevel@tonic-gate /* 4777c478bd9Sstevel@tonic-gate * -4 is not a compatable flag, therefore 4787c478bd9Sstevel@tonic-gate * we assume they want v4compat turned off 4797c478bd9Sstevel@tonic-gate */ 4807c478bd9Sstevel@tonic-gate v4compat = 0; 4817c478bd9Sstevel@tonic-gate onflags |= IFF_IPV4; 4827c478bd9Sstevel@tonic-gate break; 4837c478bd9Sstevel@tonic-gate case '6': 4847c478bd9Sstevel@tonic-gate /* 4857c478bd9Sstevel@tonic-gate * If they want IPv6, well then we'll assume 4867c478bd9Sstevel@tonic-gate * they don't want IPv4 compat 4877c478bd9Sstevel@tonic-gate */ 4887c478bd9Sstevel@tonic-gate v4compat = 0; 4897c478bd9Sstevel@tonic-gate onflags |= IFF_IPV6; 4907c478bd9Sstevel@tonic-gate break; 4917c478bd9Sstevel@tonic-gate case 'v': 4927c478bd9Sstevel@tonic-gate verbose = 1; 4937c478bd9Sstevel@tonic-gate break; 4947c478bd9Sstevel@tonic-gate case '?': 4957c478bd9Sstevel@tonic-gate usage(); 4967c478bd9Sstevel@tonic-gate exit(1); 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate if (!all) { 5007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 501c7e4935fSss "ifconfig: %s: no such interface\n", name); 5027c478bd9Sstevel@tonic-gate exit(1); 5037c478bd9Sstevel@tonic-gate } 5046e91bba0SGirish Moodalbail foreachinterface(argc, argv, af, onflags, offflags, 5057c478bd9Sstevel@tonic-gate lifc_flags); 5067c478bd9Sstevel@tonic-gate } else { 5076e91bba0SGirish Moodalbail ifconfig(argc, argv, af, NULL); 5087c478bd9Sstevel@tonic-gate } 5096e91bba0SGirish Moodalbail ipadm_close(iph); 5107c478bd9Sstevel@tonic-gate return (0); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate /* 5146e91bba0SGirish Moodalbail * For each interface, call ifconfig(argc, argv, af, ifa). 5157c478bd9Sstevel@tonic-gate * Only call function if onflags and offflags are set or clear, respectively, 5167c478bd9Sstevel@tonic-gate * in the interfaces flags field. 5177c478bd9Sstevel@tonic-gate */ 5187c478bd9Sstevel@tonic-gate static void 5196e91bba0SGirish Moodalbail foreachinterface(int argc, char *argv[], int af, 5207c478bd9Sstevel@tonic-gate int64_t onflags, int64_t offflags, int64_t lifc_flags) 5217c478bd9Sstevel@tonic-gate { 5226e91bba0SGirish Moodalbail ipadm_addr_info_t *ainfo, *ainfop; 5236e91bba0SGirish Moodalbail struct ifaddrs *ifa; 5246e91bba0SGirish Moodalbail ipadm_status_t istatus; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * Special case: 5282b24ab6bSSebastien Roy * ifconfig -a plumb should find all network interfaces in the current 5292b24ab6bSSebastien Roy * zone. 5307c478bd9Sstevel@tonic-gate */ 5317c478bd9Sstevel@tonic-gate if (argc > 0 && (strcmp(*argv, "plumb") == 0)) { 5326e91bba0SGirish Moodalbail plumball(argc, argv, onflags, offflags, lifc_flags); 5336e91bba0SGirish Moodalbail return; 5347c478bd9Sstevel@tonic-gate } 5356e91bba0SGirish Moodalbail /* Get all addresses in kernel including addresses that are zero. */ 5366e91bba0SGirish Moodalbail istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR, 5376e91bba0SGirish Moodalbail lifc_flags); 5386e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 5396e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not get addresses from kernel"); 5407c478bd9Sstevel@tonic-gate 5416e91bba0SGirish Moodalbail /* 5426e91bba0SGirish Moodalbail * For each logical interface, call ifconfig() with the 5436e91bba0SGirish Moodalbail * given arguments. 5446e91bba0SGirish Moodalbail */ 5456e91bba0SGirish Moodalbail for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) { 5466e91bba0SGirish Moodalbail if (ainfop->ia_state == IFA_DISABLED) 5476e91bba0SGirish Moodalbail continue; 5486e91bba0SGirish Moodalbail ifa = &ainfop->ia_ifa; 5497c478bd9Sstevel@tonic-gate if (onflags || offflags) { 5506e91bba0SGirish Moodalbail if ((ifa->ifa_flags & onflags) != onflags) 5517c478bd9Sstevel@tonic-gate continue; 5526e91bba0SGirish Moodalbail if ((~ifa->ifa_flags & offflags) != offflags) 5537c478bd9Sstevel@tonic-gate continue; 5547c478bd9Sstevel@tonic-gate } 5556e91bba0SGirish Moodalbail s = (ifa->ifa_addr->ss_family == AF_INET ? s4 : s6); 5566e91bba0SGirish Moodalbail (void) strncpy(name, ifa->ifa_name, sizeof (name)); 5576e91bba0SGirish Moodalbail (void) strncpy(origname, name, sizeof (origname)); 5586e91bba0SGirish Moodalbail ifconfig(argc, argv, af, ifa); 5596e91bba0SGirish Moodalbail } 5606e91bba0SGirish Moodalbail ipadm_free_addr_info(ainfo); 5616e91bba0SGirish Moodalbail } 5627c478bd9Sstevel@tonic-gate 5636e91bba0SGirish Moodalbail /* 5646e91bba0SGirish Moodalbail * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface. 5656e91bba0SGirish Moodalbail */ 5666e91bba0SGirish Moodalbail static void 5676e91bba0SGirish Moodalbail plumball(int argc, char *argv[], int64_t onflags, int64_t offflags, 5686e91bba0SGirish Moodalbail int64_t lifc_flags) 5696e91bba0SGirish Moodalbail { 5706e91bba0SGirish Moodalbail int n; 5716e91bba0SGirish Moodalbail struct lifreq *lifrp; 5726e91bba0SGirish Moodalbail struct lifconf lifc; 5736e91bba0SGirish Moodalbail char *buf; 5747c478bd9Sstevel@tonic-gate 5756e91bba0SGirish Moodalbail if (onflags != 0 || offflags != 0) { 5766e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: invalid syntax used to " 5776e91bba0SGirish Moodalbail "plumb all interfaces.\n"); 5786e91bba0SGirish Moodalbail exit(1); 5796e91bba0SGirish Moodalbail } 5806e91bba0SGirish Moodalbail 5816e91bba0SGirish Moodalbail if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 || 5826e91bba0SGirish Moodalbail lifc.lifc_len == 0) 5836e91bba0SGirish Moodalbail return; 5847c478bd9Sstevel@tonic-gate 5856e91bba0SGirish Moodalbail lifrp = lifc.lifc_req; 5866e91bba0SGirish Moodalbail for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) { 5877c478bd9Sstevel@tonic-gate /* 5887c478bd9Sstevel@tonic-gate * Reset global state 5897c478bd9Sstevel@tonic-gate * setaddr: Used by parser to tear apart source and dest 5907c478bd9Sstevel@tonic-gate * name and origname contain the name of the 'current' 5917c478bd9Sstevel@tonic-gate * interface. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate setaddr = 0; 5947c478bd9Sstevel@tonic-gate (void) strncpy(name, lifrp->lifr_name, sizeof (name)); 5957c478bd9Sstevel@tonic-gate (void) strncpy(origname, name, sizeof (origname)); 5966e91bba0SGirish Moodalbail ifconfig(argc, argv, af, NULL); 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate /* 6016e91bba0SGirish Moodalbail * Parses the interface name and the command in argv[]. Calls the 6026e91bba0SGirish Moodalbail * appropriate callback function for the given command from `cmds[]' 6036e91bba0SGirish Moodalbail * table. 6046e91bba0SGirish Moodalbail * If there is no command specified, it prints all addresses. 6057c478bd9Sstevel@tonic-gate */ 6067c478bd9Sstevel@tonic-gate static void 6076e91bba0SGirish Moodalbail ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa) 6087c478bd9Sstevel@tonic-gate { 6097c478bd9Sstevel@tonic-gate static boolean_t scan_netmask = _B_FALSE; 6107c478bd9Sstevel@tonic-gate int ret; 6116e91bba0SGirish Moodalbail ipadm_status_t istatus; 6126e91bba0SGirish Moodalbail struct lifreq lifr; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate if (argc == 0) { 6157c478bd9Sstevel@tonic-gate status(); 6167c478bd9Sstevel@tonic-gate return; 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) { 620d04ccbb3Scarlsonj /* 621d04ccbb3Scarlsonj * Some errors are ignored in the case where more than one 622d04ccbb3Scarlsonj * interface is being operated on. 623d04ccbb3Scarlsonj */ 624d04ccbb3Scarlsonj ret = setifdhcp("ifconfig", name, argc, argv); 625d04ccbb3Scarlsonj if (ret == DHCP_EXIT_IF_FAILURE) { 626d04ccbb3Scarlsonj if (!all) 627d04ccbb3Scarlsonj exit(DHCP_EXIT_FAILURE); 628d04ccbb3Scarlsonj } else if (ret != DHCP_EXIT_SUCCESS) { 629d04ccbb3Scarlsonj exit(ret); 630d04ccbb3Scarlsonj } 6317c478bd9Sstevel@tonic-gate return; 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* 6357c478bd9Sstevel@tonic-gate * The following is a "hack" to get around the existing interface 6367c478bd9Sstevel@tonic-gate * setting mechanism. Currently, each interface attribute, 6377c478bd9Sstevel@tonic-gate * such as address, netmask, broadcast, ... is set separately. But 6387c478bd9Sstevel@tonic-gate * sometimes two or more attributes must be set together. For 6397c478bd9Sstevel@tonic-gate * example, setting an address without a netmask does not make sense. 6407c478bd9Sstevel@tonic-gate * Yet they can be set separately for IPv4 address using the current 6417c478bd9Sstevel@tonic-gate * ifconfig(1M) syntax. The kernel then "infers" the correct netmask 6427c478bd9Sstevel@tonic-gate * using the deprecated "IP address classes." This is simply not 6437c478bd9Sstevel@tonic-gate * correct. 6447c478bd9Sstevel@tonic-gate * 6457c478bd9Sstevel@tonic-gate * The "hack" below is to go thru the whole command list looking for 6467c478bd9Sstevel@tonic-gate * the netmask command first. Then use this netmask to set the 6477c478bd9Sstevel@tonic-gate * address. This does not provide an extensible way to accommodate 6487c478bd9Sstevel@tonic-gate * future need for setting more than one attributes together. 6497c478bd9Sstevel@tonic-gate * 650dd7a6f5fSkcpoon * Note that if the "netmask" command argument is a "+", we need 651dd7a6f5fSkcpoon * to save this info and do the query after we know the address to 652dd7a6f5fSkcpoon * be set. The reason is that if "addif" is used, the working 653dd7a6f5fSkcpoon * interface name will be changed later when the logical interface 654dd7a6f5fSkcpoon * is created. In in_getmask(), if an address is not provided, 655dd7a6f5fSkcpoon * it will use the working interface's address to do the query. 656dd7a6f5fSkcpoon * It will be wrong now as we don't know the logical interface's name. 657dd7a6f5fSkcpoon * 6587c478bd9Sstevel@tonic-gate * ifconfig(1M) is too overloaded and the code is so convoluted 6597c478bd9Sstevel@tonic-gate * that it is "safer" not to re-architect the code to fix the above 6607c478bd9Sstevel@tonic-gate * issue, hence this "hack." We may be better off to have a new 6617c478bd9Sstevel@tonic-gate * command with better syntax for configuring network interface 6627c478bd9Sstevel@tonic-gate * parameters... 6637c478bd9Sstevel@tonic-gate */ 6647c478bd9Sstevel@tonic-gate if (!scan_netmask && afp->af_af == AF_INET) { 6657c478bd9Sstevel@tonic-gate int largc; 6667c478bd9Sstevel@tonic-gate char **largv; 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate /* Only go thru the command list once to find the netmask. */ 6697c478bd9Sstevel@tonic-gate scan_netmask = _B_TRUE; 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * Currently, if multiple netmask commands are specified, the 6737c478bd9Sstevel@tonic-gate * last one will be used as the final netmask. So we need 6747c478bd9Sstevel@tonic-gate * to scan the whole list to preserve this behavior. 6757c478bd9Sstevel@tonic-gate */ 6767c478bd9Sstevel@tonic-gate for (largc = argc, largv = argv; largc > 0; largc--, largv++) { 6777c478bd9Sstevel@tonic-gate if (strcmp(*largv, NETMASK_CMD) == 0) { 6787c478bd9Sstevel@tonic-gate if (--largc == 0) 6797c478bd9Sstevel@tonic-gate break; 6807c478bd9Sstevel@tonic-gate largv++; 6817c478bd9Sstevel@tonic-gate if (strcmp(*largv, "+") == 0) { 682dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_PENDING; 6837c478bd9Sstevel@tonic-gate } else { 6847c478bd9Sstevel@tonic-gate in_getaddr(*largv, (struct sockaddr *) 6857c478bd9Sstevel@tonic-gate &g_netmask, NULL); 686dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_SET; 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate /* Continue the scan. */ 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate } 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate while (argc > 0) { 6947c478bd9Sstevel@tonic-gate struct cmd *p; 6957c478bd9Sstevel@tonic-gate boolean_t found_cmd; 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate if (debug) 6987c478bd9Sstevel@tonic-gate (void) printf("ifconfig: argv %s\n", *argv); 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate found_cmd = _B_FALSE; 7017c478bd9Sstevel@tonic-gate for (p = cmds; p->c_func; p++) { 7027c478bd9Sstevel@tonic-gate if (p->c_name) { 7037c478bd9Sstevel@tonic-gate if (strcmp(*argv, p->c_name) == 0) { 7047c478bd9Sstevel@tonic-gate /* 7057c478bd9Sstevel@tonic-gate * indicate that the command was 7067c478bd9Sstevel@tonic-gate * found and check to see if 7077c478bd9Sstevel@tonic-gate * the address family is valid 7087c478bd9Sstevel@tonic-gate */ 7097c478bd9Sstevel@tonic-gate found_cmd = _B_TRUE; 7107c478bd9Sstevel@tonic-gate if (p->c_af == AF_ANY || 7117c478bd9Sstevel@tonic-gate af == p->c_af) 7127c478bd9Sstevel@tonic-gate break; 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate } else { 7157c478bd9Sstevel@tonic-gate if (p->c_af == AF_ANY || 7167c478bd9Sstevel@tonic-gate af == p->c_af) 7177c478bd9Sstevel@tonic-gate break; 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate /* 7217c478bd9Sstevel@tonic-gate * If we found the keyword, but the address family 7227c478bd9Sstevel@tonic-gate * did not match spit out an error 7237c478bd9Sstevel@tonic-gate */ 7247c478bd9Sstevel@tonic-gate if (found_cmd && p->c_name == 0) { 7257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Operation %s not" 7267c478bd9Sstevel@tonic-gate " supported for %s\n", *argv, afp->af_name); 7277c478bd9Sstevel@tonic-gate exit(1); 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate /* 7307c478bd9Sstevel@tonic-gate * else (no keyword found), we assume it's an address 7317c478bd9Sstevel@tonic-gate * of some sort 7327c478bd9Sstevel@tonic-gate */ 7336e91bba0SGirish Moodalbail if (setaddr && ipaddr != NULL) { 7346e91bba0SGirish Moodalbail /* 7356e91bba0SGirish Moodalbail * We must have already filled in a source address in 7366e91bba0SGirish Moodalbail * `ipaddr' and we now got a destination address. 7376e91bba0SGirish Moodalbail * Fill it in `ipaddr' and call libipadm to create 7386e91bba0SGirish Moodalbail * the static address. 7396e91bba0SGirish Moodalbail */ 7406e91bba0SGirish Moodalbail if (p->c_name == 0) { 7416e91bba0SGirish Moodalbail istatus = ipadm_set_dst_addr(ipaddr, *argv, 7426e91bba0SGirish Moodalbail (p->c_af == AF_ANY ? AF_UNSPEC : af)); 7436e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 7446e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not " 7456e91bba0SGirish Moodalbail "set destination address"); 7466e91bba0SGirish Moodalbail } 7476e91bba0SGirish Moodalbail /* 7486e91bba0SGirish Moodalbail * finished processing dstaddr, so reset setaddr 7496e91bba0SGirish Moodalbail */ 7506e91bba0SGirish Moodalbail setaddr = 0; 7516e91bba0SGirish Moodalbail } 7526e91bba0SGirish Moodalbail /* 7536e91bba0SGirish Moodalbail * Both source and destination address are in `ipaddr'. 7546e91bba0SGirish Moodalbail * Add the address by calling libipadm. 7556e91bba0SGirish Moodalbail */ 7566e91bba0SGirish Moodalbail istatus = ipadm_create_addr(iph, ipaddr, 7576e91bba0SGirish Moodalbail IPADM_OPT_ACTIVE); 7586e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 7596e91bba0SGirish Moodalbail goto createfailed; 7606e91bba0SGirish Moodalbail ipadm_destroy_addrobj(ipaddr); 7616e91bba0SGirish Moodalbail ipaddr = NULL; 7626e91bba0SGirish Moodalbail setaddr_done = _B_TRUE; 7636e91bba0SGirish Moodalbail if (p->c_name == 0) { 7646e91bba0SGirish Moodalbail /* move parser along */ 7656e91bba0SGirish Moodalbail argc--, argv++; 7666e91bba0SGirish Moodalbail continue; 7676e91bba0SGirish Moodalbail } 7686e91bba0SGirish Moodalbail } 7696e91bba0SGirish Moodalbail if (p->c_name == 0 && setaddr_done) { 7706e91bba0SGirish Moodalbail /* 7716e91bba0SGirish Moodalbail * catch odd commands like 7726e91bba0SGirish Moodalbail * "ifconfig <intf> addr1 addr2 addr3 addr4 up" 7736e91bba0SGirish Moodalbail */ 7746e91bba0SGirish Moodalbail (void) fprintf(stderr, "%s", 7756e91bba0SGirish Moodalbail "ifconfig: cannot configure more than two " 7766e91bba0SGirish Moodalbail "addresses in one command\n"); 7776e91bba0SGirish Moodalbail exit(1); 7786e91bba0SGirish Moodalbail } 7797c478bd9Sstevel@tonic-gate if (p->c_func) { 7807c478bd9Sstevel@tonic-gate if (p->c_af == AF_INET6) { 7817c478bd9Sstevel@tonic-gate v4compat = 0; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate if (p->c_parameter == NEXTARG || 7847c478bd9Sstevel@tonic-gate p->c_parameter == OPTARG) { 7857c478bd9Sstevel@tonic-gate argc--, argv++; 7867c478bd9Sstevel@tonic-gate if (argc == 0 && p->c_parameter == NEXTARG) { 7877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7887c478bd9Sstevel@tonic-gate "ifconfig: no argument for %s\n", 7897c478bd9Sstevel@tonic-gate p->c_name); 7907c478bd9Sstevel@tonic-gate exit(1); 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate /* 7947c478bd9Sstevel@tonic-gate * Call the function if: 7957c478bd9Sstevel@tonic-gate * 7967c478bd9Sstevel@tonic-gate * there's no address family 7977c478bd9Sstevel@tonic-gate * restriction 7987c478bd9Sstevel@tonic-gate * OR 7997c478bd9Sstevel@tonic-gate * we don't know the address yet 8007c478bd9Sstevel@tonic-gate * (because we were called from 8017c478bd9Sstevel@tonic-gate * main) 8027c478bd9Sstevel@tonic-gate * OR 8037c478bd9Sstevel@tonic-gate * there is a restriction AND 8047c478bd9Sstevel@tonic-gate * the address families match 8057c478bd9Sstevel@tonic-gate */ 8067c478bd9Sstevel@tonic-gate if ((p->c_af == AF_ANY) || 8076e91bba0SGirish Moodalbail (ifa == NULL) || 8086e91bba0SGirish Moodalbail (ifa->ifa_addr->ss_family == p->c_af)) { 8097c478bd9Sstevel@tonic-gate ret = (*p->c_func)(*argv, p->c_parameter); 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * If c_func failed and we should 8127c478bd9Sstevel@tonic-gate * abort processing for this 8137c478bd9Sstevel@tonic-gate * interface on failure, return 8147c478bd9Sstevel@tonic-gate * now rather than going on to 8157c478bd9Sstevel@tonic-gate * process other commands for 8167c478bd9Sstevel@tonic-gate * the same interface. 8177c478bd9Sstevel@tonic-gate */ 8187c478bd9Sstevel@tonic-gate if (ret != 0 && p->c_abortonfail) 8197c478bd9Sstevel@tonic-gate return; 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate argc--, argv++; 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8256e91bba0SGirish Moodalbail if (setaddr && ipaddr != NULL) { 8266e91bba0SGirish Moodalbail /* 8276e91bba0SGirish Moodalbail * Only the source address was provided, which was already 8286e91bba0SGirish Moodalbail * set in `ipaddr'. Add the address by calling libipadm. 8296e91bba0SGirish Moodalbail */ 8306e91bba0SGirish Moodalbail istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE); 8316e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 8326e91bba0SGirish Moodalbail goto createfailed; 8336e91bba0SGirish Moodalbail ipadm_destroy_addrobj(ipaddr); 8346e91bba0SGirish Moodalbail ipaddr = NULL; 8356e91bba0SGirish Moodalbail setaddr_done = _B_TRUE; 8366e91bba0SGirish Moodalbail } 8376e91bba0SGirish Moodalbail 8387c478bd9Sstevel@tonic-gate /* Check to see if there's a security hole in the tunnel setup. */ 8392b24ab6bSSebastien Roy if (ipsec_policy_set && !ipsec_auth_covered) { 8402b24ab6bSSebastien Roy (void) fprintf(stderr, "ifconfig: WARNING: tunnel with only " 8412b24ab6bSSebastien Roy "ESP and no authentication.\n"); 8422b24ab6bSSebastien Roy } 8436e91bba0SGirish Moodalbail return; 8446e91bba0SGirish Moodalbail 8456e91bba0SGirish Moodalbail createfailed: 8466e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: could not create address:% s\n", 8476e91bba0SGirish Moodalbail ipadm_status2str(istatus)); 8486e91bba0SGirish Moodalbail /* Remove the newly created logical interface. */ 8496e91bba0SGirish Moodalbail if (strcmp(name, origname) != 0) { 8506e91bba0SGirish Moodalbail assert(strchr(name, ':') != NULL); 8516e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 8526e91bba0SGirish Moodalbail (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 8536e91bba0SGirish Moodalbail } 8546e91bba0SGirish Moodalbail exit(1); 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8587c478bd9Sstevel@tonic-gate static int 8597c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg) 8607c478bd9Sstevel@tonic-gate { 8617c478bd9Sstevel@tonic-gate debug++; 8627c478bd9Sstevel@tonic-gate return (0); 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8667c478bd9Sstevel@tonic-gate static int 8677c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg) 8687c478bd9Sstevel@tonic-gate { 8697c478bd9Sstevel@tonic-gate verbose++; 8707c478bd9Sstevel@tonic-gate return (0); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate 873dd7a6f5fSkcpoon /* 874dd7a6f5fSkcpoon * This function fills in the given lifreq's lifr_addr field based on 875dd7a6f5fSkcpoon * g_netmask_set. 876dd7a6f5fSkcpoon */ 877dd7a6f5fSkcpoon static void 878dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr, 879dd7a6f5fSkcpoon struct sockaddr_storage *mask) 880dd7a6f5fSkcpoon { 881dd7a6f5fSkcpoon assert(addr != NULL); 882dd7a6f5fSkcpoon assert(mask != NULL); 883dd7a6f5fSkcpoon 884dd7a6f5fSkcpoon switch (g_netmask_set) { 885dd7a6f5fSkcpoon case G_NETMASK_SET: 886dd7a6f5fSkcpoon lifr->lifr_addr = g_netmask; 887dd7a6f5fSkcpoon break; 888dd7a6f5fSkcpoon 889dd7a6f5fSkcpoon case G_NETMASK_PENDING: 890dd7a6f5fSkcpoon /* 891dd7a6f5fSkcpoon * "+" is used as the argument to "netmask" command. Query 892dd7a6f5fSkcpoon * the database on the correct netmask based on the address to 893dd7a6f5fSkcpoon * be set. 894dd7a6f5fSkcpoon */ 895dd7a6f5fSkcpoon assert(afp->af_af == AF_INET); 896dd7a6f5fSkcpoon g_netmask = *addr; 897dd7a6f5fSkcpoon if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) { 898dd7a6f5fSkcpoon lifr->lifr_addr = *mask; 899dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 900dd7a6f5fSkcpoon } else { 901dd7a6f5fSkcpoon lifr->lifr_addr = g_netmask; 902dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_SET; 903dd7a6f5fSkcpoon } 904dd7a6f5fSkcpoon break; 905dd7a6f5fSkcpoon 906dd7a6f5fSkcpoon case G_NETMASK_NIL: 907dd7a6f5fSkcpoon default: 908dd7a6f5fSkcpoon lifr->lifr_addr = *mask; 909dd7a6f5fSkcpoon break; 910dd7a6f5fSkcpoon } 911dd7a6f5fSkcpoon } 912dd7a6f5fSkcpoon 9137c478bd9Sstevel@tonic-gate /* 9147c478bd9Sstevel@tonic-gate * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n> 9157c478bd9Sstevel@tonic-gate * syntax for setting the address, the address plus netmask, and just 9167c478bd9Sstevel@tonic-gate * the netmask respectively. 9177c478bd9Sstevel@tonic-gate */ 9187c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9197c478bd9Sstevel@tonic-gate static int 9207c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param) 9217c478bd9Sstevel@tonic-gate { 9226e91bba0SGirish Moodalbail ipadm_status_t istatus; 9237c478bd9Sstevel@tonic-gate int prefixlen = 0; 9246e91bba0SGirish Moodalbail struct lifreq lifr1; 9257c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 9267c478bd9Sstevel@tonic-gate struct sockaddr_storage netmask; 9277c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9287c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 9297c478bd9Sstevel@tonic-gate struct sockaddr_storage sav_netmask; 9306e91bba0SGirish Moodalbail char cidraddr[BUFSIZ]; 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate if (addr[0] == '/') 9337c478bd9Sstevel@tonic-gate return (setifprefixlen(addr, 0)); 9347c478bd9Sstevel@tonic-gate 9357c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen); 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate (void) memset(&netmask, 0, sizeof (netmask)); 9387c478bd9Sstevel@tonic-gate netmask.ss_family = afp->af_af; 9397c478bd9Sstevel@tonic-gate switch (prefixlen) { 9407c478bd9Sstevel@tonic-gate case NO_PREFIX: 9417c478bd9Sstevel@tonic-gate /* Nothing there - ok */ 9427c478bd9Sstevel@tonic-gate break; 9437c478bd9Sstevel@tonic-gate case BAD_ADDR: 9447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n", 9457c478bd9Sstevel@tonic-gate addr); 9467c478bd9Sstevel@tonic-gate exit(1); 9477c478bd9Sstevel@tonic-gate default: 9487c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 9497c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&netmask; 9507906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 9517c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 9527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 9537c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 9547c478bd9Sstevel@tonic-gate prefixlen); 9557c478bd9Sstevel@tonic-gate exit(1); 9567c478bd9Sstevel@tonic-gate } 9577c478bd9Sstevel@tonic-gate } else { 9587c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&netmask; 9597906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 9607c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 9617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 9627c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 9637c478bd9Sstevel@tonic-gate prefixlen); 9647c478bd9Sstevel@tonic-gate exit(1); 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate * Just in case of funny setting of both prefix and netmask, 9697c478bd9Sstevel@tonic-gate * prefix should override the netmask command. 9707c478bd9Sstevel@tonic-gate */ 971dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 9727c478bd9Sstevel@tonic-gate break; 9737c478bd9Sstevel@tonic-gate } 9746e91bba0SGirish Moodalbail 9756e91bba0SGirish Moodalbail /* 9766e91bba0SGirish Moodalbail * Check and see if any "netmask" command is used and perform the 9776e91bba0SGirish Moodalbail * necessary operation. 9786e91bba0SGirish Moodalbail */ 9796e91bba0SGirish Moodalbail set_mask_lifreq(&lifr, &laddr, &netmask); 9806e91bba0SGirish Moodalbail 9816e91bba0SGirish Moodalbail /* This check is temporary until libipadm supports IPMP interfaces. */ 9826e91bba0SGirish Moodalbail if (ifconfig_use_libipadm(s, name)) { 983e6889d40SVasumathi Sundaram char addrstr[INET6_ADDRSTRLEN]; 984e6889d40SVasumathi Sundaram 985e6889d40SVasumathi Sundaram if (af == AF_INET) { 986e6889d40SVasumathi Sundaram sin = (struct sockaddr_in *)&laddr; 987e6889d40SVasumathi Sundaram (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr, 988e6889d40SVasumathi Sundaram sizeof (addrstr)); 989e6889d40SVasumathi Sundaram } else { 990e6889d40SVasumathi Sundaram sin6 = (struct sockaddr_in6 *)&laddr; 991e6889d40SVasumathi Sundaram (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 992e6889d40SVasumathi Sundaram sizeof (addrstr)); 993e6889d40SVasumathi Sundaram } 9946e91bba0SGirish Moodalbail istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 9956e91bba0SGirish Moodalbail &ipaddr); 9966e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 9976e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "setifaddr"); 9986e91bba0SGirish Moodalbail 9991214df40SGirish Moodalbail /* 10001214df40SGirish Moodalbail * lifr.lifr_addr, which is updated by set_mask_lifreq() 10011214df40SGirish Moodalbail * will contain the right mask to use. 10021214df40SGirish Moodalbail */ 10031214df40SGirish Moodalbail prefixlen = mask2plen(&lifr.lifr_addr); 10041214df40SGirish Moodalbail (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 10051214df40SGirish Moodalbail addrstr, prefixlen); 10061214df40SGirish Moodalbail 10071214df40SGirish Moodalbail istatus = ipadm_set_addr(ipaddr, cidraddr, af); 10086e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 10096e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not set address"); 10106e91bba0SGirish Moodalbail /* 10116e91bba0SGirish Moodalbail * let parser know we got a source. 10126e91bba0SGirish Moodalbail * Next address, if given, should be dest 10136e91bba0SGirish Moodalbail */ 10146e91bba0SGirish Moodalbail setaddr++; 10156e91bba0SGirish Moodalbail 10166e91bba0SGirish Moodalbail /* 10176e91bba0SGirish Moodalbail * address will be set by the parser after nextarg has 10186e91bba0SGirish Moodalbail * been scanned 10196e91bba0SGirish Moodalbail */ 10206e91bba0SGirish Moodalbail return (0); 10216e91bba0SGirish Moodalbail } 10226e91bba0SGirish Moodalbail 10237c478bd9Sstevel@tonic-gate /* Tell parser that an address was set */ 10247c478bd9Sstevel@tonic-gate setaddr++; 10257c478bd9Sstevel@tonic-gate /* save copy of netmask to restore in case of error */ 10266e91bba0SGirish Moodalbail (void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name)); 10276e91bba0SGirish Moodalbail if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0) 10287c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFNETMASK"); 10296e91bba0SGirish Moodalbail sav_netmask = lifr1.lifr_addr; 10307c478bd9Sstevel@tonic-gate 10317c478bd9Sstevel@tonic-gate /* 10327c478bd9Sstevel@tonic-gate * If setting the address and not the mask, clear any existing mask 10337c478bd9Sstevel@tonic-gate * and the kernel will then assign the default (netmask has been set 10347c478bd9Sstevel@tonic-gate * to 0 in this case). If setting both (either by using a prefix or 10357c478bd9Sstevel@tonic-gate * using the netmask command), set the mask first, so the address will 10367c478bd9Sstevel@tonic-gate * be interpreted correctly. 10377c478bd9Sstevel@tonic-gate */ 10386e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 10396e91bba0SGirish Moodalbail /* lifr.lifr_addr already contains netmask from set_mask_lifreq() */ 10407c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 10417c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 10427c478bd9Sstevel@tonic-gate 10437c478bd9Sstevel@tonic-gate if (debug) { 10447c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 10457c478bd9Sstevel@tonic-gate void *addr = (afp->af_af == AF_INET) ? 10467c478bd9Sstevel@tonic-gate (void *)&((struct sockaddr_in *)&laddr)->sin_addr : 10477c478bd9Sstevel@tonic-gate (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr; 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate (void) printf("Setting %s af %d addr %s\n", 10507c478bd9Sstevel@tonic-gate lifr.lifr_name, afp->af_af, 10517c478bd9Sstevel@tonic-gate inet_ntop(afp->af_af, addr, abuf, sizeof (abuf))); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate lifr.lifr_addr = laddr; 10547c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = afp->af_af; 10557c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 10567c478bd9Sstevel@tonic-gate /* 10577c478bd9Sstevel@tonic-gate * Restore the netmask 10587c478bd9Sstevel@tonic-gate */ 10597c478bd9Sstevel@tonic-gate int saverr = errno; 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 10627c478bd9Sstevel@tonic-gate lifr.lifr_addr = sav_netmask; 10637c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr); 10647c478bd9Sstevel@tonic-gate errno = saverr; 10657c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFADDR"); 10667c478bd9Sstevel@tonic-gate } 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate return (0); 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate /* 10727c478bd9Sstevel@tonic-gate * The following functions are stolen from the ipseckey(1m) program. 10737c478bd9Sstevel@tonic-gate * Perhaps they should be somewhere common, but for now, we just maintain 10747c478bd9Sstevel@tonic-gate * two versions. We do this because of the different semantics for which 10757c478bd9Sstevel@tonic-gate * algorithms we select ("requested" for ifconfig vs. "actual" for key). 10767c478bd9Sstevel@tonic-gate */ 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate static ulong_t 10797c478bd9Sstevel@tonic-gate parsenum(char *num) 10807c478bd9Sstevel@tonic-gate { 10817c478bd9Sstevel@tonic-gate ulong_t rc; 10827c478bd9Sstevel@tonic-gate char *end = NULL; 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate errno = 0; 10857c478bd9Sstevel@tonic-gate rc = strtoul(num, &end, 0); 10867c478bd9Sstevel@tonic-gate if (errno != 0 || end == num || *end != '\0') { 10877c478bd9Sstevel@tonic-gate rc = (ulong_t)-1; 10887c478bd9Sstevel@tonic-gate } 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate return (rc); 10917c478bd9Sstevel@tonic-gate } 10927c478bd9Sstevel@tonic-gate 10937c478bd9Sstevel@tonic-gate /* 10947c478bd9Sstevel@tonic-gate * Parse and reverse parse possible algorithm values, include numbers. 10957c478bd9Sstevel@tonic-gate * Mostly stolen from ipseckey.c. See the comments above parsenum() for why 10967c478bd9Sstevel@tonic-gate * this isn't common to ipseckey.c. 10977c478bd9Sstevel@tonic-gate * 10987c478bd9Sstevel@tonic-gate * NOTE: Static buffer in this function for the return value. Since ifconfig 10992b24ab6bSSebastien Roy * isn't multithreaded, this isn't a huge problem. 11007c478bd9Sstevel@tonic-gate */ 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate #define NBUF_SIZE 20 /* Enough to print a large integer. */ 11037c478bd9Sstevel@tonic-gate 11047c478bd9Sstevel@tonic-gate static char * 11057c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num) 11067c478bd9Sstevel@tonic-gate { 11077c478bd9Sstevel@tonic-gate struct ipsecalgent *alg; 11087c478bd9Sstevel@tonic-gate static char numprint[128]; /* Enough to hold an algorithm name. */ 11097c478bd9Sstevel@tonic-gate 1110d2f8a3dfSpwernau /* 1111d2f8a3dfSpwernau * Special cases for "any" and "none" 1112d2f8a3dfSpwernau * The kernel needs to be able to distinguish between "any" 1113d2f8a3dfSpwernau * and "none" and the APIs are underdefined in this area for auth. 1114d2f8a3dfSpwernau */ 1115d2f8a3dfSpwernau if (proto_num == IPSEC_PROTO_AH) { 1116d2f8a3dfSpwernau if (alg_value == SADB_AALG_NONE) 1117d2f8a3dfSpwernau return ("none"); 1118d2f8a3dfSpwernau if (alg_value == SADB_AALG_ANY) 1119d2f8a3dfSpwernau return ("any"); 1120d2f8a3dfSpwernau } 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate alg = getipsecalgbynum(alg_value, proto_num, NULL); 11237c478bd9Sstevel@tonic-gate if (alg != NULL) { 11247c478bd9Sstevel@tonic-gate (void) strlcpy(numprint, alg->a_names[0], sizeof (numprint)); 11257c478bd9Sstevel@tonic-gate freeipsecalgent(alg); 11267c478bd9Sstevel@tonic-gate } else { 11277c478bd9Sstevel@tonic-gate (void) snprintf(numprint, sizeof (numprint), "%d", alg_value); 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate return (numprint); 11317c478bd9Sstevel@tonic-gate } 11327c478bd9Sstevel@tonic-gate 11337c478bd9Sstevel@tonic-gate static uint_t 11347c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num) 11357c478bd9Sstevel@tonic-gate { 11367c478bd9Sstevel@tonic-gate struct ipsecalgent *alg; 11377c478bd9Sstevel@tonic-gate ulong_t invalue; 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate if (algname == NULL) { 11407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Unexpected end of command " 11417c478bd9Sstevel@tonic-gate "line.\n"); 11427c478bd9Sstevel@tonic-gate exit(1); 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate /* 1146d2f8a3dfSpwernau * Special-case "none" and "any". 1147d2f8a3dfSpwernau * Use strcasecmp because its length is bounded. 11487c478bd9Sstevel@tonic-gate */ 11497c478bd9Sstevel@tonic-gate if (strcasecmp("none", algname) == 0) { 11507c478bd9Sstevel@tonic-gate return ((proto_num == IPSEC_PROTO_ESP) ? 11517c478bd9Sstevel@tonic-gate NO_ESP_EALG : NO_ESP_AALG); 11527c478bd9Sstevel@tonic-gate } 1153d2f8a3dfSpwernau if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH)) 1154d2f8a3dfSpwernau return (SADB_AALG_ANY); 11557c478bd9Sstevel@tonic-gate 11567c478bd9Sstevel@tonic-gate alg = getipsecalgbyname(algname, proto_num, NULL); 11577c478bd9Sstevel@tonic-gate if (alg != NULL) { 11587c478bd9Sstevel@tonic-gate invalue = alg->a_alg_num; 11597c478bd9Sstevel@tonic-gate freeipsecalgent(alg); 11607c478bd9Sstevel@tonic-gate return ((uint_t)invalue); 11617c478bd9Sstevel@tonic-gate } 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate /* 11647c478bd9Sstevel@tonic-gate * Since algorithms can be loaded during kernel run-time, check for 11657c478bd9Sstevel@tonic-gate * numeric algorithm values too. 11667c478bd9Sstevel@tonic-gate */ 11677c478bd9Sstevel@tonic-gate invalue = parsenum(algname); 11687c478bd9Sstevel@tonic-gate if ((invalue & (ulong_t)0xff) == invalue) 11697c478bd9Sstevel@tonic-gate return ((uint_t)invalue); 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n", 11727c478bd9Sstevel@tonic-gate (proto_num == IPSEC_PROTO_ESP) ? 11737c478bd9Sstevel@tonic-gate "Encryption" : "Authentication", algname); 11747c478bd9Sstevel@tonic-gate exit(1); 11757c478bd9Sstevel@tonic-gate /* NOTREACHED */ 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate 11787c478bd9Sstevel@tonic-gate /* 11797c478bd9Sstevel@tonic-gate * Actual ifconfig functions to set tunnel security properties. 11807c478bd9Sstevel@tonic-gate */ 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG }; 11837c478bd9Sstevel@tonic-gate 11847c478bd9Sstevel@tonic-gate static int 11857c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg) 11867c478bd9Sstevel@tonic-gate { 11872b24ab6bSSebastien Roy boolean_t encr_alg_set = _B_FALSE; 11882b24ab6bSSebastien Roy iptun_params_t params; 11892b24ab6bSSebastien Roy dladm_status_t status; 11902b24ab6bSSebastien Roy ipsec_req_t *ipsr; 11917c478bd9Sstevel@tonic-gate 11922b24ab6bSSebastien Roy if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 11932b24ab6bSSebastien Roy ¶ms.iptun_param_linkid)) != DLADM_STATUS_OK) 11942b24ab6bSSebastien Roy goto done; 11957c478bd9Sstevel@tonic-gate 11962b24ab6bSSebastien Roy status = dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE); 11972b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 11982b24ab6bSSebastien Roy goto done; 11997c478bd9Sstevel@tonic-gate 12002b24ab6bSSebastien Roy ipsr = ¶ms.iptun_param_secinfo; 12017c478bd9Sstevel@tonic-gate 12027c478bd9Sstevel@tonic-gate /* 12037c478bd9Sstevel@tonic-gate * If I'm just starting off this ifconfig, I want a clean slate, 12047c478bd9Sstevel@tonic-gate * otherwise, I've captured the current tunnel security settings. 12057c478bd9Sstevel@tonic-gate * In the case of continuation, I merely add to the settings. 12067c478bd9Sstevel@tonic-gate */ 12072b24ab6bSSebastien Roy if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO)) 12087c478bd9Sstevel@tonic-gate (void) memset(ipsr, 0, sizeof (*ipsr)); 12097c478bd9Sstevel@tonic-gate 12102b24ab6bSSebastien Roy /* We're only modifying the IPsec information */ 12112b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_SECINFO; 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate switch (which_alg) { 12147c478bd9Sstevel@tonic-gate case ESP_ENCR_ALG: 12157c478bd9Sstevel@tonic-gate if (alg == NO_ESP_EALG) { 12167c478bd9Sstevel@tonic-gate if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE) 12177c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 0; 12187c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = SADB_EALG_NONE; 1219d2f8a3dfSpwernau 1220d2f8a3dfSpwernau /* Let the user specify NULL encryption implicitly. */ 1221d2f8a3dfSpwernau if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) { 1222d2f8a3dfSpwernau encr_alg_set = _B_TRUE; 1223d2f8a3dfSpwernau ipsr->ipsr_esp_alg = SADB_EALG_NULL; 1224d2f8a3dfSpwernau } 12257c478bd9Sstevel@tonic-gate } else { 12267c478bd9Sstevel@tonic-gate encr_alg_set = _B_TRUE; 12277c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 12287c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12297c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = alg; 12307c478bd9Sstevel@tonic-gate } 12317c478bd9Sstevel@tonic-gate break; 12327c478bd9Sstevel@tonic-gate case ESP_AUTH_ALG: 12337c478bd9Sstevel@tonic-gate if (alg == NO_ESP_AALG) { 1234d2f8a3dfSpwernau if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE || 1235d2f8a3dfSpwernau ipsr->ipsr_esp_alg == SADB_EALG_NULL) && 1236d2f8a3dfSpwernau !encr_alg_set) 12377c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 0; 12387c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE; 12397c478bd9Sstevel@tonic-gate } else { 12407c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 12417c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12427c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_auth_alg = alg; 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate /* Let the user specify NULL encryption implicitly. */ 12457c478bd9Sstevel@tonic-gate if (ipsr->ipsr_esp_alg == SADB_EALG_NONE && 12467c478bd9Sstevel@tonic-gate !encr_alg_set) 12477c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = SADB_EALG_NULL; 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate break; 12507c478bd9Sstevel@tonic-gate case AH_AUTH_ALG: 12517c478bd9Sstevel@tonic-gate if (alg == NO_AH_AALG) { 12527c478bd9Sstevel@tonic-gate ipsr->ipsr_ah_req = 0; 12537c478bd9Sstevel@tonic-gate ipsr->ipsr_auth_alg = SADB_AALG_NONE; 12547c478bd9Sstevel@tonic-gate } else { 12557c478bd9Sstevel@tonic-gate ipsr->ipsr_ah_req = 12567c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12577c478bd9Sstevel@tonic-gate ipsr->ipsr_auth_alg = alg; 12587c478bd9Sstevel@tonic-gate } 12597c478bd9Sstevel@tonic-gate break; 12607c478bd9Sstevel@tonic-gate /* Will never hit DEFAULT */ 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate 12632b24ab6bSSebastien Roy status = dladm_iptun_modify(dlh, ¶ms, DLADM_OPT_ACTIVE); 12647c478bd9Sstevel@tonic-gate 12652b24ab6bSSebastien Roy done: 12662b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 12672b24ab6bSSebastien Roy dladmerr_exit(status, name); 12682b24ab6bSSebastien Roy else { 12692b24ab6bSSebastien Roy ipsec_policy_set = _B_TRUE; 12702b24ab6bSSebastien Roy if ((ipsr->ipsr_esp_req != 0 && 12712b24ab6bSSebastien Roy ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) || 12722b24ab6bSSebastien Roy (ipsr->ipsr_ah_req != 0 && 12732b24ab6bSSebastien Roy ipsr->ipsr_auth_alg != SADB_AALG_NONE)) 12742b24ab6bSSebastien Roy ipsec_auth_covered = _B_TRUE; 12752b24ab6bSSebastien Roy } 12767c478bd9Sstevel@tonic-gate return (0); 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate /* ARGSUSED */ 12807c478bd9Sstevel@tonic-gate static int 12817c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param) 12827c478bd9Sstevel@tonic-gate { 12837c478bd9Sstevel@tonic-gate return (set_tun_algs(ESP_ENCR_ALG, 1284fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_ESP))); 12857c478bd9Sstevel@tonic-gate } 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate /* ARGSUSED */ 12887c478bd9Sstevel@tonic-gate static int 12897c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param) 12907c478bd9Sstevel@tonic-gate { 12917c478bd9Sstevel@tonic-gate return (set_tun_algs(ESP_AUTH_ALG, 1292fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_AH))); 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate 12957c478bd9Sstevel@tonic-gate /* ARGSUSED */ 12967c478bd9Sstevel@tonic-gate static int 12977c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param) 12987c478bd9Sstevel@tonic-gate { 12997c478bd9Sstevel@tonic-gate return (set_tun_algs(AH_AUTH_ALG, 1300fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_AH))); 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13047c478bd9Sstevel@tonic-gate static int 13057c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param) 13067c478bd9Sstevel@tonic-gate { 13077c478bd9Sstevel@tonic-gate struct sockaddr_in laddr; 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 13107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13117c478bd9Sstevel@tonic-gate "ifconfig: revarp not possible on IPv6 interface %s\n", 13127c478bd9Sstevel@tonic-gate name); 13137c478bd9Sstevel@tonic-gate exit(1); 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate if (doifrevarp(name, &laddr)) { 13167c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13177c478bd9Sstevel@tonic-gate laddr.sin_family = AF_INET; 13187c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr)); 13197c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 13207c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFADDR"); 13217c478bd9Sstevel@tonic-gate } 13227c478bd9Sstevel@tonic-gate return (0); 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13267c478bd9Sstevel@tonic-gate static int 13277c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param) 13287c478bd9Sstevel@tonic-gate { 13297c478bd9Sstevel@tonic-gate int prefixlen = 0; 13307c478bd9Sstevel@tonic-gate struct sockaddr_storage subnet; 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, &subnet, &prefixlen); 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate switch (prefixlen) { 13357c478bd9Sstevel@tonic-gate case NO_PREFIX: 13367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13377c478bd9Sstevel@tonic-gate "ifconfig: Missing prefix length in subnet %s\n", addr); 13387c478bd9Sstevel@tonic-gate exit(1); 13397c478bd9Sstevel@tonic-gate /* NOTREACHED */ 13407c478bd9Sstevel@tonic-gate case BAD_ADDR: 13417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13427c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 13437c478bd9Sstevel@tonic-gate exit(1); 13447c478bd9Sstevel@tonic-gate default: 13457c478bd9Sstevel@tonic-gate break; 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate 13487c478bd9Sstevel@tonic-gate lifr.lifr_addr = subnet; 13497c478bd9Sstevel@tonic-gate lifr.lifr_addrlen = prefixlen; 13507c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13517c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0) 13527c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFSUBNET"); 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate return (0); 13557c478bd9Sstevel@tonic-gate } 13567c478bd9Sstevel@tonic-gate 13577c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13587c478bd9Sstevel@tonic-gate static int 13597c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param) 13607c478bd9Sstevel@tonic-gate { 13617c478bd9Sstevel@tonic-gate struct sockaddr_in netmask; 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate assert(afp->af_af != AF_INET6); 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate if (strcmp(addr, "+") == 0) { 1366dd7a6f5fSkcpoon if (!in_getmask(&netmask, _B_FALSE)) 13677c478bd9Sstevel@tonic-gate return (0); 1368dd7a6f5fSkcpoon (void) printf("Setting netmask of %s to %s\n", name, 1369dd7a6f5fSkcpoon inet_ntoa(netmask.sin_addr)); 13707c478bd9Sstevel@tonic-gate } else { 13717c478bd9Sstevel@tonic-gate in_getaddr(addr, (struct sockaddr *)&netmask, NULL); 13727c478bd9Sstevel@tonic-gate } 13737c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13747c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask)); 13757c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 13767c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 13777c478bd9Sstevel@tonic-gate return (0); 13787c478bd9Sstevel@tonic-gate } 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate /* 13817c478bd9Sstevel@tonic-gate * Parse '/<n>' as a netmask. 13827c478bd9Sstevel@tonic-gate */ 13837c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13847c478bd9Sstevel@tonic-gate static int 13857c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param) 13867c478bd9Sstevel@tonic-gate { 13877c478bd9Sstevel@tonic-gate int prefixlen; 13887c478bd9Sstevel@tonic-gate int af = afp->af_af; 13897c478bd9Sstevel@tonic-gate 13907c478bd9Sstevel@tonic-gate prefixlen = in_getprefixlen(addr, _B_TRUE, 13917906a3e0Smeem (af == AF_INET) ? IP_ABITS : IPV6_ABITS); 13927c478bd9Sstevel@tonic-gate if (prefixlen < 0) { 13937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13947c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 13957c478bd9Sstevel@tonic-gate exit(1); 13967c478bd9Sstevel@tonic-gate } 13977c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 13987c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 13997c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 14007c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 14017c478bd9Sstevel@tonic-gate 14027c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 14037906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 14047c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 14057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 14067c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 14077c478bd9Sstevel@tonic-gate prefixlen); 14087c478bd9Sstevel@tonic-gate exit(1); 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate } else if (af == AF_INET) { 14117c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 14127c478bd9Sstevel@tonic-gate 14137c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 14147906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 14157c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 14167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 14177c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 14187c478bd9Sstevel@tonic-gate prefixlen); 14197c478bd9Sstevel@tonic-gate exit(1); 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate } else { 14227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: setting prefix only supported" 14237c478bd9Sstevel@tonic-gate " for address family inet or inet6\n"); 14247c478bd9Sstevel@tonic-gate exit(1); 14257c478bd9Sstevel@tonic-gate } 14267c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14277c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 14287c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 14297c478bd9Sstevel@tonic-gate return (0); 14307c478bd9Sstevel@tonic-gate } 14317c478bd9Sstevel@tonic-gate 14327c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14337c478bd9Sstevel@tonic-gate static int 14347c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param) 14357c478bd9Sstevel@tonic-gate { 14367c478bd9Sstevel@tonic-gate struct sockaddr_in broadaddr; 14377c478bd9Sstevel@tonic-gate 14387c478bd9Sstevel@tonic-gate assert(afp->af_af != AF_INET6); 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate if (strcmp(addr, "+") == 0) { 14417c478bd9Sstevel@tonic-gate /* 14427c478bd9Sstevel@tonic-gate * This doesn't set the broadcast address at all. Rather, it 14437c478bd9Sstevel@tonic-gate * gets, then sets the interface's address, relying on the fact 14447c478bd9Sstevel@tonic-gate * that resetting the address will reset the broadcast address. 14457c478bd9Sstevel@tonic-gate */ 14467c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, 14477c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 14487c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 14497c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 14507c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFADDR"); 14517c478bd9Sstevel@tonic-gate return (0); 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 14547c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFADDR"); 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate return (0); 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL); 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr)); 14617c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14627c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0) 14637c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFBRDADDR"); 14647c478bd9Sstevel@tonic-gate return (0); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate /* 14687c478bd9Sstevel@tonic-gate * set interface destination address 14697c478bd9Sstevel@tonic-gate */ 14707c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14717c478bd9Sstevel@tonic-gate static int 14727c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param) 14737c478bd9Sstevel@tonic-gate { 14747c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL); 14757c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14767c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0) 14777c478bd9Sstevel@tonic-gate Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR"); 14787c478bd9Sstevel@tonic-gate return (0); 14797c478bd9Sstevel@tonic-gate } 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14827c478bd9Sstevel@tonic-gate static int 14837c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value) 14847c478bd9Sstevel@tonic-gate { 1485e11c3f44Smeem struct lifreq lifrl; /* local lifreq struct */ 1486e11c3f44Smeem boolean_t bringup = _B_FALSE; 14877c478bd9Sstevel@tonic-gate 14887c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14897c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) 14907c478bd9Sstevel@tonic-gate Perror0_exit("setifflags: SIOCGLIFFLAGS"); 14917c478bd9Sstevel@tonic-gate 14927c478bd9Sstevel@tonic-gate if (value < 0) { 14937c478bd9Sstevel@tonic-gate value = -value; 1494e11c3f44Smeem 1495e11c3f44Smeem if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) { 1496e11c3f44Smeem /* 1497e11c3f44Smeem * The kernel does not allow administratively up test 1498e11c3f44Smeem * addresses to be converted to data addresses. Bring 1499e11c3f44Smeem * the address down first, then bring it up after it's 1500e11c3f44Smeem * been converted to a data address. 1501e11c3f44Smeem */ 1502e11c3f44Smeem lifr.lifr_flags &= ~IFF_UP; 1503e11c3f44Smeem (void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr); 1504e11c3f44Smeem bringup = _B_TRUE; 1505e11c3f44Smeem } 1506e11c3f44Smeem 15077c478bd9Sstevel@tonic-gate lifr.lifr_flags &= ~value; 1508e11c3f44Smeem if ((value & (IFF_UP | IFF_NOFAILOVER)) && 1509e11c3f44Smeem (lifr.lifr_flags & IFF_DUPLICATE)) { 151069bb4bb4Scarlsonj /* 151169bb4bb4Scarlsonj * If the user is trying to mark an interface with a 1512e11c3f44Smeem * duplicate address as "down," or convert a duplicate 1513e11c3f44Smeem * test address to a data address, then fetch the 1514e11c3f44Smeem * address and set it. This will cause IP to clear 1515e11c3f44Smeem * the IFF_DUPLICATE flag and stop the automatic 1516e11c3f44Smeem * recovery timer. 151769bb4bb4Scarlsonj */ 151869bb4bb4Scarlsonj value = lifr.lifr_flags; 151969bb4bb4Scarlsonj if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) 152069bb4bb4Scarlsonj (void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr); 152169bb4bb4Scarlsonj lifr.lifr_flags = value; 152269bb4bb4Scarlsonj } 152369bb4bb4Scarlsonj } else { 15247c478bd9Sstevel@tonic-gate lifr.lifr_flags |= value; 152569bb4bb4Scarlsonj } 1526e11c3f44Smeem 1527e11c3f44Smeem /* 1528e11c3f44Smeem * If we're about to bring up an underlying physical IPv6 interface in 1529e11c3f44Smeem * an IPMP group, ensure the IPv6 IPMP interface is also up. This is 1530e11c3f44Smeem * for backward compatibility with legacy configurations in which 1531e11c3f44Smeem * there are no explicit hostname files for IPMP interfaces. (For 1532e11c3f44Smeem * IPv4, this is automatically handled by the kernel when migrating 1533e11c3f44Smeem * the underlying interface's data address to the IPMP interface.) 1534e11c3f44Smeem */ 1535e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 1536e11c3f44Smeem 1537e11c3f44Smeem if (lifnum(lifr.lifr_name) == 0 && 1538e11c3f44Smeem (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) && 1539e11c3f44Smeem ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 && 1540e11c3f44Smeem lifrl.lifr_groupname[0] != '\0') { 1541e11c3f44Smeem lifgroupinfo_t lifgr; 1542e11c3f44Smeem 1543e11c3f44Smeem (void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname, 1544e11c3f44Smeem LIFGRNAMSIZ); 1545e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) 1546e11c3f44Smeem Perror0_exit("setifflags: SIOCGLIFGROUPINFO"); 1547e11c3f44Smeem 1548e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ); 1549e11c3f44Smeem if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1) 1550e11c3f44Smeem Perror0_exit("setifflags: SIOCGLIFFLAGS"); 1551e11c3f44Smeem if (!(lifrl.lifr_flags & IFF_UP)) { 1552e11c3f44Smeem lifrl.lifr_flags |= IFF_UP; 1553e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1) 1554e11c3f44Smeem Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1555e11c3f44Smeem } 1556e11c3f44Smeem } 1557e11c3f44Smeem 15587c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1559e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 15607c478bd9Sstevel@tonic-gate Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1561e11c3f44Smeem 1562e11c3f44Smeem if (bringup) { 1563e11c3f44Smeem lifr.lifr_flags |= IFF_UP; 1564e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 1565e11c3f44Smeem Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP"); 15667c478bd9Sstevel@tonic-gate } 1567e11c3f44Smeem 15687c478bd9Sstevel@tonic-gate return (0); 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate 15717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15727c478bd9Sstevel@tonic-gate static int 15737c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param) 15747c478bd9Sstevel@tonic-gate { 15757c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 15767c478bd9Sstevel@tonic-gate lifr.lifr_metric = atoi(val); 15777c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0) 15787c478bd9Sstevel@tonic-gate Perror0_exit("setifmetric: SIOCSLIFMETRIC"); 15797c478bd9Sstevel@tonic-gate return (0); 15807c478bd9Sstevel@tonic-gate } 15817c478bd9Sstevel@tonic-gate 15827c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15837c478bd9Sstevel@tonic-gate static int 15847c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param) 15857c478bd9Sstevel@tonic-gate { 15867c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 15877c478bd9Sstevel@tonic-gate lifr.lifr_mtu = atoi(val); 15887c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0) 15897c478bd9Sstevel@tonic-gate Perror0_exit("setifmtu: SIOCSLIFMTU"); 15907c478bd9Sstevel@tonic-gate return (0); 15917c478bd9Sstevel@tonic-gate } 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15947c478bd9Sstevel@tonic-gate static int 15957c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param) 15967c478bd9Sstevel@tonic-gate { 15977c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 15987c478bd9Sstevel@tonic-gate lifr.lifr_index = atoi(val); 15997c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0) 16007c478bd9Sstevel@tonic-gate Perror0_exit("setifindex: SIOCSLIFINDEX"); 16017c478bd9Sstevel@tonic-gate return (0); 16027c478bd9Sstevel@tonic-gate } 16037c478bd9Sstevel@tonic-gate 1604e11c3f44Smeem /* ARGSUSED */ 1605e11c3f44Smeem static void 1606e11c3f44Smeem notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg) 1607e11c3f44Smeem { 1608e11c3f44Smeem } 1609e11c3f44Smeem 16107c478bd9Sstevel@tonic-gate /* ARGSUSED */ 16117c478bd9Sstevel@tonic-gate static int 16127c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param) 16137c478bd9Sstevel@tonic-gate { 1614e11c3f44Smeem uchar_t *hwaddr; 1615e11c3f44Smeem int hwaddrlen; 1616e11c3f44Smeem int retval; 1617e11c3f44Smeem ifaddrlistx_t *ifaddrp, *ifaddrs = NULL; 1618e11c3f44Smeem dlpi_handle_t dh; 1619e11c3f44Smeem dlpi_notifyid_t id; 16207c478bd9Sstevel@tonic-gate 16217c478bd9Sstevel@tonic-gate if (addr == NULL) { 16227c478bd9Sstevel@tonic-gate ifstatus(name); 16237c478bd9Sstevel@tonic-gate print_ifether(name); 16247c478bd9Sstevel@tonic-gate return (0); 16257c478bd9Sstevel@tonic-gate } 16267c478bd9Sstevel@tonic-gate 16277c478bd9Sstevel@tonic-gate /* 16287c478bd9Sstevel@tonic-gate * if the IP interface in the arguments is a logical 16297c478bd9Sstevel@tonic-gate * interface, exit with an error now. 16307c478bd9Sstevel@tonic-gate */ 16317c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 16327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: cannot change" 16337c478bd9Sstevel@tonic-gate " ethernet address of a logical interface\n"); 16347c478bd9Sstevel@tonic-gate exit(1); 16357c478bd9Sstevel@tonic-gate } 16367c478bd9Sstevel@tonic-gate 1637e11c3f44Smeem if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) { 1638e11c3f44Smeem if (hwaddrlen == -1) 16397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 16406e91bba0SGirish Moodalbail "ifconfig: bad ethernet address\n"); 16417c478bd9Sstevel@tonic-gate else 16427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 16437c478bd9Sstevel@tonic-gate exit(1); 16447c478bd9Sstevel@tonic-gate } 16457c478bd9Sstevel@tonic-gate 1646e11c3f44Smeem if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS) 1647e11c3f44Smeem Perrdlpi_exit("cannot dlpi_open() link", name, retval); 16487c478bd9Sstevel@tonic-gate 1649e11c3f44Smeem retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id); 1650e11c3f44Smeem if (retval == DLPI_SUCCESS) { 1651e11c3f44Smeem (void) dlpi_disabnotify(dh, id, NULL); 1652e11c3f44Smeem } else { 1653e11c3f44Smeem /* 1654e11c3f44Smeem * This link does not support DL_NOTE_PHYS_ADDR: bring down 1655e11c3f44Smeem * all of the addresses to flush the old hardware address 1656e11c3f44Smeem * information out of IP. 1657e11c3f44Smeem * 1658e11c3f44Smeem * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is 1659e11c3f44Smeem * more than an optimization: in.mpathd will set IFF_OFFLINE 1660e11c3f44Smeem * if it's notified and the new address is a duplicate of 1661e11c3f44Smeem * another in the group -- but the flags manipulation in 1662e11c3f44Smeem * ifaddr_{down,up}() cannot be atomic and thus might clobber 1663e11c3f44Smeem * IFF_OFFLINE, confusing in.mpathd. 1664e11c3f44Smeem */ 1665e11c3f44Smeem if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1) 1666e11c3f44Smeem Perror2_exit(name, "cannot get address list"); 1667e11c3f44Smeem 1668e11c3f44Smeem ifaddrp = ifaddrs; 1669e11c3f44Smeem for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1670e11c3f44Smeem if (!ifaddr_down(ifaddrp)) { 1671e11c3f44Smeem Perror2_exit(ifaddrp->ia_name, 1672e11c3f44Smeem "cannot bring down"); 1673e11c3f44Smeem } 1674e11c3f44Smeem } 16757c478bd9Sstevel@tonic-gate } 16767c478bd9Sstevel@tonic-gate 16777c478bd9Sstevel@tonic-gate /* 1678e11c3f44Smeem * Change the hardware address. 16797c478bd9Sstevel@tonic-gate */ 1680e11c3f44Smeem retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen); 1681e11c3f44Smeem if (retval != DLPI_SUCCESS) { 16827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1683e11c3f44Smeem "ifconfig: failed setting mac address on %s\n", name); 16847c478bd9Sstevel@tonic-gate } 1685e11c3f44Smeem dlpi_close(dh); 16867c478bd9Sstevel@tonic-gate 16877c478bd9Sstevel@tonic-gate /* 1688e11c3f44Smeem * If any addresses were brought down before changing the hardware 1689e11c3f44Smeem * address, bring them up again. 16907c478bd9Sstevel@tonic-gate */ 1691e11c3f44Smeem for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1692e11c3f44Smeem if (!ifaddr_up(ifaddrp)) 1693e11c3f44Smeem Perror2_exit(ifaddrp->ia_name, "cannot bring up"); 16947c478bd9Sstevel@tonic-gate } 1695e11c3f44Smeem ifaddrlistx_free(ifaddrs); 16967c478bd9Sstevel@tonic-gate 16977c478bd9Sstevel@tonic-gate return (0); 16987c478bd9Sstevel@tonic-gate } 16997c478bd9Sstevel@tonic-gate 17007c478bd9Sstevel@tonic-gate /* 17017c478bd9Sstevel@tonic-gate * Print an interface's Ethernet address, if it has one. 17027c478bd9Sstevel@tonic-gate */ 17037c478bd9Sstevel@tonic-gate static void 17046e91bba0SGirish Moodalbail print_ifether(const char *ifname) 17057c478bd9Sstevel@tonic-gate { 17062b24ab6bSSebastien Roy int fd; 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 17097c478bd9Sstevel@tonic-gate 17107c478bd9Sstevel@tonic-gate fd = socket(AF_INET, SOCK_DGRAM, 0); 17117c478bd9Sstevel@tonic-gate if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 17127c478bd9Sstevel@tonic-gate /* 17137c478bd9Sstevel@tonic-gate * It's possible the interface is only configured for 17147c478bd9Sstevel@tonic-gate * IPv6; check again with AF_INET6. 17157c478bd9Sstevel@tonic-gate */ 17167c478bd9Sstevel@tonic-gate (void) close(fd); 17177c478bd9Sstevel@tonic-gate fd = socket(AF_INET6, SOCK_DGRAM, 0); 17187c478bd9Sstevel@tonic-gate if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 17197c478bd9Sstevel@tonic-gate (void) close(fd); 17207c478bd9Sstevel@tonic-gate return; 17217c478bd9Sstevel@tonic-gate } 17227c478bd9Sstevel@tonic-gate } 17237c478bd9Sstevel@tonic-gate (void) close(fd); 17247c478bd9Sstevel@tonic-gate 1725e11c3f44Smeem /* VNI and IPMP interfaces don't have MAC addresses */ 1726e11c3f44Smeem if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) 17277c478bd9Sstevel@tonic-gate return; 17287c478bd9Sstevel@tonic-gate 17292b24ab6bSSebastien Roy /* IP tunnels also don't have Ethernet-like MAC addresses */ 17302b24ab6bSSebastien Roy if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) == 17312b24ab6bSSebastien Roy DLADM_STATUS_OK) 17322b24ab6bSSebastien Roy return; 17337c478bd9Sstevel@tonic-gate 17347c478bd9Sstevel@tonic-gate dlpi_print_address(ifname); 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate 1737f4b3ec61Sdh /* 17382b24ab6bSSebastien Roy * static int find_all_interfaces(struct lifconf *lifcp, char **buf, 1739f4b3ec61Sdh * int64_t lifc_flags) 1740f4b3ec61Sdh * 17412b24ab6bSSebastien Roy * It finds all active data links. 1742f4b3ec61Sdh * 1743f4b3ec61Sdh * It takes in input a pointer to struct lifconf to receive interfaces 1744f4b3ec61Sdh * informations, a **char to hold allocated buffer, and a lifc_flags. 1745f4b3ec61Sdh * 1746f4b3ec61Sdh * Return values: 1747f4b3ec61Sdh * 0 = everything OK 1748f4b3ec61Sdh * -1 = problem 1749f4b3ec61Sdh */ 1750f4b3ec61Sdh static int 17512b24ab6bSSebastien Roy find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags) 1752f4b3ec61Sdh { 1753f4b3ec61Sdh unsigned bufsize; 1754f4b3ec61Sdh int n; 1755f4b3ec61Sdh ni_t *nip; 1756f4b3ec61Sdh struct lifreq *lifrp; 1757d4d1f7bfSVasumathi Sundaram - Sun Microsystems dladm_status_t status; 1758f4b3ec61Sdh 17592b24ab6bSSebastien Roy if (!dlh_opened) { 17602b24ab6bSSebastien Roy status = ifconfig_dladm_open(NULL, 0, NULL); 17612b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 17622b24ab6bSSebastien Roy dladmerr_exit(status, "unable to open dladm handle"); 1763d4d1f7bfSVasumathi Sundaram - Sun Microsystems } 1764d4d1f7bfSVasumathi Sundaram - Sun Microsystems 17652b24ab6bSSebastien Roy (void) dlpi_walk(ni_entry, dlh, 0); 1766f4b3ec61Sdh 17672b24ab6bSSebastien Roy /* Now, translate the linked list into a struct lifreq buffer */ 1768f4b3ec61Sdh if (num_ni == 0) { 1769f4b3ec61Sdh lifcp->lifc_family = AF_UNSPEC; 1770f4b3ec61Sdh lifcp->lifc_flags = lifc_flags; 1771f4b3ec61Sdh lifcp->lifc_len = 0; 1772f4b3ec61Sdh lifcp->lifc_buf = NULL; 1773f4b3ec61Sdh return (0); 1774f4b3ec61Sdh } 1775f4b3ec61Sdh 1776f4b3ec61Sdh bufsize = num_ni * sizeof (struct lifreq); 1777f4b3ec61Sdh if ((*buf = malloc(bufsize)) == NULL) 1778f4b3ec61Sdh Perror0_exit("find_all_interfaces: malloc failed"); 1779f4b3ec61Sdh 1780f4b3ec61Sdh lifcp->lifc_family = AF_UNSPEC; 1781f4b3ec61Sdh lifcp->lifc_flags = lifc_flags; 1782f4b3ec61Sdh lifcp->lifc_len = bufsize; 1783f4b3ec61Sdh lifcp->lifc_buf = *buf; 1784f4b3ec61Sdh 1785f4b3ec61Sdh for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) { 1786f4b3ec61Sdh nip = ni_list; 1787f4b3ec61Sdh (void) strncpy(lifrp->lifr_name, nip->ni_name, 1788f4b3ec61Sdh sizeof (lifr.lifr_name)); 1789f4b3ec61Sdh ni_list = nip->ni_next; 1790f4b3ec61Sdh free(nip); 1791f4b3ec61Sdh } 1792f4b3ec61Sdh return (0); 1793f4b3ec61Sdh } 1794f4b3ec61Sdh 17957c478bd9Sstevel@tonic-gate /* 17967c478bd9Sstevel@tonic-gate * Create the next unused logical interface using the original name 17977c478bd9Sstevel@tonic-gate * and assign the address (and mask if '/<n>' is part of the address). 17987c478bd9Sstevel@tonic-gate * Use the new logical interface for subsequent subcommands by updating 17997c478bd9Sstevel@tonic-gate * the name variable. 18007c478bd9Sstevel@tonic-gate * 18017c478bd9Sstevel@tonic-gate * This allows syntax like: 18027c478bd9Sstevel@tonic-gate * ifconfig le0 addif 109.106.86.130 netmask + up \ 18037c478bd9Sstevel@tonic-gate * addif 109.106.86.131 netmask + up 18047c478bd9Sstevel@tonic-gate */ 18057c478bd9Sstevel@tonic-gate /* ARGSUSED */ 18067c478bd9Sstevel@tonic-gate static int 18077c478bd9Sstevel@tonic-gate addif(char *str, int64_t param) 18087c478bd9Sstevel@tonic-gate { 18097c478bd9Sstevel@tonic-gate int prefixlen = 0; 18107c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 18117c478bd9Sstevel@tonic-gate struct sockaddr_storage mask; 18123166459dSVasumathi Sundaram struct sockaddr_in6 *sin6; 18133166459dSVasumathi Sundaram struct sockaddr_in *sin; 18146e91bba0SGirish Moodalbail ipadm_status_t istatus; 18156e91bba0SGirish Moodalbail char cidraddr[BUFSIZ]; 18163166459dSVasumathi Sundaram char addrstr[INET6_ADDRSTRLEN]; 18177c478bd9Sstevel@tonic-gate 18187c478bd9Sstevel@tonic-gate (void) strncpy(name, origname, sizeof (name)); 18197c478bd9Sstevel@tonic-gate 18207c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 18217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18227c478bd9Sstevel@tonic-gate "ifconfig: addif: bad physical interface name %s\n", 18237c478bd9Sstevel@tonic-gate name); 18247c478bd9Sstevel@tonic-gate exit(1); 18257c478bd9Sstevel@tonic-gate } 18267c478bd9Sstevel@tonic-gate 18277c478bd9Sstevel@tonic-gate /* 18287c478bd9Sstevel@tonic-gate * clear so parser will interpret next address as source followed 18297c478bd9Sstevel@tonic-gate * by possible dest 18307c478bd9Sstevel@tonic-gate */ 18317c478bd9Sstevel@tonic-gate setaddr = 0; 18327c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen); 18337c478bd9Sstevel@tonic-gate 18347c478bd9Sstevel@tonic-gate switch (prefixlen) { 18357c478bd9Sstevel@tonic-gate case NO_PREFIX: 18367c478bd9Sstevel@tonic-gate /* Nothing there - ok */ 18377c478bd9Sstevel@tonic-gate break; 18387c478bd9Sstevel@tonic-gate case BAD_ADDR: 18397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18407c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", str); 18417c478bd9Sstevel@tonic-gate exit(1); 18427c478bd9Sstevel@tonic-gate default: 18437c478bd9Sstevel@tonic-gate (void) memset(&mask, 0, sizeof (mask)); 18447c478bd9Sstevel@tonic-gate mask.ss_family = afp->af_af; 18457c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 18467c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&mask; 18477906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 18487c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 18497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 18507c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 18517c478bd9Sstevel@tonic-gate prefixlen); 18527c478bd9Sstevel@tonic-gate exit(1); 18537c478bd9Sstevel@tonic-gate } 18547c478bd9Sstevel@tonic-gate } else { 18557c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&mask; 18567906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 18577c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 18587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 18597c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 18607c478bd9Sstevel@tonic-gate prefixlen); 18617c478bd9Sstevel@tonic-gate exit(1); 18627c478bd9Sstevel@tonic-gate } 18637c478bd9Sstevel@tonic-gate } 1864dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 18657c478bd9Sstevel@tonic-gate break; 18667c478bd9Sstevel@tonic-gate } 18677c478bd9Sstevel@tonic-gate 1868dd7a6f5fSkcpoon /* 1869dd7a6f5fSkcpoon * This is a "hack" to get around the problem of SIOCLIFADDIF. The 1870dd7a6f5fSkcpoon * problem is that this ioctl does not include the netmask when 1871dd7a6f5fSkcpoon * adding a logical interface. This is the same problem described 1872dd7a6f5fSkcpoon * in the ifconfig() comments. To get around this problem, we first 1873dd7a6f5fSkcpoon * add the logical interface with a 0 address. After that, we set 1874dd7a6f5fSkcpoon * the netmask if provided. Finally we set the interface address. 1875dd7a6f5fSkcpoon */ 18767c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1877dd7a6f5fSkcpoon (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 18787c478bd9Sstevel@tonic-gate 18797c478bd9Sstevel@tonic-gate /* Note: no need to do DAD here since the interface isn't up yet. */ 18807c478bd9Sstevel@tonic-gate 18817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) 18827c478bd9Sstevel@tonic-gate Perror0_exit("addif: SIOCLIFADDIF"); 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate (void) printf("Created new logical interface %s\n", 18857c478bd9Sstevel@tonic-gate lifr.lifr_name); 18867c478bd9Sstevel@tonic-gate (void) strncpy(name, lifr.lifr_name, sizeof (name)); 18877c478bd9Sstevel@tonic-gate 1888dd7a6f5fSkcpoon /* 1889dd7a6f5fSkcpoon * Check and see if any "netmask" command is used and perform the 1890dd7a6f5fSkcpoon * necessary operation. 1891dd7a6f5fSkcpoon */ 1892dd7a6f5fSkcpoon set_mask_lifreq(&lifr, &laddr, &mask); 18936e91bba0SGirish Moodalbail 18946e91bba0SGirish Moodalbail /* This check is temporary until libipadm supports IPMP interfaces. */ 18956e91bba0SGirish Moodalbail if (ifconfig_use_libipadm(s, name)) { 18966e91bba0SGirish Moodalbail /* 18976e91bba0SGirish Moodalbail * We added the logical interface above before calling 18986e91bba0SGirish Moodalbail * ipadm_create_addr(), because, with IPH_LEGACY, we need 18996e91bba0SGirish Moodalbail * to do an addif for `ifconfig ce0 addif <addr>' but not for 19006e91bba0SGirish Moodalbail * `ifconfig ce0 <addr>'. libipadm does not have a flag to 19016e91bba0SGirish Moodalbail * to differentiate between these two cases. To keep it simple, 19026e91bba0SGirish Moodalbail * we always create the logical interface and pass it to 19036e91bba0SGirish Moodalbail * libipadm instead of requiring libipadm to addif for some 19046e91bba0SGirish Moodalbail * cases and not do addif for other cases. 19056e91bba0SGirish Moodalbail */ 19066e91bba0SGirish Moodalbail istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 19076e91bba0SGirish Moodalbail &ipaddr); 19086e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 19096e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "addif"); 19106e91bba0SGirish Moodalbail 19113166459dSVasumathi Sundaram if (af == AF_INET) { 19123166459dSVasumathi Sundaram sin = (struct sockaddr_in *)&laddr; 19133166459dSVasumathi Sundaram (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr, 19143166459dSVasumathi Sundaram sizeof (addrstr)); 19153166459dSVasumathi Sundaram } else { 19163166459dSVasumathi Sundaram sin6 = (struct sockaddr_in6 *)&laddr; 19173166459dSVasumathi Sundaram (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 19183166459dSVasumathi Sundaram sizeof (addrstr)); 19196e91bba0SGirish Moodalbail } 19203166459dSVasumathi Sundaram /* 19213166459dSVasumathi Sundaram * lifr.lifr_addr, which is updated by set_mask_lifreq() 19223166459dSVasumathi Sundaram * will contain the right mask to use. 19233166459dSVasumathi Sundaram */ 19243166459dSVasumathi Sundaram prefixlen = mask2plen(&lifr.lifr_addr); 19253166459dSVasumathi Sundaram (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 19263166459dSVasumathi Sundaram addrstr, prefixlen); 19273166459dSVasumathi Sundaram 19283166459dSVasumathi Sundaram istatus = ipadm_set_addr(ipaddr, cidraddr, af); 19293166459dSVasumathi Sundaram 19306e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 19316e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not set address"); 19326e91bba0SGirish Moodalbail setaddr++; 19336e91bba0SGirish Moodalbail /* 19346e91bba0SGirish Moodalbail * address will be set by the parser after nextarg 19356e91bba0SGirish Moodalbail * has been scanned 19366e91bba0SGirish Moodalbail */ 19376e91bba0SGirish Moodalbail return (0); 19386e91bba0SGirish Moodalbail } 19396e91bba0SGirish Moodalbail 1940dd7a6f5fSkcpoon /* 1941dd7a6f5fSkcpoon * Only set the netmask if "netmask" command is used or a prefix is 1942dd7a6f5fSkcpoon * provided. 1943dd7a6f5fSkcpoon */ 1944dd7a6f5fSkcpoon if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) { 19456e91bba0SGirish Moodalbail /* 19466e91bba0SGirish Moodalbail * lifr.lifr_addr already contains netmask from 19476e91bba0SGirish Moodalbail * set_mask_lifreq(). 19486e91bba0SGirish Moodalbail */ 19497c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 19507c478bd9Sstevel@tonic-gate Perror0_exit("addif: SIOCSLIFNETMASK"); 19517c478bd9Sstevel@tonic-gate } 1952dd7a6f5fSkcpoon 1953dd7a6f5fSkcpoon /* Finally, we set the interface address. */ 1954dd7a6f5fSkcpoon lifr.lifr_addr = laddr; 1955dd7a6f5fSkcpoon if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 1956dd7a6f5fSkcpoon Perror0_exit("SIOCSLIFADDR"); 1957dd7a6f5fSkcpoon 19587c478bd9Sstevel@tonic-gate /* 19597c478bd9Sstevel@tonic-gate * let parser know we got a source. 19607c478bd9Sstevel@tonic-gate * Next address, if given, should be dest 19617c478bd9Sstevel@tonic-gate */ 19627c478bd9Sstevel@tonic-gate setaddr++; 19637c478bd9Sstevel@tonic-gate return (0); 19647c478bd9Sstevel@tonic-gate } 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate /* 19677c478bd9Sstevel@tonic-gate * Remove a logical interface based on its IP address. Unlike addif 19687c478bd9Sstevel@tonic-gate * there is no '/<n>' here. 19697c478bd9Sstevel@tonic-gate * Verifies that the interface is down before it is removed. 19707c478bd9Sstevel@tonic-gate */ 19717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 19727c478bd9Sstevel@tonic-gate static int 19737c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param) 19747c478bd9Sstevel@tonic-gate { 19757c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 19766e91bba0SGirish Moodalbail ipadm_status_t istatus; 19776e91bba0SGirish Moodalbail ipadm_addr_info_t *ainfo, *ainfop; 19787c478bd9Sstevel@tonic-gate 19797c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 19807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19817c478bd9Sstevel@tonic-gate "ifconfig: removeif: bad physical interface name %s\n", 19827c478bd9Sstevel@tonic-gate name); 19837c478bd9Sstevel@tonic-gate exit(1); 19847c478bd9Sstevel@tonic-gate } 19857c478bd9Sstevel@tonic-gate 19867c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(str, &laddr, NULL); 19877c478bd9Sstevel@tonic-gate 19886e91bba0SGirish Moodalbail /* 19896e91bba0SGirish Moodalbail * Following check is temporary until libipadm supports 19906e91bba0SGirish Moodalbail * IPMP interfaces. 19916e91bba0SGirish Moodalbail */ 19926e91bba0SGirish Moodalbail if (!ifconfig_use_libipadm(s, name)) 19936e91bba0SGirish Moodalbail goto delete; 19946e91bba0SGirish Moodalbail 19956e91bba0SGirish Moodalbail /* 19966e91bba0SGirish Moodalbail * Get all addresses and search this address among the active 19976e91bba0SGirish Moodalbail * addresses. If an address object was found, delete using 19986e91bba0SGirish Moodalbail * ipadm_delete_addr(). 19996e91bba0SGirish Moodalbail */ 20006e91bba0SGirish Moodalbail istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT); 20016e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 20026e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "removeif"); 20036e91bba0SGirish Moodalbail 20046e91bba0SGirish Moodalbail for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) 20056e91bba0SGirish Moodalbail if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr)) 20066e91bba0SGirish Moodalbail break; 20076e91bba0SGirish Moodalbail 200867ce1608SVasumathi Sundaram if (ainfop != NULL) { 200967ce1608SVasumathi Sundaram if (strchr(ainfop->ia_ifa.ifa_name, ':') == NULL) { 201067ce1608SVasumathi Sundaram (void) fprintf(stderr, 201167ce1608SVasumathi Sundaram "ifconfig: removeif: cannot remove interface: %s\n", 201267ce1608SVasumathi Sundaram name); 201367ce1608SVasumathi Sundaram exit(1); 201467ce1608SVasumathi Sundaram } 201567ce1608SVasumathi Sundaram if (ainfop->ia_aobjname[0] != '\0') { 201667ce1608SVasumathi Sundaram istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname, 201767ce1608SVasumathi Sundaram IPADM_OPT_ACTIVE); 201867ce1608SVasumathi Sundaram if (istatus != IPADM_SUCCESS) { 201967ce1608SVasumathi Sundaram ipadmerr_exit(istatus, 202067ce1608SVasumathi Sundaram "could not delete address"); 202167ce1608SVasumathi Sundaram } 202267ce1608SVasumathi Sundaram ipadm_free_addr_info(ainfo); 202367ce1608SVasumathi Sundaram return (0); 202467ce1608SVasumathi Sundaram } 20256e91bba0SGirish Moodalbail } 20266e91bba0SGirish Moodalbail ipadm_free_addr_info(ainfo); 20276e91bba0SGirish Moodalbail 20286e91bba0SGirish Moodalbail delete: 20296e91bba0SGirish Moodalbail /* 20306e91bba0SGirish Moodalbail * An address object for this address was not found in ipadm. 20316e91bba0SGirish Moodalbail * Delete with SIOCLIFREMOVEIF. 20326e91bba0SGirish Moodalbail */ 20336e91bba0SGirish Moodalbail lifr.lifr_addr = laddr; 20347c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 20357c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) { 20367c478bd9Sstevel@tonic-gate if (errno == EBUSY) { 20377c478bd9Sstevel@tonic-gate /* This can only happen if ipif_id = 0 */ 20387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 203967ce1608SVasumathi Sundaram "ifconfig: removeif: cannot remove interface: %s\n", 20407c478bd9Sstevel@tonic-gate name); 20417c478bd9Sstevel@tonic-gate exit(1); 20427c478bd9Sstevel@tonic-gate } 20437c478bd9Sstevel@tonic-gate Perror0_exit("removeif: SIOCLIFREMOVEIF"); 20447c478bd9Sstevel@tonic-gate } 20457c478bd9Sstevel@tonic-gate return (0); 20467c478bd9Sstevel@tonic-gate } 20477c478bd9Sstevel@tonic-gate 20487c478bd9Sstevel@tonic-gate /* 20497c478bd9Sstevel@tonic-gate * Set the address token for IPv6. 20507c478bd9Sstevel@tonic-gate */ 20517c478bd9Sstevel@tonic-gate /* ARGSUSED */ 20527c478bd9Sstevel@tonic-gate static int 20537c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param) 20547c478bd9Sstevel@tonic-gate { 20557c478bd9Sstevel@tonic-gate int prefixlen = 0; 20567c478bd9Sstevel@tonic-gate struct sockaddr_in6 token; 20577c478bd9Sstevel@tonic-gate 20587c478bd9Sstevel@tonic-gate in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen); 20597c478bd9Sstevel@tonic-gate switch (prefixlen) { 20607c478bd9Sstevel@tonic-gate case NO_PREFIX: 20617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20627c478bd9Sstevel@tonic-gate "ifconfig: Missing prefix length in subnet %s\n", addr); 20637c478bd9Sstevel@tonic-gate exit(1); 20647c478bd9Sstevel@tonic-gate /* NOTREACHED */ 20657c478bd9Sstevel@tonic-gate case BAD_ADDR: 20667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20677c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 20687c478bd9Sstevel@tonic-gate exit(1); 20697c478bd9Sstevel@tonic-gate default: 20707c478bd9Sstevel@tonic-gate break; 20717c478bd9Sstevel@tonic-gate } 20727c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &token, sizeof (token)); 20737c478bd9Sstevel@tonic-gate lifr.lifr_addrlen = prefixlen; 20747c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 20757c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0) { 20767c478bd9Sstevel@tonic-gate Perror0_exit("setiftoken: SIOCSLIFTOKEN"); 20777c478bd9Sstevel@tonic-gate } 20787c478bd9Sstevel@tonic-gate return (0); 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate /* ARGSUSED */ 20827c478bd9Sstevel@tonic-gate static int 2083e11c3f44Smeem setifgroupname(char *grname, int64_t param) 20847c478bd9Sstevel@tonic-gate { 2085e11c3f44Smeem lifgroupinfo_t lifgr; 2086e11c3f44Smeem struct lifreq lifrl; 2087e11c3f44Smeem ifaddrlistx_t *ifaddrp, *nextifaddrp; 2088e11c3f44Smeem ifaddrlistx_t *ifaddrs = NULL, *downaddrs = NULL; 2089e11c3f44Smeem int af; 2090e11c3f44Smeem 20917c478bd9Sstevel@tonic-gate if (debug) { 20927c478bd9Sstevel@tonic-gate (void) printf("Setting groupname %s on interface %s\n", 2093e11c3f44Smeem grname, name); 20947c478bd9Sstevel@tonic-gate } 20957c478bd9Sstevel@tonic-gate 2096e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 2097e11c3f44Smeem (void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ); 2098e11c3f44Smeem 2099e11c3f44Smeem while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2100e11c3f44Smeem switch (errno) { 2101e11c3f44Smeem case ENOENT: 2102e11c3f44Smeem /* 2103e11c3f44Smeem * The group doesn't yet exist; create it and repeat. 2104e11c3f44Smeem */ 2105e11c3f44Smeem af = afp->af_af; 2106e11c3f44Smeem if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) { 2107e11c3f44Smeem if (errno == EEXIST) 2108e11c3f44Smeem continue; 2109e11c3f44Smeem 2110e11c3f44Smeem Perror2(grname, "cannot create IPMP group"); 2111e11c3f44Smeem goto fail; 2112e11c3f44Smeem } 2113e11c3f44Smeem continue; 2114e11c3f44Smeem 2115e11c3f44Smeem case EALREADY: 2116e11c3f44Smeem /* 2117e11c3f44Smeem * The interface is already in another group; must 2118e11c3f44Smeem * remove existing membership first. 2119e11c3f44Smeem */ 2120e11c3f44Smeem lifrl.lifr_groupname[0] = '\0'; 2121e11c3f44Smeem if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2122e11c3f44Smeem Perror2(name, "cannot remove existing " 2123e11c3f44Smeem "IPMP group membership"); 2124e11c3f44Smeem goto fail; 2125e11c3f44Smeem } 2126e11c3f44Smeem (void) strlcpy(lifrl.lifr_groupname, grname, 2127e11c3f44Smeem LIFGRNAMSIZ); 2128e11c3f44Smeem continue; 2129e11c3f44Smeem 2130e11c3f44Smeem case EAFNOSUPPORT: 2131e11c3f44Smeem /* 2132e11c3f44Smeem * The group exists, but it's not configured with the 2133e11c3f44Smeem * address families the interface needs. Since only 2134e11c3f44Smeem * two address families are currently supported, just 2135e11c3f44Smeem * configure the "other" address family. Note that we 2136e11c3f44Smeem * may race with group deletion or creation by another 2137e11c3f44Smeem * process (ENOENT or EEXIST); in such cases we repeat 2138e11c3f44Smeem * our original SIOCSLIFGROUPNAME. 2139e11c3f44Smeem */ 2140e11c3f44Smeem (void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ); 2141e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) { 2142e11c3f44Smeem if (errno == ENOENT) 2143e11c3f44Smeem continue; 2144e11c3f44Smeem 2145e11c3f44Smeem Perror2(grname, "SIOCGLIFGROUPINFO"); 2146e11c3f44Smeem goto fail; 2147e11c3f44Smeem } 2148e11c3f44Smeem 2149e11c3f44Smeem af = lifgr.gi_v4 ? AF_INET6 : AF_INET; 2150e11c3f44Smeem if (create_ipmp(grname, af, lifgr.gi_grifname, 2151e11c3f44Smeem _B_TRUE) == -1) { 2152e11c3f44Smeem if (errno == EEXIST) 2153e11c3f44Smeem continue; 2154e11c3f44Smeem 2155e11c3f44Smeem Perror2(grname, "cannot configure IPMP group"); 2156e11c3f44Smeem goto fail; 2157e11c3f44Smeem } 2158e11c3f44Smeem continue; 2159e11c3f44Smeem 2160e11c3f44Smeem case EADDRINUSE: 2161e11c3f44Smeem /* 2162e11c3f44Smeem * Some addresses are in-use (or under control of DAD). 2163e11c3f44Smeem * Bring them down and retry the group join operation. 2164e11c3f44Smeem * We will bring them back up after the interface has 2165e11c3f44Smeem * been placed in the group. 2166e11c3f44Smeem */ 2167e11c3f44Smeem if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE, 2168e11c3f44Smeem 0, &ifaddrs) == -1) { 2169e11c3f44Smeem Perror2(grname, "cannot get address list"); 2170e11c3f44Smeem goto fail; 2171e11c3f44Smeem } 2172e11c3f44Smeem 2173e11c3f44Smeem ifaddrp = ifaddrs; 2174e11c3f44Smeem for (; ifaddrp != NULL; ifaddrp = nextifaddrp) { 2175e11c3f44Smeem if (!ifaddr_down(ifaddrp)) { 2176e11c3f44Smeem ifaddrs = ifaddrp; 2177e11c3f44Smeem goto fail; 2178e11c3f44Smeem } 2179e11c3f44Smeem nextifaddrp = ifaddrp->ia_next; 2180e11c3f44Smeem ifaddrp->ia_next = downaddrs; 2181e11c3f44Smeem downaddrs = ifaddrp; 2182e11c3f44Smeem } 2183e11c3f44Smeem ifaddrs = NULL; 2184e11c3f44Smeem continue; 2185e11c3f44Smeem 2186e11c3f44Smeem case EADDRNOTAVAIL: { 2187e11c3f44Smeem /* 2188e11c3f44Smeem * Some data addresses are under application control. 2189e11c3f44Smeem * For some of these (e.g., ADDRCONF), the application 2190e11c3f44Smeem * should remove the address, in which case we retry a 2191e11c3f44Smeem * few times (since the application's action is not 2192e11c3f44Smeem * atomic with respect to us) before bailing out and 2193e11c3f44Smeem * informing the user. 2194e11c3f44Smeem */ 2195e11c3f44Smeem int ntries, nappaddr = 0; 2196e11c3f44Smeem const if_appflags_t *iap = if_appflags_tbl; 2197e11c3f44Smeem 2198e11c3f44Smeem for (; iap->ia_app != NULL; iap++) { 2199e11c3f44Smeem ntries = 0; 2200e11c3f44Smeem again: 2201e11c3f44Smeem if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag, 2202e11c3f44Smeem IFF_NOFAILOVER, &ifaddrs) == -1) { 2203e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: " 2204e11c3f44Smeem "cannot get data addresses managed " 2205e11c3f44Smeem "by %s\n", lifrl.lifr_name, 2206e11c3f44Smeem iap->ia_app); 2207e11c3f44Smeem goto fail; 2208e11c3f44Smeem } 2209e11c3f44Smeem 2210e11c3f44Smeem if (ifaddrs == NULL) 2211e11c3f44Smeem continue; 2212e11c3f44Smeem 2213e11c3f44Smeem ifaddrlistx_free(ifaddrs); 2214e11c3f44Smeem ifaddrs = NULL; 2215e11c3f44Smeem 2216e11c3f44Smeem if (++ntries < iap->ia_tries) { 2217e11c3f44Smeem (void) poll(NULL, 0, 100); 2218e11c3f44Smeem goto again; 2219e11c3f44Smeem } 2220e11c3f44Smeem 2221e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot join " 2222e11c3f44Smeem "IPMP group: %s has data addresses managed " 2223e11c3f44Smeem "by %s\n", lifrl.lifr_name, iap->ia_app); 2224e11c3f44Smeem nappaddr++; 2225e11c3f44Smeem } 2226e11c3f44Smeem if (nappaddr > 0) 2227e11c3f44Smeem goto fail; 2228e11c3f44Smeem continue; 2229e11c3f44Smeem } 2230e11c3f44Smeem default: 2231e11c3f44Smeem Perror2(name, "SIOCSLIFGROUPNAME"); 2232e11c3f44Smeem goto fail; 2233e11c3f44Smeem } 22347c478bd9Sstevel@tonic-gate } 22357c478bd9Sstevel@tonic-gate 223636b41818SGirish Moodalbail /* 223736b41818SGirish Moodalbail * If the interface being moved is under the control of `ipmgmtd(1M)' 223836b41818SGirish Moodalbail * dameon then we should inform the daemon about this move, so that 223936b41818SGirish Moodalbail * the daemon can delete the state associated with this interface. 224036b41818SGirish Moodalbail * 224136b41818SGirish Moodalbail * This workaround is needed until the IPMP support in ipadm(1M). 224236b41818SGirish Moodalbail */ 224336b41818SGirish Moodalbail ipadm_if_move(iph, name); 224436b41818SGirish Moodalbail 22457c478bd9Sstevel@tonic-gate /* 2246e11c3f44Smeem * If there were addresses that we had to bring down, it's time to 2247e11c3f44Smeem * bring them up again. As part of bringing them up, the kernel will 2248e11c3f44Smeem * automatically move them to the new IPMP interface. 22497c478bd9Sstevel@tonic-gate */ 2250e11c3f44Smeem for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2251e11c3f44Smeem if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2252e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot bring back up " 2253e11c3f44Smeem "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2254e11c3f44Smeem } 22557c478bd9Sstevel@tonic-gate } 2256e11c3f44Smeem ifaddrlistx_free(downaddrs); 2257e11c3f44Smeem return (0); 2258e11c3f44Smeem fail: 22597c478bd9Sstevel@tonic-gate /* 2260e11c3f44Smeem * Attempt to bring back up any interfaces that we downed. 22617c478bd9Sstevel@tonic-gate */ 2262e11c3f44Smeem for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2263e11c3f44Smeem if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2264e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot bring back up " 2265e11c3f44Smeem "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2266e11c3f44Smeem } 22677c478bd9Sstevel@tonic-gate } 2268e11c3f44Smeem ifaddrlistx_free(downaddrs); 2269e11c3f44Smeem ifaddrlistx_free(ifaddrs); 22707c478bd9Sstevel@tonic-gate 22717c478bd9Sstevel@tonic-gate /* 2272e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2273e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 22747c478bd9Sstevel@tonic-gate */ 2275e11c3f44Smeem exit(1); 2276e11c3f44Smeem /* NOTREACHED */ 2277e11c3f44Smeem } 22787c478bd9Sstevel@tonic-gate 2279e11c3f44Smeem static boolean_t 2280e11c3f44Smeem modcheck(const char *ifname) 2281e11c3f44Smeem { 2282e11c3f44Smeem (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2283e11c3f44Smeem 2284e11c3f44Smeem if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) { 2285e11c3f44Smeem Perror0("SIOCGLIFFLAGS"); 2286e11c3f44Smeem return (_B_FALSE); 22877c478bd9Sstevel@tonic-gate } 22887c478bd9Sstevel@tonic-gate 2289e11c3f44Smeem if (lifr.lifr_flags & IFF_IPMP) { 2290e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: module operations not" 2291e11c3f44Smeem " supported on IPMP interfaces\n", ifname); 2292e11c3f44Smeem return (_B_FALSE); 2293e11c3f44Smeem } 2294e11c3f44Smeem if (lifr.lifr_flags & IFF_VIRTUAL) { 2295e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: module operations not" 2296e11c3f44Smeem " supported on virtual IP interfaces\n", ifname); 2297e11c3f44Smeem return (_B_FALSE); 2298e11c3f44Smeem } 2299e11c3f44Smeem return (_B_TRUE); 2300e11c3f44Smeem } 23017c478bd9Sstevel@tonic-gate 23027c478bd9Sstevel@tonic-gate /* 23037c478bd9Sstevel@tonic-gate * To list all the modules above a given network interface. 23047c478bd9Sstevel@tonic-gate */ 23057c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23067c478bd9Sstevel@tonic-gate static int 23077c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param) 23087c478bd9Sstevel@tonic-gate { 2309fc80c0dfSnordmark int muxid_fd; 23107c478bd9Sstevel@tonic-gate int muxfd; 23117c478bd9Sstevel@tonic-gate int ipfd_lowstr; 23127c478bd9Sstevel@tonic-gate int arpfd_lowstr; 23137c478bd9Sstevel@tonic-gate int num_mods; 23147c478bd9Sstevel@tonic-gate int i; 23157c478bd9Sstevel@tonic-gate struct str_list strlist; 23167c478bd9Sstevel@tonic-gate int orig_arpid; 23177c478bd9Sstevel@tonic-gate 2318e11c3f44Smeem /* 2319e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2320e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 2321e11c3f44Smeem */ 2322e11c3f44Smeem if (!modcheck(name)) 2323e11c3f44Smeem exit(1); 2324e11c3f44Smeem 2325fc80c0dfSnordmark if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 23267c478bd9Sstevel@tonic-gate &orig_arpid) < 0) { 23277c478bd9Sstevel@tonic-gate return (-1); 23287c478bd9Sstevel@tonic-gate } 23297c478bd9Sstevel@tonic-gate if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) { 23307c478bd9Sstevel@tonic-gate Perror0("cannot I_LIST to get the number of modules"); 23317c478bd9Sstevel@tonic-gate } else { 23327c478bd9Sstevel@tonic-gate if (debug > 0) { 23337c478bd9Sstevel@tonic-gate (void) printf("Listing (%d) modules above %s\n", 23347c478bd9Sstevel@tonic-gate num_mods, name); 23357c478bd9Sstevel@tonic-gate } 23367c478bd9Sstevel@tonic-gate 23377c478bd9Sstevel@tonic-gate strlist.sl_nmods = num_mods; 23387c478bd9Sstevel@tonic-gate strlist.sl_modlist = malloc(sizeof (struct str_mlist) * 23397c478bd9Sstevel@tonic-gate num_mods); 23407c478bd9Sstevel@tonic-gate if (strlist.sl_modlist == NULL) { 23417c478bd9Sstevel@tonic-gate Perror0("cannot malloc"); 23427c478bd9Sstevel@tonic-gate } else { 23437c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) { 23447c478bd9Sstevel@tonic-gate Perror0("cannot I_LIST for module names"); 23457c478bd9Sstevel@tonic-gate } else { 23467c478bd9Sstevel@tonic-gate for (i = 0; i < strlist.sl_nmods; i++) { 2347fc80c0dfSnordmark (void) printf("%d %s\n", i, 2348fc80c0dfSnordmark strlist.sl_modlist[i].l_name); 23497c478bd9Sstevel@tonic-gate } 23507c478bd9Sstevel@tonic-gate } 23517c478bd9Sstevel@tonic-gate free(strlist.sl_modlist); 23527c478bd9Sstevel@tonic-gate } 23537c478bd9Sstevel@tonic-gate } 2354fc80c0dfSnordmark return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2355fc80c0dfSnordmark orig_arpid)); 23567c478bd9Sstevel@tonic-gate } 23577c478bd9Sstevel@tonic-gate 23587c478bd9Sstevel@tonic-gate #define MODINSERT_OP 'i' 23597c478bd9Sstevel@tonic-gate #define MODREMOVE_OP 'r' 23607c478bd9Sstevel@tonic-gate 23617c478bd9Sstevel@tonic-gate /* 23627c478bd9Sstevel@tonic-gate * To insert a module to the stream of the interface. It is just a 23637c478bd9Sstevel@tonic-gate * wrapper. The real function is modop(). 23647c478bd9Sstevel@tonic-gate */ 23657c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23667c478bd9Sstevel@tonic-gate static int 23677c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param) 23687c478bd9Sstevel@tonic-gate { 23697c478bd9Sstevel@tonic-gate return (modop(arg, MODINSERT_OP)); 23707c478bd9Sstevel@tonic-gate } 23717c478bd9Sstevel@tonic-gate 23727c478bd9Sstevel@tonic-gate /* 23737c478bd9Sstevel@tonic-gate * To remove a module from the stream of the interface. It is just a 23747c478bd9Sstevel@tonic-gate * wrapper. The real function is modop(). 23757c478bd9Sstevel@tonic-gate */ 23767c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23777c478bd9Sstevel@tonic-gate static int 23787c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param) 23797c478bd9Sstevel@tonic-gate { 23807c478bd9Sstevel@tonic-gate return (modop(arg, MODREMOVE_OP)); 23817c478bd9Sstevel@tonic-gate } 23827c478bd9Sstevel@tonic-gate 23837c478bd9Sstevel@tonic-gate /* 23847c478bd9Sstevel@tonic-gate * Helper function for mod*() functions. It gets a fd to the lower IP 23857c478bd9Sstevel@tonic-gate * stream and I_PUNLINK's the lower stream. It also initializes the 23867c478bd9Sstevel@tonic-gate * global variable lifr. 23877c478bd9Sstevel@tonic-gate * 23887c478bd9Sstevel@tonic-gate * Param: 2389fc80c0dfSnordmark * int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2390fc80c0dfSnordmark * int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2391fc80c0dfSnordmark * int *ipfd_lowstr: fd to the lower IP stream. 2392fc80c0dfSnordmark * int *arpfd_lowstr: fd to the lower ARP stream. 23937c478bd9Sstevel@tonic-gate * 23947c478bd9Sstevel@tonic-gate * Return: 23957c478bd9Sstevel@tonic-gate * -1 if operation fails, 0 otherwise. 23967c478bd9Sstevel@tonic-gate * 2397e11c3f44Smeem * Please see the big block comment above ifplumb() for the logic of the 2398e11c3f44Smeem * PLINK/PUNLINK 23997c478bd9Sstevel@tonic-gate */ 24007c478bd9Sstevel@tonic-gate static int 2401fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr, 2402fc80c0dfSnordmark int *orig_arpid) 24037c478bd9Sstevel@tonic-gate { 24047c478bd9Sstevel@tonic-gate uint64_t flags; 24057c478bd9Sstevel@tonic-gate char *udp_dev_name; 24067c478bd9Sstevel@tonic-gate 24077c478bd9Sstevel@tonic-gate *orig_arpid = 0; 24087c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 24097c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 24107c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 24117c478bd9Sstevel@tonic-gate } 24127c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 24137c478bd9Sstevel@tonic-gate if (flags & IFF_IPV4) { 24147c478bd9Sstevel@tonic-gate udp_dev_name = UDP_DEV_NAME; 24157c478bd9Sstevel@tonic-gate } else if (flags & IFF_IPV6) { 24167c478bd9Sstevel@tonic-gate udp_dev_name = UDP6_DEV_NAME; 24177c478bd9Sstevel@tonic-gate } else { 24187c478bd9Sstevel@tonic-gate return (-1); 24197c478bd9Sstevel@tonic-gate } 24207c478bd9Sstevel@tonic-gate 2421fc80c0dfSnordmark if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) { 2422fc80c0dfSnordmark Perror2("open", udp_dev_name); 24237c478bd9Sstevel@tonic-gate return (-1); 24247c478bd9Sstevel@tonic-gate } 2425fc80c0dfSnordmark if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) { 2426fc80c0dfSnordmark Perror2("SIOCGLIFMUXID", udp_dev_name); 24277c478bd9Sstevel@tonic-gate return (-1); 24287c478bd9Sstevel@tonic-gate } 24297c478bd9Sstevel@tonic-gate if (debug > 0) { 24307c478bd9Sstevel@tonic-gate (void) printf("ARP_muxid %d IP_muxid %d\n", 24317c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid, lifr.lifr_ip_muxid); 24327c478bd9Sstevel@tonic-gate } 24337c478bd9Sstevel@tonic-gate 2434fc80c0dfSnordmark /* 2435fc80c0dfSnordmark * Use /dev/udp{,6} as the mux to avoid linkcycles. 2436fc80c0dfSnordmark */ 24376e91bba0SGirish Moodalbail if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS) 24387c478bd9Sstevel@tonic-gate return (-1); 24397c478bd9Sstevel@tonic-gate 24407c478bd9Sstevel@tonic-gate if (lifr.lifr_arp_muxid != 0) { 24417c478bd9Sstevel@tonic-gate if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 24427c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid)) < 0) { 24437c478bd9Sstevel@tonic-gate if ((errno == EINVAL) && 24447c478bd9Sstevel@tonic-gate (flags & (IFF_NOARP | IFF_IPV6))) { 24457c478bd9Sstevel@tonic-gate /* 24467c478bd9Sstevel@tonic-gate * Some plumbing utilities set the muxid to 24477c478bd9Sstevel@tonic-gate * -1 or some invalid value to signify that 24487c478bd9Sstevel@tonic-gate * there is no arp stream. Set the muxid to 0 24497c478bd9Sstevel@tonic-gate * before trying to unplumb the IP stream. 24507c478bd9Sstevel@tonic-gate * IP does not allow the IP stream to be 24517c478bd9Sstevel@tonic-gate * unplumbed if it sees a non-null arp muxid, 24527c478bd9Sstevel@tonic-gate * for consistency of IP-ARP streams. 24537c478bd9Sstevel@tonic-gate */ 24547c478bd9Sstevel@tonic-gate *orig_arpid = lifr.lifr_arp_muxid; 24557c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = 0; 2456fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, 24577c478bd9Sstevel@tonic-gate (caddr_t)&lifr); 24587c478bd9Sstevel@tonic-gate *arpfd_lowstr = -1; 24597c478bd9Sstevel@tonic-gate } else { 24607c478bd9Sstevel@tonic-gate Perror0("_I_MUXID2FD"); 24617c478bd9Sstevel@tonic-gate return (-1); 24627c478bd9Sstevel@tonic-gate } 24637c478bd9Sstevel@tonic-gate } else if (ioctl(*muxfd, I_PUNLINK, 24647c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid) < 0) { 24657c478bd9Sstevel@tonic-gate Perror2("I_PUNLINK", udp_dev_name); 24667c478bd9Sstevel@tonic-gate return (-1); 24677c478bd9Sstevel@tonic-gate } 24687c478bd9Sstevel@tonic-gate } else { 24697c478bd9Sstevel@tonic-gate *arpfd_lowstr = -1; 24707c478bd9Sstevel@tonic-gate } 24717c478bd9Sstevel@tonic-gate 24727c478bd9Sstevel@tonic-gate if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 24737c478bd9Sstevel@tonic-gate lifr.lifr_ip_muxid)) < 0) { 24747c478bd9Sstevel@tonic-gate Perror0("_I_MUXID2FD"); 24757c478bd9Sstevel@tonic-gate /* Undo any changes we made */ 24767c478bd9Sstevel@tonic-gate if (*orig_arpid != 0) { 24777c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = *orig_arpid; 2478fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 24797c478bd9Sstevel@tonic-gate } 24807c478bd9Sstevel@tonic-gate return (-1); 24817c478bd9Sstevel@tonic-gate } 24827c478bd9Sstevel@tonic-gate if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) { 24837c478bd9Sstevel@tonic-gate Perror2("I_PUNLINK", udp_dev_name); 24847c478bd9Sstevel@tonic-gate /* Undo any changes we made */ 24857c478bd9Sstevel@tonic-gate if (*orig_arpid != 0) { 24867c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = *orig_arpid; 2487fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate return (-1); 24907c478bd9Sstevel@tonic-gate } 24917c478bd9Sstevel@tonic-gate return (0); 24927c478bd9Sstevel@tonic-gate } 24937c478bd9Sstevel@tonic-gate 24947c478bd9Sstevel@tonic-gate /* 24957c478bd9Sstevel@tonic-gate * Helper function for mod*() functions. It I_PLINK's back the upper and 24967c478bd9Sstevel@tonic-gate * lower IP streams. Note that this function must be called after 24977c478bd9Sstevel@tonic-gate * ip_domux2fd(). In ip_domux2fd(), the global variable lifr is initialized 24987c478bd9Sstevel@tonic-gate * and ip_plink() needs information in lifr. So ip_domux2fd() and ip_plink() 24997c478bd9Sstevel@tonic-gate * must be called in pairs. 25007c478bd9Sstevel@tonic-gate * 25017c478bd9Sstevel@tonic-gate * Param: 2502fc80c0dfSnordmark * int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2503fc80c0dfSnordmark * int muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2504fc80c0dfSnordmark * int ipfd_lowstr: fd to the lower IP stream. 2505fc80c0dfSnordmark * int arpfd_lowstr: fd to the lower ARP stream. 25067c478bd9Sstevel@tonic-gate * 25077c478bd9Sstevel@tonic-gate * Return: 25087c478bd9Sstevel@tonic-gate * -1 if operation fails, 0 otherwise. 25097c478bd9Sstevel@tonic-gate * 2510e11c3f44Smeem * Please see the big block comment above ifplumb() for the logic of the 2511e11c3f44Smeem * PLINK/PUNLINK 25127c478bd9Sstevel@tonic-gate */ 25137c478bd9Sstevel@tonic-gate static int 2514fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr, 2515fc80c0dfSnordmark int orig_arpid) 25167c478bd9Sstevel@tonic-gate { 25177c478bd9Sstevel@tonic-gate int ip_muxid; 25187c478bd9Sstevel@tonic-gate 25197c478bd9Sstevel@tonic-gate ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr); 25207c478bd9Sstevel@tonic-gate if (ip_muxid < 0) { 25217c478bd9Sstevel@tonic-gate Perror2("I_PLINK", UDP_DEV_NAME); 25227c478bd9Sstevel@tonic-gate return (-1); 25237c478bd9Sstevel@tonic-gate } 25247c478bd9Sstevel@tonic-gate 25257c478bd9Sstevel@tonic-gate /* 25267c478bd9Sstevel@tonic-gate * If there is an arp stream, plink it. If there is no 25277c478bd9Sstevel@tonic-gate * arp stream, then it is possible that the plumbing 25287c478bd9Sstevel@tonic-gate * utility could have stored any value in the arp_muxid. 25297c478bd9Sstevel@tonic-gate * If so, restore it from orig_arpid. 25307c478bd9Sstevel@tonic-gate */ 25317c478bd9Sstevel@tonic-gate if (arpfd_lowstr != -1) { 25327c478bd9Sstevel@tonic-gate if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) { 25337c478bd9Sstevel@tonic-gate Perror2("I_PLINK", UDP_DEV_NAME); 25347c478bd9Sstevel@tonic-gate return (-1); 25357c478bd9Sstevel@tonic-gate } 25367c478bd9Sstevel@tonic-gate } else if (orig_arpid != 0) { 25377c478bd9Sstevel@tonic-gate /* Undo the changes we did in ip_domux2fd */ 25387c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = orig_arpid; 25397c478bd9Sstevel@tonic-gate lifr.lifr_ip_muxid = ip_muxid; 2540fc80c0dfSnordmark (void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 25417c478bd9Sstevel@tonic-gate } 25427c478bd9Sstevel@tonic-gate 2543fc80c0dfSnordmark (void) close(muxfd); 2544fc80c0dfSnordmark (void) close(muxid_fd); 25457c478bd9Sstevel@tonic-gate return (0); 25467c478bd9Sstevel@tonic-gate } 25477c478bd9Sstevel@tonic-gate 25487c478bd9Sstevel@tonic-gate /* 25497c478bd9Sstevel@tonic-gate * The real function to perform module insertion/removal. 25507c478bd9Sstevel@tonic-gate * 25517c478bd9Sstevel@tonic-gate * Param: 25527c478bd9Sstevel@tonic-gate * char *arg: the argument string module_name@position 25537c478bd9Sstevel@tonic-gate * char op: operation, either MODINSERT_OP or MODREMOVE_OP. 25547c478bd9Sstevel@tonic-gate * 25557c478bd9Sstevel@tonic-gate * Return: 25567c478bd9Sstevel@tonic-gate * Before doing ip_domux2fd(), this function calls exit(1) in case of 25577c478bd9Sstevel@tonic-gate * error. After ip_domux2fd() is done, it returns -1 for error, 0 25587c478bd9Sstevel@tonic-gate * otherwise. 25597c478bd9Sstevel@tonic-gate */ 25607c478bd9Sstevel@tonic-gate static int 25617c478bd9Sstevel@tonic-gate modop(char *arg, char op) 25627c478bd9Sstevel@tonic-gate { 25637c478bd9Sstevel@tonic-gate char *pos_p; 25647c478bd9Sstevel@tonic-gate int muxfd; 2565fc80c0dfSnordmark int muxid_fd; 25667c478bd9Sstevel@tonic-gate int ipfd_lowstr; /* IP stream (lower stream of mux) to be plinked */ 25677c478bd9Sstevel@tonic-gate int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */ 25687c478bd9Sstevel@tonic-gate struct strmodconf mod; 25697c478bd9Sstevel@tonic-gate char *at_char = "@"; 25707c478bd9Sstevel@tonic-gate char *arg_str; 25717c478bd9Sstevel@tonic-gate int orig_arpid; 25727c478bd9Sstevel@tonic-gate 2573e11c3f44Smeem /* 2574e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2575e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 2576e11c3f44Smeem */ 2577e11c3f44Smeem if (!modcheck(name)) 2578e11c3f44Smeem exit(1); 25797c478bd9Sstevel@tonic-gate 25807c478bd9Sstevel@tonic-gate /* Need to save the original string for -a option. */ 25817c478bd9Sstevel@tonic-gate if ((arg_str = malloc(strlen(arg) + 1)) == NULL) { 25827c478bd9Sstevel@tonic-gate Perror0("cannot malloc"); 25837c478bd9Sstevel@tonic-gate return (-1); 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate (void) strcpy(arg_str, arg); 25867c478bd9Sstevel@tonic-gate 25877c478bd9Sstevel@tonic-gate if (*arg_str == *at_char) { 25887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 25897c478bd9Sstevel@tonic-gate "ifconfig: must supply a module name\n"); 25907c478bd9Sstevel@tonic-gate exit(1); 25917c478bd9Sstevel@tonic-gate } 25927c478bd9Sstevel@tonic-gate mod.mod_name = strtok(arg_str, at_char); 25937c478bd9Sstevel@tonic-gate if (strlen(mod.mod_name) > FMNAMESZ) { 25947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: module name too long: %s\n", 25957c478bd9Sstevel@tonic-gate mod.mod_name); 25967c478bd9Sstevel@tonic-gate exit(1); 25977c478bd9Sstevel@tonic-gate } 25987c478bd9Sstevel@tonic-gate 25997c478bd9Sstevel@tonic-gate /* 26007c478bd9Sstevel@tonic-gate * Need to make sure that the core TCP/IP stack modules are not 26017c478bd9Sstevel@tonic-gate * removed. Otherwise, "bad" things can happen. If a module 26027c478bd9Sstevel@tonic-gate * is removed and inserted back, it loses its old state. But 26037c478bd9Sstevel@tonic-gate * the modules above it still have the old state. E.g. IP assumes 26047c478bd9Sstevel@tonic-gate * fast data path while tunnel after re-inserted assumes that it can 26057c478bd9Sstevel@tonic-gate * receive M_DATA only in fast data path for which it does not have 26067c478bd9Sstevel@tonic-gate * any state. This is a general caveat of _I_REMOVE/_I_INSERT. 26077c478bd9Sstevel@tonic-gate */ 26087c478bd9Sstevel@tonic-gate if (op == MODREMOVE_OP && 26097c478bd9Sstevel@tonic-gate (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 || 26102b24ab6bSSebastien Roy strcmp(mod.mod_name, IP_MOD_NAME) == 0)) { 26117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: cannot remove %s\n", 26127c478bd9Sstevel@tonic-gate mod.mod_name); 26137c478bd9Sstevel@tonic-gate exit(1); 26147c478bd9Sstevel@tonic-gate } 26157c478bd9Sstevel@tonic-gate 26167c478bd9Sstevel@tonic-gate if ((pos_p = strtok(NULL, at_char)) == NULL) { 26177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: must supply a position\n"); 26187c478bd9Sstevel@tonic-gate exit(1); 26197c478bd9Sstevel@tonic-gate } 26207c478bd9Sstevel@tonic-gate mod.pos = atoi(pos_p); 26217c478bd9Sstevel@tonic-gate 2622fc80c0dfSnordmark if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 26237c478bd9Sstevel@tonic-gate &orig_arpid) < 0) { 26247c478bd9Sstevel@tonic-gate free(arg_str); 26257c478bd9Sstevel@tonic-gate return (-1); 26267c478bd9Sstevel@tonic-gate } 26277c478bd9Sstevel@tonic-gate switch (op) { 26287c478bd9Sstevel@tonic-gate case MODINSERT_OP: 26297c478bd9Sstevel@tonic-gate if (debug > 0) { 26307c478bd9Sstevel@tonic-gate (void) printf("Inserting module %s at %d\n", 26317c478bd9Sstevel@tonic-gate mod.mod_name, mod.pos); 26327c478bd9Sstevel@tonic-gate } 26337c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) { 26347c478bd9Sstevel@tonic-gate Perror2("fail to insert module", mod.mod_name); 26357c478bd9Sstevel@tonic-gate } 26367c478bd9Sstevel@tonic-gate break; 26377c478bd9Sstevel@tonic-gate case MODREMOVE_OP: 26387c478bd9Sstevel@tonic-gate if (debug > 0) { 26397c478bd9Sstevel@tonic-gate (void) printf("Removing module %s at %d\n", 26407c478bd9Sstevel@tonic-gate mod.mod_name, mod.pos); 26417c478bd9Sstevel@tonic-gate } 26427c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) { 26437c478bd9Sstevel@tonic-gate Perror2("fail to remove module", mod.mod_name); 26447c478bd9Sstevel@tonic-gate } 26457c478bd9Sstevel@tonic-gate break; 26467c478bd9Sstevel@tonic-gate default: 26477c478bd9Sstevel@tonic-gate /* Should never get to here. */ 26487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Unknown operation\n"); 26497c478bd9Sstevel@tonic-gate break; 26507c478bd9Sstevel@tonic-gate } 26517c478bd9Sstevel@tonic-gate free(arg_str); 2652fc80c0dfSnordmark return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2653fc80c0dfSnordmark orig_arpid)); 26547c478bd9Sstevel@tonic-gate } 26557c478bd9Sstevel@tonic-gate 26562b24ab6bSSebastien Roy static int 26572b24ab6bSSebastien Roy modify_tun(iptun_params_t *params) 26582b24ab6bSSebastien Roy { 26592b24ab6bSSebastien Roy dladm_status_t status; 26602b24ab6bSSebastien Roy 26612b24ab6bSSebastien Roy if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 26622b24ab6bSSebastien Roy ¶ms->iptun_param_linkid)) == DLADM_STATUS_OK) 26632b24ab6bSSebastien Roy status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE); 26642b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 26652b24ab6bSSebastien Roy dladmerr_exit(status, name); 26662b24ab6bSSebastien Roy return (0); 26672b24ab6bSSebastien Roy } 26682b24ab6bSSebastien Roy 26697c478bd9Sstevel@tonic-gate /* 26707c478bd9Sstevel@tonic-gate * Set tunnel source address 26717c478bd9Sstevel@tonic-gate */ 26727c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26737c478bd9Sstevel@tonic-gate static int 26747c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param) 26757c478bd9Sstevel@tonic-gate { 26762b24ab6bSSebastien Roy iptun_params_t params; 26772b24ab6bSSebastien Roy 26782b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_LADDR; 26792b24ab6bSSebastien Roy (void) strlcpy(params.iptun_param_laddr, addr, 26802b24ab6bSSebastien Roy sizeof (params.iptun_param_laddr)); 26812b24ab6bSSebastien Roy return (modify_tun(¶ms)); 26827c478bd9Sstevel@tonic-gate } 26837c478bd9Sstevel@tonic-gate 26847c478bd9Sstevel@tonic-gate /* 26857c478bd9Sstevel@tonic-gate * Set tunnel destination address 26867c478bd9Sstevel@tonic-gate */ 26877c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26887c478bd9Sstevel@tonic-gate static int 26897c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param) 26907c478bd9Sstevel@tonic-gate { 26912b24ab6bSSebastien Roy iptun_params_t params; 26922b24ab6bSSebastien Roy 26932b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_RADDR; 26942b24ab6bSSebastien Roy (void) strlcpy(params.iptun_param_raddr, addr, 26952b24ab6bSSebastien Roy sizeof (params.iptun_param_raddr)); 26962b24ab6bSSebastien Roy return (modify_tun(¶ms)); 26977c478bd9Sstevel@tonic-gate } 26987c478bd9Sstevel@tonic-gate 26997c478bd9Sstevel@tonic-gate static int 27002b24ab6bSSebastien Roy set_tun_prop(const char *propname, char *value) 27017c478bd9Sstevel@tonic-gate { 27022b24ab6bSSebastien Roy dladm_status_t status; 27032b24ab6bSSebastien Roy datalink_id_t linkid; 27047c478bd9Sstevel@tonic-gate 27052b24ab6bSSebastien Roy status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid); 27062b24ab6bSSebastien Roy if (status == DLADM_STATUS_OK) { 27072b24ab6bSSebastien Roy status = dladm_set_linkprop(dlh, linkid, propname, &value, 1, 27082b24ab6bSSebastien Roy DLADM_OPT_ACTIVE); 27097c478bd9Sstevel@tonic-gate } 27102b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 27112b24ab6bSSebastien Roy dladmerr_exit(status, name); 27127c478bd9Sstevel@tonic-gate return (0); 27137c478bd9Sstevel@tonic-gate } 27147c478bd9Sstevel@tonic-gate 27157c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */ 27167c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27177c478bd9Sstevel@tonic-gate static int 27187c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param) 27197c478bd9Sstevel@tonic-gate { 27202b24ab6bSSebastien Roy return (set_tun_prop("encaplimit", arg)); 27217c478bd9Sstevel@tonic-gate } 27227c478bd9Sstevel@tonic-gate 27237c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */ 27247c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27257c478bd9Sstevel@tonic-gate static int 27267c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param) 27277c478bd9Sstevel@tonic-gate { 27282b24ab6bSSebastien Roy return (set_tun_encap_limit("-1", 0)); 27297c478bd9Sstevel@tonic-gate } 27307c478bd9Sstevel@tonic-gate 27317c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */ 27327c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27337c478bd9Sstevel@tonic-gate static int 27347c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param) 27357c478bd9Sstevel@tonic-gate { 27362b24ab6bSSebastien Roy return (set_tun_prop("hoplimit", arg)); 27377c478bd9Sstevel@tonic-gate } 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate /* Set zone ID */ 27407c478bd9Sstevel@tonic-gate static int 27417c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param) 27427c478bd9Sstevel@tonic-gate { 27437c478bd9Sstevel@tonic-gate zoneid_t zoneid = GLOBAL_ZONEID; 27447c478bd9Sstevel@tonic-gate 27457c478bd9Sstevel@tonic-gate if (param == NEXTARG) { 27467c478bd9Sstevel@tonic-gate /* zone must be active */ 27477c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(arg)) == -1) { 27487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 27497c478bd9Sstevel@tonic-gate "ifconfig: unknown zone '%s'\n", arg); 27507c478bd9Sstevel@tonic-gate exit(1); 27517c478bd9Sstevel@tonic-gate } 27527c478bd9Sstevel@tonic-gate } 27537c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 27547c478bd9Sstevel@tonic-gate lifr.lifr_zoneid = zoneid; 27557c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 27567c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFZONE"); 27577c478bd9Sstevel@tonic-gate return (0); 27587c478bd9Sstevel@tonic-gate } 27597c478bd9Sstevel@tonic-gate 276045916cd2Sjpk /* Put interface into all zones */ 276145916cd2Sjpk /* ARGSUSED */ 276245916cd2Sjpk static int 276345916cd2Sjpk setallzones(char *arg, int64_t param) 276445916cd2Sjpk { 276545916cd2Sjpk (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 276645916cd2Sjpk lifr.lifr_zoneid = ALL_ZONES; 276745916cd2Sjpk if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 276845916cd2Sjpk Perror0_exit("SIOCSLIFZONE"); 276945916cd2Sjpk return (0); 277045916cd2Sjpk } 277145916cd2Sjpk 27727c478bd9Sstevel@tonic-gate /* Set source address to use */ 27737c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27747c478bd9Sstevel@tonic-gate static int 27757c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param) 27767c478bd9Sstevel@tonic-gate { 27777c478bd9Sstevel@tonic-gate uint_t ifindex = 0; 27787c478bd9Sstevel@tonic-gate int rval; 27797c478bd9Sstevel@tonic-gate 27807c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate /* 27837c478bd9Sstevel@tonic-gate * Argument can be either an interface name or "none". The latter means 27847c478bd9Sstevel@tonic-gate * that any previous selection is cleared. 27857c478bd9Sstevel@tonic-gate */ 27867c478bd9Sstevel@tonic-gate 27876e91bba0SGirish Moodalbail if (strchr(arg, ':') != NULL) { 27887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 27896e91bba0SGirish Moodalbail "ifconfig: Cannot specify logical interface for usesrc \n"); 27907c478bd9Sstevel@tonic-gate exit(1); 27917c478bd9Sstevel@tonic-gate } 27927c478bd9Sstevel@tonic-gate 27937c478bd9Sstevel@tonic-gate rval = strcmp(arg, NONE_STR); 27947c478bd9Sstevel@tonic-gate if (rval != 0) { 27957c478bd9Sstevel@tonic-gate if ((ifindex = if_nametoindex(arg)) == 0) { 27967c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ); 27977c478bd9Sstevel@tonic-gate Perror0_exit("Could not get interface index"); 27987c478bd9Sstevel@tonic-gate } 27997c478bd9Sstevel@tonic-gate lifr.lifr_index = ifindex; 28007c478bd9Sstevel@tonic-gate } else { 28017c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0) 28027c478bd9Sstevel@tonic-gate Perror0_exit("Not a valid usesrc consumer"); 28037c478bd9Sstevel@tonic-gate lifr.lifr_index = 0; 28047c478bd9Sstevel@tonic-gate } 28057c478bd9Sstevel@tonic-gate 28067c478bd9Sstevel@tonic-gate if (debug) 28077c478bd9Sstevel@tonic-gate (void) printf("setifsrc: lifr_name %s, lifr_index %d\n", 28087c478bd9Sstevel@tonic-gate lifr.lifr_name, lifr.lifr_index); 28097c478bd9Sstevel@tonic-gate 28107c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) { 28117c478bd9Sstevel@tonic-gate if (rval == 0) 28127c478bd9Sstevel@tonic-gate Perror0_exit("Cannot reset usesrc group"); 28137c478bd9Sstevel@tonic-gate else 28147c478bd9Sstevel@tonic-gate Perror0_exit("Could not set source interface"); 28157c478bd9Sstevel@tonic-gate } 28167c478bd9Sstevel@tonic-gate 28177c478bd9Sstevel@tonic-gate return (0); 28187c478bd9Sstevel@tonic-gate } 28197c478bd9Sstevel@tonic-gate 28207c478bd9Sstevel@tonic-gate /* 28217c478bd9Sstevel@tonic-gate * Print the interface status line associated with `ifname' 28227c478bd9Sstevel@tonic-gate */ 28237c478bd9Sstevel@tonic-gate static void 28247c478bd9Sstevel@tonic-gate ifstatus(const char *ifname) 28257c478bd9Sstevel@tonic-gate { 28267c478bd9Sstevel@tonic-gate uint64_t flags; 28277c478bd9Sstevel@tonic-gate char if_usesrc_name[LIFNAMSIZ]; 28287c478bd9Sstevel@tonic-gate char *newbuf; 28297c478bd9Sstevel@tonic-gate int n, numifs, rval = 0; 28307c478bd9Sstevel@tonic-gate struct lifreq *lifrp; 28317c478bd9Sstevel@tonic-gate struct lifsrcof lifs; 28327c478bd9Sstevel@tonic-gate 28337c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 28347c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 28357c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 28367c478bd9Sstevel@tonic-gate } 28377c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 28387c478bd9Sstevel@tonic-gate 28397c478bd9Sstevel@tonic-gate /* 28407c478bd9Sstevel@tonic-gate * In V4 compatibility mode, we don't print the IFF_IPV4 flag or 28417c478bd9Sstevel@tonic-gate * interfaces with IFF_IPV6 set. 28427c478bd9Sstevel@tonic-gate */ 28437c478bd9Sstevel@tonic-gate if (v4compat) { 28447c478bd9Sstevel@tonic-gate flags &= ~IFF_IPV4; 28457c478bd9Sstevel@tonic-gate if (flags & IFF_IPV6) 28467c478bd9Sstevel@tonic-gate return; 28477c478bd9Sstevel@tonic-gate } 28487c478bd9Sstevel@tonic-gate 28497c478bd9Sstevel@tonic-gate (void) printf("%s: ", ifname); 28507c478bd9Sstevel@tonic-gate print_flags(flags); 28517c478bd9Sstevel@tonic-gate 28527c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 28537c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 28547c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFMETRIC"); 28557c478bd9Sstevel@tonic-gate } else { 28567c478bd9Sstevel@tonic-gate if (lifr.lifr_metric) 28577c478bd9Sstevel@tonic-gate (void) printf(" metric %d", lifr.lifr_metric); 28587c478bd9Sstevel@tonic-gate } 28597c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 2860c08e5e1aSdr (void) printf(" mtu %u", lifr.lifr_mtu); 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate /* don't print index or zone when in compatibility mode */ 28637c478bd9Sstevel@tonic-gate if (!v4compat) { 28647c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 28657c478bd9Sstevel@tonic-gate (void) printf(" index %d", lifr.lifr_index); 2866f4b3ec61Sdh /* 2867f4b3ec61Sdh * Stack instances use GLOBAL_ZONEID for IP data structures 2868f4b3ec61Sdh * even in the non-global zone. 2869f4b3ec61Sdh */ 28707c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 && 2871f4b3ec61Sdh lifr.lifr_zoneid != getzoneid() && 2872f4b3ec61Sdh lifr.lifr_zoneid != GLOBAL_ZONEID) { 28737c478bd9Sstevel@tonic-gate char zone_name[ZONENAME_MAX]; 28747c478bd9Sstevel@tonic-gate 287545916cd2Sjpk if (lifr.lifr_zoneid == ALL_ZONES) { 287645916cd2Sjpk (void) printf("\n\tall-zones"); 287745916cd2Sjpk } else if (getzonenamebyid(lifr.lifr_zoneid, zone_name, 28787c478bd9Sstevel@tonic-gate sizeof (zone_name)) < 0) { 28797c478bd9Sstevel@tonic-gate (void) printf("\n\tzone %d", lifr.lifr_zoneid); 28807c478bd9Sstevel@tonic-gate } else { 28817c478bd9Sstevel@tonic-gate (void) printf("\n\tzone %s", zone_name); 28827c478bd9Sstevel@tonic-gate } 28837c478bd9Sstevel@tonic-gate } 28847c478bd9Sstevel@tonic-gate } 28857c478bd9Sstevel@tonic-gate 28867c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) { 28877c478bd9Sstevel@tonic-gate lifs.lifs_ifindex = lifr.lifr_index; 28887c478bd9Sstevel@tonic-gate 28897c478bd9Sstevel@tonic-gate /* 28907c478bd9Sstevel@tonic-gate * Find the number of interfaces that use this interfaces' 28917c478bd9Sstevel@tonic-gate * address as a source address 28927c478bd9Sstevel@tonic-gate */ 28937c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 28947c478bd9Sstevel@tonic-gate lifs.lifs_maxlen = 0; 28957c478bd9Sstevel@tonic-gate for (;;) { 28967c478bd9Sstevel@tonic-gate /* The first pass will give the bufsize we need */ 28977c478bd9Sstevel@tonic-gate rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs); 28987c478bd9Sstevel@tonic-gate if (rval < 0) { 28997c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) { 29007c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29017c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 29027c478bd9Sstevel@tonic-gate } 29037c478bd9Sstevel@tonic-gate lifs.lifs_len = 0; 29047c478bd9Sstevel@tonic-gate break; 29057c478bd9Sstevel@tonic-gate } 29067c478bd9Sstevel@tonic-gate if (lifs.lifs_len <= lifs.lifs_maxlen) 29077c478bd9Sstevel@tonic-gate break; 29087c478bd9Sstevel@tonic-gate /* Use kernel's size + a small margin to avoid loops */ 29097c478bd9Sstevel@tonic-gate lifs.lifs_maxlen = lifs.lifs_len + 29107c478bd9Sstevel@tonic-gate 5 * sizeof (struct lifreq); 29117c478bd9Sstevel@tonic-gate /* For the first pass, realloc acts like malloc */ 29127c478bd9Sstevel@tonic-gate newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen); 29137c478bd9Sstevel@tonic-gate if (newbuf == NULL) { 29147c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) { 29157c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29167c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 29177c478bd9Sstevel@tonic-gate } 29187c478bd9Sstevel@tonic-gate lifs.lifs_len = 0; 29197c478bd9Sstevel@tonic-gate break; 29207c478bd9Sstevel@tonic-gate } 29217c478bd9Sstevel@tonic-gate lifs.lifs_buf = newbuf; 29227c478bd9Sstevel@tonic-gate } 29237c478bd9Sstevel@tonic-gate 29247c478bd9Sstevel@tonic-gate 29257c478bd9Sstevel@tonic-gate numifs = lifs.lifs_len / sizeof (struct lifreq); 29267c478bd9Sstevel@tonic-gate if (numifs > 0) { 29277c478bd9Sstevel@tonic-gate lifrp = lifs.lifs_req; 29287c478bd9Sstevel@tonic-gate (void) printf("\n\tsrcof"); 29297c478bd9Sstevel@tonic-gate for (n = numifs; n > 0; n--, lifrp++) { 29307c478bd9Sstevel@tonic-gate (void) printf(" %s", lifrp->lifr_name); 29317c478bd9Sstevel@tonic-gate } 29327c478bd9Sstevel@tonic-gate } 29337c478bd9Sstevel@tonic-gate 29347c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) 29357c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29367c478bd9Sstevel@tonic-gate } 29377c478bd9Sstevel@tonic-gate 29387c478bd9Sstevel@tonic-gate /* Find the interface whose source address this interface uses */ 29397c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 29407c478bd9Sstevel@tonic-gate if (lifr.lifr_index != 0) { 29417c478bd9Sstevel@tonic-gate if (if_indextoname(lifr.lifr_index, 29427c478bd9Sstevel@tonic-gate if_usesrc_name) == NULL) { 29437c478bd9Sstevel@tonic-gate (void) printf("\n\tusesrc ifIndex %d", 29447c478bd9Sstevel@tonic-gate lifr.lifr_index); 29457c478bd9Sstevel@tonic-gate } else { 29467c478bd9Sstevel@tonic-gate (void) printf("\n\tusesrc %s", if_usesrc_name); 29477c478bd9Sstevel@tonic-gate } 29487c478bd9Sstevel@tonic-gate } 29497c478bd9Sstevel@tonic-gate } 29507c478bd9Sstevel@tonic-gate 29517c478bd9Sstevel@tonic-gate (void) putchar('\n'); 29527c478bd9Sstevel@tonic-gate } 29537c478bd9Sstevel@tonic-gate 29547c478bd9Sstevel@tonic-gate /* 29557c478bd9Sstevel@tonic-gate * Print the status of the interface. If an address family was 29567c478bd9Sstevel@tonic-gate * specified, show it and it only; otherwise, show them all. 29577c478bd9Sstevel@tonic-gate */ 29587c478bd9Sstevel@tonic-gate static void 29597c478bd9Sstevel@tonic-gate status(void) 29607c478bd9Sstevel@tonic-gate { 29612b24ab6bSSebastien Roy struct afswtch *p = afp; 29622b24ab6bSSebastien Roy uint64_t flags; 29632b24ab6bSSebastien Roy datalink_id_t linkid; 29647c478bd9Sstevel@tonic-gate 29657c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 29667c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 29677c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 29687c478bd9Sstevel@tonic-gate } 29697c478bd9Sstevel@tonic-gate 29707c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 29717c478bd9Sstevel@tonic-gate 29727c478bd9Sstevel@tonic-gate /* 29737c478bd9Sstevel@tonic-gate * Only print the interface status if the address family matches 29747c478bd9Sstevel@tonic-gate * the interface family flag. 29757c478bd9Sstevel@tonic-gate */ 29767c478bd9Sstevel@tonic-gate if (p != NULL) { 29777c478bd9Sstevel@tonic-gate if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) || 29787c478bd9Sstevel@tonic-gate ((p->af_af == AF_INET) && (flags & IFF_IPV6))) 29797c478bd9Sstevel@tonic-gate return; 29807c478bd9Sstevel@tonic-gate } 29817c478bd9Sstevel@tonic-gate 29827c478bd9Sstevel@tonic-gate /* 29837c478bd9Sstevel@tonic-gate * In V4 compatibility mode, don't print IFF_IPV6 interfaces. 29847c478bd9Sstevel@tonic-gate */ 29857c478bd9Sstevel@tonic-gate if (v4compat && (flags & IFF_IPV6)) 29867c478bd9Sstevel@tonic-gate return; 29877c478bd9Sstevel@tonic-gate 29887c478bd9Sstevel@tonic-gate ifstatus(name); 29897c478bd9Sstevel@tonic-gate 29902b24ab6bSSebastien Roy if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) == 29912b24ab6bSSebastien Roy DLADM_STATUS_OK) 29922b24ab6bSSebastien Roy tun_status(linkid); 29932b24ab6bSSebastien Roy 29947c478bd9Sstevel@tonic-gate if (p != NULL) { 29957c478bd9Sstevel@tonic-gate (*p->af_status)(1, flags); 29967c478bd9Sstevel@tonic-gate } else { 29977c478bd9Sstevel@tonic-gate for (p = afs; p->af_name; p++) { 29987c478bd9Sstevel@tonic-gate /* set global af for use in p->af_status */ 29997c478bd9Sstevel@tonic-gate af = p->af_af; 30007c478bd9Sstevel@tonic-gate (*p->af_status)(0, flags); 30017c478bd9Sstevel@tonic-gate } 30027c478bd9Sstevel@tonic-gate 30037c478bd9Sstevel@tonic-gate /* 30047c478bd9Sstevel@tonic-gate * Historically, 'ether' has been an address family, 30057c478bd9Sstevel@tonic-gate * so print it here. 30067c478bd9Sstevel@tonic-gate */ 30077c478bd9Sstevel@tonic-gate print_ifether(name); 30087c478bd9Sstevel@tonic-gate } 30097c478bd9Sstevel@tonic-gate } 30107c478bd9Sstevel@tonic-gate 30117c478bd9Sstevel@tonic-gate /* 30127c478bd9Sstevel@tonic-gate * Print the status of the interface in a format that can be used to 30137c478bd9Sstevel@tonic-gate * reconfigure the interface later. Code stolen from status() above. 30147c478bd9Sstevel@tonic-gate */ 30157c478bd9Sstevel@tonic-gate /* ARGSUSED */ 30167c478bd9Sstevel@tonic-gate static int 30177c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param) 30187c478bd9Sstevel@tonic-gate { 3019e11c3f44Smeem char *cp; 30207c478bd9Sstevel@tonic-gate struct afswtch *p = afp; 30217c478bd9Sstevel@tonic-gate uint64_t flags; 3022e11c3f44Smeem char lifname[LIFNAMSIZ]; 30237c478bd9Sstevel@tonic-gate char if_usesrc_name[LIFNAMSIZ]; 30247c478bd9Sstevel@tonic-gate 30257c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3026e11c3f44Smeem 30277c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 30287c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 30297c478bd9Sstevel@tonic-gate } 30307c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate if (debug) { 30337c478bd9Sstevel@tonic-gate (void) printf("configinfo: name %s flags 0x%llx af_af %d\n", 30347c478bd9Sstevel@tonic-gate name, flags, p != NULL ? p->af_af : -1); 30357c478bd9Sstevel@tonic-gate } 30367c478bd9Sstevel@tonic-gate 3037e11c3f44Smeem /* 303867ce1608SVasumathi Sundaram * Build the interface name to print (we cannot directly use `name' 3039e11c3f44Smeem * because one cannot "plumb" ":0" interfaces). 3040e11c3f44Smeem */ 3041e11c3f44Smeem (void) strlcpy(lifname, name, LIFNAMSIZ); 3042e11c3f44Smeem if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0) 3043e11c3f44Smeem *cp = '\0'; 30447c478bd9Sstevel@tonic-gate 30457c478bd9Sstevel@tonic-gate /* 30467c478bd9Sstevel@tonic-gate * if the interface is IPv4 30477c478bd9Sstevel@tonic-gate * if we have a IPv6 address family restriction return 30487c478bd9Sstevel@tonic-gate * so it won't print 30497c478bd9Sstevel@tonic-gate * if we are in IPv4 compatibility mode, clear out IFF_IPV4 30507c478bd9Sstevel@tonic-gate * so we don't print it. 30517c478bd9Sstevel@tonic-gate */ 30527c478bd9Sstevel@tonic-gate if (flags & IFF_IPV4) { 30537c478bd9Sstevel@tonic-gate if (p && p->af_af == AF_INET6) 30547c478bd9Sstevel@tonic-gate return (-1); 30557c478bd9Sstevel@tonic-gate if (v4compat) 30567c478bd9Sstevel@tonic-gate flags &= ~IFF_IPV4; 30577c478bd9Sstevel@tonic-gate 3058e11c3f44Smeem (void) printf("%s inet plumb", lifname); 30597c478bd9Sstevel@tonic-gate } else if (flags & IFF_IPV6) { 30607c478bd9Sstevel@tonic-gate /* 30617c478bd9Sstevel@tonic-gate * else if the interface is IPv6 30627c478bd9Sstevel@tonic-gate * if we have a IPv4 address family restriction return 30637c478bd9Sstevel@tonic-gate * or we are in IPv4 compatibiltiy mode, return. 30647c478bd9Sstevel@tonic-gate */ 30657c478bd9Sstevel@tonic-gate if (p && p->af_af == AF_INET) 30667c478bd9Sstevel@tonic-gate return (-1); 30677c478bd9Sstevel@tonic-gate if (v4compat) 30687c478bd9Sstevel@tonic-gate return (-1); 30697c478bd9Sstevel@tonic-gate 3070e11c3f44Smeem (void) printf("%s inet6 plumb", lifname); 30717c478bd9Sstevel@tonic-gate } 30727c478bd9Sstevel@tonic-gate 30737c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 30747c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 30757c478bd9Sstevel@tonic-gate Perror0_exit("configinfo: SIOCGLIFMETRIC"); 30767c478bd9Sstevel@tonic-gate } else { 30777c478bd9Sstevel@tonic-gate if (lifr.lifr_metric) 30787c478bd9Sstevel@tonic-gate (void) printf(" metric %d ", lifr.lifr_metric); 30797c478bd9Sstevel@tonic-gate } 30807c478bd9Sstevel@tonic-gate if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) && 3081fc80c0dfSnordmark ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 30827c478bd9Sstevel@tonic-gate (void) printf(" mtu %d", lifr.lifr_metric); 30837c478bd9Sstevel@tonic-gate 3084e11c3f44Smeem /* Index only applies to the zeroth interface */ 3085e11c3f44Smeem if (lifnum(name) == 0) { 30867c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 30877c478bd9Sstevel@tonic-gate (void) printf(" index %d", lifr.lifr_index); 30887c478bd9Sstevel@tonic-gate } 30897c478bd9Sstevel@tonic-gate 30907c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 30917c478bd9Sstevel@tonic-gate if (lifr.lifr_index != 0) { 30927c478bd9Sstevel@tonic-gate if (if_indextoname(lifr.lifr_index, 30937c478bd9Sstevel@tonic-gate if_usesrc_name) != NULL) { 30947c478bd9Sstevel@tonic-gate (void) printf(" usesrc %s", if_usesrc_name); 30957c478bd9Sstevel@tonic-gate } 30967c478bd9Sstevel@tonic-gate } 30977c478bd9Sstevel@tonic-gate } 30987c478bd9Sstevel@tonic-gate 30997c478bd9Sstevel@tonic-gate if (p != NULL) { 31007c478bd9Sstevel@tonic-gate (*p->af_configinfo)(1, flags); 31017c478bd9Sstevel@tonic-gate } else { 31027c478bd9Sstevel@tonic-gate for (p = afs; p->af_name; p++) { 31037c478bd9Sstevel@tonic-gate (void) close(s); 31047c478bd9Sstevel@tonic-gate s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0); 31057c478bd9Sstevel@tonic-gate /* set global af for use in p->af_configinfo */ 31067c478bd9Sstevel@tonic-gate af = p->af_af; 31077c478bd9Sstevel@tonic-gate if (s == -1) { 31087c478bd9Sstevel@tonic-gate Perror0_exit("socket"); 31097c478bd9Sstevel@tonic-gate } 31107c478bd9Sstevel@tonic-gate (*p->af_configinfo)(0, flags); 31117c478bd9Sstevel@tonic-gate } 31127c478bd9Sstevel@tonic-gate } 31137c478bd9Sstevel@tonic-gate 31142b24ab6bSSebastien Roy (void) putchar('\n'); 31157c478bd9Sstevel@tonic-gate return (0); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate static void 31192b24ab6bSSebastien Roy print_tsec(iptun_params_t *params) 31207c478bd9Sstevel@tonic-gate { 31217c478bd9Sstevel@tonic-gate ipsec_req_t *ipsr; 31227c478bd9Sstevel@tonic-gate 31237c478bd9Sstevel@tonic-gate (void) printf("\ttunnel security settings "); 31242b24ab6bSSebastien Roy if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) { 31252b24ab6bSSebastien Roy (void) printf("--> use 'ipsecconf -ln -i %s'", name); 31268810c16bSdanmcd } else { 31272b24ab6bSSebastien Roy ipsr = ¶ms->iptun_param_secinfo; 31288810c16bSdanmcd if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) { 31298810c16bSdanmcd (void) printf("ah (%s) ", 31308810c16bSdanmcd rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH)); 31318810c16bSdanmcd } 31328810c16bSdanmcd if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) { 31338810c16bSdanmcd (void) printf("esp (%s", 31348810c16bSdanmcd rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP)); 31358810c16bSdanmcd (void) printf("/%s)", 31368810c16bSdanmcd rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH)); 31378810c16bSdanmcd } 31387c478bd9Sstevel@tonic-gate } 31397c478bd9Sstevel@tonic-gate (void) printf("\n"); 31407c478bd9Sstevel@tonic-gate } 31417c478bd9Sstevel@tonic-gate 31427c478bd9Sstevel@tonic-gate static void 31432b24ab6bSSebastien Roy tun_status(datalink_id_t linkid) 31447c478bd9Sstevel@tonic-gate { 31452b24ab6bSSebastien Roy iptun_params_t params; 31462b24ab6bSSebastien Roy char propval[DLADM_PROP_VAL_MAX]; 31472b24ab6bSSebastien Roy char *valptr[1]; 31482b24ab6bSSebastien Roy uint_t valcnt = 1; 31492b24ab6bSSebastien Roy boolean_t tabbed = _B_FALSE; 31502b24ab6bSSebastien Roy 31512b24ab6bSSebastien Roy params.iptun_param_linkid = linkid; 31522b24ab6bSSebastien Roy 31532b24ab6bSSebastien Roy /* If dladm_iptun_getparams() fails, assume we are not a tunnel. */ 31542b24ab6bSSebastien Roy assert(dlh_opened); 31552b24ab6bSSebastien Roy if (dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE) != 31562b24ab6bSSebastien Roy DLADM_STATUS_OK) 31577c478bd9Sstevel@tonic-gate return; 31587c478bd9Sstevel@tonic-gate 31592b24ab6bSSebastien Roy switch (params.iptun_param_type) { 31602b24ab6bSSebastien Roy case IPTUN_TYPE_IPV4: 31612b24ab6bSSebastien Roy case IPTUN_TYPE_6TO4: 31627c478bd9Sstevel@tonic-gate (void) printf("\tinet"); 31637c478bd9Sstevel@tonic-gate break; 31642b24ab6bSSebastien Roy case IPTUN_TYPE_IPV6: 31657c478bd9Sstevel@tonic-gate (void) printf("\tinet6"); 31667c478bd9Sstevel@tonic-gate break; 31677c478bd9Sstevel@tonic-gate default: 31682b24ab6bSSebastien Roy dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name); 31697c478bd9Sstevel@tonic-gate break; 31707c478bd9Sstevel@tonic-gate } 31717c478bd9Sstevel@tonic-gate 31727c478bd9Sstevel@tonic-gate /* 31732b24ab6bSSebastien Roy * There is always a source address. If it hasn't been explicitly 31742b24ab6bSSebastien Roy * set, the API will pass back a buffer containing the unspecified 31752b24ab6bSSebastien Roy * address. 31767c478bd9Sstevel@tonic-gate */ 31772b24ab6bSSebastien Roy (void) printf(" tunnel src %s ", params.iptun_param_laddr); 31782b24ab6bSSebastien Roy 31792b24ab6bSSebastien Roy if (params.iptun_param_flags & IPTUN_PARAM_RADDR) 31802b24ab6bSSebastien Roy (void) printf("tunnel dst %s\n", params.iptun_param_raddr); 31812b24ab6bSSebastien Roy else 31822b24ab6bSSebastien Roy (void) putchar('\n'); 31832b24ab6bSSebastien Roy 31842b24ab6bSSebastien Roy if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL) 31852b24ab6bSSebastien Roy print_tsec(¶ms); 31867c478bd9Sstevel@tonic-gate 31872b24ab6bSSebastien Roy valptr[0] = propval; 31882b24ab6bSSebastien Roy if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit", 31892b24ab6bSSebastien Roy (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 31902b24ab6bSSebastien Roy (void) printf("\ttunnel hop limit %s ", propval); 31917c478bd9Sstevel@tonic-gate tabbed = _B_TRUE; 31927c478bd9Sstevel@tonic-gate } 31937c478bd9Sstevel@tonic-gate 31942b24ab6bSSebastien Roy if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, 31952b24ab6bSSebastien Roy "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 31962b24ab6bSSebastien Roy uint32_t elim; 31972b24ab6bSSebastien Roy 31987c478bd9Sstevel@tonic-gate if (!tabbed) { 31992b24ab6bSSebastien Roy (void) putchar('\t'); 32007c478bd9Sstevel@tonic-gate tabbed = _B_TRUE; 32017c478bd9Sstevel@tonic-gate } 32022b24ab6bSSebastien Roy elim = strtol(propval, NULL, 10); 32032b24ab6bSSebastien Roy if (elim > 0) 32042b24ab6bSSebastien Roy (void) printf("tunnel encapsulation limit %s", propval); 32052b24ab6bSSebastien Roy else 32067c478bd9Sstevel@tonic-gate (void) printf("tunnel encapsulation limit disabled"); 32077c478bd9Sstevel@tonic-gate } 32087c478bd9Sstevel@tonic-gate 32097c478bd9Sstevel@tonic-gate if (tabbed) 32102b24ab6bSSebastien Roy (void) putchar('\n'); 32117c478bd9Sstevel@tonic-gate } 32127c478bd9Sstevel@tonic-gate 32137c478bd9Sstevel@tonic-gate static void 32147c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags) 32157c478bd9Sstevel@tonic-gate { 32162b24ab6bSSebastien Roy struct sockaddr_in *sin, *laddr; 32172b24ab6bSSebastien Roy struct sockaddr_in netmask = { AF_INET }; 32187c478bd9Sstevel@tonic-gate 32197c478bd9Sstevel@tonic-gate if (debug) 32207c478bd9Sstevel@tonic-gate (void) printf("in_status(%s) flags 0x%llx\n", name, flags); 32217c478bd9Sstevel@tonic-gate 32227c478bd9Sstevel@tonic-gate /* only print status for IPv4 interfaces */ 32237c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV4)) 32247c478bd9Sstevel@tonic-gate return; 32257c478bd9Sstevel@tonic-gate 32267c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 32277c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32287c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 32297c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 32307c478bd9Sstevel@tonic-gate errno == ENXIO) { 32317c478bd9Sstevel@tonic-gate if (!force) 32327c478bd9Sstevel@tonic-gate return; 32337c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32347c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32357c478bd9Sstevel@tonic-gate } else 32367c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFADDR"); 32377c478bd9Sstevel@tonic-gate } 32387c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 32397c478bd9Sstevel@tonic-gate (void) printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 32407c478bd9Sstevel@tonic-gate laddr = sin; 32417c478bd9Sstevel@tonic-gate } else { 32427c478bd9Sstevel@tonic-gate (void) printf("\tinet "); 32437c478bd9Sstevel@tonic-gate } 32447c478bd9Sstevel@tonic-gate 32457c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32467c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 32477c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 32487c478bd9Sstevel@tonic-gate errno == ENXIO) { 32497c478bd9Sstevel@tonic-gate if (!force) 32507c478bd9Sstevel@tonic-gate return; 32517c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32527c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32537c478bd9Sstevel@tonic-gate } else { 32547c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFSUBNET"); 32557c478bd9Sstevel@tonic-gate } 32567c478bd9Sstevel@tonic-gate } 32577c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 32587c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 32597c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 32607c478bd9Sstevel@tonic-gate (void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr), 32617c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 32627c478bd9Sstevel@tonic-gate } 32637c478bd9Sstevel@tonic-gate if (sin->sin_family != AF_INET) { 32647c478bd9Sstevel@tonic-gate (void) printf("Wrong family: %d\n", sin->sin_family); 32657c478bd9Sstevel@tonic-gate } 32667c478bd9Sstevel@tonic-gate 32677c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32687c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 32697c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 32707c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFNETMASK"); 32717c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 32727c478bd9Sstevel@tonic-gate } else 32737c478bd9Sstevel@tonic-gate netmask.sin_addr = 32747c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 32757c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 32767c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32777c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 32787c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 32797c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32807c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32817c478bd9Sstevel@tonic-gate else 3282fc80c0dfSnordmark Perror0_exit("in_status: SIOCGLIFDSTADDR"); 32837c478bd9Sstevel@tonic-gate } 32847c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 32857c478bd9Sstevel@tonic-gate (void) printf("--> %s ", inet_ntoa(sin->sin_addr)); 32867c478bd9Sstevel@tonic-gate } 32877c478bd9Sstevel@tonic-gate (void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr)); 32887c478bd9Sstevel@tonic-gate if (flags & IFF_BROADCAST) { 32897c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32907c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 32917c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 32927c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32937c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32947c478bd9Sstevel@tonic-gate else 3295fc80c0dfSnordmark Perror0_exit("in_status: SIOCGLIFBRDADDR"); 32967c478bd9Sstevel@tonic-gate } 32977c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 32987c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr != 0) { 32997c478bd9Sstevel@tonic-gate (void) printf("broadcast %s", 33007c478bd9Sstevel@tonic-gate inet_ntoa(sin->sin_addr)); 33017c478bd9Sstevel@tonic-gate } 33027c478bd9Sstevel@tonic-gate } 3303e11c3f44Smeem /* If there is a groupname, print it for only the physical interface */ 33047c478bd9Sstevel@tonic-gate if (strchr(name, ':') == NULL) { 3305e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3306e11c3f44Smeem lifr.lifr_groupname[0] != '\0') { 3307e11c3f44Smeem (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 33087c478bd9Sstevel@tonic-gate } 33097c478bd9Sstevel@tonic-gate } 33107c478bd9Sstevel@tonic-gate (void) putchar('\n'); 33117c478bd9Sstevel@tonic-gate } 33127c478bd9Sstevel@tonic-gate 33137c478bd9Sstevel@tonic-gate static void 33147c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags) 33157c478bd9Sstevel@tonic-gate { 33162b24ab6bSSebastien Roy char abuf[INET6_ADDRSTRLEN]; 33172b24ab6bSSebastien Roy struct sockaddr_in6 *sin6, *laddr6; 33187c478bd9Sstevel@tonic-gate 33197c478bd9Sstevel@tonic-gate if (debug) 33207c478bd9Sstevel@tonic-gate (void) printf("in6_status(%s) flags 0x%llx\n", name, flags); 33217c478bd9Sstevel@tonic-gate 33227c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV6)) 33237c478bd9Sstevel@tonic-gate return; 33247c478bd9Sstevel@tonic-gate 33257c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 33267c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33277c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 33287c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 33297c478bd9Sstevel@tonic-gate errno == ENXIO) { 33307c478bd9Sstevel@tonic-gate if (!force) 33317c478bd9Sstevel@tonic-gate return; 33327c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33337c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33347c478bd9Sstevel@tonic-gate } else 33357c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFADDR"); 33367c478bd9Sstevel@tonic-gate } 33377c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 33387c478bd9Sstevel@tonic-gate (void) printf("\tinet6 %s/%d ", 33397c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33407c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 33417c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 33427c478bd9Sstevel@tonic-gate laddr6 = sin6; 33437c478bd9Sstevel@tonic-gate } else { 33447c478bd9Sstevel@tonic-gate (void) printf("\tinet6 "); 33457c478bd9Sstevel@tonic-gate } 33467c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33477c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 33487c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 33497c478bd9Sstevel@tonic-gate errno == ENXIO) { 33507c478bd9Sstevel@tonic-gate if (!force) 33517c478bd9Sstevel@tonic-gate return; 33527c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33537c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33547c478bd9Sstevel@tonic-gate } else 33557c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFSUBNET"); 33567c478bd9Sstevel@tonic-gate } 33577c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 33587c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 33597c478bd9Sstevel@tonic-gate !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 33607c478bd9Sstevel@tonic-gate (void) printf("subnet %s/%d ", 33617c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33627c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 33637c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 33647c478bd9Sstevel@tonic-gate } 33657c478bd9Sstevel@tonic-gate if (sin6->sin6_family != AF_INET6) { 33667c478bd9Sstevel@tonic-gate (void) printf("Wrong family: %d\n", sin6->sin6_family); 33677c478bd9Sstevel@tonic-gate } 33687c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 33697c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33707c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 33717c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 33727c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33737c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33747c478bd9Sstevel@tonic-gate else 3375fc80c0dfSnordmark Perror0_exit("in_status6: SIOCGLIFDSTADDR"); 33767c478bd9Sstevel@tonic-gate } 33777c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 33787c478bd9Sstevel@tonic-gate (void) printf("--> %s ", 33797c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33807c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 33817c478bd9Sstevel@tonic-gate } 33827c478bd9Sstevel@tonic-gate if (verbose) { 33837c478bd9Sstevel@tonic-gate (void) putchar('\n'); 33847c478bd9Sstevel@tonic-gate (void) putchar('\t'); 33857c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33867c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 33877c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EINVAL) 33887c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33897c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33907c478bd9Sstevel@tonic-gate else 3391fc80c0dfSnordmark Perror0_exit("in_status6: SIOCGLIFTOKEN"); 33927c478bd9Sstevel@tonic-gate } else { 33937c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 33947c478bd9Sstevel@tonic-gate (void) printf("token %s/%d ", 33957c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33967c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 33977c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 33987c478bd9Sstevel@tonic-gate } 33997c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) { 34007c478bd9Sstevel@tonic-gate if (errno != EINVAL) { 34017c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFLNKINFO"); 34027c478bd9Sstevel@tonic-gate } 34037c478bd9Sstevel@tonic-gate } else { 34047c478bd9Sstevel@tonic-gate (void) printf("maxhops %u, reachtime %u ms, " 34057c478bd9Sstevel@tonic-gate "reachretrans %u ms, maxmtu %u ", 34067c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_maxhops, 34077c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_reachtime, 34087c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_reachretrans, 34097c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_maxmtu); 34107c478bd9Sstevel@tonic-gate } 34117c478bd9Sstevel@tonic-gate } 3412f7d61273Smeem /* If there is a groupname, print it for only the physical interface */ 34137c478bd9Sstevel@tonic-gate if (strchr(name, ':') == NULL) { 3414f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3415f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3416f7d61273Smeem (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 34177c478bd9Sstevel@tonic-gate } 34187c478bd9Sstevel@tonic-gate } 34197c478bd9Sstevel@tonic-gate (void) putchar('\n'); 34207c478bd9Sstevel@tonic-gate } 34217c478bd9Sstevel@tonic-gate 34227c478bd9Sstevel@tonic-gate static void 34237c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags) 34247c478bd9Sstevel@tonic-gate { 34257c478bd9Sstevel@tonic-gate struct sockaddr_in *sin, *laddr; 34267c478bd9Sstevel@tonic-gate struct sockaddr_in netmask = { AF_INET }; 34277c478bd9Sstevel@tonic-gate 34287c478bd9Sstevel@tonic-gate if (debug) 34297c478bd9Sstevel@tonic-gate (void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags); 34307c478bd9Sstevel@tonic-gate 34317c478bd9Sstevel@tonic-gate /* only configinfo info for IPv4 interfaces */ 34327c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV4)) 34337c478bd9Sstevel@tonic-gate return; 34347c478bd9Sstevel@tonic-gate 34357c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 34367c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34377c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 34387c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 34397c478bd9Sstevel@tonic-gate errno == ENXIO) { 34407c478bd9Sstevel@tonic-gate if (!force) 34417c478bd9Sstevel@tonic-gate return; 34427c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34437c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34447c478bd9Sstevel@tonic-gate } else 34457c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFADDR"); 34467c478bd9Sstevel@tonic-gate } 34477c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 3448e11c3f44Smeem (void) printf(" set %s ", inet_ntoa(sin->sin_addr)); 34497c478bd9Sstevel@tonic-gate laddr = sin; 34507c478bd9Sstevel@tonic-gate } 34517c478bd9Sstevel@tonic-gate 34527c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34537c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 34547c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 34557c478bd9Sstevel@tonic-gate errno == ENXIO) { 34567c478bd9Sstevel@tonic-gate if (!force) 34577c478bd9Sstevel@tonic-gate return; 34587c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34597c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34607c478bd9Sstevel@tonic-gate } else { 34617c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFSUBNET"); 34627c478bd9Sstevel@tonic-gate } 34637c478bd9Sstevel@tonic-gate } 34647c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 34657c478bd9Sstevel@tonic-gate 34667c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 34677c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 34687c478bd9Sstevel@tonic-gate (void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr), 34697c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 34707c478bd9Sstevel@tonic-gate } 34717c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34727c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 34737c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 34747c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFNETMASK"); 34757c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 34767c478bd9Sstevel@tonic-gate } else 34777c478bd9Sstevel@tonic-gate netmask.sin_addr = 34787c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 34797c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 34807c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 34827c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 34837c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34847c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34857c478bd9Sstevel@tonic-gate else 3486fc80c0dfSnordmark Perror0_exit("in_configinfo: SIOCGLIFDSTADDR"); 34877c478bd9Sstevel@tonic-gate } 34887c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 34897c478bd9Sstevel@tonic-gate (void) printf(" destination %s ", inet_ntoa(sin->sin_addr)); 34907c478bd9Sstevel@tonic-gate } 34917c478bd9Sstevel@tonic-gate (void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr)); 34927c478bd9Sstevel@tonic-gate if (flags & IFF_BROADCAST) { 34937c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34947c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 34957c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 34967c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34977c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34987c478bd9Sstevel@tonic-gate else 3499fc80c0dfSnordmark Perror0_exit("in_configinfo: SIOCGLIFBRDADDR"); 35007c478bd9Sstevel@tonic-gate } 35017c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 35027c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr != 0) { 35037c478bd9Sstevel@tonic-gate (void) printf(" broadcast %s ", 35047c478bd9Sstevel@tonic-gate inet_ntoa(sin->sin_addr)); 35057c478bd9Sstevel@tonic-gate } 35067c478bd9Sstevel@tonic-gate } 35077c478bd9Sstevel@tonic-gate 3508e11c3f44Smeem /* If there is a groupname, print it for only the zeroth interface */ 3509e11c3f44Smeem if (lifnum(name) == 0) { 3510f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3511f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3512f7d61273Smeem (void) printf(" group %s ", lifr.lifr_groupname); 35137c478bd9Sstevel@tonic-gate } 35147c478bd9Sstevel@tonic-gate } 35157c478bd9Sstevel@tonic-gate 35167c478bd9Sstevel@tonic-gate /* Print flags to configure */ 3517e11c3f44Smeem print_config_flags(AF_INET, flags); 35187c478bd9Sstevel@tonic-gate } 35197c478bd9Sstevel@tonic-gate 35207c478bd9Sstevel@tonic-gate static void 35217c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags) 35227c478bd9Sstevel@tonic-gate { 35237c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 35247c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6, *laddr6; 35257c478bd9Sstevel@tonic-gate 35267c478bd9Sstevel@tonic-gate if (debug) 35277c478bd9Sstevel@tonic-gate (void) printf("in6_configinfo(%s) flags 0x%llx\n", name, 35287c478bd9Sstevel@tonic-gate flags); 35297c478bd9Sstevel@tonic-gate 35307c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV6)) 35317c478bd9Sstevel@tonic-gate return; 35327c478bd9Sstevel@tonic-gate 35337c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 35347c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35357c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 35367c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 35377c478bd9Sstevel@tonic-gate errno == ENXIO) { 35387c478bd9Sstevel@tonic-gate if (!force) 35397c478bd9Sstevel@tonic-gate return; 35407c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35417c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35427c478bd9Sstevel@tonic-gate } else 35437c478bd9Sstevel@tonic-gate Perror0_exit("in6_configinfo: SIOCGLIFADDR"); 35447c478bd9Sstevel@tonic-gate } 35457c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3546e11c3f44Smeem (void) printf(" set %s/%d ", 3547e11c3f44Smeem inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)), 3548e11c3f44Smeem lifr.lifr_addrlen); 35497c478bd9Sstevel@tonic-gate laddr6 = sin6; 35507c478bd9Sstevel@tonic-gate } 35517c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35527c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 35537c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 35547c478bd9Sstevel@tonic-gate errno == ENXIO) { 35557c478bd9Sstevel@tonic-gate if (!force) 35567c478bd9Sstevel@tonic-gate return; 35577c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35587c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35597c478bd9Sstevel@tonic-gate } else 35607c478bd9Sstevel@tonic-gate Perror0_exit("in6_configinfo: SIOCGLIFSUBNET"); 35617c478bd9Sstevel@tonic-gate } 35627c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 35637c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 35647c478bd9Sstevel@tonic-gate !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 35657c478bd9Sstevel@tonic-gate (void) printf(" subnet %s/%d ", 35667c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 35677c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 35687c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 35697c478bd9Sstevel@tonic-gate } 35707c478bd9Sstevel@tonic-gate 35717c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 35727c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35737c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 35747c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 35757c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35767c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35777c478bd9Sstevel@tonic-gate else 3578fc80c0dfSnordmark Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR"); 35797c478bd9Sstevel@tonic-gate } 35807c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 35817c478bd9Sstevel@tonic-gate (void) printf(" destination %s ", 35827c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 35837c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 35847c478bd9Sstevel@tonic-gate } 35857c478bd9Sstevel@tonic-gate 35867c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35877c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 35887c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EINVAL) 35897c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35907c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35917c478bd9Sstevel@tonic-gate else 3592fc80c0dfSnordmark Perror0_exit("in6_configinfo: SIOCGLIFTOKEN"); 35937c478bd9Sstevel@tonic-gate } else { 35947c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 35957c478bd9Sstevel@tonic-gate (void) printf(" token %s/%d ", 35967c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 35977c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 35987c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 35997c478bd9Sstevel@tonic-gate } 36007c478bd9Sstevel@tonic-gate 3601e11c3f44Smeem /* If there is a groupname, print it for only the zeroth interface */ 3602e11c3f44Smeem if (lifnum(name) == 0) { 3603f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3604f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3605f7d61273Smeem (void) printf(" group %s ", lifr.lifr_groupname); 36067c478bd9Sstevel@tonic-gate } 36077c478bd9Sstevel@tonic-gate } 36087c478bd9Sstevel@tonic-gate 36097c478bd9Sstevel@tonic-gate /* Print flags to configure */ 3610e11c3f44Smeem print_config_flags(AF_INET6, flags); 36117c478bd9Sstevel@tonic-gate } 36127c478bd9Sstevel@tonic-gate 36137c478bd9Sstevel@tonic-gate /* 36147c478bd9Sstevel@tonic-gate * If this is a physical interface then remove it. 36157c478bd9Sstevel@tonic-gate * If it is a logical interface name use SIOCLIFREMOVEIF to 36167c478bd9Sstevel@tonic-gate * remove it. In both cases fail if it doesn't exist. 36177c478bd9Sstevel@tonic-gate */ 36187c478bd9Sstevel@tonic-gate /* ARGSUSED */ 36197c478bd9Sstevel@tonic-gate static int 36207c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param) 36217c478bd9Sstevel@tonic-gate { 36226e91bba0SGirish Moodalbail ipadm_status_t istatus; 3623e11c3f44Smeem 36246e91bba0SGirish Moodalbail istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 36256e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 36266e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n", 36276e91bba0SGirish Moodalbail name, ipadm_status2str(istatus)); 36286e91bba0SGirish Moodalbail exit(1); 36297c478bd9Sstevel@tonic-gate } 36307c478bd9Sstevel@tonic-gate 36317c478bd9Sstevel@tonic-gate return (0); 36327c478bd9Sstevel@tonic-gate } 36337c478bd9Sstevel@tonic-gate 36347c478bd9Sstevel@tonic-gate /* 36356e91bba0SGirish Moodalbail * Create the interface in `name', using ipadm_create_if(). If `name' is a 36366e91bba0SGirish Moodalbail * logical interface or loopback interface, ipadm_create_if() uses 36376e91bba0SGirish Moodalbail * SIOCLIFADDIF to create it. 36387c478bd9Sstevel@tonic-gate */ 36397c478bd9Sstevel@tonic-gate /* ARGSUSED */ 36407c478bd9Sstevel@tonic-gate static int 36417c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param) 36427c478bd9Sstevel@tonic-gate { 36436e91bba0SGirish Moodalbail ipadm_status_t istatus; 36447c478bd9Sstevel@tonic-gate 36456e91bba0SGirish Moodalbail istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 36466e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 36476e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n", 36486e91bba0SGirish Moodalbail name, ipadm_status2str(istatus)); 36496e91bba0SGirish Moodalbail if (istatus != IPADM_IF_EXISTS) 36506e91bba0SGirish Moodalbail exit(1); 3651f4b3ec61Sdh } 3652e11c3f44Smeem return (0); 3653e11c3f44Smeem } 3654e11c3f44Smeem 3655e11c3f44Smeem /* ARGSUSED */ 3656e11c3f44Smeem static int 3657e11c3f44Smeem inetipmp(char *arg, int64_t param) 3658e11c3f44Smeem { 3659e11c3f44Smeem int retval; 3660e11c3f44Smeem 3661e11c3f44Smeem /* 3662e11c3f44Smeem * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb". 3663e11c3f44Smeem * Otherwise, try to create the requested IPMP interface. 3664e11c3f44Smeem */ 3665e11c3f44Smeem if (strchr(name, ':') != NULL) 3666e11c3f44Smeem retval = inetplumb(arg, param); 3667e11c3f44Smeem else 3668e11c3f44Smeem retval = create_ipmp(name, afp->af_af, name, _B_FALSE); 3669e11c3f44Smeem 3670e11c3f44Smeem /* 3671e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 3672e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 3673e11c3f44Smeem */ 3674e11c3f44Smeem if (retval == -1) 3675e11c3f44Smeem exit(1); 36767c478bd9Sstevel@tonic-gate return (0); 36777c478bd9Sstevel@tonic-gate } 36787c478bd9Sstevel@tonic-gate 3679e11c3f44Smeem /* 3680e11c3f44Smeem * Create an IPMP group `grname' with address family `af'. If `ifname' is 3681e11c3f44Smeem * non-NULL, it specifies the interface name to use. Otherwise, use the name 3682e11c3f44Smeem * ipmpN, where N corresponds to the lowest available integer. If `implicit' 3683e11c3f44Smeem * is set, then the group is being created as a side-effect of placing an 3684e11c3f44Smeem * underlying interface in a group. Also start in.mpathd if necessary. 3685e11c3f44Smeem */ 3686e11c3f44Smeem static int 3687e11c3f44Smeem create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit) 3688e11c3f44Smeem { 3689e11c3f44Smeem static int ipmp_daemon_started; 36906e91bba0SGirish Moodalbail uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE; 36916e91bba0SGirish Moodalbail ipadm_status_t istatus; 3692e11c3f44Smeem 3693e11c3f44Smeem if (debug) { 3694e11c3f44Smeem (void) printf("create_ipmp: ifname %s grname %s af %d\n", 3695e11c3f44Smeem ifname != NULL ? ifname : "NULL", grname, af); 3696e11c3f44Smeem } 3697e11c3f44Smeem 36986e91bba0SGirish Moodalbail /* 36996e91bba0SGirish Moodalbail * ipadm_create_if() creates the IPMP interface and fills in the 37006e91bba0SGirish Moodalbail * ppa in lifr.lifr_name, if `ifname'="ipmp". 37016e91bba0SGirish Moodalbail */ 37026e91bba0SGirish Moodalbail (void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"), 37036e91bba0SGirish Moodalbail sizeof (lifr.lifr_name)); 37046e91bba0SGirish Moodalbail if (ifname == NULL) 37056e91bba0SGirish Moodalbail flags |= IPADM_OPT_GENPPA; 37066e91bba0SGirish Moodalbail istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags); 37076e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 37086e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot create IPMP interface " 37096e91bba0SGirish Moodalbail "%s: %s\n", grname, ipadm_status2str(istatus)); 3710e11c3f44Smeem return (-1); 3711e11c3f44Smeem } 3712e11c3f44Smeem 3713e11c3f44Smeem /* 3714e11c3f44Smeem * To preserve backward-compatibility, always bring up the link-local 3715e11c3f44Smeem * address for implicitly-created IPv6 IPMP interfaces. 3716e11c3f44Smeem */ 3717e11c3f44Smeem if (implicit && af == AF_INET6) { 3718e11c3f44Smeem if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) { 3719e11c3f44Smeem lifr.lifr_flags |= IFF_UP; 3720e11c3f44Smeem (void) ioctl(s6, SIOCSLIFFLAGS, &lifr); 3721e11c3f44Smeem } 3722e11c3f44Smeem } 3723e11c3f44Smeem 3724e11c3f44Smeem /* 3725e11c3f44Smeem * If the caller requested a different group name, issue a 3726e11c3f44Smeem * SIOCSLIFGROUPNAME on the new IPMP interface. 3727e11c3f44Smeem */ 3728e11c3f44Smeem if (strcmp(lifr.lifr_name, grname) != 0) { 3729e11c3f44Smeem (void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ); 3730e11c3f44Smeem if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) { 3731e11c3f44Smeem Perror0("SIOCSLIFGROUPNAME"); 3732e11c3f44Smeem return (-1); 3733e11c3f44Smeem } 3734e11c3f44Smeem } 3735e11c3f44Smeem 3736e11c3f44Smeem /* 3737e11c3f44Smeem * If we haven't done so yet, ensure in.mpathd is started. 3738e11c3f44Smeem */ 3739e11c3f44Smeem if (ipmp_daemon_started++ == 0) 3740e11c3f44Smeem start_ipmp_daemon(); 3741e11c3f44Smeem 3742e11c3f44Smeem return (0); 3743e11c3f44Smeem } 3744e11c3f44Smeem 3745e11c3f44Smeem /* 3746e11c3f44Smeem * Start in.mpathd if it's not already running. 3747e11c3f44Smeem */ 3748e11c3f44Smeem static void 3749e11c3f44Smeem start_ipmp_daemon(void) 3750e11c3f44Smeem { 3751e11c3f44Smeem int retval; 3752e11c3f44Smeem ipmp_handle_t ipmp_handle; 3753e11c3f44Smeem 3754e11c3f44Smeem /* 3755e11c3f44Smeem * Ping in.mpathd to see if it's running already. 3756e11c3f44Smeem */ 3757e11c3f44Smeem if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) { 3758e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot create IPMP handle: " 3759e11c3f44Smeem "%s\n", ipmp_errmsg(retval)); 3760e11c3f44Smeem return; 3761e11c3f44Smeem } 3762e11c3f44Smeem 3763e11c3f44Smeem retval = ipmp_ping_daemon(ipmp_handle); 3764e11c3f44Smeem ipmp_close(ipmp_handle); 3765e11c3f44Smeem 3766e11c3f44Smeem switch (retval) { 3767e11c3f44Smeem case IPMP_ENOMPATHD: 3768e11c3f44Smeem break; 3769e11c3f44Smeem case IPMP_SUCCESS: 3770e11c3f44Smeem return; 3771e11c3f44Smeem default: 3772e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n", 3773e11c3f44Smeem ipmp_errmsg(retval)); 3774e11c3f44Smeem break; 3775e11c3f44Smeem } 3776e11c3f44Smeem 3777e11c3f44Smeem /* 3778e11c3f44Smeem * Start in.mpathd. Note that in.mpathd will handle multiple 3779e11c3f44Smeem * incarnations (ipmp_ping_daemon() is just an optimization) so we 3780e11c3f44Smeem * don't need to worry about racing with another ifconfig process. 3781e11c3f44Smeem */ 3782e11c3f44Smeem switch (fork()) { 3783e11c3f44Smeem case -1: 3784e11c3f44Smeem Perror0_exit("start_ipmp_daemon: fork"); 3785e11c3f44Smeem /* NOTREACHED */ 3786e11c3f44Smeem case 0: 3787e11c3f44Smeem (void) execl(MPATHD_PATH, MPATHD_PATH, NULL); 3788e11c3f44Smeem _exit(1); 3789e11c3f44Smeem /* NOTREACHED */ 3790e11c3f44Smeem default: 3791e11c3f44Smeem break; 3792e11c3f44Smeem } 3793e11c3f44Smeem } 3794e11c3f44Smeem 3795e11c3f44Smeem /* 3796e11c3f44Smeem * Bring the address named by `ifaddrp' up or down. Doesn't trust any mutable 3797e11c3f44Smeem * values in ia_flags since they may be stale. 3798e11c3f44Smeem */ 3799e11c3f44Smeem static boolean_t 3800e11c3f44Smeem ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up) 3801e11c3f44Smeem { 3802e11c3f44Smeem struct lifreq lifrl; /* Local lifreq struct */ 3803e11c3f44Smeem int fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6; 3804e11c3f44Smeem 3805e11c3f44Smeem (void) memset(&lifrl, 0, sizeof (lifrl)); 3806e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ); 3807e11c3f44Smeem if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1) 3808e11c3f44Smeem return (_B_FALSE); 3809e11c3f44Smeem 3810614f1612Smeem if (up) 3811e11c3f44Smeem lifrl.lifr_flags |= IFF_UP; 3812614f1612Smeem else 3813e11c3f44Smeem lifrl.lifr_flags &= ~IFF_UP; 3814614f1612Smeem 3815614f1612Smeem if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1) 3816614f1612Smeem return (_B_FALSE); 3817614f1612Smeem 3818614f1612Smeem /* 3819614f1612Smeem * If we're trying to bring the address down, ensure that DAD activity 3820614f1612Smeem * (observable by IFF_DUPLICATE) has also been stopped. 3821614f1612Smeem */ 3822614f1612Smeem if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 && 3823614f1612Smeem lifrl.lifr_flags & IFF_DUPLICATE) { 3824614f1612Smeem if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 || 3825614f1612Smeem ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) { 3826614f1612Smeem return (_B_FALSE); 3827614f1612Smeem } 3828e11c3f44Smeem } 3829614f1612Smeem return (_B_TRUE); 3830e11c3f44Smeem } 3831e11c3f44Smeem 3832e11c3f44Smeem static boolean_t 3833e11c3f44Smeem ifaddr_up(ifaddrlistx_t *ifaddrp) 3834e11c3f44Smeem { 3835e11c3f44Smeem return (ifaddr_op(ifaddrp, _B_TRUE)); 3836e11c3f44Smeem } 3837e11c3f44Smeem 3838e11c3f44Smeem static boolean_t 3839e11c3f44Smeem ifaddr_down(ifaddrlistx_t *ifaddrp) 3840e11c3f44Smeem { 3841e11c3f44Smeem return (ifaddr_op(ifaddrp, _B_FALSE)); 3842e11c3f44Smeem } 3843e11c3f44Smeem 38442b24ab6bSSebastien Roy /* 38452b24ab6bSSebastien Roy * Open the global libdladm handle "dlh" if it isn't already opened. The 38462b24ab6bSSebastien Roy * caller may optionally supply a link name to obtain its linkid. If a link 38472b24ab6bSSebastien Roy * of a specific class or classes is required, reqclass specifies the class 38482b24ab6bSSebastien Roy * mask. 38492b24ab6bSSebastien Roy */ 38502b24ab6bSSebastien Roy static dladm_status_t 38512b24ab6bSSebastien Roy ifconfig_dladm_open(const char *name, datalink_class_t reqclass, 38522b24ab6bSSebastien Roy datalink_id_t *linkid) 38532b24ab6bSSebastien Roy { 38542b24ab6bSSebastien Roy dladm_status_t status = DLADM_STATUS_OK; 38552b24ab6bSSebastien Roy datalink_class_t class; 38562b24ab6bSSebastien Roy 38572b24ab6bSSebastien Roy if (!dlh_opened) { 38582b24ab6bSSebastien Roy if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK) 38592b24ab6bSSebastien Roy return (status); 38602b24ab6bSSebastien Roy dlh_opened = _B_TRUE; 38612b24ab6bSSebastien Roy } 38622b24ab6bSSebastien Roy if (name != NULL) { 38632b24ab6bSSebastien Roy status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL); 38642b24ab6bSSebastien Roy if (status == DLADM_STATUS_OK) { 38652b24ab6bSSebastien Roy if (!(class & reqclass)) 38662b24ab6bSSebastien Roy status = DLADM_STATUS_LINKINVAL; 38672b24ab6bSSebastien Roy } 38682b24ab6bSSebastien Roy } 38692b24ab6bSSebastien Roy return (status); 38702b24ab6bSSebastien Roy } 38712b24ab6bSSebastien Roy 38726e91bba0SGirish Moodalbail /* 38736e91bba0SGirish Moodalbail * This function checks if we can use libipadm API's. We will only 38746e91bba0SGirish Moodalbail * call libipadm functions for non-IPMP interfaces. This check is 38756e91bba0SGirish Moodalbail * temporary until libipadm supports IPMP interfaces. 38766e91bba0SGirish Moodalbail */ 38776e91bba0SGirish Moodalbail static boolean_t 38786e91bba0SGirish Moodalbail ifconfig_use_libipadm(int s, const char *lifname) 38796e91bba0SGirish Moodalbail { 38806e91bba0SGirish Moodalbail struct lifreq lifr1; 38816e91bba0SGirish Moodalbail 38826e91bba0SGirish Moodalbail (void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name)); 38836e91bba0SGirish Moodalbail if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) { 38846e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, lifname, 38856e91bba0SGirish Moodalbail sizeof (lifr.lifr_name)); 38866e91bba0SGirish Moodalbail Perror0_exit("error"); 38876e91bba0SGirish Moodalbail } 38886e91bba0SGirish Moodalbail 38896e91bba0SGirish Moodalbail return (lifr1.lifr_groupname[0] == '\0'); 38906e91bba0SGirish Moodalbail } 38916e91bba0SGirish Moodalbail 38926e91bba0SGirish Moodalbail static void 38936e91bba0SGirish Moodalbail ipadmerr_exit(ipadm_status_t status, const char *str) 38946e91bba0SGirish Moodalbail { 38956e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: %s: %s\n", str, 38966e91bba0SGirish Moodalbail ipadm_status2str(status)); 38976e91bba0SGirish Moodalbail exit(1); 38986e91bba0SGirish Moodalbail } 38996e91bba0SGirish Moodalbail 39006e91bba0SGirish Moodalbail static void 39012b24ab6bSSebastien Roy dladmerr_exit(dladm_status_t status, const char *str) 39022b24ab6bSSebastien Roy { 39032b24ab6bSSebastien Roy char errstr[DLADM_STRSIZE]; 39042b24ab6bSSebastien Roy 39052b24ab6bSSebastien Roy (void) fprintf(stderr, "%s: %s\n", str, 39062b24ab6bSSebastien Roy dladm_status2str(status, errstr)); 39072b24ab6bSSebastien Roy exit(1); 39082b24ab6bSSebastien Roy } 39092b24ab6bSSebastien Roy 39107c478bd9Sstevel@tonic-gate void 3911f7d61273Smeem Perror0(const char *cmd) 39127c478bd9Sstevel@tonic-gate { 3913f7d61273Smeem Perror2(cmd, lifr.lifr_name); 39147c478bd9Sstevel@tonic-gate } 39157c478bd9Sstevel@tonic-gate 39167c478bd9Sstevel@tonic-gate void 3917f7d61273Smeem Perror0_exit(const char *cmd) 39187c478bd9Sstevel@tonic-gate { 39197c478bd9Sstevel@tonic-gate Perror0(cmd); 39207c478bd9Sstevel@tonic-gate exit(1); 39217c478bd9Sstevel@tonic-gate } 39227c478bd9Sstevel@tonic-gate 39237c478bd9Sstevel@tonic-gate void 3924f7d61273Smeem Perror2(const char *cmd, const char *str) 39257c478bd9Sstevel@tonic-gate { 3926f7d61273Smeem int error = errno; 39277c478bd9Sstevel@tonic-gate 3928f7d61273Smeem (void) fprintf(stderr, "ifconfig: %s: ", cmd); 39297c478bd9Sstevel@tonic-gate 3930f7d61273Smeem switch (error) { 39317c478bd9Sstevel@tonic-gate case ENXIO: 3932f7d61273Smeem (void) fprintf(stderr, "%s: no such interface\n", str); 39337c478bd9Sstevel@tonic-gate break; 39347c478bd9Sstevel@tonic-gate case EPERM: 3935f7d61273Smeem (void) fprintf(stderr, "%s: permission denied\n", str); 39367c478bd9Sstevel@tonic-gate break; 3937f7d61273Smeem case EEXIST: 3938f7d61273Smeem (void) fprintf(stderr, "%s: already exists\n", str); 3939f7d61273Smeem break; 3940e899e593SGirish Moodalbail case ENAMETOOLONG: 3941e899e593SGirish Moodalbail (void) fprintf(stderr, "%s: interface name too long\n", str); 3942e899e593SGirish Moodalbail break; 3943e899e593SGirish Moodalbail case ERANGE: 3944e899e593SGirish Moodalbail (void) fprintf(stderr, "%s: logical interface id is outside " 3945e899e593SGirish Moodalbail "allowed range\n", str); 3946e899e593SGirish Moodalbail break; 3947f7d61273Smeem default: 3948f7d61273Smeem errno = error; 3949f7d61273Smeem perror(str); 39507c478bd9Sstevel@tonic-gate } 39517c478bd9Sstevel@tonic-gate } 39527c478bd9Sstevel@tonic-gate 39537c478bd9Sstevel@tonic-gate /* 39547c478bd9Sstevel@tonic-gate * Print out error message (Perror2()) and exit 39557c478bd9Sstevel@tonic-gate */ 39567c478bd9Sstevel@tonic-gate void 3957f7d61273Smeem Perror2_exit(const char *cmd, const char *str) 39587c478bd9Sstevel@tonic-gate { 39597c478bd9Sstevel@tonic-gate Perror2(cmd, str); 39607c478bd9Sstevel@tonic-gate exit(1); 39617c478bd9Sstevel@tonic-gate /* NOTREACHED */ 39627c478bd9Sstevel@tonic-gate } 39637c478bd9Sstevel@tonic-gate 3964c7e4935fSss void 3965c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err) 3966c7e4935fSss { 3967c7e4935fSss (void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd, 3968c7e4935fSss linkname, dlpi_strerror(err)); 3969c7e4935fSss } 3970c7e4935fSss 3971c7e4935fSss /* 3972c7e4935fSss * Print out error message (Perrdlpi()) and exit 3973c7e4935fSss */ 3974c7e4935fSss void 3975c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err) 3976c7e4935fSss { 3977c7e4935fSss Perrdlpi(cmd, linkname, err); 3978c7e4935fSss exit(1); 3979c7e4935fSss } 3980c7e4935fSss 39817c478bd9Sstevel@tonic-gate /* 39827c478bd9Sstevel@tonic-gate * If the last argument is non-NULL allow a <addr>/<n> syntax and 39837c478bd9Sstevel@tonic-gate * pass out <n> in *plenp. 39847c478bd9Sstevel@tonic-gate * If <n> doesn't parse return BAD_ADDR as *plenp. 39857c478bd9Sstevel@tonic-gate * If no /<n> is present return NO_PREFIX as *plenp. 39867c478bd9Sstevel@tonic-gate */ 39877c478bd9Sstevel@tonic-gate static void 39887c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp) 39897c478bd9Sstevel@tonic-gate { 399069bb4bb4Scarlsonj /* LINTED: alignment */ 39917c478bd9Sstevel@tonic-gate struct sockaddr_in *sin = (struct sockaddr_in *)saddr; 39927c478bd9Sstevel@tonic-gate struct hostent *hp; 39937c478bd9Sstevel@tonic-gate struct netent *np; 39947c478bd9Sstevel@tonic-gate char str[BUFSIZ]; 39957c478bd9Sstevel@tonic-gate int error_num; 39967c478bd9Sstevel@tonic-gate 39977c478bd9Sstevel@tonic-gate (void) strncpy(str, s, sizeof (str)); 39987c478bd9Sstevel@tonic-gate 39997c478bd9Sstevel@tonic-gate /* 40007c478bd9Sstevel@tonic-gate * Look for '/'<n> is plenp 40017c478bd9Sstevel@tonic-gate */ 40027c478bd9Sstevel@tonic-gate if (plenp != NULL) { 40037c478bd9Sstevel@tonic-gate char *cp; 40047c478bd9Sstevel@tonic-gate 40057906a3e0Smeem *plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS); 40067c478bd9Sstevel@tonic-gate if (*plenp == BAD_ADDR) 40077c478bd9Sstevel@tonic-gate return; 40087c478bd9Sstevel@tonic-gate cp = strchr(str, '/'); 40097c478bd9Sstevel@tonic-gate if (cp != NULL) 40107c478bd9Sstevel@tonic-gate *cp = '\0'; 40117c478bd9Sstevel@tonic-gate } else if (strchr(str, '/') != NULL) { 40127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 40137c478bd9Sstevel@tonic-gate exit(1); 40147c478bd9Sstevel@tonic-gate } 40157c478bd9Sstevel@tonic-gate 40167c478bd9Sstevel@tonic-gate (void) memset(sin, 0, sizeof (*sin)); 40177c478bd9Sstevel@tonic-gate 40187c478bd9Sstevel@tonic-gate /* 40197c478bd9Sstevel@tonic-gate * Try to catch attempts to set the broadcast address to all 1's. 40207c478bd9Sstevel@tonic-gate */ 40217c478bd9Sstevel@tonic-gate if (strcmp(str, "255.255.255.255") == 0 || 40227c478bd9Sstevel@tonic-gate (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) { 40237c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET; 40247c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr = 0xffffffff; 40257c478bd9Sstevel@tonic-gate return; 40267c478bd9Sstevel@tonic-gate } 40277c478bd9Sstevel@tonic-gate 40287c478bd9Sstevel@tonic-gate hp = getipnodebyname(str, AF_INET, 0, &error_num); 40297c478bd9Sstevel@tonic-gate if (hp) { 40307c478bd9Sstevel@tonic-gate sin->sin_family = hp->h_addrtype; 40317c478bd9Sstevel@tonic-gate (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); 40327c478bd9Sstevel@tonic-gate freehostent(hp); 40337c478bd9Sstevel@tonic-gate return; 40347c478bd9Sstevel@tonic-gate } 40357c478bd9Sstevel@tonic-gate np = getnetbyname(str); 40367c478bd9Sstevel@tonic-gate if (np) { 40377c478bd9Sstevel@tonic-gate sin->sin_family = np->n_addrtype; 40387c478bd9Sstevel@tonic-gate sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 40397c478bd9Sstevel@tonic-gate return; 40407c478bd9Sstevel@tonic-gate } 40417c478bd9Sstevel@tonic-gate if (error_num == TRY_AGAIN) { 40427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address " 40437c478bd9Sstevel@tonic-gate "(try again later)\n", s); 40447c478bd9Sstevel@tonic-gate } else { 40457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 40467c478bd9Sstevel@tonic-gate } 40477c478bd9Sstevel@tonic-gate exit(1); 40487c478bd9Sstevel@tonic-gate } 40497c478bd9Sstevel@tonic-gate 40507c478bd9Sstevel@tonic-gate /* 40517c478bd9Sstevel@tonic-gate * If the last argument is non-NULL allow a <addr>/<n> syntax and 40527c478bd9Sstevel@tonic-gate * pass out <n> in *plenp. 40537c478bd9Sstevel@tonic-gate * If <n> doesn't parse return BAD_ADDR as *plenp. 40547c478bd9Sstevel@tonic-gate * If no /<n> is present return NO_PREFIX as *plenp. 40557c478bd9Sstevel@tonic-gate */ 40567c478bd9Sstevel@tonic-gate static void 40577c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp) 40587c478bd9Sstevel@tonic-gate { 405969bb4bb4Scarlsonj /* LINTED: alignment */ 40607c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr; 40617c478bd9Sstevel@tonic-gate struct hostent *hp; 40627c478bd9Sstevel@tonic-gate char str[BUFSIZ]; 40637c478bd9Sstevel@tonic-gate int error_num; 40647c478bd9Sstevel@tonic-gate 40657c478bd9Sstevel@tonic-gate (void) strncpy(str, s, sizeof (str)); 40667c478bd9Sstevel@tonic-gate 40677c478bd9Sstevel@tonic-gate /* 40687c478bd9Sstevel@tonic-gate * Look for '/'<n> is plenp 40697c478bd9Sstevel@tonic-gate */ 40707c478bd9Sstevel@tonic-gate if (plenp != NULL) { 40717c478bd9Sstevel@tonic-gate char *cp; 40727c478bd9Sstevel@tonic-gate 40737906a3e0Smeem *plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS); 40747c478bd9Sstevel@tonic-gate if (*plenp == BAD_ADDR) 40757c478bd9Sstevel@tonic-gate return; 40767c478bd9Sstevel@tonic-gate cp = strchr(str, '/'); 40777c478bd9Sstevel@tonic-gate if (cp != NULL) 40787c478bd9Sstevel@tonic-gate *cp = '\0'; 40797c478bd9Sstevel@tonic-gate } else if (strchr(str, '/') != NULL) { 40807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 40817c478bd9Sstevel@tonic-gate exit(1); 40827c478bd9Sstevel@tonic-gate } 40837c478bd9Sstevel@tonic-gate 40847c478bd9Sstevel@tonic-gate (void) memset(sin6, 0, sizeof (*sin6)); 40857c478bd9Sstevel@tonic-gate 40867c478bd9Sstevel@tonic-gate hp = getipnodebyname(str, AF_INET6, 0, &error_num); 40877c478bd9Sstevel@tonic-gate if (hp) { 40887c478bd9Sstevel@tonic-gate sin6->sin6_family = hp->h_addrtype; 40897c478bd9Sstevel@tonic-gate (void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length); 40907c478bd9Sstevel@tonic-gate freehostent(hp); 40917c478bd9Sstevel@tonic-gate return; 40927c478bd9Sstevel@tonic-gate } 40937c478bd9Sstevel@tonic-gate if (error_num == TRY_AGAIN) { 40947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address " 40957c478bd9Sstevel@tonic-gate "(try again later)\n", s); 40967c478bd9Sstevel@tonic-gate } else { 40977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 40987c478bd9Sstevel@tonic-gate } 40997c478bd9Sstevel@tonic-gate exit(1); 41007c478bd9Sstevel@tonic-gate } 41017c478bd9Sstevel@tonic-gate 41027c478bd9Sstevel@tonic-gate /* 41037c478bd9Sstevel@tonic-gate * If "slash" is zero this parses the whole string as 41047c478bd9Sstevel@tonic-gate * an integer. With "slash" non zero it parses the tail part as an integer. 41057c478bd9Sstevel@tonic-gate * 41067c478bd9Sstevel@tonic-gate * If it is not a valid integer this returns BAD_ADDR. 41077c478bd9Sstevel@tonic-gate * If there is /<n> present this returns NO_PREFIX. 41087c478bd9Sstevel@tonic-gate */ 41097c478bd9Sstevel@tonic-gate static int 41107c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen) 41117c478bd9Sstevel@tonic-gate { 41127c478bd9Sstevel@tonic-gate int prefixlen; 41137c478bd9Sstevel@tonic-gate char *str, *end; 41147c478bd9Sstevel@tonic-gate 41157c478bd9Sstevel@tonic-gate if (slash) { 41167c478bd9Sstevel@tonic-gate str = strchr(addr, '/'); 41177c478bd9Sstevel@tonic-gate if (str == NULL) 41187c478bd9Sstevel@tonic-gate return (NO_PREFIX); 41197c478bd9Sstevel@tonic-gate str++; 41207c478bd9Sstevel@tonic-gate } else 41217c478bd9Sstevel@tonic-gate str = addr; 41227c478bd9Sstevel@tonic-gate 41237c478bd9Sstevel@tonic-gate prefixlen = strtol(str, &end, 10); 41247c478bd9Sstevel@tonic-gate if (prefixlen < 0) 41257c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41267c478bd9Sstevel@tonic-gate if (str == end) 41277c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41287c478bd9Sstevel@tonic-gate if (max_plen != 0 && max_plen < prefixlen) 41297c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41307c478bd9Sstevel@tonic-gate return (prefixlen); 41317c478bd9Sstevel@tonic-gate } 41327c478bd9Sstevel@tonic-gate 41337c478bd9Sstevel@tonic-gate /* 41347c478bd9Sstevel@tonic-gate * Convert a prefix length to a mask. 41357c478bd9Sstevel@tonic-gate * Returns 1 if ok. 0 otherwise. 41367c478bd9Sstevel@tonic-gate * Assumes the mask array is zero'ed by the caller. 41377c478bd9Sstevel@tonic-gate */ 41387c478bd9Sstevel@tonic-gate static boolean_t 41397c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask) 41407c478bd9Sstevel@tonic-gate { 41417c478bd9Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxlen) 41427c478bd9Sstevel@tonic-gate return (0); 41437c478bd9Sstevel@tonic-gate 41447c478bd9Sstevel@tonic-gate while (prefixlen > 0) { 41457c478bd9Sstevel@tonic-gate if (prefixlen >= 8) { 41467c478bd9Sstevel@tonic-gate *mask++ = 0xFF; 41477c478bd9Sstevel@tonic-gate prefixlen -= 8; 41487c478bd9Sstevel@tonic-gate continue; 41497c478bd9Sstevel@tonic-gate } 41507c478bd9Sstevel@tonic-gate *mask |= 1 << (8 - prefixlen); 41517c478bd9Sstevel@tonic-gate prefixlen--; 41527c478bd9Sstevel@tonic-gate } 41537c478bd9Sstevel@tonic-gate return (1); 41547c478bd9Sstevel@tonic-gate } 41557c478bd9Sstevel@tonic-gate 41567c478bd9Sstevel@tonic-gate static void 41577c478bd9Sstevel@tonic-gate print_flags(uint64_t flags) 41587c478bd9Sstevel@tonic-gate { 41597c478bd9Sstevel@tonic-gate boolean_t first = _B_TRUE; 41607c478bd9Sstevel@tonic-gate int cnt, i; 41617c478bd9Sstevel@tonic-gate 41627c478bd9Sstevel@tonic-gate (void) printf("flags=%llx", flags); 41637c478bd9Sstevel@tonic-gate cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t); 41647c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i++) { 41657c478bd9Sstevel@tonic-gate if (flags & if_flags_tbl[i].iff_value) { 41667c478bd9Sstevel@tonic-gate if (first) { 41677c478bd9Sstevel@tonic-gate (void) printf("<"); 41687c478bd9Sstevel@tonic-gate first = _B_FALSE; 41697c478bd9Sstevel@tonic-gate } else { 41707c478bd9Sstevel@tonic-gate /* 41717c478bd9Sstevel@tonic-gate * It has to be here and not with the 41727c478bd9Sstevel@tonic-gate * printf below because for the last one, 41737c478bd9Sstevel@tonic-gate * we don't want a comma before the ">". 41747c478bd9Sstevel@tonic-gate */ 41757c478bd9Sstevel@tonic-gate (void) printf(","); 41767c478bd9Sstevel@tonic-gate } 41777c478bd9Sstevel@tonic-gate (void) printf("%s", if_flags_tbl[i].iff_name); 41787c478bd9Sstevel@tonic-gate } 41797c478bd9Sstevel@tonic-gate } 41807c478bd9Sstevel@tonic-gate if (!first) 41817c478bd9Sstevel@tonic-gate (void) printf(">"); 41827c478bd9Sstevel@tonic-gate } 41837c478bd9Sstevel@tonic-gate 41847c478bd9Sstevel@tonic-gate static void 4185e11c3f44Smeem print_config_flags(int af, uint64_t flags) 41867c478bd9Sstevel@tonic-gate { 4187e11c3f44Smeem if_config_cmd_t *cmdp; 41887c478bd9Sstevel@tonic-gate 4189e11c3f44Smeem for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) { 4190e11c3f44Smeem if ((flags & cmdp->iff_flag) && 4191e11c3f44Smeem (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) { 4192e11c3f44Smeem (void) printf("%s ", cmdp->iff_name); 41937c478bd9Sstevel@tonic-gate } 41947c478bd9Sstevel@tonic-gate } 41957c478bd9Sstevel@tonic-gate } 41967c478bd9Sstevel@tonic-gate 41977c478bd9Sstevel@tonic-gate /* 419836e852a1SRaja Andra * Use the configured directory lookup mechanism (e.g. files/NIS/...) 4199dd7a6f5fSkcpoon * to find the network mask. Returns true if we found one to set. 4200dd7a6f5fSkcpoon * 4201dd7a6f5fSkcpoon * The parameter addr_set controls whether we should get the address of 4202dd7a6f5fSkcpoon * the working interface for the netmask query. If addr_set is true, 4203dd7a6f5fSkcpoon * we will use the address provided. Otherwise, we will find the working 4204dd7a6f5fSkcpoon * interface's address and use it instead. 42057c478bd9Sstevel@tonic-gate */ 42067c478bd9Sstevel@tonic-gate static boolean_t 4207dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set) 42087c478bd9Sstevel@tonic-gate { 42097c478bd9Sstevel@tonic-gate struct sockaddr_in ifaddr; 42107c478bd9Sstevel@tonic-gate 42117c478bd9Sstevel@tonic-gate /* 4212dd7a6f5fSkcpoon * Read the address from the interface if it is not passed in. 42137c478bd9Sstevel@tonic-gate */ 4214dd7a6f5fSkcpoon if (!addr_set) { 4215dd7a6f5fSkcpoon (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 4216dd7a6f5fSkcpoon if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 4217dd7a6f5fSkcpoon if (errno != EADDRNOTAVAIL) { 4218dd7a6f5fSkcpoon (void) fprintf(stderr, "Need net number for " 4219dd7a6f5fSkcpoon "mask\n"); 4220dd7a6f5fSkcpoon } 4221dd7a6f5fSkcpoon return (_B_FALSE); 4222dd7a6f5fSkcpoon } 4223dd7a6f5fSkcpoon ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr); 4224dd7a6f5fSkcpoon } else { 4225dd7a6f5fSkcpoon ifaddr.sin_addr = saddr->sin_addr; 42267c478bd9Sstevel@tonic-gate } 42277c478bd9Sstevel@tonic-gate if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) { 42287c478bd9Sstevel@tonic-gate saddr->sin_family = AF_INET; 42297c478bd9Sstevel@tonic-gate return (_B_TRUE); 42307c478bd9Sstevel@tonic-gate } 42317c478bd9Sstevel@tonic-gate return (_B_FALSE); 42327c478bd9Sstevel@tonic-gate } 42337c478bd9Sstevel@tonic-gate 42347c478bd9Sstevel@tonic-gate static int 4235e11c3f44Smeem lifnum(const char *ifname) 4236e11c3f44Smeem { 4237e11c3f44Smeem const char *cp; 4238e11c3f44Smeem 4239e11c3f44Smeem if ((cp = strchr(ifname, ':')) == NULL) 4240e11c3f44Smeem return (0); 4241e11c3f44Smeem else 4242e11c3f44Smeem return (atoi(cp + 1)); 4243e11c3f44Smeem } 4244e11c3f44Smeem 42457c478bd9Sstevel@tonic-gate static void 4246d62bc4baSyz add_ni(const char *name) 42477c478bd9Sstevel@tonic-gate { 42487c478bd9Sstevel@tonic-gate ni_t **pp; 42497c478bd9Sstevel@tonic-gate ni_t *p; 42507c478bd9Sstevel@tonic-gate 42517c478bd9Sstevel@tonic-gate for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) { 42527c478bd9Sstevel@tonic-gate if (strcmp(p->ni_name, name) == 0) { 42537c478bd9Sstevel@tonic-gate if (debug > 2) 42547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "'%s' is a duplicate\n", 42557c478bd9Sstevel@tonic-gate name); 42567c478bd9Sstevel@tonic-gate return; 42577c478bd9Sstevel@tonic-gate } 42587c478bd9Sstevel@tonic-gate } 42597c478bd9Sstevel@tonic-gate 42607c478bd9Sstevel@tonic-gate if (debug > 2) 42617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "adding '%s'\n", 42627c478bd9Sstevel@tonic-gate name); 42637c478bd9Sstevel@tonic-gate 42647c478bd9Sstevel@tonic-gate if ((p = malloc(sizeof (ni_t))) == NULL) 42657c478bd9Sstevel@tonic-gate return; 42667c478bd9Sstevel@tonic-gate 42677c478bd9Sstevel@tonic-gate (void) strlcpy(p->ni_name, name, sizeof (p->ni_name)); 42687c478bd9Sstevel@tonic-gate p->ni_next = NULL; 42697c478bd9Sstevel@tonic-gate 42707c478bd9Sstevel@tonic-gate *pp = p; 42717c478bd9Sstevel@tonic-gate num_ni++; 42727c478bd9Sstevel@tonic-gate } 42737c478bd9Sstevel@tonic-gate 4274d62bc4baSyz static boolean_t 4275d62bc4baSyz ni_entry(const char *linkname, void *arg) 42767c478bd9Sstevel@tonic-gate { 4277c7e4935fSss dlpi_handle_t dh; 4278da14cebeSEric Cheng datalink_class_t class; 42797c478bd9Sstevel@tonic-gate 4280d4d1f7bfSVasumathi Sundaram - Sun Microsystems (void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL); 42814ac67f02SAnurag S. Maskey 4282da14cebeSEric Cheng if (class == DATALINK_CLASS_ETHERSTUB) 4283da14cebeSEric Cheng return (_B_FALSE); 4284c7e4935fSss if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS) 4285d62bc4baSyz return (_B_FALSE); 42867c478bd9Sstevel@tonic-gate 4287d62bc4baSyz add_ni(linkname); 42887c478bd9Sstevel@tonic-gate 4289c7e4935fSss dlpi_close(dh); 4290d62bc4baSyz return (_B_FALSE); 42917c478bd9Sstevel@tonic-gate } 42927c478bd9Sstevel@tonic-gate 42937c478bd9Sstevel@tonic-gate /* 42947c478bd9Sstevel@tonic-gate * dhcp-related routines 42957c478bd9Sstevel@tonic-gate */ 42967c478bd9Sstevel@tonic-gate 42977c478bd9Sstevel@tonic-gate static int 42987c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[]) 42997c478bd9Sstevel@tonic-gate { 43007c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request; 43017c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply = NULL; 43027c478bd9Sstevel@tonic-gate int timeout = DHCP_IPC_WAIT_DEFAULT; 43037c478bd9Sstevel@tonic-gate dhcp_ipc_type_t type = DHCP_START; 43047c478bd9Sstevel@tonic-gate int error; 43057c478bd9Sstevel@tonic-gate boolean_t is_primary = _B_FALSE; 43067c478bd9Sstevel@tonic-gate boolean_t started = _B_FALSE; 43077c478bd9Sstevel@tonic-gate 43087c478bd9Sstevel@tonic-gate for (argv++; --argc > 0; argv++) { 43097c478bd9Sstevel@tonic-gate 43107c478bd9Sstevel@tonic-gate if (strcmp(*argv, "primary") == 0) { 43117c478bd9Sstevel@tonic-gate is_primary = _B_TRUE; 43127c478bd9Sstevel@tonic-gate continue; 43137c478bd9Sstevel@tonic-gate } 43147c478bd9Sstevel@tonic-gate 43157c478bd9Sstevel@tonic-gate if (strcmp(*argv, "wait") == 0) { 43167c478bd9Sstevel@tonic-gate if (--argc <= 0) { 43177c478bd9Sstevel@tonic-gate usage(); 43187c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43197c478bd9Sstevel@tonic-gate } 43207c478bd9Sstevel@tonic-gate argv++; 43217c478bd9Sstevel@tonic-gate 43227c478bd9Sstevel@tonic-gate if (strcmp(*argv, "forever") == 0) { 43237c478bd9Sstevel@tonic-gate timeout = DHCP_IPC_WAIT_FOREVER; 43247c478bd9Sstevel@tonic-gate continue; 43257c478bd9Sstevel@tonic-gate } 43267c478bd9Sstevel@tonic-gate 43277c478bd9Sstevel@tonic-gate if (sscanf(*argv, "%d", &timeout) != 1) { 43287c478bd9Sstevel@tonic-gate usage(); 43297c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43307c478bd9Sstevel@tonic-gate } 43317c478bd9Sstevel@tonic-gate 43327c478bd9Sstevel@tonic-gate if (timeout < 0) { 43337c478bd9Sstevel@tonic-gate usage(); 43347c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43357c478bd9Sstevel@tonic-gate } 43367c478bd9Sstevel@tonic-gate continue; 43377c478bd9Sstevel@tonic-gate } 43387c478bd9Sstevel@tonic-gate 43397c478bd9Sstevel@tonic-gate type = dhcp_string_to_request(*argv); 43407c478bd9Sstevel@tonic-gate if (type == -1) { 43417c478bd9Sstevel@tonic-gate usage(); 43427c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43437c478bd9Sstevel@tonic-gate } 43447c478bd9Sstevel@tonic-gate } 43457c478bd9Sstevel@tonic-gate 43467c478bd9Sstevel@tonic-gate /* 43477c478bd9Sstevel@tonic-gate * Only try to start agent on start or inform; in all other cases it 43487c478bd9Sstevel@tonic-gate * has to already be running for anything to make sense. 43497c478bd9Sstevel@tonic-gate */ 43507c478bd9Sstevel@tonic-gate if (type == DHCP_START || type == DHCP_INFORM) { 43517c478bd9Sstevel@tonic-gate if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) { 43527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unable to start %s\n", 43537c478bd9Sstevel@tonic-gate caller, DHCP_AGENT_PATH); 43547c478bd9Sstevel@tonic-gate return (DHCP_EXIT_FAILURE); 43557c478bd9Sstevel@tonic-gate } 43567c478bd9Sstevel@tonic-gate started = _B_TRUE; 43577c478bd9Sstevel@tonic-gate } 43587c478bd9Sstevel@tonic-gate 43597c478bd9Sstevel@tonic-gate if (is_primary) 4360d04ccbb3Scarlsonj type |= DHCP_PRIMARY; 4361d04ccbb3Scarlsonj 4362d04ccbb3Scarlsonj if (af != AF_INET) 4363d04ccbb3Scarlsonj type |= DHCP_V6; 43647c478bd9Sstevel@tonic-gate 43657c478bd9Sstevel@tonic-gate request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE); 43667c478bd9Sstevel@tonic-gate if (request == NULL) { 43677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: out of memory\n", caller); 43687c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SYSTEM); 43697c478bd9Sstevel@tonic-gate } 43707c478bd9Sstevel@tonic-gate 43717c478bd9Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, timeout); 43727c478bd9Sstevel@tonic-gate if (error != 0) { 43737c478bd9Sstevel@tonic-gate free(request); 43747c478bd9Sstevel@tonic-gate /* 43757c478bd9Sstevel@tonic-gate * Re-map connect error to not under control if we didn't try a 43767c478bd9Sstevel@tonic-gate * start operation, as this has to be true and results in a 43777c478bd9Sstevel@tonic-gate * clearer message, not to mention preserving compatibility 43787c478bd9Sstevel@tonic-gate * with the days when we always started dhcpagent for every 43797c478bd9Sstevel@tonic-gate * request. 43807c478bd9Sstevel@tonic-gate */ 43817c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_CONNECT && !started) 43827c478bd9Sstevel@tonic-gate error = DHCP_IPC_E_UNKIF; 43837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 43847c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(error)); 43857c478bd9Sstevel@tonic-gate return (DHCP_EXIT_FAILURE); 43867c478bd9Sstevel@tonic-gate } 43877c478bd9Sstevel@tonic-gate 43887c478bd9Sstevel@tonic-gate error = reply->return_code; 43897c478bd9Sstevel@tonic-gate if (error != 0) { 43907c478bd9Sstevel@tonic-gate free(request); 43917c478bd9Sstevel@tonic-gate free(reply); 43927c478bd9Sstevel@tonic-gate 43937c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_TIMEOUT && timeout == 0) 43947c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS); 43957c478bd9Sstevel@tonic-gate 43967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 43977c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(error)); 43987c478bd9Sstevel@tonic-gate 43997c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_TIMEOUT) 44007c478bd9Sstevel@tonic-gate return (DHCP_EXIT_TIMEOUT); 44017c478bd9Sstevel@tonic-gate else 44027c478bd9Sstevel@tonic-gate return (DHCP_EXIT_IF_FAILURE); 44037c478bd9Sstevel@tonic-gate } 44047c478bd9Sstevel@tonic-gate 44057c478bd9Sstevel@tonic-gate if (DHCP_IPC_CMD(type) == DHCP_STATUS) { 44067c478bd9Sstevel@tonic-gate (void) printf("%s", dhcp_status_hdr_string()); 44077c478bd9Sstevel@tonic-gate (void) printf("%s", dhcp_status_reply_to_string(reply)); 44087c478bd9Sstevel@tonic-gate } 44097c478bd9Sstevel@tonic-gate 44107c478bd9Sstevel@tonic-gate free(request); 44117c478bd9Sstevel@tonic-gate free(reply); 44127c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS); 44137c478bd9Sstevel@tonic-gate } 44147c478bd9Sstevel@tonic-gate 44157c478bd9Sstevel@tonic-gate static void 44167c478bd9Sstevel@tonic-gate usage(void) 44177c478bd9Sstevel@tonic-gate { 44187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 44197c478bd9Sstevel@tonic-gate "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n"); 44207c478bd9Sstevel@tonic-gate 44217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", 44227c478bd9Sstevel@tonic-gate "\t[ <addr_family> ]\n" 44237c478bd9Sstevel@tonic-gate "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n" 44247c478bd9Sstevel@tonic-gate "\t[ set [ <address>][/<prefix_length>] ]" 44257c478bd9Sstevel@tonic-gate " [ <address>/<prefix_length>] ]\n" 44267c478bd9Sstevel@tonic-gate "\t[ destination <dest_address> ]\n" 44277c478bd9Sstevel@tonic-gate "\t[ addif <address>[/<prefix_length>]" 44287c478bd9Sstevel@tonic-gate " [ <dest_address> ] ]\n" 44297c478bd9Sstevel@tonic-gate "\t[ removeif <address>[/<prefix_length>] ]\n" 44307c478bd9Sstevel@tonic-gate "\t[ arp | -arp ]\n" 44317c478bd9Sstevel@tonic-gate "\t[ auto-revarp ]\n" 44327c478bd9Sstevel@tonic-gate "\t[ broadcast <broad_addr> ]\n" 44337c478bd9Sstevel@tonic-gate "\t[ index <if_index> ]\n" 44347c478bd9Sstevel@tonic-gate "\t[ metric <n> ] [ mtu <n> ]\n" 44357c478bd9Sstevel@tonic-gate "\t[ netmask <mask> ]\n" 44367c478bd9Sstevel@tonic-gate "\t[ plumb ] [ unplumb ]\n" 44377c478bd9Sstevel@tonic-gate "\t[ preferred | -preferred ]\n" 44387c478bd9Sstevel@tonic-gate "\t[ private | -private ]\n" 44397c478bd9Sstevel@tonic-gate "\t[ local | -local ]\n" 44407c478bd9Sstevel@tonic-gate "\t[ router | -router ]\n" 44417c478bd9Sstevel@tonic-gate "\t[ subnet <subnet_address>]\n" 44427c478bd9Sstevel@tonic-gate "\t[ trailers | -trailers ]\n" 44437c478bd9Sstevel@tonic-gate "\t[ token <address>/<prefix_length> ]\n" 44447c478bd9Sstevel@tonic-gate "\t[ tsrc <tunnel_src_address> ]\n" 44457c478bd9Sstevel@tonic-gate "\t[ tdst <tunnel_dest_address> ]\n" 44467c478bd9Sstevel@tonic-gate "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n" 44477c478bd9Sstevel@tonic-gate "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n" 44487c478bd9Sstevel@tonic-gate "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n" 44497c478bd9Sstevel@tonic-gate "\t[ up ] [ down ]\n" 44507c478bd9Sstevel@tonic-gate "\t[ xmit | -xmit ]\n" 44517c478bd9Sstevel@tonic-gate "\t[ modlist ]\n" 44527c478bd9Sstevel@tonic-gate "\t[ modinsert <module_name@position> ]\n" 44537c478bd9Sstevel@tonic-gate "\t[ modremove <module_name@position> ]\n" 4454e11c3f44Smeem "\t[ ipmp ]\n" 44557c478bd9Sstevel@tonic-gate "\t[ group <groupname>] | [ group \"\"]\n" 44567c478bd9Sstevel@tonic-gate "\t[ deprecated | -deprecated ]\n" 44577c478bd9Sstevel@tonic-gate "\t[ standby | -standby ]\n" 44587c478bd9Sstevel@tonic-gate "\t[ failover | -failover ]\n" 44597c478bd9Sstevel@tonic-gate "\t[ zone <zonename> | -zone ]\n" 446045916cd2Sjpk "\t[ usesrc <interface> ]\n" 446145916cd2Sjpk "\t[ all-zones ]\n"); 44627c478bd9Sstevel@tonic-gate 44637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "or\n"); 44647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 44657c478bd9Sstevel@tonic-gate "\tifconfig <interface> | -a[ 4 | 6 | D ] [ u | d ]\n"); 44667c478bd9Sstevel@tonic-gate 44677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n" 44687c478bd9Sstevel@tonic-gate "\t[ wait <time> | forever ]\n\t[ primary ]\n" 44697c478bd9Sstevel@tonic-gate "\tstart | drop | ping | release | status | inform\n"); 44707c478bd9Sstevel@tonic-gate } 4471