1 /* $OpenBSD: krpc_subr.c,v 1.20 2009/10/19 22:24:18 jsg 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(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp) 151 { 152 struct sdata { 153 u_int32_t prog; /* call program */ 154 u_int32_t vers; /* call version */ 155 u_int32_t proto; /* call protocol */ 156 u_int32_t port; /* call port (unused) */ 157 } *sdata; 158 struct rdata { 159 u_int16_t pad; 160 u_int16_t port; 161 } *rdata; 162 struct mbuf *m; 163 int error; 164 165 /* The portmapper port is fixed. */ 166 if (prog == PMAPPROG) { 167 *portp = htons(PMAPPORT); 168 return 0; 169 } 170 171 m = m_get(M_WAIT, MT_DATA); 172 sdata = mtod(m, struct sdata *); 173 m->m_len = sizeof(*sdata); 174 175 /* Do the RPC to get it. */ 176 sdata->prog = txdr_unsigned(prog); 177 sdata->vers = txdr_unsigned(vers); 178 sdata->proto = txdr_unsigned(IPPROTO_UDP); 179 sdata->port = 0; 180 181 sin->sin_port = htons(PMAPPORT); 182 error = krpc_call(sin, PMAPPROG, PMAPVERS, 183 PMAPPROC_GETPORT, &m, NULL, -1); 184 if (error) 185 return error; 186 187 if (m->m_len < sizeof(*rdata)) { 188 m = m_pullup(m, sizeof(*rdata)); 189 if (m == NULL) 190 return ENOBUFS; 191 } 192 rdata = mtod(m, struct rdata *); 193 *portp = rdata->port; 194 195 m_freem(m); 196 return 0; 197 } 198 199 /* 200 * Do a remote procedure call (RPC) and wait for its reply. 201 * If from_p is non-null, then we are doing broadcast, and 202 * the address from whence the response came is saved there. 203 * data: input/output 204 * from_p: output 205 */ 206 int 207 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, 208 struct mbuf **data, struct mbuf **from_p, int retries) 209 { 210 struct socket *so; 211 struct sockaddr_in *sin; 212 struct mbuf *m, *nam, *mhead, *from, *mopt; 213 struct rpc_call *call; 214 struct rpc_reply *reply; 215 struct uio auio; 216 int error, rcvflg, timo, secs, len; 217 static u_int32_t xid = 0; 218 int *ip; 219 struct timeval *tv; 220 221 /* 222 * Validate address family. 223 * Sorry, this is INET specific... 224 */ 225 if (sa->sin_family != AF_INET) 226 return (EAFNOSUPPORT); 227 228 /* Free at end if not null. */ 229 nam = mhead = NULL; 230 from = NULL; 231 232 /* 233 * Create socket and set its receive timeout. 234 */ 235 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0))) 236 goto out; 237 238 m = m_get(M_WAIT, MT_SOOPTS); 239 tv = mtod(m, struct timeval *); 240 m->m_len = sizeof(*tv); 241 tv->tv_sec = 1; 242 tv->tv_usec = 0; 243 if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m))) 244 goto out; 245 246 /* 247 * Enable broadcast if necessary. 248 */ 249 if (from_p) { 250 int32_t *on; 251 m = m_get(M_WAIT, MT_SOOPTS); 252 on = mtod(m, int32_t *); 253 m->m_len = sizeof(*on); 254 *on = 1; 255 if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m))) 256 goto out; 257 } 258 259 /* 260 * Bind the local endpoint to a reserved port, 261 * because some NFS servers refuse requests from 262 * non-reserved (non-privileged) ports. 263 */ 264 MGET(mopt, M_WAIT, MT_SOOPTS); 265 mopt->m_len = sizeof(int); 266 ip = mtod(mopt, int *); 267 *ip = IP_PORTRANGE_LOW; 268 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 269 if (error) 270 goto out; 271 272 MGET(m, M_WAIT, MT_SONAME); 273 sin = mtod(m, struct sockaddr_in *); 274 sin->sin_len = m->m_len = sizeof (struct sockaddr_in); 275 sin->sin_family = AF_INET; 276 sin->sin_addr.s_addr = INADDR_ANY; 277 sin->sin_port = htons(0); 278 error = sobind(so, m, &proc0); 279 m_freem(m); 280 if (error) { 281 printf("bind failed\n"); 282 goto out; 283 } 284 285 MGET(mopt, M_WAIT, MT_SOOPTS); 286 mopt->m_len = sizeof(int); 287 ip = mtod(mopt, int *); 288 *ip = IP_PORTRANGE_DEFAULT; 289 error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt); 290 if (error) 291 goto out; 292 293 /* 294 * Setup socket address for the server. 295 */ 296 nam = m_get(M_WAIT, MT_SONAME); 297 sin = mtod(nam, struct sockaddr_in *); 298 bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sin_len)); 299 300 /* 301 * Prepend RPC message header. 302 */ 303 mhead = m_gethdr(M_WAIT, MT_DATA); 304 mhead->m_next = *data; 305 call = mtod(mhead, struct rpc_call *); 306 mhead->m_len = sizeof(*call); 307 bzero((caddr_t)call, sizeof(*call)); 308 /* rpc_call part */ 309 xid = krpc_get_xid(); 310 call->rp_xid = txdr_unsigned(xid); 311 /* call->rp_direction = 0; */ 312 call->rp_rpcvers = txdr_unsigned(2); 313 call->rp_prog = txdr_unsigned(prog); 314 call->rp_vers = txdr_unsigned(vers); 315 call->rp_proc = txdr_unsigned(func); 316 /* rpc_auth part (auth_unix as root) */ 317 call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX); 318 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix)); 319 /* rpc_verf part (auth_null) */ 320 call->rpc_verf.authtype = 0; 321 call->rpc_verf.authlen = 0; 322 323 /* 324 * Setup packet header 325 */ 326 len = 0; 327 m = mhead; 328 while (m) { 329 len += m->m_len; 330 m = m->m_next; 331 } 332 mhead->m_pkthdr.len = len; 333 mhead->m_pkthdr.rcvif = NULL; 334 335 /* 336 * Send it, repeatedly, until a reply is received, 337 * but delay each re-send by an increasing amount. 338 * If the delay hits the maximum, start complaining. 339 */ 340 for (timo = 0; retries; retries--) { 341 /* Send RPC request (or re-send). */ 342 m = m_copym(mhead, 0, M_COPYALL, M_WAIT); 343 if (m == NULL) { 344 error = ENOBUFS; 345 goto out; 346 } 347 error = sosend(so, nam, NULL, m, NULL, 0); 348 if (error) { 349 printf("krpc_call: sosend: %d\n", error); 350 goto out; 351 } 352 m = NULL; 353 354 /* Determine new timeout. */ 355 if (timo < MAX_RESEND_DELAY) 356 timo++; 357 else 358 printf("RPC timeout for server %s (0x%x) prog %u\n", 359 inet_ntoa(sin->sin_addr), 360 ntohl(sin->sin_addr.s_addr), prog); 361 362 /* 363 * Wait for up to timo seconds for a reply. 364 * The socket receive timeout was set to 1 second. 365 */ 366 secs = timo; 367 while (secs > 0) { 368 if (from) { 369 m_freem(from); 370 from = NULL; 371 } 372 if (m) { 373 m_freem(m); 374 m = NULL; 375 } 376 auio.uio_resid = len = 1<<16; 377 auio.uio_procp = NULL; 378 rcvflg = 0; 379 error = soreceive(so, &from, &auio, &m, NULL, &rcvflg, 380 0); 381 if (error == EWOULDBLOCK) { 382 secs--; 383 continue; 384 } 385 if (error) 386 goto out; 387 len -= auio.uio_resid; 388 389 /* Does the reply contain at least a header? */ 390 if (len < MIN_REPLY_HDR) 391 continue; 392 if (m->m_len < MIN_REPLY_HDR) 393 continue; 394 reply = mtod(m, struct rpc_reply *); 395 396 /* Is it the right reply? */ 397 if (reply->rp_direction != txdr_unsigned(RPC_REPLY)) 398 continue; 399 400 if (reply->rp_xid != txdr_unsigned(xid)) 401 continue; 402 403 /* Was RPC accepted? (authorization OK) */ 404 if (reply->rp_astatus != 0) { 405 error = fxdr_unsigned(u_int32_t, reply->rp_errno); 406 printf("rpc denied, error=%d\n", error); 407 continue; 408 } 409 410 /* Did the call succeed? */ 411 if (reply->rp_status != 0) { 412 error = fxdr_unsigned(u_int32_t, reply->rp_status); 413 printf("rpc denied, status=%d\n", error); 414 continue; 415 } 416 417 goto gotreply; /* break two levels */ 418 419 } /* while secs */ 420 } /* forever send/receive */ 421 422 error = ETIMEDOUT; 423 goto out; 424 425 gotreply: 426 427 /* 428 * Get RPC reply header into first mbuf, 429 * get its length, then strip it off. 430 */ 431 len = sizeof(*reply); 432 if (m->m_len < len) { 433 m = m_pullup(m, len); 434 if (m == NULL) { 435 error = ENOBUFS; 436 goto out; 437 } 438 } 439 reply = mtod(m, struct rpc_reply *); 440 if (reply->rp_auth.authtype != 0) { 441 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen); 442 len = (len + 3) & ~3; /* XXX? */ 443 } 444 m_adj(m, len); 445 446 /* result */ 447 *data = m; 448 if (from_p && error == 0) { 449 *from_p = from; 450 from = NULL; 451 } 452 453 out: 454 if (nam) m_freem(nam); 455 if (mhead) m_freem(mhead); 456 if (from) m_freem(from); 457 soclose(so); 458 return error; 459 } 460 461 /* 462 * eXternal Data Representation routines. 463 * (but with non-standard args...) 464 */ 465 466 /* 467 * String representation for RPC. 468 */ 469 struct xdr_string { 470 u_int32_t len; /* length without null or padding */ 471 char data[4]; /* data (longer, of course) */ 472 /* data is padded to a long-word boundary */ 473 }; 474 475 struct mbuf * 476 xdr_string_encode(char *str, int len) 477 { 478 struct mbuf *m; 479 struct xdr_string *xs; 480 int dlen; /* padded string length */ 481 int mlen; /* message length */ 482 483 dlen = (len + 3) & ~3; 484 mlen = dlen + 4; 485 486 if (mlen > MCLBYTES) /* If too big, we just can't do it. */ 487 return (NULL); 488 489 m = m_get(M_WAIT, MT_DATA); 490 if (mlen > MLEN) { 491 MCLGET(m, M_WAIT); 492 if ((m->m_flags & M_EXT) == 0) { 493 (void) m_free(m); /* There can be only one. */ 494 return (NULL); 495 } 496 } 497 xs = mtod(m, struct xdr_string *); 498 m->m_len = mlen; 499 xs->len = txdr_unsigned(len); 500 bcopy(str, xs->data, len); 501 return (m); 502 } 503 504 struct mbuf * 505 xdr_string_decode(struct mbuf *m, char *str, int *len_p) 506 { 507 struct xdr_string *xs; 508 int mlen; /* message length */ 509 int slen; /* string length */ 510 511 if (m->m_len < 4) { 512 m = m_pullup(m, 4); 513 if (m == NULL) 514 return (NULL); 515 } 516 xs = mtod(m, struct xdr_string *); 517 slen = fxdr_unsigned(u_int32_t, xs->len); 518 mlen = 4 + ((slen + 3) & ~3); 519 520 if (slen > *len_p) 521 slen = *len_p; 522 if (slen > m->m_pkthdr.len) { 523 m_freem(m); 524 return (NULL); 525 } 526 m_copydata(m, 4, slen, str); 527 m_adj(m, mlen); 528 529 str[slen] = '\0'; 530 *len_p = slen; 531 532 return (m); 533 } 534 535 536 /* 537 * Inet address in RPC messages 538 * (Note, really four ints, NOT chars. Blech.) 539 */ 540 struct xdr_inaddr { 541 u_int32_t atype; 542 u_int32_t addr[4]; 543 }; 544 545 struct mbuf * 546 xdr_inaddr_encode(struct in_addr *ia) 547 { 548 struct mbuf *m; 549 struct xdr_inaddr *xi; 550 u_int8_t *cp; 551 u_int32_t *ip; 552 553 m = m_get(M_WAIT, MT_DATA); 554 xi = mtod(m, struct xdr_inaddr *); 555 m->m_len = sizeof(*xi); 556 xi->atype = txdr_unsigned(1); 557 ip = xi->addr; 558 cp = (u_int8_t *)&ia->s_addr; 559 *ip++ = txdr_unsigned(*cp++); 560 *ip++ = txdr_unsigned(*cp++); 561 *ip++ = txdr_unsigned(*cp++); 562 *ip++ = txdr_unsigned(*cp++); 563 564 return (m); 565 } 566 567 struct mbuf * 568 xdr_inaddr_decode(struct mbuf *m, struct in_addr *ia) 569 { 570 struct xdr_inaddr *xi; 571 u_int8_t *cp; 572 u_int32_t *ip; 573 574 if (m->m_len < sizeof(*xi)) { 575 m = m_pullup(m, sizeof(*xi)); 576 if (m == NULL) 577 return (NULL); 578 } 579 xi = mtod(m, struct xdr_inaddr *); 580 if (xi->atype != txdr_unsigned(1)) { 581 ia->s_addr = INADDR_ANY; 582 goto out; 583 } 584 ip = xi->addr; 585 cp = (u_int8_t *)&ia->s_addr; 586 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 587 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 588 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 589 *cp++ = fxdr_unsigned(u_int8_t, *ip++); 590 591 out: 592 m_adj(m, sizeof(*xi)); 593 return (m); 594 } 595