2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static const char rcsid[] _U_ =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.50.2.4 2004/03/21 08:33:23 guy Exp $ (LBL)";
30 #include <sys/types.h>
32 #include <sys/timeb.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/ip_var.h>
45 #include <netinet/udp.h>
46 #include <netinet/udp_var.h>
47 #include <netinet/tcp.h>
48 #include <netinet/tcpip.h>
56 #ifdef HAVE_OS_PROTO_H
61 * The chunk size for NIT. This is the amount of buffering
62 * done for read calls.
64 #define CHUNKSIZE (2*1024)
67 * The total buffer space used by NIT.
69 #define BUFSPACE (4*CHUNKSIZE)
72 static int nit_setflags(int, int, int, char *);
75 pcap_stats_nit(pcap_t *p, struct pcap_stat *ps)
79 * "ps_recv" counts packets handed to the filter, not packets
80 * that passed the filter. As filtering is done in userland,
81 * this does not include packets dropped because we ran out
84 * "ps_drop" presumably counts packets dropped by the socket
85 * because of flow control requirements or resource exhaustion;
86 * it doesn't count packets dropped by the interface driver.
87 * As filtering is done in userland, it counts packets regardless
88 * of whether they would've passed the filter.
90 * These statistics don't include packets not yet read from the
91 * kernel by libpcap or packets not yet read from libpcap by the
99 pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
102 register struct bpf_insn *fcode = p->fcode.bf_insns;
103 register u_char *bp, *cp, *ep;
104 register struct nit_hdr *nh;
109 cc = read(p->fd, (char *)p->buffer, p->bufsize);
111 if (errno == EWOULDBLOCK)
113 snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
114 pcap_strerror(errno));
122 * Loop through each packet. The increment expression
123 * rounds up to the next int boundary past the end of
124 * the previous packet.
130 * Has "pcap_breakloop()" been called?
131 * If so, return immediately - if we haven't read any
132 * packets, clear the flag and return -2 to indicate
133 * that we were told to break out of the loop, otherwise
134 * leave the flag set, so that the *next* call will break
135 * out of the loop without having read any packets, and
136 * return the number of packets we've processed so far.
149 nh = (struct nit_hdr *)bp;
150 cp = bp + sizeof(*nh);
152 switch (nh->nh_state) {
160 p->md.stat.ps_drop = nh->nh_dropped;
167 snprintf(p->errbuf, sizeof(p->errbuf),
168 "bad nit state %d", nh->nh_state);
171 ++p->md.stat.ps_recv;
172 bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
173 sizeof(int) - 1) & ~(sizeof(int) - 1));
175 caplen = nh->nh_wirelen;
176 if (caplen > p->snapshot)
177 caplen = p->snapshot;
178 if (bpf_filter(fcode, cp, nh->nh_wirelen, caplen)) {
179 struct pcap_pkthdr h;
180 h.ts = nh->nh_timestamp;
181 h.len = nh->nh_wirelen;
183 (*callback)(user, &h, cp);
184 if (++n >= cnt && cnt >= 0) {
196 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
200 memset(&nioc, 0, sizeof(nioc));
201 nioc.nioc_bufspace = BUFSPACE;
202 nioc.nioc_chunksize = CHUNKSIZE;
203 nioc.nioc_typetomatch = NT_ALLTYPES;
204 nioc.nioc_snaplen = p->snapshot;
205 nioc.nioc_bufalign = sizeof(int);
206 nioc.nioc_bufoffset = 0;
209 nioc.nioc_flags |= NF_TIMEOUT;
210 nioc.nioc_timeout.tv_sec = to_ms / 1000;
211 nioc.nioc_timeout.tv_usec = (to_ms * 1000) % 1000000;
214 nioc.nioc_flags |= NF_PROMISC;
216 if (ioctl(fd, SIOCSNIT, &nioc) < 0) {
217 snprintf(ebuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s",
218 pcap_strerror(errno));
225 pcap_close_nit(pcap_t *p)
227 if (p->buffer != NULL)
234 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
238 struct sockaddr_nit snit;
241 p = (pcap_t *)malloc(sizeof(*p));
243 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
249 * NIT requires a snapshot length of at least 96.
253 memset(p, 0, sizeof(*p));
254 p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
256 snprintf(ebuf, PCAP_ERRBUF_SIZE,
257 "socket: %s", pcap_strerror(errno));
260 snit.snit_family = AF_NIT;
261 (void)strncpy(snit.snit_ifname, device, NITIFSIZ);
263 if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
264 snprintf(ebuf, PCAP_ERRBUF_SIZE,
265 "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno));
268 p->snapshot = snaplen;
269 nit_setflags(p->fd, promisc, to_ms, ebuf);
272 * NIT supports only ethernets.
274 p->linktype = DLT_EN10MB;
276 p->bufsize = BUFSPACE;
277 p->buffer = (u_char *)malloc(p->bufsize);
278 if (p->buffer == NULL) {
279 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
284 * "p->fd" is a socket, so "select()" should work on it.
286 p->selectable_fd = p->fd;
288 p->read_op = pcap_read_nit;
289 p->setfilter_op = install_bpf_program; /* no kernel filtering */
290 p->set_datalink_op = NULL; /* can't change data link type */
291 p->getnonblock_op = pcap_getnonblock_fd;
292 p->setnonblock_op = pcap_setnonblock_fd;
293 p->stats_op = pcap_stats_nit;
294 p->close_op = pcap_close_nit;
305 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)