1 /* $NetBSD: krpc_subr.c,v 1.19 1996/10/20 13:13:24 fvdl 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/param.h> 46 #include <sys/systm.h> 47 #include <sys/conf.h> 48 #include <sys/ioctl.h> 49 #include <sys/proc.h> 50 #include <sys/mount.h> 51 #include <sys/mbuf.h> 52 #include <sys/reboot.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 56 #include <net/if.h> 57 #include <netinet/in.h> 58 59 #include <nfs/rpcv2.h> 60 #include <nfs/krpc.h> 61 #include <nfs/xdr_subs.h> 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 73 struct auth_info { 74 u_int32_t authtype; /* auth type */ 75 u_int32_t authlen; /* auth length */ 76 }; 77 78 struct 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 86 struct 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 98 struct 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 */ 128 int 129 krpc_portmap(sin, prog, vers, proto, portp) 130 struct sockaddr_in *sin; /* server address */ 131 u_int prog, vers, proto; /* host order */ 132 u_int16_t *portp; /* network order */ 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 153 m = m_get(M_WAIT, MT_DATA); 154 sdata = mtod(m, struct sdata *); 155 m->m_len = sizeof(*sdata); 156 157 /* Do the RPC to get it. */ 158 sdata->prog = txdr_unsigned(prog); 159 sdata->vers = txdr_unsigned(vers); 160 sdata->proto = txdr_unsigned(proto); 161 sdata->port = 0; 162 163 sin->sin_port = htons(PMAPPORT); 164 error = krpc_call(sin, PMAPPROG, PMAPVERS, 165 PMAPPROC_GETPORT, &m, NULL); 166 if (error) 167 return error; 168 169 if (m->m_len < sizeof(*rdata)) { 170 m = m_pullup(m, sizeof(*rdata)); 171 if (m == NULL) 172 return ENOBUFS; 173 } 174 rdata = mtod(m, struct rdata *); 175 *portp = rdata->port; 176 177 m_freem(m); 178 return 0; 179 } 180 181 /* 182 * Do a remote procedure call (RPC) and wait for its reply. 183 * If from_p is non-null, then we are doing broadcast, and 184 * the address from whence the response came is saved there. 185 */ 186 int 187 krpc_call(sa, prog, vers, func, data, from_p) 188 struct sockaddr_in *sa; 189 u_int prog, vers, func; 190 struct mbuf **data; /* input/output */ 191 struct mbuf **from_p; /* output */ 192 { 193 struct socket *so; 194 struct sockaddr_in *sin; 195 struct mbuf *m, *nam, *mhead, *from; 196 struct rpc_call *call; 197 struct rpc_reply *reply; 198 struct uio auio; 199 int error, rcvflg, timo, secs, len; 200 static u_int32_t xid = ~0xFF; 201 u_int16_t tport; 202 struct timeval *tv; 203 204 /* 205 * Validate address family. 206 * Sorry, this is INET specific... 207 */ 208 if (sa->sin_family != AF_INET) 209 return (EAFNOSUPPORT); 210 211 /* Free at end if not null. */ 212 nam = mhead = NULL; 213 from = NULL; 214 215 /* 216 * Create socket and set its recieve timeout. 217 */ 218 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0))) 219 goto out; 220 221 m = m_get(M_WAIT, MT_SOOPTS); 222 tv = mtod(m, struct timeval *); 223 m->m_len = sizeof(*tv); 224 tv->tv_sec = 1; 225 tv->tv_usec = 0; 226 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m))) 227 goto out; 228 229 /* 230 * Enable broadcast if necessary. 231 */ 232 if (from_p) { 233 int32_t *on; 234 m = m_get(M_WAIT, MT_SOOPTS); 235 on = mtod(m, int32_t *); 236 m->m_len = sizeof(*on); 237 *on = 1; 238 if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m))) 239 goto out; 240 } 241 242 /* 243 * Bind the local endpoint to a reserved port, 244 * because some NFS servers refuse requests from 245 * non-reserved (non-privileged) ports. 246 */ 247 m = m_getclr(M_WAIT, MT_SONAME); 248 sin = mtod(m, struct sockaddr_in *); 249 sin->sin_len = m->m_len = sizeof(*sin); 250 sin->sin_family = AF_INET; 251 sin->sin_addr.s_addr = INADDR_ANY; 252 tport = IPPORT_RESERVED; 253 do { 254 tport--; 255 sin->sin_port = htons(tport); 256 error = sobind(so, m); 257 } while (error == EADDRINUSE && 258 tport > IPPORT_RESERVED / 2); 259 m_freem(m); 260 if (error) { 261 printf("bind failed\n"); 262 goto out; 263 } 264 265 /* 266 * Setup socket address for the server. 267 */ 268 nam = m_get(M_WAIT, MT_SONAME); 269 sin = mtod(nam, struct sockaddr_in *); 270 bcopy((caddr_t)sa, (caddr_t)sin, 271 (nam->m_len = sa->sin_len)); 272 273 /* 274 * Prepend RPC message header. 275 */ 276 mhead = m_gethdr(M_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, M_WAIT); 317 if (m == NULL) { 318 error = ENOBUFS; 319 goto out; 320 } 321 error = sosend(so, nam, NULL, m, NULL, 0); 322 if (error) { 323 printf("krpc_call: sosend: %d\n", error); 324 goto out; 325 } 326 m = NULL; 327 328 /* Determine new timeout. */ 329 if (timo < MAX_RESEND_DELAY) 330 timo++; 331 else 332 printf("RPC timeout for server 0x%x\n", 333 ntohl(sin->sin_addr.s_addr)); 334 335 /* 336 * Wait for up to timo seconds for a reply. 337 * The socket receive timeout was set to 1 second. 338 */ 339 secs = timo; 340 while (secs > 0) { 341 if (from) { 342 m_freem(from); 343 from = NULL; 344 } 345 if (m) { 346 m_freem(m); 347 m = NULL; 348 } 349 auio.uio_resid = len = 1<<16; 350 rcvflg = 0; 351 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg); 352 if (error == EWOULDBLOCK) { 353 secs--; 354 continue; 355 } 356 if (error) 357 goto out; 358 len -= auio.uio_resid; 359 360 /* Does the reply contain at least a header? */ 361 if (len < MIN_REPLY_HDR) 362 continue; 363 if (m->m_len < MIN_REPLY_HDR) 364 continue; 365 reply = mtod(m, struct rpc_reply *); 366 367 /* Is it the right reply? */ 368 if (reply->rp_direction != txdr_unsigned(RPC_REPLY)) 369 continue; 370 371 if (reply->rp_xid != txdr_unsigned(xid)) 372 continue; 373 374 /* Was RPC accepted? (authorization OK) */ 375 if (reply->rp_astatus != 0) { 376 error = fxdr_unsigned(u_int32_t, reply->rp_errno); 377 printf("rpc denied, error=%d\n", error); 378 continue; 379 } 380 381 /* Did the call succeed? */ 382 if (reply->rp_status != 0) { 383 error = fxdr_unsigned(u_int32_t, reply->rp_status); 384 if (error == RPC_PROGMISMATCH) 385 goto out; 386 printf("rpc failed, status=%d\n", error); 387 continue; 388 } 389 390 goto gotreply; /* break two levels */ 391 392 } /* while secs */ 393 } /* forever send/receive */ 394 395 error = ETIMEDOUT; 396 goto out; 397 398 gotreply: 399 400 /* 401 * Get RPC reply header into first mbuf, 402 * get its length, then strip it off. 403 */ 404 len = sizeof(*reply); 405 if (m->m_len < len) { 406 m = m_pullup(m, len); 407 if (m == NULL) { 408 error = ENOBUFS; 409 goto out; 410 } 411 } 412 reply = mtod(m, struct rpc_reply *); 413 if (reply->rp_auth.authtype != 0) { 414 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen); 415 len = (len + 3) & ~3; /* XXX? */ 416 } 417 m_adj(m, len); 418 419 /* result */ 420 *data = m; 421 if (from_p) { 422 *from_p = from; 423 from = NULL; 424 } 425 426 out: 427 if (nam) m_freem(nam); 428 if (mhead) m_freem(mhead); 429 if (from) m_freem(from); 430 soclose(so); 431 return error; 432 } 433 434 /* 435 * eXternal Data Representation routines. 436 * (but with non-standard args...) 437 */ 438 439 /* 440 * String representation for RPC. 441 */ 442 struct xdr_string { 443 u_int32_t len; /* length without null or padding */ 444 char data[4]; /* data (longer, of course) */ 445 /* data is padded to a long-word boundary */ 446 }; 447 448 struct mbuf * 449 xdr_string_encode(str, len) 450 char *str; 451 int len; 452 { 453 struct mbuf *m; 454 struct xdr_string *xs; 455 int dlen; /* padded string length */ 456 int mlen; /* message length */ 457 458 dlen = (len + 3) & ~3; 459 mlen = dlen + 4; 460 461 if (mlen > MCLBYTES) /* If too big, we just can't do it. */ 462 return (NULL); 463 464 m = m_get(M_WAIT, MT_DATA); 465 if (mlen > MLEN) { 466 MCLGET(m, M_WAIT); 467 if ((m->m_flags & M_EXT) == 0) { 468 (void) m_free(m); /* There can be only one. */ 469 return (NULL); 470 } 471 } 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 } 478 479 struct mbuf * 480 xdr_string_decode(m, str, len_p) 481 struct mbuf *m; 482 char *str; 483 int *len_p; /* bufsize - 1 */ 484 { 485 struct xdr_string *xs; 486 int mlen; /* message length */ 487 int slen; /* string length */ 488 489 if (m->m_len < 4) { 490 m = m_pullup(m, 4); 491 if (m == NULL) 492 return (NULL); 493 } 494 xs = mtod(m, struct xdr_string *); 495 slen = fxdr_unsigned(u_int32_t, xs->len); 496 mlen = 4 + ((slen + 3) & ~3); 497 498 if (slen > *len_p) 499 slen = *len_p; 500 m_copydata(m, 4, slen, str); 501 m_adj(m, mlen); 502 503 str[slen] = '\0'; 504 *len_p = slen; 505 506 return (m); 507 } 508 509 510 /* 511 * Inet address in RPC messages 512 * (Note, really four ints, NOT chars. Blech.) 513 */ 514 struct xdr_inaddr { 515 u_int32_t atype; 516 u_int32_t addr[4]; 517 }; 518 519 struct mbuf * 520 xdr_inaddr_encode(ia) 521 struct in_addr *ia; /* already in network order */ 522 { 523 struct mbuf *m; 524 struct xdr_inaddr *xi; 525 u_int8_t *cp; 526 u_int32_t *ip; 527 528 m = m_get(M_WAIT, MT_DATA); 529 xi = mtod(m, struct xdr_inaddr *); 530 m->m_len = sizeof(*xi); 531 xi->atype = txdr_unsigned(1); 532 ip = xi->addr; 533 cp = (u_int8_t *)&ia->s_addr; 534 *ip++ = txdr_unsigned(*cp++); 535 *ip++ = txdr_unsigned(*cp++); 536 *ip++ = txdr_unsigned(*cp++); 537 *ip++ = txdr_unsigned(*cp++); 538 539 return (m); 540 } 541 542 struct mbuf * 543 xdr_inaddr_decode(m, ia) 544 struct mbuf *m; 545 struct in_addr *ia; /* already in network order */ 546 { 547 struct xdr_inaddr *xi; 548 u_int8_t *cp; 549 u_int32_t *ip; 550 551 if (m->m_len < sizeof(*xi)) { 552 m = m_pullup(m, sizeof(*xi)); 553 if (m == NULL) 554 return (NULL); 555 } 556 xi = mtod(m, struct xdr_inaddr *); 557 if (xi->atype != txdr_unsigned(1)) { 558 ia->s_addr = INADDR_ANY; 559 goto out; 560 } 561 ip = xi->addr; 562 cp = (u_int8_t *)&ia->s_addr; 563 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 564 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 565 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 566 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 567 568 out: 569 m_adj(m, sizeof(*xi)); 570 return (m); 571 } 572