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