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