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 #include <sys/cdefs.h>
23 #ifndef lint
24 #if 0
25 static const char rcsid[] _U_ =
26     "@(#) Header: /tcpdump/master/tcpdump/print-sunrpc.c,v 1.47 2005-04-27 21:43:48 guy Exp (LBL)";
27 #else
28 __RCSID("$NetBSD: print-sunrpc.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
29 #endif
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 /*
37  * At least on HP-UX:
38  *
39  *	1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
40  *	   header files
41  *
42  * and
43  *
44  *	2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
45  *	   it
46  *
47  * so we undefine it.
48  */
49 #undef _XOPEN_SOURCE_EXTENDED
50 
51 #include <tcpdump-stdinc.h>
52 
53 #ifdef HAVE_GETRPCBYNUMBER
54 #include <rpc/rpc.h>
55 #ifdef HAVE_RPC_RPCENT_H
56 #include <rpc/rpcent.h>
57 #endif /* HAVE_RPC_RPCENT_H */
58 #endif /* HAVE_GETRPCBYNUMBER */
59 
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include "interface.h"
64 #include "addrtoname.h"
65 #include "extract.h"
66 
67 #include "ip.h"
68 #ifdef INET6
69 #include "ip6.h"
70 #endif
71 
72 #include "rpc_auth.h"
73 #include "rpc_msg.h"
74 #include "pmap_prot.h"
75 
76 static struct tok proc2str[] = {
77 	{ SUNRPC_PMAPPROC_NULL,		"null" },
78 	{ SUNRPC_PMAPPROC_SET,		"set" },
79 	{ SUNRPC_PMAPPROC_UNSET,	"unset" },
80 	{ SUNRPC_PMAPPROC_GETPORT,	"getport" },
81 	{ SUNRPC_PMAPPROC_DUMP,		"dump" },
82 	{ SUNRPC_PMAPPROC_CALLIT,	"call" },
83 	{ 0,				NULL }
84 };
85 
86 /* Forwards */
87 static char *progstr(u_int32_t);
88 
89 void
90 sunrpcrequest_print(register const u_char *bp, register u_int length,
91 		    register const u_char *bp2)
92 {
93 	register const struct sunrpc_msg *rp;
94 	register const struct ip *ip;
95 #ifdef INET6
96 	register const struct ip6_hdr *ip6;
97 #endif
98 	u_int32_t x;
99 	char srcid[20], dstid[20];	/*fits 32bit*/
100 
101 	rp = (struct sunrpc_msg *)bp;
102 
103 	if (!nflag) {
104 		snprintf(srcid, sizeof(srcid), "0x%x",
105 		    EXTRACT_32BITS(&rp->rm_xid));
106 		strlcpy(dstid, "sunrpc", sizeof(dstid));
107 	} else {
108 		snprintf(srcid, sizeof(srcid), "0x%x",
109 		    EXTRACT_32BITS(&rp->rm_xid));
110 		snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
111 	}
112 
113 	switch (IP_V((struct ip *)bp2)) {
114 	case 4:
115 		ip = (struct ip *)bp2;
116 		printf("%s.%s > %s.%s: %d",
117 		    ipaddr_string(&ip->ip_src), srcid,
118 		    ipaddr_string(&ip->ip_dst), dstid, length);
119 		break;
120 #ifdef INET6
121 	case 6:
122 		ip6 = (struct ip6_hdr *)bp2;
123 		printf("%s.%s > %s.%s: %d",
124 		    ip6addr_string(&ip6->ip6_src), srcid,
125 		    ip6addr_string(&ip6->ip6_dst), dstid, length);
126 		break;
127 #endif
128 	default:
129 		printf("%s.%s > %s.%s: %d", "?", srcid, "?", dstid, length);
130 		break;
131 	}
132 
133 	printf(" %s", tok2str(proc2str, " proc #%u",
134 	    EXTRACT_32BITS(&rp->rm_call.cb_proc)));
135 	x = EXTRACT_32BITS(&rp->rm_call.cb_rpcvers);
136 	if (x != 2)
137 		printf(" [rpcver %u]", x);
138 
139 	switch (EXTRACT_32BITS(&rp->rm_call.cb_proc)) {
140 
141 	case SUNRPC_PMAPPROC_SET:
142 	case SUNRPC_PMAPPROC_UNSET:
143 	case SUNRPC_PMAPPROC_GETPORT:
144 	case SUNRPC_PMAPPROC_CALLIT:
145 		x = EXTRACT_32BITS(&rp->rm_call.cb_prog);
146 		if (!nflag)
147 			printf(" %s", progstr(x));
148 		else
149 			printf(" %u", x);
150 		printf(".%u", EXTRACT_32BITS(&rp->rm_call.cb_vers));
151 		break;
152 	}
153 }
154 
155 static char *
156 progstr(prog)
157 	u_int32_t prog;
158 {
159 #ifdef HAVE_GETRPCBYNUMBER
160 	register struct rpcent *rp;
161 #endif
162 	static char buf[32];
163 	static u_int32_t lastprog = 0;
164 
165 	if (lastprog != 0 && prog == lastprog)
166 		return (buf);
167 #ifdef HAVE_GETRPCBYNUMBER
168 	rp = getrpcbynumber(prog);
169 	if (rp == NULL)
170 #endif
171 		(void) snprintf(buf, sizeof(buf), "#%u", prog);
172 #ifdef HAVE_GETRPCBYNUMBER
173 	else
174 		strlcpy(buf, rp->r_name, sizeof(buf));
175 #endif
176 	return (buf);
177 }
178