Lines Matching +full:no +full:- +full:can +full:- +full:fd

31  * at least on *BSD and macOS, it also defines various SIOC ioctls -
51 #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1)
70 * values, not IBM's non-standard IFT_ values.
78 * zero-copy BPF.
129 #include "pcap-int.h"
132 #include "os-proto.h"
137 * to align the IP header on a 4-byte boundary.
149 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
154 * associated with buffer so that it can be used to decide which the
190 * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
229 * 802.11-plus-radio-header DLT_'s defined, even though some
253 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
260 struct pcap_bpf *pb = p->priv; in pcap_getnonblock_bpf()
262 if (pb->zerocopy) in pcap_getnonblock_bpf()
263 return (pb->nonblock); in pcap_getnonblock_bpf()
272 struct pcap_bpf *pb = p->priv; in pcap_setnonblock_bpf()
274 if (pb->zerocopy) { in pcap_setnonblock_bpf()
275 pb->nonblock = nonblock; in pcap_setnonblock_bpf()
284 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
288 * and set up p->buffer and cc to reflect one if available. Notice that if
289 * there was no prior buffer, we select zbuf1 as this will be the first
295 struct pcap_bpf *pb = p->priv; in pcap_next_zbuf_shm()
298 if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) { in pcap_next_zbuf_shm()
299 bzh = (struct bpf_zbuf_header *)pb->zbuf1; in pcap_next_zbuf_shm()
300 if (bzh->bzh_user_gen != in pcap_next_zbuf_shm()
301 atomic_load_acq_int(&bzh->bzh_kernel_gen)) { in pcap_next_zbuf_shm()
302 pb->bzh = bzh; in pcap_next_zbuf_shm()
303 pb->zbuffer = (u_char *)pb->zbuf1; in pcap_next_zbuf_shm()
304 p->buffer = pb->zbuffer + sizeof(*bzh); in pcap_next_zbuf_shm()
305 *cc = bzh->bzh_kernel_len; in pcap_next_zbuf_shm()
308 } else if (pb->zbuffer == pb->zbuf1) { in pcap_next_zbuf_shm()
309 bzh = (struct bpf_zbuf_header *)pb->zbuf2; in pcap_next_zbuf_shm()
310 if (bzh->bzh_user_gen != in pcap_next_zbuf_shm()
311 atomic_load_acq_int(&bzh->bzh_kernel_gen)) { in pcap_next_zbuf_shm()
312 pb->bzh = bzh; in pcap_next_zbuf_shm()
313 pb->zbuffer = (u_char *)pb->zbuf2; in pcap_next_zbuf_shm()
314 p->buffer = pb->zbuffer + sizeof(*bzh); in pcap_next_zbuf_shm()
315 *cc = bzh->bzh_kernel_len; in pcap_next_zbuf_shm()
324 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
333 struct pcap_bpf *pb = p->priv; in pcap_next_zbuf()
341 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000)) in pcap_next_zbuf()
357 tmout = p->opt.timeout; in pcap_next_zbuf()
360 if (pb->interrupted && p->opt.timeout) { in pcap_next_zbuf()
361 expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout; in pcap_next_zbuf()
362 tmout = expire - TSTOMILLI(&cur); in pcap_next_zbuf()
365 pb->interrupted = 0; in pcap_next_zbuf()
369 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { in pcap_next_zbuf()
370 pcapint_fmt_errmsg_for_errno(p->errbuf, in pcap_next_zbuf()
378 * No data in the buffer, so must use select() to wait for data or in pcap_next_zbuf()
382 if (!pb->nonblock) { in pcap_next_zbuf()
384 FD_SET(p->fd, &r_set); in pcap_next_zbuf()
389 r = select(p->fd + 1, &r_set, NULL, NULL, in pcap_next_zbuf()
390 p->opt.timeout != 0 ? &tv : NULL); in pcap_next_zbuf()
392 if (!pb->interrupted && p->opt.timeout) { in pcap_next_zbuf()
393 pb->interrupted = 1; in pcap_next_zbuf()
394 pb->firstsel = cur; in pcap_next_zbuf()
398 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_next_zbuf()
403 pb->interrupted = 0; in pcap_next_zbuf()
416 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { in pcap_next_zbuf()
417 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_next_zbuf()
431 struct pcap_bpf *pb = p->priv; in pcap_ack_zbuf()
433 atomic_store_rel_int(&pb->bzh->bzh_user_gen, in pcap_ack_zbuf()
434 pb->bzh->bzh_kernel_gen); in pcap_ack_zbuf()
435 pb->bzh = NULL; in pcap_ack_zbuf()
436 p->buffer = NULL; in pcap_ack_zbuf()
450 p->activate_op = pcap_activate_bpf; in pcapint_create_interface()
451 p->can_set_rfmon_op = pcap_can_set_rfmon_bpf; in pcapint_create_interface()
457 p->tstamp_precision_list = malloc(2 * sizeof(u_int)); in pcapint_create_interface()
458 if (p->tstamp_precision_list == NULL) { in pcapint_create_interface()
464 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; in pcapint_create_interface()
465 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; in pcapint_create_interface()
466 p->tstamp_precision_count = 2; in pcapint_create_interface()
473 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
478 int fd = -1; in bpf_open() local
501 (fd = open(cloning_device, O_RDWR)) == -1 && in bpf_open()
503 (fd = open(cloning_device, O_RDONLY)) == -1)) { in bpf_open()
506 fd = PCAP_ERROR_PERM_DENIED; in bpf_open()
508 "Attempt to open %s failed - root privileges may be required", in bpf_open()
511 fd = PCAP_ERROR; in bpf_open()
516 return (fd); in bpf_open()
532 * issues, fall back to read-only. This allows a in bpf_open()
533 * non-root user to be granted specific access to pcap in bpf_open()
536 * XXX - we should have an API that has a flag that in bpf_open()
537 * controls whether to open read-only or read-write, in bpf_open()
540 * the device in question) can be indicated at open in bpf_open()
543 fd = open(device, O_RDWR); in bpf_open()
544 if (fd == -1 && errno == EACCES) in bpf_open()
545 fd = open(device, O_RDONLY); in bpf_open()
546 } while (fd < 0 && errno == EBUSY); in bpf_open()
552 if (fd < 0) { in bpf_open()
559 * means we probably have no BPF in bpf_open()
562 fd = PCAP_ERROR_CAPTURE_NOTSUP; in bpf_open()
564 "(there are no BPF devices)"); in bpf_open()
572 fd = PCAP_ERROR; in bpf_open()
584 fd = PCAP_ERROR_PERM_DENIED; in bpf_open()
586 "Attempt to open %s failed - root privileges may be required", in bpf_open()
594 fd = PCAP_ERROR; in bpf_open()
601 return (fd); in bpf_open()
620 * large - our caller may try a smaller buffer if no buffer size
630 bpf_bind(int fd, const char *name, char *errbuf) in bpf_bind() argument
644 if ((ifr.lifr_zoneid = getzoneid()) == -1) { in bpf_bind()
652 * zonename prefix string. The zonename prefixed source datalink can in bpf_bind()
654 * traffic on datalinks in non-global zones. Non-global zones in bpf_bind()
669 * configuring the system to allow non-root in bpf_bind()
678 * Wireshark might make platform-specific suggestions in bpf_bind()
686 znamelen = zonesep - name; in bpf_bind()
696 if (ifr.lifr_zoneid == -1) { in bpf_bind()
707 * no such zone". in bpf_bind()
710 "There is no zone named \"%s\"", in bpf_bind()
714 * No such zone means the name in bpf_bind()
715 * refers to a non-existent interface. in bpf_bind()
743 /* The name is too long, so it can't possibly exist. */ in bpf_bind()
747 status = ioctl(fd, BIOCSETLIF, (caddr_t)&ifr); in bpf_bind()
752 /* The name is too long, so it can't possibly exist. */ in bpf_bind()
756 status = ioctl(fd, BIOCSETIF, (caddr_t)&ifr); in bpf_bind()
776 * There's no such device. in bpf_bind()
787 * the application can report that rather than in bpf_bind()
819 * the device, but are just testing whether it can be opened, or opening
822 * Returns an error code on failure (always negative), and an FD for
823 * the now-bound BPF device on success (always non-negative).
828 int fd; in bpf_open_and_bind() local
834 fd = bpf_open(errbuf); in bpf_open_and_bind()
835 if (fd < 0) in bpf_open_and_bind()
836 return (fd); /* fd is the appropriate error code */ in bpf_open_and_bind()
841 status = bpf_bind(fd, name, errbuf); in bpf_open_and_bind()
843 close(fd); in bpf_open_and_bind()
848 * there's no buffer space. Fail. in bpf_open_and_bind()
858 return (fd); in bpf_open_and_bind()
863 device_exists(int fd, const char *name, char *errbuf) in device_exists() argument
869 /* The name is too long, so it can't possibly exist. */ in device_exists()
873 status = ioctl(fd, SIOCGIFFLAGS, (caddr_t)&ifr); in device_exists()
887 * Some other error - provide a message for it, as in device_exists()
891 "Can't get interface flags on %s", name); in device_exists()
904 get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) in get_dlt_list() argument
907 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) { in get_dlt_list()
911 bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1)); in get_dlt_list()
912 if (bdlp->bfl_list == NULL) { in get_dlt_list()
918 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) { in get_dlt_list()
921 free(bdlp->bfl_list); in get_dlt_list()
927 * list, so that an application can let you choose it, in get_dlt_list()
932 * inside the low-level Ethernet framing). in get_dlt_list()
935 * that has a link-layer type of DLT_EN10MB and that has in get_dlt_list()
936 * no alternate link-layer types; that's done to exclude in get_dlt_list()
938 * right thing to do, but I suspect it is - Ethernet <-> in get_dlt_list()
949 for (i = 0; i < bdlp->bfl_len; i++) { in get_dlt_list()
950 if (bdlp->bfl_list[i] != DLT_EN10MB in get_dlt_list()
952 && bdlp->bfl_list[i] != DLT_IPNET in get_dlt_list()
964 bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS; in get_dlt_list()
965 bdlp->bfl_len++; in get_dlt_list()
988 int fd; in pcap_can_set_rfmon_bpf() local
1001 * enN, to get monitor mode. You get whatever link-layer in pcap_can_set_rfmon_bpf()
1008 * mode (i.e., you can't get 802.11 headers except in monitor in pcap_can_set_rfmon_bpf()
1009 * mode, and you can't get Ethernet headers in monitor mode). in pcap_can_set_rfmon_bpf()
1011 if (uname(&osinfo) == -1) { in pcap_can_set_rfmon_bpf()
1013 * Can't get the OS version; just say "no". in pcap_can_set_rfmon_bpf()
1036 if (strncmp(p->opt.device, "en", 2) != 0) { in pcap_can_set_rfmon_bpf()
1038 * Not an enN device; no monitor mode. in pcap_can_set_rfmon_bpf()
1042 fd = socket(AF_INET, SOCK_DGRAM, 0); in pcap_can_set_rfmon_bpf()
1043 if (fd == -1) { in pcap_can_set_rfmon_bpf()
1044 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_can_set_rfmon_bpf()
1048 if (pcapint_asprintf(&wlt_name, "wlt%s", p->opt.device + 2) == -1) { in pcap_can_set_rfmon_bpf()
1049 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_can_set_rfmon_bpf()
1051 close(fd); in pcap_can_set_rfmon_bpf()
1054 status = device_exists(fd, wlt_name, p->errbuf); in pcap_can_set_rfmon_bpf()
1056 close(fd); in pcap_can_set_rfmon_bpf()
1077 fd = bpf_open(p->errbuf); in pcap_can_set_rfmon_bpf()
1078 if (fd < 0) in pcap_can_set_rfmon_bpf()
1079 return (fd); /* fd is the appropriate error code */ in pcap_can_set_rfmon_bpf()
1084 err = bpf_bind(fd, p->opt.device, p->errbuf); in pcap_can_set_rfmon_bpf()
1086 close(fd); in pcap_can_set_rfmon_bpf()
1091 * there's no buffer space. Fail. in pcap_can_set_rfmon_bpf()
1099 * We know the default link type -- now determine all the DLTs in pcap_can_set_rfmon_bpf()
1105 if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) { in pcap_can_set_rfmon_bpf()
1106 close(fd); in pcap_can_set_rfmon_bpf()
1109 if (find_802_11(&bdl) != -1) { in pcap_can_set_rfmon_bpf()
1111 * We have an 802.11 DLT, so we can set monitor mode. in pcap_can_set_rfmon_bpf()
1114 close(fd); in pcap_can_set_rfmon_bpf()
1118 close(fd); in pcap_can_set_rfmon_bpf()
1130 return (0); /* not an error, just a "can't do" */ in pcap_can_set_rfmon_bpf()
1161 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) { in pcap_stats_bpf()
1162 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_stats_bpf()
1168 * On illumos, NetBSD and Solaris these values are 64-bit, but struct in pcap_stats_bpf()
1171 ps->ps_recv = (u_int)s.bs_recv; in pcap_stats_bpf()
1172 ps->ps_drop = (u_int)s.bs_drop; in pcap_stats_bpf()
1173 ps->ps_ifdrop = 0; in pcap_stats_bpf()
1180 struct pcap_bpf *pb = p->priv; in pcap_read_bpf()
1196 if (p->break_loop) { in pcap_read_bpf()
1198 * Yes - clear the flag that indicates that it in pcap_read_bpf()
1202 p->break_loop = 0; in pcap_read_bpf()
1205 cc = p->cc; in pcap_read_bpf()
1206 if (p->cc == 0) { in pcap_read_bpf()
1208 * When reading without zero-copy from a file descriptor, we in pcap_read_bpf()
1210 * buffer. With zero-copy, we update the p->buffer pointer in pcap_read_bpf()
1216 if (pb->zerocopy) { in pcap_read_bpf()
1217 if (p->buffer != NULL) in pcap_read_bpf()
1227 cc = read(p->fd, p->buffer, p->bufsize); in pcap_read_bpf()
1245 * no idea why this is the case given that in pcap_read_bpf()
1272 * XXX - we should really return in pcap_read_bpf()
1278 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_read_bpf()
1289 if (lseek(p->fd, 0L, SEEK_CUR) + in pcap_read_bpf()
1290 p->bufsize < 0) { in pcap_read_bpf()
1291 (void)lseek(p->fd, 0L, SEEK_SET); in pcap_read_bpf()
1297 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_read_bpf()
1301 bp = (u_char *)p->buffer; in pcap_read_bpf()
1303 bp = p->bp; in pcap_read_bpf()
1318 pad = p->fddipad; in pcap_read_bpf()
1325 * If so, return immediately - if we haven't read any in pcap_read_bpf()
1333 if (p->break_loop) { in pcap_read_bpf()
1334 p->bp = bp; in pcap_read_bpf()
1335 p->cc = (int)(ep - bp); in pcap_read_bpf()
1347 * happened, and just set p->cc to 0. in pcap_read_bpf()
1349 if (p->cc < 0) in pcap_read_bpf()
1350 p->cc = 0; in pcap_read_bpf()
1352 p->break_loop = 0; in pcap_read_bpf()
1358 caplen = bhp->bh_caplen; in pcap_read_bpf()
1359 hdrlen = bhp->bh_hdrlen; in pcap_read_bpf()
1362 * Short-circuit evaluation: if using BPF filter in pcap_read_bpf()
1363 * in kernel, no need to do it now - we already know in pcap_read_bpf()
1368 * that p->fddipad was the amount of padding in pcap_read_bpf()
1374 if (pb->filtering_in_kernel || in pcap_read_bpf()
1375 pcapint_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) { in pcap_read_bpf()
1380 bt.sec = bhp->bh_tstamp.bt_sec; in pcap_read_bpf()
1381 bt.frac = bhp->bh_tstamp.bt_frac; in pcap_read_bpf()
1382 if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) { in pcap_read_bpf()
1396 pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec; in pcap_read_bpf()
1402 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000; in pcap_read_bpf()
1410 pkthdr.ts.tv_usec = (suseconds_t)bhp->bh_tstamp.tv_usec; in pcap_read_bpf()
1415 pkthdr.caplen = caplen - pad; in pcap_read_bpf()
1418 if (bhp->bh_datalen > pad) in pcap_read_bpf()
1419 pkthdr.len = bhp->bh_datalen - pad; in pcap_read_bpf()
1425 pkthdr.len = bhp->bh_datalen; in pcap_read_bpf()
1430 p->bp = bp; in pcap_read_bpf()
1431 p->cc = (int)(ep - bp); in pcap_read_bpf()
1433 * See comment above about p->cc < 0. in pcap_read_bpf()
1435 if (p->cc < 0) in pcap_read_bpf()
1436 p->cc = 0; in pcap_read_bpf()
1447 p->cc = 0; in pcap_read_bpf()
1456 ret = (int)write(p->fd, buf, size); in pcap_inject_bpf()
1458 if (ret == -1 && errno == EAFNOSUPPORT) { in pcap_inject_bpf()
1464 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch in pcap_inject_bpf()
1479 if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { in pcap_inject_bpf()
1480 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_inject_bpf()
1481 errno, "send: can't turn off BIOCSHDRCMPLT"); in pcap_inject_bpf()
1488 ret = (int)write(p->fd, buf, size); in pcap_inject_bpf()
1491 if (ret == -1) { in pcap_inject_bpf()
1492 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, in pcap_inject_bpf()
1505 if (odm_initialize() == -1) { in bpf_odminit()
1506 if (odm_err_msg(odmerrno, &errstr) == -1) in bpf_odminit()
1514 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) { in bpf_odminit()
1515 if (odm_err_msg(odmerrno, &errstr) == -1) in bpf_odminit()
1532 if (odm_unlock(odmlockid) == -1) { in bpf_odmcleanup()
1534 if (odm_err_msg(odmerrno, &errstr) == -1) in bpf_odmcleanup()
1543 if (odm_terminate() == -1) { in bpf_odmcleanup()
1545 if (odm_err_msg(odmerrno, &errstr) == -1) in bpf_odmcleanup()
1580 if (major == -1) { in bpf_load()
1602 if (rc == -1 && errno != ENOENT) { in bpf_load()
1604 errno, "bpf_load: can't stat %s", BPF_NODE "0"); in bpf_load()
1608 if (rc == -1 || getmajor(sbuf.st_rdev) != major) { in bpf_load()
1612 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { in bpf_load()
1615 "bpf_load: can't mknod %s", buf); in bpf_load()
1625 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) || in bpf_load()
1628 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { in bpf_load()
1642 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { in bpf_load()
1661 struct pcap_bpf *pb = p->priv; in pcap_cleanup_bpf()
1668 if (pb->must_do_on_close != 0) { in pcap_cleanup_bpf()
1674 if (pb->must_do_on_close & MUST_CLEAR_RFMON) { in pcap_cleanup_bpf()
1679 * XXX - if somebody else wants it in rfmon in pcap_cleanup_bpf()
1684 if (sock == -1) { in pcap_cleanup_bpf()
1686 "Can't restore interface flags (socket() failed: %s).\n" in pcap_cleanup_bpf()
1691 pcapint_strlcpy(req.ifm_name, pb->device, in pcap_cleanup_bpf()
1695 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n" in pcap_cleanup_bpf()
1706 pb->device, in pcap_cleanup_bpf()
1711 &ifr) == -1) { in pcap_cleanup_bpf()
1713 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n" in pcap_cleanup_bpf()
1728 if (pb->must_do_on_close & MUST_DESTROY_USBUS) { in pcap_cleanup_bpf()
1729 if (if_nametoindex(pb->device) > 0) { in pcap_cleanup_bpf()
1734 pcapint_strlcpy(ifr.ifr_name, pb->device, in pcap_cleanup_bpf()
1747 pb->must_do_on_close = 0; in pcap_cleanup_bpf()
1751 if (pb->zerocopy) { in pcap_cleanup_bpf()
1753 * Delete the mappings. Note that p->buffer gets in pcap_cleanup_bpf()
1759 if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL) in pcap_cleanup_bpf()
1760 (void) munmap(pb->zbuf1, pb->zbufsize); in pcap_cleanup_bpf()
1761 if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL) in pcap_cleanup_bpf()
1762 (void) munmap(pb->zbuf2, pb->zbufsize); in pcap_cleanup_bpf()
1763 p->buffer = NULL; in pcap_cleanup_bpf()
1766 if (pb->device != NULL) { in pcap_cleanup_bpf()
1767 free(pb->device); in pcap_cleanup_bpf()
1768 pb->device = NULL; in pcap_cleanup_bpf()
1777 int fd; in check_setif_failure() local
1782 * No such device exists. in check_setif_failure()
1784 if (p->opt.rfmon && strncmp(p->opt.device, "wlt", 3) == 0) { in check_setif_failure()
1792 fd = socket(AF_INET, SOCK_DGRAM, 0); in check_setif_failure()
1793 if (fd != -1) { in check_setif_failure()
1797 p->opt.device + 3) == -1) { in check_setif_failure()
1799 * We can't find out whether there's in check_setif_failure()
1801 * just report "no such device". in check_setif_failure()
1803 pcapint_fmt_errmsg_for_errno(p->errbuf, in check_setif_failure()
1806 close(fd); in check_setif_failure()
1809 err = device_exists(fd, en_name, p->errbuf); in check_setif_failure()
1815 * exists, but there's no in check_setif_failure()
1827 close(fd); in check_setif_failure()
1830 * We can't find out whether there's in check_setif_failure()
1832 * just report "no such device". in check_setif_failure()
1835 pcapint_fmt_errmsg_for_errno(p->errbuf, in check_setif_failure()
1843 * No such device. in check_setif_failure()
1884 struct pcap_bpf *pb = p->priv;
1889 int fd; local
1914 fd = bpf_open(p->errbuf);
1915 if (fd < 0) {
1916 status = fd;
1920 p->fd = fd;
1922 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
1923 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1930 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1944 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
1945 p->snapshot = MAXIMUM_SNAPLEN;
1947 pb->device = strdup(p->opt.device);
1948 if (pb->device == NULL) {
1949 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1966 if (p->opt.rfmon) {
1985 if (strncmp(p->opt.device, "en", 2) != 0) {
1991 if (sockfd != -1) {
1993 p->opt.device, p->errbuf);
2008 * We can't find out whether
2010 * report "no such device".
2013 pcapint_fmt_errmsg_for_errno(p->errbuf,
2019 wltdev = malloc(strlen(p->opt.device) + 2);
2021 pcapint_fmt_errmsg_for_errno(p->errbuf,
2028 strcat(wltdev, p->opt.device + 2);
2029 free(p->opt.device);
2030 p->opt.device = wltdev;
2045 if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) {
2049 if (if_nametoindex(p->opt.device) == 0) {
2051 * No. We need to create it, and, if we
2064 pcapint_fmt_errmsg_for_errno(p->errbuf,
2066 "Can't open socket");
2080 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2090 pcapint_strlcpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
2093 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2095 p->opt.device);
2097 pcapint_fmt_errmsg_for_errno(p->errbuf,
2099 "Can't create interface for %s",
2100 p->opt.device);
2110 pb->must_do_on_close |= MUST_DESTROY_USBUS;
2123 * the mode to zero-copy. If that fails, use regular buffering. If
2127 if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
2131 pb->zerocopy = 1;
2135 * size supported by zero-copy. This also lets us quickly
2136 * determine whether the kernel generally supports zero-copy.
2142 if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
2143 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2149 if (p->opt.buffer_size != 0) {
2153 v = p->opt.buffer_size;
2155 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
2160 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
2162 pb->zbufsize = roundup(v, getpagesize());
2163 if (pb->zbufsize > zbufmax)
2164 pb->zbufsize = zbufmax;
2165 pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
2166 MAP_ANON, -1, 0);
2167 pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
2168 MAP_ANON, -1, 0);
2169 if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) {
2170 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2176 bz.bz_bufa = pb->zbuf1;
2177 bz.bz_bufb = pb->zbuf2;
2178 bz.bz_buflen = pb->zbufsize;
2179 if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
2180 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2185 status = bpf_bind(fd, p->opt.device, ifnamsiz, p->errbuf);
2192 * XXX - should we do the "keep cutting
2200 v = pb->zbufsize - sizeof(struct bpf_zbuf_header);
2208 if (p->opt.buffer_size != 0) {
2212 if (ioctl(fd, BIOCSBLEN,
2213 (caddr_t)&p->opt.buffer_size) < 0) {
2214 pcapint_fmt_errmsg_for_errno(p->errbuf,
2216 "BIOCSBLEN: %s", p->opt.device);
2224 status = bpf_bind(fd, p->opt.device, p->errbuf);
2245 * No buffer size was explicitly specified.
2253 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
2258 * Ignore the return value - this is because the
2261 * no big deal, we just continue to use the
2264 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
2266 status = bpf_bind(fd, p->opt.device, p->errbuf);
2289 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2290 "BIOCSBLEN: %s: No buffer size worked",
2291 p->opt.device);
2299 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
2300 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2333 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
2363 * We know the default link type -- now determine all the DLTs
2367 if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
2371 p->dlt_count = bdl.bfl_len;
2372 p->dlt_list = bdl.bfl_list;
2380 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2385 * the DLT_EN10MB value, as you can't get that in monitor mode,
2388 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2400 if (new_dlt != -1) {
2407 if (p->opt.rfmon) {
2411 * of link-layer types, as selecting
2422 if (ioctl(p->fd, BIOCSDLT,
2423 &new_dlt) != -1) {
2437 * purge the 802.11 link-layer types
2442 if (!p->oldstyle)
2446 if (p->opt.rfmon) {
2449 * mode, but we have no 802.11
2450 * link-layer types, so they
2451 * can't have it.
2464 if (p->opt.rfmon) {
2484 if (new_dlt != -1) {
2494 if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
2511 * do, but there's not much we can do about that without finding
2515 if (v == DLT_EN10MB && p->dlt_count == 0) {
2516 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
2517 if (p->dlt_list == NULL) {
2518 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2523 p->dlt_list[0] = DLT_EN10MB;
2524 p->dlt_list[1] = DLT_DOCSIS;
2525 p->dlt_count = 2;
2529 p->fddipad = PCAP_FDDIPAD;
2532 p->fddipad = 0;
2533 p->linktype = v;
2538 * the link-layer source address isn't forcibly overwritten.
2542 * XXX - I seem to remember some packet-sending bug in some
2543 * BSDs - check CVS log for "bpf.c"?
2545 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
2546 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2555 * In zero-copy mode, we just use the timeout in select().
2556 * XXX - what if we're in non-blocking mode and the *application*
2559 if (p->opt.timeout && !pb->zerocopy) {
2561 if (p->opt.timeout) {
2564 * XXX - is this seconds/nanoseconds in AIX?
2568 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2569 * 64-bit userland - it takes, as an argument, a
2570 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2585 bpf_to.tv_sec = p->opt.timeout / 1000;
2586 bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2587 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
2588 pcapint_fmt_errmsg_for_errno(p->errbuf,
2595 to.tv_sec = p->opt.timeout / 1000;
2596 to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2597 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
2598 pcapint_fmt_errmsg_for_errno(p->errbuf,
2629 if (p->opt.immediate) {
2632 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
2633 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2642 if (p->opt.immediate) {
2646 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported");
2652 if (p->opt.promisc) {
2654 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
2655 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2663 if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) {
2664 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2671 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
2672 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2677 p->bufsize = v;
2679 if (!pb->zerocopy) {
2681 p->buffer = malloc(p->bufsize);
2682 if (p->buffer == NULL) {
2683 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2691 memset(p->buffer, 0x0, p->bufsize);
2698 * If there's no filter program installed, there's
2699 * no indication to the kernel of what the snapshot
2700 * length should be, so no snapshotting is done.
2709 total_insn.k = p->snapshot;
2713 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
2714 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2721 * On most BPF platforms, either you can do a "select()" or
2723 * or you can do it and it will return "readable" if the
2725 * a non-blocking read will, if the hold buffer is empty
2729 * In the latter case, the fact that a non-blocking read
2730 * will give you the available packets means you can work
2734 * the BPF descriptor into non-blocking mode, and read from
2740 * and non-blocking reads return EWOULDBLOCK if the hold
2741 * buffer is empty, even if the store buffer is non-empty.
2745 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2746 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2747 * here". On all other BPF platforms, we set it to the FD for
2748 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2754 * XXX - what about AIX?
2756 p->selectable_fd = p->fd; /* assume select() works until we know otherwise */
2759 * We can check what OS this is.
2762 if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
2763 strncmp(osinfo.release, "4.4-", 4) == 0)
2764 p->selectable_fd = -1;
2768 p->read_op = pcap_read_bpf;
2769 p->inject_op = pcap_inject_bpf;
2770 p->setfilter_op = pcap_setfilter_bpf;
2771 p->setdirection_op = pcap_setdirection_bpf;
2772 p->set_datalink_op = pcap_set_datalink_bpf;
2773 p->getnonblock_op = pcap_getnonblock_bpf;
2774 p->setnonblock_op = pcap_setnonblock_bpf;
2775 p->stats_op = pcap_stats_bpf;
2776 p->cleanup_op = pcap_cleanup_bpf;
2785 * Not all interfaces can be bound to by BPF, so try to bind to
2794 int fd; local
2822 en_name_len = strlen(name) - 1;
2827 return (-1);
2831 fd = bpf_open_and_bind(en_name, errbuf);
2835 fd = bpf_open_and_bind(name, errbuf);
2836 if (fd < 0) {
2838 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2840 if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
2842 * Yes, so we can't bind to this because it's
2848 * No, so we don't know whether it's supported or not;
2849 * say it is, so that the user can at least try to
2863 close(fd);
2872 * XXX - if there's a way to determine whether there's something
2892 * usbusN interfaces might, or might not, exist for them -
2907 * Leave enough room for a 32-bit (10-digit) bus number.
2921 if (strcmp(usbitem->d_name, ".") == 0 ||
2922 strcmp(usbitem->d_name, "..") == 0) {
2928 p = strchr(usbitem->d_name, '.');
2931 busnumlen = p - usbitem->d_name;
2933 memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen);
2966 if (sock == -1) {
2968 "Can't create socket to get media information for %s",
2970 return (-1);
2982 * Not supported, so we can't provide any
3001 * can sometimes return EPWROFF ("Device power
3003 * as another indication that we can't get a
3015 return (-1);
3058 * Nothing we can do other than mark loopback devices as "the
3061 * XXX - on Solaris, can we do what the dladm command does,
3063 * (Note that you can also get the link speed, and possibly
3085 get_if_flags) == -1)
3086 return (-1); /* failure */
3093 return (-1);
3097 if (finddevs_usb(devlistp, errbuf) == -1)
3098 return (-1);
3108 struct pcap_bpf *pb = p->priv;
3117 if (sock == -1) {
3118 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3119 errno, "can't open socket");
3124 pcapint_strlcpy(req.ifm_name, p->opt.device, sizeof req.ifm_name);
3131 * Can't get the media types.
3137 * There's no such device.
3142 p->errbuf[0] = '\0';
3154 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3162 * No media types.
3174 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3181 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3215 * Don't just check whether we can enable monitor mode,
3238 (void)pcapint_strlcpy(ifr.ifr_name, p->opt.device,
3241 if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
3242 pcapint_fmt_errmsg_for_errno(p->errbuf,
3248 pb->must_do_on_close |= MUST_CLEAR_RFMON;
3262 * Check whether we have any 802.11 link-layer types; return the best
3263 * of the 802.11 link-layer types if we find one, and return -1
3267 * best 802.11 link-layer type; any of the other 802.11-plus-radio
3268 * headers are second-best; 802.11 with no radio information is
3281 new_dlt = -1;
3282 for (i = 0; i < bdlp->bfl_len; i++) {
3283 switch (bdlp->bfl_list[i]) {
3287 * 802.11, but no radio.
3293 if (new_dlt == -1)
3294 new_dlt = bdlp->bfl_list[i];
3311 new_dlt = bdlp->bfl_list[i];
3320 new_dlt = bdlp->bfl_list[i];
3337 * Remove non-802.11 header types from the list of DLT_ values, as we're in
3346 * Scan the list of DLT_ values and discard non-802.11 ones.
3349 for (i = 0; i < p->dlt_count; i++) {
3350 switch (p->dlt_list[i]) {
3369 p->dlt_list[j] = p->dlt_list[i];
3376 p->dlt_count = j;
3380 * Remove 802.11 link-layer types from the list of DLT_ values, as
3393 for (i = 0; i < p->dlt_count; i++) {
3394 switch (p->dlt_list[i]) {
3423 p->dlt_list[j] = p->dlt_list[i];
3430 p->dlt_count = j;
3437 struct pcap_bpf *pb = p->priv;
3440 * Free any user-mode filter we might happen to have installed.
3442 pcap_freecode(&p->fcode);
3447 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
3451 pb->filtering_in_kernel = 1; /* filtering in the kernel */
3454 * Discard any previously-received packets, as they might
3457 * buffered in the kernel, so you can lose packets in any
3460 p->cc = 0;
3470 * run it in userland. (There's no notion of "too big" for
3474 * XXX - if the copy of the program into the kernel failed,
3479 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3481 return (-1);
3487 * XXX - what if we already have a filter in the kernel?
3490 return (-1);
3491 pb->filtering_in_kernel = 0; /* filtering in userland */
3543 if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
3544 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
3546 return (-1);
3596 if (ioctl(p->fd, BIOCSDIRFILT, &dirfilt) == -1) {
3597 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
3599 return (-1);
3626 * Outgoing, but not incoming; we can't specify that.
3628 snprintf(p->errbuf, sizeof(p->errbuf),
3630 return (-1);
3646 if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
3647 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
3649 return (-1);
3657 (void) snprintf(p->errbuf, sizeof(p->errbuf),
3659 return (-1);
3667 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
3668 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
3670 return (-1);
3683 * Platform-specific information.