Deleted Added
full compact
krpc_subr.c (171744) krpc_subr.c (177599)
1/* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
2
3/*-
4 * Copyright (c) 1995 Gordon Ross, Adam Glass
5 * Copyright (c) 1992 Regents of the University of California.
6 * All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * partially based on:
41 * libnetboot/rpc.c
42 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 */
44
45#include <sys/cdefs.h>
1/* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */
2
3/*-
4 * Copyright (c) 1995 Gordon Ross, Adam Glass
5 * Copyright (c) 1992 Regents of the University of California.
6 * All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * partially based on:
41 * libnetboot/rpc.c
42 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 */
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/sys/nfsclient/krpc_subr.c 171744 2007-08-06 14:26:03Z rwatson $");
46__FBSDID("$FreeBSD: head/sys/nfsclient/krpc_subr.c 177599 2008-03-25 09:39:02Z ru $");
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/proc.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/uio.h>
56
57#include <net/if.h>
58#include <netinet/in.h>
59
60#include <rpc/rpcclnt.h>
61
62#include <nfs/rpcv2.h>
63#include <nfsclient/krpc.h>
64#include <nfs/xdr_subs.h>
65
66/*
67 * Kernel support for Sun RPC
68 *
69 * Used currently for bootstrapping in nfs diskless configurations.
70 */
71
72/*
73 * Generic RPC headers
74 */
75
76struct auth_info {
77 u_int32_t authtype; /* auth type */
78 u_int32_t authlen; /* auth length */
79};
80
81struct auth_unix {
82 int32_t ua_time;
83 int32_t ua_hostname; /* null */
84 int32_t ua_uid;
85 int32_t ua_gid;
86 int32_t ua_gidlist; /* null */
87};
88
89struct krpc_call {
90 u_int32_t rp_xid; /* request transaction id */
91 int32_t rp_direction; /* call direction (0) */
92 u_int32_t rp_rpcvers; /* rpc version (2) */
93 u_int32_t rp_prog; /* program */
94 u_int32_t rp_vers; /* version */
95 u_int32_t rp_proc; /* procedure */
96 struct auth_info rpc_auth;
97 struct auth_unix rpc_unix;
98 struct auth_info rpc_verf;
99};
100
101struct krpc_reply {
102 u_int32_t rp_xid; /* request transaction id */
103 int32_t rp_direction; /* call direction (1) */
104 int32_t rp_astatus; /* accept status (0: accepted) */
105 union {
106 u_int32_t rpu_errno;
107 struct {
108 struct auth_info rok_auth;
109 u_int32_t rok_status;
110 } rpu_rok;
111 } rp_u;
112};
113#define rp_errno rp_u.rpu_errno
114#define rp_auth rp_u.rpu_rok.rok_auth
115#define rp_status rp_u.rpu_rok.rok_status
116
117#define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
118
119/*
120 * What is the longest we will wait before re-sending a request?
121 * Note this is also the frequency of "RPC timeout" messages.
122 * The re-send loop count sup linearly to this maximum, so the
123 * first complaint will happen after (1+2+3+4+5)=15 seconds.
124 */
125#define MAX_RESEND_DELAY 5 /* seconds */
126
127/*
128 * Call portmap to lookup a port number for a particular rpc program
129 * Returns non-zero error on failure.
130 */
131int
132krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
133 struct thread *td)
134{
135 struct sdata {
136 u_int32_t prog; /* call program */
137 u_int32_t vers; /* call version */
138 u_int32_t proto; /* call protocol */
139 u_int32_t port; /* call port (unused) */
140 } *sdata;
141 struct rdata {
142 u_int16_t pad;
143 u_int16_t port;
144 } *rdata;
145 struct mbuf *m;
146 int error;
147
148 /* The portmapper port is fixed. */
149 if (prog == PMAPPROG) {
150 *portp = htons(PMAPPORT);
151 return 0;
152 }
153
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/proc.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/uio.h>
56
57#include <net/if.h>
58#include <netinet/in.h>
59
60#include <rpc/rpcclnt.h>
61
62#include <nfs/rpcv2.h>
63#include <nfsclient/krpc.h>
64#include <nfs/xdr_subs.h>
65
66/*
67 * Kernel support for Sun RPC
68 *
69 * Used currently for bootstrapping in nfs diskless configurations.
70 */
71
72/*
73 * Generic RPC headers
74 */
75
76struct auth_info {
77 u_int32_t authtype; /* auth type */
78 u_int32_t authlen; /* auth length */
79};
80
81struct auth_unix {
82 int32_t ua_time;
83 int32_t ua_hostname; /* null */
84 int32_t ua_uid;
85 int32_t ua_gid;
86 int32_t ua_gidlist; /* null */
87};
88
89struct krpc_call {
90 u_int32_t rp_xid; /* request transaction id */
91 int32_t rp_direction; /* call direction (0) */
92 u_int32_t rp_rpcvers; /* rpc version (2) */
93 u_int32_t rp_prog; /* program */
94 u_int32_t rp_vers; /* version */
95 u_int32_t rp_proc; /* procedure */
96 struct auth_info rpc_auth;
97 struct auth_unix rpc_unix;
98 struct auth_info rpc_verf;
99};
100
101struct krpc_reply {
102 u_int32_t rp_xid; /* request transaction id */
103 int32_t rp_direction; /* call direction (1) */
104 int32_t rp_astatus; /* accept status (0: accepted) */
105 union {
106 u_int32_t rpu_errno;
107 struct {
108 struct auth_info rok_auth;
109 u_int32_t rok_status;
110 } rpu_rok;
111 } rp_u;
112};
113#define rp_errno rp_u.rpu_errno
114#define rp_auth rp_u.rpu_rok.rok_auth
115#define rp_status rp_u.rpu_rok.rok_status
116
117#define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
118
119/*
120 * What is the longest we will wait before re-sending a request?
121 * Note this is also the frequency of "RPC timeout" messages.
122 * The re-send loop count sup linearly to this maximum, so the
123 * first complaint will happen after (1+2+3+4+5)=15 seconds.
124 */
125#define MAX_RESEND_DELAY 5 /* seconds */
126
127/*
128 * Call portmap to lookup a port number for a particular rpc program
129 * Returns non-zero error on failure.
130 */
131int
132krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
133 struct thread *td)
134{
135 struct sdata {
136 u_int32_t prog; /* call program */
137 u_int32_t vers; /* call version */
138 u_int32_t proto; /* call protocol */
139 u_int32_t port; /* call port (unused) */
140 } *sdata;
141 struct rdata {
142 u_int16_t pad;
143 u_int16_t port;
144 } *rdata;
145 struct mbuf *m;
146 int error;
147
148 /* The portmapper port is fixed. */
149 if (prog == PMAPPROG) {
150 *portp = htons(PMAPPORT);
151 return 0;
152 }
153
154 m = m_get(M_TRYWAIT, MT_DATA);
155 if (m == NULL)
156 return ENOBUFS;
154 m = m_get(M_WAIT, MT_DATA);
157 sdata = mtod(m, struct sdata *);
158 m->m_len = sizeof(*sdata);
159
160 /* Do the RPC to get it. */
161 sdata->prog = txdr_unsigned(prog);
162 sdata->vers = txdr_unsigned(vers);
163 sdata->proto = txdr_unsigned(IPPROTO_UDP);
164 sdata->port = 0;
165
166 sin->sin_port = htons(PMAPPORT);
167 error = krpc_call(sin, PMAPPROG, PMAPVERS,
168 PMAPPROC_GETPORT, &m, NULL, td);
169 if (error)
170 return error;
171
172 if (m->m_len < sizeof(*rdata)) {
173 m = m_pullup(m, sizeof(*rdata));
174 if (m == NULL)
175 return ENOBUFS;
176 }
177 rdata = mtod(m, struct rdata *);
178 *portp = rdata->port;
179
180 m_freem(m);
181 return 0;
182}
183
184/*
185 * Do a remote procedure call (RPC) and wait for its reply.
186 * If from_p is non-null, then we are doing broadcast, and
187 * the address from whence the response came is saved there.
188 */
189int
190krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
191 struct mbuf **data, struct sockaddr **from_p, struct thread *td)
192{
193 struct socket *so;
194 struct sockaddr_in *sin, ssin;
195 struct sockaddr *from;
196 struct mbuf *m, *nam, *mhead;
197 struct krpc_call *call;
198 struct krpc_reply *reply;
199 struct sockopt sopt;
200 struct timeval tv;
201 struct uio auio;
202 int error, rcvflg, timo, secs, len;
203 static u_int32_t xid = ~0xFF;
204 u_int16_t tport;
205 u_int32_t saddr;
206
207 /*
208 * Validate address family.
209 * Sorry, this is INET specific...
210 */
211 if (sa->sin_family != AF_INET)
212 return (EAFNOSUPPORT);
213
214 /* Free at end if not null. */
215 nam = mhead = NULL;
216 from = NULL;
217
218 /*
219 * Create socket and set its recieve timeout.
220 */
221 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
222 goto out;
223
224 tv.tv_sec = 1;
225 tv.tv_usec = 0;
226 bzero(&sopt, sizeof sopt);
227 sopt.sopt_dir = SOPT_SET;
228 sopt.sopt_level = SOL_SOCKET;
229 sopt.sopt_name = SO_RCVTIMEO;
230 sopt.sopt_val = &tv;
231 sopt.sopt_valsize = sizeof tv;
232
233 if ((error = sosetopt(so, &sopt)) != 0)
234 goto out;
235
236 /*
237 * Enable broadcast if necessary.
238 */
239 if (from_p) {
240 int on = 1;
241 sopt.sopt_name = SO_BROADCAST;
242 sopt.sopt_val = &on;
243 sopt.sopt_valsize = sizeof on;
244 if ((error = sosetopt(so, &sopt)) != 0)
245 goto out;
246 }
247
248 /*
249 * Bind the local endpoint to a reserved port,
250 * because some NFS servers refuse requests from
251 * non-reserved (non-privileged) ports.
252 */
253 sin = &ssin;
254 bzero(sin, sizeof *sin);
255 sin->sin_len = sizeof(*sin);
256 sin->sin_family = AF_INET;
257 sin->sin_addr.s_addr = INADDR_ANY;
258 tport = IPPORT_RESERVED;
259 do {
260 tport--;
261 sin->sin_port = htons(tport);
262 error = sobind(so, (struct sockaddr *)sin, td);
263 } while (error == EADDRINUSE &&
264 tport > IPPORT_RESERVED / 2);
265 if (error) {
266 printf("bind failed\n");
267 goto out;
268 }
269
270 /*
271 * Setup socket address for the server.
272 */
273
274 /*
275 * Prepend RPC message header.
276 */
155 sdata = mtod(m, struct sdata *);
156 m->m_len = sizeof(*sdata);
157
158 /* Do the RPC to get it. */
159 sdata->prog = txdr_unsigned(prog);
160 sdata->vers = txdr_unsigned(vers);
161 sdata->proto = txdr_unsigned(IPPROTO_UDP);
162 sdata->port = 0;
163
164 sin->sin_port = htons(PMAPPORT);
165 error = krpc_call(sin, PMAPPROG, PMAPVERS,
166 PMAPPROC_GETPORT, &m, NULL, td);
167 if (error)
168 return error;
169
170 if (m->m_len < sizeof(*rdata)) {
171 m = m_pullup(m, sizeof(*rdata));
172 if (m == NULL)
173 return ENOBUFS;
174 }
175 rdata = mtod(m, struct rdata *);
176 *portp = rdata->port;
177
178 m_freem(m);
179 return 0;
180}
181
182/*
183 * Do a remote procedure call (RPC) and wait for its reply.
184 * If from_p is non-null, then we are doing broadcast, and
185 * the address from whence the response came is saved there.
186 */
187int
188krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
189 struct mbuf **data, struct sockaddr **from_p, struct thread *td)
190{
191 struct socket *so;
192 struct sockaddr_in *sin, ssin;
193 struct sockaddr *from;
194 struct mbuf *m, *nam, *mhead;
195 struct krpc_call *call;
196 struct krpc_reply *reply;
197 struct sockopt sopt;
198 struct timeval tv;
199 struct uio auio;
200 int error, rcvflg, timo, secs, len;
201 static u_int32_t xid = ~0xFF;
202 u_int16_t tport;
203 u_int32_t saddr;
204
205 /*
206 * Validate address family.
207 * Sorry, this is INET specific...
208 */
209 if (sa->sin_family != AF_INET)
210 return (EAFNOSUPPORT);
211
212 /* Free at end if not null. */
213 nam = mhead = NULL;
214 from = NULL;
215
216 /*
217 * Create socket and set its recieve timeout.
218 */
219 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
220 goto out;
221
222 tv.tv_sec = 1;
223 tv.tv_usec = 0;
224 bzero(&sopt, sizeof sopt);
225 sopt.sopt_dir = SOPT_SET;
226 sopt.sopt_level = SOL_SOCKET;
227 sopt.sopt_name = SO_RCVTIMEO;
228 sopt.sopt_val = &tv;
229 sopt.sopt_valsize = sizeof tv;
230
231 if ((error = sosetopt(so, &sopt)) != 0)
232 goto out;
233
234 /*
235 * Enable broadcast if necessary.
236 */
237 if (from_p) {
238 int on = 1;
239 sopt.sopt_name = SO_BROADCAST;
240 sopt.sopt_val = &on;
241 sopt.sopt_valsize = sizeof on;
242 if ((error = sosetopt(so, &sopt)) != 0)
243 goto out;
244 }
245
246 /*
247 * Bind the local endpoint to a reserved port,
248 * because some NFS servers refuse requests from
249 * non-reserved (non-privileged) ports.
250 */
251 sin = &ssin;
252 bzero(sin, sizeof *sin);
253 sin->sin_len = sizeof(*sin);
254 sin->sin_family = AF_INET;
255 sin->sin_addr.s_addr = INADDR_ANY;
256 tport = IPPORT_RESERVED;
257 do {
258 tport--;
259 sin->sin_port = htons(tport);
260 error = sobind(so, (struct sockaddr *)sin, td);
261 } while (error == EADDRINUSE &&
262 tport > IPPORT_RESERVED / 2);
263 if (error) {
264 printf("bind failed\n");
265 goto out;
266 }
267
268 /*
269 * Setup socket address for the server.
270 */
271
272 /*
273 * Prepend RPC message header.
274 */
277 mhead = m_gethdr(M_TRYWAIT, MT_DATA);
275 mhead = m_gethdr(M_WAIT, MT_DATA);
278 mhead->m_next = *data;
279 call = mtod(mhead, struct krpc_call *);
280 mhead->m_len = sizeof(*call);
281 bzero((caddr_t)call, sizeof(*call));
282 /* rpc_call part */
283 xid++;
284 call->rp_xid = txdr_unsigned(xid);
285 /* call->rp_direction = 0; */
286 call->rp_rpcvers = txdr_unsigned(2);
287 call->rp_prog = txdr_unsigned(prog);
288 call->rp_vers = txdr_unsigned(vers);
289 call->rp_proc = txdr_unsigned(func);
290 /* rpc_auth part (auth_unix as root) */
291 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
292 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
293 /* rpc_verf part (auth_null) */
294 call->rpc_verf.authtype = 0;
295 call->rpc_verf.authlen = 0;
296
297 /*
298 * Setup packet header
299 */
300 m_fixhdr(mhead);
301 mhead->m_pkthdr.rcvif = NULL;
302
303 /*
304 * Send it, repeatedly, until a reply is received,
305 * but delay each re-send by an increasing amount.
306 * If the delay hits the maximum, start complaining.
307 */
308 timo = 0;
309 for (;;) {
310 /* Send RPC request (or re-send). */
276 mhead->m_next = *data;
277 call = mtod(mhead, struct krpc_call *);
278 mhead->m_len = sizeof(*call);
279 bzero((caddr_t)call, sizeof(*call));
280 /* rpc_call part */
281 xid++;
282 call->rp_xid = txdr_unsigned(xid);
283 /* call->rp_direction = 0; */
284 call->rp_rpcvers = txdr_unsigned(2);
285 call->rp_prog = txdr_unsigned(prog);
286 call->rp_vers = txdr_unsigned(vers);
287 call->rp_proc = txdr_unsigned(func);
288 /* rpc_auth part (auth_unix as root) */
289 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
290 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
291 /* rpc_verf part (auth_null) */
292 call->rpc_verf.authtype = 0;
293 call->rpc_verf.authlen = 0;
294
295 /*
296 * Setup packet header
297 */
298 m_fixhdr(mhead);
299 mhead->m_pkthdr.rcvif = NULL;
300
301 /*
302 * Send it, repeatedly, until a reply is received,
303 * but delay each re-send by an increasing amount.
304 * If the delay hits the maximum, start complaining.
305 */
306 timo = 0;
307 for (;;) {
308 /* Send RPC request (or re-send). */
311 m = m_copym(mhead, 0, M_COPYALL, M_TRYWAIT);
312 if (m == NULL) {
313 error = ENOBUFS;
314 goto out;
315 }
309 m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
316 error = sosend(so, (struct sockaddr *)sa, NULL, m,
317 NULL, 0, td);
318 if (error) {
319 printf("krpc_call: sosend: %d\n", error);
320 goto out;
321 }
322 m = NULL;
323
324 /* Determine new timeout. */
325 if (timo < MAX_RESEND_DELAY)
326 timo++;
327 else {
328 saddr = ntohl(sa->sin_addr.s_addr);
329 printf("RPC timeout for server %d.%d.%d.%d\n",
330 (saddr >> 24) & 255,
331 (saddr >> 16) & 255,
332 (saddr >> 8) & 255,
333 saddr & 255);
334 }
335
336 /*
337 * Wait for up to timo seconds for a reply.
338 * The socket receive timeout was set to 1 second.
339 */
340 secs = timo;
341 while (secs > 0) {
342 if (from) {
343 FREE(from, M_SONAME);
344 from = NULL;
345 }
346 if (m) {
347 m_freem(m);
348 m = NULL;
349 }
350 bzero(&auio, sizeof(auio));
351 auio.uio_resid = len = 1<<16;
352 rcvflg = 0;
353 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
354 if (error == EWOULDBLOCK) {
355 secs--;
356 continue;
357 }
358 if (error)
359 goto out;
360 len -= auio.uio_resid;
361
362 /* Does the reply contain at least a header? */
363 if (len < MIN_REPLY_HDR)
364 continue;
365 if (m->m_len < MIN_REPLY_HDR)
366 continue;
367 reply = mtod(m, struct krpc_reply *);
368
369 /* Is it the right reply? */
370 if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
371 continue;
372
373 if (reply->rp_xid != txdr_unsigned(xid))
374 continue;
375
376 /* Was RPC accepted? (authorization OK) */
377 if (reply->rp_astatus != 0) {
378 error = fxdr_unsigned(u_int32_t, reply->rp_errno);
379 printf("rpc denied, error=%d\n", error);
380 continue;
381 }
382
383 /* Did the call succeed? */
384 if (reply->rp_status != 0) {
385 error = fxdr_unsigned(u_int32_t, reply->rp_status);
386 if (error == RPC_PROGMISMATCH) {
387 error = EBADRPC;
388 goto out;
389 }
390 printf("rpc denied, status=%d\n", error);
391 continue;
392 }
393
394 goto gotreply; /* break two levels */
395
396 } /* while secs */
397 } /* forever send/receive */
398
399 error = ETIMEDOUT;
400 goto out;
401
402 gotreply:
403
404 /*
405 * Get RPC reply header into first mbuf,
406 * get its length, then strip it off.
407 */
408 len = sizeof(*reply);
409 if (m->m_len < len) {
410 m = m_pullup(m, len);
411 if (m == NULL) {
412 error = ENOBUFS;
413 goto out;
414 }
415 }
416 reply = mtod(m, struct krpc_reply *);
417 if (reply->rp_auth.authtype != 0) {
418 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
419 len = (len + 3) & ~3; /* XXX? */
420 }
421 m_adj(m, len);
422
423 /* result */
424 *data = m;
425 if (from_p) {
426 *from_p = from;
427 from = NULL;
428 }
429
430 out:
431 if (mhead) m_freem(mhead);
432 if (from) free(from, M_SONAME);
433 soclose(so);
434 return error;
435}
436
437/*
438 * eXternal Data Representation routines.
439 * (but with non-standard args...)
440 */
441
442/*
443 * String representation for RPC.
444 */
445struct xdr_string {
446 u_int32_t len; /* length without null or padding */
447 char data[4]; /* data (longer, of course) */
448 /* data is padded to a long-word boundary */
449};
450
451struct mbuf *
452xdr_string_encode(char *str, int len)
453{
454 struct mbuf *m;
455 struct xdr_string *xs;
456 int dlen; /* padded string length */
457 int mlen; /* message length */
458
459 dlen = (len + 3) & ~3;
460 mlen = dlen + 4;
461
462 if (mlen > MCLBYTES) /* If too big, we just can't do it. */
463 return (NULL);
464
310 error = sosend(so, (struct sockaddr *)sa, NULL, m,
311 NULL, 0, td);
312 if (error) {
313 printf("krpc_call: sosend: %d\n", error);
314 goto out;
315 }
316 m = NULL;
317
318 /* Determine new timeout. */
319 if (timo < MAX_RESEND_DELAY)
320 timo++;
321 else {
322 saddr = ntohl(sa->sin_addr.s_addr);
323 printf("RPC timeout for server %d.%d.%d.%d\n",
324 (saddr >> 24) & 255,
325 (saddr >> 16) & 255,
326 (saddr >> 8) & 255,
327 saddr & 255);
328 }
329
330 /*
331 * Wait for up to timo seconds for a reply.
332 * The socket receive timeout was set to 1 second.
333 */
334 secs = timo;
335 while (secs > 0) {
336 if (from) {
337 FREE(from, M_SONAME);
338 from = NULL;
339 }
340 if (m) {
341 m_freem(m);
342 m = NULL;
343 }
344 bzero(&auio, sizeof(auio));
345 auio.uio_resid = len = 1<<16;
346 rcvflg = 0;
347 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
348 if (error == EWOULDBLOCK) {
349 secs--;
350 continue;
351 }
352 if (error)
353 goto out;
354 len -= auio.uio_resid;
355
356 /* Does the reply contain at least a header? */
357 if (len < MIN_REPLY_HDR)
358 continue;
359 if (m->m_len < MIN_REPLY_HDR)
360 continue;
361 reply = mtod(m, struct krpc_reply *);
362
363 /* Is it the right reply? */
364 if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
365 continue;
366
367 if (reply->rp_xid != txdr_unsigned(xid))
368 continue;
369
370 /* Was RPC accepted? (authorization OK) */
371 if (reply->rp_astatus != 0) {
372 error = fxdr_unsigned(u_int32_t, reply->rp_errno);
373 printf("rpc denied, error=%d\n", error);
374 continue;
375 }
376
377 /* Did the call succeed? */
378 if (reply->rp_status != 0) {
379 error = fxdr_unsigned(u_int32_t, reply->rp_status);
380 if (error == RPC_PROGMISMATCH) {
381 error = EBADRPC;
382 goto out;
383 }
384 printf("rpc denied, status=%d\n", error);
385 continue;
386 }
387
388 goto gotreply; /* break two levels */
389
390 } /* while secs */
391 } /* forever send/receive */
392
393 error = ETIMEDOUT;
394 goto out;
395
396 gotreply:
397
398 /*
399 * Get RPC reply header into first mbuf,
400 * get its length, then strip it off.
401 */
402 len = sizeof(*reply);
403 if (m->m_len < len) {
404 m = m_pullup(m, len);
405 if (m == NULL) {
406 error = ENOBUFS;
407 goto out;
408 }
409 }
410 reply = mtod(m, struct krpc_reply *);
411 if (reply->rp_auth.authtype != 0) {
412 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
413 len = (len + 3) & ~3; /* XXX? */
414 }
415 m_adj(m, len);
416
417 /* result */
418 *data = m;
419 if (from_p) {
420 *from_p = from;
421 from = NULL;
422 }
423
424 out:
425 if (mhead) m_freem(mhead);
426 if (from) free(from, M_SONAME);
427 soclose(so);
428 return error;
429}
430
431/*
432 * eXternal Data Representation routines.
433 * (but with non-standard args...)
434 */
435
436/*
437 * String representation for RPC.
438 */
439struct xdr_string {
440 u_int32_t len; /* length without null or padding */
441 char data[4]; /* data (longer, of course) */
442 /* data is padded to a long-word boundary */
443};
444
445struct mbuf *
446xdr_string_encode(char *str, int len)
447{
448 struct mbuf *m;
449 struct xdr_string *xs;
450 int dlen; /* padded string length */
451 int mlen; /* message length */
452
453 dlen = (len + 3) & ~3;
454 mlen = dlen + 4;
455
456 if (mlen > MCLBYTES) /* If too big, we just can't do it. */
457 return (NULL);
458
465 m = m_get(M_TRYWAIT, MT_DATA);
466 if (mlen > MLEN) {
467 MCLGET(m, M_TRYWAIT);
468 if ((m->m_flags & M_EXT) == 0) {
469 (void) m_free(m); /* There can be only one. */
470 return (NULL);
471 }
472 }
459 m = m_get(M_WAIT, MT_DATA);
460 if (mlen > MLEN)
461 MCLGET(m, M_WAIT);
473 xs = mtod(m, struct xdr_string *);
474 m->m_len = mlen;
475 xs->len = txdr_unsigned(len);
476 bcopy(str, xs->data, len);
477 return (m);
478}
462 xs = mtod(m, struct xdr_string *);
463 m->m_len = mlen;
464 xs->len = txdr_unsigned(len);
465 bcopy(str, xs->data, len);
466 return (m);
467}