10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52074Smeem * Common Development and Distribution License (the "License"). 62074Smeem * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*8485SPeter.Memishian@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include "mpd_defs.h" 270Sstevel@tonic-gate #include "mpd_tables.h" 280Sstevel@tonic-gate 290Sstevel@tonic-gate int debug = 0; /* Debug flag */ 300Sstevel@tonic-gate static int pollfd_num = 0; /* Num. of poll descriptors */ 310Sstevel@tonic-gate static struct pollfd *pollfds = NULL; /* Array of poll descriptors */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate /* All times below in ms */ 340Sstevel@tonic-gate int user_failure_detection_time; /* user specified failure detection */ 350Sstevel@tonic-gate /* time (fdt) */ 360Sstevel@tonic-gate int user_probe_interval; /* derived from user specified fdt */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate static int rtsock_v4; /* AF_INET routing socket */ 390Sstevel@tonic-gate static int rtsock_v6; /* AF_INET6 routing socket */ 400Sstevel@tonic-gate int ifsock_v4 = -1; /* IPv4 socket for ioctls */ 410Sstevel@tonic-gate int ifsock_v6 = -1; /* IPv6 socket for ioctls */ 420Sstevel@tonic-gate static int lsock_v4; /* Listen socket to detect mpathd */ 430Sstevel@tonic-gate static int lsock_v6; /* Listen socket to detect mpathd */ 440Sstevel@tonic-gate static int mibfd = -1; /* fd to get mib info */ 450Sstevel@tonic-gate static boolean_t force_mcast = _B_FALSE; /* Only for test purposes */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate static uint_t last_initifs_time; /* Time when initifs was last run */ 480Sstevel@tonic-gate static char **argv0; /* Saved for re-exec on SIGHUP */ 490Sstevel@tonic-gate boolean_t handle_link_notifications = _B_TRUE; 500Sstevel@tonic-gate 510Sstevel@tonic-gate static void initlog(void); 520Sstevel@tonic-gate static void run_timeouts(void); 530Sstevel@tonic-gate static void initifs(void); 540Sstevel@tonic-gate static void check_if_removed(struct phyint_instance *pii); 550Sstevel@tonic-gate static void select_test_ifs(void); 560Sstevel@tonic-gate static void ire_process_v4(mib2_ipRouteEntry_t *buf, size_t len); 570Sstevel@tonic-gate static void ire_process_v6(mib2_ipv6RouteEntry_t *buf, size_t len); 580Sstevel@tonic-gate static void router_add_common(int af, char *ifname, 590Sstevel@tonic-gate struct in6_addr nexthop); 600Sstevel@tonic-gate static void init_router_targets(); 610Sstevel@tonic-gate static void cleanup(void); 620Sstevel@tonic-gate static int setup_listener(int af); 630Sstevel@tonic-gate static void check_config(void); 644770Smeem static void check_testconfig(void); 652496Smeem static void check_addr_unique(struct phyint_instance *, 662496Smeem struct sockaddr_storage *); 670Sstevel@tonic-gate static void init_host_targets(void); 680Sstevel@tonic-gate static void dup_host_targets(struct phyint_instance *desired_pii); 690Sstevel@tonic-gate static void loopback_cmd(int sock, int family); 700Sstevel@tonic-gate static boolean_t daemonize(void); 710Sstevel@tonic-gate static int closefunc(void *, int); 720Sstevel@tonic-gate static unsigned int process_cmd(int newfd, union mi_commands *mpi); 730Sstevel@tonic-gate static unsigned int process_query(int fd, mi_query_t *miq); 74*8485SPeter.Memishian@Sun.COM static unsigned int send_addrinfo(int fd, ipmp_addrinfo_t *adinfop); 750Sstevel@tonic-gate static unsigned int send_groupinfo(int fd, ipmp_groupinfo_t *grinfop); 760Sstevel@tonic-gate static unsigned int send_grouplist(int fd, ipmp_grouplist_t *grlistp); 770Sstevel@tonic-gate static unsigned int send_ifinfo(int fd, ipmp_ifinfo_t *ifinfop); 780Sstevel@tonic-gate static unsigned int send_result(int fd, unsigned int error, int syserror); 790Sstevel@tonic-gate 80*8485SPeter.Memishian@Sun.COM addrlist_t *localaddrs; 812250Srk129064 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * Return the current time in milliseconds (from an arbitrary reference) 840Sstevel@tonic-gate * truncated to fit into an int. Truncation is ok since we are interested 850Sstevel@tonic-gate * only in differences and not the absolute values. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate uint_t 880Sstevel@tonic-gate getcurrenttime(void) 890Sstevel@tonic-gate { 900Sstevel@tonic-gate uint_t cur_time; /* In ms */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* 930Sstevel@tonic-gate * Use of a non-user-adjustable source of time is 940Sstevel@tonic-gate * required. However millisecond precision is sufficient. 950Sstevel@tonic-gate * divide by 10^6 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate cur_time = (uint_t)(gethrtime() / 1000000LL); 980Sstevel@tonic-gate return (cur_time); 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate 1014770Smeem uint64_t 1024770Smeem getcurrentsec(void) 1034770Smeem { 1044770Smeem return (gethrtime() / NANOSEC); 1054770Smeem } 1064770Smeem 1070Sstevel@tonic-gate /* 1080Sstevel@tonic-gate * Add fd to the set being polled. Returns 0 if ok; -1 if failed. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate int 1110Sstevel@tonic-gate poll_add(int fd) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate int i; 1140Sstevel@tonic-gate int new_num; 1150Sstevel@tonic-gate struct pollfd *newfds; 1160Sstevel@tonic-gate retry: 1170Sstevel@tonic-gate /* Check if already present */ 1180Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 1190Sstevel@tonic-gate if (pollfds[i].fd == fd) 1200Sstevel@tonic-gate return (0); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate /* Check for empty spot already present */ 1230Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 1240Sstevel@tonic-gate if (pollfds[i].fd == -1) { 1250Sstevel@tonic-gate pollfds[i].fd = fd; 1260Sstevel@tonic-gate return (0); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* Allocate space for 32 more fds and initialize to -1 */ 1310Sstevel@tonic-gate new_num = pollfd_num + 32; 1320Sstevel@tonic-gate newfds = realloc(pollfds, new_num * sizeof (struct pollfd)); 1330Sstevel@tonic-gate if (newfds == NULL) { 1340Sstevel@tonic-gate logperror("poll_add: realloc"); 1350Sstevel@tonic-gate return (-1); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate for (i = pollfd_num; i < new_num; i++) { 1380Sstevel@tonic-gate newfds[i].fd = -1; 1390Sstevel@tonic-gate newfds[i].events = POLLIN; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate pollfd_num = new_num; 1420Sstevel@tonic-gate pollfds = newfds; 1430Sstevel@tonic-gate goto retry; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Remove fd from the set being polled. Returns 0 if ok; -1 if failed. 1480Sstevel@tonic-gate */ 149*8485SPeter.Memishian@Sun.COM int 1500Sstevel@tonic-gate poll_remove(int fd) 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate int i; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* Check if already present */ 1550Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 1560Sstevel@tonic-gate if (pollfds[i].fd == fd) { 1570Sstevel@tonic-gate pollfds[i].fd = -1; 1580Sstevel@tonic-gate return (0); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate return (-1); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /* 1650Sstevel@tonic-gate * Extract information about the phyint instance. If the phyint instance still 1660Sstevel@tonic-gate * exists in the kernel then set pii_in_use, else clear it. check_if_removed() 1670Sstevel@tonic-gate * will use it to detect phyint instances that don't exist any longer and 1680Sstevel@tonic-gate * remove them, from our database of phyint instances. 1690Sstevel@tonic-gate * Return value: 1700Sstevel@tonic-gate * returns true if the phyint instance exists in the kernel, 1710Sstevel@tonic-gate * returns false otherwise 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate static boolean_t 1740Sstevel@tonic-gate pii_process(int af, char *name, struct phyint_instance **pii_p) 1750Sstevel@tonic-gate { 1760Sstevel@tonic-gate int err; 1770Sstevel@tonic-gate struct phyint_instance *pii; 1780Sstevel@tonic-gate struct phyint_instance *pii_other; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (debug & D_PHYINT) 1810Sstevel@tonic-gate logdebug("pii_process(%s %s)\n", AF_STR(af), name); 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate pii = phyint_inst_lookup(af, name); 1840Sstevel@tonic-gate if (pii == NULL) { 1850Sstevel@tonic-gate /* 1860Sstevel@tonic-gate * Phyint instance does not exist in our tables, 1870Sstevel@tonic-gate * create new phyint instance 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate pii = phyint_inst_init_from_k(af, name); 1900Sstevel@tonic-gate } else { 1910Sstevel@tonic-gate /* Phyint exists in our tables */ 1920Sstevel@tonic-gate err = phyint_inst_update_from_k(pii); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate switch (err) { 1950Sstevel@tonic-gate case PI_IOCTL_ERROR: 1960Sstevel@tonic-gate /* Some ioctl error. don't change anything */ 1970Sstevel@tonic-gate pii->pii_in_use = 1; 1980Sstevel@tonic-gate break; 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate case PI_GROUP_CHANGED: 2010Sstevel@tonic-gate case PI_IFINDEX_CHANGED: 2020Sstevel@tonic-gate /* 203*8485SPeter.Memishian@Sun.COM * Interface index or group membership has changed. 204*8485SPeter.Memishian@Sun.COM * Delete the old state and recreate based on the new 205*8485SPeter.Memishian@Sun.COM * state (it may no longer be in a group). 2060Sstevel@tonic-gate */ 2070Sstevel@tonic-gate pii_other = phyint_inst_other(pii); 2080Sstevel@tonic-gate if (pii_other != NULL) 2090Sstevel@tonic-gate phyint_inst_delete(pii_other); 2100Sstevel@tonic-gate phyint_inst_delete(pii); 2110Sstevel@tonic-gate pii = phyint_inst_init_from_k(af, name); 2120Sstevel@tonic-gate break; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate case PI_DELETED: 2150Sstevel@tonic-gate /* Phyint instance has disappeared from kernel */ 2160Sstevel@tonic-gate pii->pii_in_use = 0; 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate case PI_OK: 2200Sstevel@tonic-gate /* Phyint instance exists and is fine */ 2210Sstevel@tonic-gate pii->pii_in_use = 1; 2220Sstevel@tonic-gate break; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate default: 2250Sstevel@tonic-gate /* Unknown status */ 2260Sstevel@tonic-gate logerr("pii_process: Unknown status %d\n", err); 2270Sstevel@tonic-gate break; 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate *pii_p = pii; 2320Sstevel@tonic-gate if (pii != NULL) 2330Sstevel@tonic-gate return (pii->pii_in_use ? _B_TRUE : _B_FALSE); 2340Sstevel@tonic-gate else 2350Sstevel@tonic-gate return (_B_FALSE); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Scan all interfaces to detect changes as well as new and deleted interfaces 2400Sstevel@tonic-gate */ 2410Sstevel@tonic-gate static void 2420Sstevel@tonic-gate initifs() 2430Sstevel@tonic-gate { 244*8485SPeter.Memishian@Sun.COM int i, nlifr; 2450Sstevel@tonic-gate int af; 2460Sstevel@tonic-gate char *cp; 2470Sstevel@tonic-gate char *buf; 248*8485SPeter.Memishian@Sun.COM int sockfd; 249*8485SPeter.Memishian@Sun.COM uint64_t flags; 2500Sstevel@tonic-gate struct lifnum lifn; 2510Sstevel@tonic-gate struct lifconf lifc; 252*8485SPeter.Memishian@Sun.COM struct lifreq lifreq; 2530Sstevel@tonic-gate struct lifreq *lifr; 2540Sstevel@tonic-gate struct logint *li; 2550Sstevel@tonic-gate struct phyint_instance *pii; 2560Sstevel@tonic-gate struct phyint_instance *next_pii; 257*8485SPeter.Memishian@Sun.COM struct phyint_group *pg, *next_pg; 258*8485SPeter.Memishian@Sun.COM char pi_name[LIFNAMSIZ + 1]; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate if (debug & D_PHYINT) 2610Sstevel@tonic-gate logdebug("initifs: Scanning interfaces\n"); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate last_initifs_time = getcurrenttime(); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate /* 266*8485SPeter.Memishian@Sun.COM * Free the existing local address list; we'll build a new list below. 2672250Srk129064 */ 268*8485SPeter.Memishian@Sun.COM addrlist_free(&localaddrs); 2692250Srk129064 2702250Srk129064 /* 2710Sstevel@tonic-gate * Mark the interfaces so that we can find phyints and logints 2720Sstevel@tonic-gate * which have disappeared from the kernel. pii_process() and 2730Sstevel@tonic-gate * logint_init_from_k() will set {pii,li}_in_use when they find 2740Sstevel@tonic-gate * the interface in the kernel. Also, clear dupaddr bit on probe 2750Sstevel@tonic-gate * logint. check_addr_unique() will set the dupaddr bit on the 2760Sstevel@tonic-gate * probe logint, if the testaddress is not unique. 2770Sstevel@tonic-gate */ 2780Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) { 2790Sstevel@tonic-gate pii->pii_in_use = 0; 2800Sstevel@tonic-gate for (li = pii->pii_logint; li != NULL; li = li->li_next) { 2810Sstevel@tonic-gate li->li_in_use = 0; 2820Sstevel@tonic-gate if (pii->pii_probe_logint == li) 2830Sstevel@tonic-gate li->li_dupaddr = 0; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 287*8485SPeter.Memishian@Sun.COM /* 288*8485SPeter.Memishian@Sun.COM * As above, mark groups so that we can detect IPMP interfaces which 289*8485SPeter.Memishian@Sun.COM * have been removed from the kernel. Also, delete the group address 290*8485SPeter.Memishian@Sun.COM * list since we'll iteratively recreate it below. 291*8485SPeter.Memishian@Sun.COM */ 292*8485SPeter.Memishian@Sun.COM for (pg = phyint_groups; pg != NULL; pg = pg->pg_next) { 293*8485SPeter.Memishian@Sun.COM pg->pg_in_use = _B_FALSE; 294*8485SPeter.Memishian@Sun.COM addrlist_free(&pg->pg_addrs); 295*8485SPeter.Memishian@Sun.COM } 296*8485SPeter.Memishian@Sun.COM 2970Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 298*8485SPeter.Memishian@Sun.COM lifn.lifn_flags = LIFC_ALLZONES | LIFC_UNDER_IPMP; 299*8485SPeter.Memishian@Sun.COM again: 3000Sstevel@tonic-gate if (ioctl(ifsock_v4, SIOCGLIFNUM, (char *)&lifn) < 0) { 301*8485SPeter.Memishian@Sun.COM logperror("initifs: ioctl (get interface count)"); 3020Sstevel@tonic-gate return; 3030Sstevel@tonic-gate } 304*8485SPeter.Memishian@Sun.COM /* 305*8485SPeter.Memishian@Sun.COM * Pad the interface count to detect when additional interfaces have 306*8485SPeter.Memishian@Sun.COM * been configured between SIOCGLIFNUM and SIOCGLIFCONF. 307*8485SPeter.Memishian@Sun.COM */ 308*8485SPeter.Memishian@Sun.COM lifn.lifn_count += 4; 309*8485SPeter.Memishian@Sun.COM 310*8485SPeter.Memishian@Sun.COM if ((buf = calloc(lifn.lifn_count, sizeof (struct lifreq))) == NULL) { 3110Sstevel@tonic-gate logperror("initifs: calloc"); 3120Sstevel@tonic-gate return; 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 316*8485SPeter.Memishian@Sun.COM lifc.lifc_flags = LIFC_ALLZONES | LIFC_UNDER_IPMP; 317*8485SPeter.Memishian@Sun.COM lifc.lifc_len = lifn.lifn_count * sizeof (struct lifreq); 3180Sstevel@tonic-gate lifc.lifc_buf = buf; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate if (ioctl(ifsock_v4, SIOCGLIFCONF, (char *)&lifc) < 0) { 321*8485SPeter.Memishian@Sun.COM logperror("initifs: ioctl (get interface configuration)"); 3220Sstevel@tonic-gate free(buf); 3230Sstevel@tonic-gate return; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 326*8485SPeter.Memishian@Sun.COM /* 327*8485SPeter.Memishian@Sun.COM * If every lifr_req slot is taken, then additional interfaces must 328*8485SPeter.Memishian@Sun.COM * have been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF. 329*8485SPeter.Memishian@Sun.COM * Recalculate to make sure we didn't miss any interfaces. 330*8485SPeter.Memishian@Sun.COM */ 331*8485SPeter.Memishian@Sun.COM nlifr = lifc.lifc_len / sizeof (struct lifreq); 332*8485SPeter.Memishian@Sun.COM if (nlifr >= lifn.lifn_count) { 333*8485SPeter.Memishian@Sun.COM free(buf); 334*8485SPeter.Memishian@Sun.COM goto again; 335*8485SPeter.Memishian@Sun.COM } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 338*8485SPeter.Memishian@Sun.COM * Walk through the lifreqs returned by SIOGGLIFCONF, and refresh the 339*8485SPeter.Memishian@Sun.COM * global list of addresses, phyint groups, phyints, and logints. 3400Sstevel@tonic-gate */ 341*8485SPeter.Memishian@Sun.COM for (lifr = lifc.lifc_req, i = 0; i < nlifr; i++, lifr++) { 3420Sstevel@tonic-gate af = lifr->lifr_addr.ss_family; 3432250Srk129064 sockfd = (af == AF_INET) ? ifsock_v4 : ifsock_v6; 344*8485SPeter.Memishian@Sun.COM (void) strlcpy(lifreq.lifr_name, lifr->lifr_name, LIFNAMSIZ); 3452250Srk129064 3462250Srk129064 if (ioctl(sockfd, SIOCGLIFFLAGS, &lifreq) == -1) { 3472250Srk129064 if (errno != ENXIO) 3482250Srk129064 logperror("initifs: ioctl (SIOCGLIFFLAGS)"); 3492250Srk129064 continue; 3502250Srk129064 } 351*8485SPeter.Memishian@Sun.COM flags = lifreq.lifr_flags; 3522250Srk129064 3532250Srk129064 /* 354*8485SPeter.Memishian@Sun.COM * If the address is IFF_UP, add it to the local address list. 355*8485SPeter.Memishian@Sun.COM * (We ignore addresses that aren't IFF_UP since another node 356*8485SPeter.Memishian@Sun.COM * might legitimately have that address IFF_UP.) 3572250Srk129064 */ 358*8485SPeter.Memishian@Sun.COM if (flags & IFF_UP) { 359*8485SPeter.Memishian@Sun.COM (void) addrlist_add(&localaddrs, lifr->lifr_name, flags, 360*8485SPeter.Memishian@Sun.COM &lifr->lifr_addr); 3612250Srk129064 } 3622250Srk129064 3632250Srk129064 /* 364*8485SPeter.Memishian@Sun.COM * If this address is on an IPMP meta-interface, update our 365*8485SPeter.Memishian@Sun.COM * phyint_group information (either by recording that group 366*8485SPeter.Memishian@Sun.COM * still exists or creating a new group), and track what 367*8485SPeter.Memishian@Sun.COM * group the address is part of. 368*8485SPeter.Memishian@Sun.COM */ 369*8485SPeter.Memishian@Sun.COM if (flags & IFF_IPMP) { 370*8485SPeter.Memishian@Sun.COM if (ioctl(sockfd, SIOCGLIFGROUPNAME, &lifreq) == -1) { 371*8485SPeter.Memishian@Sun.COM if (errno != ENXIO) 372*8485SPeter.Memishian@Sun.COM logperror("initifs: ioctl " 373*8485SPeter.Memishian@Sun.COM "(SIOCGLIFGROUPNAME)"); 374*8485SPeter.Memishian@Sun.COM continue; 375*8485SPeter.Memishian@Sun.COM } 376*8485SPeter.Memishian@Sun.COM 377*8485SPeter.Memishian@Sun.COM pg = phyint_group_lookup(lifreq.lifr_groupname); 378*8485SPeter.Memishian@Sun.COM if (pg == NULL) { 379*8485SPeter.Memishian@Sun.COM pg = phyint_group_create(lifreq.lifr_groupname); 380*8485SPeter.Memishian@Sun.COM if (pg == NULL) { 381*8485SPeter.Memishian@Sun.COM logerr("initifs: cannot create group " 382*8485SPeter.Memishian@Sun.COM "%s\n", lifreq.lifr_groupname); 383*8485SPeter.Memishian@Sun.COM continue; 384*8485SPeter.Memishian@Sun.COM } 385*8485SPeter.Memishian@Sun.COM phyint_group_insert(pg); 386*8485SPeter.Memishian@Sun.COM } 387*8485SPeter.Memishian@Sun.COM pg->pg_in_use = _B_TRUE; 388*8485SPeter.Memishian@Sun.COM 389*8485SPeter.Memishian@Sun.COM /* 390*8485SPeter.Memishian@Sun.COM * Add this to the group's list of data addresses. 391*8485SPeter.Memishian@Sun.COM */ 392*8485SPeter.Memishian@Sun.COM if (!addrlist_add(&pg->pg_addrs, lifr->lifr_name, flags, 393*8485SPeter.Memishian@Sun.COM &lifr->lifr_addr)) { 394*8485SPeter.Memishian@Sun.COM logerr("initifs: insufficient memory to track " 395*8485SPeter.Memishian@Sun.COM "data address information for %s\n", 396*8485SPeter.Memishian@Sun.COM lifr->lifr_name); 397*8485SPeter.Memishian@Sun.COM } 398*8485SPeter.Memishian@Sun.COM continue; 399*8485SPeter.Memishian@Sun.COM } 400*8485SPeter.Memishian@Sun.COM 401*8485SPeter.Memishian@Sun.COM /* 402*8485SPeter.Memishian@Sun.COM * This isn't an address on an IPMP meta-interface, so it's 403*8485SPeter.Memishian@Sun.COM * either on an underlying interface or not related to any 404*8485SPeter.Memishian@Sun.COM * group. Update our phyint and logint information (via 405*8485SPeter.Memishian@Sun.COM * pii_process() and logint_init_from_k()) -- but first, 406*8485SPeter.Memishian@Sun.COM * convert the logint name to a phyint name so we can call 407*8485SPeter.Memishian@Sun.COM * pii_process(). 4080Sstevel@tonic-gate */ 4092250Srk129064 (void) strlcpy(pi_name, lifr->lifr_name, sizeof (pi_name)); 4100Sstevel@tonic-gate if ((cp = strchr(pi_name, IF_SEPARATOR)) != NULL) 4110Sstevel@tonic-gate *cp = '\0'; 4120Sstevel@tonic-gate 413*8485SPeter.Memishian@Sun.COM if (pii_process(af, pi_name, &pii)) { 4140Sstevel@tonic-gate /* The phyint is fine. So process the logint */ 4150Sstevel@tonic-gate logint_init_from_k(pii, lifr->lifr_name); 4162496Smeem check_addr_unique(pii, &lifr->lifr_addr); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate free(buf); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate /* 422*8485SPeter.Memishian@Sun.COM * Scan for groups, phyints and logints that have disappeared from the 4230Sstevel@tonic-gate * kernel, and delete them. 4240Sstevel@tonic-gate */ 4254770Smeem for (pii = phyint_instances; pii != NULL; pii = next_pii) { 4260Sstevel@tonic-gate next_pii = pii->pii_next; 4270Sstevel@tonic-gate check_if_removed(pii); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 430*8485SPeter.Memishian@Sun.COM for (pg = phyint_groups; pg != NULL; pg = next_pg) { 431*8485SPeter.Memishian@Sun.COM next_pg = pg->pg_next; 432*8485SPeter.Memishian@Sun.COM if (!pg->pg_in_use) { 433*8485SPeter.Memishian@Sun.COM phyint_group_delete(pg); 434*8485SPeter.Memishian@Sun.COM continue; 435*8485SPeter.Memishian@Sun.COM } 436*8485SPeter.Memishian@Sun.COM /* 437*8485SPeter.Memishian@Sun.COM * Refresh the group's state. This is necessary since the 438*8485SPeter.Memishian@Sun.COM * group's state is defined by the set of usable interfaces in 439*8485SPeter.Memishian@Sun.COM * the group, and an interface is considered unusable if all 440*8485SPeter.Memishian@Sun.COM * of its addresses are down. When an address goes down/up, 441*8485SPeter.Memishian@Sun.COM * the RTM_DELADDR/RTM_NEWADDR brings us through here. 442*8485SPeter.Memishian@Sun.COM */ 443*8485SPeter.Memishian@Sun.COM phyint_group_refresh_state(pg); 444*8485SPeter.Memishian@Sun.COM } 445*8485SPeter.Memishian@Sun.COM 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Select a test address for sending probes on each phyint instance 4480Sstevel@tonic-gate */ 4490Sstevel@tonic-gate select_test_ifs(); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* 452*8485SPeter.Memishian@Sun.COM * Handle link up/down notifications. 4530Sstevel@tonic-gate */ 4540Sstevel@tonic-gate process_link_state_changes(); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* 4582496Smeem * Check that a given test address is unique across all of the interfaces in a 4592496Smeem * group. (e.g., IPv6 link-locals may not be inherently unique, and binding 4602496Smeem * to such an (IFF_NOFAILOVER) address can produce unexpected results.) 4614770Smeem * Any issues will be reported by check_testconfig(). 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate static void 4642496Smeem check_addr_unique(struct phyint_instance *ourpii, struct sockaddr_storage *ss) 4650Sstevel@tonic-gate { 4662496Smeem struct phyint *pi; 4672496Smeem struct phyint_group *pg; 4682496Smeem struct in6_addr addr; 4690Sstevel@tonic-gate struct phyint_instance *pii; 4700Sstevel@tonic-gate struct sockaddr_in *sin; 4712496Smeem 4722496Smeem if (ss->ss_family == AF_INET) { 4732496Smeem sin = (struct sockaddr_in *)ss; 4740Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &addr); 4750Sstevel@tonic-gate } else { 4762496Smeem assert(ss->ss_family == AF_INET6); 4772496Smeem addr = ((struct sockaddr_in6 *)ss)->sin6_addr; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4812496Smeem * For anonymous groups, every interface is assumed to be on its own 4822496Smeem * link, so there is no chance of overlapping addresses. 4830Sstevel@tonic-gate */ 4842496Smeem pg = ourpii->pii_phyint->pi_group; 4852496Smeem if (pg == phyint_anongroup) 4862496Smeem return; 4872496Smeem 4882496Smeem /* 4892496Smeem * Walk the list of phyint instances in the group and check for test 4902496Smeem * addresses matching ours. Of course, we skip ourself. 4912496Smeem */ 4922496Smeem for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) { 4932496Smeem pii = PHYINT_INSTANCE(pi, ss->ss_family); 4942496Smeem if (pii == NULL || pii == ourpii || 4952496Smeem pii->pii_probe_logint == NULL) 4960Sstevel@tonic-gate continue; 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* 4994770Smeem * If this test address is not unique, set the dupaddr bit. 5000Sstevel@tonic-gate */ 5014770Smeem if (IN6_ARE_ADDR_EQUAL(&addr, &pii->pii_probe_logint->li_addr)) 5024770Smeem pii->pii_probe_logint->li_dupaddr = 1; 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Stop probing an interface. Called when an interface is offlined. 5080Sstevel@tonic-gate * The probe socket is closed on each interface instance, and the 5090Sstevel@tonic-gate * interface state set to PI_OFFLINE. 5100Sstevel@tonic-gate */ 511*8485SPeter.Memishian@Sun.COM void 5120Sstevel@tonic-gate stop_probing(struct phyint *pi) 5130Sstevel@tonic-gate { 5140Sstevel@tonic-gate struct phyint_instance *pii; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate pii = pi->pi_v4; 5170Sstevel@tonic-gate if (pii != NULL) { 5180Sstevel@tonic-gate if (pii->pii_probe_sock != -1) 5190Sstevel@tonic-gate close_probe_socket(pii, _B_TRUE); 5200Sstevel@tonic-gate pii->pii_probe_logint = NULL; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate pii = pi->pi_v6; 5240Sstevel@tonic-gate if (pii != NULL) { 5250Sstevel@tonic-gate if (pii->pii_probe_sock != -1) 5260Sstevel@tonic-gate close_probe_socket(pii, _B_TRUE); 5270Sstevel@tonic-gate pii->pii_probe_logint = NULL; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate phyint_chstate(pi, PI_OFFLINE); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5332074Smeem enum { BAD_TESTFLAGS, OK_TESTFLAGS, BEST_TESTFLAGS }; 5342074Smeem 5350Sstevel@tonic-gate /* 5362074Smeem * Rate the provided test flags. By definition, IFF_NOFAILOVER must be set. 5372074Smeem * IFF_UP must also be set so that the associated address can be used as a 5382074Smeem * source address. Further, we must be able to exchange packets with local 5392074Smeem * destinations, so IFF_NOXMIT and IFF_NOLOCAL must be clear. For historical 5402074Smeem * reasons, we have a proclivity for IFF_DEPRECATED IPv4 test addresses. 5412074Smeem */ 5422074Smeem static int 5432074Smeem rate_testflags(uint64_t flags) 5442074Smeem { 5452074Smeem if ((flags & (IFF_NOFAILOVER | IFF_UP)) != (IFF_NOFAILOVER | IFF_UP)) 5462074Smeem return (BAD_TESTFLAGS); 5472074Smeem 5482074Smeem if ((flags & (IFF_NOXMIT | IFF_NOLOCAL)) != 0) 5492074Smeem return (BAD_TESTFLAGS); 5502074Smeem 5512074Smeem if ((flags & (IFF_IPV6 | IFF_DEPRECATED)) == IFF_DEPRECATED) 5522074Smeem return (BEST_TESTFLAGS); 5532074Smeem 5542074Smeem if ((flags & (IFF_IPV6 | IFF_DEPRECATED)) == IFF_IPV6) 5552074Smeem return (BEST_TESTFLAGS); 5562074Smeem 5572074Smeem return (OK_TESTFLAGS); 5582074Smeem } 5592074Smeem 5602074Smeem /* 5612074Smeem * Attempt to select a test address for each phyint instance. 5622074Smeem * Call phyint_inst_sockinit() to complete the initializations. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate static void 5650Sstevel@tonic-gate select_test_ifs(void) 5660Sstevel@tonic-gate { 5670Sstevel@tonic-gate struct phyint *pi; 5680Sstevel@tonic-gate struct phyint_instance *pii; 5690Sstevel@tonic-gate struct phyint_instance *next_pii; 5702074Smeem struct logint *li; 5712074Smeem struct logint *probe_logint; 5722074Smeem boolean_t target_scan_reqd = _B_FALSE; 5732074Smeem int rating; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (debug & D_PHYINT) 5760Sstevel@tonic-gate logdebug("select_test_ifs\n"); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* 5790Sstevel@tonic-gate * For each phyint instance, do the test address selection 5800Sstevel@tonic-gate */ 5810Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = next_pii) { 5820Sstevel@tonic-gate next_pii = pii->pii_next; 5832074Smeem probe_logint = NULL; 5842074Smeem 5850Sstevel@tonic-gate /* 586*8485SPeter.Memishian@Sun.COM * An interface that is offline should not be probed. 587*8485SPeter.Memishian@Sun.COM * IFF_OFFLINE interfaces should always be PI_OFFLINE 5880Sstevel@tonic-gate * unless some other entity has set the offline flag. 5890Sstevel@tonic-gate */ 5900Sstevel@tonic-gate if (pii->pii_phyint->pi_flags & IFF_OFFLINE) { 5910Sstevel@tonic-gate if (pii->pii_phyint->pi_state != PI_OFFLINE) { 5920Sstevel@tonic-gate logerr("shouldn't be probing offline" 5934770Smeem " interface %s (state is: %u)." 5944770Smeem " Stopping probes.\n", 5954770Smeem pii->pii_phyint->pi_name, 5964770Smeem pii->pii_phyint->pi_state); 5970Sstevel@tonic-gate stop_probing(pii->pii_phyint); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate continue; 600*8485SPeter.Memishian@Sun.COM } else { 601*8485SPeter.Memishian@Sun.COM /* 602*8485SPeter.Memishian@Sun.COM * If something cleared IFF_OFFLINE (e.g., by accident 603*8485SPeter.Memishian@Sun.COM * because the SIOCGLIFFLAGS/SIOCSLIFFLAGS sequence is 604*8485SPeter.Memishian@Sun.COM * inherently racy), the phyint may still be offline. 605*8485SPeter.Memishian@Sun.COM * Just ignore it. 606*8485SPeter.Memishian@Sun.COM */ 607*8485SPeter.Memishian@Sun.COM if (pii->pii_phyint->pi_state == PI_OFFLINE) 608*8485SPeter.Memishian@Sun.COM continue; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6112074Smeem li = pii->pii_probe_logint; 6122074Smeem if (li != NULL) { 6130Sstevel@tonic-gate /* 6142074Smeem * We've already got a test address; only proceed 6152074Smeem * if it's suboptimal. 6160Sstevel@tonic-gate */ 6172074Smeem if (rate_testflags(li->li_flags) == BEST_TESTFLAGS) 6182074Smeem continue; 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* 6220Sstevel@tonic-gate * Walk the logints of this phyint instance, and select 6230Sstevel@tonic-gate * the best available test address 6240Sstevel@tonic-gate */ 6250Sstevel@tonic-gate for (li = pii->pii_logint; li != NULL; li = li->li_next) { 6260Sstevel@tonic-gate /* 6272496Smeem * Skip 0.0.0.0 addresses, as those are never 6282496Smeem * actually usable. 6292496Smeem */ 6302496Smeem if (pii->pii_af == AF_INET && 6312496Smeem IN6_IS_ADDR_V4MAPPED_ANY(&li->li_addr)) 6322496Smeem continue; 6332496Smeem 6342496Smeem /* 6350Sstevel@tonic-gate * Skip any IPv6 logints that are not link-local, 6360Sstevel@tonic-gate * since we should always have a link-local address 6370Sstevel@tonic-gate * anyway and in6_data() expects link-local replies. 6380Sstevel@tonic-gate */ 6390Sstevel@tonic-gate if (pii->pii_af == AF_INET6 && 6400Sstevel@tonic-gate !IN6_IS_ADDR_LINKLOCAL(&li->li_addr)) 6410Sstevel@tonic-gate continue; 6420Sstevel@tonic-gate 6432074Smeem /* 6442074Smeem * Rate the testflags. If we've found an optimal 6452074Smeem * match, then break out; otherwise, record the most 6462074Smeem * recent OK one. 6472074Smeem */ 6482074Smeem rating = rate_testflags(li->li_flags); 6492074Smeem if (rating == BAD_TESTFLAGS) 6502074Smeem continue; 6512074Smeem 6522074Smeem probe_logint = li; 6532074Smeem if (rating == BEST_TESTFLAGS) 6542074Smeem break; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* 6582074Smeem * If the probe logint has changed, ditch the old one. 6590Sstevel@tonic-gate */ 6602074Smeem if (pii->pii_probe_logint != NULL && 6612074Smeem pii->pii_probe_logint != probe_logint) { 6620Sstevel@tonic-gate if (pii->pii_probe_sock != -1) 6630Sstevel@tonic-gate close_probe_socket(pii, _B_TRUE); 6640Sstevel@tonic-gate pii->pii_probe_logint = NULL; 6650Sstevel@tonic-gate } 6660Sstevel@tonic-gate 6672074Smeem if (probe_logint == NULL) { 6680Sstevel@tonic-gate /* 6694770Smeem * We don't have a test address; zero out the probe 6704770Smeem * stats array since it is no longer relevant. 6714770Smeem * Optimize by checking if it is already zeroed out. 6720Sstevel@tonic-gate */ 6730Sstevel@tonic-gate int pr_ndx; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate pr_ndx = PROBE_INDEX_PREV(pii->pii_probe_next); 6760Sstevel@tonic-gate if (pii->pii_probes[pr_ndx].pr_status != PR_UNUSED) { 6770Sstevel@tonic-gate clear_pii_probe_stats(pii); 6780Sstevel@tonic-gate reset_crtt_all(pii->pii_phyint); 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate continue; 6812074Smeem } else if (probe_logint == pii->pii_probe_logint) { 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * If we didn't find any new test addr, go to the 6840Sstevel@tonic-gate * next phyint. 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate continue; 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * The phyint is either being assigned a new testaddr 6910Sstevel@tonic-gate * or is being assigned a testaddr for the 1st time. 6920Sstevel@tonic-gate * Need to initialize the phyint socket 6930Sstevel@tonic-gate */ 6942074Smeem pii->pii_probe_logint = probe_logint; 6950Sstevel@tonic-gate if (!phyint_inst_sockinit(pii)) { 6960Sstevel@tonic-gate if (debug & D_PHYINT) { 6970Sstevel@tonic-gate logdebug("select_test_ifs: " 6980Sstevel@tonic-gate "phyint_sockinit failed\n"); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate phyint_inst_delete(pii); 7010Sstevel@tonic-gate continue; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * This phyint instance is now enabled for probes; this 7060Sstevel@tonic-gate * impacts our state machine in two ways: 7070Sstevel@tonic-gate * 7080Sstevel@tonic-gate * 1. If we're probe *capable* as well (i.e., we have 7090Sstevel@tonic-gate * probe targets) and the interface is in PI_NOTARGETS, 7100Sstevel@tonic-gate * then transition to PI_RUNNING. 7110Sstevel@tonic-gate * 7120Sstevel@tonic-gate * 2. If we're not probe capable, and the other phyint 7130Sstevel@tonic-gate * instance is also not probe capable, and we were in 7140Sstevel@tonic-gate * PI_RUNNING, then transition to PI_NOTARGETS. 7150Sstevel@tonic-gate * 7160Sstevel@tonic-gate * Also see the state diagram in mpd_probe.c. 7170Sstevel@tonic-gate */ 7180Sstevel@tonic-gate if (PROBE_CAPABLE(pii)) { 7190Sstevel@tonic-gate if (pii->pii_phyint->pi_state == PI_NOTARGETS) 7200Sstevel@tonic-gate phyint_chstate(pii->pii_phyint, PI_RUNNING); 7210Sstevel@tonic-gate } else if (!PROBE_CAPABLE(phyint_inst_other(pii))) { 7220Sstevel@tonic-gate if (pii->pii_phyint->pi_state == PI_RUNNING) 7230Sstevel@tonic-gate phyint_chstate(pii->pii_phyint, PI_NOTARGETS); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7270Sstevel@tonic-gate * If no targets are currently known for this phyint 7280Sstevel@tonic-gate * we need to call init_router_targets. Since 7290Sstevel@tonic-gate * init_router_targets() initializes the list of targets 7300Sstevel@tonic-gate * for all phyints it is done below the loop. 7310Sstevel@tonic-gate */ 7320Sstevel@tonic-gate if (pii->pii_targets == NULL) 7330Sstevel@tonic-gate target_scan_reqd = _B_TRUE; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * Start the probe timer for this instance. 7370Sstevel@tonic-gate */ 7382496Smeem if (!pii->pii_basetime_inited && PROBE_ENABLED(pii)) { 7390Sstevel@tonic-gate start_timer(pii); 7400Sstevel@tonic-gate pii->pii_basetime_inited = 1; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 745*8485SPeter.Memishian@Sun.COM * Scan the interface list for any interfaces that are PI_FAILED or 746*8485SPeter.Memishian@Sun.COM * PI_NOTARGETS but no longer enabled to send probes, and call 747*8485SPeter.Memishian@Sun.COM * phyint_check_for_repair() to see if the link state indicates that 748*8485SPeter.Memishian@Sun.COM * the interface should be repaired. Also see the state diagram in 7490Sstevel@tonic-gate * mpd_probe.c. 7500Sstevel@tonic-gate */ 7510Sstevel@tonic-gate for (pi = phyints; pi != NULL; pi = pi->pi_next) { 752*8485SPeter.Memishian@Sun.COM if ((!PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6)) && 753*8485SPeter.Memishian@Sun.COM (pi->pi_state == PI_FAILED || 754*8485SPeter.Memishian@Sun.COM pi->pi_state == PI_NOTARGETS)) { 7550Sstevel@tonic-gate phyint_check_for_repair(pi); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7594770Smeem check_testconfig(); 7604770Smeem 7610Sstevel@tonic-gate /* 7620Sstevel@tonic-gate * Try to populate the target list. init_router_targets populates 7630Sstevel@tonic-gate * the target list from the routing table. If our target list is 7640Sstevel@tonic-gate * still empty, init_host_targets adds host targets based on the 7650Sstevel@tonic-gate * host target list of other phyints in the group. 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate if (target_scan_reqd) { 7680Sstevel@tonic-gate init_router_targets(); 7690Sstevel@tonic-gate init_host_targets(); 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate /* 7746578Smeem * Check test address configuration, and log notices/errors if appropriate. 7756578Smeem * Note that this function only logs pre-existing conditions (e.g., that 7766578Smeem * probe-based failure detection is disabled). 7774770Smeem */ 7784770Smeem static void 7794770Smeem check_testconfig(void) 7804770Smeem { 7814770Smeem struct phyint *pi; 7824770Smeem struct logint *li; 7834770Smeem char abuf[INET6_ADDRSTRLEN]; 7846578Smeem int pri; 7854770Smeem 7864770Smeem for (pi = phyints; pi != NULL; pi = pi->pi_next) { 7874770Smeem if (pi->pi_flags & IFF_OFFLINE) 7884770Smeem continue; 7894770Smeem 7904770Smeem if (PROBE_ENABLED(pi->pi_v4) || PROBE_ENABLED(pi->pi_v6)) { 7914770Smeem if (pi->pi_taddrmsg_printed || 7924770Smeem pi->pi_duptaddrmsg_printed) { 7936578Smeem if (pi->pi_duptaddrmsg_printed) 7946578Smeem pri = LOG_ERR; 7956578Smeem else 7966578Smeem pri = LOG_INFO; 7976578Smeem logmsg(pri, "Test address now configured on " 7984770Smeem "interface %s; enabling probe-based " 7994770Smeem "failure detection on it\n", pi->pi_name); 8004770Smeem pi->pi_taddrmsg_printed = 0; 8014770Smeem pi->pi_duptaddrmsg_printed = 0; 8024770Smeem } 8034770Smeem continue; 8044770Smeem } 8054770Smeem 8064770Smeem li = NULL; 8074770Smeem if (pi->pi_v4 != NULL && pi->pi_v4->pii_probe_logint != NULL && 8084770Smeem pi->pi_v4->pii_probe_logint->li_dupaddr) 8094770Smeem li = pi->pi_v4->pii_probe_logint; 8104770Smeem 8114770Smeem if (pi->pi_v6 != NULL && pi->pi_v6->pii_probe_logint != NULL && 8124770Smeem pi->pi_v6->pii_probe_logint->li_dupaddr) 8134770Smeem li = pi->pi_v6->pii_probe_logint; 8144770Smeem 815*8485SPeter.Memishian@Sun.COM if (li != NULL && li->li_dupaddr) { 816*8485SPeter.Memishian@Sun.COM if (pi->pi_duptaddrmsg_printed) 817*8485SPeter.Memishian@Sun.COM continue; 818*8485SPeter.Memishian@Sun.COM logerr("Test address %s is not unique in group; " 819*8485SPeter.Memishian@Sun.COM "disabling probe-based failure detection on %s\n", 820*8485SPeter.Memishian@Sun.COM pr_addr(li->li_phyint_inst->pii_af, 821*8485SPeter.Memishian@Sun.COM li->li_addr, abuf, sizeof (abuf)), pi->pi_name); 822*8485SPeter.Memishian@Sun.COM pi->pi_duptaddrmsg_printed = 1; 8234770Smeem continue; 8244770Smeem } 8254770Smeem 8264770Smeem if (getcurrentsec() < pi->pi_taddrthresh) 8274770Smeem continue; 8284770Smeem 8294770Smeem if (!pi->pi_taddrmsg_printed) { 8306578Smeem logtrace("No test address configured on interface %s; " 8314770Smeem "disabling probe-based failure detection on it\n", 8324770Smeem pi->pi_name); 8334770Smeem pi->pi_taddrmsg_printed = 1; 8344770Smeem } 8354770Smeem } 8364770Smeem } 8374770Smeem 8384770Smeem /* 8390Sstevel@tonic-gate * Check phyint group configuration, to detect any inconsistencies, 8400Sstevel@tonic-gate * and log an error message. This is called from runtimeouts every 8410Sstevel@tonic-gate * 20 secs. But the error message is displayed once. If the 8420Sstevel@tonic-gate * consistency is resolved by the admin, a recovery message is displayed 8430Sstevel@tonic-gate * once. 8440Sstevel@tonic-gate */ 8450Sstevel@tonic-gate static void 8460Sstevel@tonic-gate check_config(void) 8470Sstevel@tonic-gate { 8480Sstevel@tonic-gate struct phyint_group *pg; 8490Sstevel@tonic-gate struct phyint *pi; 8500Sstevel@tonic-gate boolean_t v4_in_group; 8510Sstevel@tonic-gate boolean_t v6_in_group; 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* 854*8485SPeter.Memishian@Sun.COM * All phyints of a group must be homogeneous to ensure that they can 855*8485SPeter.Memishian@Sun.COM * take over for one another. If any phyint in a group has IPv4 856*8485SPeter.Memishian@Sun.COM * plumbed, check that all phyints have IPv4 plumbed. Do a similar 857*8485SPeter.Memishian@Sun.COM * check for IPv6. 8580Sstevel@tonic-gate */ 8590Sstevel@tonic-gate for (pg = phyint_groups; pg != NULL; pg = pg->pg_next) { 8600Sstevel@tonic-gate if (pg == phyint_anongroup) 8610Sstevel@tonic-gate continue; 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate v4_in_group = _B_FALSE; 8640Sstevel@tonic-gate v6_in_group = _B_FALSE; 8650Sstevel@tonic-gate /* 8660Sstevel@tonic-gate * 1st pass. Determine if at least 1 phyint in the group 8670Sstevel@tonic-gate * has IPv4 plumbed and if so set v4_in_group to true. 8680Sstevel@tonic-gate * Repeat similarly for IPv6. 8690Sstevel@tonic-gate */ 8700Sstevel@tonic-gate for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) { 8710Sstevel@tonic-gate if (pi->pi_v4 != NULL) 8720Sstevel@tonic-gate v4_in_group = _B_TRUE; 8730Sstevel@tonic-gate if (pi->pi_v6 != NULL) 8740Sstevel@tonic-gate v6_in_group = _B_TRUE; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate /* 8780Sstevel@tonic-gate * 2nd pass. If v4_in_group is true, check that phyint 8790Sstevel@tonic-gate * has IPv4 plumbed. Repeat similarly for IPv6. Print 8800Sstevel@tonic-gate * out a message the 1st time only. 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) { 8830Sstevel@tonic-gate if (pi->pi_flags & IFF_OFFLINE) 8840Sstevel@tonic-gate continue; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate if (v4_in_group == _B_TRUE && pi->pi_v4 == NULL) { 8870Sstevel@tonic-gate if (!pi->pi_cfgmsg_printed) { 888*8485SPeter.Memishian@Sun.COM logerr("IP interface %s in group %s is" 889*8485SPeter.Memishian@Sun.COM " not plumbed for IPv4, affecting" 890*8485SPeter.Memishian@Sun.COM " IPv4 connectivity\n", 8910Sstevel@tonic-gate pi->pi_name, 8920Sstevel@tonic-gate pi->pi_group->pg_name); 8930Sstevel@tonic-gate pi->pi_cfgmsg_printed = 1; 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate } else if (v6_in_group == _B_TRUE && 8960Sstevel@tonic-gate pi->pi_v6 == NULL) { 8970Sstevel@tonic-gate if (!pi->pi_cfgmsg_printed) { 898*8485SPeter.Memishian@Sun.COM logerr("IP interface %s in group %s is" 899*8485SPeter.Memishian@Sun.COM " not plumbed for IPv6, affecting" 900*8485SPeter.Memishian@Sun.COM " IPv6 connectivity\n", 9010Sstevel@tonic-gate pi->pi_name, 9020Sstevel@tonic-gate pi->pi_group->pg_name); 9030Sstevel@tonic-gate pi->pi_cfgmsg_printed = 1; 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate } else { 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * The phyint matches the group configuration, 9080Sstevel@tonic-gate * if we have reached this point. If it was 9090Sstevel@tonic-gate * improperly configured earlier, log an 9100Sstevel@tonic-gate * error recovery message 9110Sstevel@tonic-gate */ 9120Sstevel@tonic-gate if (pi->pi_cfgmsg_printed) { 913*8485SPeter.Memishian@Sun.COM logerr("IP interface %s is now" 914*8485SPeter.Memishian@Sun.COM " consistent with group %s " 915*8485SPeter.Memishian@Sun.COM " and connectivity is restored\n", 916*8485SPeter.Memishian@Sun.COM pi->pi_name, pi->pi_group->pg_name); 9170Sstevel@tonic-gate pi->pi_cfgmsg_printed = 0; 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate } 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * Timer mechanism using relative time (in milliseconds) from the 9270Sstevel@tonic-gate * previous timer event. Timers exceeding TIMER_INFINITY milliseconds 9280Sstevel@tonic-gate * will fire after TIMER_INFINITY milliseconds. 9290Sstevel@tonic-gate * Unsigned arithmetic note: We assume a 32-bit circular sequence space for 9300Sstevel@tonic-gate * time values. Hence 2 consecutive timer events cannot be spaced farther 9310Sstevel@tonic-gate * than 0x7fffffff. We call this TIMER_INFINITY, and it is the maximum value 9320Sstevel@tonic-gate * that can be passed for the delay parameter of timer_schedule() 9330Sstevel@tonic-gate */ 9340Sstevel@tonic-gate static uint_t timer_next; /* Currently scheduled timeout */ 9350Sstevel@tonic-gate static boolean_t timer_active = _B_FALSE; /* SIGALRM has not yet occurred */ 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate static void 9380Sstevel@tonic-gate timer_init(void) 9390Sstevel@tonic-gate { 9400Sstevel@tonic-gate timer_next = getcurrenttime() + TIMER_INFINITY; 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * The call to run_timeouts() will get the timer started 9430Sstevel@tonic-gate * Since there are no phyints at this point, the timer will 9440Sstevel@tonic-gate * be set for IF_SCAN_INTERVAL ms. 9450Sstevel@tonic-gate */ 9460Sstevel@tonic-gate run_timeouts(); 9470Sstevel@tonic-gate } 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate /* 9500Sstevel@tonic-gate * Make sure the next SIGALRM occurs delay milliseconds from the current 9510Sstevel@tonic-gate * time if not earlier. We are interested only in time differences. 9520Sstevel@tonic-gate */ 9530Sstevel@tonic-gate void 9540Sstevel@tonic-gate timer_schedule(uint_t delay) 9550Sstevel@tonic-gate { 9560Sstevel@tonic-gate uint_t now; 9570Sstevel@tonic-gate struct itimerval itimerval; 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate if (debug & D_TIMER) 9600Sstevel@tonic-gate logdebug("timer_schedule(%u)\n", delay); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate assert(delay <= TIMER_INFINITY); 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate now = getcurrenttime(); 9650Sstevel@tonic-gate if (delay == 0) { 9660Sstevel@tonic-gate /* Minimum allowed delay */ 9670Sstevel@tonic-gate delay = 1; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate /* Will this timer occur before the currently scheduled SIGALRM? */ 9700Sstevel@tonic-gate if (timer_active && TIME_GE(now + delay, timer_next)) { 9710Sstevel@tonic-gate if (debug & D_TIMER) { 9720Sstevel@tonic-gate logdebug("timer_schedule(%u) - no action: " 9730Sstevel@tonic-gate "now %u next %u\n", delay, now, timer_next); 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate return; 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate timer_next = now + delay; 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate itimerval.it_value.tv_sec = delay / 1000; 9800Sstevel@tonic-gate itimerval.it_value.tv_usec = (delay % 1000) * 1000; 9810Sstevel@tonic-gate itimerval.it_interval.tv_sec = 0; 9820Sstevel@tonic-gate itimerval.it_interval.tv_usec = 0; 9830Sstevel@tonic-gate if (debug & D_TIMER) { 9840Sstevel@tonic-gate logdebug("timer_schedule(%u): sec %ld usec %ld\n", 9850Sstevel@tonic-gate delay, itimerval.it_value.tv_sec, 9860Sstevel@tonic-gate itimerval.it_value.tv_usec); 9870Sstevel@tonic-gate } 9880Sstevel@tonic-gate timer_active = _B_TRUE; 9890Sstevel@tonic-gate if (setitimer(ITIMER_REAL, &itimerval, NULL) < 0) { 9900Sstevel@tonic-gate logperror("timer_schedule: setitimer"); 9910Sstevel@tonic-gate exit(2); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * Timer has fired. Determine when the next timer event will occur by asking 9970Sstevel@tonic-gate * all the timer routines. Should not be called from a timer routine. 9980Sstevel@tonic-gate */ 9990Sstevel@tonic-gate static void 10000Sstevel@tonic-gate run_timeouts(void) 10010Sstevel@tonic-gate { 10020Sstevel@tonic-gate uint_t next; 10030Sstevel@tonic-gate uint_t next_event_time; 10040Sstevel@tonic-gate struct phyint_instance *pii; 10050Sstevel@tonic-gate struct phyint_instance *next_pii; 10060Sstevel@tonic-gate static boolean_t timeout_running; 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate /* assert that recursive timeouts don't happen. */ 10090Sstevel@tonic-gate assert(!timeout_running); 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate timeout_running = _B_TRUE; 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate if (debug & D_TIMER) 10140Sstevel@tonic-gate logdebug("run_timeouts()\n"); 10150Sstevel@tonic-gate 10164770Smeem if ((getcurrenttime() - last_initifs_time) > IF_SCAN_INTERVAL) { 10174770Smeem initifs(); 10184770Smeem check_config(); 10194770Smeem } 10204770Smeem 10210Sstevel@tonic-gate next = TIMER_INFINITY; 10220Sstevel@tonic-gate 10230Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = next_pii) { 10240Sstevel@tonic-gate next_pii = pii->pii_next; 10250Sstevel@tonic-gate next_event_time = phyint_inst_timer(pii); 10260Sstevel@tonic-gate if (next_event_time != TIMER_INFINITY && next_event_time < next) 10270Sstevel@tonic-gate next = next_event_time; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate if (debug & D_TIMER) { 10300Sstevel@tonic-gate logdebug("run_timeouts(%s %s): next scheduled for" 10310Sstevel@tonic-gate " this phyint inst %u, next scheduled global" 10320Sstevel@tonic-gate " %u ms\n", 10330Sstevel@tonic-gate AF_STR(pii->pii_af), pii->pii_phyint->pi_name, 10340Sstevel@tonic-gate next_event_time, next); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * Make sure initifs() is called at least once every 10400Sstevel@tonic-gate * IF_SCAN_INTERVAL, to make sure that we are in sync 10410Sstevel@tonic-gate * with the kernel, in case we have missed any routing 10420Sstevel@tonic-gate * socket messages. 10430Sstevel@tonic-gate */ 10440Sstevel@tonic-gate if (next > IF_SCAN_INTERVAL) 10450Sstevel@tonic-gate next = IF_SCAN_INTERVAL; 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate if (debug & D_TIMER) 10480Sstevel@tonic-gate logdebug("run_timeouts: %u ms\n", next); 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate timer_schedule(next); 10510Sstevel@tonic-gate timeout_running = _B_FALSE; 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate static int eventpipe_read = -1; /* Used for synchronous signal delivery */ 10550Sstevel@tonic-gate static int eventpipe_write = -1; 1056*8485SPeter.Memishian@Sun.COM boolean_t cleanup_started = _B_FALSE; /* true if we're going away */ 1057*8485SPeter.Memishian@Sun.COM 10580Sstevel@tonic-gate /* 10590Sstevel@tonic-gate * Ensure that signals are processed synchronously with the rest of 10600Sstevel@tonic-gate * the code by just writing a one character signal number on the pipe. 10610Sstevel@tonic-gate * The poll loop will pick this up and process the signal event. 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate static void 10640Sstevel@tonic-gate sig_handler(int signo) 10650Sstevel@tonic-gate { 10660Sstevel@tonic-gate uchar_t buf = (uchar_t)signo; 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate /* 10690Sstevel@tonic-gate * Don't write to pipe if cleanup has already begun. cleanup() 10700Sstevel@tonic-gate * might have closed the pipe already 10710Sstevel@tonic-gate */ 10720Sstevel@tonic-gate if (cleanup_started) 10730Sstevel@tonic-gate return; 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if (eventpipe_write == -1) { 10760Sstevel@tonic-gate logerr("sig_handler: no pipe found\n"); 10770Sstevel@tonic-gate return; 10780Sstevel@tonic-gate } 10790Sstevel@tonic-gate if (write(eventpipe_write, &buf, sizeof (buf)) < 0) 10800Sstevel@tonic-gate logperror("sig_handler: write"); 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate extern struct probes_missed probes_missed; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate /* 10860Sstevel@tonic-gate * Pick up a signal "byte" from the pipe and process it. 10870Sstevel@tonic-gate */ 10880Sstevel@tonic-gate static void 10890Sstevel@tonic-gate in_signal(int fd) 10900Sstevel@tonic-gate { 10910Sstevel@tonic-gate uchar_t buf; 10920Sstevel@tonic-gate uint64_t sent, acked, lost, unacked, unknown; 10930Sstevel@tonic-gate struct phyint_instance *pii; 10940Sstevel@tonic-gate int pr_ndx; 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate switch (read(fd, &buf, sizeof (buf))) { 10970Sstevel@tonic-gate case -1: 10980Sstevel@tonic-gate logperror("in_signal: read"); 10990Sstevel@tonic-gate exit(1); 11000Sstevel@tonic-gate /* NOTREACHED */ 11010Sstevel@tonic-gate case 1: 11020Sstevel@tonic-gate break; 11030Sstevel@tonic-gate case 0: 11040Sstevel@tonic-gate logerr("in_signal: read end of file\n"); 11050Sstevel@tonic-gate exit(1); 11060Sstevel@tonic-gate /* NOTREACHED */ 11070Sstevel@tonic-gate default: 11080Sstevel@tonic-gate logerr("in_signal: read > 1\n"); 11090Sstevel@tonic-gate exit(1); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate if (debug & D_TIMER) 11130Sstevel@tonic-gate logdebug("in_signal() got %d\n", buf); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate switch (buf) { 11160Sstevel@tonic-gate case SIGALRM: 11170Sstevel@tonic-gate if (debug & D_TIMER) { 11180Sstevel@tonic-gate uint_t now = getcurrenttime(); 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate logdebug("in_signal(SIGALRM) delta %u\n", 11210Sstevel@tonic-gate now - timer_next); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate timer_active = _B_FALSE; 11240Sstevel@tonic-gate run_timeouts(); 11250Sstevel@tonic-gate break; 11260Sstevel@tonic-gate case SIGUSR1: 11270Sstevel@tonic-gate logdebug("Printing configuration:\n"); 11280Sstevel@tonic-gate /* Print out the internal tables */ 11290Sstevel@tonic-gate phyint_inst_print_all(); 11300Sstevel@tonic-gate 11310Sstevel@tonic-gate /* 11320Sstevel@tonic-gate * Print out the accumulated statistics about missed 11330Sstevel@tonic-gate * probes (happens due to scheduling delay). 11340Sstevel@tonic-gate */ 11350Sstevel@tonic-gate logerr("Missed sending total of %d probes spread over" 11360Sstevel@tonic-gate " %d occurrences\n", probes_missed.pm_nprobes, 11370Sstevel@tonic-gate probes_missed.pm_ntimes); 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate /* 11400Sstevel@tonic-gate * Print out the accumulated statistics about probes 11410Sstevel@tonic-gate * that were sent. 11420Sstevel@tonic-gate */ 11430Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; 11440Sstevel@tonic-gate pii = pii->pii_next) { 11450Sstevel@tonic-gate unacked = 0; 11460Sstevel@tonic-gate acked = pii->pii_cum_stats.acked; 11470Sstevel@tonic-gate lost = pii->pii_cum_stats.lost; 11480Sstevel@tonic-gate sent = pii->pii_cum_stats.sent; 11490Sstevel@tonic-gate unknown = pii->pii_cum_stats.unknown; 11500Sstevel@tonic-gate for (pr_ndx = 0; pr_ndx < PROBE_STATS_COUNT; pr_ndx++) { 11510Sstevel@tonic-gate switch (pii->pii_probes[pr_ndx].pr_status) { 11520Sstevel@tonic-gate case PR_ACKED: 11530Sstevel@tonic-gate acked++; 11540Sstevel@tonic-gate break; 11550Sstevel@tonic-gate case PR_LOST: 11560Sstevel@tonic-gate lost++; 11570Sstevel@tonic-gate break; 11580Sstevel@tonic-gate case PR_UNACKED: 11590Sstevel@tonic-gate unacked++; 11600Sstevel@tonic-gate break; 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate } 11630Sstevel@tonic-gate logerr("\nProbe stats on (%s %s)\n" 11640Sstevel@tonic-gate "Number of probes sent %lld\n" 11650Sstevel@tonic-gate "Number of probe acks received %lld\n" 11660Sstevel@tonic-gate "Number of probes/acks lost %lld\n" 1167*8485SPeter.Memishian@Sun.COM "Number of valid unacknowledged probes %lld\n" 11680Sstevel@tonic-gate "Number of ambiguous probe acks received %lld\n", 11690Sstevel@tonic-gate AF_STR(pii->pii_af), pii->pii_name, 11700Sstevel@tonic-gate sent, acked, lost, unacked, unknown); 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate break; 11730Sstevel@tonic-gate case SIGHUP: 11740Sstevel@tonic-gate logerr("SIGHUP: restart and reread config file\n"); 11750Sstevel@tonic-gate cleanup(); 11760Sstevel@tonic-gate (void) execv(argv0[0], argv0); 11770Sstevel@tonic-gate _exit(0177); 11780Sstevel@tonic-gate /* NOTREACHED */ 11790Sstevel@tonic-gate case SIGINT: 11800Sstevel@tonic-gate case SIGTERM: 11810Sstevel@tonic-gate case SIGQUIT: 11820Sstevel@tonic-gate cleanup(); 11830Sstevel@tonic-gate exit(0); 11840Sstevel@tonic-gate /* NOTREACHED */ 11850Sstevel@tonic-gate default: 11860Sstevel@tonic-gate logerr("in_signal: unknown signal: %d\n", buf); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate static void 11910Sstevel@tonic-gate cleanup(void) 11920Sstevel@tonic-gate { 11930Sstevel@tonic-gate struct phyint_instance *pii; 11940Sstevel@tonic-gate struct phyint_instance *next_pii; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate /* 11970Sstevel@tonic-gate * Make sure that we don't write to eventpipe in 11980Sstevel@tonic-gate * sig_handler() if any signal notably SIGALRM, 11990Sstevel@tonic-gate * occurs after we close the eventpipe descriptor below 12000Sstevel@tonic-gate */ 12010Sstevel@tonic-gate cleanup_started = _B_TRUE; 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = next_pii) { 12040Sstevel@tonic-gate next_pii = pii->pii_next; 12050Sstevel@tonic-gate phyint_inst_delete(pii); 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate (void) close(ifsock_v4); 12090Sstevel@tonic-gate (void) close(ifsock_v6); 12100Sstevel@tonic-gate (void) close(rtsock_v4); 12110Sstevel@tonic-gate (void) close(rtsock_v6); 12120Sstevel@tonic-gate (void) close(lsock_v4); 12130Sstevel@tonic-gate (void) close(lsock_v6); 12140Sstevel@tonic-gate (void) close(0); 12150Sstevel@tonic-gate (void) close(1); 12160Sstevel@tonic-gate (void) close(2); 12170Sstevel@tonic-gate (void) close(mibfd); 12180Sstevel@tonic-gate (void) close(eventpipe_read); 12190Sstevel@tonic-gate (void) close(eventpipe_write); 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate /* 12230Sstevel@tonic-gate * Create pipe for signal delivery and set up signal handlers. 12240Sstevel@tonic-gate */ 12250Sstevel@tonic-gate static void 12260Sstevel@tonic-gate setup_eventpipe(void) 12270Sstevel@tonic-gate { 12280Sstevel@tonic-gate int fds[2]; 12290Sstevel@tonic-gate struct sigaction act; 12300Sstevel@tonic-gate 12310Sstevel@tonic-gate if ((pipe(fds)) < 0) { 12320Sstevel@tonic-gate logperror("setup_eventpipe: pipe"); 12330Sstevel@tonic-gate exit(1); 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate eventpipe_read = fds[0]; 12360Sstevel@tonic-gate eventpipe_write = fds[1]; 12370Sstevel@tonic-gate if (poll_add(eventpipe_read) == -1) { 12380Sstevel@tonic-gate exit(1); 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate act.sa_handler = sig_handler; 12420Sstevel@tonic-gate act.sa_flags = SA_RESTART; 12430Sstevel@tonic-gate (void) sigaction(SIGALRM, &act, NULL); 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate (void) sigset(SIGHUP, sig_handler); 12460Sstevel@tonic-gate (void) sigset(SIGUSR1, sig_handler); 12470Sstevel@tonic-gate (void) sigset(SIGTERM, sig_handler); 12480Sstevel@tonic-gate (void) sigset(SIGINT, sig_handler); 12490Sstevel@tonic-gate (void) sigset(SIGQUIT, sig_handler); 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate /* 12530Sstevel@tonic-gate * Create a routing socket for receiving RTM_IFINFO messages. 12540Sstevel@tonic-gate */ 12550Sstevel@tonic-gate static int 12560Sstevel@tonic-gate setup_rtsock(int af) 12570Sstevel@tonic-gate { 12580Sstevel@tonic-gate int s; 12590Sstevel@tonic-gate int flags; 1260*8485SPeter.Memishian@Sun.COM int aware = RTAW_UNDER_IPMP; 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate s = socket(PF_ROUTE, SOCK_RAW, af); 12630Sstevel@tonic-gate if (s == -1) { 12640Sstevel@tonic-gate logperror("setup_rtsock: socket PF_ROUTE"); 12650Sstevel@tonic-gate exit(1); 12660Sstevel@tonic-gate } 1267*8485SPeter.Memishian@Sun.COM 1268*8485SPeter.Memishian@Sun.COM if (setsockopt(s, SOL_ROUTE, RT_AWARE, &aware, sizeof (aware)) == -1) { 1269*8485SPeter.Memishian@Sun.COM logperror("setup_rtsock: setsockopt RT_AWARE"); 1270*8485SPeter.Memishian@Sun.COM (void) close(s); 1271*8485SPeter.Memishian@Sun.COM exit(1); 1272*8485SPeter.Memishian@Sun.COM } 1273*8485SPeter.Memishian@Sun.COM 12740Sstevel@tonic-gate if ((flags = fcntl(s, F_GETFL, 0)) < 0) { 12750Sstevel@tonic-gate logperror("setup_rtsock: fcntl F_GETFL"); 12760Sstevel@tonic-gate (void) close(s); 12770Sstevel@tonic-gate exit(1); 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate if ((fcntl(s, F_SETFL, flags | O_NONBLOCK)) < 0) { 12800Sstevel@tonic-gate logperror("setup_rtsock: fcntl F_SETFL"); 12810Sstevel@tonic-gate (void) close(s); 12820Sstevel@tonic-gate exit(1); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate if (poll_add(s) == -1) { 12850Sstevel@tonic-gate (void) close(s); 12860Sstevel@tonic-gate exit(1); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate return (s); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * Process an RTM_IFINFO message received on a routing socket. 12930Sstevel@tonic-gate * The return value indicates whether a full interface scan is required. 1294*8485SPeter.Memishian@Sun.COM * Link up/down notifications are reflected in the IFF_RUNNING flag. 12950Sstevel@tonic-gate * If just the state of the IFF_RUNNING interface flag has changed, a 12960Sstevel@tonic-gate * a full interface scan isn't required. 12970Sstevel@tonic-gate */ 12980Sstevel@tonic-gate static boolean_t 12990Sstevel@tonic-gate process_rtm_ifinfo(if_msghdr_t *ifm, int type) 13000Sstevel@tonic-gate { 13010Sstevel@tonic-gate struct sockaddr_dl *sdl; 13020Sstevel@tonic-gate struct phyint *pi; 13030Sstevel@tonic-gate uint64_t old_flags; 13040Sstevel@tonic-gate struct phyint_instance *pii; 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate assert(ifm->ifm_type == RTM_IFINFO && ifm->ifm_addrs == RTA_IFP); 13070Sstevel@tonic-gate 13080Sstevel@tonic-gate /* 13090Sstevel@tonic-gate * Although the sockaddr_dl structure is directly after the 13100Sstevel@tonic-gate * if_msghdr_t structure. At the time of writing, the size of the 13110Sstevel@tonic-gate * if_msghdr_t structure is different on 32 and 64 bit kernels, due 13120Sstevel@tonic-gate * to the presence of a timeval structure, which contains longs, 13130Sstevel@tonic-gate * in the if_data structure. Anyway, we know where the message ends, 13140Sstevel@tonic-gate * so we work backwards to get the start of the sockaddr_dl structure. 13150Sstevel@tonic-gate */ 13160Sstevel@tonic-gate /*LINTED*/ 13170Sstevel@tonic-gate sdl = (struct sockaddr_dl *)((char *)ifm + ifm->ifm_msglen - 13184770Smeem sizeof (struct sockaddr_dl)); 13190Sstevel@tonic-gate 13200Sstevel@tonic-gate assert(sdl->sdl_family == AF_LINK); 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate /* 13230Sstevel@tonic-gate * The interface name is in sdl_data. 13240Sstevel@tonic-gate * RTM_IFINFO messages are only generated for logical interface 13250Sstevel@tonic-gate * zero, so there is no colon and logical interface number to 13260Sstevel@tonic-gate * strip from the name. The name is not null terminated, but 13270Sstevel@tonic-gate * there should be enough space in sdl_data to add the null. 13280Sstevel@tonic-gate */ 13290Sstevel@tonic-gate if (sdl->sdl_nlen >= sizeof (sdl->sdl_data)) { 13300Sstevel@tonic-gate if (debug & D_LINKNOTE) 13314770Smeem logdebug("process_rtm_ifinfo: phyint name too long\n"); 13320Sstevel@tonic-gate return (_B_TRUE); 13330Sstevel@tonic-gate } 13340Sstevel@tonic-gate sdl->sdl_data[sdl->sdl_nlen] = 0; 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate pi = phyint_lookup(sdl->sdl_data); 13370Sstevel@tonic-gate if (pi == NULL) { 13380Sstevel@tonic-gate if (debug & D_LINKNOTE) 13390Sstevel@tonic-gate logdebug("process_rtm_ifinfo: phyint lookup failed" 13404770Smeem " for %s\n", sdl->sdl_data); 13410Sstevel@tonic-gate return (_B_TRUE); 13420Sstevel@tonic-gate } 13430Sstevel@tonic-gate 13440Sstevel@tonic-gate /* 13450Sstevel@tonic-gate * We want to try and avoid doing a full interface scan for 1346*8485SPeter.Memishian@Sun.COM * link state notifications from the datalink layer, as indicated 13470Sstevel@tonic-gate * by the state of the IFF_RUNNING flag. If just the 13480Sstevel@tonic-gate * IFF_RUNNING flag has changed state, the link state changes 13490Sstevel@tonic-gate * are processed without a full scan. 13500Sstevel@tonic-gate * If there is both an IPv4 and IPv6 instance associated with 13510Sstevel@tonic-gate * the physical interface, we will get an RTM_IFINFO message 13520Sstevel@tonic-gate * for each instance. If we just maintained a single copy of 13530Sstevel@tonic-gate * the physical interface flags, it would appear that no flags 13540Sstevel@tonic-gate * had changed when the second message is processed, leading us 13550Sstevel@tonic-gate * to believe that the message wasn't generated by a flags change, 13560Sstevel@tonic-gate * and that a full interface scan is required. 13570Sstevel@tonic-gate * To get around this problem, two additional copies of the flags 13580Sstevel@tonic-gate * are kept, one copy for each instance. These are only used in 13590Sstevel@tonic-gate * this routine. At any one time, all three copies of the flags 13600Sstevel@tonic-gate * should be identical except for the IFF_RUNNING flag. The 13610Sstevel@tonic-gate * copy of the flags in the "phyint" structure is always up to 13620Sstevel@tonic-gate * date. 13630Sstevel@tonic-gate */ 13640Sstevel@tonic-gate pii = (type == AF_INET) ? pi->pi_v4 : pi->pi_v6; 13650Sstevel@tonic-gate if (pii == NULL) { 13660Sstevel@tonic-gate if (debug & D_LINKNOTE) 13670Sstevel@tonic-gate logdebug("process_rtm_ifinfo: no instance of address " 13680Sstevel@tonic-gate "family %s for %s\n", AF_STR(type), pi->pi_name); 13690Sstevel@tonic-gate return (_B_TRUE); 13700Sstevel@tonic-gate } 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate old_flags = pii->pii_flags; 13730Sstevel@tonic-gate pii->pii_flags = PHYINT_FLAGS(ifm->ifm_flags); 13740Sstevel@tonic-gate pi->pi_flags = pii->pii_flags; 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate if (debug & D_LINKNOTE) { 13770Sstevel@tonic-gate logdebug("process_rtm_ifinfo: %s address family: %s, " 13780Sstevel@tonic-gate "old flags: %llx, new flags: %llx\n", pi->pi_name, 13790Sstevel@tonic-gate AF_STR(type), old_flags, pi->pi_flags); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* 13830Sstevel@tonic-gate * If IFF_STANDBY has changed, indicate that the interface has changed 13840Sstevel@tonic-gate * types. 13850Sstevel@tonic-gate */ 13860Sstevel@tonic-gate if ((old_flags ^ pii->pii_flags) & IFF_STANDBY) 1387*8485SPeter.Memishian@Sun.COM phyint_changed(pi); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* Has just the IFF_RUNNING flag changed state ? */ 13900Sstevel@tonic-gate if ((old_flags ^ pii->pii_flags) != IFF_RUNNING) { 13910Sstevel@tonic-gate struct phyint_instance *pii_other; 13920Sstevel@tonic-gate /* 13930Sstevel@tonic-gate * It wasn't just a link state change. Update 13940Sstevel@tonic-gate * the other instance's copy of the flags. 13950Sstevel@tonic-gate */ 13960Sstevel@tonic-gate pii_other = phyint_inst_other(pii); 13970Sstevel@tonic-gate if (pii_other != NULL) 13980Sstevel@tonic-gate pii_other->pii_flags = pii->pii_flags; 13990Sstevel@tonic-gate return (_B_TRUE); 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate 14020Sstevel@tonic-gate return (_B_FALSE); 14030Sstevel@tonic-gate } 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate /* 14060Sstevel@tonic-gate * Retrieve as many routing socket messages as possible, and try to 14070Sstevel@tonic-gate * empty the routing sockets. Initiate full scan of targets or interfaces 14080Sstevel@tonic-gate * as needed. 14090Sstevel@tonic-gate * We listen on separate IPv4 an IPv6 sockets so that we can accurately 14100Sstevel@tonic-gate * detect changes in certain flags (see "process_rtm_ifinfo()" above). 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate static void 14130Sstevel@tonic-gate process_rtsock(int rtsock_v4, int rtsock_v6) 14140Sstevel@tonic-gate { 14150Sstevel@tonic-gate int nbytes; 14160Sstevel@tonic-gate int64_t msg[2048 / 8]; 14170Sstevel@tonic-gate struct rt_msghdr *rtm; 14180Sstevel@tonic-gate boolean_t need_if_scan = _B_FALSE; 14190Sstevel@tonic-gate boolean_t need_rt_scan = _B_FALSE; 14200Sstevel@tonic-gate boolean_t rtm_ifinfo_seen = _B_FALSE; 14210Sstevel@tonic-gate int type; 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate /* Read as many messages as possible and try to empty the sockets */ 14240Sstevel@tonic-gate for (type = AF_INET; ; type = AF_INET6) { 14250Sstevel@tonic-gate for (;;) { 14260Sstevel@tonic-gate nbytes = read((type == AF_INET) ? rtsock_v4 : 14274770Smeem rtsock_v6, msg, sizeof (msg)); 14280Sstevel@tonic-gate if (nbytes <= 0) { 14290Sstevel@tonic-gate /* No more messages */ 14300Sstevel@tonic-gate break; 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate rtm = (struct rt_msghdr *)msg; 14330Sstevel@tonic-gate if (rtm->rtm_version != RTM_VERSION) { 14340Sstevel@tonic-gate logerr("process_rtsock: version %d " 14350Sstevel@tonic-gate "not understood\n", rtm->rtm_version); 14360Sstevel@tonic-gate break; 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate if (debug & D_PHYINT) { 14400Sstevel@tonic-gate logdebug("process_rtsock: message %d\n", 14410Sstevel@tonic-gate rtm->rtm_type); 14420Sstevel@tonic-gate } 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate switch (rtm->rtm_type) { 14450Sstevel@tonic-gate case RTM_NEWADDR: 14460Sstevel@tonic-gate case RTM_DELADDR: 14470Sstevel@tonic-gate /* 14480Sstevel@tonic-gate * Some logical interface has changed, 14490Sstevel@tonic-gate * have to scan everything to determine 14500Sstevel@tonic-gate * what actually changed. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate need_if_scan = _B_TRUE; 14530Sstevel@tonic-gate break; 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate case RTM_IFINFO: 14560Sstevel@tonic-gate rtm_ifinfo_seen = _B_TRUE; 14574770Smeem need_if_scan |= process_rtm_ifinfo( 14584770Smeem (if_msghdr_t *)rtm, type); 14590Sstevel@tonic-gate break; 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate case RTM_ADD: 14620Sstevel@tonic-gate case RTM_DELETE: 14630Sstevel@tonic-gate case RTM_CHANGE: 14640Sstevel@tonic-gate case RTM_OLDADD: 14650Sstevel@tonic-gate case RTM_OLDDEL: 14660Sstevel@tonic-gate need_rt_scan = _B_TRUE; 14670Sstevel@tonic-gate break; 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate default: 14700Sstevel@tonic-gate /* Not interesting */ 14710Sstevel@tonic-gate break; 14720Sstevel@tonic-gate } 14730Sstevel@tonic-gate } 14740Sstevel@tonic-gate if (type == AF_INET6) 14750Sstevel@tonic-gate break; 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate if (need_if_scan) { 14790Sstevel@tonic-gate if (debug & D_LINKNOTE && rtm_ifinfo_seen) 14800Sstevel@tonic-gate logdebug("process_rtsock: synchronizing with kernel\n"); 14810Sstevel@tonic-gate initifs(); 14820Sstevel@tonic-gate } else if (rtm_ifinfo_seen) { 14830Sstevel@tonic-gate if (debug & D_LINKNOTE) 14840Sstevel@tonic-gate logdebug("process_rtsock: " 14850Sstevel@tonic-gate "link up/down notification(s) seen\n"); 14860Sstevel@tonic-gate process_link_state_changes(); 14870Sstevel@tonic-gate } 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate if (need_rt_scan) 14900Sstevel@tonic-gate init_router_targets(); 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * Look if the phyint instance or one of its logints have been removed from 14950Sstevel@tonic-gate * the kernel and take appropriate action. 14960Sstevel@tonic-gate * Uses {pii,li}_in_use. 14970Sstevel@tonic-gate */ 14980Sstevel@tonic-gate static void 14990Sstevel@tonic-gate check_if_removed(struct phyint_instance *pii) 15000Sstevel@tonic-gate { 15010Sstevel@tonic-gate struct logint *li; 15020Sstevel@tonic-gate struct logint *next_li; 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate /* Detect phyints that have been removed from the kernel. */ 15050Sstevel@tonic-gate if (!pii->pii_in_use) { 15060Sstevel@tonic-gate logtrace("%s %s has been removed from kernel\n", 15070Sstevel@tonic-gate AF_STR(pii->pii_af), pii->pii_phyint->pi_name); 15080Sstevel@tonic-gate phyint_inst_delete(pii); 15090Sstevel@tonic-gate } else { 15100Sstevel@tonic-gate /* Detect logints that have been removed. */ 15110Sstevel@tonic-gate for (li = pii->pii_logint; li != NULL; li = next_li) { 15120Sstevel@tonic-gate next_li = li->li_next; 15130Sstevel@tonic-gate if (!li->li_in_use) { 15140Sstevel@tonic-gate logint_delete(li); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate /* 15210Sstevel@tonic-gate * Send down a T_OPTMGMT_REQ to ip asking for all data in the various 15220Sstevel@tonic-gate * tables defined by mib2.h. Parse the returned data and extract 15230Sstevel@tonic-gate * the 'routing' information table. Process the 'routing' table 15240Sstevel@tonic-gate * to get the list of known onlink routers, and update our database. 15250Sstevel@tonic-gate * These onlink routers will serve as our probe targets. 15260Sstevel@tonic-gate * Returns false, if any system calls resulted in errors, true otherwise. 15270Sstevel@tonic-gate */ 15280Sstevel@tonic-gate static boolean_t 15290Sstevel@tonic-gate update_router_list(int fd) 15300Sstevel@tonic-gate { 15310Sstevel@tonic-gate union { 15320Sstevel@tonic-gate char ubuf[1024]; 15330Sstevel@tonic-gate union T_primitives uprim; 15340Sstevel@tonic-gate } buf; 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate int flags; 15370Sstevel@tonic-gate struct strbuf ctlbuf; 15380Sstevel@tonic-gate struct strbuf databuf; 15390Sstevel@tonic-gate struct T_optmgmt_req *tor; 15400Sstevel@tonic-gate struct T_optmgmt_ack *toa; 15410Sstevel@tonic-gate struct T_error_ack *tea; 15420Sstevel@tonic-gate struct opthdr *optp; 15430Sstevel@tonic-gate struct opthdr *req; 15440Sstevel@tonic-gate int status; 15450Sstevel@tonic-gate t_scalar_t prim; 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate tor = (struct T_optmgmt_req *)&buf; 15480Sstevel@tonic-gate tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 15490Sstevel@tonic-gate tor->OPT_offset = sizeof (struct T_optmgmt_req); 15500Sstevel@tonic-gate tor->OPT_length = sizeof (struct opthdr); 15510Sstevel@tonic-gate tor->MGMT_flags = T_CURRENT; 15520Sstevel@tonic-gate 1553*8485SPeter.Memishian@Sun.COM /* 1554*8485SPeter.Memishian@Sun.COM * Note: we use the special level value below so that IP will return 1555*8485SPeter.Memishian@Sun.COM * us information concerning IRE_MARK_TESTHIDDEN routes. 1556*8485SPeter.Memishian@Sun.COM */ 15570Sstevel@tonic-gate req = (struct opthdr *)&tor[1]; 1558*8485SPeter.Memishian@Sun.COM req->level = EXPER_IP_AND_TESTHIDDEN; 15590Sstevel@tonic-gate req->name = 0; 15600Sstevel@tonic-gate req->len = 0; 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate ctlbuf.buf = (char *)&buf; 15630Sstevel@tonic-gate ctlbuf.len = tor->OPT_length + tor->OPT_offset; 15640Sstevel@tonic-gate ctlbuf.maxlen = sizeof (buf); 1565*8485SPeter.Memishian@Sun.COM if (putmsg(fd, &ctlbuf, NULL, 0) == -1) { 15660Sstevel@tonic-gate logperror("update_router_list: putmsg(ctl)"); 15670Sstevel@tonic-gate return (_B_FALSE); 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate /* 15710Sstevel@tonic-gate * The response consists of multiple T_OPTMGMT_ACK msgs, 1 msg for 15720Sstevel@tonic-gate * each table defined in mib2.h. Each T_OPTMGMT_ACK msg contains 15730Sstevel@tonic-gate * a control and data part. The control part contains a struct 15740Sstevel@tonic-gate * T_optmgmt_ack followed by a struct opthdr. The 'opthdr' identifies 15750Sstevel@tonic-gate * the level, name and length of the data in the data part. The 15760Sstevel@tonic-gate * data part contains the actual table data. The last message 15770Sstevel@tonic-gate * is an end-of-data (EOD), consisting of a T_OPTMGMT_ACK and a 15780Sstevel@tonic-gate * single option with zero optlen. 15790Sstevel@tonic-gate */ 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate for (;;) { 15820Sstevel@tonic-gate /* 15830Sstevel@tonic-gate * Go around this loop once for each table. Ignore 15840Sstevel@tonic-gate * all tables except the routing information table. 15850Sstevel@tonic-gate */ 15860Sstevel@tonic-gate flags = 0; 15870Sstevel@tonic-gate status = getmsg(fd, &ctlbuf, NULL, &flags); 15880Sstevel@tonic-gate if (status < 0) { 15890Sstevel@tonic-gate if (errno == EINTR) 15900Sstevel@tonic-gate continue; 15910Sstevel@tonic-gate logperror("update_router_list: getmsg(ctl)"); 15920Sstevel@tonic-gate return (_B_FALSE); 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate if (ctlbuf.len < sizeof (t_scalar_t)) { 15950Sstevel@tonic-gate logerr("update_router_list: ctlbuf.len %d\n", 15960Sstevel@tonic-gate ctlbuf.len); 15970Sstevel@tonic-gate return (_B_FALSE); 15980Sstevel@tonic-gate } 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate prim = buf.uprim.type; 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate switch (prim) { 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate case T_ERROR_ACK: 16050Sstevel@tonic-gate tea = &buf.uprim.error_ack; 16060Sstevel@tonic-gate if (ctlbuf.len < sizeof (struct T_error_ack)) { 16070Sstevel@tonic-gate logerr("update_router_list: T_ERROR_ACK" 16080Sstevel@tonic-gate " ctlbuf.len %d\n", ctlbuf.len); 16090Sstevel@tonic-gate return (_B_FALSE); 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate logerr("update_router_list: T_ERROR_ACK:" 16120Sstevel@tonic-gate " TLI_error = 0x%lx, UNIX_error = 0x%lx\n", 16130Sstevel@tonic-gate tea->TLI_error, tea->UNIX_error); 16140Sstevel@tonic-gate return (_B_FALSE); 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate case T_OPTMGMT_ACK: 16170Sstevel@tonic-gate toa = &buf.uprim.optmgmt_ack; 16180Sstevel@tonic-gate optp = (struct opthdr *)&toa[1]; 1619*8485SPeter.Memishian@Sun.COM if (ctlbuf.len < (sizeof (struct T_optmgmt_ack) + 1620*8485SPeter.Memishian@Sun.COM sizeof (struct opthdr))) { 16210Sstevel@tonic-gate logerr("update_router_list: ctlbuf.len %d\n", 16220Sstevel@tonic-gate ctlbuf.len); 16230Sstevel@tonic-gate return (_B_FALSE); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate if (toa->MGMT_flags != T_SUCCESS) { 16260Sstevel@tonic-gate logerr("update_router_list: MGMT_flags 0x%lx\n", 16270Sstevel@tonic-gate toa->MGMT_flags); 16280Sstevel@tonic-gate return (_B_FALSE); 16290Sstevel@tonic-gate } 16300Sstevel@tonic-gate break; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate default: 16330Sstevel@tonic-gate logerr("update_router_list: unknown primitive %ld\n", 16340Sstevel@tonic-gate prim); 16350Sstevel@tonic-gate return (_B_FALSE); 16360Sstevel@tonic-gate } 16370Sstevel@tonic-gate 1638*8485SPeter.Memishian@Sun.COM /* Process the T_OPTMGMT_ACK below */ 16390Sstevel@tonic-gate assert(prim == T_OPTMGMT_ACK); 16400Sstevel@tonic-gate 16410Sstevel@tonic-gate switch (status) { 16420Sstevel@tonic-gate case 0: 16430Sstevel@tonic-gate /* 16440Sstevel@tonic-gate * We have reached the end of this T_OPTMGMT_ACK 16450Sstevel@tonic-gate * message. If this is the last message i.e EOD, 16460Sstevel@tonic-gate * return, else process the next T_OPTMGMT_ACK msg. 16470Sstevel@tonic-gate */ 1648*8485SPeter.Memishian@Sun.COM if (optp->len == 0 && optp->name == 0 && 1649*8485SPeter.Memishian@Sun.COM optp->level == 0) { 16500Sstevel@tonic-gate /* 16510Sstevel@tonic-gate * This is the EOD message. Return 16520Sstevel@tonic-gate */ 16530Sstevel@tonic-gate return (_B_TRUE); 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate continue; 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate case MORECTL: 16580Sstevel@tonic-gate case MORECTL | MOREDATA: 16590Sstevel@tonic-gate /* 16600Sstevel@tonic-gate * This should not happen. We should be able to read 16610Sstevel@tonic-gate * the control portion in a single getmsg. 16620Sstevel@tonic-gate */ 16630Sstevel@tonic-gate logerr("update_router_list: MORECTL\n"); 16640Sstevel@tonic-gate return (_B_FALSE); 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate case MOREDATA: 16670Sstevel@tonic-gate databuf.maxlen = optp->len; 16680Sstevel@tonic-gate /* malloc of 0 bytes is ok */ 16690Sstevel@tonic-gate databuf.buf = malloc((size_t)optp->len); 16700Sstevel@tonic-gate if (databuf.maxlen != 0 && databuf.buf == NULL) { 16710Sstevel@tonic-gate logperror("update_router_list: malloc"); 16720Sstevel@tonic-gate return (_B_FALSE); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate databuf.len = 0; 16750Sstevel@tonic-gate flags = 0; 16760Sstevel@tonic-gate for (;;) { 1677*8485SPeter.Memishian@Sun.COM if (getmsg(fd, NULL, &databuf, &flags) >= 0) 16780Sstevel@tonic-gate break; 1679*8485SPeter.Memishian@Sun.COM if (errno == EINTR) 16800Sstevel@tonic-gate continue; 1681*8485SPeter.Memishian@Sun.COM 1682*8485SPeter.Memishian@Sun.COM logperror("update_router_list: getmsg(data)"); 1683*8485SPeter.Memishian@Sun.COM free(databuf.buf); 1684*8485SPeter.Memishian@Sun.COM return (_B_FALSE); 16850Sstevel@tonic-gate } 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate if (optp->level == MIB2_IP && 16880Sstevel@tonic-gate optp->name == MIB2_IP_ROUTE) { 16890Sstevel@tonic-gate /* LINTED */ 16900Sstevel@tonic-gate ire_process_v4((mib2_ipRouteEntry_t *) 16910Sstevel@tonic-gate databuf.buf, databuf.len); 16920Sstevel@tonic-gate } else if (optp->level == MIB2_IP6 && 16930Sstevel@tonic-gate optp->name == MIB2_IP6_ROUTE) { 16940Sstevel@tonic-gate /* LINTED */ 16950Sstevel@tonic-gate ire_process_v6((mib2_ipv6RouteEntry_t *) 16960Sstevel@tonic-gate databuf.buf, databuf.len); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate free(databuf.buf); 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate /* NOTREACHED */ 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate 1704*8485SPeter.Memishian@Sun.COM 17050Sstevel@tonic-gate /* 1706*8485SPeter.Memishian@Sun.COM * Convert octet `octp' to a phyint name and store in `ifname' 1707*8485SPeter.Memishian@Sun.COM */ 1708*8485SPeter.Memishian@Sun.COM static void 1709*8485SPeter.Memishian@Sun.COM oct2ifname(const Octet_t *octp, char *ifname, size_t ifsize) 1710*8485SPeter.Memishian@Sun.COM { 1711*8485SPeter.Memishian@Sun.COM char *cp; 1712*8485SPeter.Memishian@Sun.COM size_t len = MIN(octp->o_length, ifsize - 1); 1713*8485SPeter.Memishian@Sun.COM 1714*8485SPeter.Memishian@Sun.COM (void) strncpy(ifname, octp->o_bytes, len); 1715*8485SPeter.Memishian@Sun.COM ifname[len] = '\0'; 1716*8485SPeter.Memishian@Sun.COM 1717*8485SPeter.Memishian@Sun.COM if ((cp = strchr(ifname, IF_SEPARATOR)) != NULL) 1718*8485SPeter.Memishian@Sun.COM *cp = '\0'; 1719*8485SPeter.Memishian@Sun.COM } 1720*8485SPeter.Memishian@Sun.COM 1721*8485SPeter.Memishian@Sun.COM /* 1722*8485SPeter.Memishian@Sun.COM * Examine the IPv4 routing table `buf' for possible targets. For each 1723*8485SPeter.Memishian@Sun.COM * possible target, if it's on the same subnet an interface route, pass 1724*8485SPeter.Memishian@Sun.COM * it to router_add_common() for further consideration. 17250Sstevel@tonic-gate */ 17260Sstevel@tonic-gate static void 17270Sstevel@tonic-gate ire_process_v4(mib2_ipRouteEntry_t *buf, size_t len) 17280Sstevel@tonic-gate { 1729*8485SPeter.Memishian@Sun.COM char ifname[LIFNAMSIZ]; 1730*8485SPeter.Memishian@Sun.COM mib2_ipRouteEntry_t *rp, *rp1, *endp; 1731*8485SPeter.Memishian@Sun.COM struct in_addr nexthop_v4; 1732*8485SPeter.Memishian@Sun.COM struct in6_addr nexthop; 17330Sstevel@tonic-gate 17340Sstevel@tonic-gate if (len == 0) 17350Sstevel@tonic-gate return; 17360Sstevel@tonic-gate assert((len % sizeof (mib2_ipRouteEntry_t)) == 0); 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate endp = buf + (len / sizeof (mib2_ipRouteEntry_t)); 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate /* 1741*8485SPeter.Memishian@Sun.COM * Scan the routing table entries for any IRE_OFFSUBNET entries, and 1742*8485SPeter.Memishian@Sun.COM * cross-reference them with the interface routes to determine if 1743*8485SPeter.Memishian@Sun.COM * they're possible probe targets. 17440Sstevel@tonic-gate */ 17450Sstevel@tonic-gate for (rp = buf; rp < endp; rp++) { 17460Sstevel@tonic-gate if (!(rp->ipRouteInfo.re_ire_type & IRE_OFFSUBNET)) 17470Sstevel@tonic-gate continue; 17480Sstevel@tonic-gate 1749*8485SPeter.Memishian@Sun.COM /* Get the nexthop address. */ 17500Sstevel@tonic-gate nexthop_v4.s_addr = rp->ipRouteNextHop; 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate /* 1753*8485SPeter.Memishian@Sun.COM * Rescan the routing table looking for interface routes that 1754*8485SPeter.Memishian@Sun.COM * are on the same subnet, and try to add them. If they're 1755*8485SPeter.Memishian@Sun.COM * not relevant (e.g., the interface route isn't part of an 1756*8485SPeter.Memishian@Sun.COM * IPMP group, router_add_common() will discard). 17570Sstevel@tonic-gate */ 17580Sstevel@tonic-gate for (rp1 = buf; rp1 < endp; rp1++) { 1759*8485SPeter.Memishian@Sun.COM if (!(rp1->ipRouteInfo.re_ire_type & IRE_INTERFACE) || 1760*8485SPeter.Memishian@Sun.COM rp1->ipRouteIfIndex.o_length == 0) 17610Sstevel@tonic-gate continue; 1762*8485SPeter.Memishian@Sun.COM 1763*8485SPeter.Memishian@Sun.COM if ((rp1->ipRouteDest & rp1->ipRouteMask) != 1764*8485SPeter.Memishian@Sun.COM (nexthop_v4.s_addr & rp1->ipRouteMask)) 1765*8485SPeter.Memishian@Sun.COM continue; 1766*8485SPeter.Memishian@Sun.COM 1767*8485SPeter.Memishian@Sun.COM oct2ifname(&rp1->ipRouteIfIndex, ifname, LIFNAMSIZ); 1768*8485SPeter.Memishian@Sun.COM IN6_INADDR_TO_V4MAPPED(&nexthop_v4, &nexthop); 1769*8485SPeter.Memishian@Sun.COM router_add_common(AF_INET, ifname, nexthop); 17700Sstevel@tonic-gate } 17710Sstevel@tonic-gate } 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate void 17750Sstevel@tonic-gate router_add_common(int af, char *ifname, struct in6_addr nexthop) 17760Sstevel@tonic-gate { 17770Sstevel@tonic-gate struct phyint_instance *pii; 17780Sstevel@tonic-gate struct phyint *pi; 17790Sstevel@tonic-gate 17800Sstevel@tonic-gate if (debug & D_TARGET) 17810Sstevel@tonic-gate logdebug("router_add_common(%s %s)\n", AF_STR(af), ifname); 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate /* 17840Sstevel@tonic-gate * Retrieve the phyint instance; bail if it's not known to us yet. 17850Sstevel@tonic-gate */ 17860Sstevel@tonic-gate pii = phyint_inst_lookup(af, ifname); 17870Sstevel@tonic-gate if (pii == NULL) 17880Sstevel@tonic-gate return; 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate /* 17910Sstevel@tonic-gate * Don't use our own addresses as targets. 17920Sstevel@tonic-gate */ 17932250Srk129064 if (own_address(nexthop)) 17940Sstevel@tonic-gate return; 17950Sstevel@tonic-gate 17960Sstevel@tonic-gate /* 17970Sstevel@tonic-gate * If the phyint is part a named group, then add the address to all 17980Sstevel@tonic-gate * members of the group; note that this is suboptimal in the IPv4 case 17990Sstevel@tonic-gate * as it has already been added to all matching interfaces in 18000Sstevel@tonic-gate * ire_process_v4(). Otherwise, add the address only to the phyint 18010Sstevel@tonic-gate * itself, since other phyints in the anongroup may not be on the same 18020Sstevel@tonic-gate * subnet. 18030Sstevel@tonic-gate */ 18040Sstevel@tonic-gate pi = pii->pii_phyint; 18050Sstevel@tonic-gate if (pi->pi_group == phyint_anongroup) { 18060Sstevel@tonic-gate target_add(pii, nexthop, _B_TRUE); 18070Sstevel@tonic-gate } else { 18080Sstevel@tonic-gate pi = pi->pi_group->pg_phyint; 18090Sstevel@tonic-gate for (; pi != NULL; pi = pi->pi_pgnext) 18100Sstevel@tonic-gate target_add(PHYINT_INSTANCE(pi, af), nexthop, _B_TRUE); 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate /* 1815*8485SPeter.Memishian@Sun.COM * Examine the IPv6 routing table `buf' for possible link-local targets, and 1816*8485SPeter.Memishian@Sun.COM * pass any contenders to router_add_common() for further consideration. 18170Sstevel@tonic-gate */ 18180Sstevel@tonic-gate static void 18190Sstevel@tonic-gate ire_process_v6(mib2_ipv6RouteEntry_t *buf, size_t len) 18200Sstevel@tonic-gate { 1821*8485SPeter.Memishian@Sun.COM struct lifreq lifr; 1822*8485SPeter.Memishian@Sun.COM char ifname[LIFNAMSIZ]; 1823*8485SPeter.Memishian@Sun.COM char grname[LIFGRNAMSIZ]; 1824*8485SPeter.Memishian@Sun.COM mib2_ipv6RouteEntry_t *rp, *rp1, *endp; 1825*8485SPeter.Memishian@Sun.COM struct in6_addr nexthop_v6; 18260Sstevel@tonic-gate 18270Sstevel@tonic-gate if (debug & D_TARGET) 18280Sstevel@tonic-gate logdebug("ire_process_v6(len %d)\n", len); 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate if (len == 0) 18310Sstevel@tonic-gate return; 18320Sstevel@tonic-gate 18330Sstevel@tonic-gate assert((len % sizeof (mib2_ipv6RouteEntry_t)) == 0); 18340Sstevel@tonic-gate endp = buf + (len / sizeof (mib2_ipv6RouteEntry_t)); 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate /* 1837*8485SPeter.Memishian@Sun.COM * Scan the routing table entries for any IRE_OFFSUBNET entries, and 1838*8485SPeter.Memishian@Sun.COM * cross-reference them with the interface routes to determine if 1839*8485SPeter.Memishian@Sun.COM * they're possible probe targets. 18400Sstevel@tonic-gate */ 18410Sstevel@tonic-gate for (rp = buf; rp < endp; rp++) { 1842*8485SPeter.Memishian@Sun.COM if (!(rp->ipv6RouteInfo.re_ire_type & IRE_OFFSUBNET) || 1843*8485SPeter.Memishian@Sun.COM !IN6_IS_ADDR_LINKLOCAL(&rp->ipv6RouteNextHop)) 18440Sstevel@tonic-gate continue; 18450Sstevel@tonic-gate 1846*8485SPeter.Memishian@Sun.COM /* Get the nexthop address. */ 1847*8485SPeter.Memishian@Sun.COM nexthop_v6 = rp->ipv6RouteNextHop; 1848*8485SPeter.Memishian@Sun.COM 18490Sstevel@tonic-gate /* 1850*8485SPeter.Memishian@Sun.COM * The interface name should always exist for link-locals; 1851*8485SPeter.Memishian@Sun.COM * we use it to map this entry to an IPMP group name. 18520Sstevel@tonic-gate */ 1853*8485SPeter.Memishian@Sun.COM if (rp->ipv6RouteIfIndex.o_length == 0) 1854*8485SPeter.Memishian@Sun.COM continue; 1855*8485SPeter.Memishian@Sun.COM 1856*8485SPeter.Memishian@Sun.COM oct2ifname(&rp->ipv6RouteIfIndex, lifr.lifr_name, LIFNAMSIZ); 1857*8485SPeter.Memishian@Sun.COM if (ioctl(ifsock_v6, SIOCGLIFGROUPNAME, &lifr) == -1 || 1858*8485SPeter.Memishian@Sun.COM strlcpy(grname, lifr.lifr_groupname, LIFGRNAMSIZ) == 0) { 1859*8485SPeter.Memishian@Sun.COM continue; 1860*8485SPeter.Memishian@Sun.COM } 1861*8485SPeter.Memishian@Sun.COM 1862*8485SPeter.Memishian@Sun.COM /* 1863*8485SPeter.Memishian@Sun.COM * Rescan the list of routes for interface routes, and add the 1864*8485SPeter.Memishian@Sun.COM * above target to any interfaces in the same IPMP group. 1865*8485SPeter.Memishian@Sun.COM */ 1866*8485SPeter.Memishian@Sun.COM for (rp1 = buf; rp1 < endp; rp1++) { 1867*8485SPeter.Memishian@Sun.COM if (!(rp1->ipv6RouteInfo.re_ire_type & IRE_INTERFACE) || 1868*8485SPeter.Memishian@Sun.COM rp1->ipv6RouteIfIndex.o_length == 0) { 1869*8485SPeter.Memishian@Sun.COM continue; 1870*8485SPeter.Memishian@Sun.COM } 1871*8485SPeter.Memishian@Sun.COM oct2ifname(&rp1->ipv6RouteIfIndex, ifname, LIFNAMSIZ); 1872*8485SPeter.Memishian@Sun.COM (void) strlcpy(lifr.lifr_name, ifname, LIFNAMSIZ); 1873*8485SPeter.Memishian@Sun.COM 1874*8485SPeter.Memishian@Sun.COM if (ioctl(ifsock_v6, SIOCGLIFGROUPNAME, &lifr) != -1 && 1875*8485SPeter.Memishian@Sun.COM strcmp(lifr.lifr_groupname, grname) == 0) { 1876*8485SPeter.Memishian@Sun.COM router_add_common(AF_INET6, ifname, nexthop_v6); 1877*8485SPeter.Memishian@Sun.COM } 18780Sstevel@tonic-gate } 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate } 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate /* 18830Sstevel@tonic-gate * Build a list of target routers, by scanning the routing tables. 18840Sstevel@tonic-gate * It is assumed that interface routes exist, to reach the routers. 18850Sstevel@tonic-gate */ 18860Sstevel@tonic-gate static void 18870Sstevel@tonic-gate init_router_targets(void) 18880Sstevel@tonic-gate { 18890Sstevel@tonic-gate struct target *tg; 18900Sstevel@tonic-gate struct target *next_tg; 18910Sstevel@tonic-gate struct phyint_instance *pii; 18920Sstevel@tonic-gate struct phyint *pi; 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate if (force_mcast) 18950Sstevel@tonic-gate return; 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) { 18980Sstevel@tonic-gate pi = pii->pii_phyint; 18990Sstevel@tonic-gate /* 1900*8485SPeter.Memishian@Sun.COM * Set tg_in_use to false only for router targets. 19010Sstevel@tonic-gate */ 1902*8485SPeter.Memishian@Sun.COM if (!pii->pii_targets_are_routers) 19030Sstevel@tonic-gate continue; 19040Sstevel@tonic-gate 19050Sstevel@tonic-gate for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) 19060Sstevel@tonic-gate tg->tg_in_use = 0; 19070Sstevel@tonic-gate } 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate if (mibfd < 0) { 19100Sstevel@tonic-gate mibfd = open("/dev/ip", O_RDWR); 19110Sstevel@tonic-gate if (mibfd < 0) { 19120Sstevel@tonic-gate logperror("mibopen: ip open"); 19130Sstevel@tonic-gate exit(1); 19140Sstevel@tonic-gate } 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate if (!update_router_list(mibfd)) { 19180Sstevel@tonic-gate (void) close(mibfd); 19190Sstevel@tonic-gate mibfd = -1; 19200Sstevel@tonic-gate } 19210Sstevel@tonic-gate 19220Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) { 1923*8485SPeter.Memishian@Sun.COM pi = pii->pii_phyint; 1924*8485SPeter.Memishian@Sun.COM if (!pii->pii_targets_are_routers) 19250Sstevel@tonic-gate continue; 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate for (tg = pii->pii_targets; tg != NULL; tg = next_tg) { 19280Sstevel@tonic-gate next_tg = tg->tg_next; 1929*8485SPeter.Memishian@Sun.COM /* 1930*8485SPeter.Memishian@Sun.COM * If the group has failed, it's likely the route was 1931*8485SPeter.Memishian@Sun.COM * removed by an application affected by that failure. 1932*8485SPeter.Memishian@Sun.COM * In that case, we keep the target so that we can 1933*8485SPeter.Memishian@Sun.COM * reliably repair, at which point we'll refresh the 1934*8485SPeter.Memishian@Sun.COM * target list again. 1935*8485SPeter.Memishian@Sun.COM */ 1936*8485SPeter.Memishian@Sun.COM if (!tg->tg_in_use && !GROUP_FAILED(pi->pi_group)) 19370Sstevel@tonic-gate target_delete(tg); 19380Sstevel@tonic-gate } 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate /* 19430Sstevel@tonic-gate * Attempt to assign host targets to any interfaces that do not currently 19440Sstevel@tonic-gate * have probe targets by sharing targets with other interfaces in the group. 19450Sstevel@tonic-gate */ 19460Sstevel@tonic-gate static void 19470Sstevel@tonic-gate init_host_targets(void) 19480Sstevel@tonic-gate { 19490Sstevel@tonic-gate struct phyint_instance *pii; 19500Sstevel@tonic-gate struct phyint_group *pg; 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) { 19530Sstevel@tonic-gate pg = pii->pii_phyint->pi_group; 19540Sstevel@tonic-gate if (pg != phyint_anongroup && pii->pii_targets == NULL) 19550Sstevel@tonic-gate dup_host_targets(pii); 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate } 19580Sstevel@tonic-gate 19590Sstevel@tonic-gate /* 19600Sstevel@tonic-gate * Duplicate host targets from other phyints of the group to 19610Sstevel@tonic-gate * the phyint instance 'desired_pii'. 19620Sstevel@tonic-gate */ 19630Sstevel@tonic-gate static void 19640Sstevel@tonic-gate dup_host_targets(struct phyint_instance *desired_pii) 19650Sstevel@tonic-gate { 19660Sstevel@tonic-gate int af; 19670Sstevel@tonic-gate struct phyint *pi; 19680Sstevel@tonic-gate struct phyint_instance *pii; 19690Sstevel@tonic-gate struct target *tg; 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate assert(desired_pii->pii_phyint->pi_group != phyint_anongroup); 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate af = desired_pii->pii_af; 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * For every phyint in the same group as desired_pii, check if 19770Sstevel@tonic-gate * it has any host targets. If so add them to desired_pii. 19780Sstevel@tonic-gate */ 19790Sstevel@tonic-gate for (pi = desired_pii->pii_phyint; pi != NULL; pi = pi->pi_pgnext) { 19800Sstevel@tonic-gate pii = PHYINT_INSTANCE(pi, af); 19810Sstevel@tonic-gate /* 19820Sstevel@tonic-gate * We know that we don't have targets on this phyint instance 19830Sstevel@tonic-gate * since we have been called. But we still check for 19840Sstevel@tonic-gate * pii_targets_are_routers because another phyint instance 19850Sstevel@tonic-gate * could have router targets, since IFF_NOFAILOVER addresses 19860Sstevel@tonic-gate * on different phyint instances may belong to different 19870Sstevel@tonic-gate * subnets. 19880Sstevel@tonic-gate */ 19890Sstevel@tonic-gate if ((pii == NULL) || (pii == desired_pii) || 19900Sstevel@tonic-gate pii->pii_targets_are_routers) 19910Sstevel@tonic-gate continue; 19920Sstevel@tonic-gate for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) { 19930Sstevel@tonic-gate target_create(desired_pii, tg->tg_address, _B_FALSE); 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate } 19960Sstevel@tonic-gate } 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate static void 19990Sstevel@tonic-gate usage(char *cmd) 20000Sstevel@tonic-gate { 20010Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s\n", cmd); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate 20050Sstevel@tonic-gate #define MPATHD_DEFAULT_FILE "/etc/default/mpathd" 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate /* Get an option from the /etc/default/mpathd file */ 20080Sstevel@tonic-gate static char * 20090Sstevel@tonic-gate getdefault(char *name) 20100Sstevel@tonic-gate { 20110Sstevel@tonic-gate char namebuf[BUFSIZ]; 20120Sstevel@tonic-gate char *value = NULL; 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate if (defopen(MPATHD_DEFAULT_FILE) == 0) { 20150Sstevel@tonic-gate char *cp; 20160Sstevel@tonic-gate int flags; 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate /* 20190Sstevel@tonic-gate * ignore case 20200Sstevel@tonic-gate */ 20210Sstevel@tonic-gate flags = defcntl(DC_GETFLAGS, 0); 20220Sstevel@tonic-gate TURNOFF(flags, DC_CASE); 20230Sstevel@tonic-gate (void) defcntl(DC_SETFLAGS, flags); 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate /* Add "=" to the name */ 20260Sstevel@tonic-gate (void) strncpy(namebuf, name, sizeof (namebuf) - 2); 20270Sstevel@tonic-gate (void) strncat(namebuf, "=", 2); 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate if ((cp = defread(namebuf)) != NULL) 20300Sstevel@tonic-gate value = strdup(cp); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate /* close */ 20330Sstevel@tonic-gate (void) defopen((char *)NULL); 20340Sstevel@tonic-gate } 20350Sstevel@tonic-gate return (value); 20360Sstevel@tonic-gate } 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate 20390Sstevel@tonic-gate /* 20400Sstevel@tonic-gate * Command line options below 20410Sstevel@tonic-gate */ 20420Sstevel@tonic-gate boolean_t failback_enabled = _B_TRUE; /* failback enabled/disabled */ 2043*8485SPeter.Memishian@Sun.COM boolean_t track_all_phyints = _B_FALSE; /* track all IP interfaces */ 20440Sstevel@tonic-gate static boolean_t adopt = _B_FALSE; 20450Sstevel@tonic-gate static boolean_t foreground = _B_FALSE; 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate int 20480Sstevel@tonic-gate main(int argc, char *argv[]) 20490Sstevel@tonic-gate { 20500Sstevel@tonic-gate int i; 20510Sstevel@tonic-gate int c; 2052*8485SPeter.Memishian@Sun.COM struct phyint *pi; 20530Sstevel@tonic-gate struct phyint_instance *pii; 20540Sstevel@tonic-gate char *value; 20550Sstevel@tonic-gate 20560Sstevel@tonic-gate argv0 = argv; /* Saved for re-exec on SIGHUP */ 20570Sstevel@tonic-gate srandom(gethostid()); /* Initialize the random number generator */ 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate /* 20600Sstevel@tonic-gate * NOTE: The messages output by in.mpathd are not suitable for 20610Sstevel@tonic-gate * translation, so we do not call textdomain(). 20620Sstevel@tonic-gate */ 20630Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate /* 20660Sstevel@tonic-gate * Get the user specified value of 'failure detection time' 20670Sstevel@tonic-gate * from /etc/default/mpathd 20680Sstevel@tonic-gate */ 20690Sstevel@tonic-gate value = getdefault("FAILURE_DETECTION_TIME"); 20700Sstevel@tonic-gate if (value != NULL) { 20710Sstevel@tonic-gate user_failure_detection_time = 20720Sstevel@tonic-gate (int)strtol((char *)value, NULL, 0); 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate if (user_failure_detection_time <= 0) { 20750Sstevel@tonic-gate user_failure_detection_time = FAILURE_DETECTION_TIME; 20760Sstevel@tonic-gate logerr("Invalid failure detection time %s, assuming " 2077*8485SPeter.Memishian@Sun.COM "default of %d ms\n", value, 2078*8485SPeter.Memishian@Sun.COM user_failure_detection_time); 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate } else if (user_failure_detection_time < 20810Sstevel@tonic-gate MIN_FAILURE_DETECTION_TIME) { 20820Sstevel@tonic-gate user_failure_detection_time = 20830Sstevel@tonic-gate MIN_FAILURE_DETECTION_TIME; 20840Sstevel@tonic-gate logerr("Too small failure detection time of %s, " 2085*8485SPeter.Memishian@Sun.COM "assuming minimum of %d ms\n", value, 20860Sstevel@tonic-gate user_failure_detection_time); 20870Sstevel@tonic-gate } 20880Sstevel@tonic-gate free(value); 20890Sstevel@tonic-gate } else { 20900Sstevel@tonic-gate /* User has not specified the parameter, Use default value */ 20910Sstevel@tonic-gate user_failure_detection_time = FAILURE_DETECTION_TIME; 20920Sstevel@tonic-gate } 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate /* 20950Sstevel@tonic-gate * This gives the frequency at which probes will be sent. 20960Sstevel@tonic-gate * When fdt ms elapses, we should be able to determine 20970Sstevel@tonic-gate * whether 5 consecutive probes have failed or not. 20980Sstevel@tonic-gate * 1 probe will be sent in every user_probe_interval ms, 20990Sstevel@tonic-gate * randomly anytime in the (0.5 - 1.0) 2nd half of every 21000Sstevel@tonic-gate * user_probe_interval. Thus when we send out probe 'n' we 21010Sstevel@tonic-gate * can be sure that probe 'n - 2' is lost, if we have not 21020Sstevel@tonic-gate * got the ack. (since the probe interval is > crtt). But 21030Sstevel@tonic-gate * probe 'n - 1' may be a valid unacked probe, since the 21040Sstevel@tonic-gate * time between 2 successive probes could be as small as 21050Sstevel@tonic-gate * 0.5 * user_probe_interval. Hence the NUM_PROBE_FAILS + 2 21060Sstevel@tonic-gate */ 21070Sstevel@tonic-gate user_probe_interval = user_failure_detection_time / 21080Sstevel@tonic-gate (NUM_PROBE_FAILS + 2); 21090Sstevel@tonic-gate 21100Sstevel@tonic-gate /* 21110Sstevel@tonic-gate * Get the user specified value of failback_enabled from 21120Sstevel@tonic-gate * /etc/default/mpathd 21130Sstevel@tonic-gate */ 21140Sstevel@tonic-gate value = getdefault("FAILBACK"); 21150Sstevel@tonic-gate if (value != NULL) { 2116*8485SPeter.Memishian@Sun.COM if (strcasecmp(value, "yes") == 0) 21170Sstevel@tonic-gate failback_enabled = _B_TRUE; 2118*8485SPeter.Memishian@Sun.COM else if (strcasecmp(value, "no") == 0) 21190Sstevel@tonic-gate failback_enabled = _B_FALSE; 21200Sstevel@tonic-gate else 21210Sstevel@tonic-gate logerr("Invalid value for FAILBACK %s\n", value); 21220Sstevel@tonic-gate free(value); 21230Sstevel@tonic-gate } else { 21240Sstevel@tonic-gate failback_enabled = _B_TRUE; 21250Sstevel@tonic-gate } 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate /* 21280Sstevel@tonic-gate * Get the user specified value of track_all_phyints from 21290Sstevel@tonic-gate * /etc/default/mpathd. The sense is reversed in 21300Sstevel@tonic-gate * TRACK_INTERFACES_ONLY_WITH_GROUPS. 21310Sstevel@tonic-gate */ 21320Sstevel@tonic-gate value = getdefault("TRACK_INTERFACES_ONLY_WITH_GROUPS"); 21330Sstevel@tonic-gate if (value != NULL) { 2134*8485SPeter.Memishian@Sun.COM if (strcasecmp(value, "yes") == 0) 21350Sstevel@tonic-gate track_all_phyints = _B_FALSE; 2136*8485SPeter.Memishian@Sun.COM else if (strcasecmp(value, "no") == 0) 21370Sstevel@tonic-gate track_all_phyints = _B_TRUE; 21380Sstevel@tonic-gate else 21390Sstevel@tonic-gate logerr("Invalid value for " 21400Sstevel@tonic-gate "TRACK_INTERFACES_ONLY_WITH_GROUPS %s\n", value); 21410Sstevel@tonic-gate free(value); 21420Sstevel@tonic-gate } else { 21430Sstevel@tonic-gate track_all_phyints = _B_FALSE; 21440Sstevel@tonic-gate } 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate while ((c = getopt(argc, argv, "adD:ml")) != EOF) { 21470Sstevel@tonic-gate switch (c) { 21480Sstevel@tonic-gate case 'a': 21490Sstevel@tonic-gate adopt = _B_TRUE; 21500Sstevel@tonic-gate break; 21510Sstevel@tonic-gate case 'm': 21520Sstevel@tonic-gate force_mcast = _B_TRUE; 21530Sstevel@tonic-gate break; 21540Sstevel@tonic-gate case 'd': 21550Sstevel@tonic-gate debug = D_ALL; 21560Sstevel@tonic-gate foreground = _B_TRUE; 21570Sstevel@tonic-gate break; 21580Sstevel@tonic-gate case 'D': 21590Sstevel@tonic-gate i = (int)strtol(optarg, NULL, 0); 21600Sstevel@tonic-gate if (i == 0) { 21610Sstevel@tonic-gate (void) fprintf(stderr, "Bad debug flags: %s\n", 21620Sstevel@tonic-gate optarg); 21630Sstevel@tonic-gate exit(1); 21640Sstevel@tonic-gate } 21650Sstevel@tonic-gate debug |= i; 21660Sstevel@tonic-gate foreground = _B_TRUE; 21670Sstevel@tonic-gate break; 21680Sstevel@tonic-gate case 'l': 21690Sstevel@tonic-gate /* 21700Sstevel@tonic-gate * Turn off link state notification handling. 21710Sstevel@tonic-gate * Undocumented command line flag, for debugging 21720Sstevel@tonic-gate * purposes. 21730Sstevel@tonic-gate */ 21740Sstevel@tonic-gate handle_link_notifications = _B_FALSE; 21750Sstevel@tonic-gate break; 21760Sstevel@tonic-gate default: 21770Sstevel@tonic-gate usage(argv[0]); 21780Sstevel@tonic-gate exit(1); 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate } 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate /* 21830Sstevel@tonic-gate * The sockets for the loopback command interface should be listening 21840Sstevel@tonic-gate * before we fork and exit in daemonize(). This way, whoever started us 21850Sstevel@tonic-gate * can use the loopback interface as soon as they get a zero exit 21860Sstevel@tonic-gate * status. 21870Sstevel@tonic-gate */ 21880Sstevel@tonic-gate lsock_v4 = setup_listener(AF_INET); 21890Sstevel@tonic-gate lsock_v6 = setup_listener(AF_INET6); 21900Sstevel@tonic-gate 21910Sstevel@tonic-gate if (lsock_v4 < 0 && lsock_v6 < 0) { 21920Sstevel@tonic-gate logerr("main: setup_listener failed for both IPv4 and IPv6\n"); 21930Sstevel@tonic-gate exit(1); 21940Sstevel@tonic-gate } 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate if (!foreground) { 21970Sstevel@tonic-gate if (!daemonize()) { 21980Sstevel@tonic-gate logerr("cannot daemonize\n"); 21990Sstevel@tonic-gate exit(EXIT_FAILURE); 22000Sstevel@tonic-gate } 22010Sstevel@tonic-gate initlog(); 22020Sstevel@tonic-gate } 22030Sstevel@tonic-gate 22040Sstevel@tonic-gate /* 22050Sstevel@tonic-gate * Initializations: 22060Sstevel@tonic-gate * 1. Create ifsock* sockets. These are used for performing SIOC* 22070Sstevel@tonic-gate * ioctls. We have 2 sockets 1 each for IPv4 and IPv6. 22080Sstevel@tonic-gate * 2. Initialize a pipe for handling/recording signal events. 22090Sstevel@tonic-gate * 3. Create the routing sockets, used for listening 22100Sstevel@tonic-gate * to routing / interface changes. 22110Sstevel@tonic-gate * 4. phyint_init() - Initialize physical interface state 22120Sstevel@tonic-gate * (in mpd_tables.c). Must be done before creating interfaces, 22130Sstevel@tonic-gate * which timer_init() does indirectly. 22140Sstevel@tonic-gate * 5. timer_init() - Initialize timer related stuff 22150Sstevel@tonic-gate * 6. initifs() - Initialize our database of all known interfaces 22160Sstevel@tonic-gate * 7. init_router_targets() - Initialize our database of all known 22170Sstevel@tonic-gate * router targets. 22180Sstevel@tonic-gate */ 22190Sstevel@tonic-gate ifsock_v4 = socket(AF_INET, SOCK_DGRAM, 0); 22200Sstevel@tonic-gate if (ifsock_v4 < 0) { 22210Sstevel@tonic-gate logperror("main: IPv4 socket open"); 22220Sstevel@tonic-gate exit(1); 22230Sstevel@tonic-gate } 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate ifsock_v6 = socket(AF_INET6, SOCK_DGRAM, 0); 22260Sstevel@tonic-gate if (ifsock_v6 < 0) { 22270Sstevel@tonic-gate logperror("main: IPv6 socket open"); 22280Sstevel@tonic-gate exit(1); 22290Sstevel@tonic-gate } 22300Sstevel@tonic-gate 22310Sstevel@tonic-gate setup_eventpipe(); 22320Sstevel@tonic-gate 22330Sstevel@tonic-gate rtsock_v4 = setup_rtsock(AF_INET); 22340Sstevel@tonic-gate rtsock_v6 = setup_rtsock(AF_INET6); 22350Sstevel@tonic-gate 22360Sstevel@tonic-gate if (phyint_init() == -1) { 22370Sstevel@tonic-gate logerr("cannot initialize physical interface structures"); 22380Sstevel@tonic-gate exit(1); 22390Sstevel@tonic-gate } 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate timer_init(); 22420Sstevel@tonic-gate 22430Sstevel@tonic-gate initifs(); 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate /* 22460Sstevel@tonic-gate * If we're operating in "adopt" mode and no interfaces need to be 22470Sstevel@tonic-gate * tracked, shut down (ifconfig(1M) will restart us on demand if 22480Sstevel@tonic-gate * interfaces are subsequently put into multipathing groups). 22490Sstevel@tonic-gate */ 22500Sstevel@tonic-gate if (adopt && phyint_instances == NULL) 22510Sstevel@tonic-gate exit(0); 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate /* 22540Sstevel@tonic-gate * Main body. Keep listening for activity on any of the sockets 22550Sstevel@tonic-gate * that we are monitoring and take appropriate action as necessary. 22560Sstevel@tonic-gate * signals are also handled synchronously. 22570Sstevel@tonic-gate */ 22580Sstevel@tonic-gate for (;;) { 22590Sstevel@tonic-gate if (poll(pollfds, pollfd_num, -1) < 0) { 22600Sstevel@tonic-gate if (errno == EINTR) 22610Sstevel@tonic-gate continue; 22620Sstevel@tonic-gate logperror("main: poll"); 22630Sstevel@tonic-gate exit(1); 22640Sstevel@tonic-gate } 22650Sstevel@tonic-gate for (i = 0; i < pollfd_num; i++) { 22660Sstevel@tonic-gate if ((pollfds[i].fd == -1) || 22670Sstevel@tonic-gate !(pollfds[i].revents & POLLIN)) 22680Sstevel@tonic-gate continue; 22690Sstevel@tonic-gate if (pollfds[i].fd == eventpipe_read) { 22700Sstevel@tonic-gate in_signal(eventpipe_read); 22710Sstevel@tonic-gate break; 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate if (pollfds[i].fd == rtsock_v4 || 22742496Smeem pollfds[i].fd == rtsock_v6) { 22750Sstevel@tonic-gate process_rtsock(rtsock_v4, rtsock_v6); 22760Sstevel@tonic-gate break; 22770Sstevel@tonic-gate } 2278*8485SPeter.Memishian@Sun.COM 22790Sstevel@tonic-gate for (pii = phyint_instances; pii != NULL; 22800Sstevel@tonic-gate pii = pii->pii_next) { 22810Sstevel@tonic-gate if (pollfds[i].fd == pii->pii_probe_sock) { 22820Sstevel@tonic-gate if (pii->pii_af == AF_INET) 22830Sstevel@tonic-gate in_data(pii); 22840Sstevel@tonic-gate else 22850Sstevel@tonic-gate in6_data(pii); 22860Sstevel@tonic-gate break; 22870Sstevel@tonic-gate } 22880Sstevel@tonic-gate } 2289*8485SPeter.Memishian@Sun.COM 2290*8485SPeter.Memishian@Sun.COM for (pi = phyints; pi != NULL; pi = pi->pi_next) { 2291*8485SPeter.Memishian@Sun.COM if (pi->pi_notes != 0 && 2292*8485SPeter.Memishian@Sun.COM pollfds[i].fd == dlpi_fd(pi->pi_dh)) { 2293*8485SPeter.Memishian@Sun.COM (void) dlpi_recv(pi->pi_dh, NULL, NULL, 2294*8485SPeter.Memishian@Sun.COM NULL, NULL, 0, NULL); 2295*8485SPeter.Memishian@Sun.COM break; 2296*8485SPeter.Memishian@Sun.COM } 2297*8485SPeter.Memishian@Sun.COM } 2298*8485SPeter.Memishian@Sun.COM 22990Sstevel@tonic-gate if (pollfds[i].fd == lsock_v4) 23000Sstevel@tonic-gate loopback_cmd(lsock_v4, AF_INET); 23010Sstevel@tonic-gate else if (pollfds[i].fd == lsock_v6) 23020Sstevel@tonic-gate loopback_cmd(lsock_v6, AF_INET6); 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate /* NOTREACHED */ 23060Sstevel@tonic-gate return (EXIT_SUCCESS); 23070Sstevel@tonic-gate } 23080Sstevel@tonic-gate 23090Sstevel@tonic-gate static int 23100Sstevel@tonic-gate setup_listener(int af) 23110Sstevel@tonic-gate { 23120Sstevel@tonic-gate int sock; 23130Sstevel@tonic-gate int on; 23140Sstevel@tonic-gate int len; 23150Sstevel@tonic-gate int ret; 23160Sstevel@tonic-gate struct sockaddr_storage laddr; 23170Sstevel@tonic-gate struct sockaddr_in *sin; 23180Sstevel@tonic-gate struct sockaddr_in6 *sin6; 23190Sstevel@tonic-gate struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT; 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate assert(af == AF_INET || af == AF_INET6); 23220Sstevel@tonic-gate 23230Sstevel@tonic-gate sock = socket(af, SOCK_STREAM, 0); 23240Sstevel@tonic-gate if (sock < 0) { 23250Sstevel@tonic-gate logperror("setup_listener: socket"); 23260Sstevel@tonic-gate exit(1); 23270Sstevel@tonic-gate } 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate on = 1; 23300Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, 23310Sstevel@tonic-gate sizeof (on)) < 0) { 23320Sstevel@tonic-gate logperror("setup_listener: setsockopt (SO_REUSEADDR)"); 23330Sstevel@tonic-gate exit(1); 23340Sstevel@tonic-gate } 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate bzero(&laddr, sizeof (laddr)); 23370Sstevel@tonic-gate laddr.ss_family = af; 23380Sstevel@tonic-gate 23390Sstevel@tonic-gate if (af == AF_INET) { 23400Sstevel@tonic-gate sin = (struct sockaddr_in *)&laddr; 23410Sstevel@tonic-gate sin->sin_port = htons(MPATHD_PORT); 23420Sstevel@tonic-gate sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 23430Sstevel@tonic-gate len = sizeof (struct sockaddr_in); 23440Sstevel@tonic-gate } else { 23450Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&laddr; 23460Sstevel@tonic-gate sin6->sin6_port = htons(MPATHD_PORT); 23470Sstevel@tonic-gate sin6->sin6_addr = loopback_addr; 23480Sstevel@tonic-gate len = sizeof (struct sockaddr_in6); 23490Sstevel@tonic-gate } 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate ret = bind(sock, (struct sockaddr *)&laddr, len); 23520Sstevel@tonic-gate if (ret < 0) { 23530Sstevel@tonic-gate if (errno == EADDRINUSE) { 23540Sstevel@tonic-gate /* 23550Sstevel@tonic-gate * Another instance of mpathd may be already active. 23560Sstevel@tonic-gate */ 23570Sstevel@tonic-gate logerr("main: is another instance of in.mpathd " 23580Sstevel@tonic-gate "already active?\n"); 23590Sstevel@tonic-gate exit(1); 23600Sstevel@tonic-gate } else { 23610Sstevel@tonic-gate (void) close(sock); 23620Sstevel@tonic-gate return (-1); 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate } 23650Sstevel@tonic-gate if (listen(sock, 30) < 0) { 23660Sstevel@tonic-gate logperror("main: listen"); 23670Sstevel@tonic-gate exit(1); 23680Sstevel@tonic-gate } 23690Sstevel@tonic-gate if (poll_add(sock) == -1) { 23700Sstevel@tonic-gate (void) close(sock); 23710Sstevel@tonic-gate exit(1); 23720Sstevel@tonic-gate } 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate return (sock); 23750Sstevel@tonic-gate } 23760Sstevel@tonic-gate 23770Sstevel@tonic-gate /* 23780Sstevel@tonic-gate * Table of commands and their expected size; used by loopback_cmd(). 23790Sstevel@tonic-gate */ 23800Sstevel@tonic-gate static struct { 23810Sstevel@tonic-gate const char *name; 23820Sstevel@tonic-gate unsigned int size; 23830Sstevel@tonic-gate } commands[] = { 23840Sstevel@tonic-gate { "MI_PING", sizeof (uint32_t) }, 23850Sstevel@tonic-gate { "MI_OFFLINE", sizeof (mi_offline_t) }, 23860Sstevel@tonic-gate { "MI_UNDO_OFFLINE", sizeof (mi_undo_offline_t) }, 23870Sstevel@tonic-gate { "MI_QUERY", sizeof (mi_query_t) } 23880Sstevel@tonic-gate }; 23890Sstevel@tonic-gate 23900Sstevel@tonic-gate /* 2391*8485SPeter.Memishian@Sun.COM * Commands received over the loopback interface come here (via libipmp). 23920Sstevel@tonic-gate */ 23930Sstevel@tonic-gate static void 23940Sstevel@tonic-gate loopback_cmd(int sock, int family) 23950Sstevel@tonic-gate { 23960Sstevel@tonic-gate int newfd; 23970Sstevel@tonic-gate ssize_t len; 2398*8485SPeter.Memishian@Sun.COM boolean_t is_priv = _B_FALSE; 23990Sstevel@tonic-gate struct sockaddr_storage peer; 24000Sstevel@tonic-gate struct sockaddr_in *peer_sin; 24010Sstevel@tonic-gate struct sockaddr_in6 *peer_sin6; 24020Sstevel@tonic-gate socklen_t peerlen; 24030Sstevel@tonic-gate union mi_commands mpi; 24040Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 24050Sstevel@tonic-gate uint_t cmd; 24060Sstevel@tonic-gate int retval; 24070Sstevel@tonic-gate 24080Sstevel@tonic-gate peerlen = sizeof (peer); 24090Sstevel@tonic-gate newfd = accept(sock, (struct sockaddr *)&peer, &peerlen); 24100Sstevel@tonic-gate if (newfd < 0) { 24110Sstevel@tonic-gate logperror("loopback_cmd: accept"); 24120Sstevel@tonic-gate return; 24130Sstevel@tonic-gate } 24140Sstevel@tonic-gate 24150Sstevel@tonic-gate switch (family) { 24160Sstevel@tonic-gate case AF_INET: 24170Sstevel@tonic-gate /* 24180Sstevel@tonic-gate * Validate the address and port to make sure that 24190Sstevel@tonic-gate * non privileged processes don't connect and start 24200Sstevel@tonic-gate * talking to us. 24210Sstevel@tonic-gate */ 24220Sstevel@tonic-gate if (peerlen != sizeof (struct sockaddr_in)) { 24230Sstevel@tonic-gate logerr("loopback_cmd: AF_INET peerlen %d\n", peerlen); 24240Sstevel@tonic-gate (void) close(newfd); 24250Sstevel@tonic-gate return; 24260Sstevel@tonic-gate } 24270Sstevel@tonic-gate peer_sin = (struct sockaddr_in *)&peer; 2428*8485SPeter.Memishian@Sun.COM is_priv = ntohs(peer_sin->sin_port) < IPPORT_RESERVED; 2429*8485SPeter.Memishian@Sun.COM (void) inet_ntop(AF_INET, &peer_sin->sin_addr.s_addr, 2430*8485SPeter.Memishian@Sun.COM abuf, sizeof (abuf)); 2431*8485SPeter.Memishian@Sun.COM 2432*8485SPeter.Memishian@Sun.COM if (ntohl(peer_sin->sin_addr.s_addr) != INADDR_LOOPBACK) { 24330Sstevel@tonic-gate logerr("Attempt to connect from addr %s port %d\n", 24340Sstevel@tonic-gate abuf, ntohs(peer_sin->sin_port)); 24350Sstevel@tonic-gate (void) close(newfd); 24360Sstevel@tonic-gate return; 24370Sstevel@tonic-gate } 24380Sstevel@tonic-gate break; 24390Sstevel@tonic-gate 24400Sstevel@tonic-gate case AF_INET6: 24410Sstevel@tonic-gate if (peerlen != sizeof (struct sockaddr_in6)) { 24420Sstevel@tonic-gate logerr("loopback_cmd: AF_INET6 peerlen %d\n", peerlen); 24430Sstevel@tonic-gate (void) close(newfd); 24440Sstevel@tonic-gate return; 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate /* 24470Sstevel@tonic-gate * Validate the address and port to make sure that 24480Sstevel@tonic-gate * non privileged processes don't connect and start 24490Sstevel@tonic-gate * talking to us. 24500Sstevel@tonic-gate */ 24510Sstevel@tonic-gate peer_sin6 = (struct sockaddr_in6 *)&peer; 2452*8485SPeter.Memishian@Sun.COM is_priv = ntohs(peer_sin6->sin6_port) < IPPORT_RESERVED; 2453*8485SPeter.Memishian@Sun.COM (void) inet_ntop(AF_INET6, &peer_sin6->sin6_addr, abuf, 2454*8485SPeter.Memishian@Sun.COM sizeof (abuf)); 2455*8485SPeter.Memishian@Sun.COM if (!IN6_IS_ADDR_LOOPBACK(&peer_sin6->sin6_addr)) { 24560Sstevel@tonic-gate logerr("Attempt to connect from addr %s port %d\n", 24570Sstevel@tonic-gate abuf, ntohs(peer_sin6->sin6_port)); 24580Sstevel@tonic-gate (void) close(newfd); 24590Sstevel@tonic-gate return; 24600Sstevel@tonic-gate } 24610Sstevel@tonic-gate 24620Sstevel@tonic-gate default: 24630Sstevel@tonic-gate logdebug("loopback_cmd: family %d\n", family); 24640Sstevel@tonic-gate (void) close(newfd); 24650Sstevel@tonic-gate return; 24660Sstevel@tonic-gate } 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate /* 24690Sstevel@tonic-gate * The sizeof the 'mpi' buffer corresponds to the maximum size of 24700Sstevel@tonic-gate * all supported commands 24710Sstevel@tonic-gate */ 24720Sstevel@tonic-gate len = read(newfd, &mpi, sizeof (mpi)); 24730Sstevel@tonic-gate 24740Sstevel@tonic-gate /* 24750Sstevel@tonic-gate * In theory, we can receive any sized message for a stream socket, 24760Sstevel@tonic-gate * but we don't expect that to happen for a small message over a 24770Sstevel@tonic-gate * loopback connection. 24780Sstevel@tonic-gate */ 24790Sstevel@tonic-gate if (len < sizeof (uint32_t)) { 24800Sstevel@tonic-gate logerr("loopback_cmd: bad command format or read returns " 24810Sstevel@tonic-gate "partial data %d\n", len); 2482*8485SPeter.Memishian@Sun.COM (void) close(newfd); 2483*8485SPeter.Memishian@Sun.COM return; 24840Sstevel@tonic-gate } 24850Sstevel@tonic-gate 24860Sstevel@tonic-gate cmd = mpi.mi_command; 24870Sstevel@tonic-gate if (cmd >= MI_NCMD) { 24880Sstevel@tonic-gate logerr("loopback_cmd: unknown command id `%d'\n", cmd); 24890Sstevel@tonic-gate (void) close(newfd); 24900Sstevel@tonic-gate return; 24910Sstevel@tonic-gate } 24920Sstevel@tonic-gate 2493*8485SPeter.Memishian@Sun.COM /* 2494*8485SPeter.Memishian@Sun.COM * Only MI_PING and MI_QUERY can come from unprivileged sources. 2495*8485SPeter.Memishian@Sun.COM */ 2496*8485SPeter.Memishian@Sun.COM if (!is_priv && (cmd != MI_QUERY && cmd != MI_PING)) { 2497*8485SPeter.Memishian@Sun.COM logerr("Unprivileged request from %s for privileged " 2498*8485SPeter.Memishian@Sun.COM "command %s\n", abuf, commands[cmd].name); 2499*8485SPeter.Memishian@Sun.COM (void) close(newfd); 2500*8485SPeter.Memishian@Sun.COM return; 2501*8485SPeter.Memishian@Sun.COM } 2502*8485SPeter.Memishian@Sun.COM 25030Sstevel@tonic-gate if (len < commands[cmd].size) { 25040Sstevel@tonic-gate logerr("loopback_cmd: short %s command (expected %d, got %d)\n", 25050Sstevel@tonic-gate commands[cmd].name, commands[cmd].size, len); 25060Sstevel@tonic-gate (void) close(newfd); 25070Sstevel@tonic-gate return; 25080Sstevel@tonic-gate } 25090Sstevel@tonic-gate 25100Sstevel@tonic-gate retval = process_cmd(newfd, &mpi); 25110Sstevel@tonic-gate if (retval != IPMP_SUCCESS) { 25120Sstevel@tonic-gate logerr("failed processing %s: %s\n", commands[cmd].name, 25130Sstevel@tonic-gate ipmp_errmsg(retval)); 25140Sstevel@tonic-gate } 25150Sstevel@tonic-gate (void) close(newfd); 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate 25180Sstevel@tonic-gate /* 2519*8485SPeter.Memishian@Sun.COM * Process the commands received via libipmp. 25200Sstevel@tonic-gate */ 25210Sstevel@tonic-gate static unsigned int 25220Sstevel@tonic-gate process_cmd(int newfd, union mi_commands *mpi) 25230Sstevel@tonic-gate { 25240Sstevel@tonic-gate struct phyint *pi; 25250Sstevel@tonic-gate struct mi_offline *mio; 25260Sstevel@tonic-gate struct mi_undo_offline *miu; 2527*8485SPeter.Memishian@Sun.COM unsigned int retval; 2528*8485SPeter.Memishian@Sun.COM 2529*8485SPeter.Memishian@Sun.COM switch (mpi->mi_command) { 2530*8485SPeter.Memishian@Sun.COM case MI_PING: 2531*8485SPeter.Memishian@Sun.COM return (send_result(newfd, IPMP_SUCCESS, 0)); 2532*8485SPeter.Memishian@Sun.COM 25330Sstevel@tonic-gate case MI_OFFLINE: 25340Sstevel@tonic-gate mio = &mpi->mi_ocmd; 2535*8485SPeter.Memishian@Sun.COM 25360Sstevel@tonic-gate pi = phyint_lookup(mio->mio_ifname); 25370Sstevel@tonic-gate if (pi == NULL) 2538*8485SPeter.Memishian@Sun.COM return (send_result(newfd, IPMP_EUNKIF, 0)); 2539*8485SPeter.Memishian@Sun.COM 2540*8485SPeter.Memishian@Sun.COM retval = phyint_offline(pi, mio->mio_min_redundancy); 2541*8485SPeter.Memishian@Sun.COM if (retval == IPMP_FAILURE) 25420Sstevel@tonic-gate return (send_result(newfd, IPMP_FAILURE, errno)); 2543*8485SPeter.Memishian@Sun.COM 2544*8485SPeter.Memishian@Sun.COM return (send_result(newfd, retval, 0)); 25450Sstevel@tonic-gate 25460Sstevel@tonic-gate case MI_UNDO_OFFLINE: 25470Sstevel@tonic-gate miu = &mpi->mi_ucmd; 2548*8485SPeter.Memishian@Sun.COM 25490Sstevel@tonic-gate pi = phyint_lookup(miu->miu_ifname); 2550*8485SPeter.Memishian@Sun.COM if (pi == NULL) 2551*8485SPeter.Memishian@Sun.COM return (send_result(newfd, IPMP_EUNKIF, 0)); 2552*8485SPeter.Memishian@Sun.COM 2553*8485SPeter.Memishian@Sun.COM retval = phyint_undo_offline(pi); 2554*8485SPeter.Memishian@Sun.COM if (retval == IPMP_FAILURE) 25550Sstevel@tonic-gate return (send_result(newfd, IPMP_FAILURE, errno)); 25560Sstevel@tonic-gate 2557*8485SPeter.Memishian@Sun.COM return (send_result(newfd, retval, 0)); 25580Sstevel@tonic-gate 25590Sstevel@tonic-gate case MI_QUERY: 25600Sstevel@tonic-gate return (process_query(newfd, &mpi->mi_qcmd)); 25610Sstevel@tonic-gate 25620Sstevel@tonic-gate default: 25630Sstevel@tonic-gate break; 25640Sstevel@tonic-gate } 25650Sstevel@tonic-gate 25660Sstevel@tonic-gate return (send_result(newfd, IPMP_EPROTO, 0)); 25670Sstevel@tonic-gate } 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate /* 25700Sstevel@tonic-gate * Process the query request pointed to by `miq' and send a reply on file 25710Sstevel@tonic-gate * descriptor `fd'. Returns an IPMP error code. 25720Sstevel@tonic-gate */ 25730Sstevel@tonic-gate static unsigned int 25740Sstevel@tonic-gate process_query(int fd, mi_query_t *miq) 25750Sstevel@tonic-gate { 2576*8485SPeter.Memishian@Sun.COM ipmp_addrinfo_t *adinfop; 2577*8485SPeter.Memishian@Sun.COM ipmp_addrinfolist_t *adlp; 25780Sstevel@tonic-gate ipmp_groupinfo_t *grinfop; 25790Sstevel@tonic-gate ipmp_groupinfolist_t *grlp; 25800Sstevel@tonic-gate ipmp_grouplist_t *grlistp; 25810Sstevel@tonic-gate ipmp_ifinfo_t *ifinfop; 25820Sstevel@tonic-gate ipmp_ifinfolist_t *iflp; 25830Sstevel@tonic-gate ipmp_snap_t *snap; 25840Sstevel@tonic-gate unsigned int retval; 25850Sstevel@tonic-gate 25860Sstevel@tonic-gate switch (miq->miq_inforeq) { 2587*8485SPeter.Memishian@Sun.COM case IPMP_ADDRINFO: 2588*8485SPeter.Memishian@Sun.COM retval = getgraddrinfo(miq->miq_grname, &miq->miq_addr, 2589*8485SPeter.Memishian@Sun.COM &adinfop); 2590*8485SPeter.Memishian@Sun.COM if (retval != IPMP_SUCCESS) 2591*8485SPeter.Memishian@Sun.COM return (send_result(fd, retval, errno)); 2592*8485SPeter.Memishian@Sun.COM 2593*8485SPeter.Memishian@Sun.COM retval = send_result(fd, IPMP_SUCCESS, 0); 2594*8485SPeter.Memishian@Sun.COM if (retval == IPMP_SUCCESS) 2595*8485SPeter.Memishian@Sun.COM retval = send_addrinfo(fd, adinfop); 2596*8485SPeter.Memishian@Sun.COM 2597*8485SPeter.Memishian@Sun.COM ipmp_freeaddrinfo(adinfop); 2598*8485SPeter.Memishian@Sun.COM return (retval); 2599*8485SPeter.Memishian@Sun.COM 26000Sstevel@tonic-gate case IPMP_GROUPLIST: 26010Sstevel@tonic-gate retval = getgrouplist(&grlistp); 26020Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26030Sstevel@tonic-gate return (send_result(fd, retval, errno)); 26040Sstevel@tonic-gate 26050Sstevel@tonic-gate retval = send_result(fd, IPMP_SUCCESS, 0); 26060Sstevel@tonic-gate if (retval == IPMP_SUCCESS) 26070Sstevel@tonic-gate retval = send_grouplist(fd, grlistp); 26080Sstevel@tonic-gate 26090Sstevel@tonic-gate ipmp_freegrouplist(grlistp); 26100Sstevel@tonic-gate return (retval); 26110Sstevel@tonic-gate 26120Sstevel@tonic-gate case IPMP_GROUPINFO: 26130Sstevel@tonic-gate miq->miq_grname[LIFGRNAMSIZ - 1] = '\0'; 2614*8485SPeter.Memishian@Sun.COM retval = getgroupinfo(miq->miq_grname, &grinfop); 26150Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26160Sstevel@tonic-gate return (send_result(fd, retval, errno)); 26170Sstevel@tonic-gate 26180Sstevel@tonic-gate retval = send_result(fd, IPMP_SUCCESS, 0); 26190Sstevel@tonic-gate if (retval == IPMP_SUCCESS) 26200Sstevel@tonic-gate retval = send_groupinfo(fd, grinfop); 26210Sstevel@tonic-gate 26220Sstevel@tonic-gate ipmp_freegroupinfo(grinfop); 26230Sstevel@tonic-gate return (retval); 26240Sstevel@tonic-gate 26250Sstevel@tonic-gate case IPMP_IFINFO: 26260Sstevel@tonic-gate miq->miq_ifname[LIFNAMSIZ - 1] = '\0'; 26270Sstevel@tonic-gate retval = getifinfo(miq->miq_ifname, &ifinfop); 26280Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26290Sstevel@tonic-gate return (send_result(fd, retval, errno)); 26300Sstevel@tonic-gate 26310Sstevel@tonic-gate retval = send_result(fd, IPMP_SUCCESS, 0); 26320Sstevel@tonic-gate if (retval == IPMP_SUCCESS) 26330Sstevel@tonic-gate retval = send_ifinfo(fd, ifinfop); 26340Sstevel@tonic-gate 26350Sstevel@tonic-gate ipmp_freeifinfo(ifinfop); 26360Sstevel@tonic-gate return (retval); 26370Sstevel@tonic-gate 26380Sstevel@tonic-gate case IPMP_SNAP: 2639*8485SPeter.Memishian@Sun.COM /* 2640*8485SPeter.Memishian@Sun.COM * Before taking the snapshot, sync with the kernel. 2641*8485SPeter.Memishian@Sun.COM */ 2642*8485SPeter.Memishian@Sun.COM initifs(); 2643*8485SPeter.Memishian@Sun.COM 26440Sstevel@tonic-gate retval = getsnap(&snap); 26450Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26460Sstevel@tonic-gate return (send_result(fd, retval, errno)); 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate retval = send_result(fd, IPMP_SUCCESS, 0); 26490Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26500Sstevel@tonic-gate goto out; 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate retval = ipmp_writetlv(fd, IPMP_SNAP, sizeof (*snap), snap); 26530Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26540Sstevel@tonic-gate goto out; 26550Sstevel@tonic-gate 26560Sstevel@tonic-gate retval = send_grouplist(fd, snap->sn_grlistp); 26570Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26580Sstevel@tonic-gate goto out; 26590Sstevel@tonic-gate 26600Sstevel@tonic-gate iflp = snap->sn_ifinfolistp; 26610Sstevel@tonic-gate for (; iflp != NULL; iflp = iflp->ifl_next) { 26620Sstevel@tonic-gate retval = send_ifinfo(fd, iflp->ifl_ifinfop); 26630Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26640Sstevel@tonic-gate goto out; 26650Sstevel@tonic-gate } 26660Sstevel@tonic-gate 26670Sstevel@tonic-gate grlp = snap->sn_grinfolistp; 26680Sstevel@tonic-gate for (; grlp != NULL; grlp = grlp->grl_next) { 26690Sstevel@tonic-gate retval = send_groupinfo(fd, grlp->grl_grinfop); 26700Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 26710Sstevel@tonic-gate goto out; 26720Sstevel@tonic-gate } 2673*8485SPeter.Memishian@Sun.COM 2674*8485SPeter.Memishian@Sun.COM adlp = snap->sn_adinfolistp; 2675*8485SPeter.Memishian@Sun.COM for (; adlp != NULL; adlp = adlp->adl_next) { 2676*8485SPeter.Memishian@Sun.COM retval = send_addrinfo(fd, adlp->adl_adinfop); 2677*8485SPeter.Memishian@Sun.COM if (retval != IPMP_SUCCESS) 2678*8485SPeter.Memishian@Sun.COM goto out; 2679*8485SPeter.Memishian@Sun.COM } 26800Sstevel@tonic-gate out: 26810Sstevel@tonic-gate ipmp_snap_free(snap); 26820Sstevel@tonic-gate return (retval); 26830Sstevel@tonic-gate 26840Sstevel@tonic-gate default: 26850Sstevel@tonic-gate break; 26860Sstevel@tonic-gate 26870Sstevel@tonic-gate } 26880Sstevel@tonic-gate return (send_result(fd, IPMP_EPROTO, 0)); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate /* 26920Sstevel@tonic-gate * Send the group information pointed to by `grinfop' on file descriptor `fd'. 26930Sstevel@tonic-gate * Returns an IPMP error code. 26940Sstevel@tonic-gate */ 26950Sstevel@tonic-gate static unsigned int 26960Sstevel@tonic-gate send_groupinfo(int fd, ipmp_groupinfo_t *grinfop) 26970Sstevel@tonic-gate { 26980Sstevel@tonic-gate ipmp_iflist_t *iflistp = grinfop->gr_iflistp; 2699*8485SPeter.Memishian@Sun.COM ipmp_addrlist_t *adlistp = grinfop->gr_adlistp; 27000Sstevel@tonic-gate unsigned int retval; 27010Sstevel@tonic-gate 27020Sstevel@tonic-gate retval = ipmp_writetlv(fd, IPMP_GROUPINFO, sizeof (*grinfop), grinfop); 27030Sstevel@tonic-gate if (retval != IPMP_SUCCESS) 27040Sstevel@tonic-gate return (retval); 27050Sstevel@tonic-gate 2706*8485SPeter.Memishian@Sun.COM retval = ipmp_writetlv(fd, IPMP_IFLIST, 2707*8485SPeter.Memishian@Sun.COM IPMP_IFLIST_SIZE(iflistp->il_nif), iflistp); 2708*8485SPeter.Memishian@Sun.COM if (retval != IPMP_SUCCESS) 2709*8485SPeter.Memishian@Sun.COM return (retval); 2710*8485SPeter.Memishian@Sun.COM 2711*8485SPeter.Memishian@Sun.COM return (ipmp_writetlv(fd, IPMP_ADDRLIST, 2712*8485SPeter.Memishian@Sun.COM IPMP_ADDRLIST_SIZE(adlistp->al_naddr), adlistp)); 27130Sstevel@tonic-gate } 27140Sstevel@tonic-gate 27150Sstevel@tonic-gate /* 27160Sstevel@tonic-gate * Send the interface information pointed to by `ifinfop' on file descriptor 27170Sstevel@tonic-gate * `fd'. Returns an IPMP error code. 27180Sstevel@tonic-gate */ 27190Sstevel@tonic-gate static unsigned int 27200Sstevel@tonic-gate send_ifinfo(int fd, ipmp_ifinfo_t *ifinfop) 27210Sstevel@tonic-gate { 2722*8485SPeter.Memishian@Sun.COM ipmp_addrlist_t *adlist4p = ifinfop->if_targinfo4.it_targlistp; 2723*8485SPeter.Memishian@Sun.COM ipmp_addrlist_t *adlist6p = ifinfop->if_targinfo6.it_targlistp; 2724*8485SPeter.Memishian@Sun.COM unsigned int retval; 2725*8485SPeter.Memishian@Sun.COM 2726*8485SPeter.Memishian@Sun.COM retval = ipmp_writetlv(fd, IPMP_IFINFO, sizeof (*ifinfop), ifinfop); 2727*8485SPeter.Memishian@Sun.COM if (retval != IPMP_SUCCESS) 2728*8485SPeter.Memishian@Sun.COM return (retval); 2729*8485SPeter.Memishian@Sun.COM 2730*8485SPeter.Memishian@Sun.COM retval = ipmp_writetlv(fd, IPMP_ADDRLIST, 2731*8485SPeter.Memishian@Sun.COM IPMP_ADDRLIST_SIZE(adlist4p->al_naddr), adlist4p); 2732*8485SPeter.Memishian@Sun.COM if (retval != IPMP_SUCCESS) 2733*8485SPeter.Memishian@Sun.COM return (retval); 2734*8485SPeter.Memishian@Sun.COM 2735*8485SPeter.Memishian@Sun.COM return (ipmp_writetlv(fd, IPMP_ADDRLIST, 2736*8485SPeter.Memishian@Sun.COM IPMP_ADDRLIST_SIZE(adlist6p->al_naddr), adlist6p)); 2737*8485SPeter.Memishian@Sun.COM } 2738*8485SPeter.Memishian@Sun.COM 2739*8485SPeter.Memishian@Sun.COM /* 2740*8485SPeter.Memishian@Sun.COM * Send the address information pointed to by `adinfop' on file descriptor 2741*8485SPeter.Memishian@Sun.COM * `fd'. Returns an IPMP error code. 2742*8485SPeter.Memishian@Sun.COM */ 2743*8485SPeter.Memishian@Sun.COM static unsigned int 2744*8485SPeter.Memishian@Sun.COM send_addrinfo(int fd, ipmp_addrinfo_t *adinfop) 2745*8485SPeter.Memishian@Sun.COM { 2746*8485SPeter.Memishian@Sun.COM return (ipmp_writetlv(fd, IPMP_ADDRINFO, sizeof (*adinfop), adinfop)); 27470Sstevel@tonic-gate } 27480Sstevel@tonic-gate 27490Sstevel@tonic-gate /* 27500Sstevel@tonic-gate * Send the group list pointed to by `grlistp' on file descriptor `fd'. 27510Sstevel@tonic-gate * Returns an IPMP error code. 27520Sstevel@tonic-gate */ 27530Sstevel@tonic-gate static unsigned int 27540Sstevel@tonic-gate send_grouplist(int fd, ipmp_grouplist_t *grlistp) 27550Sstevel@tonic-gate { 27560Sstevel@tonic-gate return (ipmp_writetlv(fd, IPMP_GROUPLIST, 27570Sstevel@tonic-gate IPMP_GROUPLIST_SIZE(grlistp->gl_ngroup), grlistp)); 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate 27600Sstevel@tonic-gate /* 27610Sstevel@tonic-gate * Initialize an mi_result_t structure using `error' and `syserror' and 27620Sstevel@tonic-gate * send it on file descriptor `fd'. Returns an IPMP error code. 27630Sstevel@tonic-gate */ 27640Sstevel@tonic-gate static unsigned int 27650Sstevel@tonic-gate send_result(int fd, unsigned int error, int syserror) 27660Sstevel@tonic-gate { 27670Sstevel@tonic-gate mi_result_t me; 27680Sstevel@tonic-gate 27690Sstevel@tonic-gate me.me_mpathd_error = error; 27700Sstevel@tonic-gate if (error == IPMP_FAILURE) 27710Sstevel@tonic-gate me.me_sys_error = syserror; 27720Sstevel@tonic-gate else 27730Sstevel@tonic-gate me.me_sys_error = 0; 27740Sstevel@tonic-gate 27750Sstevel@tonic-gate return (ipmp_write(fd, &me, sizeof (me))); 27760Sstevel@tonic-gate } 27770Sstevel@tonic-gate 27780Sstevel@tonic-gate /* 27790Sstevel@tonic-gate * Daemonize the process. 27800Sstevel@tonic-gate */ 27810Sstevel@tonic-gate static boolean_t 27820Sstevel@tonic-gate daemonize(void) 27830Sstevel@tonic-gate { 27840Sstevel@tonic-gate switch (fork()) { 27850Sstevel@tonic-gate case -1: 27860Sstevel@tonic-gate return (_B_FALSE); 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate case 0: 27890Sstevel@tonic-gate /* 27900Sstevel@tonic-gate * Lose our controlling terminal, and become both a session 27910Sstevel@tonic-gate * leader and a process group leader. 27920Sstevel@tonic-gate */ 27930Sstevel@tonic-gate if (setsid() == -1) 27940Sstevel@tonic-gate return (_B_FALSE); 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate /* 27970Sstevel@tonic-gate * Under POSIX, a session leader can accidentally (through 27980Sstevel@tonic-gate * open(2)) acquire a controlling terminal if it does not 27990Sstevel@tonic-gate * have one. Just to be safe, fork() again so we are not a 28000Sstevel@tonic-gate * session leader. 28010Sstevel@tonic-gate */ 28020Sstevel@tonic-gate switch (fork()) { 28030Sstevel@tonic-gate case -1: 28040Sstevel@tonic-gate return (_B_FALSE); 28050Sstevel@tonic-gate 28060Sstevel@tonic-gate case 0: 28070Sstevel@tonic-gate (void) chdir("/"); 28080Sstevel@tonic-gate (void) umask(022); 28090Sstevel@tonic-gate (void) fdwalk(closefunc, NULL); 28100Sstevel@tonic-gate break; 28110Sstevel@tonic-gate 28120Sstevel@tonic-gate default: 28130Sstevel@tonic-gate _exit(EXIT_SUCCESS); 28140Sstevel@tonic-gate } 28150Sstevel@tonic-gate break; 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate default: 28180Sstevel@tonic-gate _exit(EXIT_SUCCESS); 28190Sstevel@tonic-gate } 28200Sstevel@tonic-gate 28210Sstevel@tonic-gate return (_B_TRUE); 28220Sstevel@tonic-gate } 28230Sstevel@tonic-gate 28240Sstevel@tonic-gate /* 28250Sstevel@tonic-gate * The parent has created some fds before forking on purpose, keep them open. 28260Sstevel@tonic-gate */ 28270Sstevel@tonic-gate static int 28280Sstevel@tonic-gate closefunc(void *not_used, int fd) 28290Sstevel@tonic-gate /* ARGSUSED */ 28300Sstevel@tonic-gate { 28310Sstevel@tonic-gate if (fd != lsock_v4 && fd != lsock_v6) 28320Sstevel@tonic-gate (void) close(fd); 28330Sstevel@tonic-gate return (0); 28340Sstevel@tonic-gate } 28350Sstevel@tonic-gate 28360Sstevel@tonic-gate /* LOGGER */ 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate #include <syslog.h> 28390Sstevel@tonic-gate 28400Sstevel@tonic-gate /* 28410Sstevel@tonic-gate * Logging routines. All routines log to syslog, unless the daemon is 28420Sstevel@tonic-gate * running in the foreground, in which case the logging goes to stderr. 28430Sstevel@tonic-gate * 28440Sstevel@tonic-gate * The following routines are available: 28450Sstevel@tonic-gate * 28460Sstevel@tonic-gate * logdebug(): A printf-like function for outputting debug messages 28470Sstevel@tonic-gate * (messages at LOG_DEBUG) that are only of use to developers. 28480Sstevel@tonic-gate * 28490Sstevel@tonic-gate * logtrace(): A printf-like function for outputting tracing messages 28500Sstevel@tonic-gate * (messages at LOG_INFO) from the daemon. This is typically used 28510Sstevel@tonic-gate * to log the receipt of interesting network-related conditions. 28520Sstevel@tonic-gate * 28530Sstevel@tonic-gate * logerr(): A printf-like function for outputting error messages 28540Sstevel@tonic-gate * (messages at LOG_ERR) from the daemon. 28550Sstevel@tonic-gate * 28560Sstevel@tonic-gate * logperror*(): A set of functions used to output error messages 28570Sstevel@tonic-gate * (messages at LOG_ERR); these automatically append strerror(errno) 28580Sstevel@tonic-gate * and a newline to the message passed to them. 28590Sstevel@tonic-gate * 28600Sstevel@tonic-gate * NOTE: since the logging functions write to syslog, the messages passed 28610Sstevel@tonic-gate * to them are not eligible for localization. Thus, gettext() must 28620Sstevel@tonic-gate * *not* be used. 28630Sstevel@tonic-gate */ 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate static int logging = 0; 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate static void 28680Sstevel@tonic-gate initlog(void) 28690Sstevel@tonic-gate { 28700Sstevel@tonic-gate logging++; 28716578Smeem openlog("in.mpathd", LOG_PID, LOG_DAEMON); 28720Sstevel@tonic-gate } 28730Sstevel@tonic-gate 28746578Smeem /* PRINTFLIKE2 */ 28750Sstevel@tonic-gate void 28766578Smeem logmsg(int pri, const char *fmt, ...) 28770Sstevel@tonic-gate { 28780Sstevel@tonic-gate va_list ap; 28790Sstevel@tonic-gate 28800Sstevel@tonic-gate va_start(ap, fmt); 28810Sstevel@tonic-gate 28820Sstevel@tonic-gate if (logging) 28836578Smeem vsyslog(pri, fmt, ap); 28840Sstevel@tonic-gate else 28850Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 28860Sstevel@tonic-gate va_end(ap); 28870Sstevel@tonic-gate } 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate /* PRINTFLIKE1 */ 28900Sstevel@tonic-gate void 28916578Smeem logperror(const char *str) 28920Sstevel@tonic-gate { 28930Sstevel@tonic-gate if (logging) 28940Sstevel@tonic-gate syslog(LOG_ERR, "%s: %m\n", str); 28950Sstevel@tonic-gate else 28960Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", str, strerror(errno)); 28970Sstevel@tonic-gate } 28980Sstevel@tonic-gate 28990Sstevel@tonic-gate void 29006578Smeem logperror_pii(struct phyint_instance *pii, const char *str) 29010Sstevel@tonic-gate { 29020Sstevel@tonic-gate if (logging) { 29030Sstevel@tonic-gate syslog(LOG_ERR, "%s (%s %s): %m\n", 29040Sstevel@tonic-gate str, AF_STR(pii->pii_af), pii->pii_phyint->pi_name); 29050Sstevel@tonic-gate } else { 29060Sstevel@tonic-gate (void) fprintf(stderr, "%s (%s %s): %s\n", 29070Sstevel@tonic-gate str, AF_STR(pii->pii_af), pii->pii_phyint->pi_name, 29080Sstevel@tonic-gate strerror(errno)); 29090Sstevel@tonic-gate } 29100Sstevel@tonic-gate } 29110Sstevel@tonic-gate 29120Sstevel@tonic-gate void 29136578Smeem logperror_li(struct logint *li, const char *str) 29140Sstevel@tonic-gate { 29150Sstevel@tonic-gate struct phyint_instance *pii = li->li_phyint_inst; 29160Sstevel@tonic-gate 29170Sstevel@tonic-gate if (logging) { 29180Sstevel@tonic-gate syslog(LOG_ERR, "%s (%s %s): %m\n", 29190Sstevel@tonic-gate str, AF_STR(pii->pii_af), li->li_name); 29200Sstevel@tonic-gate } else { 29210Sstevel@tonic-gate (void) fprintf(stderr, "%s (%s %s): %s\n", 29220Sstevel@tonic-gate str, AF_STR(pii->pii_af), li->li_name, 29230Sstevel@tonic-gate strerror(errno)); 29240Sstevel@tonic-gate } 29250Sstevel@tonic-gate } 29260Sstevel@tonic-gate 29270Sstevel@tonic-gate void 29280Sstevel@tonic-gate close_probe_socket(struct phyint_instance *pii, boolean_t polled) 29290Sstevel@tonic-gate { 29300Sstevel@tonic-gate if (polled) 29310Sstevel@tonic-gate (void) poll_remove(pii->pii_probe_sock); 29320Sstevel@tonic-gate (void) close(pii->pii_probe_sock); 29330Sstevel@tonic-gate pii->pii_probe_sock = -1; 29340Sstevel@tonic-gate pii->pii_basetime_inited = 0; 29350Sstevel@tonic-gate } 2936*8485SPeter.Memishian@Sun.COM 2937*8485SPeter.Memishian@Sun.COM boolean_t 2938*8485SPeter.Memishian@Sun.COM addrlist_add(addrlist_t **addrsp, const char *name, uint64_t flags, 2939*8485SPeter.Memishian@Sun.COM struct sockaddr_storage *ssp) 2940*8485SPeter.Memishian@Sun.COM { 2941*8485SPeter.Memishian@Sun.COM addrlist_t *addrp; 2942*8485SPeter.Memishian@Sun.COM 2943*8485SPeter.Memishian@Sun.COM if ((addrp = malloc(sizeof (addrlist_t))) == NULL) 2944*8485SPeter.Memishian@Sun.COM return (_B_FALSE); 2945*8485SPeter.Memishian@Sun.COM 2946*8485SPeter.Memishian@Sun.COM (void) strlcpy(addrp->al_name, name, LIFNAMSIZ); 2947*8485SPeter.Memishian@Sun.COM addrp->al_flags = flags; 2948*8485SPeter.Memishian@Sun.COM addrp->al_addr = *ssp; 2949*8485SPeter.Memishian@Sun.COM addrp->al_next = *addrsp; 2950*8485SPeter.Memishian@Sun.COM *addrsp = addrp; 2951*8485SPeter.Memishian@Sun.COM return (_B_TRUE); 2952*8485SPeter.Memishian@Sun.COM } 2953*8485SPeter.Memishian@Sun.COM 2954*8485SPeter.Memishian@Sun.COM void 2955*8485SPeter.Memishian@Sun.COM addrlist_free(addrlist_t **addrsp) 2956*8485SPeter.Memishian@Sun.COM { 2957*8485SPeter.Memishian@Sun.COM addrlist_t *addrp, *next_addrp; 2958*8485SPeter.Memishian@Sun.COM 2959*8485SPeter.Memishian@Sun.COM for (addrp = *addrsp; addrp != NULL; addrp = next_addrp) { 2960*8485SPeter.Memishian@Sun.COM next_addrp = addrp->al_next; 2961*8485SPeter.Memishian@Sun.COM free(addrp); 2962*8485SPeter.Memishian@Sun.COM } 2963*8485SPeter.Memishian@Sun.COM *addrsp = NULL; 2964*8485SPeter.Memishian@Sun.COM } 2965