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 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/ddi.h> 28 #include <sys/modctl.h> 29 #include <sys/cred.h> 30 #include <sys/disp.h> 31 #include <sys/ioccom.h> 32 #include <sys/policy.h> 33 #include <sys/cmn_err.h> 34 #include <smbsrv/smb_kproto.h> 35 #include <smbsrv/smb_ioctl.h> 36 37 static int smb_drv_open(dev_t *, int, int, cred_t *); 38 static int smb_drv_close(dev_t, int, int, cred_t *); 39 static int smb_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 40 static int smb_drv_attach(dev_info_t *, ddi_attach_cmd_t); 41 static int smb_drv_detach(dev_info_t *, ddi_detach_cmd_t); 42 static int smb_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 43 44 /* 45 * ***************************************************************************** 46 * ****************************** Global Variables ***************************** 47 * ***************************************************************************** 48 * 49 * These variables can only be changed through the /etc/system file. 50 */ 51 52 /* 53 * Maximum buffer size for NT: configurable based on the client environment. 54 * IR104720 Experiments with Windows 2000 indicate that we achieve better 55 * SmbWriteX performance with a buffer size of 64KB instead of the 37KB used 56 * with Windows NT4.0. Previous experiments with NT4.0 resulted in directory 57 * listing problems so this buffer size is configurable based on the end-user 58 * environment. When in doubt use 37KB. 59 * 60 * smb_raw_mode: read_raw and write_raw supported (1) or NOT supported (0). 61 */ 62 int smb_maxbufsize = SMB_NT_MAXBUF; 63 int smb_oplock_levelII = 1; 64 int smb_oplock_timeout = OPLOCK_STD_TIMEOUT; 65 int smb_oplock_min_timeout = OPLOCK_MIN_TIMEOUT; 66 int smb_flush_required = 1; 67 int smb_dirsymlink_enable = 1; 68 int smb_sign_debug = 0; 69 int smb_raw_mode = 0; 70 int smb_shortnames = 1; 71 uint_t smb_audit_flags = 72 #ifdef DEBUG 73 SMB_AUDIT_NODE; 74 #else 75 0; 76 #endif 77 78 /* 79 * Maximum number of simultaneous authentication, share mapping, pipe open 80 * requests to be processed. 81 */ 82 int smb_ssetup_threshold = 256; 83 int smb_tcon_threshold = 1024; 84 int smb_opipe_threshold = 1024; 85 86 /* 87 * Number of milliseconds that a request will be stalled if it comes in after 88 * the maximum number of inflight operations are being proccessed. 89 */ 90 int smb_ssetup_timeout = (30 * 1000); 91 int smb_tcon_timeout = (30 * 1000); 92 int smb_opipe_timeout = (30 * 1000); 93 94 int smb_threshold_debug = 0; 95 96 /* 97 * Thread priorities used in smbsrv. Our threads spend most of their time 98 * blocked on various conditions. However, if the system gets heavy load, 99 * the scheduler has to choose an order to run these. We want the order: 100 * (a) timers, (b) notifications, (c) workers, (d) receivers (and etc.) 101 * where notifications are oplock and change notify work. Aside from this 102 * relative ordering, smbsrv threads should run with a priority close to 103 * that of normal user-space threads (thus minclsyspri below), just like 104 * NFS and other "file service" kinds of processing. 105 */ 106 int smbsrv_base_pri = MINCLSYSPRI; 107 int smbsrv_listen_pri = MINCLSYSPRI; 108 int smbsrv_receive_pri = MINCLSYSPRI; 109 int smbsrv_worker_pri = MINCLSYSPRI + 1; 110 int smbsrv_notify_pri = MINCLSYSPRI + 2; 111 int smbsrv_timer_pri = MINCLSYSPRI + 5; 112 113 114 /* 115 * ***************************************************************************** 116 * ********************** Static Variables / Module Linkage ******************** 117 * ***************************************************************************** 118 */ 119 120 static struct cb_ops cbops = { 121 smb_drv_open, /* cb_open */ 122 smb_drv_close, /* cb_close */ 123 nodev, /* cb_strategy */ 124 nodev, /* cb_print */ 125 nodev, /* cb_dump */ 126 nodev, /* cb_read */ 127 nodev, /* cb_write */ 128 smb_drv_ioctl, /* cb_ioctl */ 129 nodev, /* cb_devmap */ 130 nodev, /* cb_mmap */ 131 nodev, /* cb_segmap */ 132 nochpoll, /* cb_chpoll */ 133 ddi_prop_op, /* cb_prop_op */ 134 NULL, /* cb_streamtab */ 135 D_MP, /* cb_flag */ 136 CB_REV, /* cb_rev */ 137 nodev, /* cb_aread */ 138 nodev, /* cb_awrite */ 139 }; 140 141 static struct dev_ops devops = { 142 DEVO_REV, /* devo_rev */ 143 0, /* devo_refcnt */ 144 smb_drv_getinfo, /* devo_getinfo */ 145 nulldev, /* devo_identify */ 146 nulldev, /* devo_probe */ 147 smb_drv_attach, /* devo_attach */ 148 smb_drv_detach, /* devo_detach */ 149 nodev, /* devo_reset */ 150 &cbops, /* devo_cb_ops */ 151 NULL, /* devo_bus_ops */ 152 NULL, /* devo_power */ 153 ddi_quiesce_not_needed, /* devo_quiesce */ 154 }; 155 156 static struct modldrv modldrv = { 157 &mod_driverops, /* drv_modops */ 158 "CIFS Server Protocol", /* drv_linkinfo */ 159 &devops, 160 }; 161 162 static struct modlinkage modlinkage = { 163 MODREV_1, /* revision of the module, must be: MODREV_1 */ 164 &modldrv, /* ptr to linkage structures */ 165 NULL, 166 }; 167 168 static dev_info_t *smb_drv_dip = NULL; 169 170 /* 171 * **************************************************************************** 172 * Module Interface 173 * **************************************************************************** 174 */ 175 176 int 177 _init(void) 178 { 179 int rc; 180 181 if ((rc = smb_kshare_init()) != 0) 182 return (rc); 183 184 if ((rc = smb_server_svc_init()) != 0) { 185 smb_kshare_fini(); 186 return (rc); 187 } 188 189 if ((rc = mod_install(&modlinkage)) != 0) { 190 smb_kshare_fini(); 191 (void) smb_server_svc_fini(); 192 } 193 194 return (rc); 195 } 196 197 int 198 _info(struct modinfo *modinfop) 199 { 200 return (mod_info(&modlinkage, modinfop)); 201 } 202 203 int 204 _fini(void) 205 { 206 int rc; 207 208 if ((rc = mod_remove(&modlinkage)) == 0) { 209 rc = smb_server_svc_fini(); 210 smb_kshare_fini(); 211 } 212 213 return (rc); 214 } 215 216 /* 217 * **************************************************************************** 218 * Pseudo Device Entry Points 219 * **************************************************************************** 220 */ 221 /* ARGSUSED */ 222 static int 223 smb_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp) 224 { 225 /* 226 * Check caller's privileges. 227 */ 228 if (secpolicy_smb(credp) != 0) 229 return (EPERM); 230 231 /* 232 * Start SMB service state machine 233 */ 234 return (smb_server_create()); 235 } 236 237 /* ARGSUSED */ 238 static int 239 smb_drv_close(dev_t dev, int flag, int otyp, cred_t *credp) 240 { 241 return (smb_server_delete()); 242 } 243 244 /* ARGSUSED */ 245 static int 246 smb_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flags, cred_t *cred, 247 int *retval) 248 { 249 smb_ioc_t *ioc; 250 smb_ioc_header_t ioc_hdr; 251 uint32_t crc; 252 boolean_t copyout = B_FALSE; 253 int rc = 0; 254 255 if (ddi_copyin((const void *)argp, &ioc_hdr, sizeof (smb_ioc_header_t), 256 flags) || (ioc_hdr.version != SMB_IOC_VERSION)) 257 return (EFAULT); 258 259 crc = ioc_hdr.crc; 260 ioc_hdr.crc = 0; 261 if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc) 262 return (EFAULT); 263 264 ioc = kmem_alloc(ioc_hdr.len, KM_SLEEP); 265 if (ddi_copyin((const void *)argp, ioc, ioc_hdr.len, flags)) { 266 kmem_free(ioc, ioc_hdr.len); 267 return (EFAULT); 268 } 269 270 switch (cmd) { 271 case SMB_IOC_CONFIG: 272 rc = smb_server_configure(&ioc->ioc_cfg); 273 break; 274 case SMB_IOC_START: 275 rc = smb_server_start(&ioc->ioc_start); 276 break; 277 case SMB_IOC_STOP: 278 rc = smb_server_stop(); 279 break; 280 case SMB_IOC_EVENT: 281 rc = smb_server_notify_event(&ioc->ioc_event); 282 break; 283 case SMB_IOC_GMTOFF: 284 rc = smb_server_set_gmtoff(&ioc->ioc_gmt); 285 break; 286 case SMB_IOC_SHARE: 287 rc = smb_kshare_export_list(&ioc->ioc_share); 288 break; 289 case SMB_IOC_UNSHARE: 290 rc = smb_kshare_unexport_list(&ioc->ioc_share); 291 break; 292 case SMB_IOC_SHAREINFO: 293 rc = smb_kshare_info(&ioc->ioc_shareinfo); 294 copyout = B_TRUE; 295 break; 296 case SMB_IOC_NUMOPEN: 297 rc = smb_server_numopen(&ioc->ioc_opennum); 298 copyout = B_TRUE; 299 break; 300 case SMB_IOC_SVCENUM: 301 rc = smb_server_enum(&ioc->ioc_svcenum); 302 copyout = B_TRUE; 303 break; 304 case SMB_IOC_SESSION_CLOSE: 305 rc = smb_server_session_close(&ioc->ioc_session); 306 break; 307 case SMB_IOC_FILE_CLOSE: 308 rc = smb_server_file_close(&ioc->ioc_fileid); 309 break; 310 case SMB_IOC_SPOOLDOC: 311 rc = smb_server_spooldoc(&ioc->ioc_spooldoc); 312 copyout = B_TRUE; 313 break; 314 default: 315 rc = ENOTTY; 316 break; 317 } 318 if ((rc == 0) && copyout) { 319 if (ddi_copyout((const void *)ioc, (void *)argp, ioc_hdr.len, 320 flags)) 321 rc = EFAULT; 322 } 323 kmem_free(ioc, ioc_hdr.len); 324 return (rc); 325 } 326 327 /* 328 * **************************************************************************** 329 * Pseudo Device Operations 330 * **************************************************************************** 331 */ 332 static int 333 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 334 { 335 if (cmd == DDI_ATTACH) { 336 /* we only allow instance 0 to attach */ 337 if (ddi_get_instance(dip) == 0) { 338 /* create the minor node */ 339 if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0, 340 DDI_PSEUDO, 0) == DDI_SUCCESS) { 341 smb_drv_dip = dip; 342 return (DDI_SUCCESS); 343 } else { 344 cmn_err(CE_WARN, "smb_drv_attach:" 345 " failed creating minor node"); 346 } 347 } 348 } 349 return (DDI_FAILURE); 350 } 351 352 static int 353 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 354 { 355 if (cmd == DDI_DETACH) { 356 ASSERT(dip == smb_drv_dip); 357 ddi_remove_minor_node(dip, NULL); 358 smb_drv_dip = NULL; 359 return (DDI_SUCCESS); 360 } 361 return (DDI_FAILURE); 362 } 363 364 /* ARGSUSED */ 365 static int 366 smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 367 { 368 ulong_t instance = getminor((dev_t)arg); 369 370 switch (cmd) { 371 case DDI_INFO_DEVT2DEVINFO: 372 *result = smb_drv_dip; 373 return (DDI_SUCCESS); 374 375 case DDI_INFO_DEVT2INSTANCE: 376 *result = (void *)instance; 377 return (DDI_SUCCESS); 378 379 default: 380 break; 381 } 382 383 return (DDI_FAILURE); 384 } 385