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
58651SVallish.Vaidyeshwara@Sun.COM * Common Development and Distribution License (the "License").
68651SVallish.Vaidyeshwara@Sun.COM * 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 /*
228651SVallish.Vaidyeshwara@Sun.COM * selfcheck.c
23*11767SAnurag.Maskey@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
248651SVallish.Vaidyeshwara@Sun.COM * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <errno.h>
280Sstevel@tonic-gate #include <syslog.h>
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <malloc.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <sys/sockio.h>
350Sstevel@tonic-gate #include <netinet/in.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <netdb.h>
380Sstevel@tonic-gate #include <net/if.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate int
self_check(char * hostname)418651SVallish.Vaidyeshwara@Sun.COM self_check(char *hostname)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate int s, res = 0;
440Sstevel@tonic-gate struct sioc_addrreq areq;
450Sstevel@tonic-gate
460Sstevel@tonic-gate struct hostent *hostinfo;
470Sstevel@tonic-gate int family;
480Sstevel@tonic-gate int flags;
490Sstevel@tonic-gate int error_num;
500Sstevel@tonic-gate char **hostptr;
510Sstevel@tonic-gate
520Sstevel@tonic-gate struct sockaddr_in6 ipv6addr;
530Sstevel@tonic-gate
540Sstevel@tonic-gate family = AF_INET6;
55*11767SAnurag.Maskey@Sun.COM /*
56*11767SAnurag.Maskey@Sun.COM * We cannot specify AI_DEFAULT since it includes AI_ADDRCONFIG.
57*11767SAnurag.Maskey@Sun.COM * Localhost name resolution will fail if no IP interfaces other than
58*11767SAnurag.Maskey@Sun.COM * loopback are plumbed and AI_ADDRCONFIG is specified, and this
59*11767SAnurag.Maskey@Sun.COM * causes localhost mounts to fail.
60*11767SAnurag.Maskey@Sun.COM */
61*11767SAnurag.Maskey@Sun.COM flags = AI_V4MAPPED;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if ((s = socket(family, SOCK_DGRAM, 0)) < 0) {
640Sstevel@tonic-gate syslog(LOG_ERR, "self_check: socket: %m");
650Sstevel@tonic-gate return (0);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate if ((hostinfo = getipnodebyname(hostname, family, flags,
690Sstevel@tonic-gate &error_num)) == NULL) {
700Sstevel@tonic-gate
710Sstevel@tonic-gate if (error_num == TRY_AGAIN)
720Sstevel@tonic-gate syslog(LOG_DEBUG,
730Sstevel@tonic-gate "self_check: unknown host: %s (try again later)\n",
740Sstevel@tonic-gate hostname);
750Sstevel@tonic-gate else
760Sstevel@tonic-gate syslog(LOG_DEBUG,
770Sstevel@tonic-gate "self_check: unknown host: %s\n", hostname);
780Sstevel@tonic-gate
790Sstevel@tonic-gate (void) close(s);
800Sstevel@tonic-gate return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate for (hostptr = hostinfo->h_addr_list; *hostptr; hostptr++) {
840Sstevel@tonic-gate bzero(&ipv6addr, sizeof (ipv6addr));
850Sstevel@tonic-gate ipv6addr.sin6_family = AF_INET6;
860Sstevel@tonic-gate ipv6addr.sin6_addr = *((struct in6_addr *)(*hostptr));
870Sstevel@tonic-gate memcpy(&areq.sa_addr, (void *)&ipv6addr, sizeof (ipv6addr));
880Sstevel@tonic-gate areq.sa_res = -1;
890Sstevel@tonic-gate (void) ioctl(s, SIOCTMYADDR, (caddr_t)&areq);
900Sstevel@tonic-gate if (areq.sa_res == 1) {
910Sstevel@tonic-gate res = 1;
920Sstevel@tonic-gate break;
930Sstevel@tonic-gate }
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate freehostent(hostinfo);
970Sstevel@tonic-gate
980Sstevel@tonic-gate (void) close(s);
990Sstevel@tonic-gate return (res);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate #define MAXIFS 32
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * create an ifconf structure that represents all the interfaces
1060Sstevel@tonic-gate * configured for this host. Two buffers are allcated here:
1070Sstevel@tonic-gate * lifc - the ifconf structure returned
1080Sstevel@tonic-gate * lifc->lifc_buf - the list of ifreq structures
1090Sstevel@tonic-gate * Both of the buffers must be freed by the calling routine.
1100Sstevel@tonic-gate * A NULL pointer is returned upon failure. In this case any
1110Sstevel@tonic-gate * data that was allocated before the failure has already been
1120Sstevel@tonic-gate * freed.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate struct lifconf *
getmyaddrs(void)1158651SVallish.Vaidyeshwara@Sun.COM getmyaddrs(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate int sock;
1180Sstevel@tonic-gate struct lifnum lifn;
1190Sstevel@tonic-gate int numifs;
1200Sstevel@tonic-gate char *buf;
1210Sstevel@tonic-gate struct lifconf *lifc;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1240Sstevel@tonic-gate syslog(LOG_ERR, "statd:getmyaddrs socket: %m");
1250Sstevel@tonic-gate return ((struct lifconf *)NULL);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC;
1290Sstevel@tonic-gate lifn.lifn_flags = 0;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
1320Sstevel@tonic-gate syslog(LOG_ERR,
1330Sstevel@tonic-gate "statd:getmyaddrs, get number of interfaces, error: %m");
1340Sstevel@tonic-gate numifs = MAXIFS;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate numifs = lifn.lifn_count;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate lifc = (struct lifconf *)malloc(sizeof (struct lifconf));
1400Sstevel@tonic-gate if (lifc == NULL) {
1410Sstevel@tonic-gate syslog(LOG_ERR,
1428651SVallish.Vaidyeshwara@Sun.COM "statd:getmyaddrs, malloc for lifconf failed: %m");
1430Sstevel@tonic-gate (void) close(sock);
1440Sstevel@tonic-gate return ((struct lifconf *)NULL);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate buf = (char *)malloc(numifs * sizeof (struct lifreq));
1470Sstevel@tonic-gate if (buf == NULL) {
1480Sstevel@tonic-gate syslog(LOG_ERR,
1498651SVallish.Vaidyeshwara@Sun.COM "statd:getmyaddrs, malloc for lifreq failed: %m");
1500Sstevel@tonic-gate (void) close(sock);
1510Sstevel@tonic-gate free(lifc);
1520Sstevel@tonic-gate return ((struct lifconf *)NULL);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate lifc->lifc_family = AF_UNSPEC;
1560Sstevel@tonic-gate lifc->lifc_flags = 0;
1570Sstevel@tonic-gate lifc->lifc_buf = buf;
1580Sstevel@tonic-gate lifc->lifc_len = numifs * sizeof (struct lifreq);
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFCONF, (char *)lifc) < 0) {
1610Sstevel@tonic-gate syslog(LOG_ERR, "statd:getmyaddrs, SIOCGLIFCONF, error: %m");
1620Sstevel@tonic-gate (void) close(sock);
1630Sstevel@tonic-gate free(buf);
1640Sstevel@tonic-gate free(lifc);
1650Sstevel@tonic-gate return ((struct lifconf *)NULL);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate (void) close(sock);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate return (lifc);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate int
Is_ipv6present(void)1740Sstevel@tonic-gate Is_ipv6present(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate int sock;
1770Sstevel@tonic-gate struct lifnum lifn;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate sock = socket(AF_INET6, SOCK_DGRAM, 0);
1800Sstevel@tonic-gate if (sock < 0)
1810Sstevel@tonic-gate return (0);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate lifn.lifn_family = AF_INET6;
1840Sstevel@tonic-gate lifn.lifn_flags = 0;
1850Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
1860Sstevel@tonic-gate close(sock);
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate close(sock);
1900Sstevel@tonic-gate if (lifn.lifn_count == 0)
1910Sstevel@tonic-gate return (0);
1920Sstevel@tonic-gate return (1);
1930Sstevel@tonic-gate }
194