1 /* $OpenBSD: krpc_subr.c,v 1.19 2009/02/22 07:47:22 otto Exp $ */ 2 /* $NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1995 Gordon Ross, Adam Glass 6 * Copyright (c) 1992 Regents of the University of California. 7 * All rights reserved. 8 * 9 * This software was developed by the Computer Systems Engineering group 10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 11 * contributed to Berkeley. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Lawrence Berkeley Laboratory and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * partially based on: 42 * libnetboot/rpc.c 43 * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL) 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/conf.h> 49 #include <sys/ioctl.h> 50 #include <sys/proc.h> 51 #include <sys/mount.h> 52 #include <sys/mbuf.h> 53 #include <sys/reboot.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 57 #include <net/if.h> 58 #include <netinet/in.h> 59 60 #include <nfs/rpcv2.h> 61 #include <nfs/krpc.h> 62 #include <nfs/xdr_subs.h> 63 #include <dev/rndvar.h> 64 #include <crypto/idgen.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 76 struct auth_info { 77 u_int32_t authtype; /* auth type */ 78 u_int32_t authlen; /* auth length */ 79 }; 80 81 struct 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 89 struct rpc_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 101 struct rpc_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 u_int32_t krpc_get_xid(void); 120 121 /* 122 * Return an unpredictable XID. 123 */ 124 u_int32_t 125 krpc_get_xid(void) 126 { 127 static struct idgen32_ctx krpc_xid_ctx; 128 static int called = 0; 129 130 if (!called) { 131 called = 1; 132 idgen32_init(&krpc_xid_ctx); 133 } 134 return idgen32(&krpc_xid_ctx); 135 } 136 137 /* 138 * What is the longest we will wait before re-sending a request? 139 * Note this is also the frequency of "RPC timeout" messages. 140 * The re-send loop count sup linearly to this maximum, so the 141 * first complaint will happen after (1+2+3+4+5)=15 seconds. 142 */ 143 #define MAX_RESEND_DELAY 5 /* seconds */ 144 145 /* 146 * Call portmap to lookup a port number for a particular rpc program 147 * Returns non-zero error on failure. 148 */ 149 int 150 krpc_portmap(sin, prog, vers, portp) 151 struct sockaddr_in *sin; /* server address */ 152 u_int prog, vers; /* host order */ 153 u_int16_t *portp; /* network order */ 154 { 155 struct sdata { 156 u_int32_t prog; /* call program */ 157 u_int32_t vers; /* call version */ 158 u_int32_t proto; /* call protocol */ 159 u_int32_t port; /* call port (unused) */ 160 } *sdata; 161 struct rdata { 162 u_int16_t pad; 163 u_int16_t port; 164 } *rdata; 165 struct mbuf *m; 166 int error; 167 168 /* The portmapper port is fixed. */ 169 if (prog == PMAPPROG) { 170 *portp = htons(PMAPPORT); 171 return 0; 172 } 173 174 m = m_get(M_WAIT, MT_DATA); 175 sdata = mtod(m, struct sdata *); 176 m->m_len = sizeof(*sdata); 177 178 /* Do the RPC to get it. */ 179 sdata->prog = txdr_unsigned(prog); 180 sdata->vers = txdr_unsigned(vers); 181 sdata->proto = txdr_unsigned(IPPROTO_UDP); 182 sdata->port = 0; 183 184 sin->sin_port = htons(PMAPPORT); 185 error = krpc_call(sin, PMAPPROG, PMAPVERS, 186 PMAPPROC_GETPORT, &m, NULL, -1); 187 if (error) 188 return error; 189 190 if (m->m_len < sizeof(*rdata)) { 191 m = m_pullup(m, sizeof(*rdata)); 192 if (m == NULL) 193 return ENOBUFS; 194 } 195 rdata = mtod(m, struct rdata *); 196 *portp = rdata->port; 197 198 m_freem(m); 199 return 0; 200 } 201 202 /* 203 * Do a remote procedure call (RPC) and wait for its reply. 204 * If from_p is non-null, then we are doing broadcast, and 205 * the address from whence the response came is saved there. 206 */ 207 int 208 krpc_call(sa, prog, vers, func, data, from_p, retries) 209 struct sockaddr_in *sa; 210 u_int prog, vers, func; 211 struct mbuf **data; /* input/output */ 212 struct mbuf **from_p; /* output */ 213 int retries; 214 { 215 struct socket *so; 216 struct sockaddr_in *sin; 217 struct mbuf *m, *nam, *mhead, *from, *mopt; 218 struct rpc_call *call; 219 struct rpc_reply *reply; 220 struct uio auio; 221 int error, rcvflg, timo, secs, len; 222 static u_int32_t xid = 0; 223 int *ip; 224 struct timeval *tv; 225 226 /* 227 * Validate address family. 228 * Sorry, this is INET specific... 229 */ 230 if (sa->sin_family != AF_INET) 231 return (EAFNOSUPPORT); 232 233 /* Free at end if not null. */ 234 nam = mhead = NULL; 235 from = NULL; 236 237 /* 238 * Create socket and set its receive timeout. 239 */ 240 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0))) 241 goto out; 242 243 m = m_get(M_WAIT, MT_SOOPTS); 244 tv = mtod(m, struct timeval *); 245 m->m_len = sizeof(*tv); 246 tv->tv_sec = 1; 247 tv->tv_usec = 0; 248 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m))) 249 goto out; 250 251 /* 252 * Enable broadcast if necessary. 253 */ 254 if (from_p) { 255 int32_t *on; 256 m = m_get(M_WAIT, MT_SOOPTS); 257 on = mtod(m, int32_t *); 258 m->m_len = sizeof(*on); 259 *on = 1; 260 if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m))) 261 goto out; 262 } 263 264 /* 265 * Bind the local endpoint to a reserved port, 266 * because some NFS servers refuse requests from 267 * non-reserved (non-privileged) ports. 268 */ 269 MGET(mopt, M_WAIT, MT_SOOPTS); 270 mopt->m_len = sizeof(int); 271 ip = mtod(mopt, int *); 272 *ip = IP_PORTRANGE_LOW; 273 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 274 if (error) 275 goto out; 276 277 MGET(m, M_WAIT, MT_SONAME); 278 sin = mtod(m, struct sockaddr_in *); 279 sin->sin_len = m->m_len = sizeof (struct sockaddr_in); 280 sin->sin_family = AF_INET; 281 sin->sin_addr.s_addr = INADDR_ANY; 282 sin->sin_port = htons(0); 283 error = sobind(so, m, &proc0); 284 m_freem(m); 285 if (error) { 286 printf("bind failed\n"); 287 goto out; 288 } 289 290 MGET(mopt, M_WAIT, MT_SOOPTS); 291 mopt->m_len = sizeof(int); 292 ip = mtod(mopt, int *); 293 *ip = IP_PORTRANGE_DEFAULT; 294 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 295 if (error) 296 goto out; 297 298 /* 299 * Setup socket address for the server. 300 */ 301 nam = m_get(M_WAIT, MT_SONAME); 302 sin = mtod(nam, struct sockaddr_in *); 303 bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sin_len)); 304 305 /* 306 * Prepend RPC message header. 307 */ 308 mhead = m_gethdr(M_WAIT, MT_DATA); 309 mhead->m_next = *data; 310 call = mtod(mhead, struct rpc_call *); 311 mhead->m_len = sizeof(*call); 312 bzero((caddr_t)call, sizeof(*call)); 313 /* rpc_call part */ 314 xid = krpc_get_xid(); 315 call->rp_xid = txdr_unsigned(xid); 316 /* call->rp_direction = 0; */ 317 call->rp_rpcvers = txdr_unsigned(2); 318 call->rp_prog = txdr_unsigned(prog); 319 call->rp_vers = txdr_unsigned(vers); 320 call->rp_proc = txdr_unsigned(func); 321 /* rpc_auth part (auth_unix as root) */ 322 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX); 323 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix)); 324 /* rpc_verf part (auth_null) */ 325 call->rpc_verf.authtype = 0; 326 call->rpc_verf.authlen = 0; 327 328 /* 329 * Setup packet header 330 */ 331 len = 0; 332 m = mhead; 333 while (m) { 334 len += m->m_len; 335 m = m->m_next; 336 } 337 mhead->m_pkthdr.len = len; 338 mhead->m_pkthdr.rcvif = NULL; 339 340 /* 341 * Send it, repeatedly, until a reply is received, 342 * but delay each re-send by an increasing amount. 343 * If the delay hits the maximum, start complaining. 344 */ 345 for (timo = 0; retries; retries--) { 346 /* Send RPC request (or re-send). */ 347 m = m_copym(mhead, 0, M_COPYALL, M_WAIT); 348 if (m == NULL) { 349 error = ENOBUFS; 350 goto out; 351 } 352 error = sosend(so, nam, NULL, m, NULL, 0); 353 if (error) { 354 printf("krpc_call: sosend: %d\n", error); 355 goto out; 356 } 357 m = NULL; 358 359 /* Determine new timeout. */ 360 if (timo < MAX_RESEND_DELAY) 361 timo++; 362 else 363 printf("RPC timeout for server %s (0x%x) prog %u\n", 364 inet_ntoa(sin->sin_addr), 365 ntohl(sin->sin_addr.s_addr), prog); 366 367 /* 368 * Wait for up to timo seconds for a reply. 369 * The socket receive timeout was set to 1 second. 370 */ 371 secs = timo; 372 while (secs > 0) { 373 if (from) { 374 m_freem(from); 375 from = NULL; 376 } 377 if (m) { 378 m_freem(m); 379 m = NULL; 380 } 381 auio.uio_resid = len = 1<<16; 382 auio.uio_procp = NULL; 383 rcvflg = 0; 384 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg, 385 0); 386 if (error == EWOULDBLOCK) { 387 secs--; 388 continue; 389 } 390 if (error) 391 goto out; 392 len -= auio.uio_resid; 393 394 /* Does the reply contain at least a header? */ 395 if (len < MIN_REPLY_HDR) 396 continue; 397 if (m->m_len < MIN_REPLY_HDR) 398 continue; 399 reply = mtod(m, struct rpc_reply *); 400 401 /* Is it the right reply? */ 402 if (reply->rp_direction != txdr_unsigned(RPC_REPLY)) 403 continue; 404 405 if (reply->rp_xid != txdr_unsigned(xid)) 406 continue; 407 408 /* Was RPC accepted? (authorization OK) */ 409 if (reply->rp_astatus != 0) { 410 error = fxdr_unsigned(u_int32_t, reply->rp_errno); 411 printf("rpc denied, error=%d\n", error); 412 continue; 413 } 414 415 /* Did the call succeed? */ 416 if (reply->rp_status != 0) { 417 error = fxdr_unsigned(u_int32_t, reply->rp_status); 418 printf("rpc denied, status=%d\n", error); 419 continue; 420 } 421 422 goto gotreply; /* break two levels */ 423 424 } /* while secs */ 425 } /* forever send/receive */ 426 427 error = ETIMEDOUT; 428 goto out; 429 430 gotreply: 431 432 /* 433 * Get RPC reply header into first mbuf, 434 * get its length, then strip it off. 435 */ 436 len = sizeof(*reply); 437 if (m->m_len < len) { 438 m = m_pullup(m, len); 439 if (m == NULL) { 440 error = ENOBUFS; 441 goto out; 442 } 443 } 444 reply = mtod(m, struct rpc_reply *); 445 if (reply->rp_auth.authtype != 0) { 446 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen); 447 len = (len + 3) & ~3; /* XXX? */ 448 } 449 m_adj(m, len); 450 451 /* result */ 452 *data = m; 453 if (from_p && error == 0) { 454 *from_p = from; 455 from = NULL; 456 } 457 458 out: 459 if (nam) m_freem(nam); 460 if (mhead) m_freem(mhead); 461 if (from) m_freem(from); 462 soclose(so); 463 return error; 464 } 465 466 /* 467 * eXternal Data Representation routines. 468 * (but with non-standard args...) 469 */ 470 471 /* 472 * String representation for RPC. 473 */ 474 struct xdr_string { 475 u_int32_t len; /* length without null or padding */ 476 char data[4]; /* data (longer, of course) */ 477 /* data is padded to a long-word boundary */ 478 }; 479 480 struct mbuf * 481 xdr_string_encode(str, len) 482 char *str; 483 int len; 484 { 485 struct mbuf *m; 486 struct xdr_string *xs; 487 int dlen; /* padded string length */ 488 int mlen; /* message length */ 489 490 dlen = (len + 3) & ~3; 491 mlen = dlen + 4; 492 493 if (mlen > MCLBYTES) /* If too big, we just can't do it. */ 494 return (NULL); 495 496 m = m_get(M_WAIT, MT_DATA); 497 if (mlen > MLEN) { 498 MCLGET(m, M_WAIT); 499 if ((m->m_flags & M_EXT) == 0) { 500 (void) m_free(m); /* There can be only one. */ 501 return (NULL); 502 } 503 } 504 xs = mtod(m, struct xdr_string *); 505 m->m_len = mlen; 506 xs->len = txdr_unsigned(len); 507 bcopy(str, xs->data, len); 508 return (m); 509 } 510 511 struct mbuf * 512 xdr_string_decode(m, str, len_p) 513 struct mbuf *m; 514 char *str; 515 int *len_p; /* bufsize - 1 */ 516 { 517 struct xdr_string *xs; 518 int mlen; /* message length */ 519 int slen; /* string length */ 520 521 if (m->m_len < 4) { 522 m = m_pullup(m, 4); 523 if (m == NULL) 524 return (NULL); 525 } 526 xs = mtod(m, struct xdr_string *); 527 slen = fxdr_unsigned(u_int32_t, xs->len); 528 mlen = 4 + ((slen + 3) & ~3); 529 530 if (slen > *len_p) 531 slen = *len_p; 532 if (slen > m->m_pkthdr.len) { 533 m_freem(m); 534 return (NULL); 535 } 536 m_copydata(m, 4, slen, str); 537 m_adj(m, mlen); 538 539 str[slen] = '\0'; 540 *len_p = slen; 541 542 return (m); 543 } 544 545 546 /* 547 * Inet address in RPC messages 548 * (Note, really four ints, NOT chars. Blech.) 549 */ 550 struct xdr_inaddr { 551 u_int32_t atype; 552 u_int32_t addr[4]; 553 }; 554 555 struct mbuf * 556 xdr_inaddr_encode(ia) 557 struct in_addr *ia; /* already in network order */ 558 { 559 struct mbuf *m; 560 struct xdr_inaddr *xi; 561 u_int8_t *cp; 562 u_int32_t *ip; 563 564 m = m_get(M_WAIT, MT_DATA); 565 xi = mtod(m, struct xdr_inaddr *); 566 m->m_len = sizeof(*xi); 567 xi->atype = txdr_unsigned(1); 568 ip = xi->addr; 569 cp = (u_int8_t *)&ia->s_addr; 570 *ip++ = txdr_unsigned(*cp++); 571 *ip++ = txdr_unsigned(*cp++); 572 *ip++ = txdr_unsigned(*cp++); 573 *ip++ = txdr_unsigned(*cp++); 574 575 return (m); 576 } 577 578 struct mbuf * 579 xdr_inaddr_decode(m, ia) 580 struct mbuf *m; 581 struct in_addr *ia; /* already in network order */ 582 { 583 struct xdr_inaddr *xi; 584 u_int8_t *cp; 585 u_int32_t *ip; 586 587 if (m->m_len < sizeof(*xi)) { 588 m = m_pullup(m, sizeof(*xi)); 589 if (m == NULL) 590 return (NULL); 591 } 592 xi = mtod(m, struct xdr_inaddr *); 593 if (xi->atype != txdr_unsigned(1)) { 594 ia->s_addr = INADDR_ANY; 595 goto out; 596 } 597 ip = xi->addr; 598 cp = (u_int8_t *)&ia->s_addr; 599 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 600 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 601 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 602 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 603 604 out: 605 m_adj(m, sizeof(*xi)); 606 return (m); 607 } 608