Lines Matching refs:d

64  * (bpf_hash) that is index'd by the minor device number for each open file.
145 static int bpf_ifname(struct bpf_d *d, char *, int);
238 * Attach file to the bpf interface, i.e. make d listen on bp.
241 bpf_attachd(struct bpf_d *d, const char *ifname, int dlt)
254 ASSERT(d->bd_bif == NULL);
255 ASSERT(d->bd_mcip == NULL);
256 zone = d->bd_zone;
314 d->bd_mac = *bpr;
315 d->bd_mcip = mcip;
316 d->bd_bif = mh;
317 d->bd_dlt = nicdlt;
319 d->bd_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
321 (void) strlcpy(d->bd_ifname, MBPF_CLIENT_NAME(&d->bd_mac, mcip),
322 sizeof (d->bd_ifname));
324 (void) MBPF_GET_LINKID(&d->bd_mac, d->bd_ifname, &d->bd_linkid,
326 (void) MBPF_PROMISC_ADD(&d->bd_mac, d->bd_mcip, 0, d,
327 &d->bd_promisc_handle, d->bd_promisc_flags);
335 bpf_detachd(struct bpf_d *d)
341 ASSERT(d->bd_inuse == -1);
342 mch = d->bd_mcip;
343 d->bd_mcip = 0;
344 mh = d->bd_bif;
345 d->bd_bif = 0;
354 if (d->bd_promisc)
355 d->bd_promisc = 0;
363 mph = d->bd_promisc_handle;
364 d->bd_promisc_handle = 0;
373 mutex_exit(&d->bd_lock);
375 MBPF_PROMISC_REMOVE(&d->bd_mac, mph);
378 MBPF_CLIENT_CLOSE(&d->bd_mac, mch);
381 MBPF_CLOSE(&d->bd_mac, mh);
387 mutex_enter(&d->bd_lock);
388 *d->bd_ifname = '\0';
389 (void) memset(&d->bd_mac, 0, sizeof (d->bd_mac));
456 struct bpf_d *d;
481 d = (struct bpf_d *)kmem_zalloc(sizeof (*d), KM_SLEEP);
482 d->bd_bufsize = bpf_bufsize;
483 d->bd_fmode = flag;
484 d->bd_zone = crgetzoneid(cred);
485 d->bd_seesent = 1;
486 d->bd_promisc_flags = MAC_PROMISC_FLAGS_NO_PHYS|
488 mutex_init(&d->bd_lock, NULL, MUTEX_DRIVER, NULL);
489 cv_init(&d->bd_wait, NULL, CV_DRIVER, NULL);
503 kmem_free(d, sizeof (*d));
506 d->bd_dev = dmin;
507 LIST_INSERT_HEAD(&bpf_list, d, bd_list);
508 bpf_dev_add(d);
527 struct bpf_d *d = bpf_dev_get(getminor(dev));
529 mutex_enter(&d->bd_lock);
531 while (d->bd_inuse != 0) {
532 d->bd_waiting++;
533 if (cv_wait_sig(&d->bd_wait, &d->bd_lock) <= 0) {
534 d->bd_waiting--;
535 mutex_exit(&d->bd_lock);
538 d->bd_waiting--;
541 d->bd_inuse = -1;
542 if (d->bd_state == BPF_WAITING)
543 bpf_clear_timeout(d);
544 d->bd_state = BPF_IDLE;
545 if (d->bd_bif)
546 bpf_detachd(d);
547 mutex_exit(&d->bd_lock);
550 LIST_REMOVE(d, bd_list);
551 bpf_dev_remove(d);
554 mutex_enter(&d->bd_lock);
555 mutex_destroy(&d->bd_lock);
556 cv_destroy(&d->bd_wait);
558 bpf_freed(d);
559 kmem_free(d, sizeof (*d));
565 * Rotate the packet buffers in descriptor d. Move the store buffer
569 #define ROTATE_BUFFERS(d) \
570 (d)->bd_hbuf = (d)->bd_sbuf; \
571 (d)->bd_hlen = (d)->bd_slen; \
572 (d)->bd_sbuf = (d)->bd_fbuf; \
573 (d)->bd_slen = 0; \
574 (d)->bd_fbuf = 0;
582 struct bpf_d *d = bpf_dev_get(getminor(dev));
587 if ((d->bd_fmode & FREAD) == 0)
594 if (uio->uio_resid != d->bd_bufsize)
597 mutex_enter(&d->bd_lock);
598 if (d->bd_state == BPF_WAITING)
599 bpf_clear_timeout(d);
600 timed_out = (d->bd_state == BPF_TIMED_OUT);
601 d->bd_state = BPF_IDLE;
607 while (d->bd_hbuf == 0) {
608 if (d->bd_nonblock) {
609 if (d->bd_slen == 0) {
610 mutex_exit(&d->bd_lock);
613 ROTATE_BUFFERS(d);
617 if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
623 ROTATE_BUFFERS(d);
627 delay = ddi_get_lbolt() + d->bd_rtout;
628 error = cv_timedwait_sig(&d->bd_wait, &d->bd_lock, delay);
630 mutex_exit(&d->bd_lock);
639 if (d->bd_hbuf)
647 if (d->bd_slen == 0) {
648 mutex_exit(&d->bd_lock);
651 ROTATE_BUFFERS(d);
657 mutex_exit(&d->bd_lock);
664 error = uiomove(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
666 mutex_enter(&d->bd_lock);
667 d->bd_fbuf = d->bd_hbuf;
668 d->bd_hbuf = 0;
669 d->bd_hlen = 0;
671 mutex_exit(&d->bd_lock);
682 bpf_wakeup(struct bpf_d *d)
684 cv_signal(&d->bd_wait);
690 struct bpf_d *d = arg;
692 mutex_enter(&d->bd_lock);
693 if (d->bd_state == BPF_WAITING) {
694 d->bd_state = BPF_TIMED_OUT;
695 if (d->bd_slen != 0)
696 cv_signal(&d->bd_wait);
698 mutex_exit(&d->bd_lock);
706 struct bpf_d *d = bpf_dev_get(getminor(dev));
713 if ((d->bd_fmode & FWRITE) == 0)
716 mutex_enter(&d->bd_lock);
717 if (d->bd_bif == 0 || d->bd_mcip == 0 || d->bd_bif == 0) {
718 mutex_exit(&d->bd_lock);
723 mutex_exit(&d->bd_lock);
727 while (d->bd_inuse < 0) {
728 d->bd_waiting++;
729 if (cv_wait_sig(&d->bd_wait, &d->bd_lock) <= 0) {
730 d->bd_waiting--;
731 mutex_exit(&d->bd_lock);
734 d->bd_waiting--;
737 mutex_exit(&d->bd_lock);
739 dlt = d->bd_dlt;
740 mch = d->bd_mcip;
741 MBPF_SDU_GET(&d->bd_mac, d->bd_bif, &mtu);
742 d->bd_inuse++;
754 DTRACE_PROBE4(bpf__tx, struct bpf_d *, d, int, dlt,
762 error = MBPF_TX(&d->bd_mac, mch, m);
776 mutex_enter(&d->bd_lock);
777 d->bd_inuse--;
778 if ((d->bd_inuse == 0) && (d->bd_waiting != 0))
779 cv_signal(&d->bd_wait);
780 mutex_exit(&d->bd_lock);
794 reset_d(struct bpf_d *d)
796 if (d->bd_hbuf) {
798 d->bd_fbuf = d->bd_hbuf;
799 d->bd_hbuf = 0;
801 d->bd_slen = 0;
802 d->bd_hlen = 0;
803 d->bd_rcount = 0;
804 d->bd_dcount = 0;
805 d->bd_ccount = 0;
829 struct bpf_d *d = bpf_dev_get(getminor(dev));
839 mutex_enter(&d->bd_lock);
840 if (d->bd_state == BPF_WAITING)
841 bpf_clear_timeout(d);
842 d->bd_state = BPF_IDLE;
843 mutex_exit(&d->bd_lock);
858 mutex_enter(&d->bd_lock);
859 n = d->bd_slen;
860 if (d->bd_hbuf)
861 n += d->bd_hlen;
862 mutex_exit(&d->bd_lock);
872 error = copyout(&d->bd_bufsize, (void *)addr,
873 sizeof (d->bd_bufsize));
885 mutex_enter(&d->bd_lock);
886 if (d->bd_bif != 0) {
894 d->bd_bufsize = size;
896 mutex_exit(&d->bd_lock);
910 error = bpf_setf(d, &prog);
917 mutex_enter(&d->bd_lock);
918 reset_d(d);
919 mutex_exit(&d->bd_lock);
928 if (d->bd_bif == 0) {
935 mutex_enter(&d->bd_lock);
936 if (d->bd_promisc == 0) {
938 if (d->bd_promisc_handle) {
941 mph = d->bd_promisc_handle;
942 d->bd_promisc_handle = 0;
944 mutex_exit(&d->bd_lock);
945 MBPF_PROMISC_REMOVE(&d->bd_mac, mph);
946 mutex_enter(&d->bd_lock);
949 d->bd_promisc_flags = MAC_PROMISC_FLAGS_NO_COPY;
950 error = MBPF_PROMISC_ADD(&d->bd_mac,
951 d->bd_mcip, MAC_CLIENT_PROMISC_ALL, d,
952 &d->bd_promisc_handle, d->bd_promisc_flags);
954 d->bd_promisc = 1;
956 mutex_exit(&d->bd_lock);
963 if (d->bd_bif == 0)
966 error = copyout(&d->bd_dlt, (void *)addr,
967 sizeof (d->bd_dlt));
974 if (d->bd_bif == 0) {
983 error = bpf_getdltlist(d, &list);
994 error = bpf_setdlt(d, (void *)addr);
1005 error = bpf_ifname(d, ifreq.ifr_name, sizeof (ifreq.ifr_name));
1021 error = bpf_setif(d, ifreq.ifr_name, sizeof (ifreq.ifr_name));
1032 error = bpf_ifname(d, lifreq.lifr_name,
1049 error = bpf_setif(d, lifreq.lifr_name,
1067 d->bd_rtout = drv_usectohz(tv.tv_sec * 1000000 +
1069 if ((d->bd_rtout == 0) && (tv.tv_usec != 0))
1070 d->bd_rtout = 1;
1082 ticks = drv_hztousec(d->bd_rtout);
1093 if (d->bd_bif == 0) {
1106 error = bpf_getdltlist(d, &list);
1129 error = bpf_setf(d, &prog);
1147 d->bd_rtout = drv_usectohz(tv.tv_sec * 1000000 +
1149 if ((d->bd_rtout == 0) && (tv.tv_usec != 0))
1150 d->bd_rtout = 1;
1162 ticks = drv_hztousec(d->bd_rtout);
1177 bs.bs_recv = d->bd_rcount;
1178 bs.bs_drop = d->bd_dcount;
1179 bs.bs_capt = d->bd_ccount;
1189 if (copyin((void *)addr, &d->bd_immediate,
1190 sizeof (d->bd_immediate)) != 0)
1206 if (copyout(&d->bd_hdrcmplt, (void *)addr,
1207 sizeof (d->bd_hdrcmplt)) != 0)
1212 if (copyin((void *)addr, &d->bd_hdrcmplt,
1213 sizeof (d->bd_hdrcmplt)) != 0)
1221 if (copyout(&d->bd_seesent, (void *)addr,
1222 sizeof (d->bd_seesent)) != 0)
1230 if (copyin((void *)addr, &d->bd_seesent,
1231 sizeof (d->bd_seesent)) != 0)
1236 if (copyin((void *)addr, &d->bd_nonblock,
1237 sizeof (d->bd_nonblock)) != 0)
1245 * Set d's packet filter program to fp. If this file already has a filter,
1251 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
1260 mutex_enter(&d->bd_lock);
1261 old = d->bd_filter;
1262 oldsize = d->bd_filter_size;
1263 d->bd_filter = 0;
1264 d->bd_filter_size = 0;
1265 reset_d(d);
1266 mutex_exit(&d->bd_lock);
1281 mutex_enter(&d->bd_lock);
1282 old = d->bd_filter;
1283 oldsize = d->bd_filter_size;
1284 d->bd_filter = fcode;
1285 d->bd_filter_size = size;
1286 reset_d(d);
1287 mutex_exit(&d->bd_lock);
1303 bpf_setif(struct bpf_d *d, char *ifname, int namesize)
1335 mutex_enter(&d->bd_lock);
1336 while (d->bd_inuse != 0) {
1337 d->bd_waiting++;
1338 if (cv_wait_sig(&d->bd_wait, &d->bd_lock) <= 0) {
1339 d->bd_waiting--;
1340 mutex_exit(&d->bd_lock);
1343 d->bd_waiting--;
1345 d->bd_inuse = -1;
1346 mutex_exit(&d->bd_lock);
1348 if (d->bd_sbuf == 0)
1349 error = bpf_allocbufs(d);
1352 mutex_enter(&d->bd_lock);
1353 if (d->bd_bif)
1357 bpf_detachd(d);
1359 error = bpf_attachd(d, ifname, -1);
1360 reset_d(d);
1361 d->bd_inuse = 0;
1362 if (d->bd_waiting != 0)
1363 cv_signal(&d->bd_wait);
1364 mutex_exit(&d->bd_lock);
1368 mutex_enter(&d->bd_lock);
1369 d->bd_inuse = 0;
1370 if (d->bd_waiting != 0)
1371 cv_signal(&d->bd_wait);
1372 mutex_exit(&d->bd_lock);
1377 return (bpf_provider_tickle(ifname, d->bd_zone));
1384 bpf_ifname(struct bpf_d *d, char *buffer, int bufsize)
1387 mutex_enter(&d->bd_lock);
1388 if (d->bd_bif == NULL) {
1389 mutex_exit(&d->bd_lock);
1393 (void) strlcpy(buffer, d->bd_ifname, bufsize);
1394 mutex_exit(&d->bd_lock);
1411 struct bpf_d *d = bpf_dev_get(getminor(dev));
1417 mutex_enter(&d->bd_lock);
1418 if (d->bd_hlen != 0 ||
1419 ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1420 d->bd_slen != 0)) {
1425 *phpp = &d->bd_poll;
1427 if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1428 bpf_clear_timeout(d);
1432 if (d->bd_callout == 0)
1433 d->bd_callout = timeout(bpf_timed_out,
1434 d, d->bd_rtout);
1435 d->bd_state = BPF_WAITING;
1438 mutex_exit(&d->bd_lock);
1480 bpf_deliver(struct bpf_d *d, cp_fn_t cpfn, void *marg, uint_t pktlen,
1486 if (!d->bd_seesent && issent)
1493 mutex_enter(&d->bd_lock);
1494 slen = bpf_filter(d->bd_filter, marg, pktlen, buflen);
1495 DTRACE_PROBE5(bpf__packet, struct bpf_if *, d->bd_bif,
1496 struct bpf_d *, d, void *, marg, uint_t, pktlen, uint_t, slen);
1497 d->bd_rcount++;
1501 catchpacket(d, marg, pktlen, slen, cpfn, &tv);
1503 mutex_exit(&d->bd_lock);
1514 struct bpf_d *d = arg;
1530 bpf_deliver(d, cpfn, marg, pktlen, buflen, issent);
1545 struct bpf_d *d = arg;
1548 if (ntohl(hdr->hpo_ifindex) != d->bd_linkid)
1550 bpf_deliver(d, bpf_mcpy, m, length, 0, issent);
1563 catchpacket(struct bpf_d *d, uchar_t *pkt, uint_t pktlen, uint_t snaplen,
1568 int hdrlen = d->bd_hdrlen;
1571 ++d->bd_ccount;
1580 if (totlen > d->bd_bufsize)
1581 totlen = d->bd_bufsize;
1586 curlen = BPF_WORDALIGN(d->bd_slen);
1587 if (curlen + totlen > d->bd_bufsize) {
1593 if (d->bd_fbuf == 0) {
1598 ++d->bd_dcount;
1602 ROTATE_BUFFERS(d);
1605 } else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
1618 hp = (struct bpf_hdr *)((char *)d->bd_sbuf + curlen);
1628 d->bd_slen = curlen + totlen;
1634 bpf_wakeup(d);
1641 bpf_allocbufs(struct bpf_d *d)
1644 d->bd_fbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
1645 if (!d->bd_fbuf)
1647 d->bd_sbuf = kmem_zalloc(d->bd_bufsize, KM_NOSLEEP);
1648 if (!d->bd_sbuf) {
1649 kmem_free(d->bd_fbuf, d->bd_bufsize);
1652 d->bd_slen = 0;
1653 d->bd_hlen = 0;
1662 bpf_freed(struct bpf_d *d)
1668 if (d->bd_sbuf != 0) {
1669 kmem_free(d->bd_sbuf, d->bd_bufsize);
1670 if (d->bd_hbuf != 0)
1671 kmem_free(d->bd_hbuf, d->bd_bufsize);
1672 if (d->bd_fbuf != 0)
1673 kmem_free(d->bd_fbuf, d->bd_bufsize);
1675 if (d->bd_filter)
1676 kmem_free(d->bd_filter, d->bd_filter_size);
1683 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *listp)
1698 mutex_enter(&d->bd_lock);
1701 error = MBPF_OPEN(bpr, d->bd_ifname, &mh, d->bd_zone);
1710 if (d->bd_zone != GLOBAL_ZONEID &&
1711 d->bd_zone != zoneid)
1728 d->bd_inuse++;
1729 mutex_exit(&d->bd_lock);
1733 mutex_enter(&d->bd_lock);
1736 d->bd_inuse--;
1749 mutex_exit(&d->bd_lock);
1768 bpf_setdlt(struct bpf_d *d, void *addr)
1778 mutex_enter(&d->bd_lock);
1780 if (d->bd_bif == 0) { /* Interface not set */
1781 mutex_exit(&d->bd_lock);
1784 if (d->bd_dlt == dlt) { /* NULL-op */
1785 mutex_exit(&d->bd_lock);
1789 error = MBPF_GET_ZONE(&d->bd_mac, d->bd_bif, &niczone);
1791 mutex_exit(&d->bd_lock);
1799 if ((d->bd_zone != GLOBAL_ZONEID) && (dlt != DLT_IPNET) &&
1800 (niczone != d->bd_zone)) {
1801 mutex_exit(&d->bd_lock);
1805 (void) strlcpy(ifname, d->bd_ifname, sizeof (ifname));
1806 d->bd_inuse = -1;
1807 bpf_detachd(d);
1808 error = bpf_attachd(d, ifname, dlt);
1809 reset_d(d);
1810 d->bd_inuse = 0;
1812 mutex_exit(&d->bd_lock);
1822 bpf_clear_timeout(struct bpf_d *d)
1824 timeout_id_t tid = d->bd_callout;
1825 d->bd_callout = 0;
1826 d->bd_inuse++;
1834 mutex_exit(&d->bd_lock);
1836 mutex_enter(&d->bd_lock);
1839 d->bd_inuse--;
1853 struct bpf_d *d = NULL;
1856 (mod_hash_val_t *)&d);
1858 return (d);
1862 bpf_dev_add(struct bpf_d *d)
1864 (void) mod_hash_insert(bpf_hash, (mod_hash_key_t)(uintptr_t)d->bd_dev,
1865 (mod_hash_val_t)d);
1869 bpf_dev_remove(struct bpf_d *d)
1873 (void) mod_hash_remove(bpf_hash, (mod_hash_key_t)(uintptr_t)d->bd_dev,
1875 ASSERT(stor == d);
1886 struct bpf_d *d = NULL;
1889 (mod_hash_val_t *)&d);
1890 ASSERT(d != NULL);
1892 return (d);