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 * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
27 */
28
29/*
30 * Find existing an VC given a list of addresses.
31 */
32
33#include <errno.h>
34#include <stdio.h>
35#include <string.h>
36#include <strings.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <netdb.h>
40#include <libintl.h>
41#include <xti.h>
42#include <assert.h>
43
44#include <sys/types.h>
45#include <sys/time.h>
46#include <sys/byteorder.h>
47#include <sys/socket.h>
48#include <sys/fcntl.h>
49
50#include <netinet/in.h>
51#include <netinet/tcp.h>
52#include <arpa/inet.h>
53
54#include <netsmb/smb.h>
55#include <netsmb/smb_lib.h>
56#include <netsmb/netbios.h>
57#include <netsmb/nb_lib.h>
58#include <netsmb/smb_dev.h>
59
60#include "charsets.h"
61#include "private.h"
62
63/*
64 * Ask the driver if it has a VC with this IP address.
65 */
66static int
67findvc(struct smb_ctx *ctx, struct addrinfo *ai)
68{
69	smbioc_ossn_t *ssn = &ctx->ct_ssn;
70
71	/*
72	 * Copy the passed address into ssn_srvaddr,
73	 * but first sanity-check lengths.  Also,
74	 * zero it first to avoid trailing junk.
75	 */
76	if (ai->ai_addrlen > sizeof (ssn->ssn_srvaddr))
77		return (EINVAL);
78	bzero(&ssn->ssn_srvaddr, sizeof (ssn->ssn_srvaddr));
79	bcopy(ai->ai_addr, &ssn->ssn_srvaddr, ai->ai_addrlen);
80
81	if (nsmb_ioctl(ctx->ct_dev_fd, SMBIOC_SSN_FIND, ssn) == -1)
82		return (errno);
83
84	return (0);
85}
86
87/*
88 * Find (and reuse) an existing VC.
89 * See also: newvc.c
90 */
91int
92smb_ctx_findvc(struct smb_ctx *ctx)
93{
94	struct addrinfo *ai;
95	int err;
96
97	/* Should already have the address list. */
98	if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
99		return (EINVAL);
100
101	if (ctx->ct_dev_fd < 0) {
102		if ((err = smb_ctx_gethandle(ctx)))
103			return (err);
104	}
105
106	for (ai = ctx->ct_addrinfo; ai; ai = ai->ai_next) {
107
108		switch (ai->ai_family) {
109
110		case AF_INET:
111		case AF_INET6:
112		case AF_NETBIOS:
113			err = findvc(ctx, ai);
114			break;
115
116		default:
117			DPRINT("skipped family %d", ai->ai_family);
118			err = EPROTONOSUPPORT;
119			break;
120		}
121
122		if (err == 0) {
123			/* re-use an existing VC */
124			return (0);
125		}
126	}
127
128	return (ENOENT);
129}
130
131/*
132 * Forcibly disconnect the current session, even if
133 * there are others using it!  This is used by the
134 * SMB server netlogon when it wants to setup a new
135 * logon session and does not want any re-use.
136 */
137int
138smb_ctx_kill(struct smb_ctx *ctx)
139{
140
141	if (nsmb_ioctl(ctx->ct_dev_fd, SMBIOC_SSN_KILL, NULL) == -1)
142		return (errno);
143
144	return (0);
145}
146