/*1* This code is derived from code formerly in pcap-dlpi.c, originally2* contributed by Atanu Ghosh ([email protected]), University College3* London, and subsequently modified by Guy Harris ([email protected]),4* Mark Pizzolato <[email protected]>,5* Mark C. Brown ([email protected]), and Sagun Shakya <[email protected]>.6*/78/*9* This file contains dlpi/libdlpi related common functions used10* by pcap-[dlpi,libdlpi].c.11*/1213#include <config.h>1415#ifndef DL_IPATM16#define DL_IPATM 0x12 /* ATM Classical IP interface */17#endif1819#ifdef HAVE_SYS_BUFMOD_H20/*21* Size of a bufmod chunk to pass upstream; that appears to be the22* biggest value to which you can set it, and setting it to that value23* (which is bigger than what appears to be the Solaris default of 8192)24* reduces the number of packet drops.25*/26#define CHUNKSIZE 655362728/*29* Size of the buffer to allocate for packet data we read; it must be30* large enough to hold a chunk.31*/32#define PKTBUFSIZE CHUNKSIZE3334#else /* HAVE_SYS_BUFMOD_H */3536/*37* Size of the buffer to allocate for packet data we read; this is38* what the value used to be - there's no particular reason why it39* should be tied to MAXDLBUF, but we'll leave it as this for now.40*/41#define MAXDLBUF 819242#define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))4344#endif4546#include <sys/types.h>47#include <sys/time.h>48#ifdef HAVE_SYS_BUFMOD_H49#include <sys/bufmod.h>50#endif51#include <sys/dlpi.h>52#include <sys/stream.h>5354#include <errno.h>55#include <memory.h>56#include <stdio.h>57#include <stdlib.h>58#include <string.h>59#include <stropts.h>60#include <unistd.h>6162#ifdef HAVE_LIBDLPI63#include <libdlpi.h>64#endif6566#include "pcap-int.h"67#include "dlpisubs.h"6869#ifdef HAVE_SYS_BUFMOD_H70static void pcap_stream_err(const char *, int, char *);71#endif7273/*74* Get the packet statistics.75*/76int77pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)78{79struct pcap_dlpi *pd = p->priv;8081/*82* "ps_recv" counts packets handed to the filter, not packets83* that passed the filter. As filtering is done in userland,84* this would not include packets dropped because we ran out85* of buffer space; in order to make this more like other86* platforms (Linux 2.4 and later, BSDs with BPF), where the87* "packets received" count includes packets received but dropped88* due to running out of buffer space, and to keep from confusing89* applications that, for example, compute packet drop percentages,90* we also make it count packets dropped by "bufmod" (otherwise we91* might run the risk of the packet drop count being bigger than92* the received-packet count).93*94* "ps_drop" counts packets dropped by "bufmod" because of95* flow control requirements or resource exhaustion; it doesn't96* count packets dropped by the interface driver, or packets97* dropped upstream. As filtering is done in userland, it counts98* packets regardless of whether they would've passed the filter.99*100* These statistics don't include packets not yet read from101* the kernel by libpcap, but they may include packets not102* yet read from libpcap by the application.103*/104*ps = pd->stat;105106/*107* Add in the drop count, as per the above comment.108*/109ps->ps_recv += ps->ps_drop;110return (0);111}112113/*114* Does the processor for which we're compiling this support aligned loads?115*/116#if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \117(defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \118(defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \119(defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \120(defined(__s390__) || defined(__s390x__) || defined(__zarch__))121/* Yes, it does. */122#else123/* No, it doesn't. */124#define REQUIRE_ALIGNMENT125#endif126127/*128* Loop through the packets and call the callback for each packet.129* Return the number of packets read.130*/131int132pcap_process_pkts(pcap_t *p, pcap_handler callback, u_char *user,133int count, u_char *bufp, int len)134{135struct pcap_dlpi *pd = p->priv;136int n, caplen, origlen;137u_char *ep, *pk;138struct pcap_pkthdr pkthdr;139#ifdef HAVE_SYS_BUFMOD_H140struct sb_hdr *sbp;141#ifdef REQUIRE_ALIGNMENT142struct sb_hdr sbhdr;143#endif144#endif145146/*147* Loop through packets.148*149* This assumes that a single buffer of packets will have150* <= INT_MAX packets, so the packet count doesn't overflow.151*/152ep = bufp + len;153n = 0;154155#ifdef HAVE_SYS_BUFMOD_H156while (bufp < ep) {157/*158* Has "pcap_breakloop()" been called?159* If so, return immediately - if we haven't read any160* packets, clear the flag and return -2 to indicate161* that we were told to break out of the loop, otherwise162* leave the flag set, so that the *next* call will break163* out of the loop without having read any packets, and164* return the number of packets we've processed so far.165*/166if (p->break_loop) {167if (n == 0) {168p->break_loop = 0;169return (-2);170} else {171p->bp = bufp;172p->cc = ep - bufp;173return (n);174}175}176#ifdef REQUIRE_ALIGNMENT177if ((long)bufp & 3) {178sbp = &sbhdr;179memcpy(sbp, bufp, sizeof(*sbp));180} else181#endif182sbp = (struct sb_hdr *)bufp;183pd->stat.ps_drop = sbp->sbh_drops;184pk = bufp + sizeof(*sbp);185bufp += sbp->sbh_totlen;186origlen = sbp->sbh_origlen;187caplen = sbp->sbh_msglen;188#else189origlen = len;190caplen = min(p->snapshot, len);191pk = bufp;192bufp += caplen;193#endif194++pd->stat.ps_recv;195if (pcapint_filter(p->fcode.bf_insns, pk, origlen, caplen)) {196#ifdef HAVE_SYS_BUFMOD_H197pkthdr.ts.tv_sec = sbp->sbh_timestamp.tv_sec;198pkthdr.ts.tv_usec = sbp->sbh_timestamp.tv_usec;199#else200(void) gettimeofday(&pkthdr.ts, NULL);201#endif202pkthdr.len = origlen;203pkthdr.caplen = caplen;204/* Insure caplen does not exceed snapshot */205if (pkthdr.caplen > (bpf_u_int32)p->snapshot)206pkthdr.caplen = (bpf_u_int32)p->snapshot;207(*callback)(user, &pkthdr, pk);208if (++n >= count && !PACKET_COUNT_IS_UNLIMITED(count)) {209p->cc = ep - bufp;210p->bp = bufp;211return (n);212}213}214#ifdef HAVE_SYS_BUFMOD_H215}216#endif217p->cc = 0;218return (n);219}220221/*222* Process the mac type. Returns -1 if no matching mac type found, otherwise 0.223*/224int225pcap_process_mactype(pcap_t *p, u_int mactype)226{227int retv = 0;228229switch (mactype) {230231case DL_CSMACD:232case DL_ETHER:233p->linktype = DLT_EN10MB;234p->offset = 2;235/*236* This is (presumably) a real Ethernet capture; give it a237* link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so238* that an application can let you choose it, in case you're239* capturing DOCSIS traffic that a Cisco Cable Modem240* Termination System is putting out onto an Ethernet (it241* doesn't put an Ethernet header onto the wire, it puts raw242* DOCSIS frames out on the wire inside the low-level243* Ethernet framing).244*/245p->dlt_list = (u_int *)malloc(sizeof(u_int) * 2);246if (p->dlt_list == NULL) {247pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,248errno, "malloc");249return (-1);250}251p->dlt_list[0] = DLT_EN10MB;252p->dlt_list[1] = DLT_DOCSIS;253p->dlt_count = 2;254break;255256case DL_FDDI:257p->linktype = DLT_FDDI;258p->offset = 3;259break;260261case DL_TPR:262/* XXX - what about DL_TPB? Is that Token Bus? */263p->linktype = DLT_IEEE802;264p->offset = 2;265break;266267#ifdef HAVE_SOLARIS268case DL_IPATM:269p->linktype = DLT_SUNATM;270p->offset = 0; /* works for LANE and LLC encapsulation */271break;272#endif273274#ifdef DL_IPV4275case DL_IPV4:276p->linktype = DLT_IPV4;277p->offset = 0;278break;279#endif280281#ifdef DL_IPV6282case DL_IPV6:283p->linktype = DLT_IPV6;284p->offset = 0;285break;286#endif287288#ifdef DL_IPNET289case DL_IPNET:290/*291* XXX - DL_IPNET devices default to "raw IP" rather than292* "IPNET header"; see293*294* https://seclists.org/tcpdump/2009/q1/202295*296* We'd have to do DL_IOC_IPNET_INFO to enable getting297* the IPNET header.298*/299p->linktype = DLT_RAW;300p->offset = 0;301break;302#endif303304default:305snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown mactype 0x%x",306mactype);307retv = -1;308}309310return (retv);311}312313#ifdef HAVE_SYS_BUFMOD_H314/*315* Push and configure the buffer module. Returns -1 for error, otherwise 0.316*/317int318pcap_conf_bufmod(pcap_t *p, int snaplen)319{320struct timeval to;321bpf_u_int32 ss, chunksize;322323/* Non-standard call to get the data nicely buffered. */324if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {325pcap_stream_err("I_PUSH bufmod", errno, p->errbuf);326return (-1);327}328329ss = snaplen;330if (ss > 0 &&331strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {332pcap_stream_err("SBIOCSSNAP", errno, p->errbuf);333return (-1);334}335336if (p->opt.immediate) {337/* Set the timeout to zero, for immediate delivery. */338to.tv_sec = 0;339to.tv_usec = 0;340if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {341pcap_stream_err("SBIOCSTIME", errno, p->errbuf);342return (-1);343}344} else {345/* Set up the bufmod timeout. */346if (p->opt.timeout != 0) {347to.tv_sec = p->opt.timeout / 1000;348to.tv_usec = (p->opt.timeout * 1000) % 1000000;349if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {350pcap_stream_err("SBIOCSTIME", errno, p->errbuf);351return (-1);352}353}354355/* Set the chunk length. */356chunksize = CHUNKSIZE;357if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize)358!= 0) {359pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf);360return (-1);361}362}363364return (0);365}366#endif /* HAVE_SYS_BUFMOD_H */367368/*369* Allocate data buffer. Returns -1 if memory allocation fails, else 0.370*/371int372pcap_alloc_databuf(pcap_t *p)373{374p->bufsize = PKTBUFSIZE;375p->buffer = malloc(p->bufsize + p->offset);376if (p->buffer == NULL) {377pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,378errno, "malloc");379return (-1);380}381382return (0);383}384385/*386* Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise387* length of returned data on success.388*/389int390strioctl(int fd, int cmd, int len, char *dp)391{392struct strioctl str;393int retv;394395str.ic_cmd = cmd;396str.ic_timout = -1;397str.ic_len = len;398str.ic_dp = dp;399if ((retv = ioctl(fd, I_STR, &str)) < 0)400return (retv);401402return (str.ic_len);403}404405#ifdef HAVE_SYS_BUFMOD_H406/*407* Write stream error message to errbuf.408*/409static void410pcap_stream_err(const char *func, int err, char *errbuf)411{412pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, err, "%s", func);413}414#endif415416417