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