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