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 /* 95 * Thread priorities used in smbsrv. Our threads spend most of their time 96 * blocked on various conditions. However, if the system gets heavy load, 97 * the scheduler has to choose an order to run these. We want the order: 98 * (a) timers, (b) notifications, (c) workers, (d) receivers (and etc.) 99 * where notifications are oplock and change notify work. Aside from this 100 * relative ordering, smbsrv threads should run with a priority close to 101 * that of normal user-space threads (thus minclsyspri below), just like 102 * NFS and other "file service" kinds of processing. 103 */ 104 int smbsrv_base_pri = MINCLSYSPRI; 105 int smbsrv_listen_pri = MINCLSYSPRI; 106 int smbsrv_receive_pri = MINCLSYSPRI; 107 int smbsrv_worker_pri = MINCLSYSPRI + 1; 108 int smbsrv_notify_pri = MINCLSYSPRI + 2; 109 int smbsrv_timer_pri = MINCLSYSPRI + 5; 110 111 112 /* 113 * ***************************************************************************** 114 * ********************** Static Variables / Module Linkage ******************** 115 * ***************************************************************************** 116 */ 117 118 static struct cb_ops cbops = { 119 smb_drv_open, /* cb_open */ 120 smb_drv_close, /* cb_close */ 121 nodev, /* cb_strategy */ 122 nodev, /* cb_print */ 123 nodev, /* cb_dump */ 124 nodev, /* cb_read */ 125 nodev, /* cb_write */ 126 smb_drv_ioctl, /* cb_ioctl */ 127 nodev, /* cb_devmap */ 128 nodev, /* cb_mmap */ 129 nodev, /* cb_segmap */ 130 nochpoll, /* cb_chpoll */ 131 ddi_prop_op, /* cb_prop_op */ 132 NULL, /* cb_streamtab */ 133 D_MP, /* cb_flag */ 134 CB_REV, /* cb_rev */ 135 nodev, /* cb_aread */ 136 nodev, /* cb_awrite */ 137 }; 138 139 static struct dev_ops devops = { 140 DEVO_REV, /* devo_rev */ 141 0, /* devo_refcnt */ 142 smb_drv_getinfo, /* devo_getinfo */ 143 nulldev, /* devo_identify */ 144 nulldev, /* devo_probe */ 145 smb_drv_attach, /* devo_attach */ 146 smb_drv_detach, /* devo_detach */ 147 nodev, /* devo_reset */ 148 &cbops, /* devo_cb_ops */ 149 NULL, /* devo_bus_ops */ 150 NULL, /* devo_power */ 151 ddi_quiesce_not_needed, /* devo_quiesce */ 152 }; 153 154 static struct modldrv modldrv = { 155 &mod_driverops, /* drv_modops */ 156 "CIFS Server Protocol", /* drv_linkinfo */ 157 &devops, 158 }; 159 160 static struct modlinkage modlinkage = { 161 MODREV_1, /* revision of the module, must be: MODREV_1 */ 162 &modldrv, /* ptr to linkage structures */ 163 NULL, 164 }; 165 166 static dev_info_t *smb_drv_dip = NULL; 167 168 /* 169 * **************************************************************************** 170 * Module Interface 171 * **************************************************************************** 172 */ 173 174 int 175 _init(void) 176 { 177 int rc; 178 179 if ((rc = smb_server_g_init()) != 0) { 180 return (rc); 181 } 182 183 if ((rc = mod_install(&modlinkage)) != 0) { 184 (void) smb_server_g_fini(); 185 } 186 187 return (rc); 188 } 189 190 int 191 _info(struct modinfo *modinfop) 192 { 193 return (mod_info(&modlinkage, modinfop)); 194 } 195 196 int 197 _fini(void) 198 { 199 int rc; 200 201 if ((rc = mod_remove(&modlinkage)) == 0) { 202 rc = smb_server_g_fini(); 203 } 204 205 return (rc); 206 } 207 208 /* 209 * **************************************************************************** 210 * Pseudo Device Entry Points 211 * **************************************************************************** 212 */ 213 /* ARGSUSED */ 214 static int 215 smb_drv_open(dev_t *devp, int flag, int otyp, cred_t *cr) 216 { 217 zoneid_t zid; 218 219 /* 220 * Check caller's privileges. 221 */ 222 if (secpolicy_smb(cr) != 0) 223 return (EPERM); 224 225 /* 226 * We need a unique minor per zone otherwise an smbd in any other 227 * zone will keep this minor open and we won't get a close call. 228 * The zone ID is good enough as a minor number. 229 */ 230 zid = crgetzoneid(cr); 231 if (zid < 0) 232 return (ENODEV); 233 *devp = makedevice(getmajor(*devp), zid); 234 235 /* 236 * Start SMB service state machine 237 */ 238 return (smb_server_create()); 239 } 240 241 /* ARGSUSED */ 242 static int 243 smb_drv_close(dev_t dev, int flag, int otyp, cred_t *credp) 244 { 245 return (smb_server_delete()); 246 } 247 248 /* ARGSUSED */ 249 static int 250 smb_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flags, cred_t *cred, 251 int *retval) 252 { 253 smb_ioc_t *ioc; 254 smb_ioc_header_t ioc_hdr; 255 uint32_t crc; 256 boolean_t copyout = B_FALSE; 257 int rc = 0; 258 259 if (ddi_copyin((const void *)argp, &ioc_hdr, sizeof (smb_ioc_header_t), 260 flags) || (ioc_hdr.version != SMB_IOC_VERSION)) 261 return (EFAULT); 262 263 crc = ioc_hdr.crc; 264 ioc_hdr.crc = 0; 265 if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc) 266 return (EFAULT); 267 268 ioc = kmem_alloc(ioc_hdr.len, KM_SLEEP); 269 if (ddi_copyin((const void *)argp, ioc, ioc_hdr.len, flags)) { 270 kmem_free(ioc, ioc_hdr.len); 271 return (EFAULT); 272 } 273 274 switch (cmd) { 275 case SMB_IOC_CONFIG: 276 rc = smb_server_configure(&ioc->ioc_cfg); 277 break; 278 case SMB_IOC_START: 279 rc = smb_server_start(&ioc->ioc_start); 280 break; 281 case SMB_IOC_STOP: 282 rc = smb_server_stop(); 283 break; 284 case SMB_IOC_EVENT: 285 rc = smb_server_notify_event(&ioc->ioc_event); 286 break; 287 case SMB_IOC_GMTOFF: 288 rc = smb_server_set_gmtoff(&ioc->ioc_gmt); 289 break; 290 case SMB_IOC_SHARE: 291 rc = smb_kshare_export_list(&ioc->ioc_share); 292 break; 293 case SMB_IOC_UNSHARE: 294 rc = smb_kshare_unexport_list(&ioc->ioc_share); 295 break; 296 case SMB_IOC_SHAREINFO: 297 rc = smb_kshare_info(&ioc->ioc_shareinfo); 298 copyout = B_TRUE; 299 break; 300 case SMB_IOC_NUMOPEN: 301 rc = smb_server_numopen(&ioc->ioc_opennum); 302 copyout = B_TRUE; 303 break; 304 case SMB_IOC_SVCENUM: 305 rc = smb_server_enum(&ioc->ioc_svcenum); 306 copyout = B_TRUE; 307 break; 308 case SMB_IOC_SESSION_CLOSE: 309 rc = smb_server_session_close(&ioc->ioc_session); 310 break; 311 case SMB_IOC_FILE_CLOSE: 312 rc = smb_server_file_close(&ioc->ioc_fileid); 313 break; 314 case SMB_IOC_SPOOLDOC: 315 rc = smb_server_spooldoc(&ioc->ioc_spooldoc); 316 copyout = B_TRUE; 317 break; 318 default: 319 rc = ENOTTY; 320 break; 321 } 322 if ((rc == 0) && copyout) { 323 if (ddi_copyout((const void *)ioc, (void *)argp, ioc_hdr.len, 324 flags)) 325 rc = EFAULT; 326 } 327 kmem_free(ioc, ioc_hdr.len); 328 return (rc); 329 } 330 331 /* 332 * **************************************************************************** 333 * Pseudo Device Operations 334 * **************************************************************************** 335 */ 336 static int 337 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 338 { 339 if (cmd == DDI_ATTACH) { 340 /* we only allow instance 0 to attach */ 341 if (ddi_get_instance(dip) == 0) { 342 /* create the minor node */ 343 if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0, 344 DDI_PSEUDO, 0) == DDI_SUCCESS) { 345 smb_drv_dip = dip; 346 return (DDI_SUCCESS); 347 } else { 348 cmn_err(CE_WARN, "smb_drv_attach:" 349 " failed creating minor node"); 350 } 351 } 352 } 353 return (DDI_FAILURE); 354 } 355 356 static int 357 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 358 { 359 if (cmd == DDI_DETACH) { 360 ASSERT(dip == smb_drv_dip); 361 ddi_remove_minor_node(dip, NULL); 362 smb_drv_dip = NULL; 363 return (DDI_SUCCESS); 364 } 365 return (DDI_FAILURE); 366 } 367 368 /* ARGSUSED */ 369 static int 370 smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 371 { 372 ulong_t instance = getminor((dev_t)arg); 373 374 switch (cmd) { 375 case DDI_INFO_DEVT2DEVINFO: 376 *result = smb_drv_dip; 377 return (DDI_SUCCESS); 378 379 case DDI_INFO_DEVT2INSTANCE: 380 *result = (void *)instance; 381 return (DDI_SUCCESS); 382 383 default: 384 break; 385 } 386 387 return (DDI_FAILURE); 388 } 389