xref: /freebsd/contrib/tcpdump/print-sunrpc.c (revision ec273ebf3b6aed5fba8c56b6ece5ad8693a48ea7)
1 /*
2  * Copyright (c) 1992, 1993, 1994, 1995, 1996
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  */
23 
24 #define NETDISSECT_REWORKED
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 /*
30  * At least on HP-UX:
31  *
32  *	1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
33  *	   header files
34  *
35  * and
36  *
37  *	2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
38  *	   it
39  *
40  * so we undefine it.
41  */
42 #undef _XOPEN_SOURCE_EXTENDED
43 
44 #include <tcpdump-stdinc.h>
45 
46 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
47 #include <rpc/rpc.h>
48 #ifdef HAVE_RPC_RPCENT_H
49 #include <rpc/rpcent.h>
50 #endif /* HAVE_RPC_RPCENT_H */
51 #endif /* defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H) */
52 
53 #include <stdio.h>
54 #include <string.h>
55 
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "extract.h"
59 
60 #include "ip.h"
61 #ifdef INET6
62 #include "ip6.h"
63 #endif
64 
65 #include "rpc_auth.h"
66 #include "rpc_msg.h"
67 
68 /*
69  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
70  * unrestricted use provided that this legend is included on all tape
71  * media and as a part of the software program in whole or part.  Users
72  * may copy or modify Sun RPC without charge, but are not authorized
73  * to license or distribute it to anyone else except as part of a product or
74  * program developed by the user.
75  *
76  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
77  * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
78  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
79  *
80  * Sun RPC is provided with no support and without any obligation on the
81  * part of Sun Microsystems, Inc. to assist in its use, correction,
82  * modification or enhancement.
83  *
84  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
85  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
86  * OR ANY PART THEREOF.
87  *
88  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
89  * or profits or other special, indirect and consequential damages, even if
90  * Sun has been advised of the possibility of such damages.
91  *
92  * Sun Microsystems, Inc.
93  * 2550 Garcia Avenue
94  * Mountain View, California  94043
95  *
96  *	from: @(#)pmap_prot.h 1.14 88/02/08 SMI
97  *	from: @(#)pmap_prot.h	2.1 88/07/29 4.0 RPCSRC
98  * $FreeBSD$
99  */
100 
101 /*
102  * pmap_prot.h
103  * Protocol for the local binder service, or pmap.
104  *
105  * Copyright (C) 1984, Sun Microsystems, Inc.
106  *
107  * The following procedures are supported by the protocol:
108  *
109  * PMAPPROC_NULL() returns ()
110  * 	takes nothing, returns nothing
111  *
112  * PMAPPROC_SET(struct pmap) returns (bool_t)
113  * 	TRUE is success, FALSE is failure.  Registers the tuple
114  *	[prog, vers, prot, port].
115  *
116  * PMAPPROC_UNSET(struct pmap) returns (bool_t)
117  *	TRUE is success, FALSE is failure.  Un-registers pair
118  *	[prog, vers].  prot and port are ignored.
119  *
120  * PMAPPROC_GETPORT(struct pmap) returns (long unsigned).
121  *	0 is failure.  Otherwise returns the port number where the pair
122  *	[prog, vers] is registered.  It may lie!
123  *
124  * PMAPPROC_DUMP() RETURNS (struct pmaplist *)
125  *
126  * PMAPPROC_CALLIT(unsigned, unsigned, unsigned, string<>)
127  * 	RETURNS (port, string<>);
128  * usage: encapsulatedresults = PMAPPROC_CALLIT(prog, vers, proc, encapsulatedargs);
129  * 	Calls the procedure on the local machine.  If it is not registered,
130  *	this procedure is quite; ie it does not return error information!!!
131  *	This procedure only is supported on rpc/udp and calls via
132  *	rpc/udp.  This routine only passes null authentication parameters.
133  *	This file has no interface to xdr routines for PMAPPROC_CALLIT.
134  *
135  * The service supports remote procedure calls on udp/ip or tcp/ip socket 111.
136  */
137 
138 #define SUNRPC_PMAPPORT		((uint16_t)111)
139 #define SUNRPC_PMAPPROG		((uint32_t)100000)
140 #define SUNRPC_PMAPVERS		((uint32_t)2)
141 #define SUNRPC_PMAPVERS_PROTO	((uint32_t)2)
142 #define SUNRPC_PMAPVERS_ORIG	((uint32_t)1)
143 #define SUNRPC_PMAPPROC_NULL	((uint32_t)0)
144 #define SUNRPC_PMAPPROC_SET	((uint32_t)1)
145 #define SUNRPC_PMAPPROC_UNSET	((uint32_t)2)
146 #define SUNRPC_PMAPPROC_GETPORT	((uint32_t)3)
147 #define SUNRPC_PMAPPROC_DUMP	((uint32_t)4)
148 #define SUNRPC_PMAPPROC_CALLIT	((uint32_t)5)
149 
150 struct sunrpc_pmap {
151 	uint32_t pm_prog;
152 	uint32_t pm_vers;
153 	uint32_t pm_prot;
154 	uint32_t pm_port;
155 };
156 
157 static const struct tok proc2str[] = {
158 	{ SUNRPC_PMAPPROC_NULL,		"null" },
159 	{ SUNRPC_PMAPPROC_SET,		"set" },
160 	{ SUNRPC_PMAPPROC_UNSET,	"unset" },
161 	{ SUNRPC_PMAPPROC_GETPORT,	"getport" },
162 	{ SUNRPC_PMAPPROC_DUMP,		"dump" },
163 	{ SUNRPC_PMAPPROC_CALLIT,	"call" },
164 	{ 0,				NULL }
165 };
166 
167 /* Forwards */
168 static char *progstr(uint32_t);
169 
170 void
171 sunrpcrequest_print(netdissect_options *ndo, register const u_char *bp,
172                     register u_int length, register const u_char *bp2)
173 {
174 	register const struct sunrpc_msg *rp;
175 	register const struct ip *ip;
176 #ifdef INET6
177 	register const struct ip6_hdr *ip6;
178 #endif
179 	uint32_t x;
180 	char srcid[20], dstid[20];	/*fits 32bit*/
181 
182 	rp = (struct sunrpc_msg *)bp;
183 
184 	if (!ndo->ndo_nflag) {
185 		snprintf(srcid, sizeof(srcid), "0x%x",
186 		    EXTRACT_32BITS(&rp->rm_xid));
187 		strlcpy(dstid, "sunrpc", sizeof(dstid));
188 	} else {
189 		snprintf(srcid, sizeof(srcid), "0x%x",
190 		    EXTRACT_32BITS(&rp->rm_xid));
191 		snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
192 	}
193 
194 	switch (IP_V((struct ip *)bp2)) {
195 	case 4:
196 		ip = (struct ip *)bp2;
197 		ND_PRINT((ndo, "%s.%s > %s.%s: %d",
198 		    ipaddr_string(ndo, &ip->ip_src), srcid,
199 		    ipaddr_string(ndo, &ip->ip_dst), dstid, length));
200 		break;
201 #ifdef INET6
202 	case 6:
203 		ip6 = (struct ip6_hdr *)bp2;
204 		ND_PRINT((ndo, "%s.%s > %s.%s: %d",
205 		    ip6addr_string(ndo, &ip6->ip6_src), srcid,
206 		    ip6addr_string(ndo, &ip6->ip6_dst), dstid, length));
207 		break;
208 #endif
209 	default:
210 		ND_PRINT((ndo, "%s.%s > %s.%s: %d", "?", srcid, "?", dstid, length));
211 		break;
212 	}
213 
214 	ND_PRINT((ndo, " %s", tok2str(proc2str, " proc #%u",
215 	    EXTRACT_32BITS(&rp->rm_call.cb_proc))));
216 	x = EXTRACT_32BITS(&rp->rm_call.cb_rpcvers);
217 	if (x != 2)
218 		ND_PRINT((ndo, " [rpcver %u]", x));
219 
220 	switch (EXTRACT_32BITS(&rp->rm_call.cb_proc)) {
221 
222 	case SUNRPC_PMAPPROC_SET:
223 	case SUNRPC_PMAPPROC_UNSET:
224 	case SUNRPC_PMAPPROC_GETPORT:
225 	case SUNRPC_PMAPPROC_CALLIT:
226 		x = EXTRACT_32BITS(&rp->rm_call.cb_prog);
227 		if (!ndo->ndo_nflag)
228 			ND_PRINT((ndo, " %s", progstr(x)));
229 		else
230 			ND_PRINT((ndo, " %u", x));
231 		ND_PRINT((ndo, ".%u", EXTRACT_32BITS(&rp->rm_call.cb_vers)));
232 		break;
233 	}
234 }
235 
236 static char *
237 progstr(uint32_t prog)
238 {
239 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
240 	register struct rpcent *rp;
241 #endif
242 	static char buf[32];
243 	static uint32_t lastprog = 0;
244 
245 	if (lastprog != 0 && prog == lastprog)
246 		return (buf);
247 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
248 	rp = getrpcbynumber(prog);
249 	if (rp == NULL)
250 #endif
251 		(void) snprintf(buf, sizeof(buf), "#%u", prog);
252 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
253 	else
254 		strlcpy(buf, rp->r_name, sizeof(buf));
255 #endif
256 	return (buf);
257 }
258