xref: /titanic_41/usr/src/uts/common/io/dls/dls_mgmt.c (revision 9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Datalink management routines.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/door.h>
32 #include <sys/zone.h>
33 #include <sys/modctl.h>
34 #include <sys/file.h>
35 #include <sys/modhash.h>
36 #include <sys/kstat.h>
37 #include <sys/vnode.h>
38 #include <sys/cmn_err.h>
39 #include <sys/softmac.h>
40 #include <sys/dls.h>
41 #include <sys/dls_impl.h>
42 #include <sys/stropts.h>
43 #include <sys/netstack.h>
44 #include <inet/iptun/iptun_impl.h>
45 
46 /*
47  * This vanity name management module is treated as part of the GLD framework
48  * and we don't hold any GLD framework lock across a call to any mac
49  * function that needs to acquire the mac perimeter. The hierarchy is
50  * mac perimeter -> framework locks
51  */
52 
53 typedef struct dls_stack {
54 	zoneid_t	dlss_zoneid;
55 } dls_stack_t;
56 
57 static kmem_cache_t	*i_dls_devnet_cachep;
58 static kmutex_t		i_dls_mgmt_lock;
59 static krwlock_t	i_dls_devnet_lock;
60 static mod_hash_t	*i_dls_devnet_id_hash;
61 static mod_hash_t	*i_dls_devnet_hash;
62 
63 bpf_attach_fn_t		dls_bpfattach_fn = NULL;
64 bpf_detach_fn_t		dls_bpfdetach_fn = NULL;
65 boolean_t		devnet_need_rebuild;
66 
67 #define	VLAN_HASHSZ	67	/* prime */
68 
69 
70 /*
71  * The following names are default tunnel interface names for backward
72  * compatibility with Solaris 10 and prior.  Opening a /dev/net node with one
73  * of these names causes a tunnel link to be implicitly created in
74  * dls_devnet_hold_by_name().
75  */
76 #define	IPTUN_IPV4_NAME	"ip.tun"
77 #define	IPTUN_IPV6_NAME	"ip6.tun"
78 #define	IPTUN_6TO4_NAME	"ip.6to4tun"
79 
80 #define	IS_IPV4_TUN(name)	(					\
81     strncmp((name), IPTUN_IPV4_NAME, strlen(IPTUN_IPV4_NAME)) == 0)
82 #define	IS_IPV6_TUN(name)	(					\
83     strncmp((name), IPTUN_IPV6_NAME, strlen(IPTUN_IPV6_NAME)) == 0)
84 #define	IS_6TO4_TUN(name)	(					\
85     strncmp((name), IPTUN_6TO4_NAME, strlen(IPTUN_6TO4_NAME)) == 0)
86 #define	IS_IPTUN_LINK(name)	(					\
87     IS_IPV4_TUN(name) || IS_IPV6_TUN(name) || IS_6TO4_TUN(name))
88 
89 /* Upcall door handle */
90 static door_handle_t	dls_mgmt_dh = NULL;
91 
92 #define	DD_CONDEMNED		0x1
93 #define	DD_KSTAT_CHANGING	0x2
94 #define	DD_IMPLICIT_IPTUN	0x4 /* Implicitly-created ip*.*tun* tunnel */
95 
96 /*
97  * This structure is used to keep the <linkid, macname> mapping.
98  * This structure itself is not protected by the mac perimeter, but is
99  * protected by the dd_mutex and i_dls_devnet_lock. Thus most of the
100  * functions manipulating this structure such as dls_devnet_set/unset etc.
101  * may be called while not holding the mac perimeter.
102  */
103 typedef struct dls_devnet_s {
104 	datalink_id_t	dd_linkid;
105 	char		dd_linkname[MAXLINKNAMELEN];
106 	char		dd_mac[MAXNAMELEN];
107 	kstat_t		*dd_ksp;	/* kstat in owner_zid */
108 	kstat_t		*dd_zone_ksp;	/* in dd_zid if != owner_zid */
109 	uint32_t	dd_ref;
110 	kmutex_t	dd_mutex;
111 	kcondvar_t	dd_cv;
112 	uint32_t	dd_tref;
113 	uint_t		dd_flags;
114 	zoneid_t	dd_owner_zid;	/* zone where node was created */
115 	zoneid_t	dd_zid;		/* current zone */
116 	boolean_t	dd_prop_loaded;
117 	taskqid_t	dd_prop_taskid;
118 } dls_devnet_t;
119 
120 static int i_dls_devnet_create_iptun(const char *, datalink_id_t *);
121 static int i_dls_devnet_destroy_iptun(datalink_id_t);
122 static int i_dls_devnet_setzid(dls_devnet_t *, zoneid_t, boolean_t);
123 static int dls_devnet_unset(const char *, datalink_id_t *, boolean_t);
124 
125 /*ARGSUSED*/
126 static int
127 i_dls_devnet_constructor(void *buf, void *arg, int kmflag)
128 {
129 	dls_devnet_t	*ddp = buf;
130 
131 	bzero(buf, sizeof (dls_devnet_t));
132 	mutex_init(&ddp->dd_mutex, NULL, MUTEX_DEFAULT, NULL);
133 	cv_init(&ddp->dd_cv, NULL, CV_DEFAULT, NULL);
134 	return (0);
135 }
136 
137 /*ARGSUSED*/
138 static void
139 i_dls_devnet_destructor(void *buf, void *arg)
140 {
141 	dls_devnet_t	*ddp = buf;
142 
143 	ASSERT(ddp->dd_ksp == NULL);
144 	ASSERT(ddp->dd_ref == 0);
145 	ASSERT(ddp->dd_tref == 0);
146 	mutex_destroy(&ddp->dd_mutex);
147 	cv_destroy(&ddp->dd_cv);
148 }
149 
150 /* ARGSUSED */
151 static int
152 dls_zone_remove(datalink_id_t linkid, void *arg)
153 {
154 	dls_devnet_t *ddp;
155 
156 	if (dls_devnet_hold_tmp(linkid, &ddp) == 0) {
157 		(void) dls_devnet_setzid(ddp, GLOBAL_ZONEID);
158 		dls_devnet_rele_tmp(ddp);
159 	}
160 	return (0);
161 }
162 
163 /* ARGSUSED */
164 static void *
165 dls_stack_init(netstackid_t stackid, netstack_t *ns)
166 {
167 	dls_stack_t *dlss;
168 
169 	dlss = kmem_zalloc(sizeof (*dlss), KM_SLEEP);
170 	dlss->dlss_zoneid = netstackid_to_zoneid(stackid);
171 	return (dlss);
172 }
173 
174 /* ARGSUSED */
175 static void
176 dls_stack_shutdown(netstackid_t stackid, void *arg)
177 {
178 	dls_stack_t	*dlss = (dls_stack_t *)arg;
179 
180 	/* Move remaining datalinks in this zone back to the global zone. */
181 	(void) zone_datalink_walk(dlss->dlss_zoneid, dls_zone_remove, NULL);
182 }
183 
184 /* ARGSUSED */
185 static void
186 dls_stack_fini(netstackid_t stackid, void *arg)
187 {
188 	dls_stack_t	*dlss = (dls_stack_t *)arg;
189 
190 	kmem_free(dlss, sizeof (*dlss));
191 }
192 
193 /*
194  * Module initialization and finalization functions.
195  */
196 void
197 dls_mgmt_init(void)
198 {
199 	mutex_init(&i_dls_mgmt_lock, NULL, MUTEX_DEFAULT, NULL);
200 	rw_init(&i_dls_devnet_lock, NULL, RW_DEFAULT, NULL);
201 
202 	/*
203 	 * Create a kmem_cache of dls_devnet_t structures.
204 	 */
205 	i_dls_devnet_cachep = kmem_cache_create("dls_devnet_cache",
206 	    sizeof (dls_devnet_t), 0, i_dls_devnet_constructor,
207 	    i_dls_devnet_destructor, NULL, NULL, NULL, 0);
208 	ASSERT(i_dls_devnet_cachep != NULL);
209 
210 	/*
211 	 * Create a hash table, keyed by dd_linkid, of dls_devnet_t.
212 	 */
213 	i_dls_devnet_id_hash = mod_hash_create_idhash("dls_devnet_id_hash",
214 	    VLAN_HASHSZ, mod_hash_null_valdtor);
215 
216 	/*
217 	 * Create a hash table, keyed by dd_mac
218 	 */
219 	i_dls_devnet_hash = mod_hash_create_extended("dls_devnet_hash",
220 	    VLAN_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
221 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
222 
223 	devnet_need_rebuild = B_FALSE;
224 
225 	netstack_register(NS_DLS, dls_stack_init, dls_stack_shutdown,
226 	    dls_stack_fini);
227 }
228 
229 void
230 dls_mgmt_fini(void)
231 {
232 	netstack_unregister(NS_DLS);
233 	mod_hash_destroy_hash(i_dls_devnet_hash);
234 	mod_hash_destroy_hash(i_dls_devnet_id_hash);
235 	kmem_cache_destroy(i_dls_devnet_cachep);
236 	rw_destroy(&i_dls_devnet_lock);
237 	mutex_destroy(&i_dls_mgmt_lock);
238 }
239 
240 int
241 dls_mgmt_door_set(boolean_t start)
242 {
243 	int	err;
244 
245 	/* handle daemon restart */
246 	mutex_enter(&i_dls_mgmt_lock);
247 	if (dls_mgmt_dh != NULL) {
248 		door_ki_rele(dls_mgmt_dh);
249 		dls_mgmt_dh = NULL;
250 	}
251 
252 	if (start && ((err = door_ki_open(DLMGMT_DOOR, &dls_mgmt_dh)) != 0)) {
253 		mutex_exit(&i_dls_mgmt_lock);
254 		return (err);
255 	}
256 
257 	mutex_exit(&i_dls_mgmt_lock);
258 
259 	/*
260 	 * Create and associate <link name, linkid> mapping for network devices
261 	 * which are already attached before the daemon is started.
262 	 */
263 	if (start)
264 		softmac_recreate();
265 	return (0);
266 }
267 
268 static boolean_t
269 i_dls_mgmt_door_revoked(door_handle_t dh)
270 {
271 	struct door_info info;
272 	extern int sys_shutdown;
273 
274 	ASSERT(dh != NULL);
275 
276 	if (sys_shutdown) {
277 		cmn_err(CE_NOTE, "dls_mgmt_door: shutdown observed\n");
278 		return (B_TRUE);
279 	}
280 
281 	if (door_ki_info(dh, &info) != 0)
282 		return (B_TRUE);
283 
284 	return ((info.di_attributes & DOOR_REVOKED) != 0);
285 }
286 
287 /*
288  * Upcall to the datalink management daemon (dlmgmtd).
289  */
290 static int
291 i_dls_mgmt_upcall(void *arg, size_t asize, void *rbuf, size_t rsize)
292 {
293 	door_arg_t			darg, save_arg;
294 	door_handle_t			dh;
295 	int				err;
296 	int				retry = 0;
297 
298 #define	MAXRETRYNUM	3
299 
300 	ASSERT(arg);
301 	darg.data_ptr = arg;
302 	darg.data_size = asize;
303 	darg.desc_ptr = NULL;
304 	darg.desc_num = 0;
305 	darg.rbuf = rbuf;
306 	darg.rsize = rsize;
307 	save_arg = darg;
308 
309 retry:
310 	mutex_enter(&i_dls_mgmt_lock);
311 	dh = dls_mgmt_dh;
312 	if ((dh == NULL) || i_dls_mgmt_door_revoked(dh)) {
313 		mutex_exit(&i_dls_mgmt_lock);
314 		return (EBADF);
315 	}
316 	door_ki_hold(dh);
317 	mutex_exit(&i_dls_mgmt_lock);
318 
319 	for (;;) {
320 		retry++;
321 		if ((err = door_ki_upcall_limited(dh, &darg, zone_kcred(),
322 		    SIZE_MAX, 0)) == 0)
323 			break;
324 
325 		/*
326 		 * handle door call errors
327 		 */
328 		darg = save_arg;
329 		switch (err) {
330 		case EINTR:
331 			/*
332 			 * If the operation which caused this door upcall gets
333 			 * interrupted, return directly.
334 			 */
335 			goto done;
336 		case EAGAIN:
337 			/*
338 			 * Repeat upcall if the maximum attempt limit has not
339 			 * been reached.
340 			 */
341 			if (retry < MAXRETRYNUM) {
342 				delay(2 * hz);
343 				break;
344 			}
345 			cmn_err(CE_WARN, "dls: dlmgmtd fatal error %d\n", err);
346 			goto done;
347 		default:
348 			/* A fatal door error */
349 			if (i_dls_mgmt_door_revoked(dh)) {
350 				cmn_err(CE_NOTE,
351 				    "dls: dlmgmtd door service revoked\n");
352 
353 				if (retry < MAXRETRYNUM) {
354 					door_ki_rele(dh);
355 					goto retry;
356 				}
357 			}
358 			cmn_err(CE_WARN, "dls: dlmgmtd fatal error %d\n", err);
359 			goto done;
360 		}
361 	}
362 
363 	if (darg.rbuf != rbuf) {
364 		/*
365 		 * The size of the input rbuf was not big enough, so the
366 		 * upcall allocated the rbuf itself.  If this happens, assume
367 		 * that this was an invalid door call request.
368 		 */
369 		kmem_free(darg.rbuf, darg.rsize);
370 		err = ENOSPC;
371 		goto done;
372 	}
373 
374 	if (darg.rsize != rsize) {
375 		err = EINVAL;
376 		goto done;
377 	}
378 
379 	err = ((dlmgmt_retval_t *)rbuf)->lr_err;
380 
381 done:
382 	door_ki_rele(dh);
383 	return (err);
384 }
385 
386 /*
387  * Request the datalink management daemon to create a link with the attributes
388  * below.  Upon success, zero is returned and linkidp contains the linkid for
389  * the new link; otherwise, an errno is returned.
390  *
391  *     - dev		physical dev_t.  required for all physical links,
392  *		        including GLDv3 links.  It will be used to force the
393  *		        attachment of a physical device, hence the
394  *		        registration of its mac
395  *     - class		datalink class
396  *     - media type	media type; DL_OTHER means unknown
397  *     - persist	whether to persist the datalink
398  */
399 int
400 dls_mgmt_create(const char *devname, dev_t dev, datalink_class_t class,
401     uint32_t media, boolean_t persist, datalink_id_t *linkidp)
402 {
403 	dlmgmt_upcall_arg_create_t	create;
404 	dlmgmt_create_retval_t		retval;
405 	int				err;
406 
407 	create.ld_cmd = DLMGMT_CMD_DLS_CREATE;
408 	create.ld_class = class;
409 	create.ld_media = media;
410 	create.ld_phymaj = getmajor(dev);
411 	create.ld_phyinst = getminor(dev);
412 	create.ld_persist = persist;
413 	if (strlcpy(create.ld_devname, devname, sizeof (create.ld_devname)) >=
414 	    sizeof (create.ld_devname))
415 		return (EINVAL);
416 
417 	if ((err = i_dls_mgmt_upcall(&create, sizeof (create), &retval,
418 	    sizeof (retval))) == 0) {
419 		*linkidp = retval.lr_linkid;
420 	}
421 	return (err);
422 }
423 
424 /*
425  * Request the datalink management daemon to destroy the specified link.
426  * Returns zero upon success, or an errno upon failure.
427  */
428 int
429 dls_mgmt_destroy(datalink_id_t linkid, boolean_t persist)
430 {
431 	dlmgmt_upcall_arg_destroy_t	destroy;
432 	dlmgmt_destroy_retval_t		retval;
433 
434 	destroy.ld_cmd = DLMGMT_CMD_DLS_DESTROY;
435 	destroy.ld_linkid = linkid;
436 	destroy.ld_persist = persist;
437 
438 	return (i_dls_mgmt_upcall(&destroy, sizeof (destroy),
439 	    &retval, sizeof (retval)));
440 }
441 
442 /*
443  * Request the datalink management daemon to verify/update the information
444  * for a physical link.  Upon success, get its linkid.
445  *
446  *     - media type	media type
447  *     - novanity	whether this physical datalink supports vanity naming.
448  *			physical links that do not use the GLDv3 MAC plugin
449  *			cannot suport vanity naming
450  *
451  * This function could fail with ENOENT or EEXIST.  Two cases return EEXIST:
452  *
453  * 1. A link with devname already exists, but the media type does not match.
454  *    In this case, mediap will bee set to the media type of the existing link.
455  * 2. A link with devname already exists, but its link name does not match
456  *    the device name, although this link does not support vanity naming.
457  */
458 int
459 dls_mgmt_update(const char *devname, uint32_t media, boolean_t novanity,
460     uint32_t *mediap, datalink_id_t *linkidp)
461 {
462 	dlmgmt_upcall_arg_update_t	update;
463 	dlmgmt_update_retval_t		retval;
464 	int				err;
465 
466 	update.ld_cmd = DLMGMT_CMD_DLS_UPDATE;
467 
468 	if (strlcpy(update.ld_devname, devname, sizeof (update.ld_devname)) >=
469 	    sizeof (update.ld_devname))
470 		return (EINVAL);
471 
472 	update.ld_media = media;
473 	update.ld_novanity = novanity;
474 
475 	if ((err = i_dls_mgmt_upcall(&update, sizeof (update), &retval,
476 	    sizeof (retval))) == EEXIST) {
477 		*linkidp = retval.lr_linkid;
478 		*mediap = retval.lr_media;
479 	} else if (err == 0) {
480 		*linkidp = retval.lr_linkid;
481 	}
482 
483 	return (err);
484 }
485 
486 /*
487  * Request the datalink management daemon to get the information for a link.
488  * Returns zero upon success, or an errno upon failure.
489  *
490  * Only fills in information for argument pointers that are non-NULL.
491  * Note that the link argument is expected to be MAXLINKNAMELEN bytes.
492  */
493 int
494 dls_mgmt_get_linkinfo(datalink_id_t linkid, char *link,
495     datalink_class_t *classp, uint32_t *mediap, uint32_t *flagsp)
496 {
497 	dlmgmt_door_getname_t	getname;
498 	dlmgmt_getname_retval_t	retval;
499 	int			err, len;
500 
501 	getname.ld_cmd = DLMGMT_CMD_GETNAME;
502 	getname.ld_linkid = linkid;
503 
504 	if ((err = i_dls_mgmt_upcall(&getname, sizeof (getname), &retval,
505 	    sizeof (retval))) != 0) {
506 		return (err);
507 	}
508 
509 	len = strlen(retval.lr_link);
510 	if (len <= 1 || len >= MAXLINKNAMELEN)
511 		return (EINVAL);
512 
513 	if (link != NULL)
514 		(void) strlcpy(link, retval.lr_link, MAXLINKNAMELEN);
515 	if (classp != NULL)
516 		*classp = retval.lr_class;
517 	if (mediap != NULL)
518 		*mediap = retval.lr_media;
519 	if (flagsp != NULL)
520 		*flagsp = retval.lr_flags;
521 	return (0);
522 }
523 
524 /*
525  * Request the datalink management daemon to get the linkid for a link.
526  * Returns a non-zero error code on failure.  The linkid argument is only
527  * set on success (when zero is returned.)
528  */
529 int
530 dls_mgmt_get_linkid(const char *link, datalink_id_t *linkid)
531 {
532 	dlmgmt_door_getlinkid_t		getlinkid;
533 	dlmgmt_getlinkid_retval_t	retval;
534 	int				err;
535 
536 	getlinkid.ld_cmd = DLMGMT_CMD_GETLINKID;
537 	(void) strlcpy(getlinkid.ld_link, link, MAXLINKNAMELEN);
538 
539 	if ((err = i_dls_mgmt_upcall(&getlinkid, sizeof (getlinkid), &retval,
540 	    sizeof (retval))) == 0) {
541 		*linkid = retval.lr_linkid;
542 	}
543 	return (err);
544 }
545 
546 datalink_id_t
547 dls_mgmt_get_next(datalink_id_t linkid, datalink_class_t class,
548     datalink_media_t dmedia, uint32_t flags)
549 {
550 	dlmgmt_door_getnext_t	getnext;
551 	dlmgmt_getnext_retval_t	retval;
552 
553 	getnext.ld_cmd = DLMGMT_CMD_GETNEXT;
554 	getnext.ld_class = class;
555 	getnext.ld_dmedia = dmedia;
556 	getnext.ld_flags = flags;
557 	getnext.ld_linkid = linkid;
558 
559 	if (i_dls_mgmt_upcall(&getnext, sizeof (getnext), &retval,
560 	    sizeof (retval)) != 0) {
561 		return (DATALINK_INVALID_LINKID);
562 	}
563 
564 	return (retval.lr_linkid);
565 }
566 
567 static int
568 i_dls_mgmt_get_linkattr(const datalink_id_t linkid, const char *attr,
569     void *attrval, size_t *attrszp)
570 {
571 	dlmgmt_upcall_arg_getattr_t	getattr;
572 	dlmgmt_getattr_retval_t		retval;
573 	int				err;
574 
575 	getattr.ld_cmd = DLMGMT_CMD_DLS_GETATTR;
576 	getattr.ld_linkid = linkid;
577 	(void) strlcpy(getattr.ld_attr, attr, MAXLINKATTRLEN);
578 
579 	if ((err = i_dls_mgmt_upcall(&getattr, sizeof (getattr), &retval,
580 	    sizeof (retval))) == 0) {
581 		if (*attrszp < retval.lr_attrsz)
582 			return (EINVAL);
583 		*attrszp = retval.lr_attrsz;
584 		bcopy(retval.lr_attrval, attrval, retval.lr_attrsz);
585 	}
586 
587 	return (err);
588 }
589 
590 /*
591  * Note that this function can only get devp successfully for non-VLAN link.
592  */
593 int
594 dls_mgmt_get_phydev(datalink_id_t linkid, dev_t *devp)
595 {
596 	uint64_t	maj, inst;
597 	size_t		attrsz = sizeof (uint64_t);
598 
599 	if (i_dls_mgmt_get_linkattr(linkid, FPHYMAJ, &maj, &attrsz) != 0 ||
600 	    attrsz != sizeof (uint64_t) ||
601 	    i_dls_mgmt_get_linkattr(linkid, FPHYINST, &inst, &attrsz) != 0 ||
602 	    attrsz != sizeof (uint64_t)) {
603 		return (EINVAL);
604 	}
605 
606 	*devp = makedevice((major_t)maj, (minor_t)inst);
607 	return (0);
608 }
609 
610 /*
611  * Request the datalink management daemon to push in
612  * all properties associated with the link.
613  * Returns a non-zero error code on failure.
614  */
615 int
616 dls_mgmt_linkprop_init(datalink_id_t linkid)
617 {
618 	dlmgmt_door_linkprop_init_t	li;
619 	dlmgmt_linkprop_init_retval_t	retval;
620 	int				err;
621 
622 	li.ld_cmd = DLMGMT_CMD_LINKPROP_INIT;
623 	li.ld_linkid = linkid;
624 
625 	err = i_dls_mgmt_upcall(&li, sizeof (li), &retval, sizeof (retval));
626 	return (err);
627 }
628 
629 static void
630 dls_devnet_prop_task(void *arg)
631 {
632 	dls_devnet_t		*ddp = arg;
633 
634 	(void) dls_mgmt_linkprop_init(ddp->dd_linkid);
635 
636 	mutex_enter(&ddp->dd_mutex);
637 	ddp->dd_prop_loaded = B_TRUE;
638 	ddp->dd_prop_taskid = NULL;
639 	cv_broadcast(&ddp->dd_cv);
640 	mutex_exit(&ddp->dd_mutex);
641 }
642 
643 /*
644  * Ensure property loading task is completed.
645  */
646 void
647 dls_devnet_prop_task_wait(dls_dl_handle_t ddp)
648 {
649 	mutex_enter(&ddp->dd_mutex);
650 	while (ddp->dd_prop_taskid != NULL)
651 		cv_wait(&ddp->dd_cv, &ddp->dd_mutex);
652 	mutex_exit(&ddp->dd_mutex);
653 }
654 
655 void
656 dls_devnet_rele_tmp(dls_dl_handle_t dlh)
657 {
658 	dls_devnet_t		*ddp = dlh;
659 
660 	mutex_enter(&ddp->dd_mutex);
661 	ASSERT(ddp->dd_tref != 0);
662 	if (--ddp->dd_tref == 0)
663 		cv_signal(&ddp->dd_cv);
664 	mutex_exit(&ddp->dd_mutex);
665 }
666 
667 int
668 dls_devnet_hold_link(datalink_id_t linkid, dls_dl_handle_t *ddhp,
669     dls_link_t **dlpp)
670 {
671 	dls_dl_handle_t	dlh;
672 	dls_link_t	*dlp;
673 	int		err;
674 
675 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
676 		return (err);
677 
678 	if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0) {
679 		dls_devnet_rele_tmp(dlh);
680 		return (err);
681 	}
682 
683 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
684 
685 	*ddhp = dlh;
686 	*dlpp = dlp;
687 	return (0);
688 }
689 
690 void
691 dls_devnet_rele_link(dls_dl_handle_t dlh, dls_link_t *dlp)
692 {
693 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
694 
695 	dls_link_rele(dlp);
696 	dls_devnet_rele_tmp(dlh);
697 }
698 
699 /*
700  * "link" kstats related functions.
701  */
702 
703 /*
704  * Query the "link" kstats.
705  *
706  * We may be called from the kstat subsystem in an arbitrary context.
707  * If the caller is the stack, the context could be an upcall data
708  * thread. Hence we can't acquire the mac perimeter in this function
709  * for fear of deadlock.
710  */
711 static int
712 dls_devnet_stat_update(kstat_t *ksp, int rw)
713 {
714 	dls_devnet_t	*ddp = ksp->ks_private;
715 	dls_link_t	*dlp;
716 	int		err;
717 
718 	/*
719 	 * Check the link is being renamed or if the link is going away
720 	 * before incrementing dd_tref which in turn prevents the link
721 	 * from being renamed or deleted until we finish.
722 	 */
723 	mutex_enter(&ddp->dd_mutex);
724 	if (ddp->dd_flags & (DD_CONDEMNED | DD_KSTAT_CHANGING)) {
725 		mutex_exit(&ddp->dd_mutex);
726 		return (ENOENT);
727 	}
728 	ddp->dd_tref++;
729 	mutex_exit(&ddp->dd_mutex);
730 
731 	/*
732 	 * If a device detach happens at this time, it will block in
733 	 * dls_devnet_unset since the dd_tref has been bumped up above. So the
734 	 * access to 'dlp' is safe even though we don't hold the mac perimeter.
735 	 */
736 	if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)ddp->dd_mac,
737 	    (mod_hash_val_t *)&dlp) != 0) {
738 		dls_devnet_rele_tmp(ddp);
739 		return (ENOENT);
740 	}
741 
742 	err = dls_stat_update(ksp, dlp, rw);
743 
744 	dls_devnet_rele_tmp(ddp);
745 	return (err);
746 }
747 
748 /*
749  * Create the "link" kstats.
750  */
751 static void
752 dls_devnet_stat_create(dls_devnet_t *ddp, zoneid_t zoneid)
753 {
754 	kstat_t	*ksp;
755 
756 	if (dls_stat_create("link", 0, ddp->dd_linkname, zoneid,
757 	    dls_devnet_stat_update, ddp, &ksp) == 0) {
758 		ASSERT(ksp != NULL);
759 		if (zoneid == ddp->dd_owner_zid) {
760 			ASSERT(ddp->dd_ksp == NULL);
761 			ddp->dd_ksp = ksp;
762 		} else {
763 			ASSERT(ddp->dd_zone_ksp == NULL);
764 			ddp->dd_zone_ksp = ksp;
765 		}
766 	}
767 }
768 
769 /*
770  * Destroy the "link" kstats.
771  */
772 static void
773 dls_devnet_stat_destroy(dls_devnet_t *ddp, zoneid_t zoneid)
774 {
775 	if (zoneid == ddp->dd_owner_zid) {
776 		if (ddp->dd_ksp != NULL) {
777 			kstat_delete(ddp->dd_ksp);
778 			ddp->dd_ksp = NULL;
779 		}
780 	} else {
781 		if (ddp->dd_zone_ksp != NULL) {
782 			kstat_delete(ddp->dd_zone_ksp);
783 			ddp->dd_zone_ksp = NULL;
784 		}
785 	}
786 }
787 
788 /*
789  * The link has been renamed. Destroy the old non-legacy kstats ("link kstats")
790  * and create the new set using the new name.
791  */
792 static void
793 dls_devnet_stat_rename(dls_devnet_t *ddp)
794 {
795 	if (ddp->dd_ksp != NULL) {
796 		kstat_delete(ddp->dd_ksp);
797 		ddp->dd_ksp = NULL;
798 	}
799 	/* We can't rename a link while it's assigned to a non-global zone. */
800 	ASSERT(ddp->dd_zone_ksp == NULL);
801 	dls_devnet_stat_create(ddp, ddp->dd_owner_zid);
802 }
803 
804 /*
805  * Associate a linkid with a given link (identified by macname)
806  */
807 static int
808 dls_devnet_set(const char *macname, datalink_id_t linkid, zoneid_t zoneid,
809     dls_devnet_t **ddpp)
810 {
811 	dls_devnet_t		*ddp = NULL;
812 	datalink_class_t	class;
813 	int			err;
814 	boolean_t		stat_create = B_FALSE;
815 	char			linkname[MAXLINKNAMELEN];
816 
817 	rw_enter(&i_dls_devnet_lock, RW_WRITER);
818 
819 	/*
820 	 * Don't allow callers to set a link name with a linkid that already
821 	 * has a name association (that's what rename is for).
822 	 */
823 	if (linkid != DATALINK_INVALID_LINKID) {
824 		if (mod_hash_find(i_dls_devnet_id_hash,
825 		    (mod_hash_key_t)(uintptr_t)linkid,
826 		    (mod_hash_val_t *)&ddp) == 0) {
827 			err = EEXIST;
828 			goto done;
829 		}
830 		if ((err = dls_mgmt_get_linkinfo(linkid, linkname, &class,
831 		    NULL, NULL)) != 0)
832 			goto done;
833 	}
834 
835 	if ((err = mod_hash_find(i_dls_devnet_hash,
836 	    (mod_hash_key_t)macname, (mod_hash_val_t *)&ddp)) == 0) {
837 		if (ddp->dd_linkid != DATALINK_INVALID_LINKID) {
838 			err = EEXIST;
839 			goto done;
840 		}
841 
842 		/*
843 		 * This might be a physical link that has already
844 		 * been created, but which does not have a linkid
845 		 * because dlmgmtd was not running when it was created.
846 		 */
847 		if (linkid == DATALINK_INVALID_LINKID ||
848 		    class != DATALINK_CLASS_PHYS) {
849 			err = EINVAL;
850 			goto done;
851 		}
852 	} else {
853 		ddp = kmem_cache_alloc(i_dls_devnet_cachep, KM_SLEEP);
854 		ddp->dd_tref = 0;
855 		ddp->dd_ref++;
856 		ddp->dd_owner_zid = zoneid;
857 		(void) strlcpy(ddp->dd_mac, macname, sizeof (ddp->dd_mac));
858 		VERIFY(mod_hash_insert(i_dls_devnet_hash,
859 		    (mod_hash_key_t)ddp->dd_mac, (mod_hash_val_t)ddp) == 0);
860 	}
861 
862 	if (linkid != DATALINK_INVALID_LINKID) {
863 		ddp->dd_linkid = linkid;
864 		(void) strlcpy(ddp->dd_linkname, linkname,
865 		    sizeof (ddp->dd_linkname));
866 		VERIFY(mod_hash_insert(i_dls_devnet_id_hash,
867 		    (mod_hash_key_t)(uintptr_t)linkid,
868 		    (mod_hash_val_t)ddp) == 0);
869 		devnet_need_rebuild = B_TRUE;
870 		stat_create = B_TRUE;
871 		mutex_enter(&ddp->dd_mutex);
872 		if (!ddp->dd_prop_loaded && (ddp->dd_prop_taskid == NULL)) {
873 			ddp->dd_prop_taskid = taskq_dispatch(system_taskq,
874 			    dls_devnet_prop_task, ddp, TQ_SLEEP);
875 		}
876 		mutex_exit(&ddp->dd_mutex);
877 	}
878 	err = 0;
879 done:
880 	/*
881 	 * It is safe to drop the i_dls_devnet_lock at this point. In the case
882 	 * of physical devices, the softmac framework will fail the device
883 	 * detach based on the smac_state or smac_hold_cnt. Other cases like
884 	 * vnic and aggr use their own scheme to serialize creates and deletes
885 	 * and ensure that *ddp is valid.
886 	 */
887 	rw_exit(&i_dls_devnet_lock);
888 	if (err == 0) {
889 		if (zoneid != GLOBAL_ZONEID &&
890 		    (err = i_dls_devnet_setzid(ddp, zoneid, B_FALSE)) != 0)
891 			(void) dls_devnet_unset(macname, &linkid, B_TRUE);
892 		/*
893 		 * The kstat subsystem holds its own locks (rather perimeter)
894 		 * before calling the ks_update (dls_devnet_stat_update) entry
895 		 * point which in turn grabs the i_dls_devnet_lock. So the
896 		 * lock hierarchy is kstat locks -> i_dls_devnet_lock.
897 		 */
898 		if (stat_create)
899 			dls_devnet_stat_create(ddp, zoneid);
900 		if (ddpp != NULL)
901 			*ddpp = ddp;
902 	}
903 	return (err);
904 }
905 
906 /*
907  * Disassociate a linkid with a given link (identified by macname)
908  * This waits until temporary references to the dls_devnet_t are gone.
909  */
910 static int
911 dls_devnet_unset(const char *macname, datalink_id_t *id, boolean_t wait)
912 {
913 	dls_devnet_t	*ddp;
914 	int		err;
915 	mod_hash_val_t	val;
916 
917 	rw_enter(&i_dls_devnet_lock, RW_WRITER);
918 	if ((err = mod_hash_find(i_dls_devnet_hash,
919 	    (mod_hash_key_t)macname, (mod_hash_val_t *)&ddp)) != 0) {
920 		ASSERT(err == MH_ERR_NOTFOUND);
921 		rw_exit(&i_dls_devnet_lock);
922 		return (ENOENT);
923 	}
924 
925 	mutex_enter(&ddp->dd_mutex);
926 
927 	/*
928 	 * Make sure downcalls into softmac_create or softmac_destroy from
929 	 * devfs don't cv_wait on any devfs related condition for fear of
930 	 * deadlock. Return EBUSY if the asynchronous thread started for
931 	 * property loading as part of the post attach hasn't yet completed.
932 	 */
933 	ASSERT(ddp->dd_ref != 0);
934 	if ((ddp->dd_ref != 1) || (!wait &&
935 	    (ddp->dd_tref != 0 || ddp->dd_prop_taskid != NULL))) {
936 		mutex_exit(&ddp->dd_mutex);
937 		rw_exit(&i_dls_devnet_lock);
938 		return (EBUSY);
939 	}
940 
941 	ddp->dd_flags |= DD_CONDEMNED;
942 	ddp->dd_ref--;
943 	*id = ddp->dd_linkid;
944 
945 	if (ddp->dd_zid != GLOBAL_ZONEID)
946 		(void) i_dls_devnet_setzid(ddp, GLOBAL_ZONEID, B_FALSE);
947 
948 	/*
949 	 * Remove this dls_devnet_t from the hash table.
950 	 */
951 	VERIFY(mod_hash_remove(i_dls_devnet_hash,
952 	    (mod_hash_key_t)ddp->dd_mac, &val) == 0);
953 
954 	if (ddp->dd_linkid != DATALINK_INVALID_LINKID) {
955 		VERIFY(mod_hash_remove(i_dls_devnet_id_hash,
956 		    (mod_hash_key_t)(uintptr_t)ddp->dd_linkid, &val) == 0);
957 
958 		devnet_need_rebuild = B_TRUE;
959 	}
960 	rw_exit(&i_dls_devnet_lock);
961 
962 	if (wait) {
963 		/*
964 		 * Wait until all temporary references are released.
965 		 */
966 		while ((ddp->dd_tref != 0) || (ddp->dd_prop_taskid != NULL))
967 			cv_wait(&ddp->dd_cv, &ddp->dd_mutex);
968 	} else {
969 		ASSERT(ddp->dd_tref == 0 && ddp->dd_prop_taskid == NULL);
970 	}
971 
972 	if (ddp->dd_linkid != DATALINK_INVALID_LINKID)
973 		dls_devnet_stat_destroy(ddp, ddp->dd_owner_zid);
974 
975 	ddp->dd_prop_loaded = B_FALSE;
976 	ddp->dd_linkid = DATALINK_INVALID_LINKID;
977 	ddp->dd_flags = 0;
978 	mutex_exit(&ddp->dd_mutex);
979 	kmem_cache_free(i_dls_devnet_cachep, ddp);
980 
981 	return (0);
982 }
983 
984 static int
985 dls_devnet_hold_common(datalink_id_t linkid, dls_devnet_t **ddpp,
986     boolean_t tmp_hold)
987 {
988 	dls_devnet_t		*ddp;
989 	dev_t			phydev = 0;
990 	dls_dev_handle_t	ddh = NULL;
991 	int			err;
992 
993 	/*
994 	 * Hold this link to prevent it being detached in case of a
995 	 * physical link.
996 	 */
997 	if (dls_mgmt_get_phydev(linkid, &phydev) == 0)
998 		(void) softmac_hold_device(phydev, &ddh);
999 
1000 	rw_enter(&i_dls_devnet_lock, RW_WRITER);
1001 	if ((err = mod_hash_find(i_dls_devnet_id_hash,
1002 	    (mod_hash_key_t)(uintptr_t)linkid, (mod_hash_val_t *)&ddp)) != 0) {
1003 		ASSERT(err == MH_ERR_NOTFOUND);
1004 		rw_exit(&i_dls_devnet_lock);
1005 		softmac_rele_device(ddh);
1006 		return (ENOENT);
1007 	}
1008 
1009 	mutex_enter(&ddp->dd_mutex);
1010 	ASSERT(ddp->dd_ref > 0);
1011 	if (ddp->dd_flags & DD_CONDEMNED) {
1012 		mutex_exit(&ddp->dd_mutex);
1013 		rw_exit(&i_dls_devnet_lock);
1014 		softmac_rele_device(ddh);
1015 		return (ENOENT);
1016 	}
1017 	if (tmp_hold)
1018 		ddp->dd_tref++;
1019 	else
1020 		ddp->dd_ref++;
1021 	mutex_exit(&ddp->dd_mutex);
1022 	rw_exit(&i_dls_devnet_lock);
1023 
1024 	softmac_rele_device(ddh);
1025 
1026 	*ddpp = ddp;
1027 	return (0);
1028 }
1029 
1030 int
1031 dls_devnet_hold(datalink_id_t linkid, dls_devnet_t **ddpp)
1032 {
1033 	return (dls_devnet_hold_common(linkid, ddpp, B_FALSE));
1034 }
1035 
1036 /*
1037  * Hold the vanity naming structure (dls_devnet_t) temporarily.  The request to
1038  * delete the dls_devnet_t will wait until the temporary reference is released.
1039  */
1040 int
1041 dls_devnet_hold_tmp(datalink_id_t linkid, dls_devnet_t **ddpp)
1042 {
1043 	return (dls_devnet_hold_common(linkid, ddpp, B_TRUE));
1044 }
1045 
1046 /*
1047  * This funtion is called when a DLS client tries to open a device node.
1048  * This dev_t could a result of a /dev/net node access (returned by
1049  * devnet_create_rvp->dls_devnet_open()) or a direct /dev node access.
1050  * In both cases, this function bumps up the reference count of the
1051  * dls_devnet_t structure. The reference is held as long as the device node
1052  * is open. In the case of /dev/net while it is true that the initial reference
1053  * is held when the devnet_create_rvp->dls_devnet_open call happens, this
1054  * initial reference is released immediately in devnet_inactive_callback ->
1055  * dls_devnet_close(). (Note that devnet_inactive_callback() is called right
1056  * after dld_open completes, not when the /dev/net node is being closed).
1057  * To undo this function, call dls_devnet_rele()
1058  */
1059 int
1060 dls_devnet_hold_by_dev(dev_t dev, dls_dl_handle_t *ddhp)
1061 {
1062 	char			name[MAXNAMELEN];
1063 	char			*drv;
1064 	dls_dev_handle_t	ddh = NULL;
1065 	dls_devnet_t		*ddp;
1066 	int			err;
1067 
1068 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
1069 		return (EINVAL);
1070 
1071 	(void) snprintf(name, sizeof (name), "%s%d", drv, getminor(dev) - 1);
1072 
1073 	/*
1074 	 * Hold this link to prevent it being detached in case of a
1075 	 * GLDv3 physical link.
1076 	 */
1077 	if (getminor(dev) - 1 < MAC_MAX_MINOR)
1078 		(void) softmac_hold_device(dev, &ddh);
1079 
1080 	rw_enter(&i_dls_devnet_lock, RW_WRITER);
1081 	if ((err = mod_hash_find(i_dls_devnet_hash,
1082 	    (mod_hash_key_t)name, (mod_hash_val_t *)&ddp)) != 0) {
1083 		ASSERT(err == MH_ERR_NOTFOUND);
1084 		rw_exit(&i_dls_devnet_lock);
1085 		softmac_rele_device(ddh);
1086 		return (ENOENT);
1087 	}
1088 	mutex_enter(&ddp->dd_mutex);
1089 	ASSERT(ddp->dd_ref > 0);
1090 	if (ddp->dd_flags & DD_CONDEMNED) {
1091 		mutex_exit(&ddp->dd_mutex);
1092 		rw_exit(&i_dls_devnet_lock);
1093 		softmac_rele_device(ddh);
1094 		return (ENOENT);
1095 	}
1096 	ddp->dd_ref++;
1097 	mutex_exit(&ddp->dd_mutex);
1098 	rw_exit(&i_dls_devnet_lock);
1099 
1100 	softmac_rele_device(ddh);
1101 
1102 	*ddhp = ddp;
1103 	return (0);
1104 }
1105 
1106 void
1107 dls_devnet_rele(dls_devnet_t *ddp)
1108 {
1109 	mutex_enter(&ddp->dd_mutex);
1110 	ASSERT(ddp->dd_ref > 1);
1111 	ddp->dd_ref--;
1112 	if ((ddp->dd_flags & DD_IMPLICIT_IPTUN) && ddp->dd_ref == 1) {
1113 		mutex_exit(&ddp->dd_mutex);
1114 		if (i_dls_devnet_destroy_iptun(ddp->dd_linkid) != 0)
1115 			ddp->dd_flags |= DD_IMPLICIT_IPTUN;
1116 		return;
1117 	}
1118 	mutex_exit(&ddp->dd_mutex);
1119 }
1120 
1121 static int
1122 dls_devnet_hold_by_name(const char *link, dls_devnet_t **ddpp)
1123 {
1124 	char			drv[MAXLINKNAMELEN];
1125 	uint_t			ppa;
1126 	major_t			major;
1127 	dev_t			phy_dev, tmp_dev;
1128 	datalink_id_t		linkid;
1129 	dls_dev_handle_t	ddh;
1130 	int			err;
1131 
1132 	if ((err = dls_mgmt_get_linkid(link, &linkid)) == 0)
1133 		return (dls_devnet_hold(linkid, ddpp));
1134 
1135 	/*
1136 	 * If we failed to get the link's linkid because the dlmgmtd daemon
1137 	 * has not been started, return ENOENT so that the application can
1138 	 * fallback to open the /dev node.
1139 	 */
1140 	if (err == EBADF)
1141 		return (ENOENT);
1142 
1143 	if (err != ENOENT)
1144 		return (err);
1145 
1146 	if (IS_IPTUN_LINK(link)) {
1147 		if ((err = i_dls_devnet_create_iptun(link, &linkid)) != 0)
1148 			return (err);
1149 		/*
1150 		 * At this point, an IP tunnel MAC has registered, which
1151 		 * resulted in a link being created.
1152 		 */
1153 		err = dls_devnet_hold(linkid, ddpp);
1154 		ASSERT(err == 0);
1155 		if (err != 0) {
1156 			VERIFY(i_dls_devnet_destroy_iptun(linkid) == 0);
1157 			return (err);
1158 		}
1159 		/*
1160 		 * dls_devnet_rele() will know to destroy the implicit IP
1161 		 * tunnel on last reference release if DD_IMPLICIT_IPTUN is
1162 		 * set.
1163 		 */
1164 		(*ddpp)->dd_flags |= DD_IMPLICIT_IPTUN;
1165 		return (0);
1166 	}
1167 
1168 	if (ddi_parse(link, drv, &ppa) != DDI_SUCCESS)
1169 		return (ENOENT);
1170 
1171 	/*
1172 	 * If this link:
1173 	 * (a) is a physical device, (b) this is the first boot, (c) the MAC
1174 	 * is not registered yet, and (d) we cannot find its linkid, then the
1175 	 * linkname is the same as the devname.
1176 	 *
1177 	 * First filter out invalid names.
1178 	 */
1179 	if ((major = ddi_name_to_major(drv)) == (major_t)-1)
1180 		return (ENOENT);
1181 
1182 	phy_dev = makedevice(major, (minor_t)ppa + 1);
1183 	if (softmac_hold_device(phy_dev, &ddh) != 0)
1184 		return (ENOENT);
1185 
1186 	/*
1187 	 * At this time, the MAC should be registered, check its phy_dev using
1188 	 * the given name.
1189 	 */
1190 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0 ||
1191 	    (err = dls_mgmt_get_phydev(linkid, &tmp_dev)) != 0) {
1192 		softmac_rele_device(ddh);
1193 		return (err);
1194 	}
1195 	if (tmp_dev != phy_dev) {
1196 		softmac_rele_device(ddh);
1197 		return (ENOENT);
1198 	}
1199 
1200 	err = dls_devnet_hold(linkid, ddpp);
1201 	softmac_rele_device(ddh);
1202 	return (err);
1203 }
1204 
1205 int
1206 dls_devnet_macname2linkid(const char *macname, datalink_id_t *linkidp)
1207 {
1208 	dls_devnet_t	*ddp;
1209 
1210 	rw_enter(&i_dls_devnet_lock, RW_READER);
1211 	if (mod_hash_find(i_dls_devnet_hash, (mod_hash_key_t)macname,
1212 	    (mod_hash_val_t *)&ddp) != 0) {
1213 		rw_exit(&i_dls_devnet_lock);
1214 		return (ENOENT);
1215 	}
1216 
1217 	*linkidp = ddp->dd_linkid;
1218 	rw_exit(&i_dls_devnet_lock);
1219 	return (0);
1220 }
1221 
1222 /*
1223  * Get linkid for the given dev.
1224  */
1225 int
1226 dls_devnet_dev2linkid(dev_t dev, datalink_id_t *linkidp)
1227 {
1228 	char	macname[MAXNAMELEN];
1229 	char	*drv;
1230 
1231 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
1232 		return (EINVAL);
1233 
1234 	(void) snprintf(macname, sizeof (macname), "%s%d", drv,
1235 	    getminor(dev) - 1);
1236 	return (dls_devnet_macname2linkid(macname, linkidp));
1237 }
1238 
1239 /*
1240  * Get the link's physical dev_t. It this is a VLAN, get the dev_t of the
1241  * link this VLAN is created on.
1242  */
1243 int
1244 dls_devnet_phydev(datalink_id_t vlanid, dev_t *devp)
1245 {
1246 	dls_devnet_t	*ddp;
1247 	int		err;
1248 
1249 	if ((err = dls_devnet_hold_tmp(vlanid, &ddp)) != 0)
1250 		return (err);
1251 
1252 	err = dls_mgmt_get_phydev(ddp->dd_linkid, devp);
1253 	dls_devnet_rele_tmp(ddp);
1254 	return (err);
1255 }
1256 
1257 /*
1258  * Handle the renaming requests.  There are two rename cases:
1259  *
1260  * 1. Request to rename a valid link (id1) to an non-existent link name
1261  *    (id2). In this case id2 is DATALINK_INVALID_LINKID.  Just check whether
1262  *    id1 is held by any applications.
1263  *
1264  *    In this case, the link's kstats need to be updated using the given name.
1265  *
1266  * 2. Request to rename a valid link (id1) to the name of a REMOVED
1267  *    physical link (id2). In this case, check that id1 and its associated
1268  *    mac is not held by any application, and update the link's linkid to id2.
1269  *
1270  *    This case does not change the <link name, linkid> mapping, so the link's
1271  *    kstats need to be updated with using name associated the given id2.
1272  */
1273 int
1274 dls_devnet_rename(datalink_id_t id1, datalink_id_t id2, const char *link)
1275 {
1276 	dls_dev_handle_t	ddh = NULL;
1277 	int			err = 0;
1278 	dev_t			phydev = 0;
1279 	dls_devnet_t		*ddp;
1280 	mac_perim_handle_t	mph = NULL;
1281 	mac_handle_t		mh;
1282 	mod_hash_val_t		val;
1283 	boolean_t		clear_dd_flag = B_FALSE;
1284 
1285 	/*
1286 	 * In the second case, id2 must be a REMOVED physical link.
1287 	 */
1288 	if ((id2 != DATALINK_INVALID_LINKID) &&
1289 	    (dls_mgmt_get_phydev(id2, &phydev) == 0) &&
1290 	    softmac_hold_device(phydev, &ddh) == 0) {
1291 		softmac_rele_device(ddh);
1292 		return (EEXIST);
1293 	}
1294 
1295 	/*
1296 	 * Hold id1 to prevent it from being detached (if a physical link).
1297 	 */
1298 	if (dls_mgmt_get_phydev(id1, &phydev) == 0)
1299 		(void) softmac_hold_device(phydev, &ddh);
1300 
1301 	/*
1302 	 * The framework does not hold hold locks across calls to the
1303 	 * mac perimeter, hence enter the perimeter first. This also waits
1304 	 * for the property loading to finish.
1305 	 */
1306 	if ((err = mac_perim_enter_by_linkid(id1, &mph)) != 0) {
1307 		softmac_rele_device(ddh);
1308 		return (err);
1309 	}
1310 
1311 	rw_enter(&i_dls_devnet_lock, RW_WRITER);
1312 	if ((err = mod_hash_find(i_dls_devnet_id_hash,
1313 	    (mod_hash_key_t)(uintptr_t)id1, (mod_hash_val_t *)&ddp)) != 0) {
1314 		ASSERT(err == MH_ERR_NOTFOUND);
1315 		err = ENOENT;
1316 		goto done;
1317 	}
1318 
1319 	/*
1320 	 * Return EBUSY if any applications have this link open, if any thread
1321 	 * is currently accessing the link kstats, or if the link is on-loan
1322 	 * to a non-global zone. Then set the DD_KSTAT_CHANGING flag to
1323 	 * prevent any access to the kstats while we delete and recreate
1324 	 * kstats below.
1325 	 */
1326 	mutex_enter(&ddp->dd_mutex);
1327 	if (ddp->dd_ref > 1) {
1328 		mutex_exit(&ddp->dd_mutex);
1329 		err = EBUSY;
1330 		goto done;
1331 	}
1332 
1333 	ddp->dd_flags |= DD_KSTAT_CHANGING;
1334 	clear_dd_flag = B_TRUE;
1335 	mutex_exit(&ddp->dd_mutex);
1336 
1337 	if (id2 == DATALINK_INVALID_LINKID) {
1338 		(void) strlcpy(ddp->dd_linkname, link,
1339 		    sizeof (ddp->dd_linkname));
1340 
1341 		/* rename mac client name and its flow if exists */
1342 		if ((err = mac_open(ddp->dd_mac, &mh)) != 0)
1343 			goto done;
1344 		(void) mac_rename_primary(mh, link);
1345 		mac_close(mh);
1346 		goto done;
1347 	}
1348 
1349 	/*
1350 	 * The second case, check whether the MAC is used by any MAC
1351 	 * user.  This must be a physical link so ddh must not be NULL.
1352 	 */
1353 	if (ddh == NULL) {
1354 		err = EINVAL;
1355 		goto done;
1356 	}
1357 
1358 	if ((err = mac_open(ddp->dd_mac, &mh)) != 0)
1359 		goto done;
1360 
1361 	/*
1362 	 * We release the reference of the MAC which mac_open() is
1363 	 * holding. Note that this mac will not be unregistered
1364 	 * because the physical device is held.
1365 	 */
1366 	mac_close(mh);
1367 
1368 	/*
1369 	 * Check if there is any other MAC clients, if not, hold this mac
1370 	 * exclusively until we are done.
1371 	 */
1372 	if ((err = mac_mark_exclusive(mh)) != 0)
1373 		goto done;
1374 
1375 	/*
1376 	 * Update the link's linkid.
1377 	 */
1378 	if ((err = mod_hash_find(i_dls_devnet_id_hash,
1379 	    (mod_hash_key_t)(uintptr_t)id2, &val)) != MH_ERR_NOTFOUND) {
1380 		mac_unmark_exclusive(mh);
1381 		err = EEXIST;
1382 		goto done;
1383 	}
1384 
1385 	err = dls_mgmt_get_linkinfo(id2, ddp->dd_linkname, NULL, NULL, NULL);
1386 	if (err != 0) {
1387 		mac_unmark_exclusive(mh);
1388 		goto done;
1389 	}
1390 
1391 	(void) mod_hash_remove(i_dls_devnet_id_hash,
1392 	    (mod_hash_key_t)(uintptr_t)id1, &val);
1393 
1394 	ddp->dd_linkid = id2;
1395 	(void) mod_hash_insert(i_dls_devnet_id_hash,
1396 	    (mod_hash_key_t)(uintptr_t)ddp->dd_linkid, (mod_hash_val_t)ddp);
1397 
1398 	mac_unmark_exclusive(mh);
1399 
1400 	/* load properties for new id */
1401 	mutex_enter(&ddp->dd_mutex);
1402 	ddp->dd_prop_loaded = B_FALSE;
1403 	ddp->dd_prop_taskid = taskq_dispatch(system_taskq,
1404 	    dls_devnet_prop_task, ddp, TQ_SLEEP);
1405 	mutex_exit(&ddp->dd_mutex);
1406 
1407 done:
1408 	/*
1409 	 * Change the name of the kstat based on the new link name.
1410 	 * We can't hold the i_dls_devnet_lock across calls to the kstat
1411 	 * subsystem. Instead the DD_KSTAT_CHANGING flag set above in this
1412 	 * function prevents any access to the dd_ksp while we delete and
1413 	 * recreate it below.
1414 	 */
1415 	rw_exit(&i_dls_devnet_lock);
1416 	if (err == 0)
1417 		dls_devnet_stat_rename(ddp);
1418 
1419 	if (clear_dd_flag) {
1420 		mutex_enter(&ddp->dd_mutex);
1421 		ddp->dd_flags &= ~DD_KSTAT_CHANGING;
1422 		mutex_exit(&ddp->dd_mutex);
1423 	}
1424 
1425 	if (mph != NULL)
1426 		mac_perim_exit(mph);
1427 	softmac_rele_device(ddh);
1428 	return (err);
1429 }
1430 
1431 static int
1432 i_dls_devnet_setzid(dls_devnet_t *ddp, zoneid_t new_zoneid, boolean_t setprop)
1433 {
1434 	int			err;
1435 	mac_perim_handle_t	mph;
1436 	boolean_t		upcall_done = B_FALSE;
1437 	datalink_id_t		linkid = ddp->dd_linkid;
1438 	zoneid_t		old_zoneid = ddp->dd_zid;
1439 	dlmgmt_door_setzoneid_t	setzid;
1440 	dlmgmt_setzoneid_retval_t retval;
1441 
1442 	if (old_zoneid == new_zoneid)
1443 		return (0);
1444 
1445 	if ((err = mac_perim_enter_by_macname(ddp->dd_mac, &mph)) != 0)
1446 		return (err);
1447 
1448 	/*
1449 	 * When changing the zoneid of an existing link, we need to tell
1450 	 * dlmgmtd about it.  dlmgmtd already knows the zoneid associated with
1451 	 * newly created links.
1452 	 */
1453 	if (setprop) {
1454 		setzid.ld_cmd = DLMGMT_CMD_SETZONEID;
1455 		setzid.ld_linkid = linkid;
1456 		setzid.ld_zoneid = new_zoneid;
1457 		err = i_dls_mgmt_upcall(&setzid, sizeof (setzid), &retval,
1458 		    sizeof (retval));
1459 		if (err != 0)
1460 			goto done;
1461 		upcall_done = B_TRUE;
1462 	}
1463 	if ((err = dls_link_setzid(ddp->dd_mac, new_zoneid)) == 0) {
1464 		ddp->dd_zid = new_zoneid;
1465 		devnet_need_rebuild = B_TRUE;
1466 	}
1467 
1468 done:
1469 	if (err != 0 && upcall_done) {
1470 		setzid.ld_zoneid = old_zoneid;
1471 		(void) i_dls_mgmt_upcall(&setzid, sizeof (setzid), &retval,
1472 		    sizeof (retval));
1473 	}
1474 	mac_perim_exit(mph);
1475 	return (err);
1476 }
1477 
1478 int
1479 dls_devnet_setzid(dls_dl_handle_t ddh, zoneid_t new_zid)
1480 {
1481 	dls_devnet_t	*ddp;
1482 	int		err;
1483 	zoneid_t	old_zid;
1484 	boolean_t	refheld = B_FALSE;
1485 
1486 	old_zid = ddh->dd_zid;
1487 
1488 	if (old_zid == new_zid)
1489 		return (0);
1490 
1491 	/*
1492 	 * Acquire an additional reference to the link if it is being assigned
1493 	 * to a non-global zone from the global zone.
1494 	 */
1495 	if (old_zid == GLOBAL_ZONEID && new_zid != GLOBAL_ZONEID) {
1496 		if ((err = dls_devnet_hold(ddh->dd_linkid, &ddp)) != 0)
1497 			return (err);
1498 		refheld = B_TRUE;
1499 	}
1500 
1501 	if ((err = i_dls_devnet_setzid(ddh, new_zid, B_TRUE)) != 0) {
1502 		if (refheld)
1503 			dls_devnet_rele(ddp);
1504 		return (err);
1505 	}
1506 
1507 	/*
1508 	 * Release the additional reference if the link is returning to the
1509 	 * global zone from a non-global zone.
1510 	 */
1511 	if (old_zid != GLOBAL_ZONEID && new_zid == GLOBAL_ZONEID)
1512 		dls_devnet_rele(ddh);
1513 
1514 	/* Re-create kstats in the appropriate zones. */
1515 	if (old_zid != GLOBAL_ZONEID)
1516 		dls_devnet_stat_destroy(ddh, old_zid);
1517 	if (new_zid != GLOBAL_ZONEID)
1518 		dls_devnet_stat_create(ddh, new_zid);
1519 
1520 	return (0);
1521 }
1522 
1523 zoneid_t
1524 dls_devnet_getzid(dls_dl_handle_t ddh)
1525 {
1526 	return (((dls_devnet_t *)ddh)->dd_zid);
1527 }
1528 
1529 zoneid_t
1530 dls_devnet_getownerzid(dls_dl_handle_t ddh)
1531 {
1532 	return (((dls_devnet_t *)ddh)->dd_owner_zid);
1533 }
1534 
1535 /*
1536  * Is linkid visible from zoneid?  A link is visible if it was created in the
1537  * zone, or if it is currently assigned to the zone.
1538  */
1539 boolean_t
1540 dls_devnet_islinkvisible(datalink_id_t linkid, zoneid_t zoneid)
1541 {
1542 	dls_devnet_t	*ddp;
1543 	boolean_t	result;
1544 
1545 	if (dls_devnet_hold_tmp(linkid, &ddp) != 0)
1546 		return (B_FALSE);
1547 	result = (ddp->dd_owner_zid == zoneid || ddp->dd_zid == zoneid);
1548 	dls_devnet_rele_tmp(ddp);
1549 	return (result);
1550 }
1551 
1552 /*
1553  * Access a vanity naming node.
1554  */
1555 int
1556 dls_devnet_open(const char *link, dls_dl_handle_t *dhp, dev_t *devp)
1557 {
1558 	dls_devnet_t	*ddp;
1559 	dls_link_t	*dlp;
1560 	zoneid_t	zid = getzoneid();
1561 	int		err;
1562 	mac_perim_handle_t	mph;
1563 
1564 	if ((err = dls_devnet_hold_by_name(link, &ddp)) != 0)
1565 		return (err);
1566 
1567 	dls_devnet_prop_task_wait(ddp);
1568 
1569 	/*
1570 	 * Opening a link that does not belong to the current non-global zone
1571 	 * is not allowed.
1572 	 */
1573 	if (zid != GLOBAL_ZONEID && ddp->dd_zid != zid) {
1574 		dls_devnet_rele(ddp);
1575 		return (ENOENT);
1576 	}
1577 
1578 	err = mac_perim_enter_by_macname(ddp->dd_mac, &mph);
1579 	if (err != 0) {
1580 		dls_devnet_rele(ddp);
1581 		return (err);
1582 	}
1583 
1584 	err = dls_link_hold_create(ddp->dd_mac, &dlp);
1585 	mac_perim_exit(mph);
1586 
1587 	if (err != 0) {
1588 		dls_devnet_rele(ddp);
1589 		return (err);
1590 	}
1591 
1592 	*dhp = ddp;
1593 	*devp = dls_link_dev(dlp);
1594 	return (0);
1595 }
1596 
1597 /*
1598  * Close access to a vanity naming node.
1599  */
1600 void
1601 dls_devnet_close(dls_dl_handle_t dlh)
1602 {
1603 	dls_devnet_t	*ddp = dlh;
1604 	dls_link_t	*dlp;
1605 	mac_perim_handle_t	mph;
1606 
1607 	VERIFY(mac_perim_enter_by_macname(ddp->dd_mac, &mph) == 0);
1608 	VERIFY(dls_link_hold(ddp->dd_mac, &dlp) == 0);
1609 
1610 	/*
1611 	 * One rele for the hold placed in dls_devnet_open, another for
1612 	 * the hold done just above
1613 	 */
1614 	dls_link_rele(dlp);
1615 	dls_link_rele(dlp);
1616 	mac_perim_exit(mph);
1617 
1618 	dls_devnet_rele(ddp);
1619 }
1620 
1621 /*
1622  * This is used by /dev/net to rebuild the nodes for readdir().  It is not
1623  * critical and no protection is needed.
1624  */
1625 boolean_t
1626 dls_devnet_rebuild()
1627 {
1628 	boolean_t updated = devnet_need_rebuild;
1629 
1630 	devnet_need_rebuild = B_FALSE;
1631 	return (updated);
1632 }
1633 
1634 int
1635 dls_devnet_create(mac_handle_t mh, datalink_id_t linkid, zoneid_t zoneid)
1636 {
1637 	dls_link_t	*dlp;
1638 	dls_devnet_t	*ddp;
1639 	int		err;
1640 	mac_perim_handle_t mph;
1641 
1642 	/*
1643 	 * Holding the mac perimeter ensures that the downcall from the
1644 	 * dlmgmt daemon which does the property loading does not proceed
1645 	 * until we relinquish the perimeter.
1646 	 */
1647 	mac_perim_enter_by_mh(mh, &mph);
1648 	/*
1649 	 * Make this association before we call dls_link_hold_create as
1650 	 * we need to use the linkid to get the user name for the link
1651 	 * when we create the MAC client.
1652 	 */
1653 	if ((err = dls_devnet_set(mac_name(mh), linkid, zoneid, &ddp)) == 0) {
1654 		if ((err = dls_link_hold_create(mac_name(mh), &dlp)) != 0) {
1655 			mac_perim_exit(mph);
1656 			(void) dls_devnet_unset(mac_name(mh), &linkid, B_TRUE);
1657 			return (err);
1658 		}
1659 	}
1660 	/*
1661 	 * Tell BPF it is here, if BPF is there
1662 	 */
1663 	if (dls_bpfattach_fn != NULL) {
1664 		/*
1665 		 * The zoneid is passed in explicitly to prevent the need to
1666 		 * do a lookup in dls using the linkid. Such a lookup would need
1667 		 * to use the same hash table that gets used for walking when
1668 		 * dls_set_bpfattach() is called.
1669 		 */
1670 		dls_bpfattach_fn((uintptr_t)mh, mac_type(mh),
1671 		    dlp->dl_zid, BPR_MAC);
1672 	}
1673 	mac_perim_exit(mph);
1674 	return (err);
1675 }
1676 
1677 /*
1678  * Set the linkid of the dls_devnet_t and add it into the i_dls_devnet_id_hash.
1679  * This is called in the case that the dlmgmtd daemon is started later than
1680  * the physical devices get attached, and the linkid is only known after the
1681  * daemon starts.
1682  */
1683 int
1684 dls_devnet_recreate(mac_handle_t mh, datalink_id_t linkid)
1685 {
1686 	ASSERT(linkid != DATALINK_INVALID_LINKID);
1687 	return (dls_devnet_set(mac_name(mh), linkid, GLOBAL_ZONEID, NULL));
1688 }
1689 
1690 int
1691 dls_devnet_destroy(mac_handle_t mh, datalink_id_t *idp, boolean_t wait)
1692 {
1693 	int			err;
1694 	mac_perim_handle_t	mph;
1695 
1696 	*idp = DATALINK_INVALID_LINKID;
1697 	err = dls_devnet_unset(mac_name(mh), idp, wait);
1698 	if (err != 0 && err != ENOENT)
1699 		return (err);
1700 
1701 	/*
1702 	 * Tell BPF that the link is going away, if BPF is there.
1703 	 */
1704 	if (dls_bpfdetach_fn != NULL)
1705 		dls_bpfdetach_fn((uintptr_t)mh);
1706 
1707 	mac_perim_enter_by_mh(mh, &mph);
1708 	err = dls_link_rele_by_name(mac_name(mh));
1709 	mac_perim_exit(mph);
1710 
1711 	if (err != 0) {
1712 		/*
1713 		 * XXX It is a general GLDv3 bug that dls_devnet_set() has to
1714 		 * be called to re-set the link when destroy fails.  The
1715 		 * zoneid below will be incorrect if this function is ever
1716 		 * called from kernel context or from a zone other than that
1717 		 * which initially created the link.
1718 		 */
1719 		(void) dls_devnet_set(mac_name(mh), *idp, crgetzoneid(CRED()),
1720 		    NULL);
1721 	}
1722 	return (err);
1723 }
1724 
1725 /*
1726  * Implicitly create an IP tunnel link.
1727  */
1728 static int
1729 i_dls_devnet_create_iptun(const char *name, datalink_id_t *linkid)
1730 {
1731 	int		err;
1732 	iptun_kparams_t	ik;
1733 	uint32_t	media;
1734 	netstack_t	*ns;
1735 	major_t		iptun_major;
1736 	dev_info_t	*iptun_dip;
1737 
1738 	/* First ensure that the iptun device is attached. */
1739 	if ((iptun_major = ddi_name_to_major(IPTUN_DRIVER_NAME)) == (major_t)-1)
1740 		return (EINVAL);
1741 	if ((iptun_dip = ddi_hold_devi_by_instance(iptun_major, 0, 0)) == NULL)
1742 		return (EINVAL);
1743 
1744 	if (IS_IPV4_TUN(name)) {
1745 		ik.iptun_kparam_type = IPTUN_TYPE_IPV4;
1746 		media = DL_IPV4;
1747 	} else if (IS_6TO4_TUN(name)) {
1748 		ik.iptun_kparam_type = IPTUN_TYPE_6TO4;
1749 		media = DL_6TO4;
1750 	} else if (IS_IPV6_TUN(name)) {
1751 		ik.iptun_kparam_type = IPTUN_TYPE_IPV6;
1752 		media = DL_IPV6;
1753 	}
1754 	ik.iptun_kparam_flags = (IPTUN_KPARAM_TYPE | IPTUN_KPARAM_IMPLICIT);
1755 
1756 	/* Obtain a datalink id for this tunnel. */
1757 	err = dls_mgmt_create((char *)name, 0, DATALINK_CLASS_IPTUN, media,
1758 	    B_FALSE, &ik.iptun_kparam_linkid);
1759 	if (err != 0) {
1760 		ddi_release_devi(iptun_dip);
1761 		return (err);
1762 	}
1763 
1764 	ns = netstack_get_current();
1765 	err = iptun_create(&ik, CRED());
1766 	netstack_rele(ns);
1767 
1768 	if (err != 0)
1769 		VERIFY(dls_mgmt_destroy(ik.iptun_kparam_linkid, B_FALSE) == 0);
1770 	else
1771 		*linkid = ik.iptun_kparam_linkid;
1772 
1773 	ddi_release_devi(iptun_dip);
1774 	return (err);
1775 }
1776 
1777 static int
1778 i_dls_devnet_destroy_iptun(datalink_id_t linkid)
1779 {
1780 	int err;
1781 
1782 	/*
1783 	 * Note the use of zone_kcred() here as opposed to CRED().  This is
1784 	 * because the process that does the last close of this /dev/net node
1785 	 * may not have necessary privileges to delete this IP tunnel, but the
1786 	 * tunnel must always be implicitly deleted on last close.
1787 	 */
1788 	if ((err = iptun_delete(linkid, zone_kcred())) == 0)
1789 		(void) dls_mgmt_destroy(linkid, B_FALSE);
1790 	return (err);
1791 }
1792 
1793 const char *
1794 dls_devnet_mac(dls_dl_handle_t ddh)
1795 {
1796 	return (ddh->dd_mac);
1797 }
1798 
1799 datalink_id_t
1800 dls_devnet_linkid(dls_dl_handle_t ddh)
1801 {
1802 	return (ddh->dd_linkid);
1803 }
1804 
1805 /*ARGSUSED*/
1806 static uint_t
1807 i_dls_bpfattach_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
1808 {
1809 	dls_link_t		*dlp = (dls_link_t *)val;
1810 
1811 	dls_bpfattach_fn((uintptr_t)dlp->dl_mh, mac_type(dlp->dl_mh),
1812 	    dlp->dl_zid, BPR_MAC);
1813 
1814 	return (MH_WALK_CONTINUE);
1815 }
1816 
1817 /*
1818  * Set the functions to call back to when adding or removing a mac so that
1819  * BPF can keep its internal list of these up to date.
1820  */
1821 void
1822 dls_set_bpfattach(bpf_attach_fn_t attach, bpf_detach_fn_t detach)
1823 {
1824 	bpf_attach_fn_t		old = dls_bpfattach_fn;
1825 
1826 	dls_bpfattach_fn = attach;
1827 	dls_bpfdetach_fn = detach;
1828 
1829 	/*
1830 	 * If we're setting a new attach function, call it for every
1831 	 * mac that has already been attached.
1832 	 */
1833 	if (attach != NULL && old == NULL) {
1834 		mod_hash_walk(i_dls_link_hash, i_dls_bpfattach_walker, NULL);
1835 	}
1836 }
1837