17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM * CDDL HEADER START
37836SJohn.Forte@Sun.COM *
47836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM *
87836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM * and limitations under the License.
127836SJohn.Forte@Sun.COM *
137836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM *
197836SJohn.Forte@Sun.COM * CDDL HEADER END
207836SJohn.Forte@Sun.COM */
217836SJohn.Forte@Sun.COM /*
22*9093SRamana.Srikanth@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237836SJohn.Forte@Sun.COM * Use is subject to license terms.
247836SJohn.Forte@Sun.COM */
257836SJohn.Forte@Sun.COM
267836SJohn.Forte@Sun.COM #include <sys/types.h>
277836SJohn.Forte@Sun.COM #include <sys/ksynch.h>
287836SJohn.Forte@Sun.COM #include <sys/errno.h>
297836SJohn.Forte@Sun.COM #include <sys/debug.h>
307836SJohn.Forte@Sun.COM #include <sys/cmn_err.h>
317836SJohn.Forte@Sun.COM #include <sys/kmem.h>
327836SJohn.Forte@Sun.COM #include <sys/errno.h>
337836SJohn.Forte@Sun.COM
347836SJohn.Forte@Sun.COM #ifdef _SunOS_2_6
357836SJohn.Forte@Sun.COM /*
367836SJohn.Forte@Sun.COM * on 2.6 both dki_lock.h and rpc/types.h define bool_t so we
377836SJohn.Forte@Sun.COM * define enum_t here as it is all we need from rpc/types.h
387836SJohn.Forte@Sun.COM * anyway and make it look like we included it. Yuck.
397836SJohn.Forte@Sun.COM */
407836SJohn.Forte@Sun.COM #define _RPC_TYPES_H
417836SJohn.Forte@Sun.COM typedef int enum_t;
427836SJohn.Forte@Sun.COM #else
437836SJohn.Forte@Sun.COM #ifndef DS_DDICT
447836SJohn.Forte@Sun.COM #include <rpc/types.h>
457836SJohn.Forte@Sun.COM #endif
467836SJohn.Forte@Sun.COM #endif /* _SunOS_2_6 */
477836SJohn.Forte@Sun.COM
487836SJohn.Forte@Sun.COM #include <sys/nsc_thread.h>
497836SJohn.Forte@Sun.COM #include <sys/nsctl/nsctl.h>
507836SJohn.Forte@Sun.COM #include "rdc_io.h"
517836SJohn.Forte@Sun.COM #include "rdc_ioctl.h"
527836SJohn.Forte@Sun.COM #include "rdc_prot.h"
537836SJohn.Forte@Sun.COM
547836SJohn.Forte@Sun.COM /*
557836SJohn.Forte@Sun.COM * Initialize a netbuf suitable for
567836SJohn.Forte@Sun.COM * describing an address
577836SJohn.Forte@Sun.COM */
587836SJohn.Forte@Sun.COM
597836SJohn.Forte@Sun.COM void
init_rdc_netbuf(struct netbuf * nbuf)607836SJohn.Forte@Sun.COM init_rdc_netbuf(struct netbuf *nbuf)
617836SJohn.Forte@Sun.COM {
627836SJohn.Forte@Sun.COM nbuf->buf = kmem_zalloc(RDC_MAXADDR, KM_SLEEP);
637836SJohn.Forte@Sun.COM nbuf->maxlen = RDC_MAXADDR;
647836SJohn.Forte@Sun.COM nbuf->len = 0;
657836SJohn.Forte@Sun.COM }
667836SJohn.Forte@Sun.COM
677836SJohn.Forte@Sun.COM /*
687836SJohn.Forte@Sun.COM * Free a netbuf
697836SJohn.Forte@Sun.COM */
707836SJohn.Forte@Sun.COM
717836SJohn.Forte@Sun.COM void
free_rdc_netbuf(struct netbuf * nbuf)727836SJohn.Forte@Sun.COM free_rdc_netbuf(struct netbuf *nbuf)
737836SJohn.Forte@Sun.COM {
747836SJohn.Forte@Sun.COM if (!(nbuf) || !(nbuf->buf)) {
757836SJohn.Forte@Sun.COM #ifdef DEBUG
767836SJohn.Forte@Sun.COM cmn_err(CE_PANIC, "Null netbuf in free_rdc_netbuf");
777836SJohn.Forte@Sun.COM #endif
787836SJohn.Forte@Sun.COM return;
797836SJohn.Forte@Sun.COM }
807836SJohn.Forte@Sun.COM kmem_free(nbuf->buf, nbuf->maxlen);
817836SJohn.Forte@Sun.COM nbuf->buf = NULL;
827836SJohn.Forte@Sun.COM nbuf->maxlen = 0;
837836SJohn.Forte@Sun.COM nbuf->len = 0;
847836SJohn.Forte@Sun.COM }
857836SJohn.Forte@Sun.COM
867836SJohn.Forte@Sun.COM
877836SJohn.Forte@Sun.COM /*
887836SJohn.Forte@Sun.COM * Duplicate a netbuf, must be followed by a free_rdc_netbuf().
897836SJohn.Forte@Sun.COM */
907836SJohn.Forte@Sun.COM void
dup_rdc_netbuf(const struct netbuf * from,struct netbuf * to)917836SJohn.Forte@Sun.COM dup_rdc_netbuf(const struct netbuf *from, struct netbuf *to)
927836SJohn.Forte@Sun.COM {
937836SJohn.Forte@Sun.COM init_rdc_netbuf(to);
947836SJohn.Forte@Sun.COM to->len = from->len;
957836SJohn.Forte@Sun.COM
967836SJohn.Forte@Sun.COM if (from->len > to->maxlen) {
97*9093SRamana.Srikanth@Sun.COM cmn_err(CE_WARN, "!dup_rdc_netbuf: from->len %d, to->maxlen %d",
98*9093SRamana.Srikanth@Sun.COM from->len, to->maxlen);
997836SJohn.Forte@Sun.COM }
1007836SJohn.Forte@Sun.COM
1017836SJohn.Forte@Sun.COM bcopy(from->buf, to->buf, (size_t)from->len);
1027836SJohn.Forte@Sun.COM }
1037836SJohn.Forte@Sun.COM
1047836SJohn.Forte@Sun.COM
1057836SJohn.Forte@Sun.COM #ifdef DEBUG
1067836SJohn.Forte@Sun.COM void
rdc_print_svinfo(rdc_srv_t * svp,char * str)1077836SJohn.Forte@Sun.COM rdc_print_svinfo(rdc_srv_t *svp, char *str)
1087836SJohn.Forte@Sun.COM {
1097836SJohn.Forte@Sun.COM int i;
1107836SJohn.Forte@Sun.COM
1117836SJohn.Forte@Sun.COM if (svp == NULL)
1127836SJohn.Forte@Sun.COM return;
1137836SJohn.Forte@Sun.COM
114*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!rdc %s servinfo: %p\n", str, (void *) svp);
1157836SJohn.Forte@Sun.COM
1167836SJohn.Forte@Sun.COM if (svp->ri_knconf != NULL) {
117*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!knconf: semantics %d",
1187836SJohn.Forte@Sun.COM svp->ri_knconf->knc_semantics);
119*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "! protofmly %s",
1207836SJohn.Forte@Sun.COM svp->ri_knconf->knc_protofmly);
121*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "! proto %s",
1227836SJohn.Forte@Sun.COM svp->ri_knconf->knc_proto);
123*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "! rdev %lx",
1247836SJohn.Forte@Sun.COM svp->ri_knconf->knc_rdev);
1257836SJohn.Forte@Sun.COM }
1267836SJohn.Forte@Sun.COM
1277836SJohn.Forte@Sun.COM for (i = 0; i < svp->ri_addr.len; i++)
1287836SJohn.Forte@Sun.COM printf("%u ", svp->ri_addr.buf[i]);
1297836SJohn.Forte@Sun.COM
130*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!\naddr: len %d buf %p\n",
1317836SJohn.Forte@Sun.COM svp->ri_addr.len, (void *) svp->ri_addr.buf);
132*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!host: %s\n", svp->ri_hostname);
1337836SJohn.Forte@Sun.COM }
1347836SJohn.Forte@Sun.COM #endif /* DEBUG */
1357836SJohn.Forte@Sun.COM
1367836SJohn.Forte@Sun.COM /*
1377836SJohn.Forte@Sun.COM * Initialize an rdc servinfo
1387836SJohn.Forte@Sun.COM * Contains all the protocol we need to do a client rpc
1397836SJohn.Forte@Sun.COM * A chain of rdc_srv_t indicates a one to many
1407836SJohn.Forte@Sun.COM */
1417836SJohn.Forte@Sun.COM
1427836SJohn.Forte@Sun.COM rdc_srv_t *
rdc_create_svinfo(char * host,struct netbuf * svaddr,struct knetconfig * conf)1437836SJohn.Forte@Sun.COM rdc_create_svinfo(char *host, struct netbuf *svaddr, struct knetconfig *conf)
1447836SJohn.Forte@Sun.COM {
1457836SJohn.Forte@Sun.COM rdc_srv_t *nvp;
1467836SJohn.Forte@Sun.COM int hlen = strlen(host) + 1;
1477836SJohn.Forte@Sun.COM
1487836SJohn.Forte@Sun.COM if (conf == NULL) {
1497836SJohn.Forte@Sun.COM return (NULL);
1507836SJohn.Forte@Sun.COM }
1517836SJohn.Forte@Sun.COM
1527836SJohn.Forte@Sun.COM if (host == NULL) {
1537836SJohn.Forte@Sun.COM return (NULL);
1547836SJohn.Forte@Sun.COM }
1557836SJohn.Forte@Sun.COM
1567836SJohn.Forte@Sun.COM nvp = kmem_zalloc(sizeof (*nvp), KM_SLEEP);
1577836SJohn.Forte@Sun.COM nvp->ri_knconf = kmem_alloc(sizeof (*nvp->ri_knconf), KM_SLEEP);
1587836SJohn.Forte@Sun.COM nvp->ri_hostname = kmem_zalloc(hlen, KM_SLEEP);
1597836SJohn.Forte@Sun.COM
1607836SJohn.Forte@Sun.COM if (nvp == NULL || nvp->ri_hostname == NULL || nvp->ri_knconf == NULL) {
1617836SJohn.Forte@Sun.COM rdc_destroy_svinfo(nvp);
1627836SJohn.Forte@Sun.COM return (NULL);
1637836SJohn.Forte@Sun.COM }
1647836SJohn.Forte@Sun.COM
1657836SJohn.Forte@Sun.COM nvp->ri_hostnamelen = hlen;
1667836SJohn.Forte@Sun.COM
1677836SJohn.Forte@Sun.COM bcopy((void *)conf, (void *)nvp->ri_knconf, sizeof (*nvp->ri_knconf));
1687836SJohn.Forte@Sun.COM nvp->ri_knconf->knc_protofmly = kmem_zalloc(KNC_STRSIZE + 1, KM_SLEEP);
1697836SJohn.Forte@Sun.COM nvp->ri_knconf->knc_proto = kmem_zalloc(KNC_STRSIZE + 1, KM_SLEEP);
1707836SJohn.Forte@Sun.COM
1717836SJohn.Forte@Sun.COM if (nvp->ri_knconf->knc_protofmly == NULL ||
1727836SJohn.Forte@Sun.COM nvp->ri_knconf->knc_proto == NULL) {
1737836SJohn.Forte@Sun.COM rdc_destroy_svinfo(nvp);
1747836SJohn.Forte@Sun.COM return (NULL);
1757836SJohn.Forte@Sun.COM
1767836SJohn.Forte@Sun.COM }
1777836SJohn.Forte@Sun.COM
1787836SJohn.Forte@Sun.COM (void) strncpy(nvp->ri_knconf->knc_protofmly, conf->knc_protofmly,
179*9093SRamana.Srikanth@Sun.COM KNC_STRSIZE);
1807836SJohn.Forte@Sun.COM (void) strncpy(nvp->ri_knconf->knc_proto, conf->knc_proto, KNC_STRSIZE);
1817836SJohn.Forte@Sun.COM
1827836SJohn.Forte@Sun.COM dup_rdc_netbuf(svaddr, &nvp->ri_addr);
1837836SJohn.Forte@Sun.COM
1847836SJohn.Forte@Sun.COM nvp->ri_secdata = NULL; /* For now */
1857836SJohn.Forte@Sun.COM (void) strncpy(nvp->ri_hostname, host, hlen);
1867836SJohn.Forte@Sun.COM #ifdef DEBUG_IP
187*9093SRamana.Srikanth@Sun.COM rdc_print_svinfo(nvp, "!create");
1887836SJohn.Forte@Sun.COM #endif
1897836SJohn.Forte@Sun.COM return (nvp);
1907836SJohn.Forte@Sun.COM }
1917836SJohn.Forte@Sun.COM
1927836SJohn.Forte@Sun.COM void
rdc_destroy_svinfo(rdc_srv_t * svp)1937836SJohn.Forte@Sun.COM rdc_destroy_svinfo(rdc_srv_t *svp)
1947836SJohn.Forte@Sun.COM {
1957836SJohn.Forte@Sun.COM if (svp == NULL)
1967836SJohn.Forte@Sun.COM return;
1977836SJohn.Forte@Sun.COM
1987836SJohn.Forte@Sun.COM if (svp->ri_addr.buf && svp->ri_addr.maxlen)
1997836SJohn.Forte@Sun.COM free_rdc_netbuf(&(svp->ri_addr));
2007836SJohn.Forte@Sun.COM
2017836SJohn.Forte@Sun.COM if (svp->ri_knconf->knc_protofmly)
2027836SJohn.Forte@Sun.COM kmem_free(svp->ri_knconf->knc_protofmly, KNC_STRSIZE + 1);
2037836SJohn.Forte@Sun.COM
2047836SJohn.Forte@Sun.COM if (svp->ri_knconf->knc_proto)
2057836SJohn.Forte@Sun.COM kmem_free(svp->ri_knconf->knc_proto, KNC_STRSIZE + 1);
2067836SJohn.Forte@Sun.COM
2077836SJohn.Forte@Sun.COM if (svp->ri_knconf)
2087836SJohn.Forte@Sun.COM kmem_free(svp->ri_knconf, sizeof (*svp->ri_knconf));
2097836SJohn.Forte@Sun.COM
2107836SJohn.Forte@Sun.COM kmem_free(svp, sizeof (*svp));
2117836SJohn.Forte@Sun.COM }
2127836SJohn.Forte@Sun.COM
2137836SJohn.Forte@Sun.COM /*
2147836SJohn.Forte@Sun.COM * rdc_netbuf_toint
2157836SJohn.Forte@Sun.COM * Returns oldsytle ipv4 RDC ver 3 addresses for RPC protocol from netbuf
2167836SJohn.Forte@Sun.COM * Note: This would never be called in the case of IPv6 and a program
2177836SJohn.Forte@Sun.COM * mismatch ie ver 3 to ver 4
2187836SJohn.Forte@Sun.COM */
2197836SJohn.Forte@Sun.COM int
rdc_netbuf_toint(struct netbuf * nb)2207836SJohn.Forte@Sun.COM rdc_netbuf_toint(struct netbuf *nb)
2217836SJohn.Forte@Sun.COM {
2227836SJohn.Forte@Sun.COM int ret;
2237836SJohn.Forte@Sun.COM if (nb->len > RDC_MAXADDR)
224*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!rdc_netbuf_toint: bad size %d", nb->len);
2257836SJohn.Forte@Sun.COM
2267836SJohn.Forte@Sun.COM switch (nb->len) {
2277836SJohn.Forte@Sun.COM case 4:
2287836SJohn.Forte@Sun.COM bcopy(nb->buf, (char *)&ret, sizeof (int));
2297836SJohn.Forte@Sun.COM return (ret);
2307836SJohn.Forte@Sun.COM
2317836SJohn.Forte@Sun.COM case 8:
2327836SJohn.Forte@Sun.COM case 16:
2337836SJohn.Forte@Sun.COM case 32:
2347836SJohn.Forte@Sun.COM bcopy(&nb->buf[4], (char *)&ret, sizeof (int));
2357836SJohn.Forte@Sun.COM return (ret);
2367836SJohn.Forte@Sun.COM
2377836SJohn.Forte@Sun.COM default:
238*9093SRamana.Srikanth@Sun.COM cmn_err(CE_NOTE, "!rdc_netbuf_toint: size %d", nb->len);
2397836SJohn.Forte@Sun.COM }
2407836SJohn.Forte@Sun.COM return (0);
2417836SJohn.Forte@Sun.COM }
242