xref: /illumos-gate/usr/src/lib/libsmbfs/smb/newvc.c (revision d8e10381a0083d7717710b0db7e64707bc0f3ff8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Create a new VC given a list of addresses.
29  */
30 
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <libintl.h>
39 #include <xti.h>
40 #include <assert.h>
41 
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <sys/byteorder.h>
45 #include <sys/socket.h>
46 #include <sys/fcntl.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50 #include <arpa/inet.h>
51 
52 #include <netsmb/smb.h>
53 #include <netsmb/smb_lib.h>
54 #include <netsmb/netbios.h>
55 #include <netsmb/nb_lib.h>
56 #include <netsmb/smb_dev.h>
57 
58 #include "charsets.h"
59 #include "private.h"
60 
61 /*
62  * Ask the IOD to create a VC with this IP address.
63  */
64 static int
65 newvc(struct smb_ctx *ctx, struct addrinfo *ai)
66 {
67 	smbioc_ossn_t *ssn = &ctx->ct_ssn;
68 
69 	/*
70 	 * Copy the passed address into ssn_srvaddr,
71 	 * but first sanity-check lengths.  Also,
72 	 * zero it first to avoid trailing junk.
73 	 */
74 	if (ai->ai_addrlen > sizeof (ssn->ssn_srvaddr))
75 		return (EINVAL);
76 	bzero(&ssn->ssn_srvaddr, sizeof (ssn->ssn_srvaddr));
77 	bcopy(ai->ai_addr, &ssn->ssn_srvaddr, ai->ai_addrlen);
78 
79 	return (smb_iod_cl_newvc(ctx));
80 }
81 
82 /*
83  * Setup a new VC via the IOD.
84  * Similar to findvc.c
85  */
86 int
87 smb_ctx_newvc(struct smb_ctx *ctx)
88 {
89 	struct addrinfo *ai;
90 	int err;
91 
92 	/* Should already have the address list. */
93 	if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
94 		return (EINVAL);
95 
96 	/*
97 	 * Get a door handle to the smbiod if we
98 	 * don't have one already.  This also
99 	 * starts the smbiod if necessary.
100 	 */
101 	if (ctx->ct_door_fd < 0) {
102 		err = smb_iod_start(ctx);
103 		if (err != 0)
104 			return (err);
105 	}
106 
107 	err = EPROTONOSUPPORT;  /* in case no AF match */
108 	for (ai = ctx->ct_addrinfo; ai; ai = ai->ai_next) {
109 
110 		switch (ai->ai_family) {
111 
112 		case AF_INET:
113 		case AF_INET6:
114 		case AF_NETBIOS:
115 			err = newvc(ctx, ai);
116 			if (err == 0)
117 				goto OK;
118 			break;
119 
120 		default:
121 			DPRINT("skipped family %d", ai->ai_family);
122 			break;
123 		}
124 	}
125 
126 	/*
127 	 * In the error case, the caller may try again
128 	 * with new auth. info, so keep the door open.
129 	 * Error return will close in smb_ctx_done.
130 	 */
131 	return (err);
132 
133 OK:
134 	/* Done with the door handle. */
135 	close(ctx->ct_door_fd);
136 	ctx->ct_door_fd = -1;
137 	return (0);
138 }
139