]> The Tcpdump Group git mirrors - tcpdump/blob - print-sunrpc.c
CHANGES: Move change(s) backported to 4.99
[tcpdump] / print-sunrpc.c
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 #include <config.h>
25
26 /*
27 * At least on HP-UX:
28 *
29 * 1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
30 * header files
31 *
32 * and
33 *
34 * 2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
35 * it
36 *
37 * so we undefine it.
38 */
39 #undef _XOPEN_SOURCE_EXTENDED
40
41 #include "netdissect-stdinc.h"
42
43 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
44 #include <rpc/rpc.h>
45 #ifdef HAVE_RPC_RPCENT_H
46 #include <rpc/rpcent.h>
47 #endif /* HAVE_RPC_RPCENT_H */
48 #endif /* defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H) */
49
50 #include <stdio.h>
51 #include <string.h>
52
53 #define ND_LONGJMP_FROM_TCHECK
54 #include "netdissect.h"
55 #include "addrtoname.h"
56 #include "extract.h"
57
58 #include "ip.h"
59 #include "ip6.h"
60
61 #include "rpc_auth.h"
62 #include "rpc_msg.h"
63
64 /*
65 * Copyright (c) 2009, Sun Microsystems, Inc.
66 * All rights reserved.
67 *
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions are met:
70 * - Redistributions of source code must retain the above copyright notice,
71 * this list of conditions and the following disclaimer.
72 * - Redistributions in binary form must reproduce the above copyright notice,
73 * this list of conditions and the following disclaimer in the documentation
74 * and/or other materials provided with the distribution.
75 * - Neither the name of Sun Microsystems, Inc. nor the names of its
76 * contributors may be used to endorse or promote products derived
77 * from this software without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
80 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
83 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
84 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
85 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
86 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
87 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
88 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
89 * POSSIBILITY OF SUCH DAMAGE.
90 *
91 * from: @(#)pmap_prot.h 1.14 88/02/08 SMI
92 * from: @(#)pmap_prot.h 2.1 88/07/29 4.0 RPCSRC
93 * $FreeBSD: src/include/rpc/pmap_prot.h,v 1.9.2.1 1999/08/29 14:39:05 peter Exp $
94 */
95
96 /*
97 * pmap_prot.h
98 * Protocol for the local binder service, or pmap.
99 *
100 * Copyright (C) 1984, Sun Microsystems, Inc.
101 *
102 * The following procedures are supported by the protocol:
103 *
104 * PMAPPROC_NULL() returns ()
105 * takes nothing, returns nothing
106 *
107 * PMAPPROC_SET(struct pmap) returns (bool_t)
108 * TRUE is success, FALSE is failure. Registers the tuple
109 * [prog, vers, prot, port].
110 *
111 * PMAPPROC_UNSET(struct pmap) returns (bool_t)
112 * TRUE is success, FALSE is failure. Un-registers pair
113 * [prog, vers]. prot and port are ignored.
114 *
115 * PMAPPROC_GETPORT(struct pmap) returns (long unsigned).
116 * 0 is failure. Otherwise returns the port number where the pair
117 * [prog, vers] is registered. It may lie!
118 *
119 * PMAPPROC_DUMP() RETURNS (struct pmaplist *)
120 *
121 * PMAPPROC_CALLIT(unsigned, unsigned, unsigned, string<>)
122 * RETURNS (port, string<>);
123 * usage: encapsulatedresults = PMAPPROC_CALLIT(prog, vers, proc, encapsulatedargs);
124 * Calls the procedure on the local machine. If it is not registered,
125 * this procedure is quite; ie it does not return error information!!!
126 * This procedure only is supported on rpc/udp and calls via
127 * rpc/udp. This routine only passes null authentication parameters.
128 * This file has no interface to xdr routines for PMAPPROC_CALLIT.
129 *
130 * The service supports remote procedure calls on udp/ip or tcp/ip socket 111.
131 */
132
133 #define SUNRPC_PMAPPORT ((uint16_t)111)
134 #define SUNRPC_PMAPPROC_NULL ((uint32_t)0)
135 #define SUNRPC_PMAPPROC_SET ((uint32_t)1)
136 #define SUNRPC_PMAPPROC_UNSET ((uint32_t)2)
137 #define SUNRPC_PMAPPROC_GETPORT ((uint32_t)3)
138 #define SUNRPC_PMAPPROC_DUMP ((uint32_t)4)
139 #define SUNRPC_PMAPPROC_CALLIT ((uint32_t)5)
140
141 static const struct tok proc2str[] = {
142 { SUNRPC_PMAPPROC_NULL, "null" },
143 { SUNRPC_PMAPPROC_SET, "set" },
144 { SUNRPC_PMAPPROC_UNSET, "unset" },
145 { SUNRPC_PMAPPROC_GETPORT, "getport" },
146 { SUNRPC_PMAPPROC_DUMP, "dump" },
147 { SUNRPC_PMAPPROC_CALLIT, "call" },
148 { 0, NULL }
149 };
150
151 /* Forwards */
152 static char *progstr(uint32_t);
153
154 void
155 sunrpc_print(netdissect_options *ndo, const u_char *bp,
156 u_int length, const u_char *bp2)
157 {
158 const struct sunrpc_msg *rp;
159 const struct ip *ip;
160 const struct ip6_hdr *ip6;
161 uint32_t x;
162 char srcid[20], dstid[20]; /*fits 32bit*/
163
164 ndo->ndo_protocol = "sunrpc";
165 rp = (const struct sunrpc_msg *)bp;
166 ND_TCHECK_SIZE(rp);
167
168 if (!ndo->ndo_nflag) {
169 snprintf(srcid, sizeof(srcid), "0x%x",
170 GET_BE_U_4(rp->rm_xid));
171 strlcpy(dstid, "sunrpc", sizeof(dstid));
172 } else {
173 snprintf(srcid, sizeof(srcid), "0x%x",
174 GET_BE_U_4(rp->rm_xid));
175 snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
176 }
177
178 switch (IP_V((const struct ip *)bp2)) {
179 case 4:
180 ip = (const struct ip *)bp2;
181 ND_PRINT("%s.%s > %s.%s: %u",
182 GET_IPADDR_STRING(ip->ip_src), srcid,
183 GET_IPADDR_STRING(ip->ip_dst), dstid, length);
184 break;
185 case 6:
186 ip6 = (const struct ip6_hdr *)bp2;
187 ND_PRINT("%s.%s > %s.%s: %u",
188 GET_IP6ADDR_STRING(ip6->ip6_src), srcid,
189 GET_IP6ADDR_STRING(ip6->ip6_dst), dstid, length);
190 break;
191 default:
192 ND_PRINT("%s.%s > %s.%s: %u", "?", srcid, "?", dstid, length);
193 break;
194 }
195
196 ND_PRINT(" %s", tok2str(proc2str, " proc #%u",
197 GET_BE_U_4(rp->rm_call.cb_proc)));
198 x = GET_BE_U_4(rp->rm_call.cb_rpcvers);
199 if (x != SUNRPC_MSG_VERSION)
200 ND_PRINT(" [rpcver %u]", x);
201
202 switch (GET_BE_U_4(rp->rm_call.cb_proc)) {
203
204 case SUNRPC_PMAPPROC_SET:
205 case SUNRPC_PMAPPROC_UNSET:
206 case SUNRPC_PMAPPROC_GETPORT:
207 case SUNRPC_PMAPPROC_CALLIT:
208 x = GET_BE_U_4(rp->rm_call.cb_prog);
209 if (!ndo->ndo_nflag)
210 ND_PRINT(" %s", progstr(x));
211 else
212 ND_PRINT(" %u", x);
213 ND_PRINT(".%u", GET_BE_U_4(rp->rm_call.cb_vers));
214 break;
215 }
216 }
217
218 static char *
219 progstr(uint32_t prog)
220 {
221 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
222 struct rpcent *rp;
223 #endif
224 static char buf[32];
225 static uint32_t lastprog = 0;
226
227 if (lastprog != 0 && prog == lastprog)
228 return (buf);
229 #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
230 rp = getrpcbynumber(prog);
231 if (rp != NULL)
232 strlcpy(buf, rp->r_name, sizeof(buf));
233 else
234 #endif
235 (void) snprintf(buf, sizeof(buf), "#%u", prog);
236 lastprog = prog;
237 return (buf);
238 }