17c208ed6SRick Macklem /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
27c208ed6SRick Macklem
37c208ed6SRick Macklem /*-
4df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
5df57947fSPedro F. Giffuni *
67c208ed6SRick Macklem * Copyright (c) 1995 Gordon Ross, Adam Glass
77c208ed6SRick Macklem * Copyright (c) 1992 Regents of the University of California.
87c208ed6SRick Macklem * All rights reserved.
97c208ed6SRick Macklem *
107c208ed6SRick Macklem * This software was developed by the Computer Systems Engineering group
117c208ed6SRick Macklem * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
127c208ed6SRick Macklem * contributed to Berkeley.
137c208ed6SRick Macklem *
147c208ed6SRick Macklem * Redistribution and use in source and binary forms, with or without
157c208ed6SRick Macklem * modification, are permitted provided that the following conditions
167c208ed6SRick Macklem * are met:
177c208ed6SRick Macklem * 1. Redistributions of source code must retain the above copyright
187c208ed6SRick Macklem * notice, this list of conditions and the following disclaimer.
197c208ed6SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright
207c208ed6SRick Macklem * notice, this list of conditions and the following disclaimer in the
217c208ed6SRick Macklem * documentation and/or other materials provided with the distribution.
227c208ed6SRick Macklem * 3. All advertising materials mentioning features or use of this software
237c208ed6SRick Macklem * must display the following acknowledgement:
247c208ed6SRick Macklem * This product includes software developed by the University of
257c208ed6SRick Macklem * California, Lawrence Berkeley Laboratory and its contributors.
267c208ed6SRick Macklem * 4. Neither the name of the University nor the names of its contributors
277c208ed6SRick Macklem * may be used to endorse or promote products derived from this software
287c208ed6SRick Macklem * without specific prior written permission.
297c208ed6SRick Macklem *
307c208ed6SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
317c208ed6SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
327c208ed6SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
337c208ed6SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
347c208ed6SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
357c208ed6SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
367c208ed6SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
377c208ed6SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
387c208ed6SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
397c208ed6SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
407c208ed6SRick Macklem * SUCH DAMAGE.
417c208ed6SRick Macklem *
427c208ed6SRick Macklem * partially based on:
437c208ed6SRick Macklem * libnetboot/rpc.c
447c208ed6SRick Macklem */
457c208ed6SRick Macklem
467c208ed6SRick Macklem #include <sys/param.h>
477c208ed6SRick Macklem #include <sys/systm.h>
487c208ed6SRick Macklem #include <sys/jail.h>
497c208ed6SRick Macklem #include <sys/malloc.h>
507c208ed6SRick Macklem #include <sys/mbuf.h>
517c208ed6SRick Macklem #include <sys/proc.h>
527c208ed6SRick Macklem #include <sys/socket.h>
537c208ed6SRick Macklem #include <sys/socketvar.h>
547c208ed6SRick Macklem #include <sys/uio.h>
557c208ed6SRick Macklem
567c208ed6SRick Macklem #include <net/if.h>
577c208ed6SRick Macklem #include <net/vnet.h>
587c208ed6SRick Macklem
597c208ed6SRick Macklem #include <netinet/in.h>
607c208ed6SRick Macklem
617c208ed6SRick Macklem #include <rpc/types.h>
627c208ed6SRick Macklem #include <rpc/auth.h>
637c208ed6SRick Macklem #include <rpc/rpc_msg.h>
647c208ed6SRick Macklem #include <nfs/krpc.h>
657c208ed6SRick Macklem #include <nfs/xdr_subs.h>
667c208ed6SRick Macklem
677c208ed6SRick Macklem /*
687c208ed6SRick Macklem * Kernel support for Sun RPC
697c208ed6SRick Macklem *
707c208ed6SRick Macklem * Used currently for bootstrapping in nfs diskless configurations.
717c208ed6SRick Macklem */
727c208ed6SRick Macklem
737c208ed6SRick Macklem /*
747c208ed6SRick Macklem * Generic RPC headers
757c208ed6SRick Macklem */
767c208ed6SRick Macklem
777c208ed6SRick Macklem struct auth_info {
787c208ed6SRick Macklem u_int32_t authtype; /* auth type */
797c208ed6SRick Macklem u_int32_t authlen; /* auth length */
807c208ed6SRick Macklem };
817c208ed6SRick Macklem
827c208ed6SRick Macklem struct auth_unix {
837c208ed6SRick Macklem int32_t ua_time;
847c208ed6SRick Macklem int32_t ua_hostname; /* null */
857c208ed6SRick Macklem int32_t ua_uid;
867c208ed6SRick Macklem int32_t ua_gid;
877c208ed6SRick Macklem int32_t ua_gidlist; /* null */
887c208ed6SRick Macklem };
897c208ed6SRick Macklem
907c208ed6SRick Macklem struct krpc_call {
917c208ed6SRick Macklem u_int32_t rp_xid; /* request transaction id */
927c208ed6SRick Macklem int32_t rp_direction; /* call direction (0) */
937c208ed6SRick Macklem u_int32_t rp_rpcvers; /* rpc version (2) */
947c208ed6SRick Macklem u_int32_t rp_prog; /* program */
957c208ed6SRick Macklem u_int32_t rp_vers; /* version */
967c208ed6SRick Macklem u_int32_t rp_proc; /* procedure */
977c208ed6SRick Macklem struct auth_info rpc_auth;
987c208ed6SRick Macklem struct auth_unix rpc_unix;
997c208ed6SRick Macklem struct auth_info rpc_verf;
1007c208ed6SRick Macklem };
1017c208ed6SRick Macklem
1027c208ed6SRick Macklem struct krpc_reply {
1037c208ed6SRick Macklem u_int32_t rp_xid; /* request transaction id */
1047c208ed6SRick Macklem int32_t rp_direction; /* call direction (1) */
1057c208ed6SRick Macklem int32_t rp_astatus; /* accept status (0: accepted) */
1067c208ed6SRick Macklem union {
1077c208ed6SRick Macklem u_int32_t rpu_errno;
1087c208ed6SRick Macklem struct {
1097c208ed6SRick Macklem struct auth_info rok_auth;
1107c208ed6SRick Macklem u_int32_t rok_status;
1117c208ed6SRick Macklem } rpu_rok;
1127c208ed6SRick Macklem } rp_u;
1137c208ed6SRick Macklem };
1147c208ed6SRick Macklem #define rp_errno rp_u.rpu_errno
1157c208ed6SRick Macklem #define rp_auth rp_u.rpu_rok.rok_auth
1167c208ed6SRick Macklem #define rp_status rp_u.rpu_rok.rok_status
1177c208ed6SRick Macklem
1187c208ed6SRick Macklem #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
1197c208ed6SRick Macklem
1207c208ed6SRick Macklem /*
1217c208ed6SRick Macklem * What is the longest we will wait before re-sending a request?
1227c208ed6SRick Macklem * Note this is also the frequency of "RPC timeout" messages.
1237c208ed6SRick Macklem * The re-send loop count sup linearly to this maximum, so the
1247c208ed6SRick Macklem * first complaint will happen after (1+2+3+4+5)=15 seconds.
1257c208ed6SRick Macklem */
1267c208ed6SRick Macklem #define MAX_RESEND_DELAY 5 /* seconds */
1277c208ed6SRick Macklem
1287c208ed6SRick Macklem /*
1297c208ed6SRick Macklem * Call portmap to lookup a port number for a particular rpc program
1307c208ed6SRick Macklem * Returns non-zero error on failure.
1317c208ed6SRick Macklem */
1327c208ed6SRick Macklem int
krpc_portmap(struct sockaddr_in * sin,u_int prog,u_int vers,u_int16_t * portp,struct thread * td)1337c208ed6SRick Macklem krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
1347c208ed6SRick Macklem struct thread *td)
1357c208ed6SRick Macklem {
1367c208ed6SRick Macklem struct sdata {
1377c208ed6SRick Macklem u_int32_t prog; /* call program */
1387c208ed6SRick Macklem u_int32_t vers; /* call version */
1397c208ed6SRick Macklem u_int32_t proto; /* call protocol */
1407c208ed6SRick Macklem u_int32_t port; /* call port (unused) */
1417c208ed6SRick Macklem } *sdata;
1427c208ed6SRick Macklem struct rdata {
1437c208ed6SRick Macklem u_int16_t pad;
1447c208ed6SRick Macklem u_int16_t port;
1457c208ed6SRick Macklem } *rdata;
1467c208ed6SRick Macklem struct mbuf *m;
1477c208ed6SRick Macklem int error;
1487c208ed6SRick Macklem
1497c208ed6SRick Macklem /* The portmapper port is fixed. */
1507c208ed6SRick Macklem if (prog == PMAPPROG) {
1517c208ed6SRick Macklem *portp = htons(PMAPPORT);
1527c208ed6SRick Macklem return 0;
1537c208ed6SRick Macklem }
1547c208ed6SRick Macklem
155eb1b1807SGleb Smirnoff m = m_get(M_WAITOK, MT_DATA);
1567c208ed6SRick Macklem sdata = mtod(m, struct sdata *);
1577c208ed6SRick Macklem m->m_len = sizeof(*sdata);
1587c208ed6SRick Macklem
1597c208ed6SRick Macklem /* Do the RPC to get it. */
1607c208ed6SRick Macklem sdata->prog = txdr_unsigned(prog);
1617c208ed6SRick Macklem sdata->vers = txdr_unsigned(vers);
1627c208ed6SRick Macklem sdata->proto = txdr_unsigned(IPPROTO_UDP);
1637c208ed6SRick Macklem sdata->port = 0;
1647c208ed6SRick Macklem
1657c208ed6SRick Macklem sin->sin_port = htons(PMAPPORT);
1667c208ed6SRick Macklem error = krpc_call(sin, PMAPPROG, PMAPVERS,
1677c208ed6SRick Macklem PMAPPROC_GETPORT, &m, NULL, td);
1687c208ed6SRick Macklem if (error)
1697c208ed6SRick Macklem return error;
1707c208ed6SRick Macklem
1717c208ed6SRick Macklem if (m->m_len < sizeof(*rdata)) {
1727c208ed6SRick Macklem m = m_pullup(m, sizeof(*rdata));
1737c208ed6SRick Macklem if (m == NULL)
1747c208ed6SRick Macklem return ENOBUFS;
1757c208ed6SRick Macklem }
1767c208ed6SRick Macklem rdata = mtod(m, struct rdata *);
1777c208ed6SRick Macklem *portp = rdata->port;
1787c208ed6SRick Macklem
1797c208ed6SRick Macklem m_freem(m);
1807c208ed6SRick Macklem return 0;
1817c208ed6SRick Macklem }
1827c208ed6SRick Macklem
1837c208ed6SRick Macklem /*
1847c208ed6SRick Macklem * Do a remote procedure call (RPC) and wait for its reply.
1857c208ed6SRick Macklem * If from_p is non-null, then we are doing broadcast, and
1867c208ed6SRick Macklem * the address from whence the response came is saved there.
1877c208ed6SRick Macklem */
1887c208ed6SRick Macklem int
krpc_call(struct sockaddr_in * sa,u_int prog,u_int vers,u_int func,struct mbuf ** data,struct sockaddr ** from_p,struct thread * td)1897c208ed6SRick Macklem krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
1907c208ed6SRick Macklem struct mbuf **data, struct sockaddr **from_p, struct thread *td)
1917c208ed6SRick Macklem {
1927c208ed6SRick Macklem struct socket *so;
1937c208ed6SRick Macklem struct sockaddr_in *sin, ssin;
1947c208ed6SRick Macklem struct sockaddr *from;
195*5c2aad7eSWarner Losh struct mbuf *m, *mhead;
1967c208ed6SRick Macklem struct krpc_call *call;
1977c208ed6SRick Macklem struct krpc_reply *reply;
1987c208ed6SRick Macklem struct sockopt sopt;
1997c208ed6SRick Macklem struct timeval tv;
2007c208ed6SRick Macklem struct uio auio;
2017c208ed6SRick Macklem int error, rcvflg, timo, secs, len;
2027c208ed6SRick Macklem static u_int32_t xid = ~0xFF;
2037c208ed6SRick Macklem u_int16_t tport;
2047c208ed6SRick Macklem u_int32_t saddr;
2057c208ed6SRick Macklem
2067c208ed6SRick Macklem /*
2077c208ed6SRick Macklem * Validate address family.
2087c208ed6SRick Macklem * Sorry, this is INET specific...
2097c208ed6SRick Macklem */
2107c208ed6SRick Macklem if (sa->sin_family != AF_INET)
2117c208ed6SRick Macklem return (EAFNOSUPPORT);
2127c208ed6SRick Macklem
2137c208ed6SRick Macklem /* Free at end if not null. */
214*5c2aad7eSWarner Losh mhead = NULL;
2157c208ed6SRick Macklem from = NULL;
2167c208ed6SRick Macklem
2177c208ed6SRick Macklem /*
218a96c9b30SPedro F. Giffuni * Create socket and set its receive timeout.
2197c208ed6SRick Macklem */
2207c208ed6SRick Macklem if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
2212b743262SAlexander Kabaev return error;
2227c208ed6SRick Macklem
2237c208ed6SRick Macklem tv.tv_sec = 1;
2247c208ed6SRick Macklem tv.tv_usec = 0;
2257c208ed6SRick Macklem bzero(&sopt, sizeof sopt);
2267c208ed6SRick Macklem sopt.sopt_dir = SOPT_SET;
2277c208ed6SRick Macklem sopt.sopt_level = SOL_SOCKET;
2287c208ed6SRick Macklem sopt.sopt_name = SO_RCVTIMEO;
2297c208ed6SRick Macklem sopt.sopt_val = &tv;
2307c208ed6SRick Macklem sopt.sopt_valsize = sizeof tv;
2317c208ed6SRick Macklem
2327c208ed6SRick Macklem if ((error = sosetopt(so, &sopt)) != 0)
2337c208ed6SRick Macklem goto out;
2347c208ed6SRick Macklem
2357c208ed6SRick Macklem /*
2367c208ed6SRick Macklem * Enable broadcast if necessary.
2377c208ed6SRick Macklem */
2387c208ed6SRick Macklem if (from_p) {
2397c208ed6SRick Macklem int on = 1;
2407c208ed6SRick Macklem sopt.sopt_name = SO_BROADCAST;
2417c208ed6SRick Macklem sopt.sopt_val = &on;
2427c208ed6SRick Macklem sopt.sopt_valsize = sizeof on;
2437c208ed6SRick Macklem if ((error = sosetopt(so, &sopt)) != 0)
2447c208ed6SRick Macklem goto out;
2457c208ed6SRick Macklem }
2467c208ed6SRick Macklem
2477c208ed6SRick Macklem /*
2487c208ed6SRick Macklem * Bind the local endpoint to a reserved port,
2497c208ed6SRick Macklem * because some NFS servers refuse requests from
2507c208ed6SRick Macklem * non-reserved (non-privileged) ports.
2517c208ed6SRick Macklem */
2527c208ed6SRick Macklem sin = &ssin;
2537c208ed6SRick Macklem bzero(sin, sizeof *sin);
2547c208ed6SRick Macklem sin->sin_len = sizeof(*sin);
2557c208ed6SRick Macklem sin->sin_family = AF_INET;
2567c208ed6SRick Macklem sin->sin_addr.s_addr = INADDR_ANY;
2577c208ed6SRick Macklem tport = IPPORT_RESERVED;
2587c208ed6SRick Macklem do {
2597c208ed6SRick Macklem tport--;
2607c208ed6SRick Macklem sin->sin_port = htons(tport);
2617c208ed6SRick Macklem error = sobind(so, (struct sockaddr *)sin, td);
2627c208ed6SRick Macklem } while (error == EADDRINUSE &&
2637c208ed6SRick Macklem tport > IPPORT_RESERVED / 2);
2647c208ed6SRick Macklem if (error) {
2657c208ed6SRick Macklem printf("bind failed\n");
2667c208ed6SRick Macklem goto out;
2677c208ed6SRick Macklem }
2687c208ed6SRick Macklem
2697c208ed6SRick Macklem /*
2707c208ed6SRick Macklem * Setup socket address for the server.
2717c208ed6SRick Macklem */
2727c208ed6SRick Macklem
2737c208ed6SRick Macklem /*
2747c208ed6SRick Macklem * Prepend RPC message header.
2757c208ed6SRick Macklem */
276eb1b1807SGleb Smirnoff mhead = m_gethdr(M_WAITOK, MT_DATA);
2777c208ed6SRick Macklem mhead->m_next = *data;
2787c208ed6SRick Macklem call = mtod(mhead, struct krpc_call *);
2797c208ed6SRick Macklem mhead->m_len = sizeof(*call);
2807c208ed6SRick Macklem bzero((caddr_t)call, sizeof(*call));
2817c208ed6SRick Macklem /* rpc_call part */
2827c208ed6SRick Macklem xid++;
2837c208ed6SRick Macklem call->rp_xid = txdr_unsigned(xid);
2847c208ed6SRick Macklem /* call->rp_direction = 0; */
2857c208ed6SRick Macklem call->rp_rpcvers = txdr_unsigned(2);
2867c208ed6SRick Macklem call->rp_prog = txdr_unsigned(prog);
2877c208ed6SRick Macklem call->rp_vers = txdr_unsigned(vers);
2887c208ed6SRick Macklem call->rp_proc = txdr_unsigned(func);
2897c208ed6SRick Macklem /* rpc_auth part (auth_unix as root) */
2907c208ed6SRick Macklem call->rpc_auth.authtype = txdr_unsigned(AUTH_UNIX);
2917c208ed6SRick Macklem call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
2927c208ed6SRick Macklem /* rpc_verf part (auth_null) */
2937c208ed6SRick Macklem call->rpc_verf.authtype = 0;
2947c208ed6SRick Macklem call->rpc_verf.authlen = 0;
2957c208ed6SRick Macklem
2967c208ed6SRick Macklem /*
2977c208ed6SRick Macklem * Setup packet header
2987c208ed6SRick Macklem */
2997c208ed6SRick Macklem m_fixhdr(mhead);
3007c208ed6SRick Macklem mhead->m_pkthdr.rcvif = NULL;
3017c208ed6SRick Macklem
3027c208ed6SRick Macklem /*
3037c208ed6SRick Macklem * Send it, repeatedly, until a reply is received,
3047c208ed6SRick Macklem * but delay each re-send by an increasing amount.
3057c208ed6SRick Macklem * If the delay hits the maximum, start complaining.
3067c208ed6SRick Macklem */
3077c208ed6SRick Macklem timo = 0;
3087c208ed6SRick Macklem for (;;) {
3097c208ed6SRick Macklem /* Send RPC request (or re-send). */
310eb1b1807SGleb Smirnoff m = m_copym(mhead, 0, M_COPYALL, M_WAITOK);
3117c208ed6SRick Macklem error = sosend(so, (struct sockaddr *)sa, NULL, m,
3127c208ed6SRick Macklem NULL, 0, td);
3137c208ed6SRick Macklem if (error) {
3147c208ed6SRick Macklem printf("krpc_call: sosend: %d\n", error);
3157c208ed6SRick Macklem goto out;
3167c208ed6SRick Macklem }
3177c208ed6SRick Macklem m = NULL;
3187c208ed6SRick Macklem
3197c208ed6SRick Macklem /* Determine new timeout. */
3207c208ed6SRick Macklem if (timo < MAX_RESEND_DELAY)
3217c208ed6SRick Macklem timo++;
3227c208ed6SRick Macklem else {
3237c208ed6SRick Macklem saddr = ntohl(sa->sin_addr.s_addr);
3247c208ed6SRick Macklem printf("RPC timeout for server %d.%d.%d.%d\n",
3257c208ed6SRick Macklem (saddr >> 24) & 255,
3267c208ed6SRick Macklem (saddr >> 16) & 255,
3277c208ed6SRick Macklem (saddr >> 8) & 255,
3287c208ed6SRick Macklem saddr & 255);
3297c208ed6SRick Macklem }
3307c208ed6SRick Macklem
3317c208ed6SRick Macklem /*
3327c208ed6SRick Macklem * Wait for up to timo seconds for a reply.
3337c208ed6SRick Macklem * The socket receive timeout was set to 1 second.
3347c208ed6SRick Macklem */
3357c208ed6SRick Macklem secs = timo;
3367c208ed6SRick Macklem while (secs > 0) {
3377c208ed6SRick Macklem if (from) {
3387c208ed6SRick Macklem free(from, M_SONAME);
3397c208ed6SRick Macklem from = NULL;
3407c208ed6SRick Macklem }
3417c208ed6SRick Macklem if (m) {
3427c208ed6SRick Macklem m_freem(m);
3437c208ed6SRick Macklem m = NULL;
3447c208ed6SRick Macklem }
3457c208ed6SRick Macklem bzero(&auio, sizeof(auio));
3467c208ed6SRick Macklem auio.uio_resid = len = 1<<16;
3477c208ed6SRick Macklem rcvflg = 0;
3487c208ed6SRick Macklem error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
3497c208ed6SRick Macklem if (error == EWOULDBLOCK) {
3507c208ed6SRick Macklem secs--;
3517c208ed6SRick Macklem continue;
3527c208ed6SRick Macklem }
3537c208ed6SRick Macklem if (error)
3547c208ed6SRick Macklem goto out;
3557c208ed6SRick Macklem len -= auio.uio_resid;
3567c208ed6SRick Macklem
3577c208ed6SRick Macklem /* Does the reply contain at least a header? */
3587c208ed6SRick Macklem if (len < MIN_REPLY_HDR)
3597c208ed6SRick Macklem continue;
3607c208ed6SRick Macklem if (m->m_len < MIN_REPLY_HDR)
3617c208ed6SRick Macklem continue;
3627c208ed6SRick Macklem reply = mtod(m, struct krpc_reply *);
3637c208ed6SRick Macklem
3647c208ed6SRick Macklem /* Is it the right reply? */
3657c208ed6SRick Macklem if (reply->rp_direction != txdr_unsigned(REPLY))
3667c208ed6SRick Macklem continue;
3677c208ed6SRick Macklem
3687c208ed6SRick Macklem if (reply->rp_xid != txdr_unsigned(xid))
3697c208ed6SRick Macklem continue;
3707c208ed6SRick Macklem
3717c208ed6SRick Macklem /* Was RPC accepted? (authorization OK) */
3727c208ed6SRick Macklem if (reply->rp_astatus != 0) {
3737c208ed6SRick Macklem error = fxdr_unsigned(u_int32_t, reply->rp_errno);
3747c208ed6SRick Macklem printf("rpc denied, error=%d\n", error);
3757c208ed6SRick Macklem continue;
3767c208ed6SRick Macklem }
3777c208ed6SRick Macklem
3787c208ed6SRick Macklem /* Did the call succeed? */
3797c208ed6SRick Macklem if (reply->rp_status != 0) {
3807c208ed6SRick Macklem error = fxdr_unsigned(u_int32_t, reply->rp_status);
3817c208ed6SRick Macklem if (error == PROG_MISMATCH) {
3827c208ed6SRick Macklem error = EBADRPC;
3837c208ed6SRick Macklem goto out;
3847c208ed6SRick Macklem }
3857c208ed6SRick Macklem printf("rpc denied, status=%d\n", error);
3867c208ed6SRick Macklem continue;
3877c208ed6SRick Macklem }
3887c208ed6SRick Macklem
3897c208ed6SRick Macklem goto gotreply; /* break two levels */
3907c208ed6SRick Macklem
3917c208ed6SRick Macklem } /* while secs */
3927c208ed6SRick Macklem } /* forever send/receive */
3937c208ed6SRick Macklem
3947c208ed6SRick Macklem error = ETIMEDOUT;
3957c208ed6SRick Macklem goto out;
3967c208ed6SRick Macklem
3977c208ed6SRick Macklem gotreply:
3987c208ed6SRick Macklem
3997c208ed6SRick Macklem /*
4007c208ed6SRick Macklem * Get RPC reply header into first mbuf,
4017c208ed6SRick Macklem * get its length, then strip it off.
4027c208ed6SRick Macklem */
4037c208ed6SRick Macklem len = sizeof(*reply);
4047c208ed6SRick Macklem if (m->m_len < len) {
4057c208ed6SRick Macklem m = m_pullup(m, len);
4067c208ed6SRick Macklem if (m == NULL) {
4077c208ed6SRick Macklem error = ENOBUFS;
4087c208ed6SRick Macklem goto out;
4097c208ed6SRick Macklem }
4107c208ed6SRick Macklem }
4117c208ed6SRick Macklem reply = mtod(m, struct krpc_reply *);
4127c208ed6SRick Macklem if (reply->rp_auth.authtype != 0) {
4137c208ed6SRick Macklem len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
4147c208ed6SRick Macklem len = (len + 3) & ~3; /* XXX? */
4157c208ed6SRick Macklem }
4167c208ed6SRick Macklem m_adj(m, len);
4177c208ed6SRick Macklem
4187c208ed6SRick Macklem /* result */
4197c208ed6SRick Macklem *data = m;
4207c208ed6SRick Macklem if (from_p) {
4217c208ed6SRick Macklem *from_p = from;
4227c208ed6SRick Macklem from = NULL;
4237c208ed6SRick Macklem }
4247c208ed6SRick Macklem
4257c208ed6SRick Macklem out:
4267c208ed6SRick Macklem if (mhead) m_freem(mhead);
4277c208ed6SRick Macklem if (from) free(from, M_SONAME);
4287c208ed6SRick Macklem soclose(so);
4297c208ed6SRick Macklem return error;
4307c208ed6SRick Macklem }
4317c208ed6SRick Macklem
4327c208ed6SRick Macklem /*
4337c208ed6SRick Macklem * eXternal Data Representation routines.
4347c208ed6SRick Macklem * (but with non-standard args...)
4357c208ed6SRick Macklem */
4367c208ed6SRick Macklem
4377c208ed6SRick Macklem /*
4387c208ed6SRick Macklem * String representation for RPC.
4397c208ed6SRick Macklem */
4407c208ed6SRick Macklem struct xdr_string {
4417c208ed6SRick Macklem u_int32_t len; /* length without null or padding */
4427c208ed6SRick Macklem char data[4]; /* data (longer, of course) */
4437c208ed6SRick Macklem /* data is padded to a long-word boundary */
4447c208ed6SRick Macklem };
4457c208ed6SRick Macklem
4467c208ed6SRick Macklem struct mbuf *
xdr_string_encode(char * str,int len)4477c208ed6SRick Macklem xdr_string_encode(char *str, int len)
4487c208ed6SRick Macklem {
4497c208ed6SRick Macklem struct mbuf *m;
4507c208ed6SRick Macklem struct xdr_string *xs;
4517c208ed6SRick Macklem int dlen; /* padded string length */
4527c208ed6SRick Macklem int mlen; /* message length */
4537c208ed6SRick Macklem
4547c208ed6SRick Macklem dlen = (len + 3) & ~3;
4557c208ed6SRick Macklem mlen = dlen + 4;
4567c208ed6SRick Macklem
4577c208ed6SRick Macklem if (mlen > MCLBYTES) /* If too big, we just can't do it. */
4587c208ed6SRick Macklem return (NULL);
4597c208ed6SRick Macklem
46041a7572bSGleb Smirnoff m = m_get2(mlen, M_WAITOK, MT_DATA, 0);
4617c208ed6SRick Macklem xs = mtod(m, struct xdr_string *);
4627c208ed6SRick Macklem m->m_len = mlen;
4637c208ed6SRick Macklem xs->len = txdr_unsigned(len);
4647c208ed6SRick Macklem bcopy(str, xs->data, len);
4657c208ed6SRick Macklem return (m);
4667c208ed6SRick Macklem }
467