Print this page
13992 VNIC in Zone loses access to properties
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/io/dld/dld_drv.c
+++ new/usr/src/uts/common/io/dld/dld_drv.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2015, Joyent Inc.
24 24 * Copyright (c) 2017, Joyent, Inc.
25 + * Copyright 2025 MNX Cloud, Inc.
25 26 */
26 27
27 28 /*
28 29 * Data-Link Driver
29 30 */
30 31
31 32 #include <sys/conf.h>
32 33 #include <sys/mkdev.h>
33 34 #include <sys/modctl.h>
34 35 #include <sys/stat.h>
35 36 #include <sys/dld_impl.h>
36 37 #include <sys/dld_ioc.h>
37 38 #include <sys/dls_impl.h>
38 39 #include <sys/softmac.h>
39 40 #include <sys/mac.h>
40 41 #include <sys/mac_ether.h>
41 42 #include <sys/mac_client.h>
42 43 #include <sys/mac_client_impl.h>
43 44 #include <sys/mac_client_priv.h>
44 45 #include <inet/common.h>
45 46 #include <sys/policy.h>
46 47 #include <sys/priv_names.h>
47 48 #include <sys/zone.h>
48 49 #include <sys/sysmacros.h>
49 50
50 51 static void drv_init(void);
51 52 static int drv_fini(void);
52 53
53 54 static int drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
54 55 static int drv_attach(dev_info_t *, ddi_attach_cmd_t);
55 56 static int drv_detach(dev_info_t *, ddi_detach_cmd_t);
56 57
57 58 /*
58 59 * Secure objects declarations
59 60 */
60 61 #define SECOBJ_WEP_HASHSZ 67
61 62 static krwlock_t drv_secobj_lock;
62 63 static kmem_cache_t *drv_secobj_cachep;
63 64 static mod_hash_t *drv_secobj_hash;
64 65 static void drv_secobj_init(void);
65 66 static void drv_secobj_fini(void);
66 67 static int drv_ioc_setap(datalink_id_t, struct dlautopush *);
67 68 static int drv_ioc_getap(datalink_id_t, struct dlautopush *);
68 69 static int drv_ioc_clrap(datalink_id_t);
69 70
70 71
71 72 /*
72 73 * The following entry points are private to dld and are used for control
73 74 * operations only. The entry points exported to mac drivers are defined
74 75 * in dld_str.c. Refer to the comment on top of dld_str.c for details.
75 76 */
76 77 static int drv_open(dev_t *, int, int, cred_t *);
77 78 static int drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
78 79
79 80 static dev_info_t *dld_dip; /* dev_info_t for the driver */
80 81 uint32_t dld_opt = 0; /* Global options */
81 82
82 83 #define NAUTOPUSH 32
83 84 static mod_hash_t *dld_ap_hashp;
84 85 static krwlock_t dld_ap_hash_lock;
85 86
86 87 static struct cb_ops drv_cb_ops = {
87 88 drv_open, /* open */
88 89 nulldev, /* close */
89 90 nulldev, /* strategy */
90 91 nulldev, /* print */
91 92 nodev, /* dump */
92 93 nodev, /* read */
93 94 nodev, /* write */
94 95 drv_ioctl, /* ioctl */
95 96 nodev, /* devmap */
96 97 nodev, /* mmap */
97 98 nodev, /* segmap */
98 99 nochpoll, /* poll */
99 100 ddi_prop_op, /* cb_prop_op */
100 101 0, /* streamtab */
101 102 D_MP /* Driver compatibility flag */
102 103 };
103 104
104 105 static struct dev_ops drv_ops = {
105 106 DEVO_REV, /* devo_rev */
106 107 0, /* refcnt */
107 108 drv_getinfo, /* get_dev_info */
108 109 nulldev, /* identify */
109 110 nulldev, /* probe */
110 111 drv_attach, /* attach */
111 112 drv_detach, /* detach */
112 113 nodev, /* reset */
113 114 &drv_cb_ops, /* driver operations */
114 115 NULL, /* bus operations */
115 116 nodev, /* dev power */
116 117 ddi_quiesce_not_supported, /* dev quiesce */
117 118 };
118 119
119 120 /*
120 121 * Module linkage information for the kernel.
121 122 */
122 123 static struct modldrv drv_modldrv = {
123 124 &mod_driverops,
124 125 DLD_INFO,
125 126 &drv_ops
126 127 };
127 128
128 129 static struct modlinkage drv_modlinkage = {
129 130 MODREV_1,
130 131 &drv_modldrv,
131 132 NULL
132 133 };
133 134
134 135 int
135 136 _init(void)
136 137 {
137 138 return (mod_install(&drv_modlinkage));
138 139 }
139 140
140 141 int
141 142 _fini(void)
142 143 {
143 144 return (mod_remove(&drv_modlinkage));
144 145 }
145 146
146 147 int
147 148 _info(struct modinfo *modinfop)
148 149 {
149 150 return (mod_info(&drv_modlinkage, modinfop));
150 151 }
151 152
152 153 /*
153 154 * Initialize component modules.
154 155 */
155 156 static void
156 157 drv_init(void)
157 158 {
158 159 drv_secobj_init();
159 160 dld_str_init();
160 161
161 162 /*
162 163 * Create a hash table for autopush configuration.
163 164 */
164 165 dld_ap_hashp = mod_hash_create_idhash("dld_autopush_hash",
165 166 NAUTOPUSH, mod_hash_null_valdtor);
166 167
167 168 ASSERT(dld_ap_hashp != NULL);
168 169 rw_init(&dld_ap_hash_lock, NULL, RW_DRIVER, NULL);
169 170 }
170 171
171 172 /* ARGSUSED */
172 173 static uint_t
173 174 drv_ap_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
174 175 {
175 176 boolean_t *pexist = arg;
176 177
177 178 *pexist = B_TRUE;
178 179 return (MH_WALK_TERMINATE);
179 180 }
180 181
181 182 static int
182 183 drv_fini(void)
183 184 {
184 185 int err;
185 186 boolean_t exist = B_FALSE;
186 187
187 188 rw_enter(&dld_ap_hash_lock, RW_READER);
188 189 mod_hash_walk(dld_ap_hashp, drv_ap_exist, &exist);
189 190 rw_exit(&dld_ap_hash_lock);
190 191 if (exist)
191 192 return (EBUSY);
192 193
193 194 if ((err = dld_str_fini()) != 0)
194 195 return (err);
195 196
196 197 drv_secobj_fini();
197 198 mod_hash_destroy_idhash(dld_ap_hashp);
198 199 rw_destroy(&dld_ap_hash_lock);
199 200 return (0);
200 201 }
201 202
202 203 /*
203 204 * devo_getinfo: getinfo(9e)
204 205 */
205 206 /*ARGSUSED*/
206 207 static int
207 208 drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
208 209 {
209 210 if (dld_dip == NULL)
210 211 return (DDI_FAILURE);
211 212
212 213 switch (cmd) {
213 214 case DDI_INFO_DEVT2INSTANCE:
214 215 *resp = 0;
215 216 break;
216 217 case DDI_INFO_DEVT2DEVINFO:
217 218 *resp = dld_dip;
218 219 break;
219 220 default:
220 221 return (DDI_FAILURE);
221 222 }
222 223
223 224 return (DDI_SUCCESS);
224 225 }
225 226
226 227 /*
227 228 * Check properties to set options. (See dld.h for property definitions).
228 229 */
229 230 static void
230 231 drv_set_opt(dev_info_t *dip)
231 232 {
232 233 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
233 234 DLD_PROP_NO_FASTPATH, 0) != 0) {
234 235 dld_opt |= DLD_OPT_NO_FASTPATH;
235 236 }
236 237
237 238 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
238 239 DLD_PROP_NO_POLL, 0) != 0) {
239 240 dld_opt |= DLD_OPT_NO_POLL;
240 241 }
241 242
242 243 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
243 244 DLD_PROP_NO_ZEROCOPY, 0) != 0) {
244 245 dld_opt |= DLD_OPT_NO_ZEROCOPY;
245 246 }
246 247
247 248 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
248 249 DLD_PROP_NO_SOFTRING, 0) != 0) {
249 250 dld_opt |= DLD_OPT_NO_SOFTRING;
250 251 }
251 252 }
252 253
253 254 /*
254 255 * devo_attach: attach(9e)
255 256 */
256 257 static int
257 258 drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
258 259 {
259 260 if (cmd != DDI_ATTACH)
260 261 return (DDI_FAILURE);
261 262
262 263 ASSERT(ddi_get_instance(dip) == 0);
263 264 drv_init();
264 265 drv_set_opt(dip);
265 266
266 267 /*
267 268 * Create control node. DLPI provider nodes will be created on demand.
268 269 */
269 270 if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR,
270 271 DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS)
271 272 return (DDI_FAILURE);
272 273
273 274 dld_dip = dip;
274 275
275 276 /*
276 277 * Log the fact that the driver is now attached.
277 278 */
278 279 ddi_report_dev(dip);
279 280 return (DDI_SUCCESS);
280 281 }
281 282
282 283 /*
283 284 * devo_detach: detach(9e)
284 285 */
285 286 static int
286 287 drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
287 288 {
288 289 if (cmd != DDI_DETACH)
289 290 return (DDI_FAILURE);
290 291
291 292 ASSERT(dld_dip == dip);
292 293 if (drv_fini() != 0)
293 294 return (DDI_FAILURE);
294 295
295 296 /*
296 297 * Remove the control node.
297 298 */
298 299 ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME);
299 300 dld_dip = NULL;
300 301
301 302 return (DDI_SUCCESS);
302 303 }
303 304
304 305 /*
305 306 * dld control node open procedure.
306 307 */
307 308 /*ARGSUSED*/
308 309 static int
309 310 drv_open(dev_t *devp, int flag, int sflag, cred_t *credp)
310 311 {
311 312 /*
312 313 * Only the control node can be opened.
313 314 */
314 315 if (getminor(*devp) != DLD_CONTROL_MINOR)
315 316 return (ENODEV);
316 317 return (0);
317 318 }
318 319
319 320 /*
320 321 * Verify if the caller is allowed to modify a link of the given class.
321 322 */
322 323 static int
323 324 drv_ioc_checkprivs(datalink_class_t class, cred_t *cred)
324 325 {
325 326 if (class == DATALINK_CLASS_IPTUN)
326 327 return (secpolicy_iptun_config(cred));
327 328 return (secpolicy_dl_config(cred));
328 329 }
329 330
330 331 /*
331 332 * DLDIOC_ATTR
332 333 */
333 334 /* ARGSUSED */
334 335 static int
335 336 drv_ioc_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
336 337 {
337 338 dld_ioc_attr_t *diap = karg;
338 339 dls_dl_handle_t dlh;
339 340 dls_link_t *dlp;
340 341 zoneid_t zoneid = crgetzoneid(cred);
341 342 int err;
342 343 mac_perim_handle_t mph;
343 344
344 345 if (zoneid != GLOBAL_ZONEID &&
345 346 zone_check_datalink(&zoneid, diap->dia_linkid) != 0)
346 347 return (ENOENT);
347 348
348 349 if ((err = dls_devnet_hold_tmp(diap->dia_linkid, &dlh)) != 0)
349 350 return (err);
350 351
351 352 if ((err = mac_perim_enter_by_macname(dls_devnet_mac(dlh),
352 353 &mph)) != 0) {
353 354 dls_devnet_rele_tmp(dlh);
354 355 return (err);
355 356 }
356 357
357 358 if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0) {
358 359 mac_perim_exit(mph);
359 360 dls_devnet_rele_tmp(dlh);
360 361 return (err);
361 362 }
362 363
363 364 mac_sdu_get(dlp->dl_mh, NULL, &diap->dia_max_sdu);
364 365 dls_link_rele(dlp);
365 366 mac_perim_exit(mph);
366 367 dls_devnet_rele_tmp(dlh);
367 368
368 369 return (0);
369 370 }
370 371
371 372 /*
372 373 * DLDIOC_PHYS_ATTR
373 374 */
374 375 /* ARGSUSED */
375 376 static int
376 377 drv_ioc_phys_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
377 378 {
378 379 dld_ioc_phys_attr_t *dipp = karg;
379 380 int err;
380 381 dls_dl_handle_t dlh;
381 382 dls_dev_handle_t ddh;
382 383 dev_t phydev;
383 384 zoneid_t zoneid = crgetzoneid(cred);
384 385
385 386 if (zoneid != GLOBAL_ZONEID &&
386 387 zone_check_datalink(&zoneid, dipp->dip_linkid) != 0)
387 388 return (ENOENT);
388 389
389 390 /*
390 391 * Every physical link should have its physical dev_t kept in the
391 392 * daemon. If not, it is not a valid physical link.
392 393 */
393 394 if (dls_mgmt_get_phydev(dipp->dip_linkid, &phydev) != 0)
394 395 return (EINVAL);
395 396
396 397 /*
397 398 * Although this is a valid physical link, it might already be removed
398 399 * by DR or during system shutdown. softmac_hold_device() would return
399 400 * ENOENT in this case.
400 401 */
401 402 if ((err = softmac_hold_device(phydev, &ddh)) != 0)
402 403 return (err);
403 404
404 405 if (dls_devnet_hold_tmp(dipp->dip_linkid, &dlh) != 0) {
405 406 /*
406 407 * Although this is an active physical link, its link type is
407 408 * not supported by GLDv3, and therefore it does not have
408 409 * vanity naming support.
409 410 */
410 411 dipp->dip_novanity = B_TRUE;
411 412 } else {
412 413 dipp->dip_novanity = B_FALSE;
413 414 dls_devnet_rele_tmp(dlh);
414 415 }
415 416 /*
416 417 * Get the physical device name from the major number and the instance
417 418 * number derived from phydev.
418 419 */
419 420 (void) snprintf(dipp->dip_dev, MAXLINKNAMELEN, "%s%d",
420 421 ddi_major_to_name(getmajor(phydev)), getminor(phydev) - 1);
421 422
422 423 softmac_rele_device(ddh);
423 424 return (0);
424 425 }
425 426
426 427 /* ARGSUSED */
427 428 static int
428 429 drv_ioc_hwgrpget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
429 430 {
430 431 dld_ioc_hwgrpget_t *hwgrpp = karg;
431 432 dld_hwgrpinfo_t hwgrp, *hip;
432 433 mac_handle_t mh = NULL;
433 434 int i, err, rgrpnum, tgrpnum;
434 435 uint_t bytes_left;
435 436 int totgrps = 0;
436 437 zoneid_t zoneid = crgetzoneid(cred);
437 438
438 439 if (zoneid != GLOBAL_ZONEID &&
439 440 zone_check_datalink(&zoneid, hwgrpp->dih_linkid) != 0)
440 441 return (ENOENT);
441 442
442 443 hwgrpp->dih_n_groups = 0;
443 444 err = mac_open_by_linkid(hwgrpp->dih_linkid, &mh);
444 445 if (err != 0)
445 446 goto done;
446 447
447 448 hip = (dld_hwgrpinfo_t *)
448 449 ((uchar_t *)arg + sizeof (dld_ioc_hwgrpget_t));
449 450 bytes_left = hwgrpp->dih_size;
450 451
451 452 rgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_RX);
452 453 /* display the default group information first */
453 454 if (rgrpnum > 0) {
454 455 if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
455 456 err = ENOSPC;
456 457 goto done;
457 458 }
458 459
459 460 bzero(&hwgrp, sizeof (hwgrp));
460 461 bcopy(mac_name(mh), hwgrp.dhi_link_name,
461 462 sizeof (hwgrp.dhi_link_name));
462 463 mac_get_hwrxgrp_info(mh, 0, &hwgrp.dhi_grp_num,
463 464 &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
464 465 &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
465 466 if (hwgrp.dhi_n_rings != 0) {
466 467 if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
467 468 err = EFAULT;
468 469 goto done;
469 470 }
470 471 }
471 472 hip++;
472 473 totgrps++;
473 474 bytes_left -= sizeof (dld_hwgrpinfo_t);
474 475 }
475 476
476 477 tgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_TX);
477 478 /* display the default group information first */
478 479 if (tgrpnum > 0) {
479 480 if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
480 481 err = ENOSPC;
481 482 goto done;
482 483 }
483 484
484 485 bzero(&hwgrp, sizeof (hwgrp));
485 486 bcopy(mac_name(mh), hwgrp.dhi_link_name,
486 487 sizeof (hwgrp.dhi_link_name));
487 488 mac_get_hwtxgrp_info(mh, tgrpnum - 1, &hwgrp.dhi_grp_num,
488 489 &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
489 490 &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
490 491 if (hwgrp.dhi_n_rings != 0) {
491 492 if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
492 493 err = EFAULT;
493 494 goto done;
494 495 }
495 496 }
496 497 hip++;
497 498 totgrps++;
498 499 bytes_left -= sizeof (dld_hwgrpinfo_t);
499 500 }
500 501
501 502 /* Rest of the rx groups */
502 503 for (i = 1; i < rgrpnum; i++) {
503 504 if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
504 505 err = ENOSPC;
505 506 goto done;
506 507 }
507 508
508 509 bzero(&hwgrp, sizeof (hwgrp));
509 510 bcopy(mac_name(mh), hwgrp.dhi_link_name,
510 511 sizeof (hwgrp.dhi_link_name));
511 512 mac_get_hwrxgrp_info(mh, i, &hwgrp.dhi_grp_num,
512 513 &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
513 514 &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
514 515 if (hwgrp.dhi_n_rings == 0)
515 516 continue;
516 517 if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
517 518 err = EFAULT;
518 519 goto done;
519 520 }
520 521
521 522 hip++;
522 523 totgrps++;
523 524 bytes_left -= sizeof (dld_hwgrpinfo_t);
524 525 }
525 526
526 527 /* Rest of the tx group */
527 528 tgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_TX);
528 529 for (i = 0; i < tgrpnum - 1; i++) {
529 530 if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
530 531 err = ENOSPC;
531 532 goto done;
532 533 }
533 534
534 535 bzero(&hwgrp, sizeof (hwgrp));
535 536 bcopy(mac_name(mh), hwgrp.dhi_link_name,
536 537 sizeof (hwgrp.dhi_link_name));
537 538 mac_get_hwtxgrp_info(mh, i, &hwgrp.dhi_grp_num,
538 539 &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
539 540 &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
540 541 if (hwgrp.dhi_n_rings == 0)
541 542 continue;
542 543 if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
543 544 err = EFAULT;
544 545 goto done;
545 546 }
546 547
547 548 hip++;
548 549 totgrps++;
549 550 bytes_left -= sizeof (dld_hwgrpinfo_t);
550 551 }
551 552
552 553 done:
553 554 if (mh != NULL)
554 555 dld_mac_close(mh);
555 556 if (err == 0)
556 557 hwgrpp->dih_n_groups = totgrps;
557 558 return (err);
558 559 }
559 560
560 561 /* ARGSUSED */
561 562 static int
562 563 drv_ioc_macaddrget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
563 564 {
564 565 dld_ioc_macaddrget_t *magp = karg;
565 566 dld_macaddrinfo_t mai, *maip;
566 567 mac_handle_t mh = NULL;
567 568 int i, err;
568 569 uint_t bytes_left;
569 570 boolean_t is_used;
570 571 zoneid_t zoneid = crgetzoneid(cred);
571 572
572 573 if (zoneid != GLOBAL_ZONEID &&
573 574 zone_check_datalink(&zoneid, magp->dig_linkid) != 0)
574 575 return (ENOENT);
575 576
576 577 magp->dig_count = 0;
577 578 err = mac_open_by_linkid(magp->dig_linkid, &mh);
578 579 if (err != 0)
579 580 goto done;
580 581
581 582 maip = (dld_macaddrinfo_t *)
582 583 ((uchar_t *)arg + sizeof (dld_ioc_macaddrget_t));
583 584 bytes_left = magp->dig_size;
584 585
585 586 for (i = 0; i < mac_addr_factory_num(mh) + 1; i++) {
586 587 if (sizeof (dld_macaddrinfo_t) > bytes_left) {
587 588 err = ENOSPC;
588 589 goto done;
589 590 }
590 591
591 592 bzero(&mai, sizeof (mai));
592 593
593 594 if (i == 0) {
594 595 /* primary MAC address */
595 596 mac_unicast_primary_get(mh, mai.dmi_addr);
596 597 mai.dmi_addrlen = mac_addr_len(mh);
597 598 mac_unicast_primary_info(mh, mai.dmi_client_name,
598 599 &is_used);
599 600 } else {
600 601 /* factory MAC address slot */
601 602 mac_addr_factory_value(mh, i, mai.dmi_addr,
602 603 &mai.dmi_addrlen, mai.dmi_client_name, &is_used);
603 604 }
604 605
605 606 mai.dmi_slot = i;
606 607 if (is_used)
607 608 mai.dmi_flags |= DLDIOCMACADDR_USED;
608 609
609 610 if (copyout(&mai, maip, sizeof (mai)) != 0) {
610 611 err = EFAULT;
611 612 goto done;
612 613 }
613 614
614 615 maip++;
615 616 bytes_left -= sizeof (dld_macaddrinfo_t);
616 617 }
|
↓ open down ↓ |
582 lines elided |
↑ open up ↑ |
617 618
618 619 done:
619 620 if (mh != NULL)
620 621 dld_mac_close(mh);
621 622 if (err == 0)
622 623 magp->dig_count = mac_addr_factory_num(mh) + 1;
623 624 return (err);
624 625 }
625 626
626 627 /*
628 + * Sometimes DLDIOC_GETMACPROP (and in the future, possibly DLDIOC_SETMACPROP)
629 + * may be allowed to be accessed by the zone that is assigned the datalink
630 + * device, as opposed to the zone that created the device.
631 + */
632 +static boolean_t
633 +dld_macprop_assigned_zone_exception(zoneid_t zoneid, dls_dl_handle_t dlh,
634 + dld_ioc_macprop_t *kprop, boolean_t set)
635 +{
636 + /*
637 + * No exceptions for setting! No exceptions unless the zoneid is
638 + * the assigned zone.
639 + */
640 + if (set || zoneid != dls_devnet_getzid(dlh))
641 + return (B_FALSE);
642 +
643 + /*
644 + * The current list of read-only exceptions are enumerated below.
645 + */
646 + switch (kprop->pr_num) {
647 + case MAC_PROP_MTU:
648 + case MAC_PROP_STATUS:
649 + return (B_TRUE);
650 + default:
651 + return (B_FALSE);
652 + }
653 +}
654 +
655 +/*
627 656 * DLDIOC_SET/GETMACPROP
628 657 */
629 658 static int
630 659 drv_ioc_prop_common(dld_ioc_macprop_t *prop, intptr_t arg, boolean_t set,
631 660 cred_t *cred, int mode)
632 661 {
633 662 int err = EINVAL;
634 663 dls_dl_handle_t dlh = NULL;
635 664 dls_link_t *dlp = NULL;
636 665 mac_perim_handle_t mph = NULL;
637 666 dld_ioc_macprop_t *kprop;
638 667 datalink_id_t linkid;
639 668 datalink_class_t class;
640 669 zoneid_t zoneid = crgetzoneid(cred);
641 670 uint_t dsize;
642 671
643 672 /*
644 673 * We only use pr_valsize from prop, as the caller only did a
645 674 * copyin() for sizeof (dld_ioc_prop_t), which doesn't cover
646 675 * the property data. We copyin the full dld_ioc_prop_t
647 676 * including the data into kprop down below.
648 677 */
649 678 dsize = sizeof (dld_ioc_macprop_t) + prop->pr_valsize - 1;
650 679 if (dsize < prop->pr_valsize)
651 680 return (EINVAL);
652 681
653 682 /*
654 683 * The property data is variable size, so we need to allocate
655 684 * a buffer for kernel use as this data was not part of the
656 685 * prop allocation and copyin() done by the framework.
657 686 */
658 687 if ((kprop = kmem_alloc(dsize, KM_NOSLEEP)) == NULL)
659 688 return (ENOMEM);
660 689
661 690 if (ddi_copyin((void *)arg, kprop, dsize, mode) != 0) {
662 691 err = EFAULT;
663 692 goto done;
664 693 }
665 694
666 695 linkid = kprop->pr_linkid;
667 696
668 697 if (set) {
669 698 if ((err = dls_mgmt_get_linkinfo(linkid, NULL, &class, NULL,
670 699 NULL)) != 0 || (err = drv_ioc_checkprivs(class, cred)) != 0)
671 700 goto done;
|
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
672 701 }
673 702
674 703 if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
675 704 goto done;
676 705 if ((err = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
677 706 goto done;
678 707 if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
679 708 goto done;
680 709
681 710 /*
682 - * Don't allow a process to get or set properties of a link if that
683 - * link doesn't belong to that zone.
711 + * In general, don't allow a process to get or set properties of a
712 + * link if that link doesn't belong to that zone.
713 + *
714 + * There are exceptions however, if the dlh's *assigned* zone (as
715 + * determined by dls_devnet_getzid()) is the one calling here. See
716 + * the local function dld_macprop_assigned_zone_exception() above.
684 717 */
685 - if (zoneid != dls_devnet_getownerzid(dlh)) {
718 + if (zoneid != dls_devnet_getownerzid(dlh) &&
719 + !dld_macprop_assigned_zone_exception(zoneid, dlh, kprop, set)) {
686 720 err = ENOENT;
687 721 goto done;
688 722 }
689 723
690 724 if (!mac_prop_check_size(kprop->pr_num, kprop->pr_valsize,
691 725 kprop->pr_flags & DLD_PROP_POSSIBLE)) {
692 726 err = ENOBUFS;
693 727 goto done;
694 728 }
695 729
696 730 switch (kprop->pr_num) {
697 731 case MAC_PROP_ZONE:
698 732 if (set) {
699 733 dld_ioc_zid_t *dzp = (dld_ioc_zid_t *)kprop->pr_val;
700 734
701 735 if (zoneid != GLOBAL_ZONEID) {
702 736 err = EACCES;
703 737 goto done;
704 738 }
705 739 err = dls_devnet_setzid(dlh, dzp->diz_zid);
706 740 } else {
707 741 kprop->pr_perm_flags = MAC_PROP_PERM_RW;
708 742 (*(zoneid_t *)kprop->pr_val) = dls_devnet_getzid(dlh);
709 743 }
710 744 break;
711 745 case MAC_PROP_AUTOPUSH: {
712 746 struct dlautopush *dlap = (struct dlautopush *)kprop->pr_val;
713 747
714 748 if (set) {
715 749 if (kprop->pr_valsize != 0)
716 750 err = drv_ioc_setap(linkid, dlap);
717 751 else
718 752 err = drv_ioc_clrap(linkid);
719 753 } else {
720 754 /*
721 755 * You might think that the earlier call to
722 756 * mac_prop_check_size() should catch this but
723 757 * it can't. The autopush prop uses 0 as a
724 758 * sentinel value to clear the prop. This
725 759 * check ensures we don't allow a get with a
726 760 * valsize of 0.
727 761 */
728 762 if (kprop->pr_valsize == 0) {
729 763 err = ENOBUFS;
730 764 goto done;
731 765 }
732 766
733 767 kprop->pr_perm_flags = MAC_PROP_PERM_RW;
734 768 err = drv_ioc_getap(linkid, dlap);
735 769 }
736 770 break;
737 771 }
738 772 case MAC_PROP_TAGMODE:
739 773 if (set) {
740 774 link_tagmode_t mode = *(link_tagmode_t *)kprop->pr_val;
741 775
742 776 if (mode != LINK_TAGMODE_VLANONLY &&
743 777 mode != LINK_TAGMODE_NORMAL) {
744 778 err = EINVAL;
745 779 } else {
746 780 dlp->dl_tagmode = mode;
747 781 err = 0;
748 782 }
749 783 } else {
750 784 *(link_tagmode_t *)kprop->pr_val = dlp->dl_tagmode;
751 785 kprop->pr_perm_flags = MAC_PROP_PERM_RW;
752 786 err = 0;
753 787 }
754 788 break;
755 789 default: {
756 790 mac_propval_range_t *rangep = NULL;
757 791 void *default_val = NULL;
758 792 uint_t default_size = 0;
759 793
760 794 /* set a property value */
761 795 if (set) {
762 796 err = mac_set_prop(dlp->dl_mh, kprop->pr_num,
763 797 kprop->pr_name, kprop->pr_val, kprop->pr_valsize);
764 798 break;
765 799 }
766 800
767 801 /*
768 802 * Get the property value, default, or possible value
769 803 * depending on flags passed from the user.
770 804 */
771 805
772 806 /* a property has RW permissions by default */
773 807 kprop->pr_perm_flags = MAC_PROP_PERM_RW;
774 808
775 809 if (kprop->pr_flags & DLD_PROP_POSSIBLE) {
776 810 rangep = (mac_propval_range_t *)kprop->pr_val;
777 811
778 812 /*
779 813 * fail if rangep is not aligned to first
780 814 * member of mac_propval_range_t.
781 815 */
782 816 ASSERT(IS_P2ALIGNED(rangep, sizeof (uint_t)));
783 817 } else if (kprop->pr_flags & DLD_PROP_DEFAULT) {
784 818 default_val = kprop->pr_val;
785 819 default_size = kprop->pr_valsize;
786 820 }
787 821
788 822 /*
789 823 * Always return the permissions, and optionally return
790 824 * the default value or possible values range.
791 825 */
792 826 err = mac_prop_info(dlp->dl_mh, kprop->pr_num, kprop->pr_name,
793 827 default_val, default_size, rangep, &kprop->pr_perm_flags);
794 828 if (err != 0)
795 829 goto done;
796 830
797 831 if (default_val == NULL && rangep == NULL) {
798 832 err = mac_get_prop(dlp->dl_mh, kprop->pr_num,
799 833 kprop->pr_name, kprop->pr_val, kprop->pr_valsize);
800 834 }
801 835 }
802 836 }
803 837
804 838 done:
805 839 if (!set && ddi_copyout(kprop, (void *)arg, dsize, mode) != 0)
806 840 err = EFAULT;
807 841
808 842 if (dlp != NULL)
809 843 dls_link_rele(dlp);
810 844
811 845 if (mph != NULL) {
812 846 int32_t cpuid;
813 847 void *mdip = NULL;
814 848
815 849 if (dlp != NULL && set && err == 0) {
816 850 cpuid = mac_client_intr_cpu(dlp->dl_mch);
817 851 mdip = mac_get_devinfo(dlp->dl_mh);
818 852 }
819 853
820 854 mac_perim_exit(mph);
821 855
822 856 if (mdip != NULL && cpuid != -1)
823 857 mac_client_set_intr_cpu(mdip, dlp->dl_mch, cpuid);
824 858 }
825 859
826 860 if (dlh != NULL)
827 861 dls_devnet_rele_tmp(dlh);
828 862
829 863 if (kprop != NULL)
830 864 kmem_free(kprop, dsize);
831 865 return (err);
832 866 }
833 867
834 868 /* ARGSUSED */
835 869 static int
836 870 drv_ioc_setprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
837 871 {
838 872 return (drv_ioc_prop_common(karg, arg, B_TRUE, cred, mode));
839 873 }
840 874
841 875 /* ARGSUSED */
842 876 static int
843 877 drv_ioc_getprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
844 878 {
845 879 return (drv_ioc_prop_common(karg, arg, B_FALSE, cred, mode));
846 880 }
847 881
848 882 /*
849 883 * DLDIOC_RENAME.
850 884 *
851 885 * This function handles two cases of link renaming. See more in comments above
852 886 * dls_datalink_rename().
853 887 */
854 888 /* ARGSUSED */
855 889 static int
856 890 drv_ioc_rename(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
857 891 {
858 892 dld_ioc_rename_t *dir = karg;
859 893 mod_hash_key_t key;
860 894 mod_hash_val_t val;
861 895 zoneid_t zoneid = crgetzoneid(cred);
862 896 datalink_class_t class;
863 897 int err;
864 898
865 899 if (zoneid != GLOBAL_ZONEID &&
866 900 (zone_check_datalink(&zoneid, dir->dir_linkid1) != 0 ||
867 901 dir->dir_linkid2 != DATALINK_INVALID_LINKID &&
868 902 zone_check_datalink(&zoneid, dir->dir_linkid2) != 0))
869 903 return (ENOENT);
870 904
871 905 if ((err = dls_mgmt_get_linkinfo(dir->dir_linkid1, NULL, &class, NULL,
872 906 NULL)) != 0)
873 907 return (err);
874 908
875 909 if ((err = drv_ioc_checkprivs(class, cred)) != 0)
876 910 return (err);
877 911
878 912 if ((err = dls_devnet_rename(dir->dir_linkid1, dir->dir_linkid2,
879 913 dir->dir_link)) != 0)
880 914 return (err);
881 915
882 916 if (dir->dir_linkid2 == DATALINK_INVALID_LINKID)
883 917 return (0);
884 918
885 919 /*
886 920 * if dir_linkid2 is not DATALINK_INVALID_LINKID, it means this
887 921 * renaming request is to rename a valid physical link (dir_linkid1)
888 922 * to a "removed" physical link (dir_linkid2, which is removed by DR
889 923 * or during system shutdown). In this case, the link (specified by
890 924 * dir_linkid1) would inherit all the configuration of dir_linkid2,
891 925 * and dir_linkid1 and its configuration would be lost.
892 926 *
893 927 * Remove per-link autopush configuration of dir_linkid1 in this case.
894 928 */
895 929 key = (mod_hash_key_t)(uintptr_t)dir->dir_linkid1;
896 930 rw_enter(&dld_ap_hash_lock, RW_WRITER);
897 931 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
898 932 rw_exit(&dld_ap_hash_lock);
899 933 return (0);
900 934 }
901 935
902 936 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
903 937 kmem_free(val, sizeof (dld_ap_t));
904 938 rw_exit(&dld_ap_hash_lock);
905 939 return (0);
906 940 }
907 941
908 942 static int
909 943 drv_ioc_setap(datalink_id_t linkid, struct dlautopush *dlap)
910 944 {
911 945 dld_ap_t *dap;
912 946 int i;
913 947 mod_hash_key_t key;
914 948
915 949 if (dlap->dap_npush == 0 || dlap->dap_npush > MAXAPUSH)
916 950 return (EINVAL);
917 951
918 952 /*
919 953 * Validate that the specified list of modules exist.
920 954 */
921 955 for (i = 0; i < dlap->dap_npush; i++) {
922 956 if (fmodsw_find(dlap->dap_aplist[i], FMODSW_LOAD) == NULL)
923 957 return (EINVAL);
924 958 }
925 959
926 960
927 961 key = (mod_hash_key_t)(uintptr_t)linkid;
928 962
929 963 rw_enter(&dld_ap_hash_lock, RW_WRITER);
930 964 if (mod_hash_find(dld_ap_hashp, key, (mod_hash_val_t *)&dap) != 0) {
931 965 dap = kmem_zalloc(sizeof (dld_ap_t), KM_NOSLEEP);
932 966 if (dap == NULL) {
933 967 rw_exit(&dld_ap_hash_lock);
934 968 return (ENOMEM);
935 969 }
936 970
937 971 dap->da_linkid = linkid;
938 972 VERIFY(mod_hash_insert(dld_ap_hashp, key,
939 973 (mod_hash_val_t)dap) == 0);
940 974 }
941 975
942 976 /*
943 977 * Update the configuration.
944 978 */
945 979 dap->da_anchor = dlap->dap_anchor;
946 980 dap->da_npush = dlap->dap_npush;
947 981 for (i = 0; i < dlap->dap_npush; i++) {
948 982 (void) strlcpy(dap->da_aplist[i], dlap->dap_aplist[i],
949 983 FMNAMESZ + 1);
950 984 }
951 985 rw_exit(&dld_ap_hash_lock);
952 986
953 987 return (0);
954 988 }
955 989
956 990 static int
957 991 drv_ioc_getap(datalink_id_t linkid, struct dlautopush *dlap)
958 992 {
959 993 dld_ap_t *dap;
960 994 int i;
961 995
962 996 rw_enter(&dld_ap_hash_lock, RW_READER);
963 997 if (mod_hash_find(dld_ap_hashp,
964 998 (mod_hash_key_t)(uintptr_t)linkid,
965 999 (mod_hash_val_t *)&dap) != 0) {
966 1000 rw_exit(&dld_ap_hash_lock);
967 1001 dlap->dap_npush = 0;
968 1002 return (0);
969 1003 }
970 1004
971 1005 /*
972 1006 * Retrieve the configuration.
973 1007 */
974 1008 dlap->dap_anchor = dap->da_anchor;
975 1009 dlap->dap_npush = dap->da_npush;
976 1010 for (i = 0; i < dap->da_npush; i++) {
977 1011 (void) strlcpy(dlap->dap_aplist[i], dap->da_aplist[i],
978 1012 FMNAMESZ + 1);
979 1013 }
980 1014 rw_exit(&dld_ap_hash_lock);
981 1015
982 1016 return (0);
983 1017 }
984 1018
985 1019 static int
986 1020 drv_ioc_clrap(datalink_id_t linkid)
987 1021 {
988 1022 mod_hash_val_t val;
989 1023 mod_hash_key_t key;
990 1024
991 1025 key = (mod_hash_key_t)(uintptr_t)linkid;
992 1026
993 1027 rw_enter(&dld_ap_hash_lock, RW_WRITER);
994 1028 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
995 1029 rw_exit(&dld_ap_hash_lock);
996 1030 return (0);
997 1031 }
998 1032
999 1033 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
1000 1034 kmem_free(val, sizeof (dld_ap_t));
1001 1035 rw_exit(&dld_ap_hash_lock);
1002 1036 return (0);
1003 1037 }
1004 1038
1005 1039 /*
1006 1040 * DLDIOC_DOORSERVER
1007 1041 */
1008 1042 /* ARGSUSED */
1009 1043 static int
1010 1044 drv_ioc_doorserver(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1011 1045 {
1012 1046 dld_ioc_door_t *did = karg;
1013 1047
1014 1048 return (dls_mgmt_door_set(did->did_start_door));
1015 1049 }
1016 1050
1017 1051 /*
1018 1052 * DLDIOC_USAGELOG
1019 1053 */
1020 1054 /* ARGSUSED */
1021 1055 static int
1022 1056 drv_ioc_usagelog(void *karg, intptr_t arg, int mode, cred_t *cred,
1023 1057 int *rvalp)
1024 1058 {
1025 1059 dld_ioc_usagelog_t *log_info = (dld_ioc_usagelog_t *)karg;
1026 1060 int err = 0;
1027 1061
1028 1062 if (log_info->ul_type < MAC_LOGTYPE_LINK ||
1029 1063 log_info->ul_type > MAC_LOGTYPE_FLOW)
1030 1064 return (EINVAL);
1031 1065
1032 1066 if (log_info->ul_onoff) {
1033 1067 err = mac_start_logusage(log_info->ul_type,
1034 1068 log_info->ul_interval);
1035 1069 } else {
1036 1070 mac_stop_logusage(log_info->ul_type);
1037 1071 }
1038 1072 return (err);
1039 1073 }
1040 1074
1041 1075 /*
1042 1076 * Process a DLDIOC_ADDFLOW request.
1043 1077 */
1044 1078 /* ARGSUSED */
1045 1079 static int
1046 1080 drv_ioc_addflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1047 1081 {
1048 1082 dld_ioc_addflow_t *afp = karg;
1049 1083
1050 1084 return (dld_add_flow(afp->af_linkid, afp->af_name,
1051 1085 &afp->af_flow_desc, &afp->af_resource_props));
1052 1086 }
1053 1087
1054 1088 /*
1055 1089 * Process a DLDIOC_REMOVEFLOW request.
1056 1090 */
1057 1091 /* ARGSUSED */
1058 1092 static int
1059 1093 drv_ioc_removeflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1060 1094 {
1061 1095 dld_ioc_removeflow_t *rfp = karg;
1062 1096
1063 1097 return (dld_remove_flow(rfp->rf_name));
1064 1098 }
1065 1099
1066 1100 /*
1067 1101 * Process a DLDIOC_MODIFYFLOW request.
1068 1102 */
1069 1103 /* ARGSUSED */
1070 1104 static int
1071 1105 drv_ioc_modifyflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1072 1106 {
1073 1107 dld_ioc_modifyflow_t *mfp = karg;
1074 1108
1075 1109 return (dld_modify_flow(mfp->mf_name, &mfp->mf_resource_props));
1076 1110 }
1077 1111
1078 1112 /*
1079 1113 * Process a DLDIOC_WALKFLOW request.
1080 1114 */
1081 1115 /* ARGSUSED */
1082 1116 static int
1083 1117 drv_ioc_walkflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1084 1118 {
1085 1119 dld_ioc_walkflow_t *wfp = karg;
1086 1120
1087 1121 return (dld_walk_flow(wfp, arg, cred));
1088 1122 }
1089 1123
1090 1124 /*
1091 1125 * Check for GLDv3 autopush information. There are three cases:
1092 1126 *
1093 1127 * 1. If devp points to a GLDv3 datalink and it has autopush configuration,
1094 1128 * fill dlap in with that information and return 0.
1095 1129 *
1096 1130 * 2. If devp points to a GLDv3 datalink but it doesn't have autopush
1097 1131 * configuration, then replace devp with the physical device (if one
1098 1132 * exists) and return 1. This allows stropen() to find the old-school
1099 1133 * per-driver autopush configuration. (For softmac, the result is that
1100 1134 * the softmac dev_t is replaced with the legacy device's dev_t).
1101 1135 *
1102 1136 * 3. If neither of the above apply, don't touch the args and return -1.
1103 1137 */
1104 1138 int
1105 1139 dld_autopush(dev_t *devp, struct dlautopush *dlap)
1106 1140 {
1107 1141 dld_ap_t *dap;
1108 1142 datalink_id_t linkid;
1109 1143 dev_t phydev;
1110 1144
1111 1145 if (!GLDV3_DRV(getmajor(*devp)))
1112 1146 return (-1);
1113 1147
1114 1148 /*
1115 1149 * Find the linkid by the link's dev_t.
1116 1150 */
1117 1151 if (dls_devnet_dev2linkid(*devp, &linkid) != 0)
1118 1152 return (-1);
1119 1153
1120 1154 /*
1121 1155 * Find the autopush configuration associated with the linkid.
1122 1156 */
1123 1157 rw_enter(&dld_ap_hash_lock, RW_READER);
1124 1158 if (mod_hash_find(dld_ap_hashp, (mod_hash_key_t)(uintptr_t)linkid,
1125 1159 (mod_hash_val_t *)&dap) == 0) {
1126 1160 *dlap = dap->da_ap;
1127 1161 rw_exit(&dld_ap_hash_lock);
1128 1162 return (0);
1129 1163 }
1130 1164 rw_exit(&dld_ap_hash_lock);
1131 1165
1132 1166 if (dls_devnet_phydev(linkid, &phydev) != 0)
1133 1167 return (-1);
1134 1168
1135 1169 *devp = phydev;
1136 1170 return (1);
1137 1171 }
1138 1172
1139 1173 /*
1140 1174 * Secure objects implementation
1141 1175 */
1142 1176
1143 1177 /* ARGSUSED */
1144 1178 static int
1145 1179 drv_secobj_ctor(void *buf, void *arg, int kmflag)
1146 1180 {
1147 1181 bzero(buf, sizeof (dld_secobj_t));
1148 1182 return (0);
1149 1183 }
1150 1184
1151 1185 static void
1152 1186 drv_secobj_init(void)
1153 1187 {
1154 1188 rw_init(&drv_secobj_lock, NULL, RW_DEFAULT, NULL);
1155 1189 drv_secobj_cachep = kmem_cache_create("drv_secobj_cache",
1156 1190 sizeof (dld_secobj_t), 0, drv_secobj_ctor, NULL,
1157 1191 NULL, NULL, NULL, 0);
1158 1192 drv_secobj_hash = mod_hash_create_extended("drv_secobj_hash",
1159 1193 SECOBJ_WEP_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
1160 1194 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
1161 1195 }
1162 1196
1163 1197 static void
1164 1198 drv_secobj_fini(void)
1165 1199 {
1166 1200 mod_hash_destroy_hash(drv_secobj_hash);
1167 1201 kmem_cache_destroy(drv_secobj_cachep);
1168 1202 rw_destroy(&drv_secobj_lock);
1169 1203 }
1170 1204
1171 1205 /* ARGSUSED */
1172 1206 static int
1173 1207 drv_ioc_secobj_set(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1174 1208 {
1175 1209 dld_ioc_secobj_set_t *ssp = karg;
1176 1210 dld_secobj_t *sobjp, *objp;
1177 1211 int err;
1178 1212
1179 1213 sobjp = &ssp->ss_obj;
1180 1214
1181 1215 if (sobjp->so_class != DLD_SECOBJ_CLASS_WEP &&
1182 1216 sobjp->so_class != DLD_SECOBJ_CLASS_WPA)
1183 1217 return (EINVAL);
1184 1218
1185 1219 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0' ||
1186 1220 sobjp->so_len > DLD_SECOBJ_VAL_MAX)
1187 1221 return (EINVAL);
1188 1222
1189 1223 rw_enter(&drv_secobj_lock, RW_WRITER);
1190 1224 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sobjp->so_name,
1191 1225 (mod_hash_val_t *)&objp);
1192 1226 if (err == 0) {
1193 1227 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) != 0) {
1194 1228 rw_exit(&drv_secobj_lock);
1195 1229 return (EEXIST);
1196 1230 }
1197 1231 } else {
1198 1232 ASSERT(err == MH_ERR_NOTFOUND);
1199 1233 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) == 0) {
1200 1234 rw_exit(&drv_secobj_lock);
1201 1235 return (ENOENT);
1202 1236 }
1203 1237 objp = kmem_cache_alloc(drv_secobj_cachep, KM_SLEEP);
1204 1238 (void) strlcpy(objp->so_name, sobjp->so_name,
1205 1239 DLD_SECOBJ_NAME_MAX);
1206 1240
1207 1241 VERIFY(mod_hash_insert(drv_secobj_hash,
1208 1242 (mod_hash_key_t)objp->so_name, (mod_hash_val_t)objp) == 0);
1209 1243 }
1210 1244 bcopy(sobjp->so_val, objp->so_val, sobjp->so_len);
1211 1245 objp->so_len = sobjp->so_len;
1212 1246 objp->so_class = sobjp->so_class;
1213 1247 rw_exit(&drv_secobj_lock);
1214 1248 return (0);
1215 1249 }
1216 1250
1217 1251 typedef struct dld_secobj_state {
1218 1252 uint_t ss_free;
1219 1253 uint_t ss_count;
1220 1254 int ss_rc;
1221 1255 int ss_mode;
1222 1256 dld_secobj_t *ss_objp;
1223 1257 } dld_secobj_state_t;
1224 1258
1225 1259 /* ARGSUSED */
1226 1260 static uint_t
1227 1261 drv_secobj_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
1228 1262 {
1229 1263 dld_secobj_state_t *statep = arg;
1230 1264 dld_secobj_t *sobjp = (dld_secobj_t *)val;
1231 1265
1232 1266 if (statep->ss_free < sizeof (dld_secobj_t)) {
1233 1267 statep->ss_rc = ENOSPC;
1234 1268 return (MH_WALK_TERMINATE);
1235 1269 }
1236 1270 if (ddi_copyout(sobjp, statep->ss_objp, sizeof (*sobjp),
1237 1271 statep->ss_mode) != 0) {
1238 1272 statep->ss_rc = EFAULT;
1239 1273 return (MH_WALK_TERMINATE);
1240 1274 }
1241 1275 statep->ss_objp++;
1242 1276 statep->ss_free -= sizeof (dld_secobj_t);
1243 1277 statep->ss_count++;
1244 1278 return (MH_WALK_CONTINUE);
1245 1279 }
1246 1280
1247 1281 /* ARGSUSED */
1248 1282 static int
1249 1283 drv_ioc_secobj_get(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1250 1284 {
1251 1285 dld_ioc_secobj_get_t *sgp = karg;
1252 1286 dld_secobj_t *sobjp, *objp;
1253 1287 int err;
1254 1288
1255 1289 sobjp = &sgp->sg_obj;
1256 1290 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1257 1291 return (EINVAL);
1258 1292
1259 1293 rw_enter(&drv_secobj_lock, RW_READER);
1260 1294 if (sobjp->so_name[0] != '\0') {
1261 1295 err = mod_hash_find(drv_secobj_hash,
1262 1296 (mod_hash_key_t)sobjp->so_name, (mod_hash_val_t *)&objp);
1263 1297 if (err != 0) {
1264 1298 ASSERT(err == MH_ERR_NOTFOUND);
1265 1299 rw_exit(&drv_secobj_lock);
1266 1300 return (ENOENT);
1267 1301 }
1268 1302 bcopy(objp->so_val, sobjp->so_val, objp->so_len);
1269 1303 sobjp->so_len = objp->so_len;
1270 1304 sobjp->so_class = objp->so_class;
1271 1305 sgp->sg_count = 1;
1272 1306 } else {
1273 1307 dld_secobj_state_t state;
1274 1308
1275 1309 state.ss_free = sgp->sg_size - sizeof (dld_ioc_secobj_get_t);
1276 1310 state.ss_count = 0;
1277 1311 state.ss_rc = 0;
1278 1312 state.ss_mode = mode;
1279 1313 state.ss_objp = (dld_secobj_t *)((uchar_t *)arg +
1280 1314 sizeof (dld_ioc_secobj_get_t));
1281 1315
1282 1316 mod_hash_walk(drv_secobj_hash, drv_secobj_walker, &state);
1283 1317 if (state.ss_rc != 0) {
1284 1318 rw_exit(&drv_secobj_lock);
1285 1319 return (state.ss_rc);
1286 1320 }
1287 1321 sgp->sg_count = state.ss_count;
1288 1322 }
1289 1323 rw_exit(&drv_secobj_lock);
1290 1324 return (0);
1291 1325 }
1292 1326
1293 1327 /* ARGSUSED */
1294 1328 static int
1295 1329 drv_ioc_secobj_unset(void *karg, intptr_t arg, int mode, cred_t *cred,
1296 1330 int *rvalp)
1297 1331 {
1298 1332 dld_ioc_secobj_unset_t *sup = karg;
1299 1333 dld_secobj_t *objp;
1300 1334 mod_hash_val_t val;
1301 1335 int err;
1302 1336
1303 1337 if (sup->su_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1304 1338 return (EINVAL);
1305 1339
1306 1340 rw_enter(&drv_secobj_lock, RW_WRITER);
1307 1341 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
1308 1342 (mod_hash_val_t *)&objp);
1309 1343 if (err != 0) {
1310 1344 ASSERT(err == MH_ERR_NOTFOUND);
1311 1345 rw_exit(&drv_secobj_lock);
1312 1346 return (ENOENT);
1313 1347 }
1314 1348 VERIFY(mod_hash_remove(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
1315 1349 (mod_hash_val_t *)&val) == 0);
1316 1350 ASSERT(objp == (dld_secobj_t *)val);
1317 1351
1318 1352 kmem_cache_free(drv_secobj_cachep, objp);
1319 1353 rw_exit(&drv_secobj_lock);
1320 1354 return (0);
1321 1355 }
1322 1356
1323 1357 /* ARGSUSED */
1324 1358 static int
1325 1359 drv_ioc_gettran(void *karg, intptr_t arg, int mode, cred_t *cred,
1326 1360 int *rvalp)
1327 1361 {
1328 1362 int ret = 0;
1329 1363 mac_perim_handle_t mph = NULL;
1330 1364 dls_dl_handle_t dlh = NULL;
1331 1365 dls_link_t *dlp = NULL;
1332 1366 dld_ioc_gettran_t *dgt = karg;
1333 1367
1334 1368 if ((ret = dls_devnet_hold_tmp(dgt->dgt_linkid, &dlh)) != 0)
1335 1369 goto done;
1336 1370
1337 1371 if ((ret = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
1338 1372 goto done;
1339 1373
1340 1374 if ((ret = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
1341 1375 goto done;
1342 1376
1343 1377 /*
1344 1378 * Make sure that this link belongs to the zone.
1345 1379 */
1346 1380 if (crgetzoneid(cred) != dls_devnet_getownerzid(dlh)) {
1347 1381 ret = ENOENT;
1348 1382 goto done;
1349 1383 }
1350 1384
1351 1385 if (dgt->dgt_tran_id == DLDIOC_GETTRAN_GETNTRAN) {
1352 1386 ret = mac_transceiver_count(dlp->dl_mh, &dgt->dgt_tran_id);
1353 1387 } else {
1354 1388 ret = mac_transceiver_info(dlp->dl_mh, dgt->dgt_tran_id,
1355 1389 &dgt->dgt_present, &dgt->dgt_usable);
1356 1390 }
1357 1391
1358 1392 done:
1359 1393 if (dlp != NULL)
1360 1394 dls_link_rele(dlp);
1361 1395
1362 1396 if (mph != NULL)
1363 1397 mac_perim_exit(mph);
1364 1398
1365 1399 if (dlh != NULL)
1366 1400 dls_devnet_rele_tmp(dlh);
1367 1401
1368 1402 return (ret);
1369 1403 }
1370 1404
1371 1405 /* ARGSUSED */
1372 1406 static int
1373 1407 drv_ioc_readtran(void *karg, intptr_t arg, int mode, cred_t *cred,
1374 1408 int *rvalp)
1375 1409 {
1376 1410 int ret = 0;
1377 1411 mac_perim_handle_t mph = NULL;
1378 1412 dls_dl_handle_t dlh = NULL;
1379 1413 dls_link_t *dlp = NULL;
1380 1414 dld_ioc_tranio_t *dti = karg;
1381 1415 uint8_t buf[256];
1382 1416 size_t nr;
1383 1417
1384 1418 /*
1385 1419 * Be strict for the moment
1386 1420 */
1387 1421 if (dti->dti_nbytes != 256 || dti->dti_off != 0)
1388 1422 return (EINVAL);
1389 1423
1390 1424 if ((ret = dls_devnet_hold_tmp(dti->dti_linkid, &dlh)) != 0)
1391 1425 goto done;
1392 1426
1393 1427 if ((ret = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
1394 1428 goto done;
1395 1429
1396 1430 if ((ret = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
1397 1431 goto done;
1398 1432
1399 1433 /*
1400 1434 * Make sure that this link belongs to the zone.
1401 1435 */
1402 1436 if (crgetzoneid(cred) != dls_devnet_getownerzid(dlh)) {
1403 1437 ret = ENOENT;
1404 1438 goto done;
1405 1439 }
1406 1440
1407 1441 bzero(buf, sizeof (buf));
1408 1442 if ((ret = mac_transceiver_read(dlp->dl_mh, dti->dti_tran_id,
1409 1443 dti->dti_page, buf, dti->dti_nbytes, dti->dti_off, &nr)) == 0) {
1410 1444 dti->dti_nbytes = nr;
1411 1445 ret = ddi_copyout(buf, (void *)(uintptr_t)dti->dti_buf,
1412 1446 sizeof (buf), mode);
1413 1447 }
1414 1448
1415 1449 done:
1416 1450 if (dlp != NULL)
1417 1451 dls_link_rele(dlp);
1418 1452
1419 1453 if (mph != NULL)
1420 1454 mac_perim_exit(mph);
1421 1455
1422 1456 if (dlh != NULL)
1423 1457 dls_devnet_rele_tmp(dlh);
1424 1458
1425 1459 return (ret);
1426 1460 }
1427 1461
1428 1462 /* ARGSUSED */
1429 1463 static int
1430 1464 drv_ioc_getled(void *karg, intptr_t arg, int mode, cred_t *cred,
1431 1465 int *rvalp)
1432 1466 {
1433 1467 int ret = 0;
1434 1468 mac_perim_handle_t mph = NULL;
1435 1469 dls_dl_handle_t dlh = NULL;
1436 1470 dls_link_t *dlp = NULL;
1437 1471 dld_ioc_led_t *dil = karg;
1438 1472
1439 1473 if ((mode & FREAD) == 0)
1440 1474 return (EBADF);
1441 1475
1442 1476 if ((ret = dls_devnet_hold_tmp(dil->dil_linkid, &dlh)) != 0)
1443 1477 goto done;
1444 1478
1445 1479 if ((ret = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
1446 1480 goto done;
1447 1481
1448 1482 if ((ret = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
1449 1483 goto done;
1450 1484
1451 1485 /*
1452 1486 * Make sure that this link belongs to the zone.
1453 1487 */
1454 1488 if (crgetzoneid(cred) != dls_devnet_getownerzid(dlh)) {
1455 1489 ret = ENOENT;
1456 1490 goto done;
1457 1491 }
1458 1492
1459 1493 ret = mac_led_get(dlp->dl_mh, &dil->dil_supported, &dil->dil_active);
1460 1494
1461 1495 done:
1462 1496 if (dlp != NULL)
1463 1497 dls_link_rele(dlp);
1464 1498
1465 1499 if (mph != NULL)
1466 1500 mac_perim_exit(mph);
1467 1501
1468 1502 if (dlh != NULL)
1469 1503 dls_devnet_rele_tmp(dlh);
1470 1504
1471 1505 return (ret);
1472 1506 }
1473 1507
1474 1508 /* ARGSUSED */
1475 1509 static int
1476 1510 drv_ioc_setled(void *karg, intptr_t arg, int mode, cred_t *cred,
1477 1511 int *rvalp)
1478 1512 {
1479 1513 int ret = 0;
1480 1514 mac_perim_handle_t mph = NULL;
1481 1515 dls_dl_handle_t dlh = NULL;
1482 1516 dls_link_t *dlp = NULL;
1483 1517 dld_ioc_led_t *dil = karg;
1484 1518
1485 1519 if ((mode & FWRITE) == 0)
1486 1520 return (EBADF);
1487 1521
1488 1522 if ((ret = dls_devnet_hold_tmp(dil->dil_linkid, &dlh)) != 0)
1489 1523 goto done;
1490 1524
1491 1525 if ((ret = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
1492 1526 goto done;
1493 1527
1494 1528 if ((ret = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
1495 1529 goto done;
1496 1530
1497 1531 /*
1498 1532 * Make sure that this link belongs to the zone.
1499 1533 */
1500 1534 if (crgetzoneid(cred) != dls_devnet_getownerzid(dlh)) {
1501 1535 ret = ENOENT;
1502 1536 goto done;
1503 1537 }
1504 1538
1505 1539 ret = mac_led_set(dlp->dl_mh, dil->dil_active);
1506 1540
1507 1541 done:
1508 1542 if (dlp != NULL)
1509 1543 dls_link_rele(dlp);
1510 1544
1511 1545 if (mph != NULL)
1512 1546 mac_perim_exit(mph);
1513 1547
1514 1548 if (dlh != NULL)
1515 1549 dls_devnet_rele_tmp(dlh);
1516 1550
1517 1551 return (ret);
1518 1552 }
1519 1553
1520 1554
1521 1555 /*
1522 1556 * Note that ioctls that modify links have a NULL di_priv_func(), as
1523 1557 * privileges can only be checked after we know the class of the link being
1524 1558 * modified (due to class-specific fine-grained privileges such as
1525 1559 * sys_iptun_config).
1526 1560 */
1527 1561 static dld_ioc_info_t drv_ioc_list[] = {
1528 1562 {DLDIOC_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_attr_t),
1529 1563 drv_ioc_attr, NULL},
1530 1564 {DLDIOC_PHYS_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_phys_attr_t),
1531 1565 drv_ioc_phys_attr, NULL},
1532 1566 {DLDIOC_SECOBJ_SET, DLDCOPYIN, sizeof (dld_ioc_secobj_set_t),
1533 1567 drv_ioc_secobj_set, secpolicy_dl_config},
1534 1568 {DLDIOC_SECOBJ_GET, DLDCOPYINOUT, sizeof (dld_ioc_secobj_get_t),
1535 1569 drv_ioc_secobj_get, secpolicy_dl_config},
1536 1570 {DLDIOC_SECOBJ_UNSET, DLDCOPYIN, sizeof (dld_ioc_secobj_unset_t),
1537 1571 drv_ioc_secobj_unset, secpolicy_dl_config},
1538 1572 {DLDIOC_DOORSERVER, DLDCOPYIN, sizeof (dld_ioc_door_t),
1539 1573 drv_ioc_doorserver, secpolicy_dl_config},
1540 1574 {DLDIOC_RENAME, DLDCOPYIN, sizeof (dld_ioc_rename_t),
1541 1575 drv_ioc_rename, NULL},
1542 1576 {DLDIOC_MACADDRGET, DLDCOPYINOUT, sizeof (dld_ioc_macaddrget_t),
1543 1577 drv_ioc_macaddrget, NULL},
1544 1578 {DLDIOC_ADDFLOW, DLDCOPYIN, sizeof (dld_ioc_addflow_t),
1545 1579 drv_ioc_addflow, secpolicy_dl_config},
1546 1580 {DLDIOC_REMOVEFLOW, DLDCOPYIN, sizeof (dld_ioc_removeflow_t),
1547 1581 drv_ioc_removeflow, secpolicy_dl_config},
1548 1582 {DLDIOC_MODIFYFLOW, DLDCOPYIN, sizeof (dld_ioc_modifyflow_t),
1549 1583 drv_ioc_modifyflow, secpolicy_dl_config},
1550 1584 {DLDIOC_WALKFLOW, DLDCOPYINOUT, sizeof (dld_ioc_walkflow_t),
1551 1585 drv_ioc_walkflow, NULL},
1552 1586 {DLDIOC_USAGELOG, DLDCOPYIN, sizeof (dld_ioc_usagelog_t),
1553 1587 drv_ioc_usagelog, secpolicy_dl_config},
1554 1588 {DLDIOC_SETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
1555 1589 drv_ioc_setprop, NULL},
1556 1590 {DLDIOC_GETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
1557 1591 drv_ioc_getprop, NULL},
1558 1592 {DLDIOC_GETHWGRP, DLDCOPYINOUT, sizeof (dld_ioc_hwgrpget_t),
1559 1593 drv_ioc_hwgrpget, NULL},
1560 1594 {DLDIOC_GETTRAN, DLDCOPYINOUT, sizeof (dld_ioc_gettran_t),
1561 1595 drv_ioc_gettran, NULL },
1562 1596 {DLDIOC_READTRAN, DLDCOPYINOUT, sizeof (dld_ioc_tranio_t),
1563 1597 drv_ioc_readtran, NULL },
1564 1598 {DLDIOC_GETLED, DLDCOPYINOUT, sizeof (dld_ioc_led_t),
1565 1599 drv_ioc_getled, NULL },
1566 1600 {DLDIOC_SETLED, DLDCOPYIN, sizeof (dld_ioc_led_t),
1567 1601 drv_ioc_setled, secpolicy_dl_config}
1568 1602 };
1569 1603
1570 1604 typedef struct dld_ioc_modentry {
1571 1605 uint16_t dim_modid; /* Top 16 bits of ioctl command */
1572 1606 char *dim_modname; /* Module to be loaded */
1573 1607 int ctrl_node_inst; /* Ctrl node instance */
1574 1608 dld_ioc_info_t *dim_list; /* array of ioctl structures */
1575 1609 uint_t dim_count; /* number of elements in dim_list */
1576 1610 } dld_ioc_modentry_t;
1577 1611
1578 1612 /*
1579 1613 * For all modules except for dld, dim_list and dim_count are assigned
1580 1614 * when the modules register their ioctls in dld_ioc_register(). We
1581 1615 * can statically initialize dld's ioctls in-line here; there's no
1582 1616 * need for it to call dld_ioc_register() itself. ctrl_node_inst controls
1583 1617 * whether an instance of the device will be held or the driver. If set to
1584 1618 * a non-negative integer, device instance specified in ctrl_node_inst will
1585 1619 * be held; so dld_ioc_register() _must_ be called in xxx_attach() routine of
1586 1620 * the driver. If set to -1, driver will be held; so dld_ioc_register() _must_
1587 1621 * be called in xxx_init() routine of the driver.
1588 1622 */
1589 1623 static dld_ioc_modentry_t dld_ioc_modtable[] = {
1590 1624 {DLD_IOC, "dld", 0, drv_ioc_list, DLDIOCCNT(drv_ioc_list)},
1591 1625 {AGGR_IOC, "aggr", 0, NULL, 0},
1592 1626 {VNIC_IOC, "vnic", 0, NULL, 0},
1593 1627 {SIMNET_IOC, "simnet", 0, NULL, 0},
1594 1628 {BRIDGE_IOC, "bridge", 0, NULL, 0},
1595 1629 {IPTUN_IOC, "iptun", 0, NULL, 0},
1596 1630 {IBPART_IOC, "ibp", -1, NULL, 0},
1597 1631 {OVERLAY_IOC, "overlay", 0, NULL, 0}
1598 1632 };
1599 1633 #define DLDIOC_CNT \
1600 1634 (sizeof (dld_ioc_modtable) / sizeof (dld_ioc_modentry_t))
1601 1635
1602 1636 static dld_ioc_modentry_t *
1603 1637 dld_ioc_findmod(uint16_t modid)
1604 1638 {
1605 1639 int i;
1606 1640
1607 1641 for (i = 0; i < DLDIOC_CNT; i++) {
1608 1642 if (modid == dld_ioc_modtable[i].dim_modid)
1609 1643 return (&dld_ioc_modtable[i]);
1610 1644 }
1611 1645 return (NULL);
1612 1646 }
1613 1647
1614 1648 int
1615 1649 dld_ioc_register(uint16_t modid, dld_ioc_info_t *list, uint_t count)
1616 1650 {
1617 1651 dld_ioc_modentry_t *dim = dld_ioc_findmod(modid);
1618 1652
1619 1653 if (dim == NULL)
1620 1654 return (ENOENT);
1621 1655
1622 1656 dim->dim_list = list;
1623 1657 dim->dim_count = count;
1624 1658 return (0);
1625 1659 }
1626 1660
1627 1661 void
1628 1662 dld_ioc_unregister(uint16_t modid)
1629 1663 {
1630 1664 VERIFY(dld_ioc_register(modid, NULL, 0) == 0);
1631 1665 }
1632 1666
1633 1667 /*
1634 1668 * The general design with GLDv3 ioctls is that all ioctls issued
1635 1669 * through /dev/dld go through this drv_ioctl() function. This
1636 1670 * function handles all ioctls on behalf of modules listed in
1637 1671 * dld_ioc_modtable.
1638 1672 *
1639 1673 * When an ioctl is received, this function looks for the associated
1640 1674 * module-id-specific ioctl information using dld_ioc_findmod(). The
1641 1675 * call to ddi_hold_driver() or ddi_hold_devi_by_instance() on the
1642 1676 * associated device will cause the kernel module responsible for the
1643 1677 * ioctl to be loaded if it's not already loaded, which should result
1644 1678 * in that module calling dld_ioc_register(), thereby filling in the
1645 1679 * dim_list containing the details for the ioctl being processed.
1646 1680 *
1647 1681 * This function can then perform operations such as copyin() data and
1648 1682 * do credential checks based on the registered ioctl information,
1649 1683 * then issue the callback function di_func() registered by the
1650 1684 * responsible module. Upon return, the appropriate copyout()
1651 1685 * operation can be performed and the operation completes.
1652 1686 */
1653 1687 /* ARGSUSED */
1654 1688 static int
1655 1689 drv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1656 1690 {
1657 1691 dld_ioc_modentry_t *dim;
1658 1692 dld_ioc_info_t *info;
1659 1693 dev_info_t *dip = NULL;
1660 1694 struct dev_ops *dops = NULL;
1661 1695 major_t major;
1662 1696 void *buf = NULL;
1663 1697 size_t sz;
1664 1698 int i, err;
1665 1699
1666 1700 if ((dim = dld_ioc_findmod(DLD_IOC_MODID(cmd))) == NULL)
1667 1701 return (ENOTSUP);
1668 1702
1669 1703 major = ddi_name_to_major(dim->dim_modname);
1670 1704
1671 1705 if (dim->ctrl_node_inst == -1) {
1672 1706 /*
1673 1707 * No dedicated instance to process ioctls.
1674 1708 * dld_ioc_register() is called in xxx_init().
1675 1709 */
1676 1710 dops = ddi_hold_driver(major);
1677 1711 } else {
1678 1712 /*
1679 1713 * Dedicated instance to handle ioctl.
1680 1714 * dld_ioc_register() is called in xxx_attach().
1681 1715 */
1682 1716 dip = ddi_hold_devi_by_instance(major, dim->ctrl_node_inst, 0);
1683 1717 }
1684 1718
1685 1719 if ((dip == NULL && dops == NULL) || dim->dim_list == NULL) {
1686 1720 err = ENODEV;
1687 1721 goto done;
1688 1722 }
1689 1723
1690 1724 for (i = 0; i < dim->dim_count; i++) {
1691 1725 if (cmd == dim->dim_list[i].di_cmd)
1692 1726 break;
1693 1727 }
1694 1728 if (i == dim->dim_count) {
1695 1729 err = ENOTSUP;
1696 1730 goto done;
1697 1731 }
1698 1732
1699 1733 info = &dim->dim_list[i];
1700 1734
1701 1735 if (info->di_priv_func != NULL &&
1702 1736 (err = info->di_priv_func(cred)) != 0)
1703 1737 goto done;
1704 1738
1705 1739 sz = info->di_argsize;
1706 1740 if ((buf = kmem_zalloc(sz, KM_NOSLEEP)) == NULL) {
1707 1741 err = ENOMEM;
1708 1742 goto done;
1709 1743 }
1710 1744
1711 1745 if ((info->di_flags & DLDCOPYIN) &&
1712 1746 ddi_copyin((void *)arg, buf, sz, mode) != 0) {
1713 1747 err = EFAULT;
1714 1748 goto done;
1715 1749 }
1716 1750
1717 1751 err = info->di_func(buf, arg, mode, cred, rvalp);
1718 1752
1719 1753 if ((info->di_flags & DLDCOPYOUT) &&
1720 1754 ddi_copyout(buf, (void *)arg, sz, mode) != 0 && err == 0)
1721 1755 err = EFAULT;
1722 1756
1723 1757 done:
1724 1758 if (buf != NULL)
1725 1759 kmem_free(buf, sz);
1726 1760 if (dip != NULL)
1727 1761 ddi_release_devi(dip);
1728 1762 if (dops != NULL)
1729 1763 ddi_rele_driver(major);
1730 1764 return (err);
1731 1765 }
|
↓ open down ↓ |
1036 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX