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