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.
26 #include <sys/types.h>
28 #include <sys/timeb.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/if_ether.h>
40 #include <netinet/ip_var.h>
41 #include <netinet/udp.h>
42 #include <netinet/udp_var.h>
43 #include <netinet/tcp.h>
44 #include <netinet/tcpip.h>
52 #ifdef HAVE_OS_PROTO_H
57 * The chunk size for NIT. This is the amount of buffering
58 * done for read calls.
60 #define CHUNKSIZE (2*1024)
63 * The total buffer space used by NIT.
65 #define BUFSPACE (4*CHUNKSIZE)
68 static int nit_setflags(int, int, int, char *);
71 * Private data for capturing on NIT devices.
74 struct pcap_stat stat
;
78 pcap_stats_nit(pcap_t
*p
, struct pcap_stat
*ps
)
80 struct pcap_nit
*pn
= p
->priv
;
83 * "ps_recv" counts packets handed to the filter, not packets
84 * that passed the filter. As filtering is done in userland,
85 * this does not include packets dropped because we ran out
88 * "ps_drop" presumably counts packets dropped by the socket
89 * because of flow control requirements or resource exhaustion;
90 * it doesn't count packets dropped by the interface driver.
91 * As filtering is done in userland, it counts packets regardless
92 * of whether they would've passed the filter.
94 * These statistics don't include packets not yet read from the
95 * kernel by libpcap or packets not yet read from libpcap by the
103 pcap_read_nit(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
105 struct pcap_nit
*pn
= p
->priv
;
107 register u_char
*bp
, *cp
, *ep
;
108 register struct nit_hdr
*nh
;
113 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
115 if (errno
== EWOULDBLOCK
)
117 snprintf(p
->errbuf
, sizeof(p
->errbuf
), "pcap_read: %s",
118 pcap_strerror(errno
));
126 * Loop through each packet. The increment expression
127 * rounds up to the next int boundary past the end of
128 * the previous packet.
134 * Has "pcap_breakloop()" been called?
135 * If so, return immediately - if we haven't read any
136 * packets, clear the flag and return -2 to indicate
137 * that we were told to break out of the loop, otherwise
138 * leave the flag set, so that the *next* call will break
139 * out of the loop without having read any packets, and
140 * return the number of packets we've processed so far.
153 nh
= (struct nit_hdr
*)bp
;
154 cp
= bp
+ sizeof(*nh
);
156 switch (nh
->nh_state
) {
164 pn
->stat
.ps_drop
= nh
->nh_dropped
;
171 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
172 "bad nit state %d", nh
->nh_state
);
176 bp
+= ((sizeof(struct nit_hdr
) + nh
->nh_datalen
+
177 sizeof(int) - 1) & ~(sizeof(int) - 1));
179 caplen
= nh
->nh_wirelen
;
180 if (caplen
> p
->snapshot
)
181 caplen
= p
->snapshot
;
182 if (bpf_filter(p
->fcode
.bf_insns
, cp
, nh
->nh_wirelen
, caplen
)) {
183 struct pcap_pkthdr h
;
184 h
.ts
= nh
->nh_timestamp
;
185 h
.len
= nh
->nh_wirelen
;
187 (*callback
)(user
, &h
, cp
);
188 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
200 pcap_inject_nit(pcap_t
*p
, const void *buf
, size_t size
)
205 memset(&sa
, 0, sizeof(sa
));
206 strncpy(sa
.sa_data
, device
, sizeof(sa
.sa_data
));
207 ret
= sendto(p
->fd
, buf
, size
, 0, &sa
, sizeof(sa
));
209 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
210 pcap_strerror(errno
));
217 nit_setflags(pcap_t
*p
)
221 memset(&nioc
, 0, sizeof(nioc
));
222 nioc
.nioc_typetomatch
= NT_ALLTYPES
;
223 nioc
.nioc_snaplen
= p
->snapshot
;
224 nioc
.nioc_bufalign
= sizeof(int);
225 nioc
.nioc_bufoffset
= 0;
227 if (p
->opt
.buffer_size
!= 0)
228 nioc
.nioc_bufspace
= p
->opt
.buffer_size
;
230 /* Default buffer size */
231 nioc
.nioc_bufspace
= BUFSPACE
;
234 if (p
->opt
.immediate
) {
236 * XXX - will this cause packets to be delivered immediately?
237 * XXX - given that this is for SunOS prior to 4.0, do
240 nioc
.nioc_chunksize
= 0;
242 nioc
.nioc_chunksize
= CHUNKSIZE
;
243 if (p
->opt
.timeout
!= 0) {
244 nioc
.nioc_flags
|= NF_TIMEOUT
;
245 nioc
.nioc_timeout
.tv_sec
= p
->opt
.timeout
/ 1000;
246 nioc
.nioc_timeout
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
249 nioc
.nioc_flags
|= NF_PROMISC
;
251 if (ioctl(p
->fd
, SIOCSNIT
, &nioc
) < 0) {
252 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCSNIT: %s",
253 pcap_strerror(errno
));
260 pcap_activate_nit(pcap_t
*p
)
263 struct sockaddr_nit snit
;
267 * No monitor mode on SunOS 3.x or earlier (no
268 * Wi-Fi *devices* for the hardware that supported
271 return (PCAP_ERROR_RFMON_NOTSUP
);
274 if (p
->snapshot
< 96)
276 * NIT requires a snapshot length of at least 96.
280 memset(p
, 0, sizeof(*p
));
281 p
->fd
= fd
= socket(AF_NIT
, SOCK_RAW
, NITPROTO_RAW
);
283 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
284 "socket: %s", pcap_strerror(errno
));
287 snit
.snit_family
= AF_NIT
;
288 (void)strncpy(snit
.snit_ifname
, p
->opt
.source
, NITIFSIZ
);
290 if (bind(fd
, (struct sockaddr
*)&snit
, sizeof(snit
))) {
291 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
292 "bind: %s: %s", snit
.snit_ifname
, pcap_strerror(errno
));
295 if (nit_setflags(p
) < 0)
299 * NIT supports only ethernets.
301 p
->linktype
= DLT_EN10MB
;
303 p
->bufsize
= BUFSPACE
;
304 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
305 if (p
->buffer
== NULL
) {
306 strlcpy(p
->errbuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
311 * "p->fd" is a socket, so "select()" should work on it.
313 p
->selectable_fd
= p
->fd
;
316 * This is (presumably) a real Ethernet capture; give it a
317 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
318 * that an application can let you choose it, in case you're
319 * capturing DOCSIS traffic that a Cisco Cable Modem
320 * Termination System is putting out onto an Ethernet (it
321 * doesn't put an Ethernet header onto the wire, it puts raw
322 * DOCSIS frames out on the wire inside the low-level
325 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
327 * If that fails, just leave the list empty.
329 if (p
->dlt_list
!= NULL
) {
330 p
->dlt_list
[0] = DLT_EN10MB
;
331 p
->dlt_list
[1] = DLT_DOCSIS
;
335 p
->read_op
= pcap_read_nit
;
336 p
->inject_op
= pcap_inject_nit
;
337 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
338 p
->setdirection_op
= NULL
; /* Not implemented. */
339 p
->set_datalink_op
= NULL
; /* can't change data link type */
340 p
->getnonblock_op
= pcap_getnonblock_fd
;
341 p
->setnonblock_op
= pcap_setnonblock_fd
;
342 p
->stats_op
= pcap_stats_nit
;
346 pcap_cleanup_live_common(p
);
351 pcap_create_interface(const char *device
, char *ebuf
)
355 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_nit
));
359 p
->activate_op
= pcap_activate_nit
;
364 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)