xref: /titanic_50/usr/src/lib/libsmbfs/smb/newvc.c (revision 6b2bcd8e40cb530c97e59f351ceccb5c807ac34a)
1613a2f6bSGordon Ross /*
2613a2f6bSGordon Ross  * CDDL HEADER START
3613a2f6bSGordon Ross  *
4613a2f6bSGordon Ross  * The contents of this file are subject to the terms of the
5613a2f6bSGordon Ross  * Common Development and Distribution License (the "License").
6613a2f6bSGordon Ross  * You may not use this file except in compliance with the License.
7613a2f6bSGordon Ross  *
8613a2f6bSGordon Ross  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9613a2f6bSGordon Ross  * or http://www.opensolaris.org/os/licensing.
10613a2f6bSGordon Ross  * See the License for the specific language governing permissions
11613a2f6bSGordon Ross  * and limitations under the License.
12613a2f6bSGordon Ross  *
13613a2f6bSGordon Ross  * When distributing Covered Code, include this CDDL HEADER in each
14613a2f6bSGordon Ross  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15613a2f6bSGordon Ross  * If applicable, add the following below this CDDL HEADER, with the
16613a2f6bSGordon Ross  * fields enclosed by brackets "[]" replaced with your own identifying
17613a2f6bSGordon Ross  * information: Portions Copyright [yyyy] [name of copyright owner]
18613a2f6bSGordon Ross  *
19613a2f6bSGordon Ross  * CDDL HEADER END
20613a2f6bSGordon Ross  */
21613a2f6bSGordon Ross 
22613a2f6bSGordon Ross /*
23613a2f6bSGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24613a2f6bSGordon Ross  * Use is subject to license terms.
25613a2f6bSGordon Ross  */
26613a2f6bSGordon Ross 
27613a2f6bSGordon Ross /*
28613a2f6bSGordon Ross  * Create a new VC given a list of addresses.
29613a2f6bSGordon Ross  */
30613a2f6bSGordon Ross 
31613a2f6bSGordon Ross #include <errno.h>
32613a2f6bSGordon Ross #include <stdio.h>
33613a2f6bSGordon Ross #include <string.h>
34613a2f6bSGordon Ross #include <strings.h>
35613a2f6bSGordon Ross #include <stdlib.h>
36613a2f6bSGordon Ross #include <unistd.h>
37613a2f6bSGordon Ross #include <netdb.h>
38613a2f6bSGordon Ross #include <libintl.h>
39613a2f6bSGordon Ross #include <xti.h>
40613a2f6bSGordon Ross #include <assert.h>
41613a2f6bSGordon Ross 
42613a2f6bSGordon Ross #include <sys/types.h>
43613a2f6bSGordon Ross #include <sys/time.h>
44613a2f6bSGordon Ross #include <sys/byteorder.h>
45613a2f6bSGordon Ross #include <sys/socket.h>
46613a2f6bSGordon Ross #include <sys/fcntl.h>
47613a2f6bSGordon Ross 
48613a2f6bSGordon Ross #include <netinet/in.h>
49613a2f6bSGordon Ross #include <netinet/tcp.h>
50613a2f6bSGordon Ross #include <arpa/inet.h>
51613a2f6bSGordon Ross 
52613a2f6bSGordon Ross #include <netsmb/smb.h>
53613a2f6bSGordon Ross #include <netsmb/smb_lib.h>
54613a2f6bSGordon Ross #include <netsmb/netbios.h>
55613a2f6bSGordon Ross #include <netsmb/nb_lib.h>
56613a2f6bSGordon Ross #include <netsmb/smb_dev.h>
57613a2f6bSGordon Ross 
58613a2f6bSGordon Ross #include "charsets.h"
59613a2f6bSGordon Ross #include "private.h"
60613a2f6bSGordon Ross 
61613a2f6bSGordon Ross /*
62613a2f6bSGordon Ross  * Ask the IOD to create a VC with this IP address.
63613a2f6bSGordon Ross  */
64613a2f6bSGordon Ross static int
newvc(struct smb_ctx * ctx,struct addrinfo * ai)65613a2f6bSGordon Ross newvc(struct smb_ctx *ctx, struct addrinfo *ai)
66613a2f6bSGordon Ross {
67613a2f6bSGordon Ross 	smbioc_ossn_t *ssn = &ctx->ct_ssn;
68613a2f6bSGordon Ross 
69613a2f6bSGordon Ross 	/*
70613a2f6bSGordon Ross 	 * Copy the passed address into ssn_srvaddr,
71613a2f6bSGordon Ross 	 * but first sanity-check lengths.  Also,
72613a2f6bSGordon Ross 	 * zero it first to avoid trailing junk.
73613a2f6bSGordon Ross 	 */
74613a2f6bSGordon Ross 	if (ai->ai_addrlen > sizeof (ssn->ssn_srvaddr))
75613a2f6bSGordon Ross 		return (EINVAL);
76613a2f6bSGordon Ross 	bzero(&ssn->ssn_srvaddr, sizeof (ssn->ssn_srvaddr));
77613a2f6bSGordon Ross 	bcopy(ai->ai_addr, &ssn->ssn_srvaddr, ai->ai_addrlen);
78613a2f6bSGordon Ross 
79613a2f6bSGordon Ross 	return (smb_iod_cl_newvc(ctx));
80613a2f6bSGordon Ross }
81613a2f6bSGordon Ross 
82613a2f6bSGordon Ross /*
83613a2f6bSGordon Ross  * Setup a new VC via the IOD.
84613a2f6bSGordon Ross  * Similar to findvc.c
85613a2f6bSGordon Ross  */
86613a2f6bSGordon Ross int
smb_ctx_newvc(struct smb_ctx * ctx)87613a2f6bSGordon Ross smb_ctx_newvc(struct smb_ctx *ctx)
88613a2f6bSGordon Ross {
89613a2f6bSGordon Ross 	struct addrinfo *ai;
90*6b2bcd8eSGordon Ross 	int err;
91613a2f6bSGordon Ross 
92613a2f6bSGordon Ross 	/* Should already have the address list. */
93613a2f6bSGordon Ross 	if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
94613a2f6bSGordon Ross 		return (EINVAL);
95613a2f6bSGordon Ross 
96*6b2bcd8eSGordon Ross 	/*
97*6b2bcd8eSGordon Ross 	 * Get a door handle to the smbiod if we
98*6b2bcd8eSGordon Ross 	 * don't have one already.  This also
99*6b2bcd8eSGordon Ross 	 * starts the smbiod if necessary.
100*6b2bcd8eSGordon Ross 	 */
101*6b2bcd8eSGordon Ross 	if (ctx->ct_door_fd < 0) {
102*6b2bcd8eSGordon Ross 		err = smb_iod_start(ctx);
103*6b2bcd8eSGordon Ross 		if (err != 0)
104*6b2bcd8eSGordon Ross 			return (err);
105*6b2bcd8eSGordon Ross 	}
106*6b2bcd8eSGordon Ross 
107*6b2bcd8eSGordon Ross 	err = EPROTONOSUPPORT;  /* in case no AF match */
108613a2f6bSGordon Ross 	for (ai = ctx->ct_addrinfo; ai; ai = ai->ai_next) {
109613a2f6bSGordon Ross 
110613a2f6bSGordon Ross 		switch (ai->ai_family) {
111613a2f6bSGordon Ross 
112613a2f6bSGordon Ross 		case AF_INET:
113613a2f6bSGordon Ross 		case AF_INET6:
114613a2f6bSGordon Ross 		case AF_NETBIOS:
115613a2f6bSGordon Ross 			err = newvc(ctx, ai);
116*6b2bcd8eSGordon Ross 			if (err == 0)
117*6b2bcd8eSGordon Ross 				goto OK;
118613a2f6bSGordon Ross 			break;
119613a2f6bSGordon Ross 
120613a2f6bSGordon Ross 		default:
121613a2f6bSGordon Ross 			DPRINT("skipped family %d", ai->ai_family);
122613a2f6bSGordon Ross 			break;
123613a2f6bSGordon Ross 		}
124613a2f6bSGordon Ross 	}
125613a2f6bSGordon Ross 
126*6b2bcd8eSGordon Ross 	/*
127*6b2bcd8eSGordon Ross 	 * In the error case, the caller may try again
128*6b2bcd8eSGordon Ross 	 * with new auth. info, so keep the door open.
129*6b2bcd8eSGordon Ross 	 * Error return will close in smb_ctx_done.
130*6b2bcd8eSGordon Ross 	 */
131613a2f6bSGordon Ross 	return (err);
132*6b2bcd8eSGordon Ross 
133*6b2bcd8eSGordon Ross OK:
134*6b2bcd8eSGordon Ross 	/* Done with the door handle. */
135*6b2bcd8eSGordon Ross 	close(ctx->ct_door_fd);
136*6b2bcd8eSGordon Ross 	ctx->ct_door_fd = -1;
137*6b2bcd8eSGordon Ross 	return (0);
138613a2f6bSGordon Ross }
139