xref: /freebsd/lib/libc/rpc/svc_dg.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*	$NetBSD: svc_dg.c,v 1.4 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35  */
36 
37 /*
38  * svc_dg.c, Server side for connectionless RPC.
39  *
40  * Does some caching in the hopes of achieving execute-at-most-once semantics.
41  */
42 
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <rpc/rpc.h>
48 #include <rpc/svc_dg.h>
49 #include <assert.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #ifdef RPC_CACHE_DEBUG
56 #include <netconfig.h>
57 #include <netdir.h>
58 #endif
59 #include <err.h>
60 #include "un-namespace.h"
61 
62 #include "rpc_com.h"
63 #include "mt_misc.h"
64 
65 #define	su_data(xprt)	((struct svc_dg_data *)((xprt)->xp_p2))
66 #define	rpc_buffer(xprt) ((xprt)->xp_p1)
67 
68 #ifndef MAX
69 #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
70 #endif
71 
72 static void svc_dg_ops(SVCXPRT *);
73 static enum xprt_stat svc_dg_stat(SVCXPRT *);
74 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *);
75 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *);
76 static bool_t svc_dg_getargs(SVCXPRT *, xdrproc_t, void *);
77 static bool_t svc_dg_freeargs(SVCXPRT *, xdrproc_t, void *);
78 static void svc_dg_destroy(SVCXPRT *);
79 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
80 static int cache_get(SVCXPRT *, struct rpc_msg *, char **, size_t *);
81 static void cache_set(SVCXPRT *, size_t);
82 int svc_dg_enablecache(SVCXPRT *, u_int);
83 
84 /*
85  * Usage:
86  *	xprt = svc_dg_create(sock, sendsize, recvsize);
87  * Does other connectionless specific initializations.
88  * Once *xprt is initialized, it is registered.
89  * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
90  * system defaults are chosen.
91  * The routines returns NULL if a problem occurred.
92  */
93 static const char svc_dg_str[] = "svc_dg_create: %s";
94 static const char svc_dg_err1[] = "could not get transport information";
95 static const char svc_dg_err2[] = "transport does not support data transfer";
96 static const char svc_dg_err3[] = "getsockname failed";
97 static const char svc_dg_err4[] = "cannot set IP_RECVDSTADDR";
98 static const char __no_mem_str[] = "out of memory";
99 
100 SVCXPRT *
svc_dg_create(int fd,u_int sendsize,u_int recvsize)101 svc_dg_create(int fd, u_int sendsize, u_int recvsize)
102 {
103 	SVCXPRT *xprt;
104 	struct svc_dg_data *su = NULL;
105 	struct __rpc_sockinfo si;
106 	struct sockaddr_storage ss;
107 	socklen_t slen;
108 
109 	if (!__rpc_fd2sockinfo(fd, &si)) {
110 		warnx(svc_dg_str, svc_dg_err1);
111 		return (NULL);
112 	}
113 	/*
114 	 * Find the receive and the send size
115 	 */
116 	sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
117 	recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
118 	if ((sendsize == 0) || (recvsize == 0)) {
119 		warnx(svc_dg_str, svc_dg_err2);
120 		return (NULL);
121 	}
122 
123 	xprt = svc_xprt_alloc();
124 	if (xprt == NULL)
125 		goto freedata;
126 
127 	su = mem_alloc(sizeof (*su));
128 	if (su == NULL)
129 		goto freedata;
130 	su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
131 	if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL)
132 		goto freedata;
133 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
134 		XDR_DECODE);
135 	su->su_cache = NULL;
136 	xprt->xp_fd = fd;
137 	xprt->xp_p2 = su;
138 	xprt->xp_verf.oa_base = su->su_verfbody;
139 	svc_dg_ops(xprt);
140 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
141 
142 	slen = sizeof ss;
143 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
144 		warnx(svc_dg_str, svc_dg_err3);
145 		goto freedata_nowarn;
146 	}
147 	xprt->xp_ltaddr.buf = mem_alloc(sizeof (struct sockaddr_storage));
148 	xprt->xp_ltaddr.maxlen = sizeof (struct sockaddr_storage);
149 	xprt->xp_ltaddr.len = slen;
150 	memcpy(xprt->xp_ltaddr.buf, &ss, slen);
151 
152 	if (ss.ss_family == AF_INET) {
153 		struct sockaddr_in *sin;
154 		static const int true_value = 1;
155 
156 		sin = (struct sockaddr_in *)(void *)&ss;
157 		if (sin->sin_addr.s_addr == INADDR_ANY) {
158 		    su->su_srcaddr.buf = mem_alloc(sizeof (ss));
159 		    su->su_srcaddr.maxlen = sizeof (ss);
160 
161 		    if (_setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR,
162 				    &true_value, sizeof(true_value))) {
163 			    warnx(svc_dg_str,  svc_dg_err4);
164 			    goto freedata_nowarn;
165 		    }
166 		}
167 	}
168 
169 	xprt_register(xprt);
170 	return (xprt);
171 freedata:
172 	(void) warnx(svc_dg_str, __no_mem_str);
173 freedata_nowarn:
174 	if (xprt) {
175 		if (su)
176 			(void) mem_free(su, sizeof (*su));
177 		svc_xprt_free(xprt);
178 	}
179 	return (NULL);
180 }
181 
182 /*ARGSUSED*/
183 static enum xprt_stat
svc_dg_stat(SVCXPRT * xprt)184 svc_dg_stat(SVCXPRT *xprt)
185 {
186 	return (XPRT_IDLE);
187 }
188 
189 static int
svc_dg_recvfrom(int fd,char * buf,int buflen,struct sockaddr * raddr,socklen_t * raddrlen,struct sockaddr * laddr,socklen_t * laddrlen)190 svc_dg_recvfrom(int fd, char *buf, int buflen,
191     struct sockaddr *raddr, socklen_t *raddrlen,
192     struct sockaddr *laddr, socklen_t *laddrlen)
193 {
194 	struct msghdr msg;
195 	struct iovec msg_iov[1];
196 	struct sockaddr_in *lin = (struct sockaddr_in *)laddr;
197 	int rlen;
198 	bool_t have_lin = FALSE;
199 	char tmp[CMSG_LEN(sizeof(*lin))];
200 	struct cmsghdr *cmsg;
201 
202 	memset((char *)&msg, 0, sizeof(msg));
203 	msg_iov[0].iov_base = buf;
204 	msg_iov[0].iov_len = buflen;
205 	msg.msg_iov = msg_iov;
206 	msg.msg_iovlen = 1;
207 	msg.msg_namelen = *raddrlen;
208 	msg.msg_name = (char *)raddr;
209 	if (laddr != NULL) {
210 	    msg.msg_control = (caddr_t)tmp;
211 	    msg.msg_controllen = CMSG_LEN(sizeof(*lin));
212 	}
213 	rlen = _recvmsg(fd, &msg, 0);
214 	if (rlen >= 0)
215 		*raddrlen = msg.msg_namelen;
216 
217 	if (rlen == -1 || laddr == NULL ||
218 	    msg.msg_controllen < sizeof(struct cmsghdr) ||
219 	    msg.msg_flags & MSG_CTRUNC)
220 		return rlen;
221 
222 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
223 	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
224 		if (cmsg->cmsg_level == IPPROTO_IP &&
225 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
226 			have_lin = TRUE;
227 			memcpy(&lin->sin_addr,
228 			    (struct in_addr *)CMSG_DATA(cmsg),
229 			    sizeof(struct in_addr));
230 			break;
231 		}
232 	}
233 
234 	lin->sin_family = AF_INET;
235 	lin->sin_port = 0;
236 	*laddrlen = sizeof(struct sockaddr_in);
237 
238 	if (!have_lin)
239 		lin->sin_addr.s_addr = INADDR_ANY;
240 
241 	return rlen;
242 }
243 
244 static bool_t
svc_dg_recv(SVCXPRT * xprt,struct rpc_msg * msg)245 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg)
246 {
247 	struct svc_dg_data *su = su_data(xprt);
248 	XDR *xdrs = &(su->su_xdrs);
249 	char *reply;
250 	struct sockaddr_storage ss;
251 	socklen_t alen;
252 	size_t replylen;
253 	ssize_t rlen;
254 
255 again:
256 	alen = sizeof (struct sockaddr_storage);
257 	rlen = svc_dg_recvfrom(xprt->xp_fd, rpc_buffer(xprt), su->su_iosz,
258 	    (struct sockaddr *)(void *)&ss, &alen,
259 	    (struct sockaddr *)su->su_srcaddr.buf, &su->su_srcaddr.len);
260 	if (rlen == -1 && errno == EINTR)
261 		goto again;
262 	if (rlen == -1 || (rlen < (ssize_t)(4 * sizeof (u_int32_t))))
263 		return (FALSE);
264 	if (xprt->xp_rtaddr.len < alen) {
265 		if (xprt->xp_rtaddr.len != 0)
266 			mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.len);
267 		xprt->xp_rtaddr.buf = mem_alloc(alen);
268 		xprt->xp_rtaddr.len = alen;
269 	}
270 	memcpy(xprt->xp_rtaddr.buf, &ss, alen);
271 #ifdef PORTMAP
272 	if (ss.ss_family == AF_INET) {
273 		xprt->xp_raddr = *(struct sockaddr_in *)xprt->xp_rtaddr.buf;
274 		xprt->xp_addrlen = sizeof (struct sockaddr_in);
275 	}
276 #endif				/* PORTMAP */
277 	xdrs->x_op = XDR_DECODE;
278 	XDR_SETPOS(xdrs, 0);
279 	if (! xdr_callmsg(xdrs, msg)) {
280 		return (FALSE);
281 	}
282 	su->su_xid = msg->rm_xid;
283 	if (su->su_cache != NULL) {
284 		if (cache_get(xprt, msg, &reply, &replylen)) {
285 			(void)_sendto(xprt->xp_fd, reply, replylen, 0,
286 			    (struct sockaddr *)(void *)&ss, alen);
287 			return (FALSE);
288 		}
289 	}
290 	return (TRUE);
291 }
292 
293 static int
svc_dg_sendto(int fd,char * buf,int buflen,const struct sockaddr * raddr,socklen_t raddrlen,const struct sockaddr * laddr,socklen_t laddrlen)294 svc_dg_sendto(int fd, char *buf, int buflen,
295     const struct sockaddr *raddr, socklen_t raddrlen,
296     const struct sockaddr *laddr, socklen_t laddrlen)
297 {
298 	struct msghdr msg;
299 	struct iovec msg_iov[1];
300 	struct sockaddr_in *laddr_in = (struct sockaddr_in *)laddr;
301 	struct in_addr *lin = &laddr_in->sin_addr;
302 	char tmp[CMSG_SPACE(sizeof(*lin))];
303 	struct cmsghdr *cmsg;
304 
305 	memset((char *)&msg, 0, sizeof(msg));
306 	msg_iov[0].iov_base = buf;
307 	msg_iov[0].iov_len = buflen;
308 	msg.msg_iov = msg_iov;
309 	msg.msg_iovlen = 1;
310 	msg.msg_namelen = raddrlen;
311 	msg.msg_name = (char *)raddr;
312 
313 	if (laddr != NULL && laddr->sa_family == AF_INET &&
314 	    lin->s_addr != INADDR_ANY) {
315 		msg.msg_control = (caddr_t)tmp;
316 		msg.msg_controllen = CMSG_LEN(sizeof(*lin));
317 		cmsg = CMSG_FIRSTHDR(&msg);
318 		cmsg->cmsg_len = CMSG_LEN(sizeof(*lin));
319 		cmsg->cmsg_level = IPPROTO_IP;
320 		cmsg->cmsg_type = IP_SENDSRCADDR;
321 		memcpy(CMSG_DATA(cmsg), lin, sizeof(*lin));
322 	}
323 
324 	return _sendmsg(fd, &msg, 0);
325 }
326 
327 static bool_t
svc_dg_reply(SVCXPRT * xprt,struct rpc_msg * msg)328 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg)
329 {
330 	struct svc_dg_data *su = su_data(xprt);
331 	XDR *xdrs = &(su->su_xdrs);
332 	bool_t stat = TRUE;
333 	size_t slen;
334 	xdrproc_t xdr_proc;
335 	caddr_t xdr_where;
336 
337 	xdrs->x_op = XDR_ENCODE;
338 	XDR_SETPOS(xdrs, 0);
339 	msg->rm_xid = su->su_xid;
340 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
341 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
342 		xdr_proc = msg->acpted_rply.ar_results.proc;
343 		xdr_where = msg->acpted_rply.ar_results.where;
344 		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
345 		msg->acpted_rply.ar_results.where = NULL;
346 
347 		if (!xdr_replymsg(xdrs, msg) ||
348 		    !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where))
349 			stat = FALSE;
350 	} else {
351 		stat = xdr_replymsg(xdrs, msg);
352 	}
353 	if (stat) {
354 		slen = XDR_GETPOS(xdrs);
355 		if (svc_dg_sendto(xprt->xp_fd, rpc_buffer(xprt), slen,
356 		    (struct sockaddr *)xprt->xp_rtaddr.buf,
357 		    (socklen_t)xprt->xp_rtaddr.len,
358 		    (struct sockaddr *)su->su_srcaddr.buf,
359 		    (socklen_t)su->su_srcaddr.len) == (ssize_t) slen) {
360 			stat = TRUE;
361 			if (su->su_cache)
362 				cache_set(xprt, slen);
363 		}
364 	}
365 	return (stat);
366 }
367 
368 static bool_t
svc_dg_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)369 svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
370 {
371 	struct svc_dg_data *su;
372 
373 	assert(xprt != NULL);
374 	su = su_data(xprt);
375 	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
376 		&su->su_xdrs, xdr_args, args_ptr));
377 }
378 
379 static bool_t
svc_dg_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)380 svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
381 {
382 	XDR *xdrs = &(su_data(xprt)->su_xdrs);
383 
384 	xdrs->x_op = XDR_FREE;
385 	return (*xdr_args)(xdrs, args_ptr);
386 }
387 
388 static void
svc_dg_destroy(SVCXPRT * xprt)389 svc_dg_destroy(SVCXPRT *xprt)
390 {
391 	struct svc_dg_data *su = su_data(xprt);
392 
393 	xprt_unregister(xprt);
394 	if (xprt->xp_fd != -1)
395 		(void)_close(xprt->xp_fd);
396 	XDR_DESTROY(&(su->su_xdrs));
397 	(void) mem_free(rpc_buffer(xprt), su->su_iosz);
398 	if (su->su_srcaddr.buf)
399 		(void) mem_free(su->su_srcaddr.buf, su->su_srcaddr.maxlen);
400 	(void) mem_free(su, sizeof (*su));
401 	if (xprt->xp_rtaddr.buf)
402 		(void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
403 	if (xprt->xp_ltaddr.buf)
404 		(void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
405 	free(xprt->xp_tp);
406 	svc_xprt_free(xprt);
407 }
408 
409 static bool_t
410 /*ARGSUSED*/
svc_dg_control(SVCXPRT * xprt,const u_int rq,void * in)411 svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in)
412 {
413 	return (FALSE);
414 }
415 
416 static void
svc_dg_ops(SVCXPRT * xprt)417 svc_dg_ops(SVCXPRT *xprt)
418 {
419 	static struct xp_ops ops;
420 	static struct xp_ops2 ops2;
421 
422 /* VARIABLES PROTECTED BY ops_lock: ops */
423 
424 	mutex_lock(&ops_lock);
425 	if (ops.xp_recv == NULL) {
426 		ops.xp_recv = svc_dg_recv;
427 		ops.xp_stat = svc_dg_stat;
428 		ops.xp_getargs = svc_dg_getargs;
429 		ops.xp_reply = svc_dg_reply;
430 		ops.xp_freeargs = svc_dg_freeargs;
431 		ops.xp_destroy = svc_dg_destroy;
432 		ops2.xp_control = svc_dg_control;
433 	}
434 	xprt->xp_ops = &ops;
435 	xprt->xp_ops2 = &ops2;
436 	mutex_unlock(&ops_lock);
437 }
438 
439 /*  The CACHING COMPONENT */
440 
441 /*
442  * Could have been a separate file, but some part of it depends upon the
443  * private structure of the client handle.
444  *
445  * Fifo cache for cl server
446  * Copies pointers to reply buffers into fifo cache
447  * Buffers are sent again if retransmissions are detected.
448  */
449 
450 #define	SPARSENESS 4	/* 75% sparse */
451 
452 #define	ALLOC(type, size)	\
453 	(type *) mem_alloc((sizeof (type) * (size)))
454 
455 #define	MEMZERO(addr, type, size)	 \
456 	(void) memset((void *) (addr), 0, sizeof (type) * (int) (size))
457 
458 #define	FREE(addr, type, size)	\
459 	mem_free((addr), (sizeof (type) * (size)))
460 
461 /*
462  * An entry in the cache
463  */
464 typedef struct cache_node *cache_ptr;
465 struct cache_node {
466 	/*
467 	 * Index into cache is xid, proc, vers, prog and address
468 	 */
469 	u_int32_t cache_xid;
470 	rpcproc_t cache_proc;
471 	rpcvers_t cache_vers;
472 	rpcprog_t cache_prog;
473 	struct netbuf cache_addr;
474 	/*
475 	 * The cached reply and length
476 	 */
477 	char *cache_reply;
478 	size_t cache_replylen;
479 	/*
480 	 * Next node on the list, if there is a collision
481 	 */
482 	cache_ptr cache_next;
483 };
484 
485 /*
486  * The entire cache
487  */
488 struct cl_cache {
489 	u_int uc_size;		/* size of cache */
490 	cache_ptr *uc_entries;	/* hash table of entries in cache */
491 	cache_ptr *uc_fifo;	/* fifo list of entries in cache */
492 	u_int uc_nextvictim;	/* points to next victim in fifo list */
493 	rpcprog_t uc_prog;	/* saved program number */
494 	rpcvers_t uc_vers;	/* saved version number */
495 	rpcproc_t uc_proc;	/* saved procedure number */
496 };
497 
498 
499 /*
500  * the hashing function
501  */
502 #define	CACHE_LOC(transp, xid)	\
503 	(xid % (SPARSENESS * ((struct cl_cache *) \
504 		su_data(transp)->su_cache)->uc_size))
505 
506 /*
507  * Enable use of the cache. Returns 1 on success, 0 on failure.
508  * Note: there is no disable.
509  */
510 static const char cache_enable_str[] = "svc_enablecache: %s %s";
511 static const char alloc_err[] = "could not allocate cache ";
512 static const char enable_err[] = "cache already enabled";
513 
514 int
svc_dg_enablecache(SVCXPRT * transp,u_int size)515 svc_dg_enablecache(SVCXPRT *transp, u_int size)
516 {
517 	struct svc_dg_data *su = su_data(transp);
518 	struct cl_cache *uc;
519 
520 	mutex_lock(&dupreq_lock);
521 	if (su->su_cache != NULL) {
522 		(void) warnx(cache_enable_str, enable_err, " ");
523 		mutex_unlock(&dupreq_lock);
524 		return (0);
525 	}
526 	uc = ALLOC(struct cl_cache, 1);
527 	if (uc == NULL) {
528 		warnx(cache_enable_str, alloc_err, " ");
529 		mutex_unlock(&dupreq_lock);
530 		return (0);
531 	}
532 	uc->uc_size = size;
533 	uc->uc_nextvictim = 0;
534 	uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS);
535 	if (uc->uc_entries == NULL) {
536 		warnx(cache_enable_str, alloc_err, "data");
537 		FREE(uc, struct cl_cache, 1);
538 		mutex_unlock(&dupreq_lock);
539 		return (0);
540 	}
541 	MEMZERO(uc->uc_entries, cache_ptr, size * SPARSENESS);
542 	uc->uc_fifo = ALLOC(cache_ptr, size);
543 	if (uc->uc_fifo == NULL) {
544 		warnx(cache_enable_str, alloc_err, "fifo");
545 		FREE(uc->uc_entries, cache_ptr, size * SPARSENESS);
546 		FREE(uc, struct cl_cache, 1);
547 		mutex_unlock(&dupreq_lock);
548 		return (0);
549 	}
550 	MEMZERO(uc->uc_fifo, cache_ptr, size);
551 	su->su_cache = (char *)(void *)uc;
552 	mutex_unlock(&dupreq_lock);
553 	return (1);
554 }
555 
556 /*
557  * Set an entry in the cache.  It assumes that the uc entry is set from
558  * the earlier call to cache_get() for the same procedure.  This will always
559  * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
560  * by svc_dg_reply().  All this hoopla because the right RPC parameters are
561  * not available at svc_dg_reply time.
562  */
563 
564 static const char cache_set_str[] = "cache_set: %s";
565 static const char cache_set_err1[] = "victim not found";
566 static const char cache_set_err2[] = "victim alloc failed";
567 static const char cache_set_err3[] = "could not allocate new rpc buffer";
568 
569 static void
cache_set(SVCXPRT * xprt,size_t replylen)570 cache_set(SVCXPRT *xprt, size_t replylen)
571 {
572 	cache_ptr victim;
573 	cache_ptr *vicp;
574 	struct svc_dg_data *su = su_data(xprt);
575 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
576 	u_int loc;
577 	char *newbuf;
578 #ifdef RPC_CACHE_DEBUG
579 	struct netconfig *nconf;
580 	char *uaddr;
581 #endif
582 
583 	mutex_lock(&dupreq_lock);
584 	/*
585 	 * Find space for the new entry, either by
586 	 * reusing an old entry, or by mallocing a new one
587 	 */
588 	victim = uc->uc_fifo[uc->uc_nextvictim];
589 	if (victim != NULL) {
590 		loc = CACHE_LOC(xprt, victim->cache_xid);
591 		for (vicp = &uc->uc_entries[loc];
592 			*vicp != NULL && *vicp != victim;
593 			vicp = &(*vicp)->cache_next)
594 			;
595 		if (*vicp == NULL) {
596 			warnx(cache_set_str, cache_set_err1);
597 			mutex_unlock(&dupreq_lock);
598 			return;
599 		}
600 		*vicp = victim->cache_next;	/* remove from cache */
601 		newbuf = victim->cache_reply;
602 	} else {
603 		victim = ALLOC(struct cache_node, 1);
604 		if (victim == NULL) {
605 			warnx(cache_set_str, cache_set_err2);
606 			mutex_unlock(&dupreq_lock);
607 			return;
608 		}
609 		newbuf = mem_alloc(su->su_iosz);
610 		if (newbuf == NULL) {
611 			warnx(cache_set_str, cache_set_err3);
612 			FREE(victim, struct cache_node, 1);
613 			mutex_unlock(&dupreq_lock);
614 			return;
615 		}
616 	}
617 
618 	/*
619 	 * Store it away
620 	 */
621 #ifdef RPC_CACHE_DEBUG
622 	if (nconf = getnetconfigent(xprt->xp_netid)) {
623 		uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
624 		freenetconfigent(nconf);
625 		printf(
626 	"cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
627 			su->su_xid, uc->uc_prog, uc->uc_vers,
628 			uc->uc_proc, uaddr);
629 		free(uaddr);
630 	}
631 #endif
632 	victim->cache_replylen = replylen;
633 	victim->cache_reply = rpc_buffer(xprt);
634 	rpc_buffer(xprt) = newbuf;
635 	xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt),
636 			su->su_iosz, XDR_ENCODE);
637 	victim->cache_xid = su->su_xid;
638 	victim->cache_proc = uc->uc_proc;
639 	victim->cache_vers = uc->uc_vers;
640 	victim->cache_prog = uc->uc_prog;
641 	victim->cache_addr = xprt->xp_rtaddr;
642 	victim->cache_addr.buf = ALLOC(char, xprt->xp_rtaddr.len);
643 	(void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
644 	    (size_t)xprt->xp_rtaddr.len);
645 	loc = CACHE_LOC(xprt, victim->cache_xid);
646 	victim->cache_next = uc->uc_entries[loc];
647 	uc->uc_entries[loc] = victim;
648 	uc->uc_fifo[uc->uc_nextvictim++] = victim;
649 	uc->uc_nextvictim %= uc->uc_size;
650 	mutex_unlock(&dupreq_lock);
651 }
652 
653 /*
654  * Try to get an entry from the cache
655  * return 1 if found, 0 if not found and set the stage for cache_set()
656  */
657 static int
cache_get(SVCXPRT * xprt,struct rpc_msg * msg,char ** replyp,size_t * replylenp)658 cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp, size_t *replylenp)
659 {
660 	u_int loc;
661 	cache_ptr ent;
662 	struct svc_dg_data *su = su_data(xprt);
663 	struct cl_cache *uc = (struct cl_cache *) su->su_cache;
664 #ifdef RPC_CACHE_DEBUG
665 	struct netconfig *nconf;
666 	char *uaddr;
667 #endif
668 
669 	mutex_lock(&dupreq_lock);
670 	loc = CACHE_LOC(xprt, su->su_xid);
671 	for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
672 		if (ent->cache_xid == su->su_xid &&
673 			ent->cache_proc == msg->rm_call.cb_proc &&
674 			ent->cache_vers == msg->rm_call.cb_vers &&
675 			ent->cache_prog == msg->rm_call.cb_prog &&
676 			ent->cache_addr.len == xprt->xp_rtaddr.len &&
677 			(memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
678 				xprt->xp_rtaddr.len) == 0)) {
679 #ifdef RPC_CACHE_DEBUG
680 			if (nconf = getnetconfigent(xprt->xp_netid)) {
681 				uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
682 				freenetconfigent(nconf);
683 				printf(
684 	"cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
685 					su->su_xid, msg->rm_call.cb_prog,
686 					msg->rm_call.cb_vers,
687 					msg->rm_call.cb_proc, uaddr);
688 				free(uaddr);
689 			}
690 #endif
691 			*replyp = ent->cache_reply;
692 			*replylenp = ent->cache_replylen;
693 			mutex_unlock(&dupreq_lock);
694 			return (1);
695 		}
696 	}
697 	/*
698 	 * Failed to find entry
699 	 * Remember a few things so we can do a set later
700 	 */
701 	uc->uc_proc = msg->rm_call.cb_proc;
702 	uc->uc_vers = msg->rm_call.cb_vers;
703 	uc->uc_prog = msg->rm_call.cb_prog;
704 	mutex_unlock(&dupreq_lock);
705 	return (0);
706 }
707