xref: /titanic_51/usr/src/uts/common/io/dls/dls_link.c (revision 3b17bf656ccff642e3bb47eab92a32f02dff5e97)
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  * Data-Link Services Module
28  */
29 
30 #include	<sys/sysmacros.h>
31 #include	<sys/strsubr.h>
32 #include	<sys/strsun.h>
33 #include	<sys/vlan.h>
34 #include	<sys/dld_impl.h>
35 #include	<sys/sdt.h>
36 #include	<sys/atomic.h>
37 
38 static void		dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid);
39 
40 static kmem_cache_t	*i_dls_link_cachep;
41 mod_hash_t		*i_dls_link_hash;
42 static uint_t		i_dls_link_count;
43 
44 #define		LINK_HASHSZ	67	/* prime */
45 #define		IMPL_HASHSZ	67	/* prime */
46 
47 /*
48  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
49  */
50 #define	MAKE_KEY(_sap)						\
51 	((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
52 
53 #define	DLS_STRIP_PADDING(pktsize, p) {			\
54 	if (pktsize != 0) {				\
55 		ssize_t delta = pktsize - msgdsize(p);	\
56 							\
57 		if (delta < 0)				\
58 			(void) adjmsg(p, delta);	\
59 	}						\
60 }
61 
62 /*
63  * Private functions.
64  */
65 
66 /*ARGSUSED*/
67 static int
68 i_dls_link_constructor(void *buf, void *arg, int kmflag)
69 {
70 	dls_link_t	*dlp = buf;
71 	char		name[MAXNAMELEN];
72 
73 	bzero(buf, sizeof (dls_link_t));
74 
75 	(void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
76 	dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
77 	    mod_hash_null_valdtor);
78 
79 	return (0);
80 }
81 
82 /*ARGSUSED*/
83 static void
84 i_dls_link_destructor(void *buf, void *arg)
85 {
86 	dls_link_t	*dlp = buf;
87 
88 	ASSERT(dlp->dl_ref == 0);
89 	ASSERT(dlp->dl_mh == NULL);
90 	ASSERT(dlp->dl_mah == NULL);
91 	ASSERT(dlp->dl_unknowns == 0);
92 
93 	mod_hash_destroy_idhash(dlp->dl_str_hash);
94 	dlp->dl_str_hash = NULL;
95 
96 }
97 
98 /*
99  * - Parse the mac header information of the given packet.
100  * - Strip the padding and skip over the header. Note that because some
101  *   DLS consumers only check the db_ref count of the first mblk, we
102  *   pullup the message into a single mblk. Because the original message
103  *   is freed as the result of message pulling up, dls_link_header_info()
104  *   is called again to update the mhi_saddr and mhi_daddr pointers in the
105  *   mhip. Further, the dls_link_header_info() function ensures that the
106  *   size of the pulled message is greater than the MAC header size,
107  *   therefore we can directly advance b_rptr to point at the payload.
108  *
109  * We choose to use a macro for performance reasons.
110  */
111 #define	DLS_PREPARE_PKT(dlp, mp, mhip, err) {				\
112 	mblk_t *nextp = (mp)->b_next;					\
113 	if (((err) = dls_link_header_info((dlp), (mp), (mhip))) == 0) {	\
114 		DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp));		\
115 		if (MBLKL((mp)) < (mhip)->mhi_hdrsize) {		\
116 			mblk_t *newmp;					\
117 			if ((newmp = msgpullup((mp), -1)) == NULL) {	\
118 				(err) = EINVAL;				\
119 			} else {					\
120 				(mp)->b_next = NULL;			\
121 				freemsg((mp));				\
122 				(mp) = newmp;				\
123 				VERIFY(dls_link_header_info((dlp),	\
124 				    (mp), (mhip)) == 0);		\
125 				(mp)->b_next = nextp;			\
126 				(mp)->b_rptr += (mhip)->mhi_hdrsize;	\
127 			}						\
128 		} else {						\
129 			(mp)->b_rptr += (mhip)->mhi_hdrsize;		\
130 		}							\
131 	}								\
132 }
133 
134 /*
135  * Truncate the chain starting at mp such that all packets in the chain
136  * have identical source and destination addresses, saps, and tag types
137  * (see below).  It returns a pointer to the mblk following the chain,
138  * NULL if there is no further packet following the processed chain.
139  * The countp argument is set to the number of valid packets in the chain.
140  * Note that the whole MAC header (including the VLAN tag if any) in each
141  * packet will be stripped.
142  */
143 static mblk_t *
144 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
145     uint_t *countp)
146 {
147 	mblk_t		*prevp;
148 	uint_t		npacket = 1;
149 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
150 	uint16_t	vid = VLAN_ID(mhip->mhi_tci);
151 	uint16_t	pri = VLAN_PRI(mhip->mhi_tci);
152 
153 	/*
154 	 * Compare with subsequent headers until we find one that has
155 	 * differing header information. After checking each packet
156 	 * strip padding and skip over the header.
157 	 */
158 	for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
159 		mac_header_info_t cmhi;
160 		uint16_t cvid, cpri;
161 		int err;
162 
163 		DLS_PREPARE_PKT(dlp, mp, &cmhi, err);
164 		if (err != 0)
165 			break;
166 
167 		prevp->b_next = mp;
168 
169 		/*
170 		 * The source, destination, sap, vlan id and the MSGNOLOOP
171 		 * flag must all match in a given subchain.
172 		 */
173 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
174 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
175 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
176 			/*
177 			 * Note that we don't need to restore the padding.
178 			 */
179 			mp->b_rptr -= cmhi.mhi_hdrsize;
180 			break;
181 		}
182 
183 		cvid = VLAN_ID(cmhi.mhi_tci);
184 		cpri = VLAN_PRI(cmhi.mhi_tci);
185 
186 		/*
187 		 * There are several types of packets. Packets don't match
188 		 * if they are classified to different type or if they are
189 		 * VLAN packets but belong to different VLANs:
190 		 *
191 		 * packet type		tagged		vid		pri
192 		 * ---------------------------------------------------------
193 		 * untagged		No		zero		zero
194 		 * VLAN packets		Yes		non-zero	-
195 		 * priority tagged	Yes		zero		non-zero
196 		 * 0 tagged		Yes		zero		zero
197 		 */
198 		if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
199 		    (vid != cvid) || ((vid == VLAN_ID_NONE) &&
200 		    (((pri == 0) && (cpri != 0)) ||
201 		    ((pri != 0) && (cpri == 0))))) {
202 			mp->b_rptr -= cmhi.mhi_hdrsize;
203 			break;
204 		}
205 
206 		npacket++;
207 	}
208 
209 	/*
210 	 * Break the chain at this point and return a pointer to the next
211 	 * sub-chain.
212 	 */
213 	prevp->b_next = NULL;
214 	*countp = npacket;
215 	return (mp);
216 }
217 
218 /* ARGSUSED */
219 static int
220 i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
221 {
222 	dls_head_t *dhp = (dls_head_t *)val;
223 
224 	/*
225 	 * The lock order is  mod_hash's internal lock -> dh_lock as in the
226 	 * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
227 	 */
228 	mutex_enter(&dhp->dh_lock);
229 	if (dhp->dh_removing) {
230 		mutex_exit(&dhp->dh_lock);
231 		return (-1);
232 	}
233 	dhp->dh_ref++;
234 	mutex_exit(&dhp->dh_lock);
235 	return (0);
236 }
237 
238 void
239 i_dls_head_rele(dls_head_t *dhp)
240 {
241 	mutex_enter(&dhp->dh_lock);
242 	dhp->dh_ref--;
243 	if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
244 		cv_broadcast(&dhp->dh_cv);
245 	mutex_exit(&dhp->dh_lock);
246 }
247 
248 static dls_head_t *
249 i_dls_head_alloc(mod_hash_key_t key)
250 {
251 	dls_head_t	*dhp;
252 
253 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
254 	dhp->dh_key = key;
255 	return (dhp);
256 }
257 
258 static void
259 i_dls_head_free(dls_head_t *dhp)
260 {
261 	ASSERT(dhp->dh_ref == 0);
262 	kmem_free(dhp, sizeof (dls_head_t));
263 }
264 
265 /*
266  * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
267  * if this message is sent to any streams.
268  * Note that this function will copy the message chain and the original
269  * mp will remain valid after this function
270  */
271 static uint_t
272 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
273     mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
274     boolean_t (*acceptfunc)())
275 {
276 	mod_hash_t	*hash = dlp->dl_str_hash;
277 	mod_hash_key_t	key;
278 	dls_head_t	*dhp;
279 	dld_str_t	*dsp;
280 	mblk_t		*nmp;
281 	dls_rx_t	ds_rx;
282 	void		*ds_rx_arg;
283 	uint_t		naccepted = 0;
284 	int		rval;
285 
286 	/*
287 	 * Construct a hash key from the VLAN identifier and the
288 	 * DLSAP that represents dld_str_t in promiscuous mode.
289 	 */
290 	key = MAKE_KEY(sap);
291 
292 	/*
293 	 * Search the hash table for dld_str_t eligible to receive
294 	 * a packet chain for this DLSAP/VLAN combination. The mod hash's
295 	 * internal lock serializes find/insert/remove from the mod hash list.
296 	 * Incrementing the dh_ref (while holding the mod hash lock) ensures
297 	 * dls_link_remove will wait for the upcall to finish.
298 	 */
299 	if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
300 	    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
301 		return (B_FALSE);
302 	}
303 
304 	/*
305 	 * Find dld_str_t that will accept the sub-chain.
306 	 */
307 	for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
308 		if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
309 			continue;
310 
311 		/*
312 		 * We have at least one acceptor.
313 		 */
314 		naccepted++;
315 
316 		/*
317 		 * There will normally be at least more dld_str_t
318 		 * (since we've yet to check for non-promiscuous
319 		 * dld_str_t) so dup the sub-chain.
320 		 */
321 		if ((nmp = copymsgchain(mp)) != NULL)
322 			ds_rx(ds_rx_arg, mrh, nmp, mhip);
323 	}
324 
325 	/*
326 	 * Release the hold on the dld_str_t chain now that we have
327 	 * finished walking it.
328 	 */
329 	i_dls_head_rele(dhp);
330 	return (naccepted);
331 }
332 
333 /* ARGSUSED */
334 void
335 i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
336     boolean_t loopback)
337 {
338 	dls_link_t			*dlp = arg;
339 	mod_hash_t			*hash = dlp->dl_str_hash;
340 	mblk_t				*nextp;
341 	mac_header_info_t		mhi;
342 	dls_head_t			*dhp;
343 	dld_str_t			*dsp;
344 	dld_str_t			*ndsp;
345 	mblk_t				*nmp;
346 	mod_hash_key_t			key;
347 	uint_t				npacket;
348 	boolean_t			accepted;
349 	dls_rx_t			ds_rx, nds_rx;
350 	void				*ds_rx_arg, *nds_rx_arg;
351 	uint16_t			vid;
352 	int				err, rval;
353 
354 	/*
355 	 * Walk the packet chain.
356 	 */
357 	for (; mp != NULL; mp = nextp) {
358 		/*
359 		 * Wipe the accepted state.
360 		 */
361 		accepted = B_FALSE;
362 
363 		DLS_PREPARE_PKT(dlp, mp, &mhi, err);
364 		if (err != 0) {
365 			atomic_add_32(&(dlp->dl_unknowns), 1);
366 			nextp = mp->b_next;
367 			mp->b_next = NULL;
368 			freemsg(mp);
369 			continue;
370 		}
371 
372 		/*
373 		 * Grab the longest sub-chain we can process as a single
374 		 * unit.
375 		 */
376 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
377 		ASSERT(npacket != 0);
378 
379 		vid = VLAN_ID(mhi.mhi_tci);
380 
381 		if (mhi.mhi_istagged) {
382 			/*
383 			 * If it is tagged traffic, send it upstream to
384 			 * all dld_str_t which are attached to the physical
385 			 * link and bound to SAP 0x8100.
386 			 */
387 			if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
388 			    ETHERTYPE_VLAN, dls_accept) > 0) {
389 				accepted = B_TRUE;
390 			}
391 
392 			/*
393 			 * Don't pass the packets up if they are tagged
394 			 * packets and:
395 			 *  - their VID and priority are both zero and the
396 			 *    original packet isn't using the PVID (invalid
397 			 *    packets).
398 			 *  - their sap is ETHERTYPE_VLAN and their VID is
399 			 *    zero as they have already been sent upstreams.
400 			 */
401 			if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid &&
402 			    VLAN_PRI(mhi.mhi_tci) == 0) ||
403 			    (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
404 			    vid == VLAN_ID_NONE)) {
405 				freemsgchain(mp);
406 				goto loop;
407 			}
408 		}
409 
410 		/*
411 		 * Construct a hash key from the VLAN identifier and the
412 		 * DLSAP.
413 		 */
414 		key = MAKE_KEY(mhi.mhi_bindsap);
415 
416 		/*
417 		 * Search the has table for dld_str_t eligible to receive
418 		 * a packet chain for this DLSAP/VLAN combination.
419 		 */
420 		if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
421 		    i_dls_head_hold, &rval) != 0 || (rval != 0)) {
422 			freemsgchain(mp);
423 			goto loop;
424 		}
425 
426 		/*
427 		 * Find the first dld_str_t that will accept the sub-chain.
428 		 */
429 		for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
430 			if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
431 				break;
432 
433 		/*
434 		 * If we did not find any dld_str_t willing to accept the
435 		 * sub-chain then throw it away.
436 		 */
437 		if (dsp == NULL) {
438 			i_dls_head_rele(dhp);
439 			freemsgchain(mp);
440 			goto loop;
441 		}
442 
443 		/*
444 		 * We have at least one acceptor.
445 		 */
446 		accepted = B_TRUE;
447 		for (;;) {
448 			/*
449 			 * Find the next dld_str_t that will accept the
450 			 * sub-chain.
451 			 */
452 			for (ndsp = dsp->ds_next; ndsp != NULL;
453 			    ndsp = ndsp->ds_next)
454 				if (dls_accept(ndsp, &mhi, &nds_rx,
455 				    &nds_rx_arg))
456 					break;
457 
458 			/*
459 			 * If there are no more dld_str_t that are willing
460 			 * to accept the sub-chain then we don't need to dup
461 			 * it before handing it to the current one.
462 			 */
463 			if (ndsp == NULL) {
464 				ds_rx(ds_rx_arg, mrh, mp, &mhi);
465 
466 				/*
467 				 * Since there are no more dld_str_t, we're
468 				 * done.
469 				 */
470 				break;
471 			}
472 
473 			/*
474 			 * There are more dld_str_t so dup the sub-chain.
475 			 */
476 			if ((nmp = copymsgchain(mp)) != NULL)
477 				ds_rx(ds_rx_arg, mrh, nmp, &mhi);
478 
479 			dsp = ndsp;
480 			ds_rx = nds_rx;
481 			ds_rx_arg = nds_rx_arg;
482 		}
483 
484 		/*
485 		 * Release the hold on the dld_str_t chain now that we have
486 		 * finished walking it.
487 		 */
488 		i_dls_head_rele(dhp);
489 
490 loop:
491 		/*
492 		 * If there were no acceptors then add the packet count to the
493 		 * 'unknown' count.
494 		 */
495 		if (!accepted)
496 			atomic_add_32(&(dlp->dl_unknowns), npacket);
497 	}
498 }
499 
500 /* ARGSUSED */
501 void
502 dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
503     boolean_t loopback)
504 {
505 	dld_str_t			*dsp = arg;
506 	dls_link_t			*dlp = dsp->ds_dlp;
507 	mac_header_info_t		mhi;
508 	dls_rx_t			ds_rx;
509 	void				*ds_rx_arg;
510 	int				err;
511 
512 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
513 	if (err != 0)
514 		goto drop;
515 
516 	/*
517 	 * If there is promiscuous handle for vlan, we filter out the untagged
518 	 * pkts and pkts that are not for the primary unicast address.
519 	 */
520 	if (dsp->ds_vlan_mph != NULL) {
521 		uint8_t prim_addr[MAXMACADDRLEN];
522 		size_t	addr_length = dsp->ds_mip->mi_addr_length;
523 
524 		if (!(mhi.mhi_istagged))
525 			goto drop;
526 		ASSERT(dsp->ds_mh != NULL);
527 		mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
528 		if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
529 			goto drop;
530 
531 		if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
532 			goto drop;
533 
534 		ds_rx(ds_rx_arg, NULL, mp, &mhi);
535 		return;
536 	}
537 
538 drop:
539 	atomic_add_32(&dlp->dl_unknowns, 1);
540 	freemsg(mp);
541 }
542 
543 /* ARGSUSED */
544 void
545 dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
546     boolean_t loopback)
547 {
548 	dld_str_t			*dsp = arg;
549 	dls_link_t			*dlp = dsp->ds_dlp;
550 	mac_header_info_t		mhi;
551 	dls_rx_t			ds_rx;
552 	void				*ds_rx_arg;
553 	int				err;
554 	dls_head_t			*dhp;
555 	mod_hash_key_t			key;
556 
557 	DLS_PREPARE_PKT(dlp, mp, &mhi, err);
558 	if (err != 0)
559 		goto drop;
560 
561 	/*
562 	 * In order to filter out sap pkt that no dls channel listens, search
563 	 * the hash table trying to find a dld_str_t eligible to receive the pkt
564 	 */
565 	if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
566 		key = MAKE_KEY(mhi.mhi_bindsap);
567 		if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
568 		    (mod_hash_val_t *)&dhp) != 0)
569 			goto drop;
570 	}
571 
572 	if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
573 		goto drop;
574 
575 	ds_rx(ds_rx_arg, NULL, mp, &mhi);
576 	return;
577 
578 drop:
579 	atomic_add_32(&dlp->dl_unknowns, 1);
580 	freemsg(mp);
581 }
582 
583 static void
584 i_dls_link_destroy(dls_link_t *dlp)
585 {
586 	ASSERT(dlp->dl_nactive == 0);
587 	ASSERT(dlp->dl_impl_count == 0);
588 	ASSERT(dlp->dl_zone_ref == 0);
589 
590 	/*
591 	 * Free the structure back to the cache.
592 	 */
593 	if (dlp->dl_mch != NULL)
594 		mac_client_close(dlp->dl_mch, 0);
595 
596 	if (dlp->dl_mh != NULL) {
597 		ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
598 		mac_close(dlp->dl_mh);
599 	}
600 
601 	dlp->dl_mh = NULL;
602 	dlp->dl_mch = NULL;
603 	dlp->dl_mip = NULL;
604 	dlp->dl_unknowns = 0;
605 	kmem_cache_free(i_dls_link_cachep, dlp);
606 }
607 
608 static int
609 i_dls_link_create(const char *name, dls_link_t **dlpp)
610 {
611 	dls_link_t		*dlp;
612 	int			err;
613 
614 	/*
615 	 * Allocate a new dls_link_t structure.
616 	 */
617 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
618 
619 	/*
620 	 * Name the dls_link_t after the MAC interface it represents.
621 	 */
622 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
623 
624 	/*
625 	 * First reference; hold open the MAC interface.
626 	 */
627 	ASSERT(dlp->dl_mh == NULL);
628 	err = mac_open(dlp->dl_name, &dlp->dl_mh);
629 	if (err != 0)
630 		goto bail;
631 
632 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
633 	dlp->dl_mip = mac_info(dlp->dl_mh);
634 
635 	/* DLS is the "primary" MAC client */
636 	ASSERT(dlp->dl_mch == NULL);
637 
638 	err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
639 	    MAC_OPEN_FLAGS_USE_DATALINK_NAME);
640 	if (err != 0)
641 		goto bail;
642 
643 	DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
644 	    dlp->dl_mch);
645 
646 	*dlpp = dlp;
647 	return (0);
648 
649 bail:
650 	i_dls_link_destroy(dlp);
651 	return (err);
652 }
653 
654 /*
655  * Module initialization functions.
656  */
657 
658 void
659 dls_link_init(void)
660 {
661 	/*
662 	 * Create a kmem_cache of dls_link_t structures.
663 	 */
664 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
665 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
666 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
667 	ASSERT(i_dls_link_cachep != NULL);
668 
669 	/*
670 	 * Create a dls_link_t hash table and associated lock.
671 	 */
672 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
673 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
674 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
675 	i_dls_link_count = 0;
676 }
677 
678 int
679 dls_link_fini(void)
680 {
681 	if (i_dls_link_count > 0)
682 		return (EBUSY);
683 
684 	/*
685 	 * Destroy the kmem_cache.
686 	 */
687 	kmem_cache_destroy(i_dls_link_cachep);
688 
689 	/*
690 	 * Destroy the hash table and associated lock.
691 	 */
692 	mod_hash_destroy_hash(i_dls_link_hash);
693 	return (0);
694 }
695 
696 /*
697  * Exported functions.
698  */
699 
700 static int
701 dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
702 {
703 	dls_link_t		*dlp;
704 	int			err;
705 
706 	/*
707 	 * Look up a dls_link_t corresponding to the given macname in the
708 	 * global hash table. The i_dls_link_hash itself is protected by the
709 	 * mod_hash package's internal lock which synchronizes
710 	 * find/insert/remove into the global mod_hash list. Assumes that
711 	 * inserts and removes are single threaded on a per mac end point
712 	 * by the mac perimeter.
713 	 */
714 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
715 	    (mod_hash_val_t *)&dlp)) == 0)
716 		goto done;
717 
718 	ASSERT(err == MH_ERR_NOTFOUND);
719 	if (!create)
720 		return (ENOENT);
721 
722 	/*
723 	 * We didn't find anything so we need to create one.
724 	 */
725 	if ((err = i_dls_link_create(name, &dlp)) != 0)
726 		return (err);
727 
728 	/*
729 	 * Insert the dls_link_t.
730 	 */
731 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
732 	    (mod_hash_val_t)dlp);
733 	ASSERT(err == 0);
734 
735 	atomic_add_32(&i_dls_link_count, 1);
736 	ASSERT(i_dls_link_count != 0);
737 
738 done:
739 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
740 	/*
741 	 * Bump the reference count and hand back the reference.
742 	 */
743 	dlp->dl_ref++;
744 	*dlpp = dlp;
745 	return (0);
746 }
747 
748 int
749 dls_link_hold_create(const char *name, dls_link_t **dlpp)
750 {
751 	return (dls_link_hold_common(name, dlpp, B_TRUE));
752 }
753 
754 int
755 dls_link_hold(const char *name, dls_link_t **dlpp)
756 {
757 	return (dls_link_hold_common(name, dlpp, B_FALSE));
758 }
759 
760 dev_info_t *
761 dls_link_devinfo(dev_t dev)
762 {
763 	dls_link_t	*dlp;
764 	dev_info_t	*dip;
765 	char	macname[MAXNAMELEN];
766 	char	*drv;
767 	mac_perim_handle_t	mph;
768 
769 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
770 		return (NULL);
771 	(void) snprintf(macname, MAXNAMELEN, "%s%d", drv,
772 	    DLS_MINOR2INST(getminor(dev)));
773 
774 	/*
775 	 * The code below assumes that the name constructed above is the
776 	 * macname. This is not the case for legacy devices. Currently this
777 	 * is ok because this function is only called in the getinfo(9e) path,
778 	 * which for a legacy device would directly end up in the driver's
779 	 * getinfo, rather than here
780 	 */
781 	if (mac_perim_enter_by_macname(macname, &mph) != 0)
782 		return (NULL);
783 
784 	if (dls_link_hold(macname, &dlp) != 0) {
785 		mac_perim_exit(mph);
786 		return (NULL);
787 	}
788 
789 	dip = mac_devinfo_get(dlp->dl_mh);
790 	dls_link_rele(dlp);
791 	mac_perim_exit(mph);
792 
793 	return (dip);
794 }
795 
796 dev_t
797 dls_link_dev(dls_link_t *dlp)
798 {
799 	return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
800 	    mac_minor(dlp->dl_mh)));
801 }
802 
803 void
804 dls_link_rele(dls_link_t *dlp)
805 {
806 	mod_hash_val_t	val;
807 
808 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
809 	/*
810 	 * Check if there are any more references.
811 	 */
812 	if (--dlp->dl_ref == 0) {
813 		(void) mod_hash_remove(i_dls_link_hash,
814 		    (mod_hash_key_t)dlp->dl_name, &val);
815 		ASSERT(dlp == (dls_link_t *)val);
816 
817 		/*
818 		 * Destroy the dls_link_t.
819 		 */
820 		i_dls_link_destroy(dlp);
821 		ASSERT(i_dls_link_count > 0);
822 		atomic_add_32(&i_dls_link_count, -1);
823 	}
824 }
825 
826 int
827 dls_link_rele_by_name(const char *name)
828 {
829 	dls_link_t		*dlp;
830 
831 	if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
832 	    (mod_hash_val_t *)&dlp) != 0)
833 		return (ENOENT);
834 
835 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
836 
837 	/*
838 	 * Must fail detach if mac client is busy.
839 	 */
840 	ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
841 	if (mac_link_has_flows(dlp->dl_mch))
842 		return (ENOTEMPTY);
843 
844 	dls_link_rele(dlp);
845 	return (0);
846 }
847 
848 int
849 dls_link_setzid(const char *name, zoneid_t zid)
850 {
851 	dls_link_t	*dlp;
852 	int		err = 0;
853 	zoneid_t	old_zid;
854 
855 	if ((err = dls_link_hold_create(name, &dlp)) != 0)
856 		return (err);
857 
858 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
859 
860 	if ((old_zid = dlp->dl_zid) == zid)
861 		goto done;
862 
863 	/*
864 	 * Check whether this dlp is used by its own zone.  If yes, we cannot
865 	 * change its zoneid.
866 	 */
867 	if (dlp->dl_zone_ref != 0) {
868 		err = EBUSY;
869 		goto done;
870 	}
871 
872 	dls_bpf_newzone(dlp, zid);
873 	dlp->dl_zid = zid;
874 
875 	if (zid == GLOBAL_ZONEID) {
876 		/*
877 		 * The link is moving from a non-global zone to the global
878 		 * zone, so we need to release the reference that was held
879 		 * when the link was originally assigned to the non-global
880 		 * zone.
881 		 */
882 		dls_link_rele(dlp);
883 	}
884 
885 done:
886 	/*
887 	 * We only keep the reference to this link open if the link has
888 	 * successfully moved from the global zone to a non-global zone.
889 	 */
890 	if (err != 0 || old_zid != GLOBAL_ZONEID)
891 		dls_link_rele(dlp);
892 	return (err);
893 }
894 
895 
896 /*
897  * When a NIC changes zone, that change needs to be communicated to BPF
898  * so that it can correctly enforce access rights on it via BPF. In the
899  * absence of a function from BPF to just change the zoneid, this is
900  * done with a detach followed by an attach.
901  */
902 static void
903 dls_bpf_newzone(dls_link_t *dlp, zoneid_t zid)
904 {
905 	if (dls_bpfdetach_fn != NULL)
906 		dls_bpfdetach_fn((uintptr_t)dlp->dl_mh);
907 
908 	if (dls_bpfattach_fn != NULL)
909 		dls_bpfattach_fn((uintptr_t)dlp->dl_mh, mac_type(dlp->dl_mh),
910 		    zid, BPR_MAC);
911 }
912 
913 int
914 dls_link_getzid(const char *name, zoneid_t *zidp)
915 {
916 	dls_link_t	*dlp;
917 	int		err = 0;
918 
919 	if ((err = dls_link_hold(name, &dlp)) != 0)
920 		return (err);
921 
922 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
923 
924 	*zidp = dlp->dl_zid;
925 
926 	dls_link_rele(dlp);
927 	return (0);
928 }
929 
930 void
931 dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
932 {
933 	mod_hash_t	*hash = dlp->dl_str_hash;
934 	mod_hash_key_t	key;
935 	dls_head_t	*dhp;
936 	dld_str_t	*p;
937 	int		err;
938 
939 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
940 
941 	/*
942 	 * Generate a hash key based on the sap.
943 	 */
944 	key = MAKE_KEY(sap);
945 
946 	/*
947 	 * Search the table for a list head with this key.
948 	 */
949 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
950 		ASSERT(err == MH_ERR_NOTFOUND);
951 
952 		dhp = i_dls_head_alloc(key);
953 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
954 		ASSERT(err == 0);
955 	}
956 
957 	/*
958 	 * Add the dld_str_t to the head of the list. List walkers in
959 	 * i_dls_link_rx_* bump up dh_ref to ensure the list does not change
960 	 * while they walk the list. The membar below ensures that list walkers
961 	 * see exactly the old list or the new list.
962 	 */
963 	ASSERT(dsp->ds_next == NULL);
964 	p = dhp->dh_list;
965 	dsp->ds_next = p;
966 
967 	membar_producer();
968 
969 	dhp->dh_list = dsp;
970 
971 	/*
972 	 * Save a pointer to the list head.
973 	 */
974 	dsp->ds_head = dhp;
975 	dlp->dl_impl_count++;
976 }
977 
978 void
979 dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
980 {
981 	mod_hash_t	*hash = dlp->dl_str_hash;
982 	dld_str_t	**pp;
983 	dld_str_t	*p;
984 	dls_head_t	*dhp;
985 
986 	ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
987 
988 	/*
989 	 * We set dh_removing here to tell the receive callbacks not to pass
990 	 * up packets anymore. Then wait till the current callbacks are done.
991 	 * This happens either in the close path or in processing the
992 	 * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
993 	 * The dh_ref ensures there aren't and there won't be any upcalls
994 	 * walking or using the dh_list. The mod hash internal lock ensures
995 	 * that the insert/remove of the dls_head_t itself synchronizes with
996 	 * any i_dls_link_rx trying to locate it. The perimeter ensures that
997 	 * there isn't another simultaneous dls_link_add/remove.
998 	 */
999 	dhp = dsp->ds_head;
1000 
1001 	mutex_enter(&dhp->dh_lock);
1002 	dhp->dh_removing = B_TRUE;
1003 	while (dhp->dh_ref != 0)
1004 		cv_wait(&dhp->dh_cv, &dhp->dh_lock);
1005 	mutex_exit(&dhp->dh_lock);
1006 
1007 	/*
1008 	 * Walk the list and remove the dld_str_t.
1009 	 */
1010 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
1011 		if (p == dsp)
1012 			break;
1013 	}
1014 	ASSERT(p != NULL);
1015 	*pp = p->ds_next;
1016 	p->ds_next = NULL;
1017 	p->ds_head = NULL;
1018 
1019 	ASSERT(dlp->dl_impl_count != 0);
1020 	dlp->dl_impl_count--;
1021 
1022 	if (dhp->dh_list == NULL) {
1023 		mod_hash_val_t	val = NULL;
1024 
1025 		/*
1026 		 * The list is empty so remove the hash table entry.
1027 		 */
1028 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1029 		ASSERT(dhp == (dls_head_t *)val);
1030 		i_dls_head_free(dhp);
1031 	} else {
1032 		mutex_enter(&dhp->dh_lock);
1033 		dhp->dh_removing = B_FALSE;
1034 		mutex_exit(&dhp->dh_lock);
1035 	}
1036 }
1037 
1038 int
1039 dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip)
1040 {
1041 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
1042 	uint16_t	pvid = mac_get_pvid(dlp->dl_mh);
1043 	int		err = 0;
1044 
1045 	/*
1046 	 * Packets should always be at least 16 bit aligned.
1047 	 */
1048 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
1049 
1050 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
1051 		return (err);
1052 
1053 	/*
1054 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
1055 	 * mac_header_info_t as returned by mac_header_info() is
1056 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
1057 	 */
1058 	mhip->mhi_ispvid = B_FALSE;
1059 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
1060 		struct ether_vlan_header *evhp;
1061 		uint16_t sap;
1062 		mblk_t *tmp = NULL;
1063 		size_t size;
1064 
1065 		size = sizeof (struct ether_vlan_header);
1066 		if (MBLKL(mp) < size) {
1067 			/*
1068 			 * Pullup the message in order to get the MAC header
1069 			 * infomation. Note that this is a read-only function,
1070 			 * we keep the input packet intact.
1071 			 */
1072 			if ((tmp = msgpullup(mp, size)) == NULL)
1073 				return (EINVAL);
1074 
1075 			mp = tmp;
1076 		}
1077 		evhp = (struct ether_vlan_header *)mp->b_rptr;
1078 		sap = ntohs(evhp->ether_type);
1079 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
1080 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
1081 		mhip->mhi_tci = ntohs(evhp->ether_tci);
1082 		mhip->mhi_istagged = B_TRUE;
1083 		freemsg(tmp);
1084 
1085 		/*
1086 		 * If this port has a non-zero PVID, then we have to lie to the
1087 		 * caller about the VLAN ID.  It's always zero on receive for
1088 		 * that VLAN.
1089 		 */
1090 		if (pvid != VLAN_ID_NONE && VLAN_ID(mhip->mhi_tci) == pvid) {
1091 			mhip->mhi_tci &= ~(VLAN_ID_MASK << VLAN_ID_SHIFT);
1092 			mhip->mhi_ispvid = B_TRUE;
1093 		}
1094 
1095 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
1096 			return (EINVAL);
1097 	} else {
1098 		mhip->mhi_istagged = B_FALSE;
1099 		mhip->mhi_tci = 0;
1100 	}
1101 
1102 	return (0);
1103 }
1104