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