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