]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/rpc/svc_vc.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / rpc / svc_vc.c
1 /*      $NetBSD: svc_vc.c,v 1.7 2000/08/03 00:01:53 fvdl Exp $  */
2
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  * 
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  * 
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  * 
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  * 
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  * 
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)svc_tcp.c    2.2 88/08/01 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * svc_vc.c, Server side for Connection Oriented based RPC. 
41  *
42  * Actually implements two flavors of transporter -
43  * a tcp rendezvouser (a listner and connection establisher)
44  * and a record/tcp stream.
45  */
46
47 #include <sys/param.h>
48 #include <sys/lock.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/protosw.h>
55 #include <sys/queue.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sx.h>
59 #include <sys/systm.h>
60 #include <sys/uio.h>
61 #include <netinet/tcp.h>
62
63 #include <rpc/rpc.h>
64
65 #include <rpc/rpc_com.h>
66
67 #include <security/mac/mac_framework.h>
68
69 static bool_t svc_vc_rendezvous_recv(SVCXPRT *, struct rpc_msg *,
70     struct sockaddr **, struct mbuf **);
71 static enum xprt_stat svc_vc_rendezvous_stat(SVCXPRT *);
72 static void svc_vc_rendezvous_destroy(SVCXPRT *);
73 static bool_t svc_vc_null(void);
74 static void svc_vc_destroy(SVCXPRT *);
75 static enum xprt_stat svc_vc_stat(SVCXPRT *);
76 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *,
77     struct sockaddr **, struct mbuf **);
78 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *,
79     struct sockaddr *, struct mbuf *);
80 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
81 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
82     void *in);
83 static SVCXPRT *svc_vc_create_conn(SVCPOOL *pool, struct socket *so,
84     struct sockaddr *raddr);
85 static int svc_vc_accept(struct socket *head, struct socket **sop);
86 static int svc_vc_soupcall(struct socket *so, void *arg, int waitflag);
87
88 static struct xp_ops svc_vc_rendezvous_ops = {
89         .xp_recv =      svc_vc_rendezvous_recv,
90         .xp_stat =      svc_vc_rendezvous_stat,
91         .xp_reply =     (bool_t (*)(SVCXPRT *, struct rpc_msg *,
92                 struct sockaddr *, struct mbuf *))svc_vc_null,
93         .xp_destroy =   svc_vc_rendezvous_destroy,
94         .xp_control =   svc_vc_rendezvous_control
95 };
96
97 static struct xp_ops svc_vc_ops = {
98         .xp_recv =      svc_vc_recv,
99         .xp_stat =      svc_vc_stat,
100         .xp_reply =     svc_vc_reply,
101         .xp_destroy =   svc_vc_destroy,
102         .xp_control =   svc_vc_control
103 };
104
105 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
106         enum xprt_stat strm_stat;
107         struct mbuf *mpending;  /* unparsed data read from the socket */
108         struct mbuf *mreq;      /* current record being built from mpending */
109         uint32_t resid;         /* number of bytes needed for fragment */
110         bool_t eor;             /* reading last fragment of current record */
111 };
112
113 /*
114  * Usage:
115  *      xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
116  *
117  * Creates, registers, and returns a (rpc) tcp based transporter.
118  * Once *xprt is initialized, it is registered as a transporter
119  * see (svc.h, xprt_register).  This routine returns
120  * a NULL if a problem occurred.
121  *
122  * The filedescriptor passed in is expected to refer to a bound, but
123  * not yet connected socket.
124  *
125  * Since streams do buffered io similar to stdio, the caller can specify
126  * how big the send and receive buffers are via the second and third parms;
127  * 0 => use the system default.
128  */
129 SVCXPRT *
130 svc_vc_create(SVCPOOL *pool, struct socket *so, size_t sendsize,
131     size_t recvsize)
132 {
133         SVCXPRT *xprt;
134         struct sockaddr* sa;
135         int error;
136
137         if (so->so_state & SS_ISCONNECTED) {
138                 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
139                 if (error)
140                         return (NULL);
141                 xprt = svc_vc_create_conn(pool, so, sa);
142                 free(sa, M_SONAME);
143                 return (xprt);
144         }
145
146         xprt = svc_xprt_alloc();
147         sx_init(&xprt->xp_lock, "xprt->xp_lock");
148         xprt->xp_pool = pool;
149         xprt->xp_socket = so;
150         xprt->xp_p1 = NULL;
151         xprt->xp_p2 = NULL;
152         xprt->xp_ops = &svc_vc_rendezvous_ops;
153
154         error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
155         if (error)
156                 goto cleanup_svc_vc_create;
157
158         memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
159         free(sa, M_SONAME);
160
161         xprt_register(xprt);
162
163         solisten(so, SOMAXCONN, curthread);
164
165         SOCKBUF_LOCK(&so->so_rcv);
166         xprt->xp_upcallset = 1;
167         soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
168         SOCKBUF_UNLOCK(&so->so_rcv);
169
170         return (xprt);
171 cleanup_svc_vc_create:
172         if (xprt)
173                 svc_xprt_free(xprt);
174         return (NULL);
175 }
176
177 /*
178  * Create a new transport for a socket optained via soaccept().
179  */
180 SVCXPRT *
181 svc_vc_create_conn(SVCPOOL *pool, struct socket *so, struct sockaddr *raddr)
182 {
183         SVCXPRT *xprt = NULL;
184         struct cf_conn *cd = NULL;
185         struct sockaddr* sa = NULL;
186         struct sockopt opt;
187         int one = 1;
188         int error;
189
190         bzero(&opt, sizeof(struct sockopt));
191         opt.sopt_dir = SOPT_SET;
192         opt.sopt_level = SOL_SOCKET;
193         opt.sopt_name = SO_KEEPALIVE;
194         opt.sopt_val = &one;
195         opt.sopt_valsize = sizeof(one);
196         error = sosetopt(so, &opt);
197         if (error)
198                 return (NULL);
199
200         if (so->so_proto->pr_protocol == IPPROTO_TCP) {
201                 bzero(&opt, sizeof(struct sockopt));
202                 opt.sopt_dir = SOPT_SET;
203                 opt.sopt_level = IPPROTO_TCP;
204                 opt.sopt_name = TCP_NODELAY;
205                 opt.sopt_val = &one;
206                 opt.sopt_valsize = sizeof(one);
207                 error = sosetopt(so, &opt);
208                 if (error)
209                         return (NULL);
210         }
211
212         cd = mem_alloc(sizeof(*cd));
213         cd->strm_stat = XPRT_IDLE;
214
215         xprt = svc_xprt_alloc();
216         sx_init(&xprt->xp_lock, "xprt->xp_lock");
217         xprt->xp_pool = pool;
218         xprt->xp_socket = so;
219         xprt->xp_p1 = cd;
220         xprt->xp_p2 = NULL;
221         xprt->xp_ops = &svc_vc_ops;
222
223         /*
224          * See http://www.connectathon.org/talks96/nfstcp.pdf - client
225          * has a 5 minute timer, server has a 6 minute timer.
226          */
227         xprt->xp_idletimeout = 6 * 60;
228
229         memcpy(&xprt->xp_rtaddr, raddr, raddr->sa_len);
230
231         error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
232         if (error)
233                 goto cleanup_svc_vc_create;
234
235         memcpy(&xprt->xp_ltaddr, sa, sa->sa_len);
236         free(sa, M_SONAME);
237
238         xprt_register(xprt);
239
240         SOCKBUF_LOCK(&so->so_rcv);
241         xprt->xp_upcallset = 1;
242         soupcall_set(so, SO_RCV, svc_vc_soupcall, xprt);
243         SOCKBUF_UNLOCK(&so->so_rcv);
244
245         /*
246          * Throw the transport into the active list in case it already
247          * has some data buffered.
248          */
249         sx_xlock(&xprt->xp_lock);
250         xprt_active(xprt);
251         sx_xunlock(&xprt->xp_lock);
252
253         return (xprt);
254 cleanup_svc_vc_create:
255         if (xprt) {
256                 mem_free(xprt, sizeof(*xprt));
257         }
258         if (cd)
259                 mem_free(cd, sizeof(*cd));
260         return (NULL);
261 }
262
263 /*
264  * This does all of the accept except the final call to soaccept. The
265  * caller will call soaccept after dropping its locks (soaccept may
266  * call malloc).
267  */
268 int
269 svc_vc_accept(struct socket *head, struct socket **sop)
270 {
271         int error = 0;
272         struct socket *so;
273
274         if ((head->so_options & SO_ACCEPTCONN) == 0) {
275                 error = EINVAL;
276                 goto done;
277         }
278 #ifdef MAC
279         error = mac_socket_check_accept(curthread->td_ucred, head);
280         if (error != 0)
281                 goto done;
282 #endif
283         ACCEPT_LOCK();
284         if (TAILQ_EMPTY(&head->so_comp)) {
285                 ACCEPT_UNLOCK();
286                 error = EWOULDBLOCK;
287                 goto done;
288         }
289         so = TAILQ_FIRST(&head->so_comp);
290         KASSERT(!(so->so_qstate & SQ_INCOMP), ("svc_vc_accept: so SQ_INCOMP"));
291         KASSERT(so->so_qstate & SQ_COMP, ("svc_vc_accept: so not SQ_COMP"));
292
293         /*
294          * Before changing the flags on the socket, we have to bump the
295          * reference count.  Otherwise, if the protocol calls sofree(),
296          * the socket will be released due to a zero refcount.
297          * XXX might not need soref() since this is simpler than kern_accept.
298          */
299         SOCK_LOCK(so);                  /* soref() and so_state update */
300         soref(so);                      /* file descriptor reference */
301
302         TAILQ_REMOVE(&head->so_comp, so, so_list);
303         head->so_qlen--;
304         so->so_state |= (head->so_state & SS_NBIO);
305         so->so_qstate &= ~SQ_COMP;
306         so->so_head = NULL;
307
308         SOCK_UNLOCK(so);
309         ACCEPT_UNLOCK();
310
311         *sop = so;
312
313         /* connection has been removed from the listen queue */
314         KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
315 done:
316         return (error);
317 }
318
319 /*ARGSUSED*/
320 static bool_t
321 svc_vc_rendezvous_recv(SVCXPRT *xprt, struct rpc_msg *msg,
322     struct sockaddr **addrp, struct mbuf **mp)
323 {
324         struct socket *so = NULL;
325         struct sockaddr *sa = NULL;
326         int error;
327         SVCXPRT *new_xprt;
328
329         /*
330          * The socket upcall calls xprt_active() which will eventually
331          * cause the server to call us here. We attempt to accept a
332          * connection from the socket and turn it into a new
333          * transport. If the accept fails, we have drained all pending
334          * connections so we call xprt_inactive().
335          */
336         sx_xlock(&xprt->xp_lock);
337
338         error = svc_vc_accept(xprt->xp_socket, &so);
339
340         if (error == EWOULDBLOCK) {
341                 /*
342                  * We must re-test for new connections after taking
343                  * the lock to protect us in the case where a new
344                  * connection arrives after our call to accept fails
345                  * with EWOULDBLOCK. The pool lock protects us from
346                  * racing the upcall after our TAILQ_EMPTY() call
347                  * returns false.
348                  */
349                 ACCEPT_LOCK();
350                 mtx_lock(&xprt->xp_pool->sp_lock);
351                 if (TAILQ_EMPTY(&xprt->xp_socket->so_comp))
352                         xprt_inactive_locked(xprt);
353                 mtx_unlock(&xprt->xp_pool->sp_lock);
354                 ACCEPT_UNLOCK();
355                 sx_xunlock(&xprt->xp_lock);
356                 return (FALSE);
357         }
358
359         if (error) {
360                 SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
361                 if (xprt->xp_upcallset) {
362                         xprt->xp_upcallset = 0;
363                         soupcall_clear(xprt->xp_socket, SO_RCV);
364                 }
365                 SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
366                 xprt_inactive(xprt);
367                 sx_xunlock(&xprt->xp_lock);
368                 return (FALSE);
369         }
370
371         sx_xunlock(&xprt->xp_lock);
372
373         sa = 0;
374         error = soaccept(so, &sa);
375
376         if (error) {
377                 /*
378                  * XXX not sure if I need to call sofree or soclose here.
379                  */
380                 if (sa)
381                         free(sa, M_SONAME);
382                 return (FALSE);
383         }
384
385         /*
386          * svc_vc_create_conn will call xprt_register - we don't need
387          * to do anything with the new connection except derefence it.
388          */
389         new_xprt = svc_vc_create_conn(xprt->xp_pool, so, sa);
390         if (!new_xprt) {
391                 soclose(so);
392         } else {
393                 SVC_RELEASE(new_xprt);
394         }
395
396         free(sa, M_SONAME);
397
398         return (FALSE); /* there is never an rpc msg to be processed */
399 }
400
401 /*ARGSUSED*/
402 static enum xprt_stat
403 svc_vc_rendezvous_stat(SVCXPRT *xprt)
404 {
405
406         return (XPRT_IDLE);
407 }
408
409 static void
410 svc_vc_destroy_common(SVCXPRT *xprt)
411 {
412         SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
413         if (xprt->xp_upcallset) {
414                 xprt->xp_upcallset = 0;
415                 soupcall_clear(xprt->xp_socket, SO_RCV);
416         }
417         SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
418
419         sx_destroy(&xprt->xp_lock);
420         if (xprt->xp_socket)
421                 (void)soclose(xprt->xp_socket);
422
423         if (xprt->xp_netid)
424                 (void) mem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
425         svc_xprt_free(xprt);
426 }
427
428 static void
429 svc_vc_rendezvous_destroy(SVCXPRT *xprt)
430 {
431
432         svc_vc_destroy_common(xprt);
433 }
434
435 static void
436 svc_vc_destroy(SVCXPRT *xprt)
437 {
438         struct cf_conn *cd = (struct cf_conn *)xprt->xp_p1;
439
440         svc_vc_destroy_common(xprt);
441
442         if (cd->mreq)
443                 m_freem(cd->mreq);
444         if (cd->mpending)
445                 m_freem(cd->mpending);
446         mem_free(cd, sizeof(*cd));
447 }
448
449 /*ARGSUSED*/
450 static bool_t
451 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
452 {
453         return (FALSE);
454 }
455
456 static bool_t
457 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
458 {
459
460         return (FALSE);
461 }
462
463 static enum xprt_stat
464 svc_vc_stat(SVCXPRT *xprt)
465 {
466         struct cf_conn *cd;
467         struct mbuf *m;
468         size_t n;
469
470         cd = (struct cf_conn *)(xprt->xp_p1);
471
472         if (cd->strm_stat == XPRT_DIED)
473                 return (XPRT_DIED);
474
475         /*
476          * Return XPRT_MOREREQS if we have buffered data and we are
477          * mid-record or if we have enough data for a record
478          * marker. Since this is only a hint, we read mpending and
479          * resid outside the lock. We do need to take the lock if we
480          * have to traverse the mbuf chain.
481          */
482         if (cd->mpending) {
483                 if (cd->resid)
484                         return (XPRT_MOREREQS);
485                 n = 0;
486                 sx_xlock(&xprt->xp_lock);
487                 m = cd->mpending;
488                 while (m && n < sizeof(uint32_t)) {
489                         n += m->m_len;
490                         m = m->m_next;
491                 }
492                 sx_xunlock(&xprt->xp_lock);
493                 if (n >= sizeof(uint32_t))
494                         return (XPRT_MOREREQS);
495         }
496
497         if (soreadable(xprt->xp_socket))
498                 return (XPRT_MOREREQS);
499
500         return (XPRT_IDLE);
501 }
502
503 static bool_t
504 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg,
505     struct sockaddr **addrp, struct mbuf **mp)
506 {
507         struct cf_conn *cd = (struct cf_conn *) xprt->xp_p1;
508         struct uio uio;
509         struct mbuf *m;
510         XDR xdrs;
511         int error, rcvflag;
512
513         /*
514          * Serialise access to the socket and our own record parsing
515          * state.
516          */
517         sx_xlock(&xprt->xp_lock);
518
519         for (;;) {
520                 /*
521                  * If we have an mbuf chain in cd->mpending, try to parse a
522                  * record from it, leaving the result in cd->mreq. If we don't
523                  * have a complete record, leave the partial result in
524                  * cd->mreq and try to read more from the socket.
525                  */
526                 if (cd->mpending) {
527                         /*
528                          * If cd->resid is non-zero, we have part of the
529                          * record already, otherwise we are expecting a record
530                          * marker.
531                          */
532                         if (!cd->resid) {
533                                 /*
534                                  * See if there is enough data buffered to
535                                  * make up a record marker. Make sure we can
536                                  * handle the case where the record marker is
537                                  * split across more than one mbuf.
538                                  */
539                                 size_t n = 0;
540                                 uint32_t header;
541
542                                 m = cd->mpending;
543                                 while (n < sizeof(uint32_t) && m) {
544                                         n += m->m_len;
545                                         m = m->m_next;
546                                 }
547                                 if (n < sizeof(uint32_t))
548                                         goto readmore;
549                                 if (cd->mpending->m_len < sizeof(uint32_t))
550                                         cd->mpending = m_pullup(cd->mpending,
551                                             sizeof(uint32_t));
552                                 memcpy(&header, mtod(cd->mpending, uint32_t *),
553                                     sizeof(header));
554                                 header = ntohl(header);
555                                 cd->eor = (header & 0x80000000) != 0;
556                                 cd->resid = header & 0x7fffffff;
557                                 m_adj(cd->mpending, sizeof(uint32_t));
558                         }
559
560                         /*
561                          * Start pulling off mbufs from cd->mpending
562                          * until we either have a complete record or
563                          * we run out of data. We use m_split to pull
564                          * data - it will pull as much as possible and
565                          * split the last mbuf if necessary.
566                          */
567                         while (cd->mpending && cd->resid) {
568                                 m = cd->mpending;
569                                 if (cd->mpending->m_next
570                                     || cd->mpending->m_len > cd->resid)
571                                         cd->mpending = m_split(cd->mpending,
572                                             cd->resid, M_WAIT);
573                                 else
574                                         cd->mpending = NULL;
575                                 if (cd->mreq)
576                                         m_last(cd->mreq)->m_next = m;
577                                 else
578                                         cd->mreq = m;
579                                 while (m) {
580                                         cd->resid -= m->m_len;
581                                         m = m->m_next;
582                                 }
583                         }
584
585                         /*
586                          * If cd->resid is zero now, we have managed to
587                          * receive a record fragment from the stream. Check
588                          * for the end-of-record mark to see if we need more.
589                          */
590                         if (cd->resid == 0) {
591                                 if (!cd->eor)
592                                         continue;
593
594                                 /*
595                                  * Success - we have a complete record in
596                                  * cd->mreq.
597                                  */
598                                 xdrmbuf_create(&xdrs, cd->mreq, XDR_DECODE);
599                                 cd->mreq = NULL;
600                                 sx_xunlock(&xprt->xp_lock);
601
602                                 if (! xdr_callmsg(&xdrs, msg)) {
603                                         XDR_DESTROY(&xdrs);
604                                         return (FALSE);
605                                 }
606
607                                 *addrp = NULL;
608                                 *mp = xdrmbuf_getall(&xdrs);
609                                 XDR_DESTROY(&xdrs);
610
611                                 return (TRUE);
612                         }
613                 }
614
615         readmore:
616                 /*
617                  * The socket upcall calls xprt_active() which will eventually
618                  * cause the server to call us here. We attempt to
619                  * read as much as possible from the socket and put
620                  * the result in cd->mpending. If the read fails,
621                  * we have drained both cd->mpending and the socket so
622                  * we can call xprt_inactive().
623                  */
624                 uio.uio_resid = 1000000000;
625                 uio.uio_td = curthread;
626                 m = NULL;
627                 rcvflag = MSG_DONTWAIT;
628                 error = soreceive(xprt->xp_socket, NULL, &uio, &m, NULL,
629                     &rcvflag);
630
631                 if (error == EWOULDBLOCK) {
632                         /*
633                          * We must re-test for readability after
634                          * taking the lock to protect us in the case
635                          * where a new packet arrives on the socket
636                          * after our call to soreceive fails with
637                          * EWOULDBLOCK. The pool lock protects us from
638                          * racing the upcall after our soreadable()
639                          * call returns false.
640                          */
641                         mtx_lock(&xprt->xp_pool->sp_lock);
642                         if (!soreadable(xprt->xp_socket))
643                                 xprt_inactive_locked(xprt);
644                         mtx_unlock(&xprt->xp_pool->sp_lock);
645                         sx_xunlock(&xprt->xp_lock);
646                         return (FALSE);
647                 }
648
649                 if (error) {
650                         SOCKBUF_LOCK(&xprt->xp_socket->so_rcv);
651                         if (xprt->xp_upcallset) {
652                                 xprt->xp_upcallset = 0;
653                                 soupcall_clear(xprt->xp_socket, SO_RCV);
654                         }
655                         SOCKBUF_UNLOCK(&xprt->xp_socket->so_rcv);
656                         xprt_inactive(xprt);
657                         cd->strm_stat = XPRT_DIED;
658                         sx_xunlock(&xprt->xp_lock);
659                         return (FALSE);
660                 }
661
662                 if (!m) {
663                         /*
664                          * EOF - the other end has closed the socket.
665                          */
666                         xprt_inactive(xprt);
667                         cd->strm_stat = XPRT_DIED;
668                         sx_xunlock(&xprt->xp_lock);
669                         return (FALSE);
670                 }
671
672                 if (cd->mpending)
673                         m_last(cd->mpending)->m_next = m;
674                 else
675                         cd->mpending = m;
676         }
677 }
678
679 static bool_t
680 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg,
681     struct sockaddr *addr, struct mbuf *m)
682 {
683         XDR xdrs;
684         struct mbuf *mrep;
685         bool_t stat = TRUE;
686         int error;
687
688         /*
689          * Leave space for record mark.
690          */
691         MGETHDR(mrep, M_WAIT, MT_DATA);
692         mrep->m_len = 0;
693         mrep->m_data += sizeof(uint32_t);
694
695         xdrmbuf_create(&xdrs, mrep, XDR_ENCODE);
696
697         if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
698             msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
699                 if (!xdr_replymsg(&xdrs, msg))
700                         stat = FALSE;
701                 else
702                         xdrmbuf_append(&xdrs, m);
703         } else {
704                 stat = xdr_replymsg(&xdrs, msg);
705         }
706
707         if (stat) {
708                 m_fixhdr(mrep);
709
710                 /*
711                  * Prepend a record marker containing the reply length.
712                  */
713                 M_PREPEND(mrep, sizeof(uint32_t), M_WAIT);
714                 *mtod(mrep, uint32_t *) =
715                         htonl(0x80000000 | (mrep->m_pkthdr.len
716                                 - sizeof(uint32_t)));
717                 error = sosend(xprt->xp_socket, NULL, NULL, mrep, NULL,
718                     0, curthread);
719                 if (!error) {
720                         stat = TRUE;
721                 }
722         } else {
723                 m_freem(mrep);
724         }
725
726         XDR_DESTROY(&xdrs);
727         xprt->xp_p2 = NULL;
728
729         return (stat);
730 }
731
732 static bool_t
733 svc_vc_null()
734 {
735
736         return (FALSE);
737 }
738
739 static int
740 svc_vc_soupcall(struct socket *so, void *arg, int waitflag)
741 {
742         SVCXPRT *xprt = (SVCXPRT *) arg;
743
744         xprt_active(xprt);
745         return (SU_OK);
746 }
747
748 #if 0
749 /*
750  * Get the effective UID of the sending process. Used by rpcbind, keyserv
751  * and rpc.yppasswdd on AF_LOCAL.
752  */
753 int
754 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
755         int sock, ret;
756         gid_t egid;
757         uid_t euid;
758         struct sockaddr *sa;
759
760         sock = transp->xp_fd;
761         sa = (struct sockaddr *)transp->xp_rtaddr;
762         if (sa->sa_family == AF_LOCAL) {
763                 ret = getpeereid(sock, &euid, &egid);
764                 if (ret == 0)
765                         *uid = euid;
766                 return (ret);
767         } else
768                 return (-1);
769 }
770 #endif