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