1 /* NetBSD: krpc_subr.c,v 1.12.4.1 1996/06/07 00:52:26 cgd 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/cdefs.h> 46 /* __FBSDID("FreeBSD: head/sys/nfs/krpc_subr.c 248207 2013-03-12 13:42:47Z glebius "); */ 47 __RCSID("$NetBSD: krpc_subr.c,v 1.1.1.1 2013/09/30 07:19:32 dholland Exp $"); 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/jail.h> 52 #include <sys/malloc.h> 53 #include <sys/mbuf.h> 54 #include <sys/proc.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/uio.h> 58 59 #include <net/if.h> 60 #include <net/vnet.h> 61 62 #include <netinet/in.h> 63 64 #include <rpc/types.h> 65 #include <rpc/auth.h> 66 #include <rpc/rpc_msg.h> 67 #include <nfs/krpc.h> 68 #include <nfs/xdr_subs.h> 69 70 /* 71 * Kernel support for Sun RPC 72 * 73 * Used currently for bootstrapping in nfs diskless configurations. 74 */ 75 76 /* 77 * Generic RPC headers 78 */ 79 80 struct auth_info { 81 u_int32_t authtype; /* auth type */ 82 u_int32_t authlen; /* auth length */ 83 }; 84 85 struct auth_unix { 86 int32_t ua_time; 87 int32_t ua_hostname; /* null */ 88 int32_t ua_uid; 89 int32_t ua_gid; 90 int32_t ua_gidlist; /* null */ 91 }; 92 93 struct krpc_call { 94 u_int32_t rp_xid; /* request transaction id */ 95 int32_t rp_direction; /* call direction (0) */ 96 u_int32_t rp_rpcvers; /* rpc version (2) */ 97 u_int32_t rp_prog; /* program */ 98 u_int32_t rp_vers; /* version */ 99 u_int32_t rp_proc; /* procedure */ 100 struct auth_info rpc_auth; 101 struct auth_unix rpc_unix; 102 struct auth_info rpc_verf; 103 }; 104 105 struct krpc_reply { 106 u_int32_t rp_xid; /* request transaction id */ 107 int32_t rp_direction; /* call direction (1) */ 108 int32_t rp_astatus; /* accept status (0: accepted) */ 109 union { 110 u_int32_t rpu_errno; 111 struct { 112 struct auth_info rok_auth; 113 u_int32_t rok_status; 114 } rpu_rok; 115 } rp_u; 116 }; 117 #define rp_errno rp_u.rpu_errno 118 #define rp_auth rp_u.rpu_rok.rok_auth 119 #define rp_status rp_u.rpu_rok.rok_status 120 121 #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */ 122 123 /* 124 * What is the longest we will wait before re-sending a request? 125 * Note this is also the frequency of "RPC timeout" messages. 126 * The re-send loop count sup linearly to this maximum, so the 127 * first complaint will happen after (1+2+3+4+5)=15 seconds. 128 */ 129 #define MAX_RESEND_DELAY 5 /* seconds */ 130 131 /* 132 * Call portmap to lookup a port number for a particular rpc program 133 * Returns non-zero error on failure. 134 */ 135 int 136 krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp, 137 struct thread *td) 138 { 139 struct sdata { 140 u_int32_t prog; /* call program */ 141 u_int32_t vers; /* call version */ 142 u_int32_t proto; /* call protocol */ 143 u_int32_t port; /* call port (unused) */ 144 } *sdata; 145 struct rdata { 146 u_int16_t pad; 147 u_int16_t port; 148 } *rdata; 149 struct mbuf *m; 150 int error; 151 152 /* The portmapper port is fixed. */ 153 if (prog == PMAPPROG) { 154 *portp = htons(PMAPPORT); 155 return 0; 156 } 157 158 m = m_get(M_WAITOK, MT_DATA); 159 sdata = mtod(m, struct sdata *); 160 m->m_len = sizeof(*sdata); 161 162 /* Do the RPC to get it. */ 163 sdata->prog = txdr_unsigned(prog); 164 sdata->vers = txdr_unsigned(vers); 165 sdata->proto = txdr_unsigned(IPPROTO_UDP); 166 sdata->port = 0; 167 168 sin->sin_port = htons(PMAPPORT); 169 error = krpc_call(sin, PMAPPROG, PMAPVERS, 170 PMAPPROC_GETPORT, &m, NULL, td); 171 if (error) 172 return error; 173 174 if (m->m_len < sizeof(*rdata)) { 175 m = m_pullup(m, sizeof(*rdata)); 176 if (m == NULL) 177 return ENOBUFS; 178 } 179 rdata = mtod(m, struct rdata *); 180 *portp = rdata->port; 181 182 m_freem(m); 183 return 0; 184 } 185 186 /* 187 * Do a remote procedure call (RPC) and wait for its reply. 188 * If from_p is non-null, then we are doing broadcast, and 189 * the address from whence the response came is saved there. 190 */ 191 int 192 krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, 193 struct mbuf **data, struct sockaddr **from_p, struct thread *td) 194 { 195 struct socket *so; 196 struct sockaddr_in *sin, ssin; 197 struct sockaddr *from; 198 struct mbuf *m, *nam, *mhead; 199 struct krpc_call *call; 200 struct krpc_reply *reply; 201 struct sockopt sopt; 202 struct timeval tv; 203 struct uio auio; 204 int error, rcvflg, timo, secs, len; 205 static u_int32_t xid = ~0xFF; 206 u_int16_t tport; 207 u_int32_t saddr; 208 209 /* 210 * Validate address family. 211 * Sorry, this is INET specific... 212 */ 213 if (sa->sin_family != AF_INET) 214 return (EAFNOSUPPORT); 215 216 /* Free at end if not null. */ 217 nam = mhead = NULL; 218 from = NULL; 219 220 /* 221 * Create socket and set its recieve timeout. 222 */ 223 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td))) 224 goto out; 225 226 tv.tv_sec = 1; 227 tv.tv_usec = 0; 228 bzero(&sopt, sizeof sopt); 229 sopt.sopt_dir = SOPT_SET; 230 sopt.sopt_level = SOL_SOCKET; 231 sopt.sopt_name = SO_RCVTIMEO; 232 sopt.sopt_val = &tv; 233 sopt.sopt_valsize = sizeof tv; 234 235 if ((error = sosetopt(so, &sopt)) != 0) 236 goto out; 237 238 /* 239 * Enable broadcast if necessary. 240 */ 241 if (from_p) { 242 int on = 1; 243 sopt.sopt_name = SO_BROADCAST; 244 sopt.sopt_val = &on; 245 sopt.sopt_valsize = sizeof on; 246 if ((error = sosetopt(so, &sopt)) != 0) 247 goto out; 248 } 249 250 /* 251 * Bind the local endpoint to a reserved port, 252 * because some NFS servers refuse requests from 253 * non-reserved (non-privileged) ports. 254 */ 255 sin = &ssin; 256 bzero(sin, sizeof *sin); 257 sin->sin_len = sizeof(*sin); 258 sin->sin_family = AF_INET; 259 sin->sin_addr.s_addr = INADDR_ANY; 260 tport = IPPORT_RESERVED; 261 do { 262 tport--; 263 sin->sin_port = htons(tport); 264 error = sobind(so, (struct sockaddr *)sin, td); 265 } while (error == EADDRINUSE && 266 tport > IPPORT_RESERVED / 2); 267 if (error) { 268 printf("bind failed\n"); 269 goto out; 270 } 271 272 /* 273 * Setup socket address for the server. 274 */ 275 276 /* 277 * Prepend RPC message header. 278 */ 279 mhead = m_gethdr(M_WAITOK, MT_DATA); 280 mhead->m_next = *data; 281 call = mtod(mhead, struct krpc_call *); 282 mhead->m_len = sizeof(*call); 283 bzero((caddr_t)call, sizeof(*call)); 284 /* rpc_call part */ 285 xid++; 286 call->rp_xid = txdr_unsigned(xid); 287 /* call->rp_direction = 0; */ 288 call->rp_rpcvers = txdr_unsigned(2); 289 call->rp_prog = txdr_unsigned(prog); 290 call->rp_vers = txdr_unsigned(vers); 291 call->rp_proc = txdr_unsigned(func); 292 /* rpc_auth part (auth_unix as root) */ 293 call->rpc_auth.authtype = txdr_unsigned(AUTH_UNIX); 294 call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix)); 295 /* rpc_verf part (auth_null) */ 296 call->rpc_verf.authtype = 0; 297 call->rpc_verf.authlen = 0; 298 299 /* 300 * Setup packet header 301 */ 302 m_fixhdr(mhead); 303 mhead->m_pkthdr.rcvif = NULL; 304 305 /* 306 * Send it, repeatedly, until a reply is received, 307 * but delay each re-send by an increasing amount. 308 * If the delay hits the maximum, start complaining. 309 */ 310 timo = 0; 311 for (;;) { 312 /* Send RPC request (or re-send). */ 313 m = m_copym(mhead, 0, M_COPYALL, M_WAITOK); 314 error = sosend(so, (struct sockaddr *)sa, NULL, m, 315 NULL, 0, td); 316 if (error) { 317 printf("krpc_call: sosend: %d\n", error); 318 goto out; 319 } 320 m = NULL; 321 322 /* Determine new timeout. */ 323 if (timo < MAX_RESEND_DELAY) 324 timo++; 325 else { 326 saddr = ntohl(sa->sin_addr.s_addr); 327 printf("RPC timeout for server %d.%d.%d.%d\n", 328 (saddr >> 24) & 255, 329 (saddr >> 16) & 255, 330 (saddr >> 8) & 255, 331 saddr & 255); 332 } 333 334 /* 335 * Wait for up to timo seconds for a reply. 336 * The socket receive timeout was set to 1 second. 337 */ 338 secs = timo; 339 while (secs > 0) { 340 if (from) { 341 free(from, M_SONAME); 342 from = NULL; 343 } 344 if (m) { 345 m_freem(m); 346 m = NULL; 347 } 348 bzero(&auio, sizeof(auio)); 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 krpc_reply *); 366 367 /* Is it the right reply? */ 368 if (reply->rp_direction != txdr_unsigned(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 == PROG_MISMATCH) { 385 error = EBADRPC; 386 goto out; 387 } 388 printf("rpc denied, status=%d\n", error); 389 continue; 390 } 391 392 goto gotreply; /* break two levels */ 393 394 } /* while secs */ 395 } /* forever send/receive */ 396 397 error = ETIMEDOUT; 398 goto out; 399 400 gotreply: 401 402 /* 403 * Get RPC reply header into first mbuf, 404 * get its length, then strip it off. 405 */ 406 len = sizeof(*reply); 407 if (m->m_len < len) { 408 m = m_pullup(m, len); 409 if (m == NULL) { 410 error = ENOBUFS; 411 goto out; 412 } 413 } 414 reply = mtod(m, struct krpc_reply *); 415 if (reply->rp_auth.authtype != 0) { 416 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen); 417 len = (len + 3) & ~3; /* XXX? */ 418 } 419 m_adj(m, len); 420 421 /* result */ 422 *data = m; 423 if (from_p) { 424 *from_p = from; 425 from = NULL; 426 } 427 428 out: 429 if (mhead) m_freem(mhead); 430 if (from) free(from, M_SONAME); 431 soclose(so); 432 return error; 433 } 434 435 /* 436 * eXternal Data Representation routines. 437 * (but with non-standard args...) 438 */ 439 440 /* 441 * String representation for RPC. 442 */ 443 struct xdr_string { 444 u_int32_t len; /* length without null or padding */ 445 char data[4]; /* data (longer, of course) */ 446 /* data is padded to a long-word boundary */ 447 }; 448 449 struct mbuf * 450 xdr_string_encode(char *str, int len) 451 { 452 struct mbuf *m; 453 struct xdr_string *xs; 454 int dlen; /* padded string length */ 455 int mlen; /* message length */ 456 457 dlen = (len + 3) & ~3; 458 mlen = dlen + 4; 459 460 if (mlen > MCLBYTES) /* If too big, we just can't do it. */ 461 return (NULL); 462 463 m = m_get2(mlen, M_WAITOK, MT_DATA, 0); 464 xs = mtod(m, struct xdr_string *); 465 m->m_len = mlen; 466 xs->len = txdr_unsigned(len); 467 bcopy(str, xs->data, len); 468 return (m); 469 } 470