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.
258329232eSGordon Ross *
26*40c0e231SGordon Ross * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
27613a2f6bSGordon Ross */
28613a2f6bSGordon Ross
29613a2f6bSGordon Ross /*
30613a2f6bSGordon Ross * Find existing an VC given a list of addresses.
31613a2f6bSGordon Ross */
32613a2f6bSGordon Ross
33613a2f6bSGordon Ross #include <errno.h>
34613a2f6bSGordon Ross #include <stdio.h>
35613a2f6bSGordon Ross #include <string.h>
36613a2f6bSGordon Ross #include <strings.h>
37613a2f6bSGordon Ross #include <stdlib.h>
38613a2f6bSGordon Ross #include <unistd.h>
39613a2f6bSGordon Ross #include <netdb.h>
40613a2f6bSGordon Ross #include <libintl.h>
41613a2f6bSGordon Ross #include <xti.h>
42613a2f6bSGordon Ross #include <assert.h>
43613a2f6bSGordon Ross
44613a2f6bSGordon Ross #include <sys/types.h>
45613a2f6bSGordon Ross #include <sys/time.h>
46613a2f6bSGordon Ross #include <sys/byteorder.h>
47613a2f6bSGordon Ross #include <sys/socket.h>
48613a2f6bSGordon Ross #include <sys/fcntl.h>
49613a2f6bSGordon Ross
50613a2f6bSGordon Ross #include <netinet/in.h>
51613a2f6bSGordon Ross #include <netinet/tcp.h>
52613a2f6bSGordon Ross #include <arpa/inet.h>
53613a2f6bSGordon Ross
54613a2f6bSGordon Ross #include <netsmb/smb.h>
55613a2f6bSGordon Ross #include <netsmb/smb_lib.h>
56613a2f6bSGordon Ross #include <netsmb/netbios.h>
57613a2f6bSGordon Ross #include <netsmb/nb_lib.h>
58613a2f6bSGordon Ross #include <netsmb/smb_dev.h>
59613a2f6bSGordon Ross
60613a2f6bSGordon Ross #include "charsets.h"
61613a2f6bSGordon Ross #include "private.h"
62613a2f6bSGordon Ross
63613a2f6bSGordon Ross /*
64613a2f6bSGordon Ross * Ask the driver if it has a VC with this IP address.
65613a2f6bSGordon Ross */
66613a2f6bSGordon Ross static int
findvc(struct smb_ctx * ctx,struct addrinfo * ai)67613a2f6bSGordon Ross findvc(struct smb_ctx *ctx, struct addrinfo *ai)
68613a2f6bSGordon Ross {
69613a2f6bSGordon Ross smbioc_ossn_t *ssn = &ctx->ct_ssn;
70613a2f6bSGordon Ross
71613a2f6bSGordon Ross /*
72613a2f6bSGordon Ross * Copy the passed address into ssn_srvaddr,
73613a2f6bSGordon Ross * but first sanity-check lengths. Also,
74613a2f6bSGordon Ross * zero it first to avoid trailing junk.
75613a2f6bSGordon Ross */
76613a2f6bSGordon Ross if (ai->ai_addrlen > sizeof (ssn->ssn_srvaddr))
77613a2f6bSGordon Ross return (EINVAL);
78613a2f6bSGordon Ross bzero(&ssn->ssn_srvaddr, sizeof (ssn->ssn_srvaddr));
79613a2f6bSGordon Ross bcopy(ai->ai_addr, &ssn->ssn_srvaddr, ai->ai_addrlen);
80613a2f6bSGordon Ross
818329232eSGordon Ross if (nsmb_ioctl(ctx->ct_dev_fd, SMBIOC_SSN_FIND, ssn) == -1)
82613a2f6bSGordon Ross return (errno);
83613a2f6bSGordon Ross
84613a2f6bSGordon Ross return (0);
85613a2f6bSGordon Ross }
86613a2f6bSGordon Ross
87613a2f6bSGordon Ross /*
88613a2f6bSGordon Ross * Find (and reuse) an existing VC.
89613a2f6bSGordon Ross * See also: newvc.c
90613a2f6bSGordon Ross */
91613a2f6bSGordon Ross int
smb_ctx_findvc(struct smb_ctx * ctx)92613a2f6bSGordon Ross smb_ctx_findvc(struct smb_ctx *ctx)
93613a2f6bSGordon Ross {
94613a2f6bSGordon Ross struct addrinfo *ai;
95613a2f6bSGordon Ross int err;
96613a2f6bSGordon Ross
97613a2f6bSGordon Ross /* Should already have the address list. */
98613a2f6bSGordon Ross if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
99613a2f6bSGordon Ross return (EINVAL);
100613a2f6bSGordon Ross
101a6d10110SGordon Ross if (ctx->ct_dev_fd < 0) {
102a6d10110SGordon Ross if ((err = smb_ctx_gethandle(ctx)))
103a6d10110SGordon Ross return (err);
104a6d10110SGordon Ross }
105a6d10110SGordon Ross
106613a2f6bSGordon Ross for (ai = ctx->ct_addrinfo; ai; ai = ai->ai_next) {
107613a2f6bSGordon Ross
108613a2f6bSGordon Ross switch (ai->ai_family) {
109613a2f6bSGordon Ross
110613a2f6bSGordon Ross case AF_INET:
111613a2f6bSGordon Ross case AF_INET6:
112613a2f6bSGordon Ross case AF_NETBIOS:
113613a2f6bSGordon Ross err = findvc(ctx, ai);
114613a2f6bSGordon Ross break;
115613a2f6bSGordon Ross
116613a2f6bSGordon Ross default:
117613a2f6bSGordon Ross DPRINT("skipped family %d", ai->ai_family);
118613a2f6bSGordon Ross err = EPROTONOSUPPORT;
119613a2f6bSGordon Ross break;
120613a2f6bSGordon Ross }
121613a2f6bSGordon Ross
122613a2f6bSGordon Ross if (err == 0) {
123613a2f6bSGordon Ross /* re-use an existing VC */
124613a2f6bSGordon Ross return (0);
125613a2f6bSGordon Ross }
126613a2f6bSGordon Ross }
127613a2f6bSGordon Ross
128613a2f6bSGordon Ross return (ENOENT);
129613a2f6bSGordon Ross }
130613a2f6bSGordon Ross
131613a2f6bSGordon Ross /*
132613a2f6bSGordon Ross * Forcibly disconnect the current session, even if
133613a2f6bSGordon Ross * there are others using it! This is used by the
134613a2f6bSGordon Ross * SMB server netlogon when it wants to setup a new
135613a2f6bSGordon Ross * logon session and does not want any re-use.
136613a2f6bSGordon Ross */
137613a2f6bSGordon Ross int
smb_ctx_kill(struct smb_ctx * ctx)138613a2f6bSGordon Ross smb_ctx_kill(struct smb_ctx *ctx)
139613a2f6bSGordon Ross {
140613a2f6bSGordon Ross
1418329232eSGordon Ross if (nsmb_ioctl(ctx->ct_dev_fd, SMBIOC_SSN_KILL, NULL) == -1)
142613a2f6bSGordon Ross return (errno);
143613a2f6bSGordon Ross
144613a2f6bSGordon Ross return (0);
145613a2f6bSGordon Ross }
146