]>
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
[] _U_
=
28 "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.77 2008-04-14 20:40:58 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 * Private data for capturing on STREAMS NIT devices.
91 struct pcap_stat stat
;
95 pcap_stats_snit(pcap_t
*p
, struct pcap_stat
*ps
)
97 struct pcap_snit
*psn
= p
->private;
100 * "ps_recv" counts packets handed to the filter, not packets
101 * that passed the filter. As filtering is done in userland,
102 * this does not include packets dropped because we ran out
105 * "ps_drop" counts packets dropped inside the "/dev/nit"
106 * device because of flow control requirements or resource
107 * exhaustion; it doesn't count packets dropped by the
108 * interface driver, or packets dropped upstream. As filtering
109 * is done in userland, it counts packets regardless of whether
110 * they would've passed the filter.
112 * These statistics don't include packets not yet read from the
113 * kernel by libpcap or packets not yet read from libpcap by the
121 pcap_read_snit(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
123 struct pcap_snit
*psn
= p
->private;
125 register u_char
*bp
, *cp
, *ep
;
126 register struct nit_bufhdr
*hdrp
;
127 register struct nit_iftime
*ntp
;
128 register struct nit_iflen
*nlp
;
129 register struct nit_ifdrops
*ndp
;
134 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
136 if (errno
== EWOULDBLOCK
)
138 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
139 pcap_strerror(errno
));
147 * loop through each snapshot in the chunk
153 * Has "pcap_breakloop()" been called?
154 * If so, return immediately - if we haven't read any
155 * packets, clear the flag and return -2 to indicate
156 * that we were told to break out of the loop, otherwise
157 * leave the flag set, so that the *next* call will break
158 * out of the loop without having read any packets, and
159 * return the number of packets we've processed so far.
175 /* get past NIT buffer */
176 hdrp
= (struct nit_bufhdr
*)cp
;
179 /* get past NIT timer */
180 ntp
= (struct nit_iftime
*)cp
;
183 ndp
= (struct nit_ifdrops
*)cp
;
184 psn
->stat
.ps_drop
= ndp
->nh_drops
;
187 /* get past packet len */
188 nlp
= (struct nit_iflen
*)cp
;
192 bp
+= hdrp
->nhb_totlen
;
194 caplen
= nlp
->nh_pktlen
;
195 if (caplen
> p
->snapshot
)
196 caplen
= p
->snapshot
;
198 if (bpf_filter(p
->fcode
.bf_insns
, cp
, nlp
->nh_pktlen
, caplen
)) {
199 struct pcap_pkthdr h
;
200 h
.ts
= ntp
->nh_timestamp
;
201 h
.len
= nlp
->nh_pktlen
;
203 (*callback
)(user
, &h
, cp
);
204 if (++n
>= cnt
&& cnt
> 0) {
216 pcap_inject_snit(pcap_t
*p
, const void *buf
, size_t size
)
218 struct strbuf ctl
, data
;
221 * XXX - can we just do
223 ret = write(pd->f, buf, size);
225 ctl
.len
= sizeof(*sa
); /* XXX - what was this? */
226 ctl
.buf
= (char *)sa
;
229 ret
= putmsg(p
->fd
, &ctl
, &data
);
231 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
232 pcap_strerror(errno
));
239 nit_setflags(int fd
, int promisc
, int to_ms
, char *ebuf
)
243 struct timeval timeout
;
245 si
.ic_timout
= INFTIM
;
247 timeout
.tv_sec
= to_ms
/ 1000;
248 timeout
.tv_usec
= (to_ms
* 1000) % 1000000;
249 si
.ic_cmd
= NIOCSTIME
;
250 si
.ic_len
= sizeof(timeout
);
251 si
.ic_dp
= (char *)&timeout
;
252 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
253 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSTIME: %s",
254 pcap_strerror(errno
));
258 flags
= NI_TIMESTAMP
| NI_LEN
| NI_DROPS
;
261 si
.ic_cmd
= NIOCSFLAGS
;
262 si
.ic_len
= sizeof(flags
);
263 si
.ic_dp
= (char *)&flags
;
264 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
265 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "NIOCSFLAGS: %s",
266 pcap_strerror(errno
));
273 pcap_activate_snit(pcap_t
*p
)
275 struct strioctl si
; /* struct for ioctl() */
276 struct ifreq ifr
; /* interface request struct */
277 int chunksize
= CHUNKSIZE
;
279 static char dev
[] = "/dev/nit";
283 * No monitor mode on SunOS 4.x (no Wi-Fi devices on
284 * hardware supported by SunOS 4.x).
286 return (PCAP_ERROR_RFMON_NOTSUP
);
289 if (p
->snapshot
< 96)
291 * NIT requires a snapshot length of at least 96.
296 * Initially try a read/write open (to allow the inject
297 * method to work). If that fails due to permission
298 * issues, fall back to read-only. This allows a
299 * non-root user to be granted specific access to pcap
300 * capabilities via file permissions.
302 * XXX - we should have an API that has a flag that
303 * controls whether to open read-only or read-write,
304 * so that denial of permission to send (or inability
305 * to send, if sending packets isn't supported on
306 * the device in question) can be indicated at open
309 p
->fd
= fd
= open(dev
, O_RDWR
);
310 if (fd
< 0 && errno
== EACCES
)
311 p
->fd
= fd
= open(dev
, O_RDONLY
);
313 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", dev
,
314 pcap_strerror(errno
));
318 /* arrange to get discrete messages from the STREAM and use NIT_BUF */
319 if (ioctl(fd
, I_SRDOPT
, (char *)RMSGD
) < 0) {
320 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "I_SRDOPT: %s",
321 pcap_strerror(errno
));
324 if (ioctl(fd
, I_PUSH
, "nbuf") < 0) {
325 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "push nbuf: %s",
326 pcap_strerror(errno
));
329 /* set the chunksize */
330 si
.ic_cmd
= NIOCSCHUNK
;
331 si
.ic_timout
= INFTIM
;
332 si
.ic_len
= sizeof(chunksize
);
333 si
.ic_dp
= (char *)&chunksize
;
334 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
335 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "NIOCSCHUNK: %s",
336 pcap_strerror(errno
));
340 /* request the interface */
341 strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
342 ifr
.ifr_name
[sizeof(ifr
.ifr_name
) - 1] = '\0';
343 si
.ic_cmd
= NIOCBIND
;
344 si
.ic_len
= sizeof(ifr
);
345 si
.ic_dp
= (char *)&ifr
;
346 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
347 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "NIOCBIND: %s: %s",
348 ifr
.ifr_name
, pcap_strerror(errno
));
352 /* set the snapshot length */
353 si
.ic_cmd
= NIOCSSNAP
;
354 si
.ic_len
= sizeof(p
->snapshot
);
355 si
.ic_dp
= (char *)&p
->snapshot
;
356 if (ioctl(fd
, I_STR
, (char *)&si
) < 0) {
357 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "NIOCSSNAP: %s",
358 pcap_strerror(errno
));
361 if (nit_setflags(p
->fd
, p
->opt
.promisc
, p
->opt
.timeout
, p
->errbuf
) < 0)
364 (void)ioctl(fd
, I_FLUSH
, (char *)FLUSHR
);
366 * NIT supports only ethernets.
368 p
->linktype
= DLT_EN10MB
;
370 p
->bufsize
= BUFSPACE
;
371 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
372 if (p
->buffer
== NULL
) {
373 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
378 * "p->fd" is an FD for a STREAMS device, so "select()" and
379 * "poll()" should work on it.
381 p
->selectable_fd
= p
->fd
;
384 * This is (presumably) a real Ethernet capture; give it a
385 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
386 * that an application can let you choose it, in case you're
387 * capturing DOCSIS traffic that a Cisco Cable Modem
388 * Termination System is putting out onto an Ethernet (it
389 * doesn't put an Ethernet header onto the wire, it puts raw
390 * DOCSIS frames out on the wire inside the low-level
393 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
395 * If that fails, just leave the list empty.
397 if (p
->dlt_list
!= NULL
) {
398 p
->dlt_list
[0] = DLT_EN10MB
;
399 p
->dlt_list
[1] = DLT_DOCSIS
;
403 p
->read_op
= pcap_read_snit
;
404 p
->inject_op
= pcap_inject_snit
;
405 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
406 p
->setdirection_op
= NULL
; /* Not implemented. */
407 p
->set_datalink_op
= NULL
; /* can't change data link type */
408 p
->getnonblock_op
= pcap_getnonblock_fd
;
409 p
->setnonblock_op
= pcap_setnonblock_fd
;
410 p
->stats_op
= pcap_stats_snit
;
414 pcap_cleanup_live_common(p
);
419 pcap_create_interface(const char *device
, char *ebuf
)
423 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_snit
));
427 p
->activate_op
= pcap_activate_snit
;
432 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)