pcap-snit.c revision 146768
16059Samurai/* 26059Samurai * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 36059Samurai * The Regents of the University of California. All rights reserved. 46059Samurai * 56059Samurai * Redistribution and use in source and binary forms, with or without 66059Samurai * modification, are permitted provided that: (1) source code distributions 76059Samurai * retain the above copyright notice and this paragraph in its entirety, (2) 86059Samurai * distributions including binary code include the above copyright notice and 96059Samurai * this paragraph in its entirety in the documentation or other materials 106059Samurai * provided with the distribution, and (3) all advertising materials mentioning 116059Samurai * features or use of this software display the following acknowledgement: 126059Samurai * ``This product includes software developed by the University of California, 136059Samurai * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 146059Samurai * the University nor the names of its contributors may be used to endorse 156059Samurai * or promote products derived from this software without specific prior 166059Samurai * written permission. 176059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 186059Samurai * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 196059Samurai * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2041654Sbrian * 218857Srgrimes * Modifications made to accommodate the new SunOS4.0 NIT facility by 226059Samurai * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989. 236059Samurai * This module now handles the STREAMS based NIT. 2436285Sbrian */ 2536452Sbrian 2630715Sbrian#ifndef lint 2730715Sbrianstatic const char rcsid[] _U_ = 2830715Sbrian "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.72 2004/10/19 07:06:13 guy Exp $ (LBL)"; 2936285Sbrian#endif 3030715Sbrian 3130715Sbrian#ifdef HAVE_CONFIG_H 326059Samurai#include "config.h" 3311336Samurai#endif 3430715Sbrian 3530715Sbrian#include <sys/types.h> 3630715Sbrian#include <sys/time.h> 376059Samurai#include <sys/timeb.h> 386059Samurai#include <sys/dir.h> 3918786Sjkh#include <sys/fcntlcom.h> 4030715Sbrian#include <sys/file.h> 4139395Sbrian#include <sys/ioctl.h> 4239395Sbrian#include <sys/socket.h> 4339395Sbrian#include <sys/stropts.h> 4439395Sbrian 4539395Sbrian#include <net/if.h> 4639395Sbrian#include <net/nit.h> 4739395Sbrian#include <net/nit_if.h> 4837141Sbrian#include <net/nit_pf.h> 4930715Sbrian#include <net/nit_buf.h> 5030715Sbrian 5130715Sbrian#include <netinet/in.h> 5231061Sbrian#include <netinet/in_systm.h> 5330715Sbrian#include <netinet/ip.h> 5430715Sbrian#include <netinet/if_ether.h> 5536285Sbrian#include <netinet/ip_var.h> 566059Samurai#include <netinet/udp.h> 5731514Sbrian#include <netinet/udp_var.h> 5813389Sphk#include <netinet/tcp.h> 5936285Sbrian#include <netinet/tcpip.h> 6036285Sbrian 6136285Sbrian#include <ctype.h> 626059Samurai#include <errno.h> 6336285Sbrian#include <stdio.h> 6436285Sbrian#include <string.h> 6536285Sbrian#include <unistd.h> 6636285Sbrian 6736285Sbrian#include "pcap-int.h" 686735Samurai 6913389Sphk#ifdef HAVE_OS_PROTO_H 7023840Sbrian#include "os-proto.h" 7130715Sbrian#endif 7236285Sbrian 7336285Sbrian/* 7436285Sbrian * The chunk size for NIT. This is the amount of buffering 7536285Sbrian * done for read calls. 7638174Sbrian */ 7736285Sbrian#define CHUNKSIZE (2*1024) 7840561Sbrian 796059Samurai/* 806735Samurai * The total buffer space used by NIT. 816735Samurai */ 826735Samurai#define BUFSPACE (4*CHUNKSIZE) 836735Samurai 846735Samurai/* Forwards */ 856735Samuraistatic int nit_setflags(int, int, int, char *); 8636431Sbrian 8730715Sbrianstatic int 8831343Sbrianpcap_stats_snit(pcap_t *p, struct pcap_stat *ps) 8930715Sbrian{ 9036285Sbrian 9136285Sbrian /* 926059Samurai * "ps_recv" counts packets handed to the filter, not packets 9310528Samurai * that passed the filter. As filtering is done in userland, 9436285Sbrian * this does not include packets dropped because we ran out 956059Samurai * of buffer space. 9636285Sbrian * 9738008Sbrian * "ps_drop" counts packets dropped inside the "/dev/nit" 986059Samurai * device because of flow control requirements or resource 996059Samurai * exhaustion; it doesn't count packets dropped by the 1006059Samurai * interface driver, or packets dropped upstream. As filtering 10136285Sbrian * is done in userland, it counts packets regardless of whether 1026059Samurai * they would've passed the filter. 10336285Sbrian * 10436285Sbrian * These statistics don't include packets not yet read from the 10537007Sbrian * kernel by libpcap or packets not yet read from libpcap by the 10636285Sbrian * application. 10736285Sbrian */ 1086059Samurai *ps = p->md.stat; 1096059Samurai return (0); 1106059Samurai} 1116059Samurai 11228679Sbrianstatic int 1136059Samuraipcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 11426858Sbrian{ 11536285Sbrian register int cc, n; 11636285Sbrian register struct bpf_insn *fcode = p->fcode.bf_insns; 11737018Sbrian register u_char *bp, *cp, *ep; 11836285Sbrian register struct nit_bufhdr *hdrp; 1196059Samurai register struct nit_iftime *ntp; 1206059Samurai register struct nit_iflen *nlp; 1216059Samurai register struct nit_ifdrops *ndp; 12228679Sbrian register int caplen; 1236059Samurai 12436285Sbrian cc = p->cc; 12528679Sbrian if (cc == 0) { 1266059Samurai cc = read(p->fd, (char *)p->buffer, p->bufsize); 1276059Samurai if (cc < 0) { 12836285Sbrian if (errno == EWOULDBLOCK) 12936285Sbrian return (0); 13010528Samurai snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", 13136285Sbrian pcap_strerror(errno)); 13210528Samurai return (-1); 13336285Sbrian } 13436285Sbrian bp = p->buffer; 13510528Samurai } else 13610528Samurai bp = p->bp; 13710528Samurai 13836285Sbrian /* 13910528Samurai * loop through each snapshot in the chunk 14036285Sbrian */ 14136285Sbrian n = 0; 14210528Samurai ep = bp + cc; 14310528Samurai while (bp < ep) { 14426940Sbrian /* 14536285Sbrian * Has "pcap_breakloop()" been called? 14626940Sbrian * If so, return immediately - if we haven't read any 14736285Sbrian * packets, clear the flag and return -2 to indicate 14836285Sbrian * that we were told to break out of the loop, otherwise 14936285Sbrian * leave the flag set, so that the *next* call will break 15026940Sbrian * out of the loop without having read any packets, and 15126940Sbrian * return the number of packets we've processed so far. 15231081Sbrian */ 15331081Sbrian if (p->break_loop) { 15431081Sbrian if (n == 0) { 15536285Sbrian p->break_loop = 0; 15636285Sbrian return (-2); 15731081Sbrian } else { 15831081Sbrian p->bp = bp; 15931343Sbrian p->cc = ep - bp; 16025908Sbrian return (n); 16125908Sbrian } 16237010Sbrian } 16331343Sbrian 16431343Sbrian ++p->md.stat.ps_recv; 16531343Sbrian cp = bp; 16631343Sbrian 16710528Samurai /* get past NIT buffer */ 16831962Sbrian hdrp = (struct nit_bufhdr *)cp; 16925908Sbrian cp += sizeof(*hdrp); 17025908Sbrian 17125908Sbrian /* get past NIT timer */ 17225908Sbrian ntp = (struct nit_iftime *)cp; 17325908Sbrian cp += sizeof(*ntp); 17430715Sbrian 17531343Sbrian ndp = (struct nit_ifdrops *)cp; 1766059Samurai p->md.stat.ps_drop = ndp->nh_drops; 17720120Snate cp += sizeof *ndp; 17831343Sbrian 17931343Sbrian /* get past packet len */ 18031343Sbrian nlp = (struct nit_iflen *)cp; 18131343Sbrian cp += sizeof(*nlp); 18240797Sbrian 1836059Samurai /* next snapshot */ 1846059Samurai bp += hdrp->nhb_totlen; 1856059Samurai 18640797Sbrian caplen = nlp->nh_pktlen; 18737191Sbrian if (caplen > p->snapshot) 1886059Samurai caplen = p->snapshot; 18940797Sbrian 1906059Samurai if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) { 1916059Samurai struct pcap_pkthdr h; 19240797Sbrian h.ts = ntp->nh_timestamp; 19336465Sbrian h.len = nlp->nh_pktlen; 19437191Sbrian h.caplen = caplen; 19540797Sbrian (*callback)(user, &h, cp); 19640797Sbrian if (++n >= cnt && cnt >= 0) { 19736465Sbrian p->cc = ep - bp; 19836465Sbrian p->bp = bp; 19936465Sbrian return (n); 20036465Sbrian } 20137191Sbrian } 20240797Sbrian } 20337191Sbrian p->cc = 0; 20437191Sbrian return (n); 20536285Sbrian} 20636465Sbrian 20736465Sbrianstatic int 20836465Sbrianpcap_inject_snit(pcap_t *p, const void *buf, size_t size) 20936465Sbrian{ 21036465Sbrian struct strbuf ctl, data; 21136465Sbrian 21236465Sbrian /* 21336465Sbrian * XXX - can we just do 21436465Sbrian * 21536465Sbrian ret = write(pd->f, buf, size); 21636465Sbrian */ 21736465Sbrian ctl.len = sizeof(*sa); /* XXX - what was this? */ 2186059Samurai ctl.buf = (char *)sa; 21936465Sbrian data.buf = buf; 2206059Samurai data.len = size; 22136285Sbrian ret = putmsg(p->fd, &ctl, &data); 2226059Samurai if (ret == -1) { 2236059Samurai snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 22431197Sbrian pcap_strerror(errno)); 22540797Sbrian return (-1); 22640797Sbrian } 22736285Sbrian return (ret); 22836285Sbrian} 22936285Sbrian 23040797Sbrianstatic int 2316059Samurainit_setflags(int fd, int promisc, int to_ms, char *ebuf) 2326059Samurai{ 23340797Sbrian bpf_u_int32 flags; 23440797Sbrian struct strioctl si; 23540797Sbrian struct timeval timeout; 23640797Sbrian 23740797Sbrian si.ic_timout = INFTIM; 23840797Sbrian if (to_ms != 0) { 23940797Sbrian timeout.tv_sec = to_ms / 1000; 24040797Sbrian timeout.tv_usec = (to_ms * 1000) % 1000000; 24140797Sbrian si.ic_cmd = NIOCSTIME; 24240797Sbrian si.ic_len = sizeof(timeout); 24340797Sbrian si.ic_dp = (char *)&timeout; 24440797Sbrian if (ioctl(fd, I_STR, (char *)&si) < 0) { 24540797Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s", 24640797Sbrian pcap_strerror(errno)); 24740797Sbrian return (-1); 24840797Sbrian } 24940797Sbrian } 25026940Sbrian flags = NI_TIMESTAMP | NI_LEN | NI_DROPS; 25128679Sbrian if (promisc) 2526059Samurai flags |= NI_PROMISC; 25340797Sbrian si.ic_cmd = NIOCSFLAGS; 25440797Sbrian si.ic_len = sizeof(flags); 25536285Sbrian si.ic_dp = (char *)&flags; 25636285Sbrian if (ioctl(fd, I_STR, (char *)&si) < 0) { 25726516Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s", 25831823Sbrian pcap_strerror(errno)); 25931823Sbrian return (-1); 26031823Sbrian } 26131823Sbrian return (0); 26231823Sbrian} 26331823Sbrian 26431823Sbrianpcap_t * 26531823Sbrianpcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 26631823Sbrian char *ebuf) 26731823Sbrian{ 26830715Sbrian struct strioctl si; /* struct for ioctl() */ 26936285Sbrian struct ifreq ifr; /* interface request struct */ 27026516Sbrian int chunksize = CHUNKSIZE; 27138198Sbrian int fd; 27238198Sbrian static char dev[] = "/dev/nit"; 27338198Sbrian register pcap_t *p; 27440797Sbrian 27531121Sbrian p = (pcap_t *)malloc(sizeof(*p)); 27636285Sbrian if (p == NULL) { 27736285Sbrian strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 27836285Sbrian return (NULL); 27936285Sbrian } 28036285Sbrian 28136285Sbrian if (snaplen < 96) 28236285Sbrian /* 28336285Sbrian * NIT requires a snapshot length of at least 96. 28436465Sbrian */ 28536285Sbrian snaplen = 96; 28636285Sbrian 28736285Sbrian memset(p, 0, sizeof(*p)); 28836285Sbrian /* 28936285Sbrian * Initially try a read/write open (to allow the inject 29036285Sbrian * method to work). If that fails due to permission 29136285Sbrian * issues, fall back to read-only. This allows a 29236285Sbrian * non-root user to be granted specific access to pcap 29336285Sbrian * capabilities via file permissions. 29436285Sbrian * 29536285Sbrian * XXX - we should have an API that has a flag that 29636285Sbrian * controls whether to open read-only or read-write, 29736285Sbrian * so that denial of permission to send (or inability 29836465Sbrian * to send, if sending packets isn't supported on 29936285Sbrian * the device in question) can be indicated at open 30036285Sbrian * time. 30131121Sbrian */ 30231158Sbrian p->fd = fd = open(dev, O_RDWR); 30331158Sbrian if (fd < 0 && errno == EACCES) 30431158Sbrian p->fd = fd = open(dev, O_RDONLY); 30531158Sbrian if (fd < 0) { 30631158Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev, 30731158Sbrian pcap_strerror(errno)); 30837019Sbrian goto bad; 30937019Sbrian } 31031158Sbrian 31131158Sbrian /* arrange to get discrete messages from the STREAM and use NIT_BUF */ 31231158Sbrian if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { 31331158Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s", 31431158Sbrian pcap_strerror(errno)); 31531158Sbrian goto bad; 31631158Sbrian } 31731158Sbrian if (ioctl(fd, I_PUSH, "nbuf") < 0) { 31840797Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s", 31940797Sbrian pcap_strerror(errno)); 32040797Sbrian goto bad; 32140797Sbrian } 32240797Sbrian /* set the chunksize */ 32331121Sbrian si.ic_cmd = NIOCSCHUNK; 32436467Sbrian si.ic_timout = INFTIM; 32536285Sbrian si.ic_len = sizeof(chunksize); 32626940Sbrian si.ic_dp = (char *)&chunksize; 3276059Samurai if (ioctl(fd, I_STR, (char *)&si) < 0) { 32836314Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", 32936314Sbrian pcap_strerror(errno)); 33040561Sbrian goto bad; 33136314Sbrian } 33236285Sbrian 33337191Sbrian /* request the interface */ 33440561Sbrian strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 33540561Sbrian ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; 33631121Sbrian si.ic_cmd = NIOCBIND; 33737008Sbrian si.ic_len = sizeof(ifr); 33836285Sbrian si.ic_dp = (char *)𝔦 33936285Sbrian if (ioctl(fd, I_STR, (char *)&si) < 0) { 34036285Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s", 34136285Sbrian ifr.ifr_name, pcap_strerror(errno)); 34236285Sbrian goto bad; 34336285Sbrian } 34436285Sbrian 34524753Sache /* set the snapshot length */ 3466059Samurai si.ic_cmd = NIOCSSNAP; 34736465Sbrian si.ic_len = sizeof(snaplen); 34836285Sbrian si.ic_dp = (char *)&snaplen; 34936285Sbrian if (ioctl(fd, I_STR, (char *)&si) < 0) { 35036285Sbrian snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s", 35136285Sbrian pcap_strerror(errno)); 35240797Sbrian goto bad; 35340797Sbrian } 35440797Sbrian p->snapshot = snaplen; 35540797Sbrian if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0) 3566059Samurai goto bad; 35726940Sbrian 35840797Sbrian (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); 35940797Sbrian /* 36040797Sbrian * NIT supports only ethernets. 36140797Sbrian */ 36240797Sbrian p->linktype = DLT_EN10MB; 36340797Sbrian 36440797Sbrian p->bufsize = BUFSPACE; 36540797Sbrian p->buffer = (u_char *)malloc(p->bufsize); 36640797Sbrian if (p->buffer == NULL) { 36740797Sbrian strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 36840797Sbrian goto bad; 36936465Sbrian } 37036285Sbrian 37136285Sbrian /* 37236285Sbrian * "p->fd" is an FD for a STREAMS device, so "select()" and 37336285Sbrian * "poll()" should work on it. 37436465Sbrian */ 37536285Sbrian p->selectable_fd = p->fd; 37636285Sbrian 37720813Sjkh /* 3786059Samurai * This is (presumably) a real Ethernet capture; give it a 37928679Sbrian * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 38020813Sjkh * that an application can let you choose it, in case you're 38136285Sbrian * capturing DOCSIS traffic that a Cisco Cable Modem 38236285Sbrian * Termination System is putting out onto an Ethernet (it 38320813Sjkh * doesn't put an Ethernet header onto the wire, it puts raw 38436285Sbrian * DOCSIS frames out on the wire inside the low-level 38520813Sjkh * Ethernet framing). 38620813Sjkh */ 38711336Samurai p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 38836465Sbrian /* 38936285Sbrian * If that fails, just leave the list empty. 39020813Sjkh */ 39136285Sbrian if (p->dlt_list != NULL) { 39236285Sbrian p->dlt_list[0] = DLT_EN10MB; 39336285Sbrian p->dlt_list[1] = DLT_DOCSIS; 39436285Sbrian p->dlt_count = 2; 39536285Sbrian } 39636285Sbrian 39736285Sbrian p->read_op = pcap_read_snit; 39836285Sbrian p->inject_op = pcap_inject_snit; 39936285Sbrian p->setfilter_op = install_bpf_program; /* no kernel filtering */ 40036285Sbrian p->set_datalink_op = NULL; /* can't change data link type */ 40125908Sbrian p->getnonblock_op = pcap_getnonblock_fd; 40236285Sbrian p->setnonblock_op = pcap_setnonblock_fd; 40336285Sbrian p->stats_op = pcap_stats_snit; 40425908Sbrian p->close_op = pcap_close_common; 40536285Sbrian 40636285Sbrian return (p); 40728679Sbrian bad: 40828679Sbrian if (fd >= 0) 40936285Sbrian close(fd); 41020813Sjkh free(p); 41128679Sbrian return (NULL); 41236465Sbrian} 41336285Sbrian 41436285Sbrianint 41536285Sbrianpcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 41620813Sjkh{ 41736709Sbrian return (0); 41836709Sbrian} 41936285Sbrian