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 $ */ 3 /* $DragonFly: src/sys/vfs/nfs/krpc_subr.c,v 1.7 2005/05/29 10:08:36 hsu Exp $ */ 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> 54 55 #include <net/if.h> 56 #include <netinet/in.h> 57 58 #include "rpcv2.h" 59 #include "krpc.h" 60 #include "xdr_subs.h" 61 62 /* 63 * Kernel support for Sun RPC 64 * 65 * Used currently for bootstrapping in nfs diskless configurations. 66 */ 67 68 /* 69 * Generic RPC headers 70 */ 71 72 struct auth_info { 73 u_int32_t authtype; /* auth type */ 74 u_int32_t authlen; /* auth length */ 75 }; 76 77 struct auth_unix { 78 int32_t ua_time; 79 int32_t ua_hostname; /* null */ 80 int32_t ua_uid; 81 int32_t ua_gid; 82 int32_t ua_gidlist; /* null */ 83 }; 84 85 struct rpc_call { 86 u_int32_t rp_xid; /* request transaction id */ 87 int32_t rp_direction; /* call direction (0) */ 88 u_int32_t rp_rpcvers; /* rpc version (2) */ 89 u_int32_t rp_prog; /* program */ 90 u_int32_t rp_vers; /* version */ 91 u_int32_t rp_proc; /* procedure */ 92 struct auth_info rpc_auth; 93 struct auth_unix rpc_unix; 94 struct auth_info rpc_verf; 95 }; 96 97 struct rpc_reply { 98 u_int32_t rp_xid; /* request transaction id */ 99 int32_t rp_direction; /* call direction (1) */ 100 int32_t rp_astatus; /* accept status (0: accepted) */ 101 union { 102 u_int32_t rpu_errno; 103 struct { 104 struct auth_info rok_auth; 105 u_int32_t rok_status; 106 } rpu_rok; 107 } rp_u; 108 }; 109 #define rp_errno rp_u.rpu_errno 110 #define rp_auth rp_u.rpu_rok.rok_auth 111 #define rp_status rp_u.rpu_rok.rok_status 112 113 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */ 114 115 /* 116 * What is the longest we will wait before re-sending a request? 117 * Note this is also the frequency of "RPC timeout" messages. 118 * The re-send loop count sup linearly to this maximum, so the 119 * first complaint will happen after (1+2+3+4+5)=15 seconds. 120 */ 121 #define MAX_RESEND_DELAY 5 /* seconds */ 122 123 /* 124 * Call portmap to lookup a port number for a particular rpc program 125 * Returns non-zero error on failure. 126 */ 127 int 128 krpc_portmap(struct sockaddr_in *sin, /* server address */ 129 u_int prog, u_int vers, /* host order */ 130 u_int16_t *portp, /* network order */ 131 struct thread *td) 132 { 133 struct sdata { 134 u_int32_t prog; /* call program */ 135 u_int32_t vers; /* call version */ 136 u_int32_t proto; /* call protocol */ 137 u_int32_t port; /* call port (unused) */ 138 } *sdata; 139 struct rdata { 140 u_int16_t pad; 141 u_int16_t port; 142 } *rdata; 143 struct mbuf *m; 144 int error; 145 146 /* The portmapper port is fixed. */ 147 if (prog == PMAPPROG) { 148 *portp = htons(PMAPPORT); 149 return 0; 150 } 151 152 m = m_get(MB_WAIT, MT_DATA); 153 if (m == NULL) 154 return ENOBUFS; 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 */ 187 int 188 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, 189 struct mbuf **data, /* input/output */ 190 struct sockaddr **from_p, /* output */ 191 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 rpc_call *call; 198 struct rpc_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))) 222 goto out; 223 224 tv.tv_sec = 1; 225 tv.tv_usec = 0; 226 bzero(&sopt, sizeof sopt); 227 sopt.sopt_level = SOL_SOCKET; 228 sopt.sopt_name = SO_RCVTIMEO; 229 sopt.sopt_val = &tv; 230 sopt.sopt_valsize = sizeof tv; 231 232 if ((error = sosetopt(so, &sopt)) != 0) 233 goto out; 234 235 /* 236 * Enable broadcast if necessary. 237 */ 238 if (from_p) { 239 int on = 1; 240 sopt.sopt_name = SO_BROADCAST; 241 sopt.sopt_val = &on; 242 sopt.sopt_valsize = sizeof on; 243 if ((error = sosetopt(so, &sopt)) != 0) 244 goto out; 245 } 246 247 /* 248 * Bind the local endpoint to a reserved port, 249 * because some NFS servers refuse requests from 250 * non-reserved (non-privileged) ports. 251 */ 252 sin = &ssin; 253 bzero(sin, sizeof *sin); 254 sin->sin_len = sizeof(*sin); 255 sin->sin_family = AF_INET; 256 sin->sin_addr.s_addr = INADDR_ANY; 257 tport = IPPORT_RESERVED; 258 do { 259 tport--; 260 sin->sin_port = htons(tport); 261 error = sobind(so, (struct sockaddr *)sin, td); 262 } while (error == EADDRINUSE && 263 tport > IPPORT_RESERVED / 2); 264 if (error) { 265 printf("bind failed\n"); 266 goto out; 267 } 268 269 /* 270 * Setup socket address for the server. 271 */ 272 273 /* 274 * Prepend RPC message header. 275 */ 276 mhead = m_gethdr(MB_WAIT, MT_DATA); 277 mhead->m_next = *data; 278 call = mtod(mhead, struct rpc_call *); 279 mhead->m_len = sizeof(*call); 280 bzero((caddr_t)call, sizeof(*call)); 281 /* rpc_call part */ 282 xid++; 283 call->rp_xid = txdr_unsigned(xid); 284 /* call->rp_direction = 0; */ 285 call->rp_rpcvers = txdr_unsigned(2); 286 call->rp_prog = txdr_unsigned(prog); 287 call->rp_vers = txdr_unsigned(vers); 288 call->rp_proc = txdr_unsigned(func); 289 /* rpc_auth part (auth_unix as root) */ 290 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX); 291 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix)); 292 /* rpc_verf part (auth_null) */ 293 call->rpc_verf.authtype = 0; 294 call->rpc_verf.authlen = 0; 295 296 /* 297 * Setup packet header 298 */ 299 len = 0; 300 m = mhead; 301 while (m) { 302 len += m->m_len; 303 m = m->m_next; 304 } 305 mhead->m_pkthdr.len = len; 306 mhead->m_pkthdr.rcvif = NULL; 307 308 /* 309 * Send it, repeatedly, until a reply is received, 310 * but delay each re-send by an increasing amount. 311 * If the delay hits the maximum, start complaining. 312 */ 313 timo = 0; 314 for (;;) { 315 /* Send RPC request (or re-send). */ 316 m = m_copym(mhead, 0, M_COPYALL, MB_WAIT); 317 if (m == NULL) { 318 error = ENOBUFS; 319 goto out; 320 } 321 error = sosend(so, (struct sockaddr *)sa, NULL, m, 322 NULL, 0, td); 323 if (error) { 324 printf("krpc_call: sosend: %d\n", error); 325 goto out; 326 } 327 m = NULL; 328 329 /* Determine new timeout. */ 330 if (timo < MAX_RESEND_DELAY) 331 timo++; 332 else { 333 saddr = ntohl(sa->sin_addr.s_addr); 334 printf("RPC timeout for server %d.%d.%d.%d\n", 335 (saddr >> 24) & 255, 336 (saddr >> 16) & 255, 337 (saddr >> 8) & 255, 338 saddr & 255); 339 } 340 341 /* 342 * Wait for up to timo seconds for a reply. 343 * The socket receive timeout was set to 1 second. 344 */ 345 secs = timo; 346 while (secs > 0) { 347 if (from) { 348 FREE(from, M_SONAME); 349 from = NULL; 350 } 351 if (m) { 352 m_freem(m); 353 m = NULL; 354 } 355 bzero(&auio,sizeof(auio)); 356 auio.uio_resid = len = 1<<16; 357 rcvflg = 0; 358 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg); 359 if (error == EWOULDBLOCK) { 360 secs--; 361 continue; 362 } 363 if (error) 364 goto out; 365 len -= auio.uio_resid; 366 367 /* Does the reply contain at least a header? */ 368 if (len < MIN_REPLY_HDR) 369 continue; 370 if (m->m_len < MIN_REPLY_HDR) 371 continue; 372 reply = mtod(m, struct rpc_reply *); 373 374 /* Is it the right reply? */ 375 if (reply->rp_direction != txdr_unsigned(RPC_REPLY)) 376 continue; 377 378 if (reply->rp_xid != txdr_unsigned(xid)) 379 continue; 380 381 /* Was RPC accepted? (authorization OK) */ 382 if (reply->rp_astatus != 0) { 383 error = fxdr_unsigned(u_int32_t, reply->rp_errno); 384 printf("rpc denied, error=%d\n", error); 385 continue; 386 } 387 388 /* Did the call succeed? */ 389 if (reply->rp_status != 0) { 390 error = fxdr_unsigned(u_int32_t, reply->rp_status); 391 if (error == RPC_PROGMISMATCH) { 392 error = EBADRPC; 393 goto out; 394 } 395 printf("rpc denied, status=%d\n", error); 396 continue; 397 } 398 399 goto gotreply; /* break two levels */ 400 401 } /* while secs */ 402 } /* forever send/receive */ 403 404 error = ETIMEDOUT; 405 goto out; 406 407 gotreply: 408 409 /* 410 * Get RPC reply header into first mbuf, 411 * get its length, then strip it off. 412 */ 413 len = sizeof(*reply); 414 if (m->m_len < len) { 415 m = m_pullup(m, len); 416 if (m == NULL) { 417 error = ENOBUFS; 418 goto out; 419 } 420 } 421 reply = mtod(m, struct rpc_reply *); 422 if (reply->rp_auth.authtype != 0) { 423 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen); 424 len = (len + 3) & ~3; /* XXX? */ 425 } 426 m_adj(m, len); 427 428 /* result */ 429 *data = m; 430 if (from_p) { 431 *from_p = from; 432 from = NULL; 433 } 434 435 out: 436 if (mhead) m_freem(mhead); 437 if (from) free(from, M_SONAME); 438 soclose(so); 439 return error; 440 } 441 442 /* 443 * eXternal Data Representation routines. 444 * (but with non-standard args...) 445 */ 446 447 /* 448 * String representation for RPC. 449 */ 450 struct xdr_string { 451 u_int32_t len; /* length without null or padding */ 452 char data[4]; /* data (longer, of course) */ 453 /* data is padded to a long-word boundary */ 454 }; 455 456 struct mbuf * 457 xdr_string_encode(char *str, int len) 458 { 459 struct mbuf *m; 460 struct xdr_string *xs; 461 int dlen; /* padded string length */ 462 int mlen; /* message length */ 463 464 dlen = (len + 3) & ~3; 465 mlen = dlen + 4; 466 467 if (mlen > MCLBYTES) /* If too big, we just can't do it. */ 468 return (NULL); 469 470 m = m_getl(mlen, MB_WAIT, MT_DATA, 0, NULL); 471 xs = mtod(m, struct xdr_string *); 472 m->m_len = mlen; 473 xs->len = txdr_unsigned(len); 474 bcopy(str, xs->data, len); 475 return (m); 476 } 477