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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 /*
28  * Client-side interface to the IO Daemon (IOD)
29  */
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <netdb.h>
39 #include <libintl.h>
40 #include <thread.h>
41 
42 #include <sys/byteorder.h>
43 #include <sys/types.h>
44 #include <sys/fcntl.h>
45 #include <sys/ioctl.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51 #include <arpa/inet.h>
52 
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 <assert.h>
59 
60 #include "smb/charsets.h"
61 #include "smb/private.h"
62 
63 /*
64  * Make sure we don't call the real IOD here.
65  */
66 int
smb_iod_open_door(int * fdp)67 smb_iod_open_door(int *fdp)
68 {
69 	*fdp = -1;
70 	return (ENOTSUP);
71 }
72 
73 /*
74  * Get a door handle to the IOD...
75  */
76 int
smb_iod_start(smb_ctx_t * ctx)77 smb_iod_start(smb_ctx_t *ctx)
78 {
79 	return (0);
80 }
81 
82 void *
iod_work(void * arg)83 iod_work(void *arg)
84 {
85 	smb_ctx_t *ctx = arg;
86 	(void) smb_iod_work(ctx);
87 	smb_ctx_free(ctx);
88 	return (NULL);
89 }
90 
91 /*
92  * Ask the IOD to connect using the info in ctx.
93  * Called by newvc.
94  *
95  * This function largely follows smbiod.c : iod_newvc()
96  */
97 int
smb_iod_cl_newvc(smb_ctx_t * cl_ctx)98 smb_iod_cl_newvc(smb_ctx_t *cl_ctx)
99 {
100 	smb_ctx_t *ctx;
101 	thread_t tid;
102 	int err = 0;
103 
104 	/*
105 	 * Clone the context, like in smbiod.c
106 	 */
107 	err = smb_ctx_alloc(&ctx);
108 	if (err)
109 		return (err);
110 	bcopy(&cl_ctx->ct_iod_ssn, &ctx->ct_iod_ssn,
111 	    sizeof (ctx->ct_iod_ssn));
112 
113 	/*
114 	 * Create the driver session first...
115 	 */
116 	if ((err = smb_ctx_gethandle(ctx)) != 0)
117 		goto out;
118 	if (nsmb_ioctl(ctx->ct_dev_fd, SMBIOC_SSN_CREATE, &ctx->ct_ssn) < 0) {
119 		err = errno;
120 		if (err == EEXIST)
121 			err = 0; /* see above */
122 		goto out;
123 	}
124 
125 	/*
126 	 * Do the initial connection setup here, so we can
127 	 * report the outcome to the door client.
128 	 */
129 	err = smb_iod_connect(ctx);
130 	if (err != 0) {
131 		fprintf(stderr, "smb_iod_connect, err=%d\n", err);
132 		goto out;
133 	}
134 
135 	/* The rest happens in the iod_work thread. */
136 	err = thr_create(NULL, 0, iod_work, ctx, THR_DETACHED, &tid);
137 	if (err == 0) {
138 		/*
139 		 * Given to the new thread.
140 		 * free at end of iod_work
141 		 */
142 		ctx = NULL;
143 	}
144 
145 out:
146 	if (ctx != NULL)
147 		smb_ctx_free(ctx);
148 
149 	return (err);
150 }
151