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 2015-2023 RackTop Systems, Inc. 25 * Copyright 2019 Joyent, Inc. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/conf.h> 30 #include <sys/ddi.h> 31 #include <sys/modctl.h> 32 #include <sys/cred.h> 33 #include <sys/disp.h> 34 #include <sys/ioccom.h> 35 #include <sys/policy.h> 36 #include <sys/cmn_err.h> 37 #include <smbsrv/smb_kproto.h> 38 #include <smbsrv/smb_ioctl.h> 39 40 #ifdef _FAKE_KERNEL 41 #error "See libfksmbsrv" 42 #endif /* _FAKE_KERNEL */ 43 44 static int smb_drv_open(dev_t *, int, int, cred_t *); 45 static int smb_drv_close(dev_t, int, int, cred_t *); 46 static int smb_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 47 static int smb_drv_attach(dev_info_t *, ddi_attach_cmd_t); 48 static int smb_drv_detach(dev_info_t *, ddi_detach_cmd_t); 49 static int smb_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 50 51 /* 52 * ***************************************************************************** 53 * ****************************** Global Variables ***************************** 54 * ***************************************************************************** 55 * 56 * These variables can only be changed through the /etc/system file. 57 */ 58 59 /* 60 * Maximum buffer size for NT: configurable based on the client environment. 61 * IR104720 Experiments with Windows 2000 indicate that we achieve better 62 * SmbWriteX performance with a buffer size of 64KB instead of the 37KB used 63 * with Windows NT4.0. Previous experiments with NT4.0 resulted in directory 64 * listing problems so this buffer size is configurable based on the end-user 65 * environment. When in doubt use 37KB. 66 */ 67 int smb_maxbufsize = SMB_NT_MAXBUF; 68 int smb_flush_required = 1; 69 int smb_dirsymlink_enable = 1; 70 int smb_sign_debug = 0; 71 uint_t smb_audit_flags = 72 #ifdef DEBUG 73 SMB_AUDIT_NODE; 74 #else 75 0; 76 #endif 77 78 int smb_allow_advisory_locks = 0; /* See smb_vops.c */ 79 80 /* 81 * Maximum number of simultaneous authentication, share mapping, pipe open 82 * requests to be processed. 83 */ 84 int smb_ssetup_threshold = SMB_AUTHSVC_MAXTHREAD; 85 int smb_tcon_threshold = 1024; 86 int smb_opipe_threshold = 1024; 87 88 /* 89 * Number of milliseconds that a request will be stalled if it comes in after 90 * the maximum number of inflight operations are being proccessed. 91 */ 92 int smb_ssetup_timeout = (30 * 1000); 93 int smb_tcon_timeout = (30 * 1000); 94 int smb_opipe_timeout = (30 * 1000); 95 96 /* 97 * Thread priorities used in smbsrv. 98 * 99 * The SMB server runs at a priority a little below the maximum for 100 * user-level process threads so it won't monopolize the CPU. 101 * Todo: make this configurable 102 * 103 * Aside from that, we want these relative priorities: (a) timers, 104 * (b) notify + oplock completions, (c) workers, (d) receivers, etc. 105 * The "base" is somewhat arbirary, and what shows up in prstat 106 * because it's used for the main thread in newproc(). 107 */ 108 int smbsrv_timer_pri = MINCLSYSPRI; /* smb_server_timers */ 109 int smbsrv_base_pri = MINCLSYSPRI - 1; /* kshare thread, newproc */ 110 int smbsrv_notify_pri = MINCLSYSPRI - 1; /* oplocks, notify */ 111 /* Gap in which user-level administrative stuff runs. */ 112 int smbsrv_worker_pri = MINCLSYSPRI - 7; 113 int smbsrv_receive_pri = MINCLSYSPRI - 8; 114 int smbsrv_listen_pri = MINCLSYSPRI - 9; 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 smb_server_t *sv; 254 int rc; 255 256 rc = smb_server_lookup(&sv); 257 if (rc == 0) 258 rc = smb_server_delete(sv); 259 260 return (rc); 261 } 262 263 /* ARGSUSED */ 264 static int 265 smb_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flags, cred_t *cred, 266 int *retval) 267 { 268 smb_ioc_t *ioc; 269 smb_ioc_header_t ioc_hdr; 270 uint32_t crc; 271 boolean_t copyout = B_FALSE; 272 int rc = 0; 273 size_t alloclen; 274 275 if (ddi_copyin((void *)argp, &ioc_hdr, sizeof (ioc_hdr), flags)) 276 return (EFAULT); 277 278 /* 279 * Check version and length. 280 * 281 * Note that some ioctls (i.e. SMB_IOC_SVCENUM) have payload 282 * data after the ioctl struct, in which case they specify a 283 * length much larger than sizeof smb_ioc_t. The theoretical 284 * largest ioctl data is therefore the size of the union plus 285 * the max size of the payload (which is SMB_IOC_DATA_SIZE). 286 */ 287 if (ioc_hdr.version != SMB_IOC_VERSION || 288 ioc_hdr.len < sizeof (ioc_hdr) || 289 ioc_hdr.len > (sizeof (*ioc) + SMB_IOC_DATA_SIZE)) 290 return (EINVAL); 291 292 crc = ioc_hdr.crc; 293 ioc_hdr.crc = 0; 294 if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc) 295 return (EINVAL); 296 297 /* 298 * Note that smb_ioc_t is a union, and callers set ioc_hdr.len 299 * to the size of the actual union arm. If some caller were to 300 * set that size too small, we could end up passing under-sized 301 * memory to one of the type-specific handler functions. Avoid 302 * that problem by allocating at least the size of the union, 303 * (zeroed out) and then copy in the caller specified length. 304 */ 305 alloclen = MAX(ioc_hdr.len, sizeof (*ioc)); 306 ioc = kmem_zalloc(alloclen, KM_SLEEP); 307 if (ddi_copyin((void *)argp, ioc, ioc_hdr.len, flags)) { 308 kmem_free(ioc, alloclen); 309 return (EFAULT); 310 } 311 312 /* Don't allow the request size to change mid-ioctl */ 313 if (ioc_hdr.len != ioc->ioc_hdr.len) { 314 kmem_free(ioc, alloclen); 315 return (EINVAL); 316 } 317 318 switch (cmd) { 319 case SMB_IOC_CONFIG: 320 rc = smb_server_configure(&ioc->ioc_cfg); 321 break; 322 case SMB_IOC_START: 323 rc = smb_server_start(&ioc->ioc_start); 324 break; 325 case SMB_IOC_STOP: 326 rc = smb_server_stop(); 327 break; 328 case SMB_IOC_EVENT: 329 rc = smb_server_notify_event(&ioc->ioc_event); 330 break; 331 case SMB_IOC_GMTOFF: 332 rc = smb_server_set_gmtoff(&ioc->ioc_gmt); 333 break; 334 case SMB_IOC_SHARE: 335 rc = smb_kshare_export_list(&ioc->ioc_share); 336 break; 337 case SMB_IOC_UNSHARE: 338 rc = smb_kshare_unexport_list(&ioc->ioc_share); 339 break; 340 case SMB_IOC_SHAREINFO: 341 rc = smb_kshare_info(&ioc->ioc_shareinfo); 342 copyout = B_TRUE; 343 break; 344 case SMB_IOC_SHAREACCESS: 345 rc = smb_kshare_access(&ioc->ioc_shareaccess); 346 break; 347 case SMB_IOC_NUMOPEN: 348 rc = smb_server_numopen(&ioc->ioc_opennum); 349 copyout = B_TRUE; 350 break; 351 case SMB_IOC_SVCENUM: 352 rc = smb_server_enum(&ioc->ioc_svcenum); 353 copyout = B_TRUE; 354 break; 355 case SMB_IOC_SESSION_CLOSE: 356 rc = smb_server_session_close(&ioc->ioc_session); 357 break; 358 case SMB_IOC_FILE_CLOSE: 359 rc = smb_server_file_close(&ioc->ioc_fileid); 360 break; 361 case SMB_IOC_SPOOLDOC: 362 rc = smb_server_spooldoc(&ioc->ioc_spooldoc); 363 copyout = B_TRUE; 364 break; 365 default: 366 rc = ENOTTY; 367 break; 368 } 369 if ((rc == 0) && copyout) { 370 if (ddi_copyout(ioc, (void *)argp, ioc_hdr.len, flags)) 371 rc = EFAULT; 372 } 373 kmem_free(ioc, alloclen); 374 return (rc); 375 } 376 377 /* 378 * **************************************************************************** 379 * Pseudo Device Operations 380 * **************************************************************************** 381 */ 382 static int 383 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 384 { 385 if (cmd == DDI_ATTACH) { 386 /* we only allow instance 0 to attach */ 387 if (ddi_get_instance(dip) == 0) { 388 /* create the minor node */ 389 if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0, 390 DDI_PSEUDO, 0) == DDI_SUCCESS) { 391 smb_drv_dip = dip; 392 return (DDI_SUCCESS); 393 } else { 394 cmn_err(CE_WARN, "smb_drv_attach:" 395 " failed creating minor node"); 396 } 397 } 398 } 399 return (DDI_FAILURE); 400 } 401 402 static int 403 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 404 { 405 if (cmd == DDI_DETACH) { 406 ASSERT(dip == smb_drv_dip); 407 ddi_remove_minor_node(dip, NULL); 408 smb_drv_dip = NULL; 409 return (DDI_SUCCESS); 410 } 411 return (DDI_FAILURE); 412 } 413 414 /* ARGSUSED */ 415 static int 416 smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 417 { 418 ulong_t instance = getminor((dev_t)arg); 419 420 switch (cmd) { 421 case DDI_INFO_DEVT2DEVINFO: 422 *result = smb_drv_dip; 423 return (DDI_SUCCESS); 424 425 case DDI_INFO_DEVT2INSTANCE: 426 *result = (void *)instance; 427 return (DDI_SUCCESS); 428 429 default: 430 break; 431 } 432 433 return (DDI_FAILURE); 434 } 435