1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2009, Sun Microsystems, Inc. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions are met: 11 * - Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * - Neither the name of Sun Microsystems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 34 */ 35 36 #include <sys/cdefs.h> 37 /* 38 * rpc_generic.c, Miscl routines for RPC. 39 * 40 */ 41 42 #include "opt_inet6.h" 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/module.h> 49 #include <sys/proc.h> 50 #include <sys/protosw.h> 51 #include <sys/sbuf.h> 52 #include <sys/systm.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/syslog.h> 56 57 #include <net/vnet.h> 58 59 #include <rpc/rpc.h> 60 #include <rpc/nettype.h> 61 #include <rpc/rpcsec_gss.h> 62 #include <rpc/rpcsec_tls.h> 63 64 #include <rpc/rpc_com.h> 65 #include <rpc/krpc.h> 66 67 #include <vm/vm.h> 68 #include <vm/pmap.h> 69 #include <vm/vm_param.h> 70 71 extern u_long sb_max_adj; /* not defined in socketvar.h */ 72 73 /* Provide an entry point hook for the rpcsec_gss module. */ 74 struct rpc_gss_entries rpc_gss_entries; 75 76 struct handle { 77 NCONF_HANDLE *nhandle; 78 int nflag; /* Whether NETPATH or NETCONFIG */ 79 int nettype; 80 }; 81 82 static const struct _rpcnettype { 83 const char *name; 84 const int type; 85 } _rpctypelist[] = { 86 { "netpath", _RPC_NETPATH }, 87 { "visible", _RPC_VISIBLE }, 88 { "circuit_v", _RPC_CIRCUIT_V }, 89 { "datagram_v", _RPC_DATAGRAM_V }, 90 { "circuit_n", _RPC_CIRCUIT_N }, 91 { "datagram_n", _RPC_DATAGRAM_N }, 92 { "tcp", _RPC_TCP }, 93 { "udp", _RPC_UDP }, 94 { 0, _RPC_NONE } 95 }; 96 97 struct netid_af { 98 const char *netid; 99 int af; 100 int protocol; 101 }; 102 103 static const struct netid_af na_cvt[] = { 104 { "udp", AF_INET, IPPROTO_UDP }, 105 { "tcp", AF_INET, IPPROTO_TCP }, 106 #ifdef INET6 107 { "udp6", AF_INET6, IPPROTO_UDP }, 108 { "tcp6", AF_INET6, IPPROTO_TCP }, 109 #endif 110 { "local", AF_LOCAL, 0 } 111 }; 112 113 struct rpc_createerr rpc_createerr; 114 115 /* 116 * Find the appropriate buffer size 117 */ 118 u_int 119 /*ARGSUSED*/ 120 __rpc_get_t_size(int af, int proto, int size) 121 { 122 int defsize; 123 124 switch (proto) { 125 case IPPROTO_TCP: 126 defsize = 64 * 1024; /* XXX */ 127 break; 128 case IPPROTO_UDP: 129 defsize = UDPMSGSIZE; 130 break; 131 default: 132 defsize = RPC_MAXDATASIZE; 133 break; 134 } 135 if (size == 0) 136 return defsize; 137 138 /* Check whether the value is within the upper max limit */ 139 return (size > sb_max_adj ? (u_int)sb_max_adj : (u_int)size); 140 } 141 142 /* 143 * Find the appropriate address buffer size 144 */ 145 u_int 146 __rpc_get_a_size(int af) 147 { 148 switch (af) { 149 case AF_INET: 150 return sizeof (struct sockaddr_in); 151 #ifdef INET6 152 case AF_INET6: 153 return sizeof (struct sockaddr_in6); 154 #endif 155 case AF_LOCAL: 156 return sizeof (struct sockaddr_un); 157 default: 158 break; 159 } 160 return ((u_int)RPC_MAXADDRSIZE); 161 } 162 163 #if 0 164 165 /* 166 * Used to ping the NULL procedure for clnt handle. 167 * Returns NULL if fails, else a non-NULL pointer. 168 */ 169 void * 170 rpc_nullproc(clnt) 171 CLIENT *clnt; 172 { 173 struct timeval TIMEOUT = {25, 0}; 174 175 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL, 176 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) { 177 return (NULL); 178 } 179 return ((void *) clnt); 180 } 181 182 #endif 183 184 int 185 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip) 186 { 187 int type, proto; 188 struct sockaddr *sa; 189 sa_family_t family; 190 struct sockopt opt; 191 int error; 192 193 CURVNET_SET(so->so_vnet); 194 error = so->so_proto->pr_sockaddr(so, &sa); 195 CURVNET_RESTORE(); 196 if (error) 197 return 0; 198 199 sip->si_alen = sa->sa_len; 200 family = sa->sa_family; 201 free(sa, M_SONAME); 202 203 opt.sopt_dir = SOPT_GET; 204 opt.sopt_level = SOL_SOCKET; 205 opt.sopt_name = SO_TYPE; 206 opt.sopt_val = &type; 207 opt.sopt_valsize = sizeof type; 208 opt.sopt_td = NULL; 209 error = sogetopt(so, &opt); 210 if (error) 211 return 0; 212 213 /* XXX */ 214 if (family != AF_LOCAL) { 215 if (type == SOCK_STREAM) 216 proto = IPPROTO_TCP; 217 else if (type == SOCK_DGRAM) 218 proto = IPPROTO_UDP; 219 else 220 return 0; 221 } else 222 proto = 0; 223 224 sip->si_af = family; 225 sip->si_proto = proto; 226 sip->si_socktype = type; 227 228 return 1; 229 } 230 231 /* 232 * Linear search, but the number of entries is small. 233 */ 234 int 235 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip) 236 { 237 int i; 238 239 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) 240 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || ( 241 strcmp(nconf->nc_netid, "unix") == 0 && 242 strcmp(na_cvt[i].netid, "local") == 0)) { 243 sip->si_af = na_cvt[i].af; 244 sip->si_proto = na_cvt[i].protocol; 245 sip->si_socktype = 246 __rpc_seman2socktype((int)nconf->nc_semantics); 247 if (sip->si_socktype == -1) 248 return 0; 249 sip->si_alen = __rpc_get_a_size(sip->si_af); 250 return 1; 251 } 252 253 return 0; 254 } 255 256 struct socket * 257 __rpc_nconf2socket(const struct netconfig *nconf) 258 { 259 struct __rpc_sockinfo si; 260 struct socket *so; 261 int error; 262 263 if (!__rpc_nconf2sockinfo(nconf, &si)) 264 return 0; 265 266 so = NULL; 267 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto, 268 curthread->td_ucred, curthread); 269 270 if (error) 271 return NULL; 272 else 273 return so; 274 } 275 276 char * 277 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf) 278 { 279 struct __rpc_sockinfo si; 280 281 if (!__rpc_nconf2sockinfo(nconf, &si)) 282 return NULL; 283 return __rpc_taddr2uaddr_af(si.si_af, nbuf); 284 } 285 286 struct netbuf * 287 uaddr2taddr(const struct netconfig *nconf, const char *uaddr) 288 { 289 struct __rpc_sockinfo si; 290 291 if (!__rpc_nconf2sockinfo(nconf, &si)) 292 return NULL; 293 return __rpc_uaddr2taddr_af(si.si_af, uaddr); 294 } 295 296 char * 297 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf) 298 { 299 char *ret; 300 struct sbuf sb; 301 struct sockaddr_in *sin; 302 struct sockaddr_un *sun; 303 char namebuf[INET_ADDRSTRLEN]; 304 #ifdef INET6 305 struct sockaddr_in6 *sin6; 306 char namebuf6[INET6_ADDRSTRLEN]; 307 #endif 308 uint16_t port; 309 310 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND); 311 312 switch (af) { 313 case AF_INET: 314 if (nbuf->len < sizeof(*sin)) 315 return NULL; 316 sin = nbuf->buf; 317 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf) 318 == NULL) 319 return NULL; 320 port = ntohs(sin->sin_port); 321 if (sbuf_printf(&sb, "%s.%u.%u", namebuf, 322 ((uint32_t)port) >> 8, 323 port & 0xff) < 0) 324 return NULL; 325 break; 326 #ifdef INET6 327 case AF_INET6: 328 if (nbuf->len < sizeof(*sin6)) 329 return NULL; 330 sin6 = nbuf->buf; 331 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6) 332 == NULL) 333 return NULL; 334 port = ntohs(sin6->sin6_port); 335 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6, 336 ((uint32_t)port) >> 8, 337 port & 0xff) < 0) 338 return NULL; 339 break; 340 #endif 341 case AF_LOCAL: 342 sun = nbuf->buf; 343 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len - 344 offsetof(struct sockaddr_un, sun_path)), 345 sun->sun_path) < 0) 346 return (NULL); 347 break; 348 default: 349 return NULL; 350 } 351 352 sbuf_finish(&sb); 353 ret = strdup(sbuf_data(&sb), M_RPC); 354 sbuf_delete(&sb); 355 356 return ret; 357 } 358 359 struct netbuf * 360 __rpc_uaddr2taddr_af(int af, const char *uaddr) 361 { 362 struct netbuf *ret = NULL; 363 char *addrstr, *p; 364 unsigned port, portlo, porthi; 365 struct sockaddr_in *sin; 366 #ifdef INET6 367 struct sockaddr_in6 *sin6; 368 #endif 369 struct sockaddr_un *sun; 370 371 port = 0; 372 sin = NULL; 373 374 if (uaddr == NULL) 375 return NULL; 376 377 addrstr = strdup(uaddr, M_RPC); 378 if (addrstr == NULL) 379 return NULL; 380 381 /* 382 * AF_LOCAL addresses are expected to be absolute 383 * pathnames, anything else will be AF_INET or AF_INET6. 384 */ 385 if (*addrstr != '/') { 386 p = strrchr(addrstr, '.'); 387 if (p == NULL) 388 goto out; 389 portlo = (unsigned)strtol(p + 1, NULL, 10); 390 *p = '\0'; 391 392 p = strrchr(addrstr, '.'); 393 if (p == NULL) 394 goto out; 395 porthi = (unsigned)strtol(p + 1, NULL, 10); 396 *p = '\0'; 397 port = (porthi << 8) | portlo; 398 } 399 400 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK); 401 402 switch (af) { 403 case AF_INET: 404 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC, 405 M_WAITOK); 406 memset(sin, 0, sizeof *sin); 407 sin->sin_family = AF_INET; 408 sin->sin_port = htons(port); 409 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) { 410 free(sin, M_RPC); 411 free(ret, M_RPC); 412 ret = NULL; 413 goto out; 414 } 415 sin->sin_len = ret->maxlen = ret->len = sizeof *sin; 416 ret->buf = sin; 417 break; 418 #ifdef INET6 419 case AF_INET6: 420 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC, 421 M_WAITOK); 422 memset(sin6, 0, sizeof *sin6); 423 sin6->sin6_family = AF_INET6; 424 sin6->sin6_port = htons(port); 425 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) { 426 free(sin6, M_RPC); 427 free(ret, M_RPC); 428 ret = NULL; 429 goto out; 430 } 431 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6; 432 ret->buf = sin6; 433 break; 434 #endif 435 case AF_LOCAL: 436 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC, 437 M_WAITOK); 438 memset(sun, 0, sizeof *sun); 439 sun->sun_family = AF_LOCAL; 440 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1); 441 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun); 442 ret->buf = sun; 443 break; 444 default: 445 break; 446 } 447 out: 448 free(addrstr, M_RPC); 449 return ret; 450 } 451 452 int 453 __rpc_seman2socktype(int semantics) 454 { 455 switch (semantics) { 456 case NC_TPI_CLTS: 457 return SOCK_DGRAM; 458 case NC_TPI_COTS_ORD: 459 return SOCK_STREAM; 460 case NC_TPI_RAW: 461 return SOCK_RAW; 462 default: 463 break; 464 } 465 466 return -1; 467 } 468 469 int 470 __rpc_socktype2seman(int socktype) 471 { 472 switch (socktype) { 473 case SOCK_DGRAM: 474 return NC_TPI_CLTS; 475 case SOCK_STREAM: 476 return NC_TPI_COTS_ORD; 477 case SOCK_RAW: 478 return NC_TPI_RAW; 479 default: 480 break; 481 } 482 483 return -1; 484 } 485 486 /* 487 * Returns the type of the network as defined in <rpc/nettype.h> 488 * If nettype is NULL, it defaults to NETPATH. 489 */ 490 static int 491 getnettype(const char *nettype) 492 { 493 int i; 494 495 if ((nettype == NULL) || (nettype[0] == 0)) { 496 return (_RPC_NETPATH); /* Default */ 497 } 498 499 #if 0 500 nettype = strlocase(nettype); 501 #endif 502 for (i = 0; _rpctypelist[i].name; i++) 503 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) { 504 return (_rpctypelist[i].type); 505 } 506 return (_rpctypelist[i].type); 507 } 508 509 /* 510 * For the given nettype (tcp or udp only), return the first structure found. 511 * This should be freed by calling freenetconfigent() 512 */ 513 struct netconfig * 514 __rpc_getconfip(const char *nettype) 515 { 516 char *netid; 517 static char *netid_tcp = (char *) NULL; 518 static char *netid_udp = (char *) NULL; 519 struct netconfig *dummy; 520 521 if (!netid_udp && !netid_tcp) { 522 struct netconfig *nconf; 523 void *confighandle; 524 525 if (!(confighandle = setnetconfig())) { 526 log(LOG_ERR, "rpc: failed to open " NETCONFIG); 527 return (NULL); 528 } 529 while ((nconf = getnetconfig(confighandle)) != NULL) { 530 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 531 if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 532 netid_tcp = strdup(nconf->nc_netid, 533 M_RPC); 534 } else 535 if (strcmp(nconf->nc_proto, NC_UDP) == 0) { 536 netid_udp = strdup(nconf->nc_netid, 537 M_RPC); 538 } 539 } 540 } 541 endnetconfig(confighandle); 542 } 543 if (strcmp(nettype, "udp") == 0) 544 netid = netid_udp; 545 else if (strcmp(nettype, "tcp") == 0) 546 netid = netid_tcp; 547 else { 548 return (NULL); 549 } 550 if ((netid == NULL) || (netid[0] == 0)) { 551 return (NULL); 552 } 553 dummy = getnetconfigent(netid); 554 return (dummy); 555 } 556 557 /* 558 * Returns the type of the nettype, which should then be used with 559 * __rpc_getconf(). 560 * 561 * For simplicity in the kernel, we don't support the NETPATH 562 * environment variable. We behave as userland would then NETPATH is 563 * unset, i.e. iterate over all visible entries in netconfig. 564 */ 565 void * 566 __rpc_setconf(const char *nettype) 567 { 568 struct handle *handle; 569 570 handle = (struct handle *) malloc(sizeof (struct handle), 571 M_RPC, M_WAITOK); 572 switch (handle->nettype = getnettype(nettype)) { 573 case _RPC_NETPATH: 574 case _RPC_CIRCUIT_N: 575 case _RPC_DATAGRAM_N: 576 if (!(handle->nhandle = setnetconfig())) 577 goto failed; 578 handle->nflag = TRUE; 579 break; 580 case _RPC_VISIBLE: 581 case _RPC_CIRCUIT_V: 582 case _RPC_DATAGRAM_V: 583 case _RPC_TCP: 584 case _RPC_UDP: 585 if (!(handle->nhandle = setnetconfig())) { 586 log(LOG_ERR, "rpc: failed to open " NETCONFIG); 587 goto failed; 588 } 589 handle->nflag = FALSE; 590 break; 591 default: 592 goto failed; 593 } 594 595 return (handle); 596 597 failed: 598 free(handle, M_RPC); 599 return (NULL); 600 } 601 602 /* 603 * Returns the next netconfig struct for the given "net" type. 604 * __rpc_setconf() should have been called previously. 605 */ 606 struct netconfig * 607 __rpc_getconf(void *vhandle) 608 { 609 struct handle *handle; 610 struct netconfig *nconf; 611 612 handle = (struct handle *)vhandle; 613 if (handle == NULL) { 614 return (NULL); 615 } 616 for (;;) { 617 if (handle->nflag) { 618 nconf = getnetconfig(handle->nhandle); 619 if (nconf && !(nconf->nc_flag & NC_VISIBLE)) 620 continue; 621 } else { 622 nconf = getnetconfig(handle->nhandle); 623 } 624 if (nconf == NULL) 625 break; 626 if ((nconf->nc_semantics != NC_TPI_CLTS) && 627 (nconf->nc_semantics != NC_TPI_COTS) && 628 (nconf->nc_semantics != NC_TPI_COTS_ORD)) 629 continue; 630 switch (handle->nettype) { 631 case _RPC_VISIBLE: 632 if (!(nconf->nc_flag & NC_VISIBLE)) 633 continue; 634 /* FALLTHROUGH */ 635 case _RPC_NETPATH: /* Be happy */ 636 break; 637 case _RPC_CIRCUIT_V: 638 if (!(nconf->nc_flag & NC_VISIBLE)) 639 continue; 640 /* FALLTHROUGH */ 641 case _RPC_CIRCUIT_N: 642 if ((nconf->nc_semantics != NC_TPI_COTS) && 643 (nconf->nc_semantics != NC_TPI_COTS_ORD)) 644 continue; 645 break; 646 case _RPC_DATAGRAM_V: 647 if (!(nconf->nc_flag & NC_VISIBLE)) 648 continue; 649 /* FALLTHROUGH */ 650 case _RPC_DATAGRAM_N: 651 if (nconf->nc_semantics != NC_TPI_CLTS) 652 continue; 653 break; 654 case _RPC_TCP: 655 if (((nconf->nc_semantics != NC_TPI_COTS) && 656 (nconf->nc_semantics != NC_TPI_COTS_ORD)) || 657 (strcmp(nconf->nc_protofmly, NC_INET) 658 #ifdef INET6 659 && strcmp(nconf->nc_protofmly, NC_INET6)) 660 #else 661 ) 662 #endif 663 || 664 strcmp(nconf->nc_proto, NC_TCP)) 665 continue; 666 break; 667 case _RPC_UDP: 668 if ((nconf->nc_semantics != NC_TPI_CLTS) || 669 (strcmp(nconf->nc_protofmly, NC_INET) 670 #ifdef INET6 671 && strcmp(nconf->nc_protofmly, NC_INET6)) 672 #else 673 ) 674 #endif 675 || 676 strcmp(nconf->nc_proto, NC_UDP)) 677 continue; 678 break; 679 } 680 break; 681 } 682 return (nconf); 683 } 684 685 void 686 __rpc_endconf(void *vhandle) 687 { 688 struct handle *handle; 689 690 handle = (struct handle *) vhandle; 691 if (handle == NULL) { 692 return; 693 } 694 endnetconfig(handle->nhandle); 695 free(handle, M_RPC); 696 } 697 698 int 699 __rpc_sockisbound(struct socket *so) 700 { 701 struct sockaddr *sa; 702 int error, bound; 703 704 CURVNET_SET(so->so_vnet); 705 error = so->so_proto->pr_sockaddr(so, &sa); 706 CURVNET_RESTORE(); 707 if (error) 708 return (0); 709 710 switch (sa->sa_family) { 711 case AF_INET: 712 bound = (((struct sockaddr_in *) sa)->sin_port != 0); 713 break; 714 #ifdef INET6 715 case AF_INET6: 716 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0); 717 break; 718 #endif 719 case AF_LOCAL: 720 /* XXX check this */ 721 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0'); 722 break; 723 default: 724 bound = FALSE; 725 break; 726 } 727 728 free(sa, M_SONAME); 729 730 return bound; 731 } 732 733 /* 734 * Implement XDR-style API for RPC call. 735 */ 736 enum clnt_stat 737 clnt_call_private( 738 CLIENT *cl, /* client handle */ 739 struct rpc_callextra *ext, /* call metadata */ 740 rpcproc_t proc, /* procedure number */ 741 xdrproc_t xargs, /* xdr routine for args */ 742 void *argsp, /* pointer to args */ 743 xdrproc_t xresults, /* xdr routine for results */ 744 void *resultsp, /* pointer to results */ 745 struct timeval utimeout) /* seconds to wait before giving up */ 746 { 747 XDR xdrs; 748 struct mbuf *mreq; 749 struct mbuf *mrep; 750 enum clnt_stat stat; 751 752 mreq = m_getcl(M_WAITOK, MT_DATA, 0); 753 754 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE); 755 if (!xargs(&xdrs, argsp)) { 756 m_freem(mreq); 757 return (RPC_CANTENCODEARGS); 758 } 759 XDR_DESTROY(&xdrs); 760 761 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout); 762 m_freem(mreq); 763 764 if (stat == RPC_SUCCESS) { 765 xdrmbuf_create(&xdrs, mrep, XDR_DECODE); 766 if (!xresults(&xdrs, resultsp)) { 767 XDR_DESTROY(&xdrs); 768 return (RPC_CANTDECODERES); 769 } 770 XDR_DESTROY(&xdrs); 771 } 772 773 return (stat); 774 } 775 776 /* 777 * Bind a socket to a privileged IP port 778 */ 779 int 780 bindresvport(struct socket *so, struct sockaddr *sa) 781 { 782 int old, error, af; 783 bool_t freesa = FALSE; 784 struct sockaddr_in *sin; 785 #ifdef INET6 786 struct sockaddr_in6 *sin6; 787 #endif 788 struct sockopt opt; 789 int proto, portrange, portlow; 790 uint16_t *portp; 791 socklen_t salen; 792 793 if (sa == NULL) { 794 CURVNET_SET(so->so_vnet); 795 error = so->so_proto->pr_sockaddr(so, &sa); 796 CURVNET_RESTORE(); 797 if (error) 798 return (error); 799 freesa = TRUE; 800 af = sa->sa_family; 801 salen = sa->sa_len; 802 memset(sa, 0, sa->sa_len); 803 } else { 804 af = sa->sa_family; 805 salen = sa->sa_len; 806 } 807 808 switch (af) { 809 case AF_INET: 810 proto = IPPROTO_IP; 811 portrange = IP_PORTRANGE; 812 portlow = IP_PORTRANGE_LOW; 813 sin = (struct sockaddr_in *)sa; 814 portp = &sin->sin_port; 815 break; 816 #ifdef INET6 817 case AF_INET6: 818 proto = IPPROTO_IPV6; 819 portrange = IPV6_PORTRANGE; 820 portlow = IPV6_PORTRANGE_LOW; 821 sin6 = (struct sockaddr_in6 *)sa; 822 portp = &sin6->sin6_port; 823 break; 824 #endif 825 default: 826 return (EPFNOSUPPORT); 827 } 828 829 sa->sa_family = af; 830 sa->sa_len = salen; 831 832 if (*portp == 0) { 833 bzero(&opt, sizeof(opt)); 834 opt.sopt_dir = SOPT_GET; 835 opt.sopt_level = proto; 836 opt.sopt_name = portrange; 837 opt.sopt_val = &old; 838 opt.sopt_valsize = sizeof(old); 839 error = sogetopt(so, &opt); 840 if (error) { 841 goto out; 842 } 843 844 opt.sopt_dir = SOPT_SET; 845 opt.sopt_val = &portlow; 846 error = sosetopt(so, &opt); 847 if (error) 848 goto out; 849 } 850 851 error = sobind(so, sa, curthread); 852 853 if (*portp == 0) { 854 if (error) { 855 opt.sopt_dir = SOPT_SET; 856 opt.sopt_val = &old; 857 sosetopt(so, &opt); 858 } 859 } 860 out: 861 if (freesa) 862 free(sa, M_SONAME); 863 864 return (error); 865 } 866 867 /* 868 * Make sure an mbuf list is made up entirely of ext_pgs mbufs. 869 * This is needed for sosend() when KERN_TLS is being used. 870 * (There might also be a performance improvement for certain 871 * network interfaces that handle ext_pgs mbufs efficiently.) 872 * It expects at least one non-ext_pgs mbuf followed by zero 873 * or more ext_pgs mbufs. It does not handle the case where 874 * non-ext_pgs mbuf(s) follow ext_pgs ones. 875 * It also performs sanity checks on the resultant list. 876 * The "mp" argument list is consumed. 877 * The "maxextsiz" argument is the upper bound on the data 878 * size for each mbuf (usually 16K for KERN_TLS). 879 */ 880 struct mbuf * 881 _rpc_copym_into_ext_pgs(struct mbuf *mp, int maxextsiz) 882 { 883 struct mbuf *m, *m2, *m3, *mhead; 884 int tlen; 885 886 KASSERT((mp->m_flags & (M_EXT | M_EXTPG)) != 887 (M_EXT | M_EXTPG), ("_rpc_copym_into_ext_pgs:" 888 " first mbuf is an ext_pgs")); 889 /* 890 * Find the last non-ext_pgs mbuf and the total 891 * length of the non-ext_pgs mbuf(s). 892 * The first mbuf must always be a non-ext_pgs 893 * mbuf. 894 */ 895 tlen = mp->m_len; 896 m2 = mp; 897 for (m = mp->m_next; m != NULL; m = m->m_next) { 898 if ((m->m_flags & M_EXTPG) != 0) 899 break; 900 tlen += m->m_len; 901 m2 = m; 902 } 903 904 /* 905 * Copy the non-ext_pgs mbuf(s) into an ext_pgs 906 * mbuf list. 907 */ 908 m2->m_next = NULL; 909 mhead = mb_mapped_to_unmapped(mp, tlen, maxextsiz, 910 M_WAITOK, &m2); 911 912 /* 913 * Link the ext_pgs list onto the newly copied 914 * list and free up the non-ext_pgs mbuf(s). 915 */ 916 m2->m_next = m; 917 m_freem(mp); 918 919 /* 920 * Sanity check the resultant mbuf list. Check for and 921 * remove any 0 length mbufs in the list, since the 922 * KERN_TLS code does not expect any 0 length mbuf(s) 923 * in the list. 924 */ 925 m3 = NULL; 926 m2 = mhead; 927 tlen = 0; 928 while (m2 != NULL) { 929 KASSERT(m2->m_len >= 0, ("_rpc_copym_into_ext_pgs:" 930 " negative m_len")); 931 KASSERT((m2->m_flags & (M_EXT | M_EXTPG)) == 932 (M_EXT | M_EXTPG), ("_rpc_copym_into_ext_pgs:" 933 " non-nomap mbuf in list")); 934 if (m2->m_len == 0) { 935 if (m3 != NULL) 936 m3->m_next = m2->m_next; 937 else 938 m = m2->m_next; 939 m2->m_next = NULL; 940 m_free(m2); 941 if (m3 != NULL) 942 m2 = m3->m_next; 943 else 944 m2 = m; 945 } else { 946 MBUF_EXT_PGS_ASSERT_SANITY(m2); 947 m3 = m2; 948 tlen += m2->m_len; 949 m2 = m2->m_next; 950 } 951 } 952 return (mhead); 953 } 954 955 /* 956 * Kernel module glue 957 */ 958 static int 959 krpc_modevent(module_t mod, int type, void *data) 960 { 961 int error = 0; 962 963 switch (type) { 964 case MOD_LOAD: 965 error = rpctls_init(); 966 break; 967 case MOD_UNLOAD: 968 /* 969 * Cannot be unloaded, since the rpctlssd or rpctlscd daemons 970 * might be performing a rpctls syscall. 971 */ 972 /* FALLTHROUGH */ 973 default: 974 error = EOPNOTSUPP; 975 } 976 return (error); 977 } 978 static moduledata_t krpc_mod = { 979 "krpc", 980 krpc_modevent, 981 NULL, 982 }; 983 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY); 984 985 /* So that loader and kldload(2) can find us, wherever we are.. */ 986 MODULE_VERSION(krpc, 1); 987 MODULE_DEPEND(krpc, xdr, 1, 1, 1); 988