]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-snit.c
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.
21 * Modifications made to accommodate the new SunOS4.0 NIT facility by
22 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
23 * This module now handles the STREAMS based NIT.
27 static const char rcsid
[] =
28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.65 2003-07-25 05:32:05 guy Exp $ (LBL)";
35 #include <sys/types.h>
37 #include <sys/timeb.h>
39 #include <sys/fcntlcom.h>
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 #include <sys/stropts.h>
47 #include <net/nit_if.h>
48 #include <net/nit_pf.h>
49 #include <net/nit_buf.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
58 #include <netinet/tcp.h>
59 #include <netinet/tcpip.h>
69 #ifdef HAVE_OS_PROTO_H
74 * The chunk size for NIT. This is the amount of buffering
75 * done for read calls.
77 #define CHUNKSIZE (2*1024)
80 * The total buffer space used by NIT.
82 #define BUFSPACE (4*CHUNKSIZE)
85 static int nit_setflags(int, int, int, char *);
88 pcap_stats_snit(pcap_t
*p
, struct pcap_stat
*ps
)
92 * "ps_recv" counts packets handed to the filter, not packets
93 * that passed the filter. As filtering is done in userland,
94 * this does not include packets dropped because we ran out
97 * "ps_drop" counts packets dropped inside the "/dev/nit"
98 * device because of flow control requirements or resource
99 * exhaustion; it doesn't count packets dropped by the
100 * interface driver, or packets dropped upstream. As filtering
101 * is done in userland, it counts packets regardless of whether
102 * they would've passed the filter.
104 * These statistics don't include packets not yet read from the
105 * kernel by libpcap or packets not yet read from libpcap by the
113 pcap_read_snit(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
116 register struct bpf_insn
*fcode
= p
->fcode
.bf_insns
;
117 register u_char
*bp
, *cp
, *ep
;
118 register struct nit_bufhdr
*hdrp
;
119 register struct nit_iftime
*ntp
;
120 register struct nit_iflen
*nlp
;
121 register struct nit_ifdrops
*ndp
;
126 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
128 if (errno
== EWOULDBLOCK
)
130 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
131 pcap_strerror(errno
));
139 * loop through each snapshot in the chunk
144 ++p
->md
.stat
.ps_recv
;
147 /* get past NIT buffer */
148 hdrp
= (struct nit_bufhdr
*)cp
;
151 /* get past NIT timer */
152 ntp
= (struct nit_iftime
*)cp
;
155 ndp
= (struct nit_ifdrops
*)cp
;
156 p
->md
.stat
.ps_drop
= ndp
->nh_drops
;
159 /* get past packet len */
160 nlp
= (struct nit_iflen
*)cp
;
164 bp
+= hdrp
->nhb_totlen
;
166 caplen
= nlp
->nh_pktlen
;
167 if (caplen
> p
->snapshot
)
168 caplen
= p
->snapshot
;
170 if (bpf_filter(fcode
, cp
, nlp
->nh_pktlen
, caplen
)) {
171 struct pcap_pkthdr h
;
172 h
.ts
= ntp
->nh_timestamp
;
173 h
.len
= nlp
->nh_pktlen
;
175 (*callback
)(user
, &h
, cp
);
176 if (++n
>= cnt
&& cnt
>= 0) {
188 nit_setflags(int fd
, int promisc
, int to_ms
, char *ebuf
)
192 struct timeval timeout
;
194 si
.ic_timout
= INFTIM
;
196 timeout
.tv_sec
= to_ms
/ 1000;
197 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
198 si
.ic_cmd
= NIOCSTIME
;
199 si
.ic_len
= sizeof(timeout
);
200 si
.ic_dp
= (char *)&timeout
;
201 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
202 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSTIME: %s",
203 pcap_strerror(errno
));
207 flags
= NI_TIMESTAMP
| NI_LEN
| NI_DROPS
;
210 si
.ic_cmd
= NIOCSFLAGS
;
211 si
.ic_len
= sizeof(flags
);
212 si
.ic_dp
= (char *)&flags
;
213 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
214 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSFLAGS: %s",
215 pcap_strerror(errno
));
222 pcap_close_snit(pcap_t
*p
)
224 if (p
->buffer
!= NULL
)
231 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
234 struct strioctl si
; /* struct for ioctl() */
235 struct ifreq ifr
; /* interface request struct */
236 int chunksize
= CHUNKSIZE
;
238 static char dev
[] = "/dev/nit";
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
= open(dev
, O_RDONLY
);
256 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dev
,
257 pcap_strerror(errno
));
261 /* arrange to get discrete messages from the STREAM and use NIT_BUF */
262 if (ioctl(fd
, I_SRDOPT
, (char *)RMSGD
) < 0) {
263 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "I_SRDOPT: %s",
264 pcap_strerror(errno
));
267 if (ioctl(fd
, I_PUSH
, "nbuf") < 0) {
268 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "push nbuf: %s",
269 pcap_strerror(errno
));
272 /* set the chunksize */
273 si
.ic_cmd
= NIOCSCHUNK
;
274 si
.ic_timout
= INFTIM
;
275 si
.ic_len
= sizeof(chunksize
);
276 si
.ic_dp
= (char *)&chunksize
;
277 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
278 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSCHUNK: %s",
279 pcap_strerror(errno
));
283 /* request the interface */
284 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
285 ifr
.ifr_name
[sizeof(ifr
.ifr_name
) - 1] = '\0';
286 si
.ic_cmd
= NIOCBIND
;
287 si
.ic_len
= sizeof(ifr
);
288 si
.ic_dp
= (char *)&ifr
;
289 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
290 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCBIND: %s: %s",
291 ifr
.ifr_name
, pcap_strerror(errno
));
295 /* set the snapshot length */
296 si
.ic_cmd
= NIOCSSNAP
;
297 si
.ic_len
= sizeof(snaplen
);
298 si
.ic_dp
= (char *)&snaplen
;
299 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
300 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSSNAP: %s",
301 pcap_strerror(errno
));
304 p
->snapshot
= snaplen
;
305 if (nit_setflags(p
->fd
, promisc
, to_ms
, ebuf
) < 0)
308 (void)ioctl(fd
, I_FLUSH
, (char *)FLUSHR
);
310 * NIT supports only ethernets.
312 p
->linktype
= DLT_EN10MB
;
314 p
->bufsize
= BUFSPACE
;
315 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
316 if (p
->buffer
== NULL
) {
317 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
321 p
->read_op
= pcap_read_snit
;
322 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
323 p
->set_datalink_op
= NULL
; /* can't change data link type */
324 p
->stats_op
= pcap_stats_snit
;
325 p
->close_op
= pcap_close_snit
;
336 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)