1*f7c14501SDaniil Lunev /* 2*f7c14501SDaniil Lunev * Copyright 2012, Daniil Lunev. All rights reserved. 3*f7c14501SDaniil Lunev */ 47c478bd9Sstevel@tonic-gate /* 536b41818SGirish Moodalbail * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 67c478bd9Sstevel@tonic-gate */ 77c478bd9Sstevel@tonic-gate /* 87c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California. 97c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 107c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 117c478bd9Sstevel@tonic-gate */ 127c478bd9Sstevel@tonic-gate 137c478bd9Sstevel@tonic-gate #include "defs.h" 147c478bd9Sstevel@tonic-gate #include "strings.h" 157c478bd9Sstevel@tonic-gate #include "ifconfig.h" 167c478bd9Sstevel@tonic-gate #include <compat.h> 177c478bd9Sstevel@tonic-gate #include <libdlpi.h> 18da14cebeSEric Cheng #include <libdllink.h> 192b24ab6bSSebastien Roy #include <libdliptun.h> 202b24ab6bSSebastien Roy #include <libdllink.h> 21ff550d0eSmasputra #include <inet/ip.h> 22d2f8a3dfSpwernau #include <inet/ipsec_impl.h> 236e91bba0SGirish Moodalbail #include <libipadm.h> 246e91bba0SGirish Moodalbail #include <ifaddrs.h> 256e91bba0SGirish Moodalbail #include <libsocket_priv.h> 26ff550d0eSmasputra 277c478bd9Sstevel@tonic-gate #define LOOPBACK_IF "lo0" 287c478bd9Sstevel@tonic-gate #define NONE_STR "none" 297c478bd9Sstevel@tonic-gate #define ARP_MOD_NAME "arp" 306e91bba0SGirish Moodalbail #define LIFC_DEFAULT (LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\ 316e91bba0SGirish Moodalbail LIFC_UNDER_IPMP) 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate typedef struct if_flags { 347c478bd9Sstevel@tonic-gate uint64_t iff_value; 357c478bd9Sstevel@tonic-gate char *iff_name; 367c478bd9Sstevel@tonic-gate } if_flags_t; 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate static if_flags_t if_flags_tbl[] = { 397c478bd9Sstevel@tonic-gate { IFF_UP, "UP" }, 407c478bd9Sstevel@tonic-gate { IFF_BROADCAST, "BROADCAST" }, 417c478bd9Sstevel@tonic-gate { IFF_DEBUG, "DEBUG" }, 427c478bd9Sstevel@tonic-gate { IFF_LOOPBACK, "LOOPBACK" }, 437c478bd9Sstevel@tonic-gate { IFF_POINTOPOINT, "POINTOPOINT" }, 447c478bd9Sstevel@tonic-gate { IFF_NOTRAILERS, "NOTRAILERS" }, 457c478bd9Sstevel@tonic-gate { IFF_RUNNING, "RUNNING" }, 467c478bd9Sstevel@tonic-gate { IFF_NOARP, "NOARP" }, 477c478bd9Sstevel@tonic-gate { IFF_PROMISC, "PROMISC" }, 487c478bd9Sstevel@tonic-gate { IFF_ALLMULTI, "ALLMULTI" }, 497c478bd9Sstevel@tonic-gate { IFF_INTELLIGENT, "INTELLIGENT" }, 507c478bd9Sstevel@tonic-gate { IFF_MULTICAST, "MULTICAST" }, 517c478bd9Sstevel@tonic-gate { IFF_MULTI_BCAST, "MULTI_BCAST" }, 527c478bd9Sstevel@tonic-gate { IFF_UNNUMBERED, "UNNUMBERED" }, 537c478bd9Sstevel@tonic-gate { IFF_DHCPRUNNING, "DHCP" }, 547c478bd9Sstevel@tonic-gate { IFF_PRIVATE, "PRIVATE" }, 557c478bd9Sstevel@tonic-gate { IFF_NOXMIT, "NOXMIT" }, 567c478bd9Sstevel@tonic-gate { IFF_NOLOCAL, "NOLOCAL" }, 577c478bd9Sstevel@tonic-gate { IFF_DEPRECATED, "DEPRECATED" }, 587c478bd9Sstevel@tonic-gate { IFF_ADDRCONF, "ADDRCONF" }, 597c478bd9Sstevel@tonic-gate { IFF_ROUTER, "ROUTER" }, 607c478bd9Sstevel@tonic-gate { IFF_NONUD, "NONUD" }, 617c478bd9Sstevel@tonic-gate { IFF_ANYCAST, "ANYCAST" }, 627c478bd9Sstevel@tonic-gate { IFF_NORTEXCH, "NORTEXCH" }, 637c478bd9Sstevel@tonic-gate { IFF_IPV4, "IPv4" }, 647c478bd9Sstevel@tonic-gate { IFF_IPV6, "IPv6" }, 657c478bd9Sstevel@tonic-gate { IFF_NOFAILOVER, "NOFAILOVER" }, 667c478bd9Sstevel@tonic-gate { IFF_FAILED, "FAILED" }, 677c478bd9Sstevel@tonic-gate { IFF_STANDBY, "STANDBY" }, 687c478bd9Sstevel@tonic-gate { IFF_INACTIVE, "INACTIVE" }, 697c478bd9Sstevel@tonic-gate { IFF_OFFLINE, "OFFLINE" }, 707c478bd9Sstevel@tonic-gate { IFF_XRESOLV, "XRESOLV" }, 717c478bd9Sstevel@tonic-gate { IFF_COS_ENABLED, "CoS" }, 727c478bd9Sstevel@tonic-gate { IFF_PREFERRED, "PREFERRED" }, 737c478bd9Sstevel@tonic-gate { IFF_TEMPORARY, "TEMPORARY" }, 747c478bd9Sstevel@tonic-gate { IFF_FIXEDMTU, "FIXEDMTU" }, 7569bb4bb4Scarlsonj { IFF_VIRTUAL, "VIRTUAL" }, 76e11c3f44Smeem { IFF_DUPLICATE, "DUPLICATE" }, 771cb875aeSCathy Zhou { IFF_IPMP, "IPMP"}, 781cb875aeSCathy Zhou { IFF_VRRP, "VRRP"}, 79550b6e40SSowmini Varadhan { IFF_NOACCEPT, "NOACCEPT"}, 80550b6e40SSowmini Varadhan { IFF_L3PROTECT, "L3PROTECT"} 81e11c3f44Smeem }; 82e11c3f44Smeem 83e11c3f44Smeem typedef struct { 84e11c3f44Smeem const char *ia_app; 85e11c3f44Smeem uint64_t ia_flag; 86e11c3f44Smeem uint_t ia_tries; 87e11c3f44Smeem } if_appflags_t; 88e11c3f44Smeem 89e11c3f44Smeem static const if_appflags_t if_appflags_tbl[] = { 90e11c3f44Smeem { "dhcpagent(1M)", IFF_DHCPRUNNING, 1 }, 91e11c3f44Smeem { "in.ndpd(1M)", IFF_ADDRCONF, 3 }, 92e11c3f44Smeem { NULL, 0, 0 } 937c478bd9Sstevel@tonic-gate }; 947c478bd9Sstevel@tonic-gate 952b24ab6bSSebastien Roy static dladm_handle_t dlh; 962b24ab6bSSebastien Roy boolean_t dlh_opened; 972b24ab6bSSebastien Roy static struct lifreq lifr; 987906a3e0Smeem /* current interface name a particular function is accessing */ 992b24ab6bSSebastien Roy static char name[LIFNAMSIZ]; 1007c478bd9Sstevel@tonic-gate /* foreach interface saved name */ 1012b24ab6bSSebastien Roy static char origname[LIFNAMSIZ]; 1022b24ab6bSSebastien Roy static int setaddr; 1036e91bba0SGirish Moodalbail static boolean_t setaddr_done = _B_FALSE; 1042b24ab6bSSebastien Roy static boolean_t ipsec_policy_set; 1052b24ab6bSSebastien Roy static boolean_t ipsec_auth_covered; 1066e91bba0SGirish Moodalbail static ipadm_handle_t iph; 1076e91bba0SGirish Moodalbail static ipadm_addrobj_t ipaddr; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Make sure the algorithm variables hold more than the sizeof an algorithm 1117c478bd9Sstevel@tonic-gate * in PF_KEY. (For now, more than a uint8_t.) The NO_***_?ALG indicates that 1127c478bd9Sstevel@tonic-gate * there was no algorithm requested, and in the ipsec_req that service should 1137c478bd9Sstevel@tonic-gate * be disabled. (E.g. if ah_aalg remains NO_AH_AALG, then AH will be 1147c478bd9Sstevel@tonic-gate * disabled on that tunnel.) 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate #define NO_AH_AALG 256 1177c478bd9Sstevel@tonic-gate #define NO_ESP_AALG 256 1187c478bd9Sstevel@tonic-gate #define NO_ESP_EALG 256 1197c478bd9Sstevel@tonic-gate 120e11c3f44Smeem int s, s4, s6; 1217c478bd9Sstevel@tonic-gate int af = AF_INET; /* default address family */ 1227c478bd9Sstevel@tonic-gate int debug = 0; 1237c478bd9Sstevel@tonic-gate int all = 0; /* setifdhcp() needs to know this */ 1247c478bd9Sstevel@tonic-gate int verbose = 0; 1257c478bd9Sstevel@tonic-gate int v4compat = 0; /* Compatible printing format */ 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Function prototypes for command functions. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate static int addif(char *arg, int64_t param); 131e11c3f44Smeem static int inetipmp(char *arg, int64_t param); 1327c478bd9Sstevel@tonic-gate static int inetplumb(char *arg, int64_t param); 1337c478bd9Sstevel@tonic-gate static int inetunplumb(char *arg, int64_t param); 1347c478bd9Sstevel@tonic-gate static int removeif(char *arg, int64_t param); 1357c478bd9Sstevel@tonic-gate static int setdebugflag(char *arg, int64_t param); 1367c478bd9Sstevel@tonic-gate static int setifaddr(char *arg, int64_t param); 1377c478bd9Sstevel@tonic-gate static int setifbroadaddr(char *arg, int64_t param); 1387c478bd9Sstevel@tonic-gate static int setifdstaddr(char *arg, int64_t param); 1397c478bd9Sstevel@tonic-gate static int setifether(char *arg, int64_t param); 1407c478bd9Sstevel@tonic-gate static int setifflags(char *arg, int64_t param); 1417c478bd9Sstevel@tonic-gate static int setifindex(char *arg, int64_t param); 1427c478bd9Sstevel@tonic-gate static int setifmetric(char *arg, int64_t param); 1437c478bd9Sstevel@tonic-gate static int setifmtu(char *arg, int64_t param); 1447c478bd9Sstevel@tonic-gate static int setifnetmask(char *arg, int64_t param); 1457c478bd9Sstevel@tonic-gate static int setifprefixlen(char *arg, int64_t param); 1467c478bd9Sstevel@tonic-gate static int setifrevarp(char *arg, int64_t param); 1477c478bd9Sstevel@tonic-gate static int setifsubnet(char *arg, int64_t param); 1487c478bd9Sstevel@tonic-gate static int setiftdst(char *arg, int64_t param); 1497c478bd9Sstevel@tonic-gate static int setiftoken(char *arg, int64_t param); 1507c478bd9Sstevel@tonic-gate static int setiftsrc(char *arg, int64_t param); 1517c478bd9Sstevel@tonic-gate static int setverboseflag(char *arg, int64_t param); 1527c478bd9Sstevel@tonic-gate static int set_tun_ah_alg(char *arg, int64_t param); 1537c478bd9Sstevel@tonic-gate static int set_tun_esp_auth_alg(char *arg, int64_t param); 1547c478bd9Sstevel@tonic-gate static int set_tun_esp_encr_alg(char *arg, int64_t param); 1557c478bd9Sstevel@tonic-gate static int modlist(char *arg, int64_t param); 1567c478bd9Sstevel@tonic-gate static int modinsert(char *arg, int64_t param); 1577c478bd9Sstevel@tonic-gate static int modremove(char *arg, int64_t param); 1587c478bd9Sstevel@tonic-gate static int setifgroupname(char *arg, int64_t param); 1597c478bd9Sstevel@tonic-gate static int configinfo(char *arg, int64_t param); 160e11c3f44Smeem static void print_config_flags(int af, uint64_t flags); 1617c478bd9Sstevel@tonic-gate static void print_flags(uint64_t flags); 1626e91bba0SGirish Moodalbail static void print_ifether(const char *ifname); 1637c478bd9Sstevel@tonic-gate static int set_tun_encap_limit(char *arg, int64_t param); 1647c478bd9Sstevel@tonic-gate static int clr_tun_encap_limit(char *arg, int64_t param); 1657c478bd9Sstevel@tonic-gate static int set_tun_hop_limit(char *arg, int64_t param); 1667c478bd9Sstevel@tonic-gate static int setzone(char *arg, int64_t param); 16745916cd2Sjpk static int setallzones(char *arg, int64_t param); 1687c478bd9Sstevel@tonic-gate static int setifsrc(char *arg, int64_t param); 169e11c3f44Smeem static int lifnum(const char *ifname); 1706e91bba0SGirish Moodalbail static void plumball(int, char **, int64_t, int64_t, int64_t); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Address family specific function prototypes. 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate static void in_getaddr(char *s, struct sockaddr *saddr, int *plenp); 1767c478bd9Sstevel@tonic-gate static void in_status(int force, uint64_t flags); 1777c478bd9Sstevel@tonic-gate static void in_configinfo(int force, uint64_t flags); 1787c478bd9Sstevel@tonic-gate static void in6_getaddr(char *s, struct sockaddr *saddr, int *plenp); 1797c478bd9Sstevel@tonic-gate static void in6_status(int force, uint64_t flags); 1807c478bd9Sstevel@tonic-gate static void in6_configinfo(int force, uint64_t flags); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* 1837c478bd9Sstevel@tonic-gate * Misc support functions 1847c478bd9Sstevel@tonic-gate */ 185d62bc4baSyz static boolean_t ni_entry(const char *, void *); 1866e91bba0SGirish Moodalbail static void foreachinterface(int argc, char *argv[], 1876e91bba0SGirish Moodalbail int af, int64_t onflags, int64_t offflags, 1886e91bba0SGirish Moodalbail int64_t lifc_flags); 1896e91bba0SGirish Moodalbail static void ifconfig(int argc, char *argv[], int af, 1906e91bba0SGirish Moodalbail struct ifaddrs *ifa); 191dd7a6f5fSkcpoon static boolean_t in_getmask(struct sockaddr_in *saddr, 192dd7a6f5fSkcpoon boolean_t addr_set); 1936e91bba0SGirish Moodalbail static int in_getprefixlen(char *addr, boolean_t slash, int plen); 1947c478bd9Sstevel@tonic-gate static boolean_t in_prefixlentomask(int prefixlen, int maxlen, 1957c478bd9Sstevel@tonic-gate uchar_t *mask); 1966e91bba0SGirish Moodalbail static void status(void); 1976e91bba0SGirish Moodalbail static void ifstatus(const char *ifname); 1986e91bba0SGirish Moodalbail static void tun_status(datalink_id_t); 1996e91bba0SGirish Moodalbail static void usage(void); 2006e91bba0SGirish Moodalbail static int setifdhcp(const char *caller, const char *ifname, 2016e91bba0SGirish Moodalbail int argc, char *argv[]); 2026e91bba0SGirish Moodalbail static int ip_domux2fd(int *, int *, int *, int *, int *); 2036e91bba0SGirish Moodalbail static int ip_plink(int, int, int, int, int); 2046e91bba0SGirish Moodalbail static int modop(char *arg, char op); 2056e91bba0SGirish Moodalbail static int find_all_interfaces(struct lifconf *lifcp, char **buf, 2066e91bba0SGirish Moodalbail int64_t lifc_flags); 2076e91bba0SGirish Moodalbail static int create_ipmp(const char *grname, int af, 2086e91bba0SGirish Moodalbail const char *ifname, boolean_t implicit); 2096e91bba0SGirish Moodalbail static void start_ipmp_daemon(void); 2106e91bba0SGirish Moodalbail static boolean_t ifaddr_up(ifaddrlistx_t *ifaddrp); 2116e91bba0SGirish Moodalbail static boolean_t ifaddr_down(ifaddrlistx_t *ifaddrp); 2122b24ab6bSSebastien Roy static dladm_status_t ifconfig_dladm_open(const char *, datalink_class_t, 2136e91bba0SGirish Moodalbail datalink_id_t *); 2146e91bba0SGirish Moodalbail static void dladmerr_exit(dladm_status_t status, const char *str); 2156e91bba0SGirish Moodalbail static void ipadmerr_exit(ipadm_status_t status, const char *str); 2166e91bba0SGirish Moodalbail static boolean_t ifconfig_use_libipadm(int, const char *); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a)) 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there 2227c478bd9Sstevel@tonic-gate * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep 2237c478bd9Sstevel@tonic-gate * on going rather than exit with an error. 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate #define DHCP_EXIT_IF_FAILURE -1 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate #define NEXTARG 0xffffff /* command takes an argument */ 2297c478bd9Sstevel@tonic-gate #define OPTARG 0xfffffe /* command takes an optional argument */ 2307c478bd9Sstevel@tonic-gate #define AF_ANY (-1) 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */ 2337c478bd9Sstevel@tonic-gate #define NETMASK_CMD "netmask" 2347c478bd9Sstevel@tonic-gate struct sockaddr_storage g_netmask; 235dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET } 236dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate struct cmd { 2397c478bd9Sstevel@tonic-gate char *c_name; 2407c478bd9Sstevel@tonic-gate int64_t c_parameter; /* NEXTARG means next argv */ 2417c478bd9Sstevel@tonic-gate int (*c_func)(char *, int64_t); 2427c478bd9Sstevel@tonic-gate int c_abortonfail; /* don't continue parsing args */ 2437c478bd9Sstevel@tonic-gate /* for the current interface */ 2447c478bd9Sstevel@tonic-gate int c_af; /* address family restrictions */ 2457c478bd9Sstevel@tonic-gate } cmds[] = { 2467c478bd9Sstevel@tonic-gate { "up", IFF_UP, setifflags, 0, AF_ANY }, 2477c478bd9Sstevel@tonic-gate { "down", -IFF_UP, setifflags, 0, AF_ANY }, 2487c478bd9Sstevel@tonic-gate { "trailers", -IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 2497c478bd9Sstevel@tonic-gate { "-trailers", IFF_NOTRAILERS, setifflags, 0, AF_ANY }, 2507c478bd9Sstevel@tonic-gate { "arp", -IFF_NOARP, setifflags, 0, AF_INET }, 2517c478bd9Sstevel@tonic-gate { "-arp", IFF_NOARP, setifflags, 0, AF_INET }, 2527c478bd9Sstevel@tonic-gate { "router", IFF_ROUTER, setifflags, 0, AF_ANY }, 2537c478bd9Sstevel@tonic-gate { "-router", -IFF_ROUTER, setifflags, 0, AF_ANY }, 2547c478bd9Sstevel@tonic-gate { "private", IFF_PRIVATE, setifflags, 0, AF_ANY }, 2557c478bd9Sstevel@tonic-gate { "-private", -IFF_PRIVATE, setifflags, 0, AF_ANY }, 2567c478bd9Sstevel@tonic-gate { "xmit", -IFF_NOXMIT, setifflags, 0, AF_ANY }, 2577c478bd9Sstevel@tonic-gate { "-xmit", IFF_NOXMIT, setifflags, 0, AF_ANY }, 2587c478bd9Sstevel@tonic-gate { "-nud", IFF_NONUD, setifflags, 0, AF_INET6 }, 2597c478bd9Sstevel@tonic-gate { "nud", -IFF_NONUD, setifflags, 0, AF_INET6 }, 2607c478bd9Sstevel@tonic-gate { "anycast", IFF_ANYCAST, setifflags, 0, AF_ANY }, 2617c478bd9Sstevel@tonic-gate { "-anycast", -IFF_ANYCAST, setifflags, 0, AF_ANY }, 2627c478bd9Sstevel@tonic-gate { "local", -IFF_NOLOCAL, setifflags, 0, AF_ANY }, 2637c478bd9Sstevel@tonic-gate { "-local", IFF_NOLOCAL, setifflags, 0, AF_ANY }, 2647c478bd9Sstevel@tonic-gate { "deprecated", IFF_DEPRECATED, setifflags, 0, AF_ANY }, 2657c478bd9Sstevel@tonic-gate { "-deprecated", -IFF_DEPRECATED, setifflags, 0, AF_ANY }, 2667c478bd9Sstevel@tonic-gate { "preferred", IFF_PREFERRED, setifflags, 0, AF_INET6 }, 2677c478bd9Sstevel@tonic-gate { "-preferred", -IFF_PREFERRED, setifflags, 0, AF_INET6 }, 2687c478bd9Sstevel@tonic-gate { "debug", 0, setdebugflag, 0, AF_ANY }, 2697c478bd9Sstevel@tonic-gate { "verbose", 0, setverboseflag, 0, AF_ANY }, 2707c478bd9Sstevel@tonic-gate { NETMASK_CMD, NEXTARG, setifnetmask, 0, AF_INET }, 2717c478bd9Sstevel@tonic-gate { "metric", NEXTARG, setifmetric, 0, AF_ANY }, 2727c478bd9Sstevel@tonic-gate { "mtu", NEXTARG, setifmtu, 0, AF_ANY }, 2737c478bd9Sstevel@tonic-gate { "index", NEXTARG, setifindex, 0, AF_ANY }, 2747c478bd9Sstevel@tonic-gate { "broadcast", NEXTARG, setifbroadaddr, 0, AF_INET }, 2757c478bd9Sstevel@tonic-gate { "auto-revarp", 0, setifrevarp, 1, AF_INET }, 276e11c3f44Smeem { "ipmp", 0, inetipmp, 1, AF_ANY }, 2777c478bd9Sstevel@tonic-gate { "plumb", 0, inetplumb, 1, AF_ANY }, 2787c478bd9Sstevel@tonic-gate { "unplumb", 0, inetunplumb, 0, AF_ANY }, 2797c478bd9Sstevel@tonic-gate { "subnet", NEXTARG, setifsubnet, 0, AF_ANY }, 2807c478bd9Sstevel@tonic-gate { "token", NEXTARG, setiftoken, 0, AF_INET6 }, 2817c478bd9Sstevel@tonic-gate { "tsrc", NEXTARG, setiftsrc, 0, AF_ANY }, 2827c478bd9Sstevel@tonic-gate { "tdst", NEXTARG, setiftdst, 0, AF_ANY }, 2837c478bd9Sstevel@tonic-gate { "encr_auth_algs", NEXTARG, set_tun_esp_auth_alg, 0, AF_ANY }, 2847c478bd9Sstevel@tonic-gate { "encr_algs", NEXTARG, set_tun_esp_encr_alg, 0, AF_ANY }, 2857c478bd9Sstevel@tonic-gate { "auth_algs", NEXTARG, set_tun_ah_alg, 0, AF_ANY }, 2867c478bd9Sstevel@tonic-gate { "addif", NEXTARG, addif, 1, AF_ANY }, 2877c478bd9Sstevel@tonic-gate { "removeif", NEXTARG, removeif, 1, AF_ANY }, 2887c478bd9Sstevel@tonic-gate { "modlist", 0, modlist, 1, AF_ANY }, 2897c478bd9Sstevel@tonic-gate { "modinsert", NEXTARG, modinsert, 1, AF_ANY }, 2907c478bd9Sstevel@tonic-gate { "modremove", NEXTARG, modremove, 1, AF_ANY }, 2917c478bd9Sstevel@tonic-gate { "failover", -IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 2927c478bd9Sstevel@tonic-gate { "-failover", IFF_NOFAILOVER, setifflags, 1, AF_ANY }, 2937c478bd9Sstevel@tonic-gate { "standby", IFF_STANDBY, setifflags, 1, AF_ANY }, 2947c478bd9Sstevel@tonic-gate { "-standby", -IFF_STANDBY, setifflags, 1, AF_ANY }, 2957c478bd9Sstevel@tonic-gate { "failed", IFF_FAILED, setifflags, 1, AF_ANY }, 2967c478bd9Sstevel@tonic-gate { "-failed", -IFF_FAILED, setifflags, 1, AF_ANY }, 2977c478bd9Sstevel@tonic-gate { "group", NEXTARG, setifgroupname, 1, AF_ANY }, 2987c478bd9Sstevel@tonic-gate { "configinfo", 0, configinfo, 1, AF_ANY }, 2997906a3e0Smeem { "encaplimit", NEXTARG, set_tun_encap_limit, 0, AF_ANY }, 3007906a3e0Smeem { "-encaplimit", 0, clr_tun_encap_limit, 0, AF_ANY }, 3017906a3e0Smeem { "thoplimit", NEXTARG, set_tun_hop_limit, 0, AF_ANY }, 3027c478bd9Sstevel@tonic-gate { "set", NEXTARG, setifaddr, 0, AF_ANY }, 3037c478bd9Sstevel@tonic-gate { "destination", NEXTARG, setifdstaddr, 0, AF_ANY }, 3047c478bd9Sstevel@tonic-gate { "zone", NEXTARG, setzone, 0, AF_ANY }, 3057c478bd9Sstevel@tonic-gate { "-zone", 0, setzone, 0, AF_ANY }, 30645916cd2Sjpk { "all-zones", 0, setallzones, 0, AF_ANY }, 3077c478bd9Sstevel@tonic-gate { "ether", OPTARG, setifether, 0, AF_ANY }, 3087c478bd9Sstevel@tonic-gate { "usesrc", NEXTARG, setifsrc, 0, AF_ANY }, 309f7d61273Smeem 310f7d61273Smeem /* 311f7d61273Smeem * NOTE: any additions to this table must also be applied to ifparse 312f7d61273Smeem * (usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c) 313f7d61273Smeem */ 314f7d61273Smeem 3157c478bd9Sstevel@tonic-gate { 0, 0, setifaddr, 0, AF_ANY }, 3167c478bd9Sstevel@tonic-gate { 0, 0, setifdstaddr, 0, AF_ANY }, 3177c478bd9Sstevel@tonic-gate { 0, 0, 0, 0, 0 }, 3187c478bd9Sstevel@tonic-gate }; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate typedef struct if_config_cmd { 3227c478bd9Sstevel@tonic-gate uint64_t iff_flag; 323e11c3f44Smeem int iff_af; 3247c478bd9Sstevel@tonic-gate char *iff_name; 3257c478bd9Sstevel@tonic-gate } if_config_cmd_t; 3267c478bd9Sstevel@tonic-gate 327e11c3f44Smeem /* 328e11c3f44Smeem * NOTE: print_config_flags() processes this table in order, so we put "up" 329e11c3f44Smeem * last so that we can be sure "-failover" will take effect first. Otherwise, 330e11c3f44Smeem * IPMP test addresses will erroneously migrate to the IPMP interface. 331e11c3f44Smeem */ 3327c478bd9Sstevel@tonic-gate static if_config_cmd_t if_config_cmd_tbl[] = { 333e11c3f44Smeem { IFF_NOTRAILERS, AF_UNSPEC, "-trailers" }, 334e11c3f44Smeem { IFF_PRIVATE, AF_UNSPEC, "private" }, 335e11c3f44Smeem { IFF_NOXMIT, AF_UNSPEC, "-xmit" }, 336e11c3f44Smeem { IFF_ANYCAST, AF_INET6, "anycast" }, 337e11c3f44Smeem { IFF_NOLOCAL, AF_UNSPEC, "-local" }, 338e11c3f44Smeem { IFF_DEPRECATED, AF_UNSPEC, "deprecated" }, 339e11c3f44Smeem { IFF_NOFAILOVER, AF_UNSPEC, "-failover" }, 340e11c3f44Smeem { IFF_STANDBY, AF_UNSPEC, "standby" }, 341e11c3f44Smeem { IFF_FAILED, AF_UNSPEC, "failed" }, 342e11c3f44Smeem { IFF_PREFERRED, AF_UNSPEC, "preferred" }, 343e11c3f44Smeem { IFF_NONUD, AF_INET6, "-nud" }, 344e11c3f44Smeem { IFF_NOARP, AF_INET, "-arp" }, 345e11c3f44Smeem { IFF_UP, AF_UNSPEC, "up" }, 346e11c3f44Smeem { 0, 0, NULL }, 3477c478bd9Sstevel@tonic-gate }; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate typedef struct ni { 3507c478bd9Sstevel@tonic-gate char ni_name[LIFNAMSIZ]; 3517c478bd9Sstevel@tonic-gate struct ni *ni_next; 3527c478bd9Sstevel@tonic-gate } ni_t; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate static ni_t *ni_list = NULL; 3557c478bd9Sstevel@tonic-gate static int num_ni = 0; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */ 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate /* Known address families */ 3607c478bd9Sstevel@tonic-gate struct afswtch { 3617c478bd9Sstevel@tonic-gate char *af_name; 3627c478bd9Sstevel@tonic-gate short af_af; 3637c478bd9Sstevel@tonic-gate void (*af_status)(); 3647c478bd9Sstevel@tonic-gate void (*af_getaddr)(); 3657c478bd9Sstevel@tonic-gate void (*af_configinfo)(); 3667c478bd9Sstevel@tonic-gate } afs[] = { 3677c478bd9Sstevel@tonic-gate { "inet", AF_INET, in_status, in_getaddr, in_configinfo }, 3687c478bd9Sstevel@tonic-gate { "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo }, 3697c478bd9Sstevel@tonic-gate { 0, 0, 0, 0, 0 } 3707c478bd9Sstevel@tonic-gate }; 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af)) 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate struct afswtch *afp; /* the address family being set or asked about */ 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate int 3777c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3787c478bd9Sstevel@tonic-gate { 3792b24ab6bSSebastien Roy int64_t lifc_flags; 3802b24ab6bSSebastien Roy char *default_ip_str; 3816e91bba0SGirish Moodalbail ipadm_status_t istatus; 3827c478bd9Sstevel@tonic-gate 3836e91bba0SGirish Moodalbail lifc_flags = LIFC_DEFAULT; 384e11c3f44Smeem 3857c478bd9Sstevel@tonic-gate if (argc < 2) { 386*f7c14501SDaniil Lunev (void) strncpy(name, "-a", sizeof (name)); 387*f7c14501SDaniil Lunev } else { 388*f7c14501SDaniil Lunev argc--, argv++; 389*f7c14501SDaniil Lunev if (strlen(*argv) > sizeof (name) - 1) { 390*f7c14501SDaniil Lunev (void) fprintf(stderr, "%s: interface name too long\n", 391*f7c14501SDaniil Lunev *argv); 392*f7c14501SDaniil Lunev exit(1); 393*f7c14501SDaniil Lunev } 394*f7c14501SDaniil Lunev (void) strncpy(name, *argv, sizeof (name)); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate name[sizeof (name) - 1] = '\0'; 3977c478bd9Sstevel@tonic-gate (void) strncpy(origname, name, sizeof (origname)); /* For addif */ 3987c478bd9Sstevel@tonic-gate default_ip_str = NULL; 3997c478bd9Sstevel@tonic-gate v4compat = get_compat_flag(&default_ip_str); 4007c478bd9Sstevel@tonic-gate if (v4compat == DEFAULT_PROT_BAD_VALUE) { 4017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4027c478bd9Sstevel@tonic-gate "ifconfig: %s: Bad value for %s in %s\n", default_ip_str, 4037c478bd9Sstevel@tonic-gate DEFAULT_IP, INET_DEFAULT_FILE); 4047c478bd9Sstevel@tonic-gate free(default_ip_str); 4057c478bd9Sstevel@tonic-gate exit(2); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate free(default_ip_str); 4087c478bd9Sstevel@tonic-gate argc--, argv++; 4097c478bd9Sstevel@tonic-gate if (argc > 0) { 4107c478bd9Sstevel@tonic-gate struct afswtch *myafp; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate for (myafp = afp = afs; myafp->af_name; myafp++) { 4137c478bd9Sstevel@tonic-gate if (strcmp(myafp->af_name, *argv) == 0) { 4147c478bd9Sstevel@tonic-gate afp = myafp; argc--; argv++; 4157c478bd9Sstevel@tonic-gate break; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family = afp->af_af; 4197c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 4207c478bd9Sstevel@tonic-gate v4compat = 0; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate s = socket(SOCKET_AF(af), SOCK_DGRAM, 0); 425e11c3f44Smeem s4 = socket(AF_INET, SOCK_DGRAM, 0); 426e11c3f44Smeem s6 = socket(AF_INET6, SOCK_DGRAM, 0); 427e11c3f44Smeem if (s == -1 || s4 == -1 || s6 == -1) 4287c478bd9Sstevel@tonic-gate Perror0_exit("socket"); 4296e91bba0SGirish Moodalbail /* 4306e91bba0SGirish Moodalbail * Open the global libipadm handle. The flag IPH_LEGACY has to 4316e91bba0SGirish Moodalbail * be specified to indicate that logical interface names will 4326e91bba0SGirish Moodalbail * be used during interface creation and address creation. 4336e91bba0SGirish Moodalbail */ 4346e91bba0SGirish Moodalbail if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS) 4356e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "unable to open handle to libipadm"); 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * Special interface names is any combination of these flags. 4397c478bd9Sstevel@tonic-gate * Note that due to the ifconfig syntax they have to be combined 4407c478bd9Sstevel@tonic-gate * as a single '-' option. 4417c478bd9Sstevel@tonic-gate * -a All interfaces 4427c478bd9Sstevel@tonic-gate * -u "up" interfaces 4437c478bd9Sstevel@tonic-gate * -d "down" interfaces 4447c478bd9Sstevel@tonic-gate * -D Interfaces not controlled by DHCP 4457c478bd9Sstevel@tonic-gate * -4 IPv4 interfaces 4467c478bd9Sstevel@tonic-gate * -6 IPv6 interfaces 4477c478bd9Sstevel@tonic-gate * -X Turn on debug (not documented) 4487c478bd9Sstevel@tonic-gate * -v Turn on verbose 4497c478bd9Sstevel@tonic-gate * -Z Only interfaces in caller's zone 4507c478bd9Sstevel@tonic-gate */ 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate if (name[0] == '-') { 4537c478bd9Sstevel@tonic-gate /* One or more options */ 4547c478bd9Sstevel@tonic-gate int64_t onflags = 0; 4557c478bd9Sstevel@tonic-gate int64_t offflags = 0; 4567c478bd9Sstevel@tonic-gate int c; 4577c478bd9Sstevel@tonic-gate char *av[2] = { "ifconfig", name }; 4587c478bd9Sstevel@tonic-gate 459*f7c14501SDaniil Lunev while ((c = getopt(2, av, "audhDXZ46v")) != -1) { 4607c478bd9Sstevel@tonic-gate switch ((char)c) { 4617c478bd9Sstevel@tonic-gate case 'a': 4627c478bd9Sstevel@tonic-gate all = 1; 4637c478bd9Sstevel@tonic-gate break; 4647c478bd9Sstevel@tonic-gate case 'u': 4657c478bd9Sstevel@tonic-gate onflags |= IFF_UP; 4667c478bd9Sstevel@tonic-gate break; 4677c478bd9Sstevel@tonic-gate case 'd': 4687c478bd9Sstevel@tonic-gate offflags |= IFF_UP; 4697c478bd9Sstevel@tonic-gate break; 4707c478bd9Sstevel@tonic-gate case 'D': 4717c478bd9Sstevel@tonic-gate offflags |= IFF_DHCPRUNNING; 4727c478bd9Sstevel@tonic-gate break; 4737c478bd9Sstevel@tonic-gate case 'X': 4747c478bd9Sstevel@tonic-gate debug += 3; 4757c478bd9Sstevel@tonic-gate break; 4767c478bd9Sstevel@tonic-gate case 'Z': 4777c478bd9Sstevel@tonic-gate lifc_flags &= ~LIFC_ALLZONES; 4787c478bd9Sstevel@tonic-gate break; 4797c478bd9Sstevel@tonic-gate case '4': 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * -4 is not a compatable flag, therefore 4827c478bd9Sstevel@tonic-gate * we assume they want v4compat turned off 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate v4compat = 0; 4857c478bd9Sstevel@tonic-gate onflags |= IFF_IPV4; 4867c478bd9Sstevel@tonic-gate break; 4877c478bd9Sstevel@tonic-gate case '6': 4887c478bd9Sstevel@tonic-gate /* 4897c478bd9Sstevel@tonic-gate * If they want IPv6, well then we'll assume 4907c478bd9Sstevel@tonic-gate * they don't want IPv4 compat 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate v4compat = 0; 4937c478bd9Sstevel@tonic-gate onflags |= IFF_IPV6; 4947c478bd9Sstevel@tonic-gate break; 4957c478bd9Sstevel@tonic-gate case 'v': 4967c478bd9Sstevel@tonic-gate verbose = 1; 4977c478bd9Sstevel@tonic-gate break; 498*f7c14501SDaniil Lunev case 'h': 4997c478bd9Sstevel@tonic-gate case '?': 5007c478bd9Sstevel@tonic-gate usage(); 5017c478bd9Sstevel@tonic-gate exit(1); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate if (!all) { 5057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 506c7e4935fSss "ifconfig: %s: no such interface\n", name); 5077c478bd9Sstevel@tonic-gate exit(1); 5087c478bd9Sstevel@tonic-gate } 5096e91bba0SGirish Moodalbail foreachinterface(argc, argv, af, onflags, offflags, 5107c478bd9Sstevel@tonic-gate lifc_flags); 5117c478bd9Sstevel@tonic-gate } else { 5126e91bba0SGirish Moodalbail ifconfig(argc, argv, af, NULL); 5137c478bd9Sstevel@tonic-gate } 5146e91bba0SGirish Moodalbail ipadm_close(iph); 5157c478bd9Sstevel@tonic-gate return (0); 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate /* 5196e91bba0SGirish Moodalbail * For each interface, call ifconfig(argc, argv, af, ifa). 5207c478bd9Sstevel@tonic-gate * Only call function if onflags and offflags are set or clear, respectively, 5217c478bd9Sstevel@tonic-gate * in the interfaces flags field. 5227c478bd9Sstevel@tonic-gate */ 5237c478bd9Sstevel@tonic-gate static void 5246e91bba0SGirish Moodalbail foreachinterface(int argc, char *argv[], int af, 5257c478bd9Sstevel@tonic-gate int64_t onflags, int64_t offflags, int64_t lifc_flags) 5267c478bd9Sstevel@tonic-gate { 5276e91bba0SGirish Moodalbail ipadm_addr_info_t *ainfo, *ainfop; 5286e91bba0SGirish Moodalbail struct ifaddrs *ifa; 5296e91bba0SGirish Moodalbail ipadm_status_t istatus; 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate /* 5327c478bd9Sstevel@tonic-gate * Special case: 5332b24ab6bSSebastien Roy * ifconfig -a plumb should find all network interfaces in the current 5342b24ab6bSSebastien Roy * zone. 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate if (argc > 0 && (strcmp(*argv, "plumb") == 0)) { 5376e91bba0SGirish Moodalbail plumball(argc, argv, onflags, offflags, lifc_flags); 5386e91bba0SGirish Moodalbail return; 5397c478bd9Sstevel@tonic-gate } 5406e91bba0SGirish Moodalbail /* Get all addresses in kernel including addresses that are zero. */ 5416e91bba0SGirish Moodalbail istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR, 5426e91bba0SGirish Moodalbail lifc_flags); 5436e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 5446e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not get addresses from kernel"); 5457c478bd9Sstevel@tonic-gate 5466e91bba0SGirish Moodalbail /* 5476e91bba0SGirish Moodalbail * For each logical interface, call ifconfig() with the 5486e91bba0SGirish Moodalbail * given arguments. 5496e91bba0SGirish Moodalbail */ 5506e91bba0SGirish Moodalbail for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) { 5516e91bba0SGirish Moodalbail if (ainfop->ia_state == IFA_DISABLED) 5526e91bba0SGirish Moodalbail continue; 5536e91bba0SGirish Moodalbail ifa = &ainfop->ia_ifa; 5547c478bd9Sstevel@tonic-gate if (onflags || offflags) { 5556e91bba0SGirish Moodalbail if ((ifa->ifa_flags & onflags) != onflags) 5567c478bd9Sstevel@tonic-gate continue; 5576e91bba0SGirish Moodalbail if ((~ifa->ifa_flags & offflags) != offflags) 5587c478bd9Sstevel@tonic-gate continue; 5597c478bd9Sstevel@tonic-gate } 56064639aafSDarren Reed s = (ifa->ifa_addr->sa_family == AF_INET ? s4 : s6); 5616e91bba0SGirish Moodalbail (void) strncpy(name, ifa->ifa_name, sizeof (name)); 5626e91bba0SGirish Moodalbail (void) strncpy(origname, name, sizeof (origname)); 5636e91bba0SGirish Moodalbail ifconfig(argc, argv, af, ifa); 5646e91bba0SGirish Moodalbail } 5656e91bba0SGirish Moodalbail ipadm_free_addr_info(ainfo); 5666e91bba0SGirish Moodalbail } 5677c478bd9Sstevel@tonic-gate 5686e91bba0SGirish Moodalbail /* 5696e91bba0SGirish Moodalbail * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface. 5706e91bba0SGirish Moodalbail */ 5716e91bba0SGirish Moodalbail static void 5726e91bba0SGirish Moodalbail plumball(int argc, char *argv[], int64_t onflags, int64_t offflags, 5736e91bba0SGirish Moodalbail int64_t lifc_flags) 5746e91bba0SGirish Moodalbail { 5756e91bba0SGirish Moodalbail int n; 5766e91bba0SGirish Moodalbail struct lifreq *lifrp; 5776e91bba0SGirish Moodalbail struct lifconf lifc; 5786e91bba0SGirish Moodalbail char *buf; 5797c478bd9Sstevel@tonic-gate 5806e91bba0SGirish Moodalbail if (onflags != 0 || offflags != 0) { 5816e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: invalid syntax used to " 5826e91bba0SGirish Moodalbail "plumb all interfaces.\n"); 5836e91bba0SGirish Moodalbail exit(1); 5846e91bba0SGirish Moodalbail } 5856e91bba0SGirish Moodalbail 5866e91bba0SGirish Moodalbail if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 || 5876e91bba0SGirish Moodalbail lifc.lifc_len == 0) 5886e91bba0SGirish Moodalbail return; 5897c478bd9Sstevel@tonic-gate 5906e91bba0SGirish Moodalbail lifrp = lifc.lifc_req; 5916e91bba0SGirish Moodalbail for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) { 5927c478bd9Sstevel@tonic-gate /* 5937c478bd9Sstevel@tonic-gate * Reset global state 5947c478bd9Sstevel@tonic-gate * setaddr: Used by parser to tear apart source and dest 5957c478bd9Sstevel@tonic-gate * name and origname contain the name of the 'current' 5967c478bd9Sstevel@tonic-gate * interface. 5977c478bd9Sstevel@tonic-gate */ 5987c478bd9Sstevel@tonic-gate setaddr = 0; 5997c478bd9Sstevel@tonic-gate (void) strncpy(name, lifrp->lifr_name, sizeof (name)); 6007c478bd9Sstevel@tonic-gate (void) strncpy(origname, name, sizeof (origname)); 6016e91bba0SGirish Moodalbail ifconfig(argc, argv, af, NULL); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate /* 6066e91bba0SGirish Moodalbail * Parses the interface name and the command in argv[]. Calls the 6076e91bba0SGirish Moodalbail * appropriate callback function for the given command from `cmds[]' 6086e91bba0SGirish Moodalbail * table. 6096e91bba0SGirish Moodalbail * If there is no command specified, it prints all addresses. 6107c478bd9Sstevel@tonic-gate */ 6117c478bd9Sstevel@tonic-gate static void 6126e91bba0SGirish Moodalbail ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa) 6137c478bd9Sstevel@tonic-gate { 6147c478bd9Sstevel@tonic-gate static boolean_t scan_netmask = _B_FALSE; 6157c478bd9Sstevel@tonic-gate int ret; 6166e91bba0SGirish Moodalbail ipadm_status_t istatus; 6176e91bba0SGirish Moodalbail struct lifreq lifr; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (argc == 0) { 6207c478bd9Sstevel@tonic-gate status(); 6217c478bd9Sstevel@tonic-gate return; 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) { 625d04ccbb3Scarlsonj /* 626d04ccbb3Scarlsonj * Some errors are ignored in the case where more than one 627d04ccbb3Scarlsonj * interface is being operated on. 628d04ccbb3Scarlsonj */ 629d04ccbb3Scarlsonj ret = setifdhcp("ifconfig", name, argc, argv); 630d04ccbb3Scarlsonj if (ret == DHCP_EXIT_IF_FAILURE) { 631d04ccbb3Scarlsonj if (!all) 632d04ccbb3Scarlsonj exit(DHCP_EXIT_FAILURE); 633d04ccbb3Scarlsonj } else if (ret != DHCP_EXIT_SUCCESS) { 634d04ccbb3Scarlsonj exit(ret); 635d04ccbb3Scarlsonj } 6367c478bd9Sstevel@tonic-gate return; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate /* 6407c478bd9Sstevel@tonic-gate * The following is a "hack" to get around the existing interface 6417c478bd9Sstevel@tonic-gate * setting mechanism. Currently, each interface attribute, 6427c478bd9Sstevel@tonic-gate * such as address, netmask, broadcast, ... is set separately. But 6437c478bd9Sstevel@tonic-gate * sometimes two or more attributes must be set together. For 6447c478bd9Sstevel@tonic-gate * example, setting an address without a netmask does not make sense. 6457c478bd9Sstevel@tonic-gate * Yet they can be set separately for IPv4 address using the current 6467c478bd9Sstevel@tonic-gate * ifconfig(1M) syntax. The kernel then "infers" the correct netmask 6477c478bd9Sstevel@tonic-gate * using the deprecated "IP address classes." This is simply not 6487c478bd9Sstevel@tonic-gate * correct. 6497c478bd9Sstevel@tonic-gate * 6507c478bd9Sstevel@tonic-gate * The "hack" below is to go thru the whole command list looking for 6517c478bd9Sstevel@tonic-gate * the netmask command first. Then use this netmask to set the 6527c478bd9Sstevel@tonic-gate * address. This does not provide an extensible way to accommodate 6537c478bd9Sstevel@tonic-gate * future need for setting more than one attributes together. 6547c478bd9Sstevel@tonic-gate * 655dd7a6f5fSkcpoon * Note that if the "netmask" command argument is a "+", we need 656dd7a6f5fSkcpoon * to save this info and do the query after we know the address to 657dd7a6f5fSkcpoon * be set. The reason is that if "addif" is used, the working 658dd7a6f5fSkcpoon * interface name will be changed later when the logical interface 659dd7a6f5fSkcpoon * is created. In in_getmask(), if an address is not provided, 660dd7a6f5fSkcpoon * it will use the working interface's address to do the query. 661dd7a6f5fSkcpoon * It will be wrong now as we don't know the logical interface's name. 662dd7a6f5fSkcpoon * 6637c478bd9Sstevel@tonic-gate * ifconfig(1M) is too overloaded and the code is so convoluted 6647c478bd9Sstevel@tonic-gate * that it is "safer" not to re-architect the code to fix the above 6657c478bd9Sstevel@tonic-gate * issue, hence this "hack." We may be better off to have a new 6667c478bd9Sstevel@tonic-gate * command with better syntax for configuring network interface 6677c478bd9Sstevel@tonic-gate * parameters... 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate if (!scan_netmask && afp->af_af == AF_INET) { 6707c478bd9Sstevel@tonic-gate int largc; 6717c478bd9Sstevel@tonic-gate char **largv; 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate /* Only go thru the command list once to find the netmask. */ 6747c478bd9Sstevel@tonic-gate scan_netmask = _B_TRUE; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate /* 6777c478bd9Sstevel@tonic-gate * Currently, if multiple netmask commands are specified, the 6787c478bd9Sstevel@tonic-gate * last one will be used as the final netmask. So we need 6797c478bd9Sstevel@tonic-gate * to scan the whole list to preserve this behavior. 6807c478bd9Sstevel@tonic-gate */ 6817c478bd9Sstevel@tonic-gate for (largc = argc, largv = argv; largc > 0; largc--, largv++) { 6827c478bd9Sstevel@tonic-gate if (strcmp(*largv, NETMASK_CMD) == 0) { 6837c478bd9Sstevel@tonic-gate if (--largc == 0) 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate largv++; 6867c478bd9Sstevel@tonic-gate if (strcmp(*largv, "+") == 0) { 687dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_PENDING; 6887c478bd9Sstevel@tonic-gate } else { 6897c478bd9Sstevel@tonic-gate in_getaddr(*largv, (struct sockaddr *) 6907c478bd9Sstevel@tonic-gate &g_netmask, NULL); 691dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_SET; 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate /* Continue the scan. */ 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate while (argc > 0) { 6997c478bd9Sstevel@tonic-gate struct cmd *p; 7007c478bd9Sstevel@tonic-gate boolean_t found_cmd; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate if (debug) 7037c478bd9Sstevel@tonic-gate (void) printf("ifconfig: argv %s\n", *argv); 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate found_cmd = _B_FALSE; 7067c478bd9Sstevel@tonic-gate for (p = cmds; p->c_func; p++) { 7077c478bd9Sstevel@tonic-gate if (p->c_name) { 7087c478bd9Sstevel@tonic-gate if (strcmp(*argv, p->c_name) == 0) { 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * indicate that the command was 7117c478bd9Sstevel@tonic-gate * found and check to see if 7127c478bd9Sstevel@tonic-gate * the address family is valid 7137c478bd9Sstevel@tonic-gate */ 7147c478bd9Sstevel@tonic-gate found_cmd = _B_TRUE; 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 } else { 7207c478bd9Sstevel@tonic-gate if (p->c_af == AF_ANY || 7217c478bd9Sstevel@tonic-gate af == p->c_af) 7227c478bd9Sstevel@tonic-gate break; 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate /* 7267c478bd9Sstevel@tonic-gate * If we found the keyword, but the address family 7277c478bd9Sstevel@tonic-gate * did not match spit out an error 7287c478bd9Sstevel@tonic-gate */ 7297c478bd9Sstevel@tonic-gate if (found_cmd && p->c_name == 0) { 7307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Operation %s not" 7317c478bd9Sstevel@tonic-gate " supported for %s\n", *argv, afp->af_name); 7327c478bd9Sstevel@tonic-gate exit(1); 7337c478bd9Sstevel@tonic-gate } 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * else (no keyword found), we assume it's an address 7367c478bd9Sstevel@tonic-gate * of some sort 7377c478bd9Sstevel@tonic-gate */ 7386e91bba0SGirish Moodalbail if (setaddr && ipaddr != NULL) { 7396e91bba0SGirish Moodalbail /* 7406e91bba0SGirish Moodalbail * We must have already filled in a source address in 7416e91bba0SGirish Moodalbail * `ipaddr' and we now got a destination address. 7426e91bba0SGirish Moodalbail * Fill it in `ipaddr' and call libipadm to create 7436e91bba0SGirish Moodalbail * the static address. 7446e91bba0SGirish Moodalbail */ 7456e91bba0SGirish Moodalbail if (p->c_name == 0) { 7466e91bba0SGirish Moodalbail istatus = ipadm_set_dst_addr(ipaddr, *argv, 7476e91bba0SGirish Moodalbail (p->c_af == AF_ANY ? AF_UNSPEC : af)); 7486e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 7496e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not " 7506e91bba0SGirish Moodalbail "set destination address"); 7516e91bba0SGirish Moodalbail } 7526e91bba0SGirish Moodalbail /* 7536e91bba0SGirish Moodalbail * finished processing dstaddr, so reset setaddr 7546e91bba0SGirish Moodalbail */ 7556e91bba0SGirish Moodalbail setaddr = 0; 7566e91bba0SGirish Moodalbail } 7576e91bba0SGirish Moodalbail /* 7586e91bba0SGirish Moodalbail * Both source and destination address are in `ipaddr'. 7596e91bba0SGirish Moodalbail * Add the address by calling libipadm. 7606e91bba0SGirish Moodalbail */ 7616e91bba0SGirish Moodalbail istatus = ipadm_create_addr(iph, ipaddr, 7626e91bba0SGirish Moodalbail IPADM_OPT_ACTIVE); 7636e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 7646e91bba0SGirish Moodalbail goto createfailed; 7656e91bba0SGirish Moodalbail ipadm_destroy_addrobj(ipaddr); 7666e91bba0SGirish Moodalbail ipaddr = NULL; 7676e91bba0SGirish Moodalbail setaddr_done = _B_TRUE; 7686e91bba0SGirish Moodalbail if (p->c_name == 0) { 7696e91bba0SGirish Moodalbail /* move parser along */ 7706e91bba0SGirish Moodalbail argc--, argv++; 7716e91bba0SGirish Moodalbail continue; 7726e91bba0SGirish Moodalbail } 7736e91bba0SGirish Moodalbail } 7746e91bba0SGirish Moodalbail if (p->c_name == 0 && setaddr_done) { 7756e91bba0SGirish Moodalbail /* 7766e91bba0SGirish Moodalbail * catch odd commands like 7776e91bba0SGirish Moodalbail * "ifconfig <intf> addr1 addr2 addr3 addr4 up" 7786e91bba0SGirish Moodalbail */ 7796e91bba0SGirish Moodalbail (void) fprintf(stderr, "%s", 7806e91bba0SGirish Moodalbail "ifconfig: cannot configure more than two " 7816e91bba0SGirish Moodalbail "addresses in one command\n"); 7826e91bba0SGirish Moodalbail exit(1); 7836e91bba0SGirish Moodalbail } 7847c478bd9Sstevel@tonic-gate if (p->c_func) { 7857c478bd9Sstevel@tonic-gate if (p->c_af == AF_INET6) { 7867c478bd9Sstevel@tonic-gate v4compat = 0; 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate if (p->c_parameter == NEXTARG || 7897c478bd9Sstevel@tonic-gate p->c_parameter == OPTARG) { 7907c478bd9Sstevel@tonic-gate argc--, argv++; 7917c478bd9Sstevel@tonic-gate if (argc == 0 && p->c_parameter == NEXTARG) { 7927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7937c478bd9Sstevel@tonic-gate "ifconfig: no argument for %s\n", 7947c478bd9Sstevel@tonic-gate p->c_name); 7957c478bd9Sstevel@tonic-gate exit(1); 7967c478bd9Sstevel@tonic-gate } 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate /* 7997c478bd9Sstevel@tonic-gate * Call the function if: 8007c478bd9Sstevel@tonic-gate * 8017c478bd9Sstevel@tonic-gate * there's no address family 8027c478bd9Sstevel@tonic-gate * restriction 8037c478bd9Sstevel@tonic-gate * OR 8047c478bd9Sstevel@tonic-gate * we don't know the address yet 8057c478bd9Sstevel@tonic-gate * (because we were called from 8067c478bd9Sstevel@tonic-gate * main) 8077c478bd9Sstevel@tonic-gate * OR 8087c478bd9Sstevel@tonic-gate * there is a restriction AND 8097c478bd9Sstevel@tonic-gate * the address families match 8107c478bd9Sstevel@tonic-gate */ 8117c478bd9Sstevel@tonic-gate if ((p->c_af == AF_ANY) || 8126e91bba0SGirish Moodalbail (ifa == NULL) || 81364639aafSDarren Reed (ifa->ifa_addr->sa_family == p->c_af)) { 8147c478bd9Sstevel@tonic-gate ret = (*p->c_func)(*argv, p->c_parameter); 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * If c_func failed and we should 8177c478bd9Sstevel@tonic-gate * abort processing for this 8187c478bd9Sstevel@tonic-gate * interface on failure, return 8197c478bd9Sstevel@tonic-gate * now rather than going on to 8207c478bd9Sstevel@tonic-gate * process other commands for 8217c478bd9Sstevel@tonic-gate * the same interface. 8227c478bd9Sstevel@tonic-gate */ 8237c478bd9Sstevel@tonic-gate if (ret != 0 && p->c_abortonfail) 8247c478bd9Sstevel@tonic-gate return; 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate argc--, argv++; 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate 8306e91bba0SGirish Moodalbail if (setaddr && ipaddr != NULL) { 8316e91bba0SGirish Moodalbail /* 8326e91bba0SGirish Moodalbail * Only the source address was provided, which was already 8336e91bba0SGirish Moodalbail * set in `ipaddr'. Add the address by calling libipadm. 8346e91bba0SGirish Moodalbail */ 8356e91bba0SGirish Moodalbail istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE); 8366e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 8376e91bba0SGirish Moodalbail goto createfailed; 8386e91bba0SGirish Moodalbail ipadm_destroy_addrobj(ipaddr); 8396e91bba0SGirish Moodalbail ipaddr = NULL; 8406e91bba0SGirish Moodalbail setaddr_done = _B_TRUE; 8416e91bba0SGirish Moodalbail } 8426e91bba0SGirish Moodalbail 8437c478bd9Sstevel@tonic-gate /* Check to see if there's a security hole in the tunnel setup. */ 8442b24ab6bSSebastien Roy if (ipsec_policy_set && !ipsec_auth_covered) { 8452b24ab6bSSebastien Roy (void) fprintf(stderr, "ifconfig: WARNING: tunnel with only " 8462b24ab6bSSebastien Roy "ESP and no authentication.\n"); 8472b24ab6bSSebastien Roy } 8486e91bba0SGirish Moodalbail return; 8496e91bba0SGirish Moodalbail 8506e91bba0SGirish Moodalbail createfailed: 8516e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: could not create address:% s\n", 8526e91bba0SGirish Moodalbail ipadm_status2str(istatus)); 8536e91bba0SGirish Moodalbail /* Remove the newly created logical interface. */ 8546e91bba0SGirish Moodalbail if (strcmp(name, origname) != 0) { 8556e91bba0SGirish Moodalbail assert(strchr(name, ':') != NULL); 8566e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 8576e91bba0SGirish Moodalbail (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 8586e91bba0SGirish Moodalbail } 8596e91bba0SGirish Moodalbail exit(1); 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8637c478bd9Sstevel@tonic-gate static int 8647c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg) 8657c478bd9Sstevel@tonic-gate { 8667c478bd9Sstevel@tonic-gate debug++; 8677c478bd9Sstevel@tonic-gate return (0); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* ARGSUSED */ 8717c478bd9Sstevel@tonic-gate static int 8727c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg) 8737c478bd9Sstevel@tonic-gate { 8747c478bd9Sstevel@tonic-gate verbose++; 8757c478bd9Sstevel@tonic-gate return (0); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 878dd7a6f5fSkcpoon /* 879dd7a6f5fSkcpoon * This function fills in the given lifreq's lifr_addr field based on 880dd7a6f5fSkcpoon * g_netmask_set. 881dd7a6f5fSkcpoon */ 882dd7a6f5fSkcpoon static void 883dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr, 884dd7a6f5fSkcpoon struct sockaddr_storage *mask) 885dd7a6f5fSkcpoon { 886dd7a6f5fSkcpoon assert(addr != NULL); 887dd7a6f5fSkcpoon assert(mask != NULL); 888dd7a6f5fSkcpoon 889dd7a6f5fSkcpoon switch (g_netmask_set) { 890dd7a6f5fSkcpoon case G_NETMASK_SET: 891dd7a6f5fSkcpoon lifr->lifr_addr = g_netmask; 892dd7a6f5fSkcpoon break; 893dd7a6f5fSkcpoon 894dd7a6f5fSkcpoon case G_NETMASK_PENDING: 895dd7a6f5fSkcpoon /* 896dd7a6f5fSkcpoon * "+" is used as the argument to "netmask" command. Query 897dd7a6f5fSkcpoon * the database on the correct netmask based on the address to 898dd7a6f5fSkcpoon * be set. 899dd7a6f5fSkcpoon */ 900dd7a6f5fSkcpoon assert(afp->af_af == AF_INET); 901dd7a6f5fSkcpoon g_netmask = *addr; 902dd7a6f5fSkcpoon if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) { 903dd7a6f5fSkcpoon lifr->lifr_addr = *mask; 904dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 905dd7a6f5fSkcpoon } else { 906dd7a6f5fSkcpoon lifr->lifr_addr = g_netmask; 907dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_SET; 908dd7a6f5fSkcpoon } 909dd7a6f5fSkcpoon break; 910dd7a6f5fSkcpoon 911dd7a6f5fSkcpoon case G_NETMASK_NIL: 912dd7a6f5fSkcpoon default: 913dd7a6f5fSkcpoon lifr->lifr_addr = *mask; 914dd7a6f5fSkcpoon break; 915dd7a6f5fSkcpoon } 916dd7a6f5fSkcpoon } 917dd7a6f5fSkcpoon 9187c478bd9Sstevel@tonic-gate /* 9197c478bd9Sstevel@tonic-gate * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n> 9207c478bd9Sstevel@tonic-gate * syntax for setting the address, the address plus netmask, and just 9217c478bd9Sstevel@tonic-gate * the netmask respectively. 9227c478bd9Sstevel@tonic-gate */ 9237c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9247c478bd9Sstevel@tonic-gate static int 9257c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param) 9267c478bd9Sstevel@tonic-gate { 9276e91bba0SGirish Moodalbail ipadm_status_t istatus; 9287c478bd9Sstevel@tonic-gate int prefixlen = 0; 9296e91bba0SGirish Moodalbail struct lifreq lifr1; 9307c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 9317c478bd9Sstevel@tonic-gate struct sockaddr_storage netmask; 9327c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9337c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 9347c478bd9Sstevel@tonic-gate struct sockaddr_storage sav_netmask; 9356e91bba0SGirish Moodalbail char cidraddr[BUFSIZ]; 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate if (addr[0] == '/') 9387c478bd9Sstevel@tonic-gate return (setifprefixlen(addr, 0)); 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen); 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate (void) memset(&netmask, 0, sizeof (netmask)); 9437c478bd9Sstevel@tonic-gate netmask.ss_family = afp->af_af; 9447c478bd9Sstevel@tonic-gate switch (prefixlen) { 9457c478bd9Sstevel@tonic-gate case NO_PREFIX: 9467c478bd9Sstevel@tonic-gate /* Nothing there - ok */ 9477c478bd9Sstevel@tonic-gate break; 9487c478bd9Sstevel@tonic-gate case BAD_ADDR: 9497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n", 9507c478bd9Sstevel@tonic-gate addr); 9517c478bd9Sstevel@tonic-gate exit(1); 9527c478bd9Sstevel@tonic-gate default: 9537c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 9547c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&netmask; 9557906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 9567c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 9577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 9587c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 9597c478bd9Sstevel@tonic-gate prefixlen); 9607c478bd9Sstevel@tonic-gate exit(1); 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate } else { 9637c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&netmask; 9647906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 9657c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 9667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 9677c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 9687c478bd9Sstevel@tonic-gate prefixlen); 9697c478bd9Sstevel@tonic-gate exit(1); 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate } 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate * Just in case of funny setting of both prefix and netmask, 9747c478bd9Sstevel@tonic-gate * prefix should override the netmask command. 9757c478bd9Sstevel@tonic-gate */ 976dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 9777c478bd9Sstevel@tonic-gate break; 9787c478bd9Sstevel@tonic-gate } 9796e91bba0SGirish Moodalbail 9806e91bba0SGirish Moodalbail /* 9816e91bba0SGirish Moodalbail * Check and see if any "netmask" command is used and perform the 9826e91bba0SGirish Moodalbail * necessary operation. 9836e91bba0SGirish Moodalbail */ 9846e91bba0SGirish Moodalbail set_mask_lifreq(&lifr, &laddr, &netmask); 9856e91bba0SGirish Moodalbail 9866e91bba0SGirish Moodalbail /* This check is temporary until libipadm supports IPMP interfaces. */ 9876e91bba0SGirish Moodalbail if (ifconfig_use_libipadm(s, name)) { 988e6889d40SVasumathi Sundaram char addrstr[INET6_ADDRSTRLEN]; 989e6889d40SVasumathi Sundaram 990e6889d40SVasumathi Sundaram if (af == AF_INET) { 991e6889d40SVasumathi Sundaram sin = (struct sockaddr_in *)&laddr; 992e6889d40SVasumathi Sundaram (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr, 993e6889d40SVasumathi Sundaram sizeof (addrstr)); 994e6889d40SVasumathi Sundaram } else { 995e6889d40SVasumathi Sundaram sin6 = (struct sockaddr_in6 *)&laddr; 996e6889d40SVasumathi Sundaram (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 997e6889d40SVasumathi Sundaram sizeof (addrstr)); 998e6889d40SVasumathi Sundaram } 9996e91bba0SGirish Moodalbail istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 10006e91bba0SGirish Moodalbail &ipaddr); 10016e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 10026e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "setifaddr"); 10036e91bba0SGirish Moodalbail 10041214df40SGirish Moodalbail /* 10051214df40SGirish Moodalbail * lifr.lifr_addr, which is updated by set_mask_lifreq() 10061214df40SGirish Moodalbail * will contain the right mask to use. 10071214df40SGirish Moodalbail */ 100864639aafSDarren Reed prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr); 10091214df40SGirish Moodalbail (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 10101214df40SGirish Moodalbail addrstr, prefixlen); 10111214df40SGirish Moodalbail 10121214df40SGirish Moodalbail istatus = ipadm_set_addr(ipaddr, cidraddr, af); 10136e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 10146e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not set address"); 10156e91bba0SGirish Moodalbail /* 10166e91bba0SGirish Moodalbail * let parser know we got a source. 10176e91bba0SGirish Moodalbail * Next address, if given, should be dest 10186e91bba0SGirish Moodalbail */ 10196e91bba0SGirish Moodalbail setaddr++; 10206e91bba0SGirish Moodalbail 10216e91bba0SGirish Moodalbail /* 10226e91bba0SGirish Moodalbail * address will be set by the parser after nextarg has 10236e91bba0SGirish Moodalbail * been scanned 10246e91bba0SGirish Moodalbail */ 10256e91bba0SGirish Moodalbail return (0); 10266e91bba0SGirish Moodalbail } 10276e91bba0SGirish Moodalbail 10287c478bd9Sstevel@tonic-gate /* Tell parser that an address was set */ 10297c478bd9Sstevel@tonic-gate setaddr++; 10307c478bd9Sstevel@tonic-gate /* save copy of netmask to restore in case of error */ 10316e91bba0SGirish Moodalbail (void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name)); 10326e91bba0SGirish Moodalbail if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0) 10337c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFNETMASK"); 10346e91bba0SGirish Moodalbail sav_netmask = lifr1.lifr_addr; 10357c478bd9Sstevel@tonic-gate 10367c478bd9Sstevel@tonic-gate /* 10377c478bd9Sstevel@tonic-gate * If setting the address and not the mask, clear any existing mask 10387c478bd9Sstevel@tonic-gate * and the kernel will then assign the default (netmask has been set 10397c478bd9Sstevel@tonic-gate * to 0 in this case). If setting both (either by using a prefix or 10407c478bd9Sstevel@tonic-gate * using the netmask command), set the mask first, so the address will 10417c478bd9Sstevel@tonic-gate * be interpreted correctly. 10427c478bd9Sstevel@tonic-gate */ 10436e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 10446e91bba0SGirish Moodalbail /* lifr.lifr_addr already contains netmask from set_mask_lifreq() */ 10457c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 10467c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate if (debug) { 10497c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 10507c478bd9Sstevel@tonic-gate void *addr = (afp->af_af == AF_INET) ? 10517c478bd9Sstevel@tonic-gate (void *)&((struct sockaddr_in *)&laddr)->sin_addr : 10527c478bd9Sstevel@tonic-gate (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr; 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate (void) printf("Setting %s af %d addr %s\n", 10557c478bd9Sstevel@tonic-gate lifr.lifr_name, afp->af_af, 10567c478bd9Sstevel@tonic-gate inet_ntop(afp->af_af, addr, abuf, sizeof (abuf))); 10577c478bd9Sstevel@tonic-gate } 10587c478bd9Sstevel@tonic-gate lifr.lifr_addr = laddr; 10597c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = afp->af_af; 10607c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 10617c478bd9Sstevel@tonic-gate /* 10627c478bd9Sstevel@tonic-gate * Restore the netmask 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate int saverr = errno; 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 10677c478bd9Sstevel@tonic-gate lifr.lifr_addr = sav_netmask; 10687c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr); 10697c478bd9Sstevel@tonic-gate errno = saverr; 10707c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFADDR"); 10717c478bd9Sstevel@tonic-gate } 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate return (0); 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate /* 10777c478bd9Sstevel@tonic-gate * The following functions are stolen from the ipseckey(1m) program. 10787c478bd9Sstevel@tonic-gate * Perhaps they should be somewhere common, but for now, we just maintain 10797c478bd9Sstevel@tonic-gate * two versions. We do this because of the different semantics for which 10807c478bd9Sstevel@tonic-gate * algorithms we select ("requested" for ifconfig vs. "actual" for key). 10817c478bd9Sstevel@tonic-gate */ 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate static ulong_t 10847c478bd9Sstevel@tonic-gate parsenum(char *num) 10857c478bd9Sstevel@tonic-gate { 10867c478bd9Sstevel@tonic-gate ulong_t rc; 10877c478bd9Sstevel@tonic-gate char *end = NULL; 10887c478bd9Sstevel@tonic-gate 10897c478bd9Sstevel@tonic-gate errno = 0; 10907c478bd9Sstevel@tonic-gate rc = strtoul(num, &end, 0); 10917c478bd9Sstevel@tonic-gate if (errno != 0 || end == num || *end != '\0') { 10927c478bd9Sstevel@tonic-gate rc = (ulong_t)-1; 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate return (rc); 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate /* 10997c478bd9Sstevel@tonic-gate * Parse and reverse parse possible algorithm values, include numbers. 11007c478bd9Sstevel@tonic-gate * Mostly stolen from ipseckey.c. See the comments above parsenum() for why 11017c478bd9Sstevel@tonic-gate * this isn't common to ipseckey.c. 11027c478bd9Sstevel@tonic-gate * 11037c478bd9Sstevel@tonic-gate * NOTE: Static buffer in this function for the return value. Since ifconfig 11042b24ab6bSSebastien Roy * isn't multithreaded, this isn't a huge problem. 11057c478bd9Sstevel@tonic-gate */ 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate #define NBUF_SIZE 20 /* Enough to print a large integer. */ 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate static char * 11107c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num) 11117c478bd9Sstevel@tonic-gate { 11127c478bd9Sstevel@tonic-gate struct ipsecalgent *alg; 11137c478bd9Sstevel@tonic-gate static char numprint[128]; /* Enough to hold an algorithm name. */ 11147c478bd9Sstevel@tonic-gate 1115d2f8a3dfSpwernau /* 1116d2f8a3dfSpwernau * Special cases for "any" and "none" 1117d2f8a3dfSpwernau * The kernel needs to be able to distinguish between "any" 1118d2f8a3dfSpwernau * and "none" and the APIs are underdefined in this area for auth. 1119d2f8a3dfSpwernau */ 1120d2f8a3dfSpwernau if (proto_num == IPSEC_PROTO_AH) { 1121d2f8a3dfSpwernau if (alg_value == SADB_AALG_NONE) 1122d2f8a3dfSpwernau return ("none"); 1123d2f8a3dfSpwernau if (alg_value == SADB_AALG_ANY) 1124d2f8a3dfSpwernau return ("any"); 1125d2f8a3dfSpwernau } 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate alg = getipsecalgbynum(alg_value, proto_num, NULL); 11287c478bd9Sstevel@tonic-gate if (alg != NULL) { 11297c478bd9Sstevel@tonic-gate (void) strlcpy(numprint, alg->a_names[0], sizeof (numprint)); 11307c478bd9Sstevel@tonic-gate freeipsecalgent(alg); 11317c478bd9Sstevel@tonic-gate } else { 11327c478bd9Sstevel@tonic-gate (void) snprintf(numprint, sizeof (numprint), "%d", alg_value); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate return (numprint); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate static uint_t 11397c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num) 11407c478bd9Sstevel@tonic-gate { 11417c478bd9Sstevel@tonic-gate struct ipsecalgent *alg; 11427c478bd9Sstevel@tonic-gate ulong_t invalue; 11437c478bd9Sstevel@tonic-gate 11447c478bd9Sstevel@tonic-gate if (algname == NULL) { 11457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: Unexpected end of command " 11467c478bd9Sstevel@tonic-gate "line.\n"); 11477c478bd9Sstevel@tonic-gate exit(1); 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate /* 1151d2f8a3dfSpwernau * Special-case "none" and "any". 1152d2f8a3dfSpwernau * Use strcasecmp because its length is bounded. 11537c478bd9Sstevel@tonic-gate */ 11547c478bd9Sstevel@tonic-gate if (strcasecmp("none", algname) == 0) { 11557c478bd9Sstevel@tonic-gate return ((proto_num == IPSEC_PROTO_ESP) ? 11567c478bd9Sstevel@tonic-gate NO_ESP_EALG : NO_ESP_AALG); 11577c478bd9Sstevel@tonic-gate } 1158d2f8a3dfSpwernau if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH)) 1159d2f8a3dfSpwernau return (SADB_AALG_ANY); 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate alg = getipsecalgbyname(algname, proto_num, NULL); 11627c478bd9Sstevel@tonic-gate if (alg != NULL) { 11637c478bd9Sstevel@tonic-gate invalue = alg->a_alg_num; 11647c478bd9Sstevel@tonic-gate freeipsecalgent(alg); 11657c478bd9Sstevel@tonic-gate return ((uint_t)invalue); 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate /* 11697c478bd9Sstevel@tonic-gate * Since algorithms can be loaded during kernel run-time, check for 11707c478bd9Sstevel@tonic-gate * numeric algorithm values too. 11717c478bd9Sstevel@tonic-gate */ 11727c478bd9Sstevel@tonic-gate invalue = parsenum(algname); 11737c478bd9Sstevel@tonic-gate if ((invalue & (ulong_t)0xff) == invalue) 11747c478bd9Sstevel@tonic-gate return ((uint_t)invalue); 11757c478bd9Sstevel@tonic-gate 11767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n", 11777c478bd9Sstevel@tonic-gate (proto_num == IPSEC_PROTO_ESP) ? 11787c478bd9Sstevel@tonic-gate "Encryption" : "Authentication", algname); 11797c478bd9Sstevel@tonic-gate exit(1); 11807c478bd9Sstevel@tonic-gate /* NOTREACHED */ 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate /* 11847c478bd9Sstevel@tonic-gate * Actual ifconfig functions to set tunnel security properties. 11857c478bd9Sstevel@tonic-gate */ 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG }; 11887c478bd9Sstevel@tonic-gate 11897c478bd9Sstevel@tonic-gate static int 11907c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg) 11917c478bd9Sstevel@tonic-gate { 11922b24ab6bSSebastien Roy boolean_t encr_alg_set = _B_FALSE; 11932b24ab6bSSebastien Roy iptun_params_t params; 11942b24ab6bSSebastien Roy dladm_status_t status; 11952b24ab6bSSebastien Roy ipsec_req_t *ipsr; 11967c478bd9Sstevel@tonic-gate 11972b24ab6bSSebastien Roy if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 11982b24ab6bSSebastien Roy ¶ms.iptun_param_linkid)) != DLADM_STATUS_OK) 11992b24ab6bSSebastien Roy goto done; 12007c478bd9Sstevel@tonic-gate 12012b24ab6bSSebastien Roy status = dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE); 12022b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 12032b24ab6bSSebastien Roy goto done; 12047c478bd9Sstevel@tonic-gate 12052b24ab6bSSebastien Roy ipsr = ¶ms.iptun_param_secinfo; 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate /* 12087c478bd9Sstevel@tonic-gate * If I'm just starting off this ifconfig, I want a clean slate, 12097c478bd9Sstevel@tonic-gate * otherwise, I've captured the current tunnel security settings. 12107c478bd9Sstevel@tonic-gate * In the case of continuation, I merely add to the settings. 12117c478bd9Sstevel@tonic-gate */ 12122b24ab6bSSebastien Roy if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO)) 12137c478bd9Sstevel@tonic-gate (void) memset(ipsr, 0, sizeof (*ipsr)); 12147c478bd9Sstevel@tonic-gate 12152b24ab6bSSebastien Roy /* We're only modifying the IPsec information */ 12162b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_SECINFO; 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate switch (which_alg) { 12197c478bd9Sstevel@tonic-gate case ESP_ENCR_ALG: 12207c478bd9Sstevel@tonic-gate if (alg == NO_ESP_EALG) { 12217c478bd9Sstevel@tonic-gate if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE) 12227c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 0; 12237c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = SADB_EALG_NONE; 1224d2f8a3dfSpwernau 1225d2f8a3dfSpwernau /* Let the user specify NULL encryption implicitly. */ 1226d2f8a3dfSpwernau if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) { 1227d2f8a3dfSpwernau encr_alg_set = _B_TRUE; 1228d2f8a3dfSpwernau ipsr->ipsr_esp_alg = SADB_EALG_NULL; 1229d2f8a3dfSpwernau } 12307c478bd9Sstevel@tonic-gate } else { 12317c478bd9Sstevel@tonic-gate encr_alg_set = _B_TRUE; 12327c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 12337c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12347c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = alg; 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate break; 12377c478bd9Sstevel@tonic-gate case ESP_AUTH_ALG: 12387c478bd9Sstevel@tonic-gate if (alg == NO_ESP_AALG) { 1239d2f8a3dfSpwernau if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE || 1240d2f8a3dfSpwernau ipsr->ipsr_esp_alg == SADB_EALG_NULL) && 1241d2f8a3dfSpwernau !encr_alg_set) 12427c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 0; 12437c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE; 12447c478bd9Sstevel@tonic-gate } else { 12457c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_req = 12467c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12477c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_auth_alg = alg; 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate /* Let the user specify NULL encryption implicitly. */ 12507c478bd9Sstevel@tonic-gate if (ipsr->ipsr_esp_alg == SADB_EALG_NONE && 12517c478bd9Sstevel@tonic-gate !encr_alg_set) 12527c478bd9Sstevel@tonic-gate ipsr->ipsr_esp_alg = SADB_EALG_NULL; 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate break; 12557c478bd9Sstevel@tonic-gate case AH_AUTH_ALG: 12567c478bd9Sstevel@tonic-gate if (alg == NO_AH_AALG) { 12577c478bd9Sstevel@tonic-gate ipsr->ipsr_ah_req = 0; 12587c478bd9Sstevel@tonic-gate ipsr->ipsr_auth_alg = SADB_AALG_NONE; 12597c478bd9Sstevel@tonic-gate } else { 12607c478bd9Sstevel@tonic-gate ipsr->ipsr_ah_req = 12617c478bd9Sstevel@tonic-gate IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE; 12627c478bd9Sstevel@tonic-gate ipsr->ipsr_auth_alg = alg; 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate break; 12657c478bd9Sstevel@tonic-gate /* Will never hit DEFAULT */ 12667c478bd9Sstevel@tonic-gate } 12677c478bd9Sstevel@tonic-gate 12682b24ab6bSSebastien Roy status = dladm_iptun_modify(dlh, ¶ms, DLADM_OPT_ACTIVE); 12697c478bd9Sstevel@tonic-gate 12702b24ab6bSSebastien Roy done: 12712b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 12722b24ab6bSSebastien Roy dladmerr_exit(status, name); 12732b24ab6bSSebastien Roy else { 12742b24ab6bSSebastien Roy ipsec_policy_set = _B_TRUE; 12752b24ab6bSSebastien Roy if ((ipsr->ipsr_esp_req != 0 && 12762b24ab6bSSebastien Roy ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) || 12772b24ab6bSSebastien Roy (ipsr->ipsr_ah_req != 0 && 12782b24ab6bSSebastien Roy ipsr->ipsr_auth_alg != SADB_AALG_NONE)) 12792b24ab6bSSebastien Roy ipsec_auth_covered = _B_TRUE; 12802b24ab6bSSebastien Roy } 12817c478bd9Sstevel@tonic-gate return (0); 12827c478bd9Sstevel@tonic-gate } 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate /* ARGSUSED */ 12857c478bd9Sstevel@tonic-gate static int 12867c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param) 12877c478bd9Sstevel@tonic-gate { 12887c478bd9Sstevel@tonic-gate return (set_tun_algs(ESP_ENCR_ALG, 1289fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_ESP))); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate 12927c478bd9Sstevel@tonic-gate /* ARGSUSED */ 12937c478bd9Sstevel@tonic-gate static int 12947c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param) 12957c478bd9Sstevel@tonic-gate { 12967c478bd9Sstevel@tonic-gate return (set_tun_algs(ESP_AUTH_ALG, 1297fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_AH))); 12987c478bd9Sstevel@tonic-gate } 12997c478bd9Sstevel@tonic-gate 13007c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13017c478bd9Sstevel@tonic-gate static int 13027c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param) 13037c478bd9Sstevel@tonic-gate { 13047c478bd9Sstevel@tonic-gate return (set_tun_algs(AH_AUTH_ALG, 1305fc80c0dfSnordmark parsealg(addr, IPSEC_PROTO_AH))); 13067c478bd9Sstevel@tonic-gate } 13077c478bd9Sstevel@tonic-gate 13087c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13097c478bd9Sstevel@tonic-gate static int 13107c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param) 13117c478bd9Sstevel@tonic-gate { 13127c478bd9Sstevel@tonic-gate struct sockaddr_in laddr; 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 13157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13167c478bd9Sstevel@tonic-gate "ifconfig: revarp not possible on IPv6 interface %s\n", 13177c478bd9Sstevel@tonic-gate name); 13187c478bd9Sstevel@tonic-gate exit(1); 13197c478bd9Sstevel@tonic-gate } 13207c478bd9Sstevel@tonic-gate if (doifrevarp(name, &laddr)) { 13217c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13227c478bd9Sstevel@tonic-gate laddr.sin_family = AF_INET; 13237c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr)); 13247c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 13257c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFADDR"); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate return (0); 13287c478bd9Sstevel@tonic-gate } 13297c478bd9Sstevel@tonic-gate 13307c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13317c478bd9Sstevel@tonic-gate static int 13327c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param) 13337c478bd9Sstevel@tonic-gate { 13347c478bd9Sstevel@tonic-gate int prefixlen = 0; 13357c478bd9Sstevel@tonic-gate struct sockaddr_storage subnet; 13367c478bd9Sstevel@tonic-gate 13377c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, &subnet, &prefixlen); 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate switch (prefixlen) { 13407c478bd9Sstevel@tonic-gate case NO_PREFIX: 13417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13427c478bd9Sstevel@tonic-gate "ifconfig: Missing prefix length in subnet %s\n", addr); 13437c478bd9Sstevel@tonic-gate exit(1); 13447c478bd9Sstevel@tonic-gate /* NOTREACHED */ 13457c478bd9Sstevel@tonic-gate case BAD_ADDR: 13467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13477c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 13487c478bd9Sstevel@tonic-gate exit(1); 13497c478bd9Sstevel@tonic-gate default: 13507c478bd9Sstevel@tonic-gate break; 13517c478bd9Sstevel@tonic-gate } 13527c478bd9Sstevel@tonic-gate 13537c478bd9Sstevel@tonic-gate lifr.lifr_addr = subnet; 13547c478bd9Sstevel@tonic-gate lifr.lifr_addrlen = prefixlen; 13557c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13567c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0) 13577c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFSUBNET"); 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate return (0); 13607c478bd9Sstevel@tonic-gate } 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13637c478bd9Sstevel@tonic-gate static int 13647c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param) 13657c478bd9Sstevel@tonic-gate { 13667c478bd9Sstevel@tonic-gate struct sockaddr_in netmask; 13677c478bd9Sstevel@tonic-gate 13687c478bd9Sstevel@tonic-gate assert(afp->af_af != AF_INET6); 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate if (strcmp(addr, "+") == 0) { 1371dd7a6f5fSkcpoon if (!in_getmask(&netmask, _B_FALSE)) 13727c478bd9Sstevel@tonic-gate return (0); 1373dd7a6f5fSkcpoon (void) printf("Setting netmask of %s to %s\n", name, 1374dd7a6f5fSkcpoon inet_ntoa(netmask.sin_addr)); 13757c478bd9Sstevel@tonic-gate } else { 13767c478bd9Sstevel@tonic-gate in_getaddr(addr, (struct sockaddr *)&netmask, NULL); 13777c478bd9Sstevel@tonic-gate } 13787c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 13797c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask)); 13807c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 13817c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 13827c478bd9Sstevel@tonic-gate return (0); 13837c478bd9Sstevel@tonic-gate } 13847c478bd9Sstevel@tonic-gate 13857c478bd9Sstevel@tonic-gate /* 13867c478bd9Sstevel@tonic-gate * Parse '/<n>' as a netmask. 13877c478bd9Sstevel@tonic-gate */ 13887c478bd9Sstevel@tonic-gate /* ARGSUSED */ 13897c478bd9Sstevel@tonic-gate static int 13907c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param) 13917c478bd9Sstevel@tonic-gate { 13927c478bd9Sstevel@tonic-gate int prefixlen; 13937c478bd9Sstevel@tonic-gate int af = afp->af_af; 13947c478bd9Sstevel@tonic-gate 13957c478bd9Sstevel@tonic-gate prefixlen = in_getprefixlen(addr, _B_TRUE, 13967906a3e0Smeem (af == AF_INET) ? IP_ABITS : IPV6_ABITS); 13977c478bd9Sstevel@tonic-gate if (prefixlen < 0) { 13987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13997c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 14007c478bd9Sstevel@tonic-gate exit(1); 14017c478bd9Sstevel@tonic-gate } 14027c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 14037c478bd9Sstevel@tonic-gate lifr.lifr_addr.ss_family = af; 14047c478bd9Sstevel@tonic-gate if (af == AF_INET6) { 14057c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 14067c478bd9Sstevel@tonic-gate 14077c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 14087906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 14097c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 14107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 14117c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 14127c478bd9Sstevel@tonic-gate prefixlen); 14137c478bd9Sstevel@tonic-gate exit(1); 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate } else if (af == AF_INET) { 14167c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 14177c478bd9Sstevel@tonic-gate 14187c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 14197906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 14207c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 14217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 14227c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 14237c478bd9Sstevel@tonic-gate prefixlen); 14247c478bd9Sstevel@tonic-gate exit(1); 14257c478bd9Sstevel@tonic-gate } 14267c478bd9Sstevel@tonic-gate } else { 14277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: setting prefix only supported" 14287c478bd9Sstevel@tonic-gate " for address family inet or inet6\n"); 14297c478bd9Sstevel@tonic-gate exit(1); 14307c478bd9Sstevel@tonic-gate } 14317c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14327c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 14337c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFNETMASK"); 14347c478bd9Sstevel@tonic-gate return (0); 14357c478bd9Sstevel@tonic-gate } 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14387c478bd9Sstevel@tonic-gate static int 14397c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param) 14407c478bd9Sstevel@tonic-gate { 14417c478bd9Sstevel@tonic-gate struct sockaddr_in broadaddr; 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate assert(afp->af_af != AF_INET6); 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate if (strcmp(addr, "+") == 0) { 14467c478bd9Sstevel@tonic-gate /* 14477c478bd9Sstevel@tonic-gate * This doesn't set the broadcast address at all. Rather, it 14487c478bd9Sstevel@tonic-gate * gets, then sets the interface's address, relying on the fact 14497c478bd9Sstevel@tonic-gate * that resetting the address will reset the broadcast address. 14507c478bd9Sstevel@tonic-gate */ 14517c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, 14527c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 14537c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 14547c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 14557c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFADDR"); 14567c478bd9Sstevel@tonic-gate return (0); 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 14597c478bd9Sstevel@tonic-gate Perror0_exit("SIOCGLIFADDR"); 14607c478bd9Sstevel@tonic-gate 14617c478bd9Sstevel@tonic-gate return (0); 14627c478bd9Sstevel@tonic-gate } 14637c478bd9Sstevel@tonic-gate in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL); 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr)); 14667c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14677c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0) 14687c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFBRDADDR"); 14697c478bd9Sstevel@tonic-gate return (0); 14707c478bd9Sstevel@tonic-gate } 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate /* 14737c478bd9Sstevel@tonic-gate * set interface destination address 14747c478bd9Sstevel@tonic-gate */ 14757c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14767c478bd9Sstevel@tonic-gate static int 14777c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param) 14787c478bd9Sstevel@tonic-gate { 14797c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL); 14807c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0) 14827c478bd9Sstevel@tonic-gate Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR"); 14837c478bd9Sstevel@tonic-gate return (0); 14847c478bd9Sstevel@tonic-gate } 14857c478bd9Sstevel@tonic-gate 14867c478bd9Sstevel@tonic-gate /* ARGSUSED */ 14877c478bd9Sstevel@tonic-gate static int 14887c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value) 14897c478bd9Sstevel@tonic-gate { 1490e11c3f44Smeem struct lifreq lifrl; /* local lifreq struct */ 1491e11c3f44Smeem boolean_t bringup = _B_FALSE; 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 14947c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) 14957c478bd9Sstevel@tonic-gate Perror0_exit("setifflags: SIOCGLIFFLAGS"); 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate if (value < 0) { 14987c478bd9Sstevel@tonic-gate value = -value; 1499e11c3f44Smeem 1500e11c3f44Smeem if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) { 1501e11c3f44Smeem /* 1502e11c3f44Smeem * The kernel does not allow administratively up test 1503e11c3f44Smeem * addresses to be converted to data addresses. Bring 1504e11c3f44Smeem * the address down first, then bring it up after it's 1505e11c3f44Smeem * been converted to a data address. 1506e11c3f44Smeem */ 1507e11c3f44Smeem lifr.lifr_flags &= ~IFF_UP; 1508e11c3f44Smeem (void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr); 1509e11c3f44Smeem bringup = _B_TRUE; 1510e11c3f44Smeem } 1511e11c3f44Smeem 15127c478bd9Sstevel@tonic-gate lifr.lifr_flags &= ~value; 1513e11c3f44Smeem if ((value & (IFF_UP | IFF_NOFAILOVER)) && 1514e11c3f44Smeem (lifr.lifr_flags & IFF_DUPLICATE)) { 151569bb4bb4Scarlsonj /* 151669bb4bb4Scarlsonj * If the user is trying to mark an interface with a 1517e11c3f44Smeem * duplicate address as "down," or convert a duplicate 1518e11c3f44Smeem * test address to a data address, then fetch the 1519e11c3f44Smeem * address and set it. This will cause IP to clear 1520e11c3f44Smeem * the IFF_DUPLICATE flag and stop the automatic 1521e11c3f44Smeem * recovery timer. 152269bb4bb4Scarlsonj */ 152369bb4bb4Scarlsonj value = lifr.lifr_flags; 152469bb4bb4Scarlsonj if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) 152569bb4bb4Scarlsonj (void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr); 152669bb4bb4Scarlsonj lifr.lifr_flags = value; 152769bb4bb4Scarlsonj } 152869bb4bb4Scarlsonj } else { 15297c478bd9Sstevel@tonic-gate lifr.lifr_flags |= value; 153069bb4bb4Scarlsonj } 1531e11c3f44Smeem 1532e11c3f44Smeem /* 1533e11c3f44Smeem * If we're about to bring up an underlying physical IPv6 interface in 1534e11c3f44Smeem * an IPMP group, ensure the IPv6 IPMP interface is also up. This is 1535e11c3f44Smeem * for backward compatibility with legacy configurations in which 1536e11c3f44Smeem * there are no explicit hostname files for IPMP interfaces. (For 1537e11c3f44Smeem * IPv4, this is automatically handled by the kernel when migrating 1538e11c3f44Smeem * the underlying interface's data address to the IPMP interface.) 1539e11c3f44Smeem */ 1540e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 1541e11c3f44Smeem 1542e11c3f44Smeem if (lifnum(lifr.lifr_name) == 0 && 1543e11c3f44Smeem (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) && 1544e11c3f44Smeem ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 && 1545e11c3f44Smeem lifrl.lifr_groupname[0] != '\0') { 1546e11c3f44Smeem lifgroupinfo_t lifgr; 1547e11c3f44Smeem 1548e11c3f44Smeem (void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname, 1549e11c3f44Smeem LIFGRNAMSIZ); 1550e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) 1551e11c3f44Smeem Perror0_exit("setifflags: SIOCGLIFGROUPINFO"); 1552e11c3f44Smeem 1553e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ); 1554e11c3f44Smeem if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1) 1555e11c3f44Smeem Perror0_exit("setifflags: SIOCGLIFFLAGS"); 1556e11c3f44Smeem if (!(lifrl.lifr_flags & IFF_UP)) { 1557e11c3f44Smeem lifrl.lifr_flags |= IFF_UP; 1558e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1) 1559e11c3f44Smeem Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1560e11c3f44Smeem } 1561e11c3f44Smeem } 1562e11c3f44Smeem 15637c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1564e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 15657c478bd9Sstevel@tonic-gate Perror0_exit("setifflags: SIOCSLIFFLAGS"); 1566e11c3f44Smeem 1567e11c3f44Smeem if (bringup) { 1568e11c3f44Smeem lifr.lifr_flags |= IFF_UP; 1569e11c3f44Smeem if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) 1570e11c3f44Smeem Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP"); 15717c478bd9Sstevel@tonic-gate } 1572e11c3f44Smeem 15737c478bd9Sstevel@tonic-gate return (0); 15747c478bd9Sstevel@tonic-gate } 15757c478bd9Sstevel@tonic-gate 15767c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15777c478bd9Sstevel@tonic-gate static int 15787c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param) 15797c478bd9Sstevel@tonic-gate { 15807c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 15817c478bd9Sstevel@tonic-gate lifr.lifr_metric = atoi(val); 15827c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0) 15837c478bd9Sstevel@tonic-gate Perror0_exit("setifmetric: SIOCSLIFMETRIC"); 15847c478bd9Sstevel@tonic-gate return (0); 15857c478bd9Sstevel@tonic-gate } 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15887c478bd9Sstevel@tonic-gate static int 15897c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param) 15907c478bd9Sstevel@tonic-gate { 15917c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 15927c478bd9Sstevel@tonic-gate lifr.lifr_mtu = atoi(val); 15937c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0) 15947c478bd9Sstevel@tonic-gate Perror0_exit("setifmtu: SIOCSLIFMTU"); 15957c478bd9Sstevel@tonic-gate return (0); 15967c478bd9Sstevel@tonic-gate } 15977c478bd9Sstevel@tonic-gate 15987c478bd9Sstevel@tonic-gate /* ARGSUSED */ 15997c478bd9Sstevel@tonic-gate static int 16007c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param) 16017c478bd9Sstevel@tonic-gate { 16027c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 16037c478bd9Sstevel@tonic-gate lifr.lifr_index = atoi(val); 16047c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0) 16057c478bd9Sstevel@tonic-gate Perror0_exit("setifindex: SIOCSLIFINDEX"); 16067c478bd9Sstevel@tonic-gate return (0); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate 1609e11c3f44Smeem /* ARGSUSED */ 1610e11c3f44Smeem static void 1611e11c3f44Smeem notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg) 1612e11c3f44Smeem { 1613e11c3f44Smeem } 1614e11c3f44Smeem 16157c478bd9Sstevel@tonic-gate /* ARGSUSED */ 16167c478bd9Sstevel@tonic-gate static int 16177c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param) 16187c478bd9Sstevel@tonic-gate { 1619e11c3f44Smeem uchar_t *hwaddr; 1620e11c3f44Smeem int hwaddrlen; 1621e11c3f44Smeem int retval; 1622e11c3f44Smeem ifaddrlistx_t *ifaddrp, *ifaddrs = NULL; 1623e11c3f44Smeem dlpi_handle_t dh; 1624e11c3f44Smeem dlpi_notifyid_t id; 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate if (addr == NULL) { 16277c478bd9Sstevel@tonic-gate ifstatus(name); 16287c478bd9Sstevel@tonic-gate print_ifether(name); 16297c478bd9Sstevel@tonic-gate return (0); 16307c478bd9Sstevel@tonic-gate } 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate /* 16337c478bd9Sstevel@tonic-gate * if the IP interface in the arguments is a logical 16347c478bd9Sstevel@tonic-gate * interface, exit with an error now. 16357c478bd9Sstevel@tonic-gate */ 16367c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 16377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: cannot change" 16387c478bd9Sstevel@tonic-gate " ethernet address of a logical interface\n"); 16397c478bd9Sstevel@tonic-gate exit(1); 16407c478bd9Sstevel@tonic-gate } 16417c478bd9Sstevel@tonic-gate 1642e11c3f44Smeem if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) { 1643e11c3f44Smeem if (hwaddrlen == -1) 16447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 16456e91bba0SGirish Moodalbail "ifconfig: bad ethernet address\n"); 16467c478bd9Sstevel@tonic-gate else 16477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: malloc() failed\n"); 16487c478bd9Sstevel@tonic-gate exit(1); 16497c478bd9Sstevel@tonic-gate } 16507c478bd9Sstevel@tonic-gate 1651e11c3f44Smeem if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS) 1652e11c3f44Smeem Perrdlpi_exit("cannot dlpi_open() link", name, retval); 16537c478bd9Sstevel@tonic-gate 1654e11c3f44Smeem retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id); 1655e11c3f44Smeem if (retval == DLPI_SUCCESS) { 1656e11c3f44Smeem (void) dlpi_disabnotify(dh, id, NULL); 1657e11c3f44Smeem } else { 1658e11c3f44Smeem /* 1659e11c3f44Smeem * This link does not support DL_NOTE_PHYS_ADDR: bring down 1660e11c3f44Smeem * all of the addresses to flush the old hardware address 1661e11c3f44Smeem * information out of IP. 1662e11c3f44Smeem * 1663e11c3f44Smeem * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is 1664e11c3f44Smeem * more than an optimization: in.mpathd will set IFF_OFFLINE 1665e11c3f44Smeem * if it's notified and the new address is a duplicate of 1666e11c3f44Smeem * another in the group -- but the flags manipulation in 1667e11c3f44Smeem * ifaddr_{down,up}() cannot be atomic and thus might clobber 1668e11c3f44Smeem * IFF_OFFLINE, confusing in.mpathd. 1669e11c3f44Smeem */ 1670e11c3f44Smeem if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1) 1671e11c3f44Smeem Perror2_exit(name, "cannot get address list"); 1672e11c3f44Smeem 1673e11c3f44Smeem ifaddrp = ifaddrs; 1674e11c3f44Smeem for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1675e11c3f44Smeem if (!ifaddr_down(ifaddrp)) { 1676e11c3f44Smeem Perror2_exit(ifaddrp->ia_name, 1677e11c3f44Smeem "cannot bring down"); 1678e11c3f44Smeem } 1679e11c3f44Smeem } 16807c478bd9Sstevel@tonic-gate } 16817c478bd9Sstevel@tonic-gate 16827c478bd9Sstevel@tonic-gate /* 1683e11c3f44Smeem * Change the hardware address. 16847c478bd9Sstevel@tonic-gate */ 1685e11c3f44Smeem retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen); 1686e11c3f44Smeem if (retval != DLPI_SUCCESS) { 16877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1688e11c3f44Smeem "ifconfig: failed setting mac address on %s\n", name); 16897c478bd9Sstevel@tonic-gate } 1690e11c3f44Smeem dlpi_close(dh); 16917c478bd9Sstevel@tonic-gate 16927c478bd9Sstevel@tonic-gate /* 1693e11c3f44Smeem * If any addresses were brought down before changing the hardware 1694e11c3f44Smeem * address, bring them up again. 16957c478bd9Sstevel@tonic-gate */ 1696e11c3f44Smeem for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 1697e11c3f44Smeem if (!ifaddr_up(ifaddrp)) 1698e11c3f44Smeem Perror2_exit(ifaddrp->ia_name, "cannot bring up"); 16997c478bd9Sstevel@tonic-gate } 1700e11c3f44Smeem ifaddrlistx_free(ifaddrs); 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate return (0); 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate /* 17067c478bd9Sstevel@tonic-gate * Print an interface's Ethernet address, if it has one. 17077c478bd9Sstevel@tonic-gate */ 17087c478bd9Sstevel@tonic-gate static void 17096e91bba0SGirish Moodalbail print_ifether(const char *ifname) 17107c478bd9Sstevel@tonic-gate { 17112b24ab6bSSebastien Roy int fd; 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 17147c478bd9Sstevel@tonic-gate 17157c478bd9Sstevel@tonic-gate fd = socket(AF_INET, SOCK_DGRAM, 0); 17167c478bd9Sstevel@tonic-gate if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 17177c478bd9Sstevel@tonic-gate /* 17187c478bd9Sstevel@tonic-gate * It's possible the interface is only configured for 17197c478bd9Sstevel@tonic-gate * IPv6; check again with AF_INET6. 17207c478bd9Sstevel@tonic-gate */ 17217c478bd9Sstevel@tonic-gate (void) close(fd); 17227c478bd9Sstevel@tonic-gate fd = socket(AF_INET6, SOCK_DGRAM, 0); 17237c478bd9Sstevel@tonic-gate if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 17247c478bd9Sstevel@tonic-gate (void) close(fd); 17257c478bd9Sstevel@tonic-gate return; 17267c478bd9Sstevel@tonic-gate } 17277c478bd9Sstevel@tonic-gate } 17287c478bd9Sstevel@tonic-gate (void) close(fd); 17297c478bd9Sstevel@tonic-gate 1730e11c3f44Smeem /* VNI and IPMP interfaces don't have MAC addresses */ 1731e11c3f44Smeem if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP)) 17327c478bd9Sstevel@tonic-gate return; 17337c478bd9Sstevel@tonic-gate 17342b24ab6bSSebastien Roy /* IP tunnels also don't have Ethernet-like MAC addresses */ 17352b24ab6bSSebastien Roy if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) == 17362b24ab6bSSebastien Roy DLADM_STATUS_OK) 17372b24ab6bSSebastien Roy return; 17387c478bd9Sstevel@tonic-gate 17397c478bd9Sstevel@tonic-gate dlpi_print_address(ifname); 17407c478bd9Sstevel@tonic-gate } 17417c478bd9Sstevel@tonic-gate 1742f4b3ec61Sdh /* 17432b24ab6bSSebastien Roy * static int find_all_interfaces(struct lifconf *lifcp, char **buf, 1744f4b3ec61Sdh * int64_t lifc_flags) 1745f4b3ec61Sdh * 17462b24ab6bSSebastien Roy * It finds all active data links. 1747f4b3ec61Sdh * 1748f4b3ec61Sdh * It takes in input a pointer to struct lifconf to receive interfaces 1749f4b3ec61Sdh * informations, a **char to hold allocated buffer, and a lifc_flags. 1750f4b3ec61Sdh * 1751f4b3ec61Sdh * Return values: 1752f4b3ec61Sdh * 0 = everything OK 1753f4b3ec61Sdh * -1 = problem 1754f4b3ec61Sdh */ 1755f4b3ec61Sdh static int 17562b24ab6bSSebastien Roy find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags) 1757f4b3ec61Sdh { 1758f4b3ec61Sdh unsigned bufsize; 1759f4b3ec61Sdh int n; 1760f4b3ec61Sdh ni_t *nip; 1761f4b3ec61Sdh struct lifreq *lifrp; 1762d4d1f7bfSVasumathi Sundaram - Sun Microsystems dladm_status_t status; 1763f4b3ec61Sdh 17642b24ab6bSSebastien Roy if (!dlh_opened) { 17652b24ab6bSSebastien Roy status = ifconfig_dladm_open(NULL, 0, NULL); 17662b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 17672b24ab6bSSebastien Roy dladmerr_exit(status, "unable to open dladm handle"); 1768d4d1f7bfSVasumathi Sundaram - Sun Microsystems } 1769d4d1f7bfSVasumathi Sundaram - Sun Microsystems 17702b24ab6bSSebastien Roy (void) dlpi_walk(ni_entry, dlh, 0); 1771f4b3ec61Sdh 17722b24ab6bSSebastien Roy /* Now, translate the linked list into a struct lifreq buffer */ 1773f4b3ec61Sdh if (num_ni == 0) { 1774f4b3ec61Sdh lifcp->lifc_family = AF_UNSPEC; 1775f4b3ec61Sdh lifcp->lifc_flags = lifc_flags; 1776f4b3ec61Sdh lifcp->lifc_len = 0; 1777f4b3ec61Sdh lifcp->lifc_buf = NULL; 1778f4b3ec61Sdh return (0); 1779f4b3ec61Sdh } 1780f4b3ec61Sdh 1781f4b3ec61Sdh bufsize = num_ni * sizeof (struct lifreq); 1782f4b3ec61Sdh if ((*buf = malloc(bufsize)) == NULL) 1783f4b3ec61Sdh Perror0_exit("find_all_interfaces: malloc failed"); 1784f4b3ec61Sdh 1785f4b3ec61Sdh lifcp->lifc_family = AF_UNSPEC; 1786f4b3ec61Sdh lifcp->lifc_flags = lifc_flags; 1787f4b3ec61Sdh lifcp->lifc_len = bufsize; 1788f4b3ec61Sdh lifcp->lifc_buf = *buf; 1789f4b3ec61Sdh 1790f4b3ec61Sdh for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) { 1791f4b3ec61Sdh nip = ni_list; 1792f4b3ec61Sdh (void) strncpy(lifrp->lifr_name, nip->ni_name, 1793f4b3ec61Sdh sizeof (lifr.lifr_name)); 1794f4b3ec61Sdh ni_list = nip->ni_next; 1795f4b3ec61Sdh free(nip); 1796f4b3ec61Sdh } 1797f4b3ec61Sdh return (0); 1798f4b3ec61Sdh } 1799f4b3ec61Sdh 18007c478bd9Sstevel@tonic-gate /* 18017c478bd9Sstevel@tonic-gate * Create the next unused logical interface using the original name 18027c478bd9Sstevel@tonic-gate * and assign the address (and mask if '/<n>' is part of the address). 18037c478bd9Sstevel@tonic-gate * Use the new logical interface for subsequent subcommands by updating 18047c478bd9Sstevel@tonic-gate * the name variable. 18057c478bd9Sstevel@tonic-gate * 18067c478bd9Sstevel@tonic-gate * This allows syntax like: 18077c478bd9Sstevel@tonic-gate * ifconfig le0 addif 109.106.86.130 netmask + up \ 18087c478bd9Sstevel@tonic-gate * addif 109.106.86.131 netmask + up 18097c478bd9Sstevel@tonic-gate */ 18107c478bd9Sstevel@tonic-gate /* ARGSUSED */ 18117c478bd9Sstevel@tonic-gate static int 18127c478bd9Sstevel@tonic-gate addif(char *str, int64_t param) 18137c478bd9Sstevel@tonic-gate { 18147c478bd9Sstevel@tonic-gate int prefixlen = 0; 18157c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 18167c478bd9Sstevel@tonic-gate struct sockaddr_storage mask; 18173166459dSVasumathi Sundaram struct sockaddr_in6 *sin6; 18183166459dSVasumathi Sundaram struct sockaddr_in *sin; 18196e91bba0SGirish Moodalbail ipadm_status_t istatus; 18206e91bba0SGirish Moodalbail char cidraddr[BUFSIZ]; 18213166459dSVasumathi Sundaram char addrstr[INET6_ADDRSTRLEN]; 18227c478bd9Sstevel@tonic-gate 18237c478bd9Sstevel@tonic-gate (void) strncpy(name, origname, sizeof (name)); 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 18267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18277c478bd9Sstevel@tonic-gate "ifconfig: addif: bad physical interface name %s\n", 18287c478bd9Sstevel@tonic-gate name); 18297c478bd9Sstevel@tonic-gate exit(1); 18307c478bd9Sstevel@tonic-gate } 18317c478bd9Sstevel@tonic-gate 18327c478bd9Sstevel@tonic-gate /* 18337c478bd9Sstevel@tonic-gate * clear so parser will interpret next address as source followed 18347c478bd9Sstevel@tonic-gate * by possible dest 18357c478bd9Sstevel@tonic-gate */ 18367c478bd9Sstevel@tonic-gate setaddr = 0; 18377c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen); 18387c478bd9Sstevel@tonic-gate 18397c478bd9Sstevel@tonic-gate switch (prefixlen) { 18407c478bd9Sstevel@tonic-gate case NO_PREFIX: 18417c478bd9Sstevel@tonic-gate /* Nothing there - ok */ 18427c478bd9Sstevel@tonic-gate break; 18437c478bd9Sstevel@tonic-gate case BAD_ADDR: 18447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18457c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", str); 18467c478bd9Sstevel@tonic-gate exit(1); 18477c478bd9Sstevel@tonic-gate default: 18487c478bd9Sstevel@tonic-gate (void) memset(&mask, 0, sizeof (mask)); 18497c478bd9Sstevel@tonic-gate mask.ss_family = afp->af_af; 18507c478bd9Sstevel@tonic-gate if (afp->af_af == AF_INET6) { 18517c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&mask; 18527906a3e0Smeem if (!in_prefixlentomask(prefixlen, IPV6_ABITS, 18537c478bd9Sstevel@tonic-gate (uchar_t *)&sin6->sin6_addr)) { 18547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 18557c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 18567c478bd9Sstevel@tonic-gate prefixlen); 18577c478bd9Sstevel@tonic-gate exit(1); 18587c478bd9Sstevel@tonic-gate } 18597c478bd9Sstevel@tonic-gate } else { 18607c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&mask; 18617906a3e0Smeem if (!in_prefixlentomask(prefixlen, IP_ABITS, 18627c478bd9Sstevel@tonic-gate (uchar_t *)&sin->sin_addr)) { 18637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: " 18647c478bd9Sstevel@tonic-gate "Bad prefix length: %d\n", 18657c478bd9Sstevel@tonic-gate prefixlen); 18667c478bd9Sstevel@tonic-gate exit(1); 18677c478bd9Sstevel@tonic-gate } 18687c478bd9Sstevel@tonic-gate } 1869dd7a6f5fSkcpoon g_netmask_set = G_NETMASK_NIL; 18707c478bd9Sstevel@tonic-gate break; 18717c478bd9Sstevel@tonic-gate } 18727c478bd9Sstevel@tonic-gate 1873dd7a6f5fSkcpoon /* 1874dd7a6f5fSkcpoon * This is a "hack" to get around the problem of SIOCLIFADDIF. The 1875dd7a6f5fSkcpoon * problem is that this ioctl does not include the netmask when 1876dd7a6f5fSkcpoon * adding a logical interface. This is the same problem described 1877dd7a6f5fSkcpoon * in the ifconfig() comments. To get around this problem, we first 1878dd7a6f5fSkcpoon * add the logical interface with a 0 address. After that, we set 1879dd7a6f5fSkcpoon * the netmask if provided. Finally we set the interface address. 1880dd7a6f5fSkcpoon */ 18817c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 1882dd7a6f5fSkcpoon (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate /* Note: no need to do DAD here since the interface isn't up yet. */ 18857c478bd9Sstevel@tonic-gate 18867c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) 18877c478bd9Sstevel@tonic-gate Perror0_exit("addif: SIOCLIFADDIF"); 18887c478bd9Sstevel@tonic-gate 18897c478bd9Sstevel@tonic-gate (void) printf("Created new logical interface %s\n", 18907c478bd9Sstevel@tonic-gate lifr.lifr_name); 18917c478bd9Sstevel@tonic-gate (void) strncpy(name, lifr.lifr_name, sizeof (name)); 18927c478bd9Sstevel@tonic-gate 1893dd7a6f5fSkcpoon /* 1894dd7a6f5fSkcpoon * Check and see if any "netmask" command is used and perform the 1895dd7a6f5fSkcpoon * necessary operation. 1896dd7a6f5fSkcpoon */ 1897dd7a6f5fSkcpoon set_mask_lifreq(&lifr, &laddr, &mask); 18986e91bba0SGirish Moodalbail 18996e91bba0SGirish Moodalbail /* This check is temporary until libipadm supports IPMP interfaces. */ 19006e91bba0SGirish Moodalbail if (ifconfig_use_libipadm(s, name)) { 19016e91bba0SGirish Moodalbail /* 19026e91bba0SGirish Moodalbail * We added the logical interface above before calling 19036e91bba0SGirish Moodalbail * ipadm_create_addr(), because, with IPH_LEGACY, we need 19046e91bba0SGirish Moodalbail * to do an addif for `ifconfig ce0 addif <addr>' but not for 19056e91bba0SGirish Moodalbail * `ifconfig ce0 <addr>'. libipadm does not have a flag to 19066e91bba0SGirish Moodalbail * to differentiate between these two cases. To keep it simple, 19076e91bba0SGirish Moodalbail * we always create the logical interface and pass it to 19086e91bba0SGirish Moodalbail * libipadm instead of requiring libipadm to addif for some 19096e91bba0SGirish Moodalbail * cases and not do addif for other cases. 19106e91bba0SGirish Moodalbail */ 19116e91bba0SGirish Moodalbail istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name, 19126e91bba0SGirish Moodalbail &ipaddr); 19136e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 19146e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "addif"); 19156e91bba0SGirish Moodalbail 19163166459dSVasumathi Sundaram if (af == AF_INET) { 19173166459dSVasumathi Sundaram sin = (struct sockaddr_in *)&laddr; 19183166459dSVasumathi Sundaram (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr, 19193166459dSVasumathi Sundaram sizeof (addrstr)); 19203166459dSVasumathi Sundaram } else { 19213166459dSVasumathi Sundaram sin6 = (struct sockaddr_in6 *)&laddr; 19223166459dSVasumathi Sundaram (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr, 19233166459dSVasumathi Sundaram sizeof (addrstr)); 19246e91bba0SGirish Moodalbail } 19253166459dSVasumathi Sundaram /* 19263166459dSVasumathi Sundaram * lifr.lifr_addr, which is updated by set_mask_lifreq() 19273166459dSVasumathi Sundaram * will contain the right mask to use. 19283166459dSVasumathi Sundaram */ 192964639aafSDarren Reed prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr); 193064639aafSDarren Reed 19313166459dSVasumathi Sundaram (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d", 19323166459dSVasumathi Sundaram addrstr, prefixlen); 19333166459dSVasumathi Sundaram 19343166459dSVasumathi Sundaram istatus = ipadm_set_addr(ipaddr, cidraddr, af); 19353166459dSVasumathi Sundaram 19366e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 19376e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "could not set address"); 19386e91bba0SGirish Moodalbail setaddr++; 19396e91bba0SGirish Moodalbail /* 19406e91bba0SGirish Moodalbail * address will be set by the parser after nextarg 19416e91bba0SGirish Moodalbail * has been scanned 19426e91bba0SGirish Moodalbail */ 19436e91bba0SGirish Moodalbail return (0); 19446e91bba0SGirish Moodalbail } 19456e91bba0SGirish Moodalbail 1946dd7a6f5fSkcpoon /* 1947dd7a6f5fSkcpoon * Only set the netmask if "netmask" command is used or a prefix is 1948dd7a6f5fSkcpoon * provided. 1949dd7a6f5fSkcpoon */ 1950dd7a6f5fSkcpoon if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) { 19516e91bba0SGirish Moodalbail /* 19526e91bba0SGirish Moodalbail * lifr.lifr_addr already contains netmask from 19536e91bba0SGirish Moodalbail * set_mask_lifreq(). 19546e91bba0SGirish Moodalbail */ 19557c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) 19567c478bd9Sstevel@tonic-gate Perror0_exit("addif: SIOCSLIFNETMASK"); 19577c478bd9Sstevel@tonic-gate } 1958dd7a6f5fSkcpoon 1959dd7a6f5fSkcpoon /* Finally, we set the interface address. */ 1960dd7a6f5fSkcpoon lifr.lifr_addr = laddr; 1961dd7a6f5fSkcpoon if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) 1962dd7a6f5fSkcpoon Perror0_exit("SIOCSLIFADDR"); 1963dd7a6f5fSkcpoon 19647c478bd9Sstevel@tonic-gate /* 19657c478bd9Sstevel@tonic-gate * let parser know we got a source. 19667c478bd9Sstevel@tonic-gate * Next address, if given, should be dest 19677c478bd9Sstevel@tonic-gate */ 19687c478bd9Sstevel@tonic-gate setaddr++; 19697c478bd9Sstevel@tonic-gate return (0); 19707c478bd9Sstevel@tonic-gate } 19717c478bd9Sstevel@tonic-gate 19727c478bd9Sstevel@tonic-gate /* 19737c478bd9Sstevel@tonic-gate * Remove a logical interface based on its IP address. Unlike addif 19747c478bd9Sstevel@tonic-gate * there is no '/<n>' here. 19757c478bd9Sstevel@tonic-gate * Verifies that the interface is down before it is removed. 19767c478bd9Sstevel@tonic-gate */ 19777c478bd9Sstevel@tonic-gate /* ARGSUSED */ 19787c478bd9Sstevel@tonic-gate static int 19797c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param) 19807c478bd9Sstevel@tonic-gate { 19817c478bd9Sstevel@tonic-gate struct sockaddr_storage laddr; 19826e91bba0SGirish Moodalbail ipadm_status_t istatus; 19836e91bba0SGirish Moodalbail ipadm_addr_info_t *ainfo, *ainfop; 19847c478bd9Sstevel@tonic-gate 19857c478bd9Sstevel@tonic-gate if (strchr(name, ':') != NULL) { 19867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19877c478bd9Sstevel@tonic-gate "ifconfig: removeif: bad physical interface name %s\n", 19887c478bd9Sstevel@tonic-gate name); 19897c478bd9Sstevel@tonic-gate exit(1); 19907c478bd9Sstevel@tonic-gate } 19917c478bd9Sstevel@tonic-gate 19927c478bd9Sstevel@tonic-gate (*afp->af_getaddr)(str, &laddr, NULL); 19937c478bd9Sstevel@tonic-gate 19946e91bba0SGirish Moodalbail /* 19956e91bba0SGirish Moodalbail * Following check is temporary until libipadm supports 19966e91bba0SGirish Moodalbail * IPMP interfaces. 19976e91bba0SGirish Moodalbail */ 19986e91bba0SGirish Moodalbail if (!ifconfig_use_libipadm(s, name)) 19996e91bba0SGirish Moodalbail goto delete; 20006e91bba0SGirish Moodalbail 20016e91bba0SGirish Moodalbail /* 20026e91bba0SGirish Moodalbail * Get all addresses and search this address among the active 20036e91bba0SGirish Moodalbail * addresses. If an address object was found, delete using 20046e91bba0SGirish Moodalbail * ipadm_delete_addr(). 20056e91bba0SGirish Moodalbail */ 20066e91bba0SGirish Moodalbail istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT); 20076e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) 20086e91bba0SGirish Moodalbail ipadmerr_exit(istatus, "removeif"); 20096e91bba0SGirish Moodalbail 20106e91bba0SGirish Moodalbail for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) 201164639aafSDarren Reed if (sockaddrcmp( 201264639aafSDarren Reed (struct sockaddr_storage *)ainfop->ia_ifa.ifa_addr, &laddr)) 20136e91bba0SGirish Moodalbail break; 20146e91bba0SGirish Moodalbail 201567ce1608SVasumathi Sundaram if (ainfop != NULL) { 201667ce1608SVasumathi Sundaram if (strchr(ainfop->ia_ifa.ifa_name, ':') == NULL) { 201767ce1608SVasumathi Sundaram (void) fprintf(stderr, 201867ce1608SVasumathi Sundaram "ifconfig: removeif: cannot remove interface: %s\n", 201967ce1608SVasumathi Sundaram name); 202067ce1608SVasumathi Sundaram exit(1); 202167ce1608SVasumathi Sundaram } 202267ce1608SVasumathi Sundaram if (ainfop->ia_aobjname[0] != '\0') { 202367ce1608SVasumathi Sundaram istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname, 202467ce1608SVasumathi Sundaram IPADM_OPT_ACTIVE); 202567ce1608SVasumathi Sundaram if (istatus != IPADM_SUCCESS) { 202667ce1608SVasumathi Sundaram ipadmerr_exit(istatus, 202767ce1608SVasumathi Sundaram "could not delete address"); 202867ce1608SVasumathi Sundaram } 202967ce1608SVasumathi Sundaram ipadm_free_addr_info(ainfo); 203067ce1608SVasumathi Sundaram return (0); 203167ce1608SVasumathi Sundaram } 20326e91bba0SGirish Moodalbail } 20336e91bba0SGirish Moodalbail ipadm_free_addr_info(ainfo); 20346e91bba0SGirish Moodalbail 20356e91bba0SGirish Moodalbail delete: 20366e91bba0SGirish Moodalbail /* 20376e91bba0SGirish Moodalbail * An address object for this address was not found in ipadm. 20386e91bba0SGirish Moodalbail * Delete with SIOCLIFREMOVEIF. 20396e91bba0SGirish Moodalbail */ 20406e91bba0SGirish Moodalbail lifr.lifr_addr = laddr; 20417c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 20427c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) { 20437c478bd9Sstevel@tonic-gate if (errno == EBUSY) { 20447c478bd9Sstevel@tonic-gate /* This can only happen if ipif_id = 0 */ 20457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 204667ce1608SVasumathi Sundaram "ifconfig: removeif: cannot remove interface: %s\n", 20477c478bd9Sstevel@tonic-gate name); 20487c478bd9Sstevel@tonic-gate exit(1); 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate Perror0_exit("removeif: SIOCLIFREMOVEIF"); 20517c478bd9Sstevel@tonic-gate } 20527c478bd9Sstevel@tonic-gate return (0); 20537c478bd9Sstevel@tonic-gate } 20547c478bd9Sstevel@tonic-gate 20557c478bd9Sstevel@tonic-gate /* 20567c478bd9Sstevel@tonic-gate * Set the address token for IPv6. 20577c478bd9Sstevel@tonic-gate */ 20587c478bd9Sstevel@tonic-gate /* ARGSUSED */ 20597c478bd9Sstevel@tonic-gate static int 20607c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param) 20617c478bd9Sstevel@tonic-gate { 20627c478bd9Sstevel@tonic-gate int prefixlen = 0; 20637c478bd9Sstevel@tonic-gate struct sockaddr_in6 token; 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen); 20667c478bd9Sstevel@tonic-gate switch (prefixlen) { 20677c478bd9Sstevel@tonic-gate case NO_PREFIX: 20687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20697c478bd9Sstevel@tonic-gate "ifconfig: Missing prefix length in subnet %s\n", addr); 20707c478bd9Sstevel@tonic-gate exit(1); 20717c478bd9Sstevel@tonic-gate /* NOTREACHED */ 20727c478bd9Sstevel@tonic-gate case BAD_ADDR: 20737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 20747c478bd9Sstevel@tonic-gate "ifconfig: Bad prefix length in %s\n", addr); 20757c478bd9Sstevel@tonic-gate exit(1); 20767c478bd9Sstevel@tonic-gate default: 20777c478bd9Sstevel@tonic-gate break; 20787c478bd9Sstevel@tonic-gate } 20797c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &token, sizeof (token)); 20807c478bd9Sstevel@tonic-gate lifr.lifr_addrlen = prefixlen; 20817c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 20827c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0) { 20837c478bd9Sstevel@tonic-gate Perror0_exit("setiftoken: SIOCSLIFTOKEN"); 20847c478bd9Sstevel@tonic-gate } 20857c478bd9Sstevel@tonic-gate return (0); 20867c478bd9Sstevel@tonic-gate } 20877c478bd9Sstevel@tonic-gate 20887c478bd9Sstevel@tonic-gate /* ARGSUSED */ 20897c478bd9Sstevel@tonic-gate static int 2090e11c3f44Smeem setifgroupname(char *grname, int64_t param) 20917c478bd9Sstevel@tonic-gate { 2092e11c3f44Smeem lifgroupinfo_t lifgr; 2093e11c3f44Smeem struct lifreq lifrl; 2094e11c3f44Smeem ifaddrlistx_t *ifaddrp, *nextifaddrp; 2095e11c3f44Smeem ifaddrlistx_t *ifaddrs = NULL, *downaddrs = NULL; 2096e11c3f44Smeem int af; 2097e11c3f44Smeem 20987c478bd9Sstevel@tonic-gate if (debug) { 20997c478bd9Sstevel@tonic-gate (void) printf("Setting groupname %s on interface %s\n", 2100e11c3f44Smeem grname, name); 21017c478bd9Sstevel@tonic-gate } 21027c478bd9Sstevel@tonic-gate 2103e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ); 2104e11c3f44Smeem (void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ); 2105e11c3f44Smeem 2106e11c3f44Smeem while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2107e11c3f44Smeem switch (errno) { 2108e11c3f44Smeem case ENOENT: 2109e11c3f44Smeem /* 2110e11c3f44Smeem * The group doesn't yet exist; create it and repeat. 2111e11c3f44Smeem */ 2112e11c3f44Smeem af = afp->af_af; 2113e11c3f44Smeem if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) { 2114e11c3f44Smeem if (errno == EEXIST) 2115e11c3f44Smeem continue; 2116e11c3f44Smeem 2117e11c3f44Smeem Perror2(grname, "cannot create IPMP group"); 2118e11c3f44Smeem goto fail; 2119e11c3f44Smeem } 2120e11c3f44Smeem continue; 2121e11c3f44Smeem 2122e11c3f44Smeem case EALREADY: 2123e11c3f44Smeem /* 2124e11c3f44Smeem * The interface is already in another group; must 2125e11c3f44Smeem * remove existing membership first. 2126e11c3f44Smeem */ 2127e11c3f44Smeem lifrl.lifr_groupname[0] = '\0'; 2128e11c3f44Smeem if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) { 2129e11c3f44Smeem Perror2(name, "cannot remove existing " 2130e11c3f44Smeem "IPMP group membership"); 2131e11c3f44Smeem goto fail; 2132e11c3f44Smeem } 2133e11c3f44Smeem (void) strlcpy(lifrl.lifr_groupname, grname, 2134e11c3f44Smeem LIFGRNAMSIZ); 2135e11c3f44Smeem continue; 2136e11c3f44Smeem 2137e11c3f44Smeem case EAFNOSUPPORT: 2138e11c3f44Smeem /* 2139e11c3f44Smeem * The group exists, but it's not configured with the 2140e11c3f44Smeem * address families the interface needs. Since only 2141e11c3f44Smeem * two address families are currently supported, just 2142e11c3f44Smeem * configure the "other" address family. Note that we 2143e11c3f44Smeem * may race with group deletion or creation by another 2144e11c3f44Smeem * process (ENOENT or EEXIST); in such cases we repeat 2145e11c3f44Smeem * our original SIOCSLIFGROUPNAME. 2146e11c3f44Smeem */ 2147e11c3f44Smeem (void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ); 2148e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) { 2149e11c3f44Smeem if (errno == ENOENT) 2150e11c3f44Smeem continue; 2151e11c3f44Smeem 2152e11c3f44Smeem Perror2(grname, "SIOCGLIFGROUPINFO"); 2153e11c3f44Smeem goto fail; 2154e11c3f44Smeem } 2155e11c3f44Smeem 2156e11c3f44Smeem af = lifgr.gi_v4 ? AF_INET6 : AF_INET; 2157e11c3f44Smeem if (create_ipmp(grname, af, lifgr.gi_grifname, 2158e11c3f44Smeem _B_TRUE) == -1) { 2159e11c3f44Smeem if (errno == EEXIST) 2160e11c3f44Smeem continue; 2161e11c3f44Smeem 2162e11c3f44Smeem Perror2(grname, "cannot configure IPMP group"); 2163e11c3f44Smeem goto fail; 2164e11c3f44Smeem } 2165e11c3f44Smeem continue; 2166e11c3f44Smeem 2167e11c3f44Smeem case EADDRINUSE: 2168e11c3f44Smeem /* 2169e11c3f44Smeem * Some addresses are in-use (or under control of DAD). 2170e11c3f44Smeem * Bring them down and retry the group join operation. 2171e11c3f44Smeem * We will bring them back up after the interface has 2172e11c3f44Smeem * been placed in the group. 2173e11c3f44Smeem */ 2174e11c3f44Smeem if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE, 2175e11c3f44Smeem 0, &ifaddrs) == -1) { 2176e11c3f44Smeem Perror2(grname, "cannot get address list"); 2177e11c3f44Smeem goto fail; 2178e11c3f44Smeem } 2179e11c3f44Smeem 2180e11c3f44Smeem ifaddrp = ifaddrs; 2181e11c3f44Smeem for (; ifaddrp != NULL; ifaddrp = nextifaddrp) { 2182e11c3f44Smeem if (!ifaddr_down(ifaddrp)) { 2183e11c3f44Smeem ifaddrs = ifaddrp; 2184e11c3f44Smeem goto fail; 2185e11c3f44Smeem } 2186e11c3f44Smeem nextifaddrp = ifaddrp->ia_next; 2187e11c3f44Smeem ifaddrp->ia_next = downaddrs; 2188e11c3f44Smeem downaddrs = ifaddrp; 2189e11c3f44Smeem } 2190e11c3f44Smeem ifaddrs = NULL; 2191e11c3f44Smeem continue; 2192e11c3f44Smeem 2193e11c3f44Smeem case EADDRNOTAVAIL: { 2194e11c3f44Smeem /* 2195e11c3f44Smeem * Some data addresses are under application control. 2196e11c3f44Smeem * For some of these (e.g., ADDRCONF), the application 2197e11c3f44Smeem * should remove the address, in which case we retry a 2198e11c3f44Smeem * few times (since the application's action is not 2199e11c3f44Smeem * atomic with respect to us) before bailing out and 2200e11c3f44Smeem * informing the user. 2201e11c3f44Smeem */ 2202e11c3f44Smeem int ntries, nappaddr = 0; 2203e11c3f44Smeem const if_appflags_t *iap = if_appflags_tbl; 2204e11c3f44Smeem 2205e11c3f44Smeem for (; iap->ia_app != NULL; iap++) { 2206e11c3f44Smeem ntries = 0; 2207e11c3f44Smeem again: 2208e11c3f44Smeem if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag, 2209e11c3f44Smeem IFF_NOFAILOVER, &ifaddrs) == -1) { 2210e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: " 2211e11c3f44Smeem "cannot get data addresses managed " 2212e11c3f44Smeem "by %s\n", lifrl.lifr_name, 2213e11c3f44Smeem iap->ia_app); 2214e11c3f44Smeem goto fail; 2215e11c3f44Smeem } 2216e11c3f44Smeem 2217e11c3f44Smeem if (ifaddrs == NULL) 2218e11c3f44Smeem continue; 2219e11c3f44Smeem 2220e11c3f44Smeem ifaddrlistx_free(ifaddrs); 2221e11c3f44Smeem ifaddrs = NULL; 2222e11c3f44Smeem 2223e11c3f44Smeem if (++ntries < iap->ia_tries) { 2224e11c3f44Smeem (void) poll(NULL, 0, 100); 2225e11c3f44Smeem goto again; 2226e11c3f44Smeem } 2227e11c3f44Smeem 2228e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot join " 2229e11c3f44Smeem "IPMP group: %s has data addresses managed " 2230e11c3f44Smeem "by %s\n", lifrl.lifr_name, iap->ia_app); 2231e11c3f44Smeem nappaddr++; 2232e11c3f44Smeem } 2233e11c3f44Smeem if (nappaddr > 0) 2234e11c3f44Smeem goto fail; 2235e11c3f44Smeem continue; 2236e11c3f44Smeem } 2237e11c3f44Smeem default: 2238e11c3f44Smeem Perror2(name, "SIOCSLIFGROUPNAME"); 2239e11c3f44Smeem goto fail; 2240e11c3f44Smeem } 22417c478bd9Sstevel@tonic-gate } 22427c478bd9Sstevel@tonic-gate 224336b41818SGirish Moodalbail /* 224436b41818SGirish Moodalbail * If the interface being moved is under the control of `ipmgmtd(1M)' 224536b41818SGirish Moodalbail * dameon then we should inform the daemon about this move, so that 224636b41818SGirish Moodalbail * the daemon can delete the state associated with this interface. 224736b41818SGirish Moodalbail * 224836b41818SGirish Moodalbail * This workaround is needed until the IPMP support in ipadm(1M). 224936b41818SGirish Moodalbail */ 225036b41818SGirish Moodalbail ipadm_if_move(iph, name); 225136b41818SGirish Moodalbail 22527c478bd9Sstevel@tonic-gate /* 2253e11c3f44Smeem * If there were addresses that we had to bring down, it's time to 2254e11c3f44Smeem * bring them up again. As part of bringing them up, the kernel will 2255e11c3f44Smeem * automatically move them to the new IPMP interface. 22567c478bd9Sstevel@tonic-gate */ 2257e11c3f44Smeem for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2258e11c3f44Smeem if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2259e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot bring back up " 2260e11c3f44Smeem "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2261e11c3f44Smeem } 22627c478bd9Sstevel@tonic-gate } 2263e11c3f44Smeem ifaddrlistx_free(downaddrs); 2264e11c3f44Smeem return (0); 2265e11c3f44Smeem fail: 22667c478bd9Sstevel@tonic-gate /* 2267e11c3f44Smeem * Attempt to bring back up any interfaces that we downed. 22687c478bd9Sstevel@tonic-gate */ 2269e11c3f44Smeem for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) { 2270e11c3f44Smeem if (!ifaddr_up(ifaddrp) && errno != ENXIO) { 2271e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot bring back up " 2272e11c3f44Smeem "%s: %s\n", ifaddrp->ia_name, strerror(errno)); 2273e11c3f44Smeem } 22747c478bd9Sstevel@tonic-gate } 2275e11c3f44Smeem ifaddrlistx_free(downaddrs); 2276e11c3f44Smeem ifaddrlistx_free(ifaddrs); 22777c478bd9Sstevel@tonic-gate 22787c478bd9Sstevel@tonic-gate /* 2279e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2280e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 22817c478bd9Sstevel@tonic-gate */ 2282e11c3f44Smeem exit(1); 2283e11c3f44Smeem /* NOTREACHED */ 2284e11c3f44Smeem } 22857c478bd9Sstevel@tonic-gate 2286e11c3f44Smeem static boolean_t 2287e11c3f44Smeem modcheck(const char *ifname) 2288e11c3f44Smeem { 2289e11c3f44Smeem (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 2290e11c3f44Smeem 2291e11c3f44Smeem if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) { 2292e11c3f44Smeem Perror0("SIOCGLIFFLAGS"); 2293e11c3f44Smeem return (_B_FALSE); 22947c478bd9Sstevel@tonic-gate } 22957c478bd9Sstevel@tonic-gate 2296e11c3f44Smeem if (lifr.lifr_flags & IFF_IPMP) { 2297e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: module operations not" 2298e11c3f44Smeem " supported on IPMP interfaces\n", ifname); 2299e11c3f44Smeem return (_B_FALSE); 2300e11c3f44Smeem } 2301e11c3f44Smeem if (lifr.lifr_flags & IFF_VIRTUAL) { 2302e11c3f44Smeem (void) fprintf(stderr, "ifconfig: %s: module operations not" 2303e11c3f44Smeem " supported on virtual IP interfaces\n", ifname); 2304e11c3f44Smeem return (_B_FALSE); 2305e11c3f44Smeem } 2306e11c3f44Smeem return (_B_TRUE); 2307e11c3f44Smeem } 23087c478bd9Sstevel@tonic-gate 23097c478bd9Sstevel@tonic-gate /* 23107c478bd9Sstevel@tonic-gate * To list all the modules above a given network interface. 23117c478bd9Sstevel@tonic-gate */ 23127c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23137c478bd9Sstevel@tonic-gate static int 23147c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param) 23157c478bd9Sstevel@tonic-gate { 2316fc80c0dfSnordmark int muxid_fd; 23177c478bd9Sstevel@tonic-gate int muxfd; 23187c478bd9Sstevel@tonic-gate int ipfd_lowstr; 23197c478bd9Sstevel@tonic-gate int arpfd_lowstr; 23207c478bd9Sstevel@tonic-gate int num_mods; 23217c478bd9Sstevel@tonic-gate int i; 23227c478bd9Sstevel@tonic-gate struct str_list strlist; 23237c478bd9Sstevel@tonic-gate int orig_arpid; 23247c478bd9Sstevel@tonic-gate 2325e11c3f44Smeem /* 2326e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2327e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 2328e11c3f44Smeem */ 2329e11c3f44Smeem if (!modcheck(name)) 2330e11c3f44Smeem exit(1); 2331e11c3f44Smeem 2332fc80c0dfSnordmark if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 23337c478bd9Sstevel@tonic-gate &orig_arpid) < 0) { 23347c478bd9Sstevel@tonic-gate return (-1); 23357c478bd9Sstevel@tonic-gate } 23367c478bd9Sstevel@tonic-gate if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) { 23377c478bd9Sstevel@tonic-gate Perror0("cannot I_LIST to get the number of modules"); 23387c478bd9Sstevel@tonic-gate } else { 23397c478bd9Sstevel@tonic-gate if (debug > 0) { 23407c478bd9Sstevel@tonic-gate (void) printf("Listing (%d) modules above %s\n", 23417c478bd9Sstevel@tonic-gate num_mods, name); 23427c478bd9Sstevel@tonic-gate } 23437c478bd9Sstevel@tonic-gate 23447c478bd9Sstevel@tonic-gate strlist.sl_nmods = num_mods; 23457c478bd9Sstevel@tonic-gate strlist.sl_modlist = malloc(sizeof (struct str_mlist) * 23467c478bd9Sstevel@tonic-gate num_mods); 23477c478bd9Sstevel@tonic-gate if (strlist.sl_modlist == NULL) { 23487c478bd9Sstevel@tonic-gate Perror0("cannot malloc"); 23497c478bd9Sstevel@tonic-gate } else { 23507c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) { 23517c478bd9Sstevel@tonic-gate Perror0("cannot I_LIST for module names"); 23527c478bd9Sstevel@tonic-gate } else { 23537c478bd9Sstevel@tonic-gate for (i = 0; i < strlist.sl_nmods; i++) { 2354fc80c0dfSnordmark (void) printf("%d %s\n", i, 2355fc80c0dfSnordmark strlist.sl_modlist[i].l_name); 23567c478bd9Sstevel@tonic-gate } 23577c478bd9Sstevel@tonic-gate } 23587c478bd9Sstevel@tonic-gate free(strlist.sl_modlist); 23597c478bd9Sstevel@tonic-gate } 23607c478bd9Sstevel@tonic-gate } 2361fc80c0dfSnordmark return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2362fc80c0dfSnordmark orig_arpid)); 23637c478bd9Sstevel@tonic-gate } 23647c478bd9Sstevel@tonic-gate 23657c478bd9Sstevel@tonic-gate #define MODINSERT_OP 'i' 23667c478bd9Sstevel@tonic-gate #define MODREMOVE_OP 'r' 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate /* 23697c478bd9Sstevel@tonic-gate * To insert a module to the stream of the interface. It is just a 23707c478bd9Sstevel@tonic-gate * wrapper. The real function is modop(). 23717c478bd9Sstevel@tonic-gate */ 23727c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23737c478bd9Sstevel@tonic-gate static int 23747c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param) 23757c478bd9Sstevel@tonic-gate { 23767c478bd9Sstevel@tonic-gate return (modop(arg, MODINSERT_OP)); 23777c478bd9Sstevel@tonic-gate } 23787c478bd9Sstevel@tonic-gate 23797c478bd9Sstevel@tonic-gate /* 23807c478bd9Sstevel@tonic-gate * To remove a module from the stream of the interface. It is just a 23817c478bd9Sstevel@tonic-gate * wrapper. The real function is modop(). 23827c478bd9Sstevel@tonic-gate */ 23837c478bd9Sstevel@tonic-gate /* ARGSUSED */ 23847c478bd9Sstevel@tonic-gate static int 23857c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param) 23867c478bd9Sstevel@tonic-gate { 23877c478bd9Sstevel@tonic-gate return (modop(arg, MODREMOVE_OP)); 23887c478bd9Sstevel@tonic-gate } 23897c478bd9Sstevel@tonic-gate 23907c478bd9Sstevel@tonic-gate /* 23917c478bd9Sstevel@tonic-gate * Helper function for mod*() functions. It gets a fd to the lower IP 23927c478bd9Sstevel@tonic-gate * stream and I_PUNLINK's the lower stream. It also initializes the 23937c478bd9Sstevel@tonic-gate * global variable lifr. 23947c478bd9Sstevel@tonic-gate * 23957c478bd9Sstevel@tonic-gate * Param: 2396fc80c0dfSnordmark * int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2397fc80c0dfSnordmark * int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2398fc80c0dfSnordmark * int *ipfd_lowstr: fd to the lower IP stream. 2399fc80c0dfSnordmark * int *arpfd_lowstr: fd to the lower ARP stream. 24007c478bd9Sstevel@tonic-gate * 24017c478bd9Sstevel@tonic-gate * Return: 24027c478bd9Sstevel@tonic-gate * -1 if operation fails, 0 otherwise. 24037c478bd9Sstevel@tonic-gate * 2404e11c3f44Smeem * Please see the big block comment above ifplumb() for the logic of the 2405e11c3f44Smeem * PLINK/PUNLINK 24067c478bd9Sstevel@tonic-gate */ 24077c478bd9Sstevel@tonic-gate static int 2408fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr, 2409fc80c0dfSnordmark int *orig_arpid) 24107c478bd9Sstevel@tonic-gate { 24117c478bd9Sstevel@tonic-gate uint64_t flags; 24127c478bd9Sstevel@tonic-gate char *udp_dev_name; 24137c478bd9Sstevel@tonic-gate 24147c478bd9Sstevel@tonic-gate *orig_arpid = 0; 24157c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 24167c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 24177c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 24187c478bd9Sstevel@tonic-gate } 24197c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 24207c478bd9Sstevel@tonic-gate if (flags & IFF_IPV4) { 24217c478bd9Sstevel@tonic-gate udp_dev_name = UDP_DEV_NAME; 24227c478bd9Sstevel@tonic-gate } else if (flags & IFF_IPV6) { 24237c478bd9Sstevel@tonic-gate udp_dev_name = UDP6_DEV_NAME; 24247c478bd9Sstevel@tonic-gate } else { 24257c478bd9Sstevel@tonic-gate return (-1); 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate 2428fc80c0dfSnordmark if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) { 2429fc80c0dfSnordmark Perror2("open", udp_dev_name); 24307c478bd9Sstevel@tonic-gate return (-1); 24317c478bd9Sstevel@tonic-gate } 2432fc80c0dfSnordmark if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) { 2433fc80c0dfSnordmark Perror2("SIOCGLIFMUXID", udp_dev_name); 24347c478bd9Sstevel@tonic-gate return (-1); 24357c478bd9Sstevel@tonic-gate } 24367c478bd9Sstevel@tonic-gate if (debug > 0) { 24377c478bd9Sstevel@tonic-gate (void) printf("ARP_muxid %d IP_muxid %d\n", 24387c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid, lifr.lifr_ip_muxid); 24397c478bd9Sstevel@tonic-gate } 24407c478bd9Sstevel@tonic-gate 2441fc80c0dfSnordmark /* 2442fc80c0dfSnordmark * Use /dev/udp{,6} as the mux to avoid linkcycles. 2443fc80c0dfSnordmark */ 24446e91bba0SGirish Moodalbail if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS) 24457c478bd9Sstevel@tonic-gate return (-1); 24467c478bd9Sstevel@tonic-gate 24477c478bd9Sstevel@tonic-gate if (lifr.lifr_arp_muxid != 0) { 24487c478bd9Sstevel@tonic-gate if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 24497c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid)) < 0) { 24507c478bd9Sstevel@tonic-gate if ((errno == EINVAL) && 24517c478bd9Sstevel@tonic-gate (flags & (IFF_NOARP | IFF_IPV6))) { 24527c478bd9Sstevel@tonic-gate /* 24537c478bd9Sstevel@tonic-gate * Some plumbing utilities set the muxid to 24547c478bd9Sstevel@tonic-gate * -1 or some invalid value to signify that 24557c478bd9Sstevel@tonic-gate * there is no arp stream. Set the muxid to 0 24567c478bd9Sstevel@tonic-gate * before trying to unplumb the IP stream. 24577c478bd9Sstevel@tonic-gate * IP does not allow the IP stream to be 24587c478bd9Sstevel@tonic-gate * unplumbed if it sees a non-null arp muxid, 24597c478bd9Sstevel@tonic-gate * for consistency of IP-ARP streams. 24607c478bd9Sstevel@tonic-gate */ 24617c478bd9Sstevel@tonic-gate *orig_arpid = lifr.lifr_arp_muxid; 24627c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = 0; 2463fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, 24647c478bd9Sstevel@tonic-gate (caddr_t)&lifr); 24657c478bd9Sstevel@tonic-gate *arpfd_lowstr = -1; 24667c478bd9Sstevel@tonic-gate } else { 24677c478bd9Sstevel@tonic-gate Perror0("_I_MUXID2FD"); 24687c478bd9Sstevel@tonic-gate return (-1); 24697c478bd9Sstevel@tonic-gate } 24707c478bd9Sstevel@tonic-gate } else if (ioctl(*muxfd, I_PUNLINK, 24717c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid) < 0) { 24727c478bd9Sstevel@tonic-gate Perror2("I_PUNLINK", udp_dev_name); 24737c478bd9Sstevel@tonic-gate return (-1); 24747c478bd9Sstevel@tonic-gate } 24757c478bd9Sstevel@tonic-gate } else { 24767c478bd9Sstevel@tonic-gate *arpfd_lowstr = -1; 24777c478bd9Sstevel@tonic-gate } 24787c478bd9Sstevel@tonic-gate 24797c478bd9Sstevel@tonic-gate if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD, 24807c478bd9Sstevel@tonic-gate lifr.lifr_ip_muxid)) < 0) { 24817c478bd9Sstevel@tonic-gate Perror0("_I_MUXID2FD"); 24827c478bd9Sstevel@tonic-gate /* Undo any changes we made */ 24837c478bd9Sstevel@tonic-gate if (*orig_arpid != 0) { 24847c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = *orig_arpid; 2485fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 24867c478bd9Sstevel@tonic-gate } 24877c478bd9Sstevel@tonic-gate return (-1); 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) { 24907c478bd9Sstevel@tonic-gate Perror2("I_PUNLINK", udp_dev_name); 24917c478bd9Sstevel@tonic-gate /* Undo any changes we made */ 24927c478bd9Sstevel@tonic-gate if (*orig_arpid != 0) { 24937c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = *orig_arpid; 2494fc80c0dfSnordmark (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 24957c478bd9Sstevel@tonic-gate } 24967c478bd9Sstevel@tonic-gate return (-1); 24977c478bd9Sstevel@tonic-gate } 24987c478bd9Sstevel@tonic-gate return (0); 24997c478bd9Sstevel@tonic-gate } 25007c478bd9Sstevel@tonic-gate 25017c478bd9Sstevel@tonic-gate /* 25027c478bd9Sstevel@tonic-gate * Helper function for mod*() functions. It I_PLINK's back the upper and 25037c478bd9Sstevel@tonic-gate * lower IP streams. Note that this function must be called after 25047c478bd9Sstevel@tonic-gate * ip_domux2fd(). In ip_domux2fd(), the global variable lifr is initialized 25057c478bd9Sstevel@tonic-gate * and ip_plink() needs information in lifr. So ip_domux2fd() and ip_plink() 25067c478bd9Sstevel@tonic-gate * must be called in pairs. 25077c478bd9Sstevel@tonic-gate * 25087c478bd9Sstevel@tonic-gate * Param: 2509fc80c0dfSnordmark * int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK 2510fc80c0dfSnordmark * int muxid_fd: fd to /dev/udp{,6} for LIFMUXID 2511fc80c0dfSnordmark * int ipfd_lowstr: fd to the lower IP stream. 2512fc80c0dfSnordmark * int arpfd_lowstr: fd to the lower ARP stream. 25137c478bd9Sstevel@tonic-gate * 25147c478bd9Sstevel@tonic-gate * Return: 25157c478bd9Sstevel@tonic-gate * -1 if operation fails, 0 otherwise. 25167c478bd9Sstevel@tonic-gate * 2517e11c3f44Smeem * Please see the big block comment above ifplumb() for the logic of the 2518e11c3f44Smeem * PLINK/PUNLINK 25197c478bd9Sstevel@tonic-gate */ 25207c478bd9Sstevel@tonic-gate static int 2521fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr, 2522fc80c0dfSnordmark int orig_arpid) 25237c478bd9Sstevel@tonic-gate { 25247c478bd9Sstevel@tonic-gate int ip_muxid; 25257c478bd9Sstevel@tonic-gate 25267c478bd9Sstevel@tonic-gate ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr); 25277c478bd9Sstevel@tonic-gate if (ip_muxid < 0) { 25287c478bd9Sstevel@tonic-gate Perror2("I_PLINK", UDP_DEV_NAME); 25297c478bd9Sstevel@tonic-gate return (-1); 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate 25327c478bd9Sstevel@tonic-gate /* 25337c478bd9Sstevel@tonic-gate * If there is an arp stream, plink it. If there is no 25347c478bd9Sstevel@tonic-gate * arp stream, then it is possible that the plumbing 25357c478bd9Sstevel@tonic-gate * utility could have stored any value in the arp_muxid. 25367c478bd9Sstevel@tonic-gate * If so, restore it from orig_arpid. 25377c478bd9Sstevel@tonic-gate */ 25387c478bd9Sstevel@tonic-gate if (arpfd_lowstr != -1) { 25397c478bd9Sstevel@tonic-gate if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) { 25407c478bd9Sstevel@tonic-gate Perror2("I_PLINK", UDP_DEV_NAME); 25417c478bd9Sstevel@tonic-gate return (-1); 25427c478bd9Sstevel@tonic-gate } 25437c478bd9Sstevel@tonic-gate } else if (orig_arpid != 0) { 25447c478bd9Sstevel@tonic-gate /* Undo the changes we did in ip_domux2fd */ 25457c478bd9Sstevel@tonic-gate lifr.lifr_arp_muxid = orig_arpid; 25467c478bd9Sstevel@tonic-gate lifr.lifr_ip_muxid = ip_muxid; 2547fc80c0dfSnordmark (void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr); 25487c478bd9Sstevel@tonic-gate } 25497c478bd9Sstevel@tonic-gate 2550fc80c0dfSnordmark (void) close(muxfd); 2551fc80c0dfSnordmark (void) close(muxid_fd); 25527c478bd9Sstevel@tonic-gate return (0); 25537c478bd9Sstevel@tonic-gate } 25547c478bd9Sstevel@tonic-gate 25557c478bd9Sstevel@tonic-gate /* 25567c478bd9Sstevel@tonic-gate * The real function to perform module insertion/removal. 25577c478bd9Sstevel@tonic-gate * 25587c478bd9Sstevel@tonic-gate * Param: 25597c478bd9Sstevel@tonic-gate * char *arg: the argument string module_name@position 25607c478bd9Sstevel@tonic-gate * char op: operation, either MODINSERT_OP or MODREMOVE_OP. 25617c478bd9Sstevel@tonic-gate * 25627c478bd9Sstevel@tonic-gate * Return: 25637c478bd9Sstevel@tonic-gate * Before doing ip_domux2fd(), this function calls exit(1) in case of 25647c478bd9Sstevel@tonic-gate * error. After ip_domux2fd() is done, it returns -1 for error, 0 25657c478bd9Sstevel@tonic-gate * otherwise. 25667c478bd9Sstevel@tonic-gate */ 25677c478bd9Sstevel@tonic-gate static int 25687c478bd9Sstevel@tonic-gate modop(char *arg, char op) 25697c478bd9Sstevel@tonic-gate { 25707c478bd9Sstevel@tonic-gate char *pos_p; 25717c478bd9Sstevel@tonic-gate int muxfd; 2572fc80c0dfSnordmark int muxid_fd; 25737c478bd9Sstevel@tonic-gate int ipfd_lowstr; /* IP stream (lower stream of mux) to be plinked */ 25747c478bd9Sstevel@tonic-gate int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */ 25757c478bd9Sstevel@tonic-gate struct strmodconf mod; 25767c478bd9Sstevel@tonic-gate char *at_char = "@"; 25777c478bd9Sstevel@tonic-gate char *arg_str; 25787c478bd9Sstevel@tonic-gate int orig_arpid; 25797c478bd9Sstevel@tonic-gate 2580e11c3f44Smeem /* 2581e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 2582e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 2583e11c3f44Smeem */ 2584e11c3f44Smeem if (!modcheck(name)) 2585e11c3f44Smeem exit(1); 25867c478bd9Sstevel@tonic-gate 25877c478bd9Sstevel@tonic-gate /* Need to save the original string for -a option. */ 25887c478bd9Sstevel@tonic-gate if ((arg_str = malloc(strlen(arg) + 1)) == NULL) { 25897c478bd9Sstevel@tonic-gate Perror0("cannot malloc"); 25907c478bd9Sstevel@tonic-gate return (-1); 25917c478bd9Sstevel@tonic-gate } 25927c478bd9Sstevel@tonic-gate (void) strcpy(arg_str, arg); 25937c478bd9Sstevel@tonic-gate 25947c478bd9Sstevel@tonic-gate if (*arg_str == *at_char) { 25957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 25967c478bd9Sstevel@tonic-gate "ifconfig: must supply a module name\n"); 25977c478bd9Sstevel@tonic-gate exit(1); 25987c478bd9Sstevel@tonic-gate } 25997c478bd9Sstevel@tonic-gate mod.mod_name = strtok(arg_str, at_char); 26007c478bd9Sstevel@tonic-gate if (strlen(mod.mod_name) > FMNAMESZ) { 26017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: module name too long: %s\n", 26027c478bd9Sstevel@tonic-gate mod.mod_name); 26037c478bd9Sstevel@tonic-gate exit(1); 26047c478bd9Sstevel@tonic-gate } 26057c478bd9Sstevel@tonic-gate 26067c478bd9Sstevel@tonic-gate /* 26077c478bd9Sstevel@tonic-gate * Need to make sure that the core TCP/IP stack modules are not 26087c478bd9Sstevel@tonic-gate * removed. Otherwise, "bad" things can happen. If a module 26097c478bd9Sstevel@tonic-gate * is removed and inserted back, it loses its old state. But 26107c478bd9Sstevel@tonic-gate * the modules above it still have the old state. E.g. IP assumes 26117c478bd9Sstevel@tonic-gate * fast data path while tunnel after re-inserted assumes that it can 26127c478bd9Sstevel@tonic-gate * receive M_DATA only in fast data path for which it does not have 26137c478bd9Sstevel@tonic-gate * any state. This is a general caveat of _I_REMOVE/_I_INSERT. 26147c478bd9Sstevel@tonic-gate */ 26157c478bd9Sstevel@tonic-gate if (op == MODREMOVE_OP && 26167c478bd9Sstevel@tonic-gate (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 || 26172b24ab6bSSebastien Roy strcmp(mod.mod_name, IP_MOD_NAME) == 0)) { 26187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: cannot remove %s\n", 26197c478bd9Sstevel@tonic-gate mod.mod_name); 26207c478bd9Sstevel@tonic-gate exit(1); 26217c478bd9Sstevel@tonic-gate } 26227c478bd9Sstevel@tonic-gate 26237c478bd9Sstevel@tonic-gate if ((pos_p = strtok(NULL, at_char)) == NULL) { 26247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: must supply a position\n"); 26257c478bd9Sstevel@tonic-gate exit(1); 26267c478bd9Sstevel@tonic-gate } 26277c478bd9Sstevel@tonic-gate mod.pos = atoi(pos_p); 26287c478bd9Sstevel@tonic-gate 2629fc80c0dfSnordmark if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr, 26307c478bd9Sstevel@tonic-gate &orig_arpid) < 0) { 26317c478bd9Sstevel@tonic-gate free(arg_str); 26327c478bd9Sstevel@tonic-gate return (-1); 26337c478bd9Sstevel@tonic-gate } 26347c478bd9Sstevel@tonic-gate switch (op) { 26357c478bd9Sstevel@tonic-gate case MODINSERT_OP: 26367c478bd9Sstevel@tonic-gate if (debug > 0) { 26377c478bd9Sstevel@tonic-gate (void) printf("Inserting module %s at %d\n", 26387c478bd9Sstevel@tonic-gate mod.mod_name, mod.pos); 26397c478bd9Sstevel@tonic-gate } 26407c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) { 26417c478bd9Sstevel@tonic-gate Perror2("fail to insert module", mod.mod_name); 26427c478bd9Sstevel@tonic-gate } 26437c478bd9Sstevel@tonic-gate break; 26447c478bd9Sstevel@tonic-gate case MODREMOVE_OP: 26457c478bd9Sstevel@tonic-gate if (debug > 0) { 26467c478bd9Sstevel@tonic-gate (void) printf("Removing module %s at %d\n", 26477c478bd9Sstevel@tonic-gate mod.mod_name, mod.pos); 26487c478bd9Sstevel@tonic-gate } 26497c478bd9Sstevel@tonic-gate if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) { 26507c478bd9Sstevel@tonic-gate Perror2("fail to remove module", mod.mod_name); 26517c478bd9Sstevel@tonic-gate } 26527c478bd9Sstevel@tonic-gate break; 26537c478bd9Sstevel@tonic-gate default: 26547c478bd9Sstevel@tonic-gate /* Should never get to here. */ 26557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Unknown operation\n"); 26567c478bd9Sstevel@tonic-gate break; 26577c478bd9Sstevel@tonic-gate } 26587c478bd9Sstevel@tonic-gate free(arg_str); 2659fc80c0dfSnordmark return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr, 2660fc80c0dfSnordmark orig_arpid)); 26617c478bd9Sstevel@tonic-gate } 26627c478bd9Sstevel@tonic-gate 26632b24ab6bSSebastien Roy static int 26642b24ab6bSSebastien Roy modify_tun(iptun_params_t *params) 26652b24ab6bSSebastien Roy { 26662b24ab6bSSebastien Roy dladm_status_t status; 26672b24ab6bSSebastien Roy 26682b24ab6bSSebastien Roy if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, 26692b24ab6bSSebastien Roy ¶ms->iptun_param_linkid)) == DLADM_STATUS_OK) 26702b24ab6bSSebastien Roy status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE); 26712b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 26722b24ab6bSSebastien Roy dladmerr_exit(status, name); 26732b24ab6bSSebastien Roy return (0); 26742b24ab6bSSebastien Roy } 26752b24ab6bSSebastien Roy 26767c478bd9Sstevel@tonic-gate /* 26777c478bd9Sstevel@tonic-gate * Set tunnel source address 26787c478bd9Sstevel@tonic-gate */ 26797c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26807c478bd9Sstevel@tonic-gate static int 26817c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param) 26827c478bd9Sstevel@tonic-gate { 26832b24ab6bSSebastien Roy iptun_params_t params; 26842b24ab6bSSebastien Roy 26852b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_LADDR; 26862b24ab6bSSebastien Roy (void) strlcpy(params.iptun_param_laddr, addr, 26872b24ab6bSSebastien Roy sizeof (params.iptun_param_laddr)); 26882b24ab6bSSebastien Roy return (modify_tun(¶ms)); 26897c478bd9Sstevel@tonic-gate } 26907c478bd9Sstevel@tonic-gate 26917c478bd9Sstevel@tonic-gate /* 26927c478bd9Sstevel@tonic-gate * Set tunnel destination address 26937c478bd9Sstevel@tonic-gate */ 26947c478bd9Sstevel@tonic-gate /* ARGSUSED */ 26957c478bd9Sstevel@tonic-gate static int 26967c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param) 26977c478bd9Sstevel@tonic-gate { 26982b24ab6bSSebastien Roy iptun_params_t params; 26992b24ab6bSSebastien Roy 27002b24ab6bSSebastien Roy params.iptun_param_flags = IPTUN_PARAM_RADDR; 27012b24ab6bSSebastien Roy (void) strlcpy(params.iptun_param_raddr, addr, 27022b24ab6bSSebastien Roy sizeof (params.iptun_param_raddr)); 27032b24ab6bSSebastien Roy return (modify_tun(¶ms)); 27047c478bd9Sstevel@tonic-gate } 27057c478bd9Sstevel@tonic-gate 27067c478bd9Sstevel@tonic-gate static int 27072b24ab6bSSebastien Roy set_tun_prop(const char *propname, char *value) 27087c478bd9Sstevel@tonic-gate { 27092b24ab6bSSebastien Roy dladm_status_t status; 27102b24ab6bSSebastien Roy datalink_id_t linkid; 27117c478bd9Sstevel@tonic-gate 27122b24ab6bSSebastien Roy status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid); 27132b24ab6bSSebastien Roy if (status == DLADM_STATUS_OK) { 27142b24ab6bSSebastien Roy status = dladm_set_linkprop(dlh, linkid, propname, &value, 1, 27152b24ab6bSSebastien Roy DLADM_OPT_ACTIVE); 27167c478bd9Sstevel@tonic-gate } 27172b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) 27182b24ab6bSSebastien Roy dladmerr_exit(status, name); 27197c478bd9Sstevel@tonic-gate return (0); 27207c478bd9Sstevel@tonic-gate } 27217c478bd9Sstevel@tonic-gate 27227c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */ 27237c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27247c478bd9Sstevel@tonic-gate static int 27257c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param) 27267c478bd9Sstevel@tonic-gate { 27272b24ab6bSSebastien Roy return (set_tun_prop("encaplimit", arg)); 27287c478bd9Sstevel@tonic-gate } 27297c478bd9Sstevel@tonic-gate 27307c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */ 27317c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27327c478bd9Sstevel@tonic-gate static int 27337c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param) 27347c478bd9Sstevel@tonic-gate { 27352b24ab6bSSebastien Roy return (set_tun_encap_limit("-1", 0)); 27367c478bd9Sstevel@tonic-gate } 27377c478bd9Sstevel@tonic-gate 27387c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */ 27397c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27407c478bd9Sstevel@tonic-gate static int 27417c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param) 27427c478bd9Sstevel@tonic-gate { 27432b24ab6bSSebastien Roy return (set_tun_prop("hoplimit", arg)); 27447c478bd9Sstevel@tonic-gate } 27457c478bd9Sstevel@tonic-gate 27467c478bd9Sstevel@tonic-gate /* Set zone ID */ 27477c478bd9Sstevel@tonic-gate static int 27487c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param) 27497c478bd9Sstevel@tonic-gate { 27507c478bd9Sstevel@tonic-gate zoneid_t zoneid = GLOBAL_ZONEID; 27517c478bd9Sstevel@tonic-gate 27527c478bd9Sstevel@tonic-gate if (param == NEXTARG) { 27537c478bd9Sstevel@tonic-gate /* zone must be active */ 27547c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(arg)) == -1) { 27557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 27567c478bd9Sstevel@tonic-gate "ifconfig: unknown zone '%s'\n", arg); 27577c478bd9Sstevel@tonic-gate exit(1); 27587c478bd9Sstevel@tonic-gate } 27597c478bd9Sstevel@tonic-gate } 27607c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 27617c478bd9Sstevel@tonic-gate lifr.lifr_zoneid = zoneid; 27627c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 27637c478bd9Sstevel@tonic-gate Perror0_exit("SIOCSLIFZONE"); 27647c478bd9Sstevel@tonic-gate return (0); 27657c478bd9Sstevel@tonic-gate } 27667c478bd9Sstevel@tonic-gate 276745916cd2Sjpk /* Put interface into all zones */ 276845916cd2Sjpk /* ARGSUSED */ 276945916cd2Sjpk static int 277045916cd2Sjpk setallzones(char *arg, int64_t param) 277145916cd2Sjpk { 277245916cd2Sjpk (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 277345916cd2Sjpk lifr.lifr_zoneid = ALL_ZONES; 277445916cd2Sjpk if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1) 277545916cd2Sjpk Perror0_exit("SIOCSLIFZONE"); 277645916cd2Sjpk return (0); 277745916cd2Sjpk } 277845916cd2Sjpk 27797c478bd9Sstevel@tonic-gate /* Set source address to use */ 27807c478bd9Sstevel@tonic-gate /* ARGSUSED */ 27817c478bd9Sstevel@tonic-gate static int 27827c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param) 27837c478bd9Sstevel@tonic-gate { 27847c478bd9Sstevel@tonic-gate uint_t ifindex = 0; 27857c478bd9Sstevel@tonic-gate int rval; 27867c478bd9Sstevel@tonic-gate 27877c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 27887c478bd9Sstevel@tonic-gate 27897c478bd9Sstevel@tonic-gate /* 27907c478bd9Sstevel@tonic-gate * Argument can be either an interface name or "none". The latter means 27917c478bd9Sstevel@tonic-gate * that any previous selection is cleared. 27927c478bd9Sstevel@tonic-gate */ 27937c478bd9Sstevel@tonic-gate 27946e91bba0SGirish Moodalbail if (strchr(arg, ':') != NULL) { 27957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 27966e91bba0SGirish Moodalbail "ifconfig: Cannot specify logical interface for usesrc \n"); 27977c478bd9Sstevel@tonic-gate exit(1); 27987c478bd9Sstevel@tonic-gate } 27997c478bd9Sstevel@tonic-gate 28007c478bd9Sstevel@tonic-gate rval = strcmp(arg, NONE_STR); 28017c478bd9Sstevel@tonic-gate if (rval != 0) { 28027c478bd9Sstevel@tonic-gate if ((ifindex = if_nametoindex(arg)) == 0) { 28037c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ); 28047c478bd9Sstevel@tonic-gate Perror0_exit("Could not get interface index"); 28057c478bd9Sstevel@tonic-gate } 28067c478bd9Sstevel@tonic-gate lifr.lifr_index = ifindex; 28077c478bd9Sstevel@tonic-gate } else { 28087c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0) 28097c478bd9Sstevel@tonic-gate Perror0_exit("Not a valid usesrc consumer"); 28107c478bd9Sstevel@tonic-gate lifr.lifr_index = 0; 28117c478bd9Sstevel@tonic-gate } 28127c478bd9Sstevel@tonic-gate 28137c478bd9Sstevel@tonic-gate if (debug) 28147c478bd9Sstevel@tonic-gate (void) printf("setifsrc: lifr_name %s, lifr_index %d\n", 28157c478bd9Sstevel@tonic-gate lifr.lifr_name, lifr.lifr_index); 28167c478bd9Sstevel@tonic-gate 28177c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) { 28187c478bd9Sstevel@tonic-gate if (rval == 0) 28197c478bd9Sstevel@tonic-gate Perror0_exit("Cannot reset usesrc group"); 28207c478bd9Sstevel@tonic-gate else 28217c478bd9Sstevel@tonic-gate Perror0_exit("Could not set source interface"); 28227c478bd9Sstevel@tonic-gate } 28237c478bd9Sstevel@tonic-gate 28247c478bd9Sstevel@tonic-gate return (0); 28257c478bd9Sstevel@tonic-gate } 28267c478bd9Sstevel@tonic-gate 28277c478bd9Sstevel@tonic-gate /* 28287c478bd9Sstevel@tonic-gate * Print the interface status line associated with `ifname' 28297c478bd9Sstevel@tonic-gate */ 28307c478bd9Sstevel@tonic-gate static void 28317c478bd9Sstevel@tonic-gate ifstatus(const char *ifname) 28327c478bd9Sstevel@tonic-gate { 28337c478bd9Sstevel@tonic-gate uint64_t flags; 28347c478bd9Sstevel@tonic-gate char if_usesrc_name[LIFNAMSIZ]; 28357c478bd9Sstevel@tonic-gate char *newbuf; 28367c478bd9Sstevel@tonic-gate int n, numifs, rval = 0; 28377c478bd9Sstevel@tonic-gate struct lifreq *lifrp; 28387c478bd9Sstevel@tonic-gate struct lifsrcof lifs; 28397c478bd9Sstevel@tonic-gate 28407c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 28417c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 28427c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 28437c478bd9Sstevel@tonic-gate } 28447c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 28457c478bd9Sstevel@tonic-gate 28467c478bd9Sstevel@tonic-gate /* 28477c478bd9Sstevel@tonic-gate * In V4 compatibility mode, we don't print the IFF_IPV4 flag or 28487c478bd9Sstevel@tonic-gate * interfaces with IFF_IPV6 set. 28497c478bd9Sstevel@tonic-gate */ 28507c478bd9Sstevel@tonic-gate if (v4compat) { 28517c478bd9Sstevel@tonic-gate flags &= ~IFF_IPV4; 28527c478bd9Sstevel@tonic-gate if (flags & IFF_IPV6) 28537c478bd9Sstevel@tonic-gate return; 28547c478bd9Sstevel@tonic-gate } 28557c478bd9Sstevel@tonic-gate 28567c478bd9Sstevel@tonic-gate (void) printf("%s: ", ifname); 28577c478bd9Sstevel@tonic-gate print_flags(flags); 28587c478bd9Sstevel@tonic-gate 28597c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name)); 28607c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 28617c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFMETRIC"); 28627c478bd9Sstevel@tonic-gate } else { 28637c478bd9Sstevel@tonic-gate if (lifr.lifr_metric) 28647c478bd9Sstevel@tonic-gate (void) printf(" metric %d", lifr.lifr_metric); 28657c478bd9Sstevel@tonic-gate } 28667c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 2867c08e5e1aSdr (void) printf(" mtu %u", lifr.lifr_mtu); 28687c478bd9Sstevel@tonic-gate 28697c478bd9Sstevel@tonic-gate /* don't print index or zone when in compatibility mode */ 28707c478bd9Sstevel@tonic-gate if (!v4compat) { 28717c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 28727c478bd9Sstevel@tonic-gate (void) printf(" index %d", lifr.lifr_index); 2873f4b3ec61Sdh /* 2874f4b3ec61Sdh * Stack instances use GLOBAL_ZONEID for IP data structures 2875f4b3ec61Sdh * even in the non-global zone. 2876f4b3ec61Sdh */ 28777c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 && 2878f4b3ec61Sdh lifr.lifr_zoneid != getzoneid() && 2879f4b3ec61Sdh lifr.lifr_zoneid != GLOBAL_ZONEID) { 28807c478bd9Sstevel@tonic-gate char zone_name[ZONENAME_MAX]; 28817c478bd9Sstevel@tonic-gate 288245916cd2Sjpk if (lifr.lifr_zoneid == ALL_ZONES) { 288345916cd2Sjpk (void) printf("\n\tall-zones"); 288445916cd2Sjpk } else if (getzonenamebyid(lifr.lifr_zoneid, zone_name, 28857c478bd9Sstevel@tonic-gate sizeof (zone_name)) < 0) { 28867c478bd9Sstevel@tonic-gate (void) printf("\n\tzone %d", lifr.lifr_zoneid); 28877c478bd9Sstevel@tonic-gate } else { 28887c478bd9Sstevel@tonic-gate (void) printf("\n\tzone %s", zone_name); 28897c478bd9Sstevel@tonic-gate } 28907c478bd9Sstevel@tonic-gate } 28917c478bd9Sstevel@tonic-gate } 28927c478bd9Sstevel@tonic-gate 28937c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) { 28947c478bd9Sstevel@tonic-gate lifs.lifs_ifindex = lifr.lifr_index; 28957c478bd9Sstevel@tonic-gate 28967c478bd9Sstevel@tonic-gate /* 28977c478bd9Sstevel@tonic-gate * Find the number of interfaces that use this interfaces' 28987c478bd9Sstevel@tonic-gate * address as a source address 28997c478bd9Sstevel@tonic-gate */ 29007c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 29017c478bd9Sstevel@tonic-gate lifs.lifs_maxlen = 0; 29027c478bd9Sstevel@tonic-gate for (;;) { 29037c478bd9Sstevel@tonic-gate /* The first pass will give the bufsize we need */ 29047c478bd9Sstevel@tonic-gate rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs); 29057c478bd9Sstevel@tonic-gate if (rval < 0) { 29067c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) { 29077c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29087c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 29097c478bd9Sstevel@tonic-gate } 29107c478bd9Sstevel@tonic-gate lifs.lifs_len = 0; 29117c478bd9Sstevel@tonic-gate break; 29127c478bd9Sstevel@tonic-gate } 29137c478bd9Sstevel@tonic-gate if (lifs.lifs_len <= lifs.lifs_maxlen) 29147c478bd9Sstevel@tonic-gate break; 29157c478bd9Sstevel@tonic-gate /* Use kernel's size + a small margin to avoid loops */ 29167c478bd9Sstevel@tonic-gate lifs.lifs_maxlen = lifs.lifs_len + 29177c478bd9Sstevel@tonic-gate 5 * sizeof (struct lifreq); 29187c478bd9Sstevel@tonic-gate /* For the first pass, realloc acts like malloc */ 29197c478bd9Sstevel@tonic-gate newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen); 29207c478bd9Sstevel@tonic-gate if (newbuf == NULL) { 29217c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) { 29227c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29237c478bd9Sstevel@tonic-gate lifs.lifs_buf = NULL; 29247c478bd9Sstevel@tonic-gate } 29257c478bd9Sstevel@tonic-gate lifs.lifs_len = 0; 29267c478bd9Sstevel@tonic-gate break; 29277c478bd9Sstevel@tonic-gate } 29287c478bd9Sstevel@tonic-gate lifs.lifs_buf = newbuf; 29297c478bd9Sstevel@tonic-gate } 29307c478bd9Sstevel@tonic-gate 29317c478bd9Sstevel@tonic-gate 29327c478bd9Sstevel@tonic-gate numifs = lifs.lifs_len / sizeof (struct lifreq); 29337c478bd9Sstevel@tonic-gate if (numifs > 0) { 29347c478bd9Sstevel@tonic-gate lifrp = lifs.lifs_req; 29357c478bd9Sstevel@tonic-gate (void) printf("\n\tsrcof"); 29367c478bd9Sstevel@tonic-gate for (n = numifs; n > 0; n--, lifrp++) { 29377c478bd9Sstevel@tonic-gate (void) printf(" %s", lifrp->lifr_name); 29387c478bd9Sstevel@tonic-gate } 29397c478bd9Sstevel@tonic-gate } 29407c478bd9Sstevel@tonic-gate 29417c478bd9Sstevel@tonic-gate if (lifs.lifs_buf != NULL) 29427c478bd9Sstevel@tonic-gate free(lifs.lifs_buf); 29437c478bd9Sstevel@tonic-gate } 29447c478bd9Sstevel@tonic-gate 29457c478bd9Sstevel@tonic-gate /* Find the interface whose source address this interface uses */ 29467c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 29477c478bd9Sstevel@tonic-gate if (lifr.lifr_index != 0) { 29487c478bd9Sstevel@tonic-gate if (if_indextoname(lifr.lifr_index, 29497c478bd9Sstevel@tonic-gate if_usesrc_name) == NULL) { 29507c478bd9Sstevel@tonic-gate (void) printf("\n\tusesrc ifIndex %d", 29517c478bd9Sstevel@tonic-gate lifr.lifr_index); 29527c478bd9Sstevel@tonic-gate } else { 29537c478bd9Sstevel@tonic-gate (void) printf("\n\tusesrc %s", if_usesrc_name); 29547c478bd9Sstevel@tonic-gate } 29557c478bd9Sstevel@tonic-gate } 29567c478bd9Sstevel@tonic-gate } 29577c478bd9Sstevel@tonic-gate 29587c478bd9Sstevel@tonic-gate (void) putchar('\n'); 29597c478bd9Sstevel@tonic-gate } 29607c478bd9Sstevel@tonic-gate 29617c478bd9Sstevel@tonic-gate /* 29627c478bd9Sstevel@tonic-gate * Print the status of the interface. If an address family was 29637c478bd9Sstevel@tonic-gate * specified, show it and it only; otherwise, show them all. 29647c478bd9Sstevel@tonic-gate */ 29657c478bd9Sstevel@tonic-gate static void 29667c478bd9Sstevel@tonic-gate status(void) 29677c478bd9Sstevel@tonic-gate { 29682b24ab6bSSebastien Roy struct afswtch *p = afp; 29692b24ab6bSSebastien Roy uint64_t flags; 29702b24ab6bSSebastien Roy datalink_id_t linkid; 29717c478bd9Sstevel@tonic-gate 29727c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 29737c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 29747c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 29757c478bd9Sstevel@tonic-gate } 29767c478bd9Sstevel@tonic-gate 29777c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 29787c478bd9Sstevel@tonic-gate 29797c478bd9Sstevel@tonic-gate /* 29807c478bd9Sstevel@tonic-gate * Only print the interface status if the address family matches 29817c478bd9Sstevel@tonic-gate * the interface family flag. 29827c478bd9Sstevel@tonic-gate */ 29837c478bd9Sstevel@tonic-gate if (p != NULL) { 29847c478bd9Sstevel@tonic-gate if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) || 29857c478bd9Sstevel@tonic-gate ((p->af_af == AF_INET) && (flags & IFF_IPV6))) 29867c478bd9Sstevel@tonic-gate return; 29877c478bd9Sstevel@tonic-gate } 29887c478bd9Sstevel@tonic-gate 29897c478bd9Sstevel@tonic-gate /* 29907c478bd9Sstevel@tonic-gate * In V4 compatibility mode, don't print IFF_IPV6 interfaces. 29917c478bd9Sstevel@tonic-gate */ 29927c478bd9Sstevel@tonic-gate if (v4compat && (flags & IFF_IPV6)) 29937c478bd9Sstevel@tonic-gate return; 29947c478bd9Sstevel@tonic-gate 29957c478bd9Sstevel@tonic-gate ifstatus(name); 29967c478bd9Sstevel@tonic-gate 29972b24ab6bSSebastien Roy if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) == 29982b24ab6bSSebastien Roy DLADM_STATUS_OK) 29992b24ab6bSSebastien Roy tun_status(linkid); 30002b24ab6bSSebastien Roy 30017c478bd9Sstevel@tonic-gate if (p != NULL) { 30027c478bd9Sstevel@tonic-gate (*p->af_status)(1, flags); 30037c478bd9Sstevel@tonic-gate } else { 30047c478bd9Sstevel@tonic-gate for (p = afs; p->af_name; p++) { 30057c478bd9Sstevel@tonic-gate /* set global af for use in p->af_status */ 30067c478bd9Sstevel@tonic-gate af = p->af_af; 30077c478bd9Sstevel@tonic-gate (*p->af_status)(0, flags); 30087c478bd9Sstevel@tonic-gate } 30097c478bd9Sstevel@tonic-gate 30107c478bd9Sstevel@tonic-gate /* 30117c478bd9Sstevel@tonic-gate * Historically, 'ether' has been an address family, 30127c478bd9Sstevel@tonic-gate * so print it here. 30137c478bd9Sstevel@tonic-gate */ 30147c478bd9Sstevel@tonic-gate print_ifether(name); 30157c478bd9Sstevel@tonic-gate } 30167c478bd9Sstevel@tonic-gate } 30177c478bd9Sstevel@tonic-gate 30187c478bd9Sstevel@tonic-gate /* 30197c478bd9Sstevel@tonic-gate * Print the status of the interface in a format that can be used to 30207c478bd9Sstevel@tonic-gate * reconfigure the interface later. Code stolen from status() above. 30217c478bd9Sstevel@tonic-gate */ 30227c478bd9Sstevel@tonic-gate /* ARGSUSED */ 30237c478bd9Sstevel@tonic-gate static int 30247c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param) 30257c478bd9Sstevel@tonic-gate { 3026e11c3f44Smeem char *cp; 30277c478bd9Sstevel@tonic-gate struct afswtch *p = afp; 30287c478bd9Sstevel@tonic-gate uint64_t flags; 3029e11c3f44Smeem char lifname[LIFNAMSIZ]; 30307c478bd9Sstevel@tonic-gate char if_usesrc_name[LIFNAMSIZ]; 30317c478bd9Sstevel@tonic-gate 30327c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 3033e11c3f44Smeem 30347c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 30357c478bd9Sstevel@tonic-gate Perror0_exit("status: SIOCGLIFFLAGS"); 30367c478bd9Sstevel@tonic-gate } 30377c478bd9Sstevel@tonic-gate flags = lifr.lifr_flags; 30387c478bd9Sstevel@tonic-gate 30397c478bd9Sstevel@tonic-gate if (debug) { 30407c478bd9Sstevel@tonic-gate (void) printf("configinfo: name %s flags 0x%llx af_af %d\n", 30417c478bd9Sstevel@tonic-gate name, flags, p != NULL ? p->af_af : -1); 30427c478bd9Sstevel@tonic-gate } 30437c478bd9Sstevel@tonic-gate 3044e11c3f44Smeem /* 304567ce1608SVasumathi Sundaram * Build the interface name to print (we cannot directly use `name' 3046e11c3f44Smeem * because one cannot "plumb" ":0" interfaces). 3047e11c3f44Smeem */ 3048e11c3f44Smeem (void) strlcpy(lifname, name, LIFNAMSIZ); 3049e11c3f44Smeem if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0) 3050e11c3f44Smeem *cp = '\0'; 30517c478bd9Sstevel@tonic-gate 30527c478bd9Sstevel@tonic-gate /* 30537c478bd9Sstevel@tonic-gate * if the interface is IPv4 30547c478bd9Sstevel@tonic-gate * if we have a IPv6 address family restriction return 30557c478bd9Sstevel@tonic-gate * so it won't print 30567c478bd9Sstevel@tonic-gate * if we are in IPv4 compatibility mode, clear out IFF_IPV4 30577c478bd9Sstevel@tonic-gate * so we don't print it. 30587c478bd9Sstevel@tonic-gate */ 30597c478bd9Sstevel@tonic-gate if (flags & IFF_IPV4) { 30607c478bd9Sstevel@tonic-gate if (p && p->af_af == AF_INET6) 30617c478bd9Sstevel@tonic-gate return (-1); 30627c478bd9Sstevel@tonic-gate if (v4compat) 30637c478bd9Sstevel@tonic-gate flags &= ~IFF_IPV4; 30647c478bd9Sstevel@tonic-gate 3065e11c3f44Smeem (void) printf("%s inet plumb", lifname); 30667c478bd9Sstevel@tonic-gate } else if (flags & IFF_IPV6) { 30677c478bd9Sstevel@tonic-gate /* 30687c478bd9Sstevel@tonic-gate * else if the interface is IPv6 30697c478bd9Sstevel@tonic-gate * if we have a IPv4 address family restriction return 30707c478bd9Sstevel@tonic-gate * or we are in IPv4 compatibiltiy mode, return. 30717c478bd9Sstevel@tonic-gate */ 30727c478bd9Sstevel@tonic-gate if (p && p->af_af == AF_INET) 30737c478bd9Sstevel@tonic-gate return (-1); 30747c478bd9Sstevel@tonic-gate if (v4compat) 30757c478bd9Sstevel@tonic-gate return (-1); 30767c478bd9Sstevel@tonic-gate 3077e11c3f44Smeem (void) printf("%s inet6 plumb", lifname); 30787c478bd9Sstevel@tonic-gate } 30797c478bd9Sstevel@tonic-gate 30807c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 30817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) { 30827c478bd9Sstevel@tonic-gate Perror0_exit("configinfo: SIOCGLIFMETRIC"); 30837c478bd9Sstevel@tonic-gate } else { 30847c478bd9Sstevel@tonic-gate if (lifr.lifr_metric) 30857c478bd9Sstevel@tonic-gate (void) printf(" metric %d ", lifr.lifr_metric); 30867c478bd9Sstevel@tonic-gate } 30877c478bd9Sstevel@tonic-gate if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) && 3088fc80c0dfSnordmark ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) 30897c478bd9Sstevel@tonic-gate (void) printf(" mtu %d", lifr.lifr_metric); 30907c478bd9Sstevel@tonic-gate 3091e11c3f44Smeem /* Index only applies to the zeroth interface */ 3092e11c3f44Smeem if (lifnum(name) == 0) { 30937c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) 30947c478bd9Sstevel@tonic-gate (void) printf(" index %d", lifr.lifr_index); 30957c478bd9Sstevel@tonic-gate } 30967c478bd9Sstevel@tonic-gate 30977c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) { 30987c478bd9Sstevel@tonic-gate if (lifr.lifr_index != 0) { 30997c478bd9Sstevel@tonic-gate if (if_indextoname(lifr.lifr_index, 31007c478bd9Sstevel@tonic-gate if_usesrc_name) != NULL) { 31017c478bd9Sstevel@tonic-gate (void) printf(" usesrc %s", if_usesrc_name); 31027c478bd9Sstevel@tonic-gate } 31037c478bd9Sstevel@tonic-gate } 31047c478bd9Sstevel@tonic-gate } 31057c478bd9Sstevel@tonic-gate 31067c478bd9Sstevel@tonic-gate if (p != NULL) { 31077c478bd9Sstevel@tonic-gate (*p->af_configinfo)(1, flags); 31087c478bd9Sstevel@tonic-gate } else { 31097c478bd9Sstevel@tonic-gate for (p = afs; p->af_name; p++) { 31107c478bd9Sstevel@tonic-gate (void) close(s); 31117c478bd9Sstevel@tonic-gate s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0); 31127c478bd9Sstevel@tonic-gate /* set global af for use in p->af_configinfo */ 31137c478bd9Sstevel@tonic-gate af = p->af_af; 31147c478bd9Sstevel@tonic-gate if (s == -1) { 31157c478bd9Sstevel@tonic-gate Perror0_exit("socket"); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate (*p->af_configinfo)(0, flags); 31187c478bd9Sstevel@tonic-gate } 31197c478bd9Sstevel@tonic-gate } 31207c478bd9Sstevel@tonic-gate 31212b24ab6bSSebastien Roy (void) putchar('\n'); 31227c478bd9Sstevel@tonic-gate return (0); 31237c478bd9Sstevel@tonic-gate } 31247c478bd9Sstevel@tonic-gate 31257c478bd9Sstevel@tonic-gate static void 31262b24ab6bSSebastien Roy print_tsec(iptun_params_t *params) 31277c478bd9Sstevel@tonic-gate { 31287c478bd9Sstevel@tonic-gate ipsec_req_t *ipsr; 31297c478bd9Sstevel@tonic-gate 31307c478bd9Sstevel@tonic-gate (void) printf("\ttunnel security settings "); 31312b24ab6bSSebastien Roy if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) { 31322b24ab6bSSebastien Roy (void) printf("--> use 'ipsecconf -ln -i %s'", name); 31338810c16bSdanmcd } else { 31342b24ab6bSSebastien Roy ipsr = ¶ms->iptun_param_secinfo; 31358810c16bSdanmcd if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) { 31368810c16bSdanmcd (void) printf("ah (%s) ", 31378810c16bSdanmcd rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH)); 31388810c16bSdanmcd } 31398810c16bSdanmcd if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) { 31408810c16bSdanmcd (void) printf("esp (%s", 31418810c16bSdanmcd rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP)); 31428810c16bSdanmcd (void) printf("/%s)", 31438810c16bSdanmcd rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH)); 31448810c16bSdanmcd } 31457c478bd9Sstevel@tonic-gate } 31467c478bd9Sstevel@tonic-gate (void) printf("\n"); 31477c478bd9Sstevel@tonic-gate } 31487c478bd9Sstevel@tonic-gate 31497c478bd9Sstevel@tonic-gate static void 31502b24ab6bSSebastien Roy tun_status(datalink_id_t linkid) 31517c478bd9Sstevel@tonic-gate { 31522b24ab6bSSebastien Roy iptun_params_t params; 31532b24ab6bSSebastien Roy char propval[DLADM_PROP_VAL_MAX]; 31542b24ab6bSSebastien Roy char *valptr[1]; 31552b24ab6bSSebastien Roy uint_t valcnt = 1; 31562b24ab6bSSebastien Roy boolean_t tabbed = _B_FALSE; 31572b24ab6bSSebastien Roy 31582b24ab6bSSebastien Roy params.iptun_param_linkid = linkid; 31592b24ab6bSSebastien Roy 31602b24ab6bSSebastien Roy /* If dladm_iptun_getparams() fails, assume we are not a tunnel. */ 31612b24ab6bSSebastien Roy assert(dlh_opened); 31622b24ab6bSSebastien Roy if (dladm_iptun_getparams(dlh, ¶ms, DLADM_OPT_ACTIVE) != 31632b24ab6bSSebastien Roy DLADM_STATUS_OK) 31647c478bd9Sstevel@tonic-gate return; 31657c478bd9Sstevel@tonic-gate 31662b24ab6bSSebastien Roy switch (params.iptun_param_type) { 31672b24ab6bSSebastien Roy case IPTUN_TYPE_IPV4: 31682b24ab6bSSebastien Roy case IPTUN_TYPE_6TO4: 31697c478bd9Sstevel@tonic-gate (void) printf("\tinet"); 31707c478bd9Sstevel@tonic-gate break; 31712b24ab6bSSebastien Roy case IPTUN_TYPE_IPV6: 31727c478bd9Sstevel@tonic-gate (void) printf("\tinet6"); 31737c478bd9Sstevel@tonic-gate break; 31747c478bd9Sstevel@tonic-gate default: 31752b24ab6bSSebastien Roy dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name); 31767c478bd9Sstevel@tonic-gate break; 31777c478bd9Sstevel@tonic-gate } 31787c478bd9Sstevel@tonic-gate 31797c478bd9Sstevel@tonic-gate /* 31802b24ab6bSSebastien Roy * There is always a source address. If it hasn't been explicitly 31812b24ab6bSSebastien Roy * set, the API will pass back a buffer containing the unspecified 31822b24ab6bSSebastien Roy * address. 31837c478bd9Sstevel@tonic-gate */ 31842b24ab6bSSebastien Roy (void) printf(" tunnel src %s ", params.iptun_param_laddr); 31852b24ab6bSSebastien Roy 31862b24ab6bSSebastien Roy if (params.iptun_param_flags & IPTUN_PARAM_RADDR) 31872b24ab6bSSebastien Roy (void) printf("tunnel dst %s\n", params.iptun_param_raddr); 31882b24ab6bSSebastien Roy else 31892b24ab6bSSebastien Roy (void) putchar('\n'); 31902b24ab6bSSebastien Roy 31912b24ab6bSSebastien Roy if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL) 31922b24ab6bSSebastien Roy print_tsec(¶ms); 31937c478bd9Sstevel@tonic-gate 31942b24ab6bSSebastien Roy valptr[0] = propval; 31952b24ab6bSSebastien Roy if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit", 31962b24ab6bSSebastien Roy (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 31972b24ab6bSSebastien Roy (void) printf("\ttunnel hop limit %s ", propval); 31987c478bd9Sstevel@tonic-gate tabbed = _B_TRUE; 31997c478bd9Sstevel@tonic-gate } 32007c478bd9Sstevel@tonic-gate 32012b24ab6bSSebastien Roy if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, 32022b24ab6bSSebastien Roy "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) { 32032b24ab6bSSebastien Roy uint32_t elim; 32042b24ab6bSSebastien Roy 32057c478bd9Sstevel@tonic-gate if (!tabbed) { 32062b24ab6bSSebastien Roy (void) putchar('\t'); 32077c478bd9Sstevel@tonic-gate tabbed = _B_TRUE; 32087c478bd9Sstevel@tonic-gate } 32092b24ab6bSSebastien Roy elim = strtol(propval, NULL, 10); 32102b24ab6bSSebastien Roy if (elim > 0) 32112b24ab6bSSebastien Roy (void) printf("tunnel encapsulation limit %s", propval); 32122b24ab6bSSebastien Roy else 32137c478bd9Sstevel@tonic-gate (void) printf("tunnel encapsulation limit disabled"); 32147c478bd9Sstevel@tonic-gate } 32157c478bd9Sstevel@tonic-gate 32167c478bd9Sstevel@tonic-gate if (tabbed) 32172b24ab6bSSebastien Roy (void) putchar('\n'); 32187c478bd9Sstevel@tonic-gate } 32197c478bd9Sstevel@tonic-gate 32207c478bd9Sstevel@tonic-gate static void 32217c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags) 32227c478bd9Sstevel@tonic-gate { 32232b24ab6bSSebastien Roy struct sockaddr_in *sin, *laddr; 32242b24ab6bSSebastien Roy struct sockaddr_in netmask = { AF_INET }; 32257c478bd9Sstevel@tonic-gate 32267c478bd9Sstevel@tonic-gate if (debug) 32277c478bd9Sstevel@tonic-gate (void) printf("in_status(%s) flags 0x%llx\n", name, flags); 32287c478bd9Sstevel@tonic-gate 32297c478bd9Sstevel@tonic-gate /* only print status for IPv4 interfaces */ 32307c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV4)) 32317c478bd9Sstevel@tonic-gate return; 32327c478bd9Sstevel@tonic-gate 32337c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 32347c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32357c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 32367c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 32377c478bd9Sstevel@tonic-gate errno == ENXIO) { 32387c478bd9Sstevel@tonic-gate if (!force) 32397c478bd9Sstevel@tonic-gate return; 32407c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32417c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32427c478bd9Sstevel@tonic-gate } else 32437c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFADDR"); 32447c478bd9Sstevel@tonic-gate } 32457c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 32467c478bd9Sstevel@tonic-gate (void) printf("\tinet %s ", inet_ntoa(sin->sin_addr)); 32477c478bd9Sstevel@tonic-gate laddr = sin; 32487c478bd9Sstevel@tonic-gate } else { 32497c478bd9Sstevel@tonic-gate (void) printf("\tinet "); 32507c478bd9Sstevel@tonic-gate } 32517c478bd9Sstevel@tonic-gate 32527c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32537c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 32547c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 32557c478bd9Sstevel@tonic-gate errno == ENXIO) { 32567c478bd9Sstevel@tonic-gate if (!force) 32577c478bd9Sstevel@tonic-gate return; 32587c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32597c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32607c478bd9Sstevel@tonic-gate } else { 32617c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFSUBNET"); 32627c478bd9Sstevel@tonic-gate } 32637c478bd9Sstevel@tonic-gate } 32647c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 32657c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 32667c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 32677c478bd9Sstevel@tonic-gate (void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr), 32687c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 32697c478bd9Sstevel@tonic-gate } 32707c478bd9Sstevel@tonic-gate if (sin->sin_family != AF_INET) { 32717c478bd9Sstevel@tonic-gate (void) printf("Wrong family: %d\n", sin->sin_family); 32727c478bd9Sstevel@tonic-gate } 32737c478bd9Sstevel@tonic-gate 32747c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32757c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 32767c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 32777c478bd9Sstevel@tonic-gate Perror0_exit("in_status: SIOCGLIFNETMASK"); 32787c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 32797c478bd9Sstevel@tonic-gate } else 32807c478bd9Sstevel@tonic-gate netmask.sin_addr = 32817c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 32827c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 32837c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32847c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 32857c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 32867c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 32877c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 32887c478bd9Sstevel@tonic-gate else 3289fc80c0dfSnordmark Perror0_exit("in_status: SIOCGLIFDSTADDR"); 32907c478bd9Sstevel@tonic-gate } 32917c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 32927c478bd9Sstevel@tonic-gate (void) printf("--> %s ", inet_ntoa(sin->sin_addr)); 32937c478bd9Sstevel@tonic-gate } 32947c478bd9Sstevel@tonic-gate (void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr)); 32957c478bd9Sstevel@tonic-gate if (flags & IFF_BROADCAST) { 32967c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 32977c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 32987c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 32997c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33007c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33017c478bd9Sstevel@tonic-gate else 3302fc80c0dfSnordmark Perror0_exit("in_status: SIOCGLIFBRDADDR"); 33037c478bd9Sstevel@tonic-gate } 33047c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 33057c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr != 0) { 33067c478bd9Sstevel@tonic-gate (void) printf("broadcast %s", 33077c478bd9Sstevel@tonic-gate inet_ntoa(sin->sin_addr)); 33087c478bd9Sstevel@tonic-gate } 33097c478bd9Sstevel@tonic-gate } 3310e11c3f44Smeem /* If there is a groupname, print it for only the physical interface */ 33117c478bd9Sstevel@tonic-gate if (strchr(name, ':') == NULL) { 3312e11c3f44Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3313e11c3f44Smeem lifr.lifr_groupname[0] != '\0') { 3314e11c3f44Smeem (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 33157c478bd9Sstevel@tonic-gate } 33167c478bd9Sstevel@tonic-gate } 33177c478bd9Sstevel@tonic-gate (void) putchar('\n'); 33187c478bd9Sstevel@tonic-gate } 33197c478bd9Sstevel@tonic-gate 33207c478bd9Sstevel@tonic-gate static void 33217c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags) 33227c478bd9Sstevel@tonic-gate { 33232b24ab6bSSebastien Roy char abuf[INET6_ADDRSTRLEN]; 33242b24ab6bSSebastien Roy struct sockaddr_in6 *sin6, *laddr6; 33257c478bd9Sstevel@tonic-gate 33267c478bd9Sstevel@tonic-gate if (debug) 33277c478bd9Sstevel@tonic-gate (void) printf("in6_status(%s) flags 0x%llx\n", name, flags); 33287c478bd9Sstevel@tonic-gate 33297c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV6)) 33307c478bd9Sstevel@tonic-gate return; 33317c478bd9Sstevel@tonic-gate 33327c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 33337c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33347c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 33357c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 33367c478bd9Sstevel@tonic-gate errno == ENXIO) { 33377c478bd9Sstevel@tonic-gate if (!force) 33387c478bd9Sstevel@tonic-gate return; 33397c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33407c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33417c478bd9Sstevel@tonic-gate } else 33427c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFADDR"); 33437c478bd9Sstevel@tonic-gate } 33447c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 33457c478bd9Sstevel@tonic-gate (void) printf("\tinet6 %s/%d ", 33467c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33477c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 33487c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 33497c478bd9Sstevel@tonic-gate laddr6 = sin6; 33507c478bd9Sstevel@tonic-gate } else { 33517c478bd9Sstevel@tonic-gate (void) printf("\tinet6 "); 33527c478bd9Sstevel@tonic-gate } 33537c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33547c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 33557c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 33567c478bd9Sstevel@tonic-gate errno == ENXIO) { 33577c478bd9Sstevel@tonic-gate if (!force) 33587c478bd9Sstevel@tonic-gate return; 33597c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33607c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33617c478bd9Sstevel@tonic-gate } else 33627c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFSUBNET"); 33637c478bd9Sstevel@tonic-gate } 33647c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 33657c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 33667c478bd9Sstevel@tonic-gate !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 33677c478bd9Sstevel@tonic-gate (void) printf("subnet %s/%d ", 33687c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33697c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 33707c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 33717c478bd9Sstevel@tonic-gate } 33727c478bd9Sstevel@tonic-gate if (sin6->sin6_family != AF_INET6) { 33737c478bd9Sstevel@tonic-gate (void) printf("Wrong family: %d\n", sin6->sin6_family); 33747c478bd9Sstevel@tonic-gate } 33757c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 33767c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33777c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 33787c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 33797c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33807c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33817c478bd9Sstevel@tonic-gate else 3382fc80c0dfSnordmark Perror0_exit("in_status6: SIOCGLIFDSTADDR"); 33837c478bd9Sstevel@tonic-gate } 33847c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 33857c478bd9Sstevel@tonic-gate (void) printf("--> %s ", 33867c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 33877c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 33887c478bd9Sstevel@tonic-gate } 33897c478bd9Sstevel@tonic-gate if (verbose) { 33907c478bd9Sstevel@tonic-gate (void) putchar('\n'); 33917c478bd9Sstevel@tonic-gate (void) putchar('\t'); 33927c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 33937c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 33947c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EINVAL) 33957c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 33967c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 33977c478bd9Sstevel@tonic-gate else 3398fc80c0dfSnordmark Perror0_exit("in_status6: SIOCGLIFTOKEN"); 33997c478bd9Sstevel@tonic-gate } else { 34007c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 34017c478bd9Sstevel@tonic-gate (void) printf("token %s/%d ", 34027c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 34037c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 34047c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 34057c478bd9Sstevel@tonic-gate } 34067c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) { 34077c478bd9Sstevel@tonic-gate if (errno != EINVAL) { 34087c478bd9Sstevel@tonic-gate Perror0_exit("in_status6: SIOCGLIFLNKINFO"); 34097c478bd9Sstevel@tonic-gate } 34107c478bd9Sstevel@tonic-gate } else { 34117c478bd9Sstevel@tonic-gate (void) printf("maxhops %u, reachtime %u ms, " 34127c478bd9Sstevel@tonic-gate "reachretrans %u ms, maxmtu %u ", 34137c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_maxhops, 34147c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_reachtime, 34157c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_reachretrans, 34167c478bd9Sstevel@tonic-gate lifr.lifr_ifinfo.lir_maxmtu); 34177c478bd9Sstevel@tonic-gate } 34187c478bd9Sstevel@tonic-gate } 3419f7d61273Smeem /* If there is a groupname, print it for only the physical interface */ 34207c478bd9Sstevel@tonic-gate if (strchr(name, ':') == NULL) { 3421f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3422f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3423f7d61273Smeem (void) printf("\n\tgroupname %s", lifr.lifr_groupname); 34247c478bd9Sstevel@tonic-gate } 34257c478bd9Sstevel@tonic-gate } 34267c478bd9Sstevel@tonic-gate (void) putchar('\n'); 34277c478bd9Sstevel@tonic-gate } 34287c478bd9Sstevel@tonic-gate 34297c478bd9Sstevel@tonic-gate static void 34307c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags) 34317c478bd9Sstevel@tonic-gate { 34327c478bd9Sstevel@tonic-gate struct sockaddr_in *sin, *laddr; 34337c478bd9Sstevel@tonic-gate struct sockaddr_in netmask = { AF_INET }; 34347c478bd9Sstevel@tonic-gate 34357c478bd9Sstevel@tonic-gate if (debug) 34367c478bd9Sstevel@tonic-gate (void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags); 34377c478bd9Sstevel@tonic-gate 34387c478bd9Sstevel@tonic-gate /* only configinfo info for IPv4 interfaces */ 34397c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV4)) 34407c478bd9Sstevel@tonic-gate return; 34417c478bd9Sstevel@tonic-gate 34427c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 34437c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34447c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 34457c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 34467c478bd9Sstevel@tonic-gate errno == ENXIO) { 34477c478bd9Sstevel@tonic-gate if (!force) 34487c478bd9Sstevel@tonic-gate return; 34497c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34507c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34517c478bd9Sstevel@tonic-gate } else 34527c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFADDR"); 34537c478bd9Sstevel@tonic-gate } 34547c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 3455e11c3f44Smeem (void) printf(" set %s ", inet_ntoa(sin->sin_addr)); 34567c478bd9Sstevel@tonic-gate laddr = sin; 34577c478bd9Sstevel@tonic-gate } 34587c478bd9Sstevel@tonic-gate 34597c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34607c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 34617c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 34627c478bd9Sstevel@tonic-gate errno == ENXIO) { 34637c478bd9Sstevel@tonic-gate if (!force) 34647c478bd9Sstevel@tonic-gate return; 34657c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34667c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34677c478bd9Sstevel@tonic-gate } else { 34687c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFSUBNET"); 34697c478bd9Sstevel@tonic-gate } 34707c478bd9Sstevel@tonic-gate } 34717c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 34727c478bd9Sstevel@tonic-gate 34737c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 34747c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr != laddr->sin_addr.s_addr) { 34757c478bd9Sstevel@tonic-gate (void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr), 34767c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 34777c478bd9Sstevel@tonic-gate } 34787c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34797c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) { 34807c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) 34817c478bd9Sstevel@tonic-gate Perror0_exit("in_configinfo: SIOCGLIFNETMASK"); 34827c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 34837c478bd9Sstevel@tonic-gate } else 34847c478bd9Sstevel@tonic-gate netmask.sin_addr = 34857c478bd9Sstevel@tonic-gate ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr; 34867c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 34877c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 34887c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 34897c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 34907c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 34917c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 34927c478bd9Sstevel@tonic-gate else 3493fc80c0dfSnordmark Perror0_exit("in_configinfo: SIOCGLIFDSTADDR"); 34947c478bd9Sstevel@tonic-gate } 34957c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_dstaddr; 34967c478bd9Sstevel@tonic-gate (void) printf(" destination %s ", inet_ntoa(sin->sin_addr)); 34977c478bd9Sstevel@tonic-gate } 34987c478bd9Sstevel@tonic-gate (void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr)); 34997c478bd9Sstevel@tonic-gate if (flags & IFF_BROADCAST) { 35007c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35017c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) { 35027c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 35037c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35047c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35057c478bd9Sstevel@tonic-gate else 3506fc80c0dfSnordmark Perror0_exit("in_configinfo: SIOCGLIFBRDADDR"); 35077c478bd9Sstevel@tonic-gate } 35087c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)&lifr.lifr_addr; 35097c478bd9Sstevel@tonic-gate if (sin->sin_addr.s_addr != 0) { 35107c478bd9Sstevel@tonic-gate (void) printf(" broadcast %s ", 35117c478bd9Sstevel@tonic-gate inet_ntoa(sin->sin_addr)); 35127c478bd9Sstevel@tonic-gate } 35137c478bd9Sstevel@tonic-gate } 35147c478bd9Sstevel@tonic-gate 3515e11c3f44Smeem /* If there is a groupname, print it for only the zeroth interface */ 3516e11c3f44Smeem if (lifnum(name) == 0) { 3517f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3518f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3519f7d61273Smeem (void) printf(" group %s ", lifr.lifr_groupname); 35207c478bd9Sstevel@tonic-gate } 35217c478bd9Sstevel@tonic-gate } 35227c478bd9Sstevel@tonic-gate 35237c478bd9Sstevel@tonic-gate /* Print flags to configure */ 3524e11c3f44Smeem print_config_flags(AF_INET, flags); 35257c478bd9Sstevel@tonic-gate } 35267c478bd9Sstevel@tonic-gate 35277c478bd9Sstevel@tonic-gate static void 35287c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags) 35297c478bd9Sstevel@tonic-gate { 35307c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 35317c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6, *laddr6; 35327c478bd9Sstevel@tonic-gate 35337c478bd9Sstevel@tonic-gate if (debug) 35347c478bd9Sstevel@tonic-gate (void) printf("in6_configinfo(%s) flags 0x%llx\n", name, 35357c478bd9Sstevel@tonic-gate flags); 35367c478bd9Sstevel@tonic-gate 35377c478bd9Sstevel@tonic-gate if (!(flags & IFF_IPV6)) 35387c478bd9Sstevel@tonic-gate return; 35397c478bd9Sstevel@tonic-gate 35407c478bd9Sstevel@tonic-gate if (!(flags & IFF_NOLOCAL)) { 35417c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35427c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 35437c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 35447c478bd9Sstevel@tonic-gate errno == ENXIO) { 35457c478bd9Sstevel@tonic-gate if (!force) 35467c478bd9Sstevel@tonic-gate return; 35477c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35487c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35497c478bd9Sstevel@tonic-gate } else 35507c478bd9Sstevel@tonic-gate Perror0_exit("in6_configinfo: SIOCGLIFADDR"); 35517c478bd9Sstevel@tonic-gate } 35527c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 3553e11c3f44Smeem (void) printf(" set %s/%d ", 3554e11c3f44Smeem inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)), 3555e11c3f44Smeem lifr.lifr_addrlen); 35567c478bd9Sstevel@tonic-gate laddr6 = sin6; 35577c478bd9Sstevel@tonic-gate } 35587c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35597c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) { 35607c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT || 35617c478bd9Sstevel@tonic-gate errno == ENXIO) { 35627c478bd9Sstevel@tonic-gate if (!force) 35637c478bd9Sstevel@tonic-gate return; 35647c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35657c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35667c478bd9Sstevel@tonic-gate } else 35677c478bd9Sstevel@tonic-gate Perror0_exit("in6_configinfo: SIOCGLIFSUBNET"); 35687c478bd9Sstevel@tonic-gate } 35697c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 35707c478bd9Sstevel@tonic-gate if ((flags & IFF_NOLOCAL) || 35717c478bd9Sstevel@tonic-gate !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) { 35727c478bd9Sstevel@tonic-gate (void) printf(" subnet %s/%d ", 35737c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 35747c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 35757c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 35767c478bd9Sstevel@tonic-gate } 35777c478bd9Sstevel@tonic-gate 35787c478bd9Sstevel@tonic-gate if (flags & IFF_POINTOPOINT) { 35797c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35807c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) { 35817c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL) 35827c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35837c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35847c478bd9Sstevel@tonic-gate else 3585fc80c0dfSnordmark Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR"); 35867c478bd9Sstevel@tonic-gate } 35877c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr; 35887c478bd9Sstevel@tonic-gate (void) printf(" destination %s ", 35897c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 35907c478bd9Sstevel@tonic-gate abuf, sizeof (abuf))); 35917c478bd9Sstevel@tonic-gate } 35927c478bd9Sstevel@tonic-gate 35937c478bd9Sstevel@tonic-gate (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 35947c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) { 35957c478bd9Sstevel@tonic-gate if (errno == EADDRNOTAVAIL || errno == EINVAL) 35967c478bd9Sstevel@tonic-gate (void) memset(&lifr.lifr_addr, 0, 35977c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_addr)); 35987c478bd9Sstevel@tonic-gate else 3599fc80c0dfSnordmark Perror0_exit("in6_configinfo: SIOCGLIFTOKEN"); 36007c478bd9Sstevel@tonic-gate } else { 36017c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr; 36027c478bd9Sstevel@tonic-gate (void) printf(" token %s/%d ", 36037c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, 36047c478bd9Sstevel@tonic-gate abuf, sizeof (abuf)), 36057c478bd9Sstevel@tonic-gate lifr.lifr_addrlen); 36067c478bd9Sstevel@tonic-gate } 36077c478bd9Sstevel@tonic-gate 3608e11c3f44Smeem /* If there is a groupname, print it for only the zeroth interface */ 3609e11c3f44Smeem if (lifnum(name) == 0) { 3610f7d61273Smeem if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 && 3611f7d61273Smeem lifr.lifr_groupname[0] != '\0') { 3612f7d61273Smeem (void) printf(" group %s ", lifr.lifr_groupname); 36137c478bd9Sstevel@tonic-gate } 36147c478bd9Sstevel@tonic-gate } 36157c478bd9Sstevel@tonic-gate 36167c478bd9Sstevel@tonic-gate /* Print flags to configure */ 3617e11c3f44Smeem print_config_flags(AF_INET6, flags); 36187c478bd9Sstevel@tonic-gate } 36197c478bd9Sstevel@tonic-gate 36207c478bd9Sstevel@tonic-gate /* 36217c478bd9Sstevel@tonic-gate * If this is a physical interface then remove it. 36227c478bd9Sstevel@tonic-gate * If it is a logical interface name use SIOCLIFREMOVEIF to 36237c478bd9Sstevel@tonic-gate * remove it. In both cases fail if it doesn't exist. 36247c478bd9Sstevel@tonic-gate */ 36257c478bd9Sstevel@tonic-gate /* ARGSUSED */ 36267c478bd9Sstevel@tonic-gate static int 36277c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param) 36287c478bd9Sstevel@tonic-gate { 36296e91bba0SGirish Moodalbail ipadm_status_t istatus; 3630e11c3f44Smeem 36316e91bba0SGirish Moodalbail istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 36326e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 36336e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n", 36346e91bba0SGirish Moodalbail name, ipadm_status2str(istatus)); 36356e91bba0SGirish Moodalbail exit(1); 36367c478bd9Sstevel@tonic-gate } 36377c478bd9Sstevel@tonic-gate 36387c478bd9Sstevel@tonic-gate return (0); 36397c478bd9Sstevel@tonic-gate } 36407c478bd9Sstevel@tonic-gate 36417c478bd9Sstevel@tonic-gate /* 36426e91bba0SGirish Moodalbail * Create the interface in `name', using ipadm_create_if(). If `name' is a 36436e91bba0SGirish Moodalbail * logical interface or loopback interface, ipadm_create_if() uses 36446e91bba0SGirish Moodalbail * SIOCLIFADDIF to create it. 36457c478bd9Sstevel@tonic-gate */ 36467c478bd9Sstevel@tonic-gate /* ARGSUSED */ 36477c478bd9Sstevel@tonic-gate static int 36487c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param) 36497c478bd9Sstevel@tonic-gate { 36506e91bba0SGirish Moodalbail ipadm_status_t istatus; 36517c478bd9Sstevel@tonic-gate 36526e91bba0SGirish Moodalbail istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE); 36536e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 36546e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n", 36556e91bba0SGirish Moodalbail name, ipadm_status2str(istatus)); 36566e91bba0SGirish Moodalbail if (istatus != IPADM_IF_EXISTS) 36576e91bba0SGirish Moodalbail exit(1); 3658f4b3ec61Sdh } 3659e11c3f44Smeem return (0); 3660e11c3f44Smeem } 3661e11c3f44Smeem 3662e11c3f44Smeem /* ARGSUSED */ 3663e11c3f44Smeem static int 3664e11c3f44Smeem inetipmp(char *arg, int64_t param) 3665e11c3f44Smeem { 3666e11c3f44Smeem int retval; 3667e11c3f44Smeem 3668e11c3f44Smeem /* 3669e11c3f44Smeem * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb". 3670e11c3f44Smeem * Otherwise, try to create the requested IPMP interface. 3671e11c3f44Smeem */ 3672e11c3f44Smeem if (strchr(name, ':') != NULL) 3673e11c3f44Smeem retval = inetplumb(arg, param); 3674e11c3f44Smeem else 3675e11c3f44Smeem retval = create_ipmp(name, afp->af_af, name, _B_FALSE); 3676e11c3f44Smeem 3677e11c3f44Smeem /* 3678e11c3f44Smeem * We'd return -1, but foreachinterface() doesn't propagate the error 3679e11c3f44Smeem * into the exit status, so we're forced to explicitly exit(). 3680e11c3f44Smeem */ 3681e11c3f44Smeem if (retval == -1) 3682e11c3f44Smeem exit(1); 36837c478bd9Sstevel@tonic-gate return (0); 36847c478bd9Sstevel@tonic-gate } 36857c478bd9Sstevel@tonic-gate 3686e11c3f44Smeem /* 3687e11c3f44Smeem * Create an IPMP group `grname' with address family `af'. If `ifname' is 3688e11c3f44Smeem * non-NULL, it specifies the interface name to use. Otherwise, use the name 3689e11c3f44Smeem * ipmpN, where N corresponds to the lowest available integer. If `implicit' 3690e11c3f44Smeem * is set, then the group is being created as a side-effect of placing an 3691e11c3f44Smeem * underlying interface in a group. Also start in.mpathd if necessary. 3692e11c3f44Smeem */ 3693e11c3f44Smeem static int 3694e11c3f44Smeem create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit) 3695e11c3f44Smeem { 3696e11c3f44Smeem static int ipmp_daemon_started; 36976e91bba0SGirish Moodalbail uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE; 36986e91bba0SGirish Moodalbail ipadm_status_t istatus; 3699e11c3f44Smeem 3700e11c3f44Smeem if (debug) { 3701e11c3f44Smeem (void) printf("create_ipmp: ifname %s grname %s af %d\n", 3702e11c3f44Smeem ifname != NULL ? ifname : "NULL", grname, af); 3703e11c3f44Smeem } 3704e11c3f44Smeem 37056e91bba0SGirish Moodalbail /* 37066e91bba0SGirish Moodalbail * ipadm_create_if() creates the IPMP interface and fills in the 37076e91bba0SGirish Moodalbail * ppa in lifr.lifr_name, if `ifname'="ipmp". 37086e91bba0SGirish Moodalbail */ 37096e91bba0SGirish Moodalbail (void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"), 37106e91bba0SGirish Moodalbail sizeof (lifr.lifr_name)); 37116e91bba0SGirish Moodalbail if (ifname == NULL) 37126e91bba0SGirish Moodalbail flags |= IPADM_OPT_GENPPA; 37136e91bba0SGirish Moodalbail istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags); 37146e91bba0SGirish Moodalbail if (istatus != IPADM_SUCCESS) { 37156e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: cannot create IPMP interface " 37166e91bba0SGirish Moodalbail "%s: %s\n", grname, ipadm_status2str(istatus)); 3717e11c3f44Smeem return (-1); 3718e11c3f44Smeem } 3719e11c3f44Smeem 3720e11c3f44Smeem /* 3721e11c3f44Smeem * To preserve backward-compatibility, always bring up the link-local 3722e11c3f44Smeem * address for implicitly-created IPv6 IPMP interfaces. 3723e11c3f44Smeem */ 3724e11c3f44Smeem if (implicit && af == AF_INET6) { 3725e11c3f44Smeem if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) { 3726e11c3f44Smeem lifr.lifr_flags |= IFF_UP; 3727e11c3f44Smeem (void) ioctl(s6, SIOCSLIFFLAGS, &lifr); 3728e11c3f44Smeem } 3729e11c3f44Smeem } 3730e11c3f44Smeem 3731e11c3f44Smeem /* 3732e11c3f44Smeem * If the caller requested a different group name, issue a 3733e11c3f44Smeem * SIOCSLIFGROUPNAME on the new IPMP interface. 3734e11c3f44Smeem */ 3735e11c3f44Smeem if (strcmp(lifr.lifr_name, grname) != 0) { 3736e11c3f44Smeem (void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ); 3737e11c3f44Smeem if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) { 3738e11c3f44Smeem Perror0("SIOCSLIFGROUPNAME"); 3739e11c3f44Smeem return (-1); 3740e11c3f44Smeem } 3741e11c3f44Smeem } 3742e11c3f44Smeem 3743e11c3f44Smeem /* 3744e11c3f44Smeem * If we haven't done so yet, ensure in.mpathd is started. 3745e11c3f44Smeem */ 3746e11c3f44Smeem if (ipmp_daemon_started++ == 0) 3747e11c3f44Smeem start_ipmp_daemon(); 3748e11c3f44Smeem 3749e11c3f44Smeem return (0); 3750e11c3f44Smeem } 3751e11c3f44Smeem 3752e11c3f44Smeem /* 3753e11c3f44Smeem * Start in.mpathd if it's not already running. 3754e11c3f44Smeem */ 3755e11c3f44Smeem static void 3756e11c3f44Smeem start_ipmp_daemon(void) 3757e11c3f44Smeem { 3758e11c3f44Smeem int retval; 3759e11c3f44Smeem ipmp_handle_t ipmp_handle; 3760e11c3f44Smeem 3761e11c3f44Smeem /* 3762e11c3f44Smeem * Ping in.mpathd to see if it's running already. 3763e11c3f44Smeem */ 3764e11c3f44Smeem if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) { 3765e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot create IPMP handle: " 3766e11c3f44Smeem "%s\n", ipmp_errmsg(retval)); 3767e11c3f44Smeem return; 3768e11c3f44Smeem } 3769e11c3f44Smeem 3770e11c3f44Smeem retval = ipmp_ping_daemon(ipmp_handle); 3771e11c3f44Smeem ipmp_close(ipmp_handle); 3772e11c3f44Smeem 3773e11c3f44Smeem switch (retval) { 3774e11c3f44Smeem case IPMP_ENOMPATHD: 3775e11c3f44Smeem break; 3776e11c3f44Smeem case IPMP_SUCCESS: 3777e11c3f44Smeem return; 3778e11c3f44Smeem default: 3779e11c3f44Smeem (void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n", 3780e11c3f44Smeem ipmp_errmsg(retval)); 3781e11c3f44Smeem break; 3782e11c3f44Smeem } 3783e11c3f44Smeem 3784e11c3f44Smeem /* 3785e11c3f44Smeem * Start in.mpathd. Note that in.mpathd will handle multiple 3786e11c3f44Smeem * incarnations (ipmp_ping_daemon() is just an optimization) so we 3787e11c3f44Smeem * don't need to worry about racing with another ifconfig process. 3788e11c3f44Smeem */ 3789e11c3f44Smeem switch (fork()) { 3790e11c3f44Smeem case -1: 3791e11c3f44Smeem Perror0_exit("start_ipmp_daemon: fork"); 3792e11c3f44Smeem /* NOTREACHED */ 3793e11c3f44Smeem case 0: 3794e11c3f44Smeem (void) execl(MPATHD_PATH, MPATHD_PATH, NULL); 3795e11c3f44Smeem _exit(1); 3796e11c3f44Smeem /* NOTREACHED */ 3797e11c3f44Smeem default: 3798e11c3f44Smeem break; 3799e11c3f44Smeem } 3800e11c3f44Smeem } 3801e11c3f44Smeem 3802e11c3f44Smeem /* 3803e11c3f44Smeem * Bring the address named by `ifaddrp' up or down. Doesn't trust any mutable 3804e11c3f44Smeem * values in ia_flags since they may be stale. 3805e11c3f44Smeem */ 3806e11c3f44Smeem static boolean_t 3807e11c3f44Smeem ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up) 3808e11c3f44Smeem { 3809e11c3f44Smeem struct lifreq lifrl; /* Local lifreq struct */ 3810e11c3f44Smeem int fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6; 3811e11c3f44Smeem 3812e11c3f44Smeem (void) memset(&lifrl, 0, sizeof (lifrl)); 3813e11c3f44Smeem (void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ); 3814e11c3f44Smeem if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1) 3815e11c3f44Smeem return (_B_FALSE); 3816e11c3f44Smeem 3817614f1612Smeem if (up) 3818e11c3f44Smeem lifrl.lifr_flags |= IFF_UP; 3819614f1612Smeem else 3820e11c3f44Smeem lifrl.lifr_flags &= ~IFF_UP; 3821614f1612Smeem 3822614f1612Smeem if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1) 3823614f1612Smeem return (_B_FALSE); 3824614f1612Smeem 3825614f1612Smeem /* 3826614f1612Smeem * If we're trying to bring the address down, ensure that DAD activity 3827614f1612Smeem * (observable by IFF_DUPLICATE) has also been stopped. 3828614f1612Smeem */ 3829614f1612Smeem if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 && 3830614f1612Smeem lifrl.lifr_flags & IFF_DUPLICATE) { 3831614f1612Smeem if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 || 3832614f1612Smeem ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) { 3833614f1612Smeem return (_B_FALSE); 3834614f1612Smeem } 3835e11c3f44Smeem } 3836614f1612Smeem return (_B_TRUE); 3837e11c3f44Smeem } 3838e11c3f44Smeem 3839e11c3f44Smeem static boolean_t 3840e11c3f44Smeem ifaddr_up(ifaddrlistx_t *ifaddrp) 3841e11c3f44Smeem { 3842e11c3f44Smeem return (ifaddr_op(ifaddrp, _B_TRUE)); 3843e11c3f44Smeem } 3844e11c3f44Smeem 3845e11c3f44Smeem static boolean_t 3846e11c3f44Smeem ifaddr_down(ifaddrlistx_t *ifaddrp) 3847e11c3f44Smeem { 3848e11c3f44Smeem return (ifaddr_op(ifaddrp, _B_FALSE)); 3849e11c3f44Smeem } 3850e11c3f44Smeem 38512b24ab6bSSebastien Roy /* 38522b24ab6bSSebastien Roy * Open the global libdladm handle "dlh" if it isn't already opened. The 38532b24ab6bSSebastien Roy * caller may optionally supply a link name to obtain its linkid. If a link 38542b24ab6bSSebastien Roy * of a specific class or classes is required, reqclass specifies the class 38552b24ab6bSSebastien Roy * mask. 38562b24ab6bSSebastien Roy */ 38572b24ab6bSSebastien Roy static dladm_status_t 38582b24ab6bSSebastien Roy ifconfig_dladm_open(const char *name, datalink_class_t reqclass, 38592b24ab6bSSebastien Roy datalink_id_t *linkid) 38602b24ab6bSSebastien Roy { 38612b24ab6bSSebastien Roy dladm_status_t status = DLADM_STATUS_OK; 38622b24ab6bSSebastien Roy datalink_class_t class; 38632b24ab6bSSebastien Roy 38642b24ab6bSSebastien Roy if (!dlh_opened) { 38652b24ab6bSSebastien Roy if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK) 38662b24ab6bSSebastien Roy return (status); 38672b24ab6bSSebastien Roy dlh_opened = _B_TRUE; 38682b24ab6bSSebastien Roy } 38692b24ab6bSSebastien Roy if (name != NULL) { 38702b24ab6bSSebastien Roy status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL); 38712b24ab6bSSebastien Roy if (status == DLADM_STATUS_OK) { 38722b24ab6bSSebastien Roy if (!(class & reqclass)) 38732b24ab6bSSebastien Roy status = DLADM_STATUS_LINKINVAL; 38742b24ab6bSSebastien Roy } 38752b24ab6bSSebastien Roy } 38762b24ab6bSSebastien Roy return (status); 38772b24ab6bSSebastien Roy } 38782b24ab6bSSebastien Roy 38796e91bba0SGirish Moodalbail /* 38806e91bba0SGirish Moodalbail * This function checks if we can use libipadm API's. We will only 38816e91bba0SGirish Moodalbail * call libipadm functions for non-IPMP interfaces. This check is 38826e91bba0SGirish Moodalbail * temporary until libipadm supports IPMP interfaces. 38836e91bba0SGirish Moodalbail */ 38846e91bba0SGirish Moodalbail static boolean_t 38856e91bba0SGirish Moodalbail ifconfig_use_libipadm(int s, const char *lifname) 38866e91bba0SGirish Moodalbail { 38876e91bba0SGirish Moodalbail struct lifreq lifr1; 38886e91bba0SGirish Moodalbail 38896e91bba0SGirish Moodalbail (void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name)); 38906e91bba0SGirish Moodalbail if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) { 38916e91bba0SGirish Moodalbail (void) strncpy(lifr.lifr_name, lifname, 38926e91bba0SGirish Moodalbail sizeof (lifr.lifr_name)); 38936e91bba0SGirish Moodalbail Perror0_exit("error"); 38946e91bba0SGirish Moodalbail } 38956e91bba0SGirish Moodalbail 38966e91bba0SGirish Moodalbail return (lifr1.lifr_groupname[0] == '\0'); 38976e91bba0SGirish Moodalbail } 38986e91bba0SGirish Moodalbail 38996e91bba0SGirish Moodalbail static void 39006e91bba0SGirish Moodalbail ipadmerr_exit(ipadm_status_t status, const char *str) 39016e91bba0SGirish Moodalbail { 39026e91bba0SGirish Moodalbail (void) fprintf(stderr, "ifconfig: %s: %s\n", str, 39036e91bba0SGirish Moodalbail ipadm_status2str(status)); 39046e91bba0SGirish Moodalbail exit(1); 39056e91bba0SGirish Moodalbail } 39066e91bba0SGirish Moodalbail 39076e91bba0SGirish Moodalbail static void 39082b24ab6bSSebastien Roy dladmerr_exit(dladm_status_t status, const char *str) 39092b24ab6bSSebastien Roy { 39102b24ab6bSSebastien Roy char errstr[DLADM_STRSIZE]; 39112b24ab6bSSebastien Roy 39122b24ab6bSSebastien Roy (void) fprintf(stderr, "%s: %s\n", str, 39132b24ab6bSSebastien Roy dladm_status2str(status, errstr)); 39142b24ab6bSSebastien Roy exit(1); 39152b24ab6bSSebastien Roy } 39162b24ab6bSSebastien Roy 39177c478bd9Sstevel@tonic-gate void 3918f7d61273Smeem Perror0(const char *cmd) 39197c478bd9Sstevel@tonic-gate { 3920f7d61273Smeem Perror2(cmd, lifr.lifr_name); 39217c478bd9Sstevel@tonic-gate } 39227c478bd9Sstevel@tonic-gate 39237c478bd9Sstevel@tonic-gate void 3924f7d61273Smeem Perror0_exit(const char *cmd) 39257c478bd9Sstevel@tonic-gate { 39267c478bd9Sstevel@tonic-gate Perror0(cmd); 39277c478bd9Sstevel@tonic-gate exit(1); 39287c478bd9Sstevel@tonic-gate } 39297c478bd9Sstevel@tonic-gate 39307c478bd9Sstevel@tonic-gate void 3931f7d61273Smeem Perror2(const char *cmd, const char *str) 39327c478bd9Sstevel@tonic-gate { 3933f7d61273Smeem int error = errno; 39347c478bd9Sstevel@tonic-gate 3935f7d61273Smeem (void) fprintf(stderr, "ifconfig: %s: ", cmd); 39367c478bd9Sstevel@tonic-gate 3937f7d61273Smeem switch (error) { 39387c478bd9Sstevel@tonic-gate case ENXIO: 3939f7d61273Smeem (void) fprintf(stderr, "%s: no such interface\n", str); 39407c478bd9Sstevel@tonic-gate break; 39417c478bd9Sstevel@tonic-gate case EPERM: 3942f7d61273Smeem (void) fprintf(stderr, "%s: permission denied\n", str); 39437c478bd9Sstevel@tonic-gate break; 3944f7d61273Smeem case EEXIST: 3945f7d61273Smeem (void) fprintf(stderr, "%s: already exists\n", str); 3946f7d61273Smeem break; 3947e899e593SGirish Moodalbail case ENAMETOOLONG: 3948e899e593SGirish Moodalbail (void) fprintf(stderr, "%s: interface name too long\n", str); 3949e899e593SGirish Moodalbail break; 3950e899e593SGirish Moodalbail case ERANGE: 3951e899e593SGirish Moodalbail (void) fprintf(stderr, "%s: logical interface id is outside " 3952e899e593SGirish Moodalbail "allowed range\n", str); 3953e899e593SGirish Moodalbail break; 3954f7d61273Smeem default: 3955f7d61273Smeem errno = error; 3956f7d61273Smeem perror(str); 39577c478bd9Sstevel@tonic-gate } 39587c478bd9Sstevel@tonic-gate } 39597c478bd9Sstevel@tonic-gate 39607c478bd9Sstevel@tonic-gate /* 39617c478bd9Sstevel@tonic-gate * Print out error message (Perror2()) and exit 39627c478bd9Sstevel@tonic-gate */ 39637c478bd9Sstevel@tonic-gate void 3964f7d61273Smeem Perror2_exit(const char *cmd, const char *str) 39657c478bd9Sstevel@tonic-gate { 39667c478bd9Sstevel@tonic-gate Perror2(cmd, str); 39677c478bd9Sstevel@tonic-gate exit(1); 39687c478bd9Sstevel@tonic-gate /* NOTREACHED */ 39697c478bd9Sstevel@tonic-gate } 39707c478bd9Sstevel@tonic-gate 3971c7e4935fSss void 3972c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err) 3973c7e4935fSss { 3974c7e4935fSss (void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd, 3975c7e4935fSss linkname, dlpi_strerror(err)); 3976c7e4935fSss } 3977c7e4935fSss 3978c7e4935fSss /* 3979c7e4935fSss * Print out error message (Perrdlpi()) and exit 3980c7e4935fSss */ 3981c7e4935fSss void 3982c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err) 3983c7e4935fSss { 3984c7e4935fSss Perrdlpi(cmd, linkname, err); 3985c7e4935fSss exit(1); 3986c7e4935fSss } 3987c7e4935fSss 39887c478bd9Sstevel@tonic-gate /* 39897c478bd9Sstevel@tonic-gate * If the last argument is non-NULL allow a <addr>/<n> syntax and 39907c478bd9Sstevel@tonic-gate * pass out <n> in *plenp. 39917c478bd9Sstevel@tonic-gate * If <n> doesn't parse return BAD_ADDR as *plenp. 39927c478bd9Sstevel@tonic-gate * If no /<n> is present return NO_PREFIX as *plenp. 39937c478bd9Sstevel@tonic-gate */ 39947c478bd9Sstevel@tonic-gate static void 39957c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp) 39967c478bd9Sstevel@tonic-gate { 399769bb4bb4Scarlsonj /* LINTED: alignment */ 39987c478bd9Sstevel@tonic-gate struct sockaddr_in *sin = (struct sockaddr_in *)saddr; 39997c478bd9Sstevel@tonic-gate struct hostent *hp; 40007c478bd9Sstevel@tonic-gate struct netent *np; 40017c478bd9Sstevel@tonic-gate char str[BUFSIZ]; 40027c478bd9Sstevel@tonic-gate int error_num; 40037c478bd9Sstevel@tonic-gate 40047c478bd9Sstevel@tonic-gate (void) strncpy(str, s, sizeof (str)); 40057c478bd9Sstevel@tonic-gate 40067c478bd9Sstevel@tonic-gate /* 40077c478bd9Sstevel@tonic-gate * Look for '/'<n> is plenp 40087c478bd9Sstevel@tonic-gate */ 40097c478bd9Sstevel@tonic-gate if (plenp != NULL) { 40107c478bd9Sstevel@tonic-gate char *cp; 40117c478bd9Sstevel@tonic-gate 40127906a3e0Smeem *plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS); 40137c478bd9Sstevel@tonic-gate if (*plenp == BAD_ADDR) 40147c478bd9Sstevel@tonic-gate return; 40157c478bd9Sstevel@tonic-gate cp = strchr(str, '/'); 40167c478bd9Sstevel@tonic-gate if (cp != NULL) 40177c478bd9Sstevel@tonic-gate *cp = '\0'; 40187c478bd9Sstevel@tonic-gate } else if (strchr(str, '/') != NULL) { 40197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 40207c478bd9Sstevel@tonic-gate exit(1); 40217c478bd9Sstevel@tonic-gate } 40227c478bd9Sstevel@tonic-gate 40237c478bd9Sstevel@tonic-gate (void) memset(sin, 0, sizeof (*sin)); 40247c478bd9Sstevel@tonic-gate 40257c478bd9Sstevel@tonic-gate /* 40267c478bd9Sstevel@tonic-gate * Try to catch attempts to set the broadcast address to all 1's. 40277c478bd9Sstevel@tonic-gate */ 40287c478bd9Sstevel@tonic-gate if (strcmp(str, "255.255.255.255") == 0 || 40297c478bd9Sstevel@tonic-gate (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) { 40307c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET; 40317c478bd9Sstevel@tonic-gate sin->sin_addr.s_addr = 0xffffffff; 40327c478bd9Sstevel@tonic-gate return; 40337c478bd9Sstevel@tonic-gate } 40347c478bd9Sstevel@tonic-gate 40357c478bd9Sstevel@tonic-gate hp = getipnodebyname(str, AF_INET, 0, &error_num); 40367c478bd9Sstevel@tonic-gate if (hp) { 40377c478bd9Sstevel@tonic-gate sin->sin_family = hp->h_addrtype; 40387c478bd9Sstevel@tonic-gate (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length); 40397c478bd9Sstevel@tonic-gate freehostent(hp); 40407c478bd9Sstevel@tonic-gate return; 40417c478bd9Sstevel@tonic-gate } 40427c478bd9Sstevel@tonic-gate np = getnetbyname(str); 40437c478bd9Sstevel@tonic-gate if (np) { 40447c478bd9Sstevel@tonic-gate sin->sin_family = np->n_addrtype; 40457c478bd9Sstevel@tonic-gate sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY); 40467c478bd9Sstevel@tonic-gate return; 40477c478bd9Sstevel@tonic-gate } 40487c478bd9Sstevel@tonic-gate if (error_num == TRY_AGAIN) { 40497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address " 40507c478bd9Sstevel@tonic-gate "(try again later)\n", s); 40517c478bd9Sstevel@tonic-gate } else { 40527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 40537c478bd9Sstevel@tonic-gate } 40547c478bd9Sstevel@tonic-gate exit(1); 40557c478bd9Sstevel@tonic-gate } 40567c478bd9Sstevel@tonic-gate 40577c478bd9Sstevel@tonic-gate /* 40587c478bd9Sstevel@tonic-gate * If the last argument is non-NULL allow a <addr>/<n> syntax and 40597c478bd9Sstevel@tonic-gate * pass out <n> in *plenp. 40607c478bd9Sstevel@tonic-gate * If <n> doesn't parse return BAD_ADDR as *plenp. 40617c478bd9Sstevel@tonic-gate * If no /<n> is present return NO_PREFIX as *plenp. 40627c478bd9Sstevel@tonic-gate */ 40637c478bd9Sstevel@tonic-gate static void 40647c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp) 40657c478bd9Sstevel@tonic-gate { 406669bb4bb4Scarlsonj /* LINTED: alignment */ 40677c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr; 40687c478bd9Sstevel@tonic-gate struct hostent *hp; 40697c478bd9Sstevel@tonic-gate char str[BUFSIZ]; 40707c478bd9Sstevel@tonic-gate int error_num; 40717c478bd9Sstevel@tonic-gate 40727c478bd9Sstevel@tonic-gate (void) strncpy(str, s, sizeof (str)); 40737c478bd9Sstevel@tonic-gate 40747c478bd9Sstevel@tonic-gate /* 40757c478bd9Sstevel@tonic-gate * Look for '/'<n> is plenp 40767c478bd9Sstevel@tonic-gate */ 40777c478bd9Sstevel@tonic-gate if (plenp != NULL) { 40787c478bd9Sstevel@tonic-gate char *cp; 40797c478bd9Sstevel@tonic-gate 40807906a3e0Smeem *plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS); 40817c478bd9Sstevel@tonic-gate if (*plenp == BAD_ADDR) 40827c478bd9Sstevel@tonic-gate return; 40837c478bd9Sstevel@tonic-gate cp = strchr(str, '/'); 40847c478bd9Sstevel@tonic-gate if (cp != NULL) 40857c478bd9Sstevel@tonic-gate *cp = '\0'; 40867c478bd9Sstevel@tonic-gate } else if (strchr(str, '/') != NULL) { 40877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str); 40887c478bd9Sstevel@tonic-gate exit(1); 40897c478bd9Sstevel@tonic-gate } 40907c478bd9Sstevel@tonic-gate 40917c478bd9Sstevel@tonic-gate (void) memset(sin6, 0, sizeof (*sin6)); 40927c478bd9Sstevel@tonic-gate 40937c478bd9Sstevel@tonic-gate hp = getipnodebyname(str, AF_INET6, 0, &error_num); 40947c478bd9Sstevel@tonic-gate if (hp) { 40957c478bd9Sstevel@tonic-gate sin6->sin6_family = hp->h_addrtype; 40967c478bd9Sstevel@tonic-gate (void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length); 40977c478bd9Sstevel@tonic-gate freehostent(hp); 40987c478bd9Sstevel@tonic-gate return; 40997c478bd9Sstevel@tonic-gate } 41007c478bd9Sstevel@tonic-gate if (error_num == TRY_AGAIN) { 41017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address " 41027c478bd9Sstevel@tonic-gate "(try again later)\n", s); 41037c478bd9Sstevel@tonic-gate } else { 41047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ifconfig: %s: bad address\n", s); 41057c478bd9Sstevel@tonic-gate } 41067c478bd9Sstevel@tonic-gate exit(1); 41077c478bd9Sstevel@tonic-gate } 41087c478bd9Sstevel@tonic-gate 41097c478bd9Sstevel@tonic-gate /* 41107c478bd9Sstevel@tonic-gate * If "slash" is zero this parses the whole string as 41117c478bd9Sstevel@tonic-gate * an integer. With "slash" non zero it parses the tail part as an integer. 41127c478bd9Sstevel@tonic-gate * 41137c478bd9Sstevel@tonic-gate * If it is not a valid integer this returns BAD_ADDR. 41147c478bd9Sstevel@tonic-gate * If there is /<n> present this returns NO_PREFIX. 41157c478bd9Sstevel@tonic-gate */ 41167c478bd9Sstevel@tonic-gate static int 41177c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen) 41187c478bd9Sstevel@tonic-gate { 41197c478bd9Sstevel@tonic-gate int prefixlen; 41207c478bd9Sstevel@tonic-gate char *str, *end; 41217c478bd9Sstevel@tonic-gate 41227c478bd9Sstevel@tonic-gate if (slash) { 41237c478bd9Sstevel@tonic-gate str = strchr(addr, '/'); 41247c478bd9Sstevel@tonic-gate if (str == NULL) 41257c478bd9Sstevel@tonic-gate return (NO_PREFIX); 41267c478bd9Sstevel@tonic-gate str++; 41277c478bd9Sstevel@tonic-gate } else 41287c478bd9Sstevel@tonic-gate str = addr; 41297c478bd9Sstevel@tonic-gate 41307c478bd9Sstevel@tonic-gate prefixlen = strtol(str, &end, 10); 41317c478bd9Sstevel@tonic-gate if (prefixlen < 0) 41327c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41337c478bd9Sstevel@tonic-gate if (str == end) 41347c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41357c478bd9Sstevel@tonic-gate if (max_plen != 0 && max_plen < prefixlen) 41367c478bd9Sstevel@tonic-gate return (BAD_ADDR); 41377c478bd9Sstevel@tonic-gate return (prefixlen); 41387c478bd9Sstevel@tonic-gate } 41397c478bd9Sstevel@tonic-gate 41407c478bd9Sstevel@tonic-gate /* 41417c478bd9Sstevel@tonic-gate * Convert a prefix length to a mask. 41427c478bd9Sstevel@tonic-gate * Returns 1 if ok. 0 otherwise. 41437c478bd9Sstevel@tonic-gate * Assumes the mask array is zero'ed by the caller. 41447c478bd9Sstevel@tonic-gate */ 41457c478bd9Sstevel@tonic-gate static boolean_t 41467c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask) 41477c478bd9Sstevel@tonic-gate { 41487c478bd9Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxlen) 41497c478bd9Sstevel@tonic-gate return (0); 41507c478bd9Sstevel@tonic-gate 41517c478bd9Sstevel@tonic-gate while (prefixlen > 0) { 41527c478bd9Sstevel@tonic-gate if (prefixlen >= 8) { 41537c478bd9Sstevel@tonic-gate *mask++ = 0xFF; 41547c478bd9Sstevel@tonic-gate prefixlen -= 8; 41557c478bd9Sstevel@tonic-gate continue; 41567c478bd9Sstevel@tonic-gate } 41577c478bd9Sstevel@tonic-gate *mask |= 1 << (8 - prefixlen); 41587c478bd9Sstevel@tonic-gate prefixlen--; 41597c478bd9Sstevel@tonic-gate } 41607c478bd9Sstevel@tonic-gate return (1); 41617c478bd9Sstevel@tonic-gate } 41627c478bd9Sstevel@tonic-gate 41637c478bd9Sstevel@tonic-gate static void 41647c478bd9Sstevel@tonic-gate print_flags(uint64_t flags) 41657c478bd9Sstevel@tonic-gate { 41667c478bd9Sstevel@tonic-gate boolean_t first = _B_TRUE; 41677c478bd9Sstevel@tonic-gate int cnt, i; 41687c478bd9Sstevel@tonic-gate 41697c478bd9Sstevel@tonic-gate (void) printf("flags=%llx", flags); 41707c478bd9Sstevel@tonic-gate cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t); 41717c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i++) { 41727c478bd9Sstevel@tonic-gate if (flags & if_flags_tbl[i].iff_value) { 41737c478bd9Sstevel@tonic-gate if (first) { 41747c478bd9Sstevel@tonic-gate (void) printf("<"); 41757c478bd9Sstevel@tonic-gate first = _B_FALSE; 41767c478bd9Sstevel@tonic-gate } else { 41777c478bd9Sstevel@tonic-gate /* 41787c478bd9Sstevel@tonic-gate * It has to be here and not with the 41797c478bd9Sstevel@tonic-gate * printf below because for the last one, 41807c478bd9Sstevel@tonic-gate * we don't want a comma before the ">". 41817c478bd9Sstevel@tonic-gate */ 41827c478bd9Sstevel@tonic-gate (void) printf(","); 41837c478bd9Sstevel@tonic-gate } 41847c478bd9Sstevel@tonic-gate (void) printf("%s", if_flags_tbl[i].iff_name); 41857c478bd9Sstevel@tonic-gate } 41867c478bd9Sstevel@tonic-gate } 41877c478bd9Sstevel@tonic-gate if (!first) 41887c478bd9Sstevel@tonic-gate (void) printf(">"); 41897c478bd9Sstevel@tonic-gate } 41907c478bd9Sstevel@tonic-gate 41917c478bd9Sstevel@tonic-gate static void 4192e11c3f44Smeem print_config_flags(int af, uint64_t flags) 41937c478bd9Sstevel@tonic-gate { 4194e11c3f44Smeem if_config_cmd_t *cmdp; 41957c478bd9Sstevel@tonic-gate 4196e11c3f44Smeem for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) { 4197e11c3f44Smeem if ((flags & cmdp->iff_flag) && 4198e11c3f44Smeem (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) { 4199e11c3f44Smeem (void) printf("%s ", cmdp->iff_name); 42007c478bd9Sstevel@tonic-gate } 42017c478bd9Sstevel@tonic-gate } 42027c478bd9Sstevel@tonic-gate } 42037c478bd9Sstevel@tonic-gate 42047c478bd9Sstevel@tonic-gate /* 420536e852a1SRaja Andra * Use the configured directory lookup mechanism (e.g. files/NIS/...) 4206dd7a6f5fSkcpoon * to find the network mask. Returns true if we found one to set. 4207dd7a6f5fSkcpoon * 4208dd7a6f5fSkcpoon * The parameter addr_set controls whether we should get the address of 4209dd7a6f5fSkcpoon * the working interface for the netmask query. If addr_set is true, 4210dd7a6f5fSkcpoon * we will use the address provided. Otherwise, we will find the working 4211dd7a6f5fSkcpoon * interface's address and use it instead. 42127c478bd9Sstevel@tonic-gate */ 42137c478bd9Sstevel@tonic-gate static boolean_t 4214dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set) 42157c478bd9Sstevel@tonic-gate { 42167c478bd9Sstevel@tonic-gate struct sockaddr_in ifaddr; 42177c478bd9Sstevel@tonic-gate 42187c478bd9Sstevel@tonic-gate /* 4219dd7a6f5fSkcpoon * Read the address from the interface if it is not passed in. 42207c478bd9Sstevel@tonic-gate */ 4221dd7a6f5fSkcpoon if (!addr_set) { 4222dd7a6f5fSkcpoon (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name)); 4223dd7a6f5fSkcpoon if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 4224dd7a6f5fSkcpoon if (errno != EADDRNOTAVAIL) { 4225dd7a6f5fSkcpoon (void) fprintf(stderr, "Need net number for " 4226dd7a6f5fSkcpoon "mask\n"); 4227dd7a6f5fSkcpoon } 4228dd7a6f5fSkcpoon return (_B_FALSE); 4229dd7a6f5fSkcpoon } 4230dd7a6f5fSkcpoon ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr); 4231dd7a6f5fSkcpoon } else { 4232dd7a6f5fSkcpoon ifaddr.sin_addr = saddr->sin_addr; 42337c478bd9Sstevel@tonic-gate } 42347c478bd9Sstevel@tonic-gate if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) { 42357c478bd9Sstevel@tonic-gate saddr->sin_family = AF_INET; 42367c478bd9Sstevel@tonic-gate return (_B_TRUE); 42377c478bd9Sstevel@tonic-gate } 42387c478bd9Sstevel@tonic-gate return (_B_FALSE); 42397c478bd9Sstevel@tonic-gate } 42407c478bd9Sstevel@tonic-gate 42417c478bd9Sstevel@tonic-gate static int 4242e11c3f44Smeem lifnum(const char *ifname) 4243e11c3f44Smeem { 4244e11c3f44Smeem const char *cp; 4245e11c3f44Smeem 4246e11c3f44Smeem if ((cp = strchr(ifname, ':')) == NULL) 4247e11c3f44Smeem return (0); 4248e11c3f44Smeem else 4249e11c3f44Smeem return (atoi(cp + 1)); 4250e11c3f44Smeem } 4251e11c3f44Smeem 42527c478bd9Sstevel@tonic-gate static void 4253d62bc4baSyz add_ni(const char *name) 42547c478bd9Sstevel@tonic-gate { 42557c478bd9Sstevel@tonic-gate ni_t **pp; 42567c478bd9Sstevel@tonic-gate ni_t *p; 42577c478bd9Sstevel@tonic-gate 42587c478bd9Sstevel@tonic-gate for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) { 42597c478bd9Sstevel@tonic-gate if (strcmp(p->ni_name, name) == 0) { 42607c478bd9Sstevel@tonic-gate if (debug > 2) 42617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "'%s' is a duplicate\n", 42627c478bd9Sstevel@tonic-gate name); 42637c478bd9Sstevel@tonic-gate return; 42647c478bd9Sstevel@tonic-gate } 42657c478bd9Sstevel@tonic-gate } 42667c478bd9Sstevel@tonic-gate 42677c478bd9Sstevel@tonic-gate if (debug > 2) 42687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "adding '%s'\n", 42697c478bd9Sstevel@tonic-gate name); 42707c478bd9Sstevel@tonic-gate 42717c478bd9Sstevel@tonic-gate if ((p = malloc(sizeof (ni_t))) == NULL) 42727c478bd9Sstevel@tonic-gate return; 42737c478bd9Sstevel@tonic-gate 42747c478bd9Sstevel@tonic-gate (void) strlcpy(p->ni_name, name, sizeof (p->ni_name)); 42757c478bd9Sstevel@tonic-gate p->ni_next = NULL; 42767c478bd9Sstevel@tonic-gate 42777c478bd9Sstevel@tonic-gate *pp = p; 42787c478bd9Sstevel@tonic-gate num_ni++; 42797c478bd9Sstevel@tonic-gate } 42807c478bd9Sstevel@tonic-gate 4281d62bc4baSyz static boolean_t 4282d62bc4baSyz ni_entry(const char *linkname, void *arg) 42837c478bd9Sstevel@tonic-gate { 4284c7e4935fSss dlpi_handle_t dh; 4285da14cebeSEric Cheng datalink_class_t class; 42867c478bd9Sstevel@tonic-gate 4287d4d1f7bfSVasumathi Sundaram - Sun Microsystems (void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL); 42884ac67f02SAnurag S. Maskey 4289da14cebeSEric Cheng if (class == DATALINK_CLASS_ETHERSTUB) 4290da14cebeSEric Cheng return (_B_FALSE); 4291c7e4935fSss if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS) 4292d62bc4baSyz return (_B_FALSE); 42937c478bd9Sstevel@tonic-gate 4294d62bc4baSyz add_ni(linkname); 42957c478bd9Sstevel@tonic-gate 4296c7e4935fSss dlpi_close(dh); 4297d62bc4baSyz return (_B_FALSE); 42987c478bd9Sstevel@tonic-gate } 42997c478bd9Sstevel@tonic-gate 43007c478bd9Sstevel@tonic-gate /* 43017c478bd9Sstevel@tonic-gate * dhcp-related routines 43027c478bd9Sstevel@tonic-gate */ 43037c478bd9Sstevel@tonic-gate 43047c478bd9Sstevel@tonic-gate static int 43057c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[]) 43067c478bd9Sstevel@tonic-gate { 43077c478bd9Sstevel@tonic-gate dhcp_ipc_request_t *request; 43087c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *reply = NULL; 43097c478bd9Sstevel@tonic-gate int timeout = DHCP_IPC_WAIT_DEFAULT; 43107c478bd9Sstevel@tonic-gate dhcp_ipc_type_t type = DHCP_START; 43117c478bd9Sstevel@tonic-gate int error; 43127c478bd9Sstevel@tonic-gate boolean_t is_primary = _B_FALSE; 43137c478bd9Sstevel@tonic-gate boolean_t started = _B_FALSE; 43147c478bd9Sstevel@tonic-gate 43157c478bd9Sstevel@tonic-gate for (argv++; --argc > 0; argv++) { 43167c478bd9Sstevel@tonic-gate 43177c478bd9Sstevel@tonic-gate if (strcmp(*argv, "primary") == 0) { 43187c478bd9Sstevel@tonic-gate is_primary = _B_TRUE; 43197c478bd9Sstevel@tonic-gate continue; 43207c478bd9Sstevel@tonic-gate } 43217c478bd9Sstevel@tonic-gate 43227c478bd9Sstevel@tonic-gate if (strcmp(*argv, "wait") == 0) { 43237c478bd9Sstevel@tonic-gate if (--argc <= 0) { 43247c478bd9Sstevel@tonic-gate usage(); 43257c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43267c478bd9Sstevel@tonic-gate } 43277c478bd9Sstevel@tonic-gate argv++; 43287c478bd9Sstevel@tonic-gate 43297c478bd9Sstevel@tonic-gate if (strcmp(*argv, "forever") == 0) { 43307c478bd9Sstevel@tonic-gate timeout = DHCP_IPC_WAIT_FOREVER; 43317c478bd9Sstevel@tonic-gate continue; 43327c478bd9Sstevel@tonic-gate } 43337c478bd9Sstevel@tonic-gate 43347c478bd9Sstevel@tonic-gate if (sscanf(*argv, "%d", &timeout) != 1) { 43357c478bd9Sstevel@tonic-gate usage(); 43367c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43377c478bd9Sstevel@tonic-gate } 43387c478bd9Sstevel@tonic-gate 43397c478bd9Sstevel@tonic-gate if (timeout < 0) { 43407c478bd9Sstevel@tonic-gate usage(); 43417c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43427c478bd9Sstevel@tonic-gate } 43437c478bd9Sstevel@tonic-gate continue; 43447c478bd9Sstevel@tonic-gate } 43457c478bd9Sstevel@tonic-gate 43467c478bd9Sstevel@tonic-gate type = dhcp_string_to_request(*argv); 43477c478bd9Sstevel@tonic-gate if (type == -1) { 43487c478bd9Sstevel@tonic-gate usage(); 43497c478bd9Sstevel@tonic-gate return (DHCP_EXIT_BADARGS); 43507c478bd9Sstevel@tonic-gate } 43517c478bd9Sstevel@tonic-gate } 43527c478bd9Sstevel@tonic-gate 43537c478bd9Sstevel@tonic-gate /* 43547c478bd9Sstevel@tonic-gate * Only try to start agent on start or inform; in all other cases it 43557c478bd9Sstevel@tonic-gate * has to already be running for anything to make sense. 43567c478bd9Sstevel@tonic-gate */ 43577c478bd9Sstevel@tonic-gate if (type == DHCP_START || type == DHCP_INFORM) { 43587c478bd9Sstevel@tonic-gate if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) { 43597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unable to start %s\n", 43607c478bd9Sstevel@tonic-gate caller, DHCP_AGENT_PATH); 43617c478bd9Sstevel@tonic-gate return (DHCP_EXIT_FAILURE); 43627c478bd9Sstevel@tonic-gate } 43637c478bd9Sstevel@tonic-gate started = _B_TRUE; 43647c478bd9Sstevel@tonic-gate } 43657c478bd9Sstevel@tonic-gate 43667c478bd9Sstevel@tonic-gate if (is_primary) 4367d04ccbb3Scarlsonj type |= DHCP_PRIMARY; 4368d04ccbb3Scarlsonj 4369d04ccbb3Scarlsonj if (af != AF_INET) 4370d04ccbb3Scarlsonj type |= DHCP_V6; 43717c478bd9Sstevel@tonic-gate 43727c478bd9Sstevel@tonic-gate request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE); 43737c478bd9Sstevel@tonic-gate if (request == NULL) { 43747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: out of memory\n", caller); 43757c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SYSTEM); 43767c478bd9Sstevel@tonic-gate } 43777c478bd9Sstevel@tonic-gate 43787c478bd9Sstevel@tonic-gate error = dhcp_ipc_make_request(request, &reply, timeout); 43797c478bd9Sstevel@tonic-gate if (error != 0) { 43807c478bd9Sstevel@tonic-gate free(request); 43817c478bd9Sstevel@tonic-gate /* 43827c478bd9Sstevel@tonic-gate * Re-map connect error to not under control if we didn't try a 43837c478bd9Sstevel@tonic-gate * start operation, as this has to be true and results in a 43847c478bd9Sstevel@tonic-gate * clearer message, not to mention preserving compatibility 43857c478bd9Sstevel@tonic-gate * with the days when we always started dhcpagent for every 43867c478bd9Sstevel@tonic-gate * request. 43877c478bd9Sstevel@tonic-gate */ 43887c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_CONNECT && !started) 43897c478bd9Sstevel@tonic-gate error = DHCP_IPC_E_UNKIF; 43907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 43917c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(error)); 43927c478bd9Sstevel@tonic-gate return (DHCP_EXIT_FAILURE); 43937c478bd9Sstevel@tonic-gate } 43947c478bd9Sstevel@tonic-gate 43957c478bd9Sstevel@tonic-gate error = reply->return_code; 43967c478bd9Sstevel@tonic-gate if (error != 0) { 43977c478bd9Sstevel@tonic-gate free(request); 43987c478bd9Sstevel@tonic-gate free(reply); 43997c478bd9Sstevel@tonic-gate 44007c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_TIMEOUT && timeout == 0) 44017c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS); 44027c478bd9Sstevel@tonic-gate 44037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname, 44047c478bd9Sstevel@tonic-gate dhcp_ipc_strerror(error)); 44057c478bd9Sstevel@tonic-gate 44067c478bd9Sstevel@tonic-gate if (error == DHCP_IPC_E_TIMEOUT) 44077c478bd9Sstevel@tonic-gate return (DHCP_EXIT_TIMEOUT); 44087c478bd9Sstevel@tonic-gate else 44097c478bd9Sstevel@tonic-gate return (DHCP_EXIT_IF_FAILURE); 44107c478bd9Sstevel@tonic-gate } 44117c478bd9Sstevel@tonic-gate 44127c478bd9Sstevel@tonic-gate if (DHCP_IPC_CMD(type) == DHCP_STATUS) { 44137c478bd9Sstevel@tonic-gate (void) printf("%s", dhcp_status_hdr_string()); 44147c478bd9Sstevel@tonic-gate (void) printf("%s", dhcp_status_reply_to_string(reply)); 44157c478bd9Sstevel@tonic-gate } 44167c478bd9Sstevel@tonic-gate 44177c478bd9Sstevel@tonic-gate free(request); 44187c478bd9Sstevel@tonic-gate free(reply); 44197c478bd9Sstevel@tonic-gate return (DHCP_EXIT_SUCCESS); 44207c478bd9Sstevel@tonic-gate } 44217c478bd9Sstevel@tonic-gate 44227c478bd9Sstevel@tonic-gate static void 44237c478bd9Sstevel@tonic-gate usage(void) 44247c478bd9Sstevel@tonic-gate { 44257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 44267c478bd9Sstevel@tonic-gate "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n"); 44277c478bd9Sstevel@tonic-gate 44287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", 44297c478bd9Sstevel@tonic-gate "\t[ <addr_family> ]\n" 44307c478bd9Sstevel@tonic-gate "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n" 44317c478bd9Sstevel@tonic-gate "\t[ set [ <address>][/<prefix_length>] ]" 44327c478bd9Sstevel@tonic-gate " [ <address>/<prefix_length>] ]\n" 44337c478bd9Sstevel@tonic-gate "\t[ destination <dest_address> ]\n" 44347c478bd9Sstevel@tonic-gate "\t[ addif <address>[/<prefix_length>]" 44357c478bd9Sstevel@tonic-gate " [ <dest_address> ] ]\n" 44367c478bd9Sstevel@tonic-gate "\t[ removeif <address>[/<prefix_length>] ]\n" 44377c478bd9Sstevel@tonic-gate "\t[ arp | -arp ]\n" 44387c478bd9Sstevel@tonic-gate "\t[ auto-revarp ]\n" 44397c478bd9Sstevel@tonic-gate "\t[ broadcast <broad_addr> ]\n" 44407c478bd9Sstevel@tonic-gate "\t[ index <if_index> ]\n" 44417c478bd9Sstevel@tonic-gate "\t[ metric <n> ] [ mtu <n> ]\n" 44427c478bd9Sstevel@tonic-gate "\t[ netmask <mask> ]\n" 44437c478bd9Sstevel@tonic-gate "\t[ plumb ] [ unplumb ]\n" 44447c478bd9Sstevel@tonic-gate "\t[ preferred | -preferred ]\n" 44457c478bd9Sstevel@tonic-gate "\t[ private | -private ]\n" 44467c478bd9Sstevel@tonic-gate "\t[ local | -local ]\n" 44477c478bd9Sstevel@tonic-gate "\t[ router | -router ]\n" 44487c478bd9Sstevel@tonic-gate "\t[ subnet <subnet_address>]\n" 44497c478bd9Sstevel@tonic-gate "\t[ trailers | -trailers ]\n" 44507c478bd9Sstevel@tonic-gate "\t[ token <address>/<prefix_length> ]\n" 44517c478bd9Sstevel@tonic-gate "\t[ tsrc <tunnel_src_address> ]\n" 44527c478bd9Sstevel@tonic-gate "\t[ tdst <tunnel_dest_address> ]\n" 44537c478bd9Sstevel@tonic-gate "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n" 44547c478bd9Sstevel@tonic-gate "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n" 44557c478bd9Sstevel@tonic-gate "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n" 44567c478bd9Sstevel@tonic-gate "\t[ up ] [ down ]\n" 44577c478bd9Sstevel@tonic-gate "\t[ xmit | -xmit ]\n" 44587c478bd9Sstevel@tonic-gate "\t[ modlist ]\n" 44597c478bd9Sstevel@tonic-gate "\t[ modinsert <module_name@position> ]\n" 44607c478bd9Sstevel@tonic-gate "\t[ modremove <module_name@position> ]\n" 4461e11c3f44Smeem "\t[ ipmp ]\n" 44627c478bd9Sstevel@tonic-gate "\t[ group <groupname>] | [ group \"\"]\n" 44637c478bd9Sstevel@tonic-gate "\t[ deprecated | -deprecated ]\n" 44647c478bd9Sstevel@tonic-gate "\t[ standby | -standby ]\n" 44657c478bd9Sstevel@tonic-gate "\t[ failover | -failover ]\n" 44667c478bd9Sstevel@tonic-gate "\t[ zone <zonename> | -zone ]\n" 446745916cd2Sjpk "\t[ usesrc <interface> ]\n" 446845916cd2Sjpk "\t[ all-zones ]\n"); 44697c478bd9Sstevel@tonic-gate 44707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "or\n"); 44717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 44727c478bd9Sstevel@tonic-gate "\tifconfig <interface> | -a[ 4 | 6 | D ] [ u | d ]\n"); 44737c478bd9Sstevel@tonic-gate 44747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n" 44757c478bd9Sstevel@tonic-gate "\t[ wait <time> | forever ]\n\t[ primary ]\n" 44767c478bd9Sstevel@tonic-gate "\tstart | drop | ping | release | status | inform\n"); 44777c478bd9Sstevel@tonic-gate } 4478