xref: /freebsd/sys/rpc/svc_dg.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 #if defined(LIBC_SCCS) && !defined(lint)
38 #ident	"@(#)svc_dg.c	1.17	94/04/24 SMI"
39 #endif
40 #include <sys/cdefs.h>
41 /*
42  * svc_dg.c, Server side for connectionless RPC.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/jail.h>
47 #include <sys/lock.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sx.h>
58 #include <sys/systm.h>
59 #include <sys/uio.h>
60 
61 #include <net/vnet.h>
62 
63 #include <rpc/rpc.h>
64 
65 #include <rpc/rpc_com.h>
66 
67 static enum xprt_stat svc_dg_stat(SVCXPRT *);
68 static bool_t svc_dg_recv(SVCXPRT *, struct rpc_msg *,
69     struct sockaddr **, struct mbuf **);
70 static bool_t svc_dg_reply(SVCXPRT *, struct rpc_msg *,
71     struct sockaddr *, struct mbuf *, uint32_t *);
72 static void svc_dg_destroy(SVCXPRT *);
73 static bool_t svc_dg_control(SVCXPRT *, const u_int, void *);
74 static int svc_dg_soupcall(struct socket *so, void *arg, int waitflag);
75 
76 static const struct xp_ops svc_dg_ops = {
77 	.xp_recv =	svc_dg_recv,
78 	.xp_stat =	svc_dg_stat,
79 	.xp_reply =	svc_dg_reply,
80 	.xp_destroy =	svc_dg_destroy,
81 	.xp_control =	svc_dg_control,
82 };
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 __no_mem_str[] = "out of memory";
97 
98 SVCXPRT *
99 svc_dg_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
100     size_t recvsize)
101 {
102 	SVCXPRT *xprt;
103 	struct __rpc_sockinfo si;
104 	struct sockaddr* sa;
105 	int error;
106 
107 	if (jailed(curthread->td_ucred))
108 		return (NULL);
109 	if (!__rpc_socket2sockinfo(so, &si)) {
110 		printf(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 		printf(svc_dg_str, svc_dg_err2);
120 		return (NULL);
121 	}
122 
123 	xprt = svc_xprt_alloc();
124 	sx_init(&xprt->xp_lock, "xprt->xp_lock");
125 	xprt->xp_pool = pool;
126 	xprt->xp_socket = so;
127 	xprt->xp_p1 = NULL;
128 	xprt->xp_p2 = NULL;
129 	xprt->xp_ops = &svc_dg_ops;
130 
131 	CURVNET_SET(so->so_vnet);
132 	error = so->so_proto->pr_sockaddr(so, &sa);
133 	CURVNET_RESTORE();
134 	if (error)
135 		goto freedata;
136 
137 	memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
138 	free(sa, M_SONAME);
139 
140 	xprt_register(xprt);
141 
142 	SOCKBUF_LOCK(&so->so_rcv);
143 	soupcall_set(so, SO_RCV, svc_dg_soupcall, xprt);
144 	SOCKBUF_UNLOCK(&so->so_rcv);
145 
146 	return (xprt);
147 freedata:
148 	(void) printf(svc_dg_str, __no_mem_str);
149 	svc_xprt_free(xprt);
150 
151 	return (NULL);
152 }
153 
154 /*ARGSUSED*/
155 static enum xprt_stat
156 svc_dg_stat(SVCXPRT *xprt)
157 {
158 
159 	if (soreadable(xprt->xp_socket))
160 		return (XPRT_MOREREQS);
161 
162 	return (XPRT_IDLE);
163 }
164 
165 static bool_t
166 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg,
167     struct sockaddr **addrp, struct mbuf **mp)
168 {
169 	struct uio uio;
170 	struct sockaddr *raddr;
171 	struct mbuf *mreq;
172 	XDR xdrs;
173 	int error, rcvflag;
174 
175 	/*
176 	 * Serialise access to the socket.
177 	 */
178 	sx_xlock(&xprt->xp_lock);
179 
180 	/*
181 	 * The socket upcall calls xprt_active() which will eventually
182 	 * cause the server to call us here. We attempt to read a
183 	 * packet from the socket and process it. If the read fails,
184 	 * we have drained all pending requests so we call
185 	 * xprt_inactive().
186 	 */
187 	uio.uio_resid = 1000000000;
188 	uio.uio_td = curthread;
189 	mreq = NULL;
190 	rcvflag = MSG_DONTWAIT;
191 	error = soreceive(xprt->xp_socket, &raddr, &uio, &mreq, NULL, &rcvflag);
192 
193 	if (error == EWOULDBLOCK) {
194 		/*
195 		 * We must re-test for readability after taking the
196 		 * lock to protect us in the case where a new packet
197 		 * arrives on the socket after our call to soreceive
198 		 * fails with EWOULDBLOCK. The pool lock protects us
199 		 * from racing the upcall after our soreadable() call
200 		 * returns false.
201 		 */
202 		SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
203 		if (!soreadable(xprt->xp_socket))
204 			xprt_inactive_self(xprt);
205 		SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
206 		sx_xunlock(&xprt->xp_lock);
207 		return (FALSE);
208 	}
209 
210 	if (error) {
211 		SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
212 		soupcall_clear(xprt->xp_socket, SO_RCV);
213 		SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
214 		xprt_inactive_self(xprt);
215 		sx_xunlock(&xprt->xp_lock);
216 		return (FALSE);
217 	}
218 
219 	sx_xunlock(&xprt->xp_lock);
220 
221 	xdrmbuf_create(&xdrs, mreq, XDR_DECODE);
222 	if (! xdr_callmsg(&xdrs, msg)) {
223 		XDR_DESTROY(&xdrs);
224 		return (FALSE);
225 	}
226 
227 	*addrp = raddr;
228 	*mp = xdrmbuf_getall(&xdrs);
229 	XDR_DESTROY(&xdrs);
230 
231 	return (TRUE);
232 }
233 
234 static bool_t
235 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg,
236     struct sockaddr *addr, struct mbuf *m, uint32_t *seq)
237 {
238 	XDR xdrs;
239 	struct mbuf *mrep;
240 	bool_t stat = TRUE;
241 	int error;
242 
243 	mrep = m_gethdr(M_WAITOK, MT_DATA);
244 
245 	xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
246 
247 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
248 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
249 		if (!xdr_replymsg(&xdrs, msg))
250 			stat = FALSE;
251 		else
252 			xdrmbuf_append(&xdrs, m);
253 	} else {
254 		stat = xdr_replymsg(&xdrs, msg);
255 	}
256 
257 	if (stat) {
258 		m_fixhdr(mrep);
259 		error = sosend(xprt->xp_socket, addr, NULL, mrep, NULL,
260 		    0, curthread);
261 		if (!error) {
262 			stat = TRUE;
263 		}
264 	} else {
265 		m_freem(mrep);
266 	}
267 
268 	XDR_DESTROY(&xdrs);
269 	xprt->xp_p2 = NULL;
270 
271 	return (stat);
272 }
273 
274 static void
275 svc_dg_destroy(SVCXPRT *xprt)
276 {
277 
278 	SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
279 	soupcall_clear(xprt->xp_socket, SO_RCV);
280 	SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
281 
282 	sx_destroy(&xprt->xp_lock);
283 	if (xprt->xp_socket)
284 		(void)soclose(xprt->xp_socket);
285 
286 	if (xprt->xp_netid)
287 		(void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
288 	svc_xprt_free(xprt);
289 }
290 
291 static bool_t
292 /*ARGSUSED*/
293 svc_dg_control(SVCXPRT *xprt, const u_int rq, void *in)
294 {
295 
296 	return (FALSE);
297 }
298 
299 static int
300 svc_dg_soupcall(struct socket *so, void *arg, int waitflag)
301 {
302 	SVCXPRT *xprt = (SVCXPRT *) arg;
303 
304 	xprt_active(xprt);
305 	return (SU_OK);
306 }
307