17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55e7d5a1cSVallish Vaidyeshwara * Common Development and Distribution License (the "License").
65e7d5a1cSVallish Vaidyeshwara * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate * selfcheck.c
23*6ba597c5SAnurag S. Maskey * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
245e7d5a1cSVallish Vaidyeshwara * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <syslog.h>
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <malloc.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <sys/socket.h>
377c478bd9Sstevel@tonic-gate #include <netdb.h>
387c478bd9Sstevel@tonic-gate #include <net/if.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate int
self_check(char * hostname)415e7d5a1cSVallish Vaidyeshwara self_check(char *hostname)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate int s, res = 0;
447c478bd9Sstevel@tonic-gate struct sioc_addrreq areq;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate struct hostent *hostinfo;
477c478bd9Sstevel@tonic-gate int family;
487c478bd9Sstevel@tonic-gate int flags;
497c478bd9Sstevel@tonic-gate int error_num;
507c478bd9Sstevel@tonic-gate char **hostptr;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate struct sockaddr_in6 ipv6addr;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate family = AF_INET6;
55*6ba597c5SAnurag S. Maskey /*
56*6ba597c5SAnurag S. Maskey * We cannot specify AI_DEFAULT since it includes AI_ADDRCONFIG.
57*6ba597c5SAnurag S. Maskey * Localhost name resolution will fail if no IP interfaces other than
58*6ba597c5SAnurag S. Maskey * loopback are plumbed and AI_ADDRCONFIG is specified, and this
59*6ba597c5SAnurag S. Maskey * causes localhost mounts to fail.
60*6ba597c5SAnurag S. Maskey */
61*6ba597c5SAnurag S. Maskey flags = AI_V4MAPPED;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if ((s = socket(family, SOCK_DGRAM, 0)) < 0) {
647c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "self_check: socket: %m");
657c478bd9Sstevel@tonic-gate return (0);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if ((hostinfo = getipnodebyname(hostname, family, flags,
697c478bd9Sstevel@tonic-gate &error_num)) == NULL) {
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate if (error_num == TRY_AGAIN)
727c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
737c478bd9Sstevel@tonic-gate "self_check: unknown host: %s (try again later)\n",
747c478bd9Sstevel@tonic-gate hostname);
757c478bd9Sstevel@tonic-gate else
767c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG,
777c478bd9Sstevel@tonic-gate "self_check: unknown host: %s\n", hostname);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate (void) close(s);
807c478bd9Sstevel@tonic-gate return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate for (hostptr = hostinfo->h_addr_list; *hostptr; hostptr++) {
847c478bd9Sstevel@tonic-gate bzero(&ipv6addr, sizeof (ipv6addr));
857c478bd9Sstevel@tonic-gate ipv6addr.sin6_family = AF_INET6;
867c478bd9Sstevel@tonic-gate ipv6addr.sin6_addr = *((struct in6_addr *)(*hostptr));
877c478bd9Sstevel@tonic-gate memcpy(&areq.sa_addr, (void *)&ipv6addr, sizeof (ipv6addr));
887c478bd9Sstevel@tonic-gate areq.sa_res = -1;
897c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCTMYADDR, (caddr_t)&areq);
907c478bd9Sstevel@tonic-gate if (areq.sa_res == 1) {
917c478bd9Sstevel@tonic-gate res = 1;
927c478bd9Sstevel@tonic-gate break;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate freehostent(hostinfo);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate (void) close(s);
997c478bd9Sstevel@tonic-gate return (res);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate #define MAXIFS 32
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * create an ifconf structure that represents all the interfaces
1067c478bd9Sstevel@tonic-gate * configured for this host. Two buffers are allcated here:
1077c478bd9Sstevel@tonic-gate * lifc - the ifconf structure returned
1087c478bd9Sstevel@tonic-gate * lifc->lifc_buf - the list of ifreq structures
1097c478bd9Sstevel@tonic-gate * Both of the buffers must be freed by the calling routine.
1107c478bd9Sstevel@tonic-gate * A NULL pointer is returned upon failure. In this case any
1117c478bd9Sstevel@tonic-gate * data that was allocated before the failure has already been
1127c478bd9Sstevel@tonic-gate * freed.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate struct lifconf *
getmyaddrs(void)1155e7d5a1cSVallish Vaidyeshwara getmyaddrs(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate int sock;
1187c478bd9Sstevel@tonic-gate struct lifnum lifn;
1197c478bd9Sstevel@tonic-gate int numifs;
1207c478bd9Sstevel@tonic-gate char *buf;
1217c478bd9Sstevel@tonic-gate struct lifconf *lifc;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1247c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd:getmyaddrs socket: %m");
1257c478bd9Sstevel@tonic-gate return ((struct lifconf *)NULL);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC;
1297c478bd9Sstevel@tonic-gate lifn.lifn_flags = 0;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
1327c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
1337c478bd9Sstevel@tonic-gate "statd:getmyaddrs, get number of interfaces, error: %m");
1347c478bd9Sstevel@tonic-gate numifs = MAXIFS;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate numifs = lifn.lifn_count;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate lifc = (struct lifconf *)malloc(sizeof (struct lifconf));
1407c478bd9Sstevel@tonic-gate if (lifc == NULL) {
1417c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
1427c478bd9Sstevel@tonic-gate "statd:getmyaddrs, malloc for lifconf failed: %m");
1437c478bd9Sstevel@tonic-gate (void) close(sock);
1447c478bd9Sstevel@tonic-gate return ((struct lifconf *)NULL);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate buf = (char *)malloc(numifs * sizeof (struct lifreq));
1477c478bd9Sstevel@tonic-gate if (buf == NULL) {
1487c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
1497c478bd9Sstevel@tonic-gate "statd:getmyaddrs, malloc for lifreq failed: %m");
1507c478bd9Sstevel@tonic-gate (void) close(sock);
1517c478bd9Sstevel@tonic-gate free(lifc);
1527c478bd9Sstevel@tonic-gate return ((struct lifconf *)NULL);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate lifc->lifc_family = AF_UNSPEC;
1567c478bd9Sstevel@tonic-gate lifc->lifc_flags = 0;
1577c478bd9Sstevel@tonic-gate lifc->lifc_buf = buf;
1587c478bd9Sstevel@tonic-gate lifc->lifc_len = numifs * sizeof (struct lifreq);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFCONF, (char *)lifc) < 0) {
1617c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd:getmyaddrs, SIOCGLIFCONF, error: %m");
1627c478bd9Sstevel@tonic-gate (void) close(sock);
1637c478bd9Sstevel@tonic-gate free(buf);
1647c478bd9Sstevel@tonic-gate free(lifc);
1657c478bd9Sstevel@tonic-gate return ((struct lifconf *)NULL);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate (void) close(sock);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate return (lifc);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate int
Is_ipv6present(void)1747c478bd9Sstevel@tonic-gate Is_ipv6present(void)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate int sock;
1777c478bd9Sstevel@tonic-gate struct lifnum lifn;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate sock = socket(AF_INET6, SOCK_DGRAM, 0);
1807c478bd9Sstevel@tonic-gate if (sock < 0)
1817c478bd9Sstevel@tonic-gate return (0);
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_INET6;
1847c478bd9Sstevel@tonic-gate lifn.lifn_flags = 0;
1857c478bd9Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
1867c478bd9Sstevel@tonic-gate close(sock);
1877c478bd9Sstevel@tonic-gate return (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate close(sock);
1907c478bd9Sstevel@tonic-gate if (lifn.lifn_count == 0)
1917c478bd9Sstevel@tonic-gate return (0);
1927c478bd9Sstevel@tonic-gate return (1);
1937c478bd9Sstevel@tonic-gate }
194