xref: /titanic_50/usr/src/cmd/fs.d/nfs/nfslog/nfslog_ipaddr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1991, 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <malloc.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
36*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
37*7c478bd9Sstevel@tonic-gate #include <net/if.h>
38*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
39*7c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
40*7c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
41*7c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
42*7c478bd9Sstevel@tonic-gate #include <netdb.h>
43*7c478bd9Sstevel@tonic-gate #include <string.h>
44*7c478bd9Sstevel@tonic-gate #include <signal.h>
45*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * Note: If making changes to this file, check also the file
49*7c478bd9Sstevel@tonic-gate  *	 cmd/cmd-inet/usr.sbin/snoop/snoop_ipaddr.c
50*7c478bd9Sstevel@tonic-gate  *	 as it has the same functions there.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate static jmp_buf nisjmp;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #define	MAXHASH 1024  /* must be a power of 2 */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate struct hostdata {
57*7c478bd9Sstevel@tonic-gate 	struct hostdata *h_next;
58*7c478bd9Sstevel@tonic-gate 	char		*h_hostname;
59*7c478bd9Sstevel@tonic-gate 	int		h_pktsout;
60*7c478bd9Sstevel@tonic-gate 	int		h_pktsin;
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate struct hostdata4 {
64*7c478bd9Sstevel@tonic-gate 	struct hostdata4	*h4_next;
65*7c478bd9Sstevel@tonic-gate 	char		*h4_hostname;
66*7c478bd9Sstevel@tonic-gate 	int		h4_pktsout;
67*7c478bd9Sstevel@tonic-gate 	int		h4_pktsin;
68*7c478bd9Sstevel@tonic-gate 	struct in_addr	h4_addr;
69*7c478bd9Sstevel@tonic-gate };
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate struct hostdata6 {
72*7c478bd9Sstevel@tonic-gate 	struct hostdata6	*h6_next;
73*7c478bd9Sstevel@tonic-gate 	char		*h6_hostname;
74*7c478bd9Sstevel@tonic-gate 	int		h6_pktsout;
75*7c478bd9Sstevel@tonic-gate 	int		h6_pktsin;
76*7c478bd9Sstevel@tonic-gate 	struct in6_addr	h6_addr;
77*7c478bd9Sstevel@tonic-gate };
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static struct hostdata *addhost(int, void *, char *);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static struct hostdata4 *h_table4[MAXHASH];
82*7c478bd9Sstevel@tonic-gate static struct hostdata6 *h_table6[MAXHASH];
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate #define	iphash(e)  ((e) & (MAXHASH-1))
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
87*7c478bd9Sstevel@tonic-gate static void
wakeup(int n)88*7c478bd9Sstevel@tonic-gate wakeup(int n)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	longjmp(nisjmp, 1);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate extern char *inet_ntoa();
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static struct hostdata *
iplookup(ipaddr)96*7c478bd9Sstevel@tonic-gate iplookup(ipaddr)
97*7c478bd9Sstevel@tonic-gate 	struct in_addr *ipaddr;
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	register struct hostdata4 *h;
100*7c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
101*7c478bd9Sstevel@tonic-gate 	struct netent *np;
102*7c478bd9Sstevel@tonic-gate 	int error_num;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	for (h = h_table4[iphash(ipaddr->s_addr)]; h; h = h->h4_next) {
105*7c478bd9Sstevel@tonic-gate 		if (h->h4_addr.s_addr == ipaddr->s_addr)
106*7c478bd9Sstevel@tonic-gate 			return ((struct hostdata *)h);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	/* not found.  Put it in */
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (ipaddr->s_addr == htonl(INADDR_BROADCAST))
112*7c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET, ipaddr, "BROADCAST"));
113*7c478bd9Sstevel@tonic-gate 	if (ipaddr->s_addr == htonl(INADDR_ANY))
114*7c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET, ipaddr, "OLD-BROADCAST"));
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
118*7c478bd9Sstevel@tonic-gate 	 * an unresponsive name server.
119*7c478bd9Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
120*7c478bd9Sstevel@tonic-gate 	 */
121*7c478bd9Sstevel@tonic-gate 	if (setjmp(nisjmp) == 0) {
122*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGALRM, wakeup);
123*7c478bd9Sstevel@tonic-gate 		(void) alarm(3);
124*7c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)ipaddr, sizeof (struct in_addr),
125*7c478bd9Sstevel@tonic-gate 		    AF_INET, &error_num);
126*7c478bd9Sstevel@tonic-gate 		if (hp == NULL && inet_lnaof(*ipaddr) == 0) {
127*7c478bd9Sstevel@tonic-gate 			np = getnetbyaddr(inet_netof(*ipaddr), AF_INET);
128*7c478bd9Sstevel@tonic-gate 			if (np)
129*7c478bd9Sstevel@tonic-gate 				return (addhost(AF_INET, ipaddr, np->n_name));
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		(void) alarm(0);
132*7c478bd9Sstevel@tonic-gate 	} else {
133*7c478bd9Sstevel@tonic-gate 		hp = NULL;
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	return (addhost(AF_INET, ipaddr, hp ? hp->h_name : inet_ntoa(*ipaddr)));
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate static struct hostdata *
ip6lookup(ip6addr)140*7c478bd9Sstevel@tonic-gate ip6lookup(ip6addr)
141*7c478bd9Sstevel@tonic-gate 	struct in6_addr *ip6addr;
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	struct hostdata6 *h;
144*7c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
145*7c478bd9Sstevel@tonic-gate 	int error_num;
146*7c478bd9Sstevel@tonic-gate 	char addrstr[INET6_ADDRSTRLEN];
147*7c478bd9Sstevel@tonic-gate 	char *addname;
148*7c478bd9Sstevel@tonic-gate 	struct hostdata *retval;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	for (h = h_table6[iphash(((uint32_t *)ip6addr)[3])]; h;
151*7c478bd9Sstevel@tonic-gate 	    h = h->h6_next) {
152*7c478bd9Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&h->h6_addr, ip6addr))
153*7c478bd9Sstevel@tonic-gate 			return ((struct hostdata *)h);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/* not in the hash table, put it in */
157*7c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(ip6addr))
158*7c478bd9Sstevel@tonic-gate 		return (addhost(AF_INET6, ip6addr, "UNSPECIFIED"));
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	/*
161*7c478bd9Sstevel@tonic-gate 	 * Set an alarm here so we don't get held up by
162*7c478bd9Sstevel@tonic-gate 	 * an unresponsive name server.
163*7c478bd9Sstevel@tonic-gate 	 * Give it 3 sec to do its work.
164*7c478bd9Sstevel@tonic-gate 	 */
165*7c478bd9Sstevel@tonic-gate 	if (setjmp(nisjmp) == 0) {
166*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGALRM, wakeup);
167*7c478bd9Sstevel@tonic-gate 		(void) alarm(3);
168*7c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr(ip6addr, sizeof (struct in6_addr),
169*7c478bd9Sstevel@tonic-gate 		    AF_INET6, &error_num);
170*7c478bd9Sstevel@tonic-gate 		(void) alarm(0);
171*7c478bd9Sstevel@tonic-gate 	} else {
172*7c478bd9Sstevel@tonic-gate 		hp = NULL;
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (hp != NULL)
176*7c478bd9Sstevel@tonic-gate 		addname = hp->h_name;
177*7c478bd9Sstevel@tonic-gate 	else {
178*7c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, ip6addr, addrstr, INET6_ADDRSTRLEN);
179*7c478bd9Sstevel@tonic-gate 		addname = addrstr;
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	retval = addhost(AF_INET6, ip6addr, addname);
183*7c478bd9Sstevel@tonic-gate 	freehostent(hp);
184*7c478bd9Sstevel@tonic-gate 	return (retval);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate static struct hostdata *
addhost(family,ipaddr,name)188*7c478bd9Sstevel@tonic-gate addhost(family, ipaddr, name)
189*7c478bd9Sstevel@tonic-gate 	int family;
190*7c478bd9Sstevel@tonic-gate 	void *ipaddr;
191*7c478bd9Sstevel@tonic-gate 	char *name;
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	register struct hostdata **hp, *n;
194*7c478bd9Sstevel@tonic-gate 	int hashval;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	switch (family) {
197*7c478bd9Sstevel@tonic-gate 	case AF_INET:
198*7c478bd9Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata4));
199*7c478bd9Sstevel@tonic-gate 		if (n == NULL)
200*7c478bd9Sstevel@tonic-gate 			goto alloc_failed;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 		(void) memset(n, 0, sizeof (struct hostdata4));
203*7c478bd9Sstevel@tonic-gate 		n->h_hostname = strdup(name);
204*7c478bd9Sstevel@tonic-gate 		if (n->h_hostname == NULL)
205*7c478bd9Sstevel@tonic-gate 			goto alloc_failed;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 		((struct hostdata4 *)n)->h4_addr = *(struct in_addr *)ipaddr;
208*7c478bd9Sstevel@tonic-gate 		hashval = ((struct in_addr *)ipaddr)->s_addr;
209*7c478bd9Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table4[iphash(hashval)];
210*7c478bd9Sstevel@tonic-gate 		break;
211*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
212*7c478bd9Sstevel@tonic-gate 		n = (struct hostdata *)malloc(sizeof (struct hostdata6));
213*7c478bd9Sstevel@tonic-gate 		if (n == NULL)
214*7c478bd9Sstevel@tonic-gate 			goto alloc_failed;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		(void) memset(n, 0, sizeof (struct hostdata6));
217*7c478bd9Sstevel@tonic-gate 		n->h_hostname = strdup(name);
218*7c478bd9Sstevel@tonic-gate 		if (n->h_hostname == NULL)
219*7c478bd9Sstevel@tonic-gate 			goto alloc_failed;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&((struct hostdata6 *)n)->h6_addr, ipaddr,
222*7c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr));
223*7c478bd9Sstevel@tonic-gate 		hashval = ((int *)ipaddr)[3];
224*7c478bd9Sstevel@tonic-gate 		hp = (struct hostdata **)&h_table6[iphash(hashval)];
225*7c478bd9Sstevel@tonic-gate 		break;
226*7c478bd9Sstevel@tonic-gate 	default:
227*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
228*7c478bd9Sstevel@tonic-gate 			"nfslog: addhost ERROR: Unknown address family: %d",
229*7c478bd9Sstevel@tonic-gate 			family);
230*7c478bd9Sstevel@tonic-gate 		return (NULL);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	n->h_next = *hp;
234*7c478bd9Sstevel@tonic-gate 	*hp = n;
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	return (n);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate alloc_failed:
239*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "addhost: no mem\n");
240*7c478bd9Sstevel@tonic-gate 	if (n != NULL)
241*7c478bd9Sstevel@tonic-gate 		free(n);
242*7c478bd9Sstevel@tonic-gate 	return (NULL);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate char *
addrtoname(void * sockp)246*7c478bd9Sstevel@tonic-gate addrtoname(void *sockp)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	struct hostdata *hostp;
249*7c478bd9Sstevel@tonic-gate 	int family = ((struct sockaddr_in *)sockp)->sin_family;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	switch (family) {
252*7c478bd9Sstevel@tonic-gate 	case AF_INET:
253*7c478bd9Sstevel@tonic-gate 		hostp = iplookup(&((struct sockaddr_in *)sockp)->sin_addr);
254*7c478bd9Sstevel@tonic-gate 		break;
255*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
256*7c478bd9Sstevel@tonic-gate 		hostp = ip6lookup(&((struct sockaddr_in6 *)sockp)->sin6_addr);
257*7c478bd9Sstevel@tonic-gate 		break;
258*7c478bd9Sstevel@tonic-gate 	default:
259*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "nfslog: ERROR: unknown address " \
260*7c478bd9Sstevel@tonic-gate 		    "family: %d\n", family);
261*7c478bd9Sstevel@tonic-gate 		hostp = NULL;
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 	return ((hostp != NULL) ? hostp->h_hostname : NULL);
264*7c478bd9Sstevel@tonic-gate }
265