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