1 /* $NetBSD: pcap-snoop.c,v 1.3 2015/03/31 21:39:42 christos Exp $ */
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: pcap-snoop.c,v 1.3 2015/03/31 21:39:42 christos Exp $");
31 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #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>
58 #ifdef HAVE_OS_PROTO_H
63 * Private data for capturing on snoop devices.
66 struct pcap_stat stat
;
70 pcap_read_snoop(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
72 struct pcap_snoop
*psn
= p
->priv
;
74 register struct snoopheader
*sh
;
75 register u_int datalen
;
76 register u_int caplen
;
81 * Has "pcap_breakloop()" been called?
85 * Yes - clear the flag that indicates that it
86 * has, and return -2 to indicate that we were
87 * told to break out of the loop.
92 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
94 /* Don't choke when we get ptraced */
101 return (0); /* XXX */
103 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
104 "read: %s", pcap_strerror(errno
));
107 sh
= (struct snoopheader
*)p
->buffer
;
108 datalen
= sh
->snoop_packetlen
;
111 * XXX - Sigh, snoop_packetlen is a 16 bit quantity. If we
112 * got a short length, but read a full sized snoop pakcet,
113 * assume we overflowed and add back the 64K...
115 if (cc
== (p
->snapshot
+ sizeof(struct snoopheader
)) &&
116 (datalen
< p
->snapshot
))
117 datalen
+= (64 * 1024);
119 caplen
= (datalen
< p
->snapshot
) ? datalen
: p
->snapshot
;
120 cp
= (u_char
*)(sh
+ 1) + p
->offset
; /* XXX */
123 * XXX unfortunately snoop loopback isn't exactly like
124 * BSD's. The address family is encoded in the first 2
125 * bytes rather than the first 4 bytes! Luckily the last
126 * two snoop loopback bytes are zeroed.
128 if (p
->linktype
== DLT_NULL
&& *((short *)(cp
+ 2)) == 0) {
129 u_int
*uip
= (u_int
*)cp
;
133 if (p
->fcode
.bf_insns
== NULL
||
134 bpf_filter(p
->fcode
.bf_insns
, cp
, datalen
, caplen
)) {
135 struct pcap_pkthdr h
;
137 h
.ts
.tv_sec
= sh
->snoop_timestamp
.tv_sec
;
138 h
.ts
.tv_usec
= sh
->snoop_timestamp
.tv_usec
;
141 (*callback
)(user
, &h
, cp
);
148 pcap_inject_snoop(pcap_t
*p
, const void *buf
, size_t size
)
153 * XXX - libnet overwrites the source address with what I
154 * presume is the interface's address; is that required?
156 ret
= write(p
->fd
, buf
, size
);
158 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
159 pcap_strerror(errno
));
166 pcap_stats_snoop(pcap_t
*p
, struct pcap_stat
*ps
)
168 struct pcap_snoop
*psn
= p
->priv
;
169 register struct rawstats
*rs
;
170 struct rawstats rawstats
;
173 memset(rs
, 0, sizeof(*rs
));
174 if (ioctl(p
->fd
, SIOCRAWSTATS
, (char *)rs
) < 0) {
175 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
176 "SIOCRAWSTATS: %s", pcap_strerror(errno
));
181 * "ifdrops" are those dropped by the network interface
182 * due to resource shortages or hardware errors.
184 * "sbdrops" are those dropped due to socket buffer limits.
186 * As filter is done in userland, "sbdrops" counts packets
187 * regardless of whether they would've passed the filter.
189 * XXX - does this count *all* Snoop or Drain sockets,
190 * rather than just this socket? If not, why does it have
191 * both Snoop and Drain statistics?
194 rs
->rs_snoop
.ss_ifdrops
+ rs
->rs_snoop
.ss_sbdrops
+
195 rs
->rs_drain
.ds_ifdrops
+ rs
->rs_drain
.ds_sbdrops
;
198 * "ps_recv" counts only packets that passed the filter.
199 * As filtering is done in userland, this does not include
200 * packets dropped because we ran out of buffer space.
206 /* XXX can't disable promiscuous */
208 pcap_activate_snoop(pcap_t
*p
)
211 struct sockaddr_raw sr
;
212 struct snoopfilter sf
;
218 fd
= socket(PF_RAW
, SOCK_RAW
, RAWPROTO_SNOOP
);
220 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snoop socket: %s",
221 pcap_strerror(errno
));
225 memset(&sr
, 0, sizeof(sr
));
226 sr
.sr_family
= AF_RAW
;
227 (void)strncpy(sr
.sr_ifname
, p
->opt
.source
, sizeof(sr
.sr_ifname
));
228 if (bind(fd
, (struct sockaddr
*)&sr
, sizeof(sr
))) {
229 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "snoop bind: %s",
230 pcap_strerror(errno
));
233 memset(&sf
, 0, sizeof(sf
));
234 if (ioctl(fd
, SIOCADDSNOOP
, &sf
) < 0) {
235 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCADDSNOOP: %s",
236 pcap_strerror(errno
));
239 if (p
->opt
.buffer_size
!= 0)
240 v
= p
->opt
.buffer_size
;
242 v
= 64 * 1024; /* default to 64K buffer size */
243 (void)setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *)&v
, sizeof(v
));
245 * XXX hack - map device name to link layer type
247 if (strncmp("et", p
->opt
.source
, 2) == 0 || /* Challenge 10 Mbit */
248 strncmp("ec", p
->opt
.source
, 2) == 0 || /* Indigo/Indy 10 Mbit,
250 strncmp("ef", p
->opt
.source
, 2) == 0 || /* O200/2000 10/100 Mbit */
251 strncmp("eg", p
->opt
.source
, 2) == 0 || /* Octane/O2xxx/O3xxx Gigabit */
252 strncmp("gfe", p
->opt
.source
, 3) == 0 || /* GIO 100 Mbit */
253 strncmp("fxp", p
->opt
.source
, 3) == 0 || /* Challenge VME Enet */
254 strncmp("ep", p
->opt
.source
, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */
255 strncmp("vfe", p
->opt
.source
, 3) == 0 || /* Challenge VME 100Mbit */
256 strncmp("fa", p
->opt
.source
, 2) == 0 ||
257 strncmp("qaa", p
->opt
.source
, 3) == 0 ||
258 strncmp("cip", p
->opt
.source
, 3) == 0 ||
259 strncmp("el", p
->opt
.source
, 2) == 0) {
260 p
->linktype
= DLT_EN10MB
;
261 p
->offset
= RAW_HDRPAD(sizeof(struct ether_header
));
262 ll_hdrlen
= sizeof(struct ether_header
);
264 * This is (presumably) a real Ethernet capture; give it a
265 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
266 * that an application can let you choose it, in case you're
267 * capturing DOCSIS traffic that a Cisco Cable Modem
268 * Termination System is putting out onto an Ethernet (it
269 * doesn't put an Ethernet header onto the wire, it puts raw
270 * DOCSIS frames out on the wire inside the low-level
273 * XXX - are there any sorts of "fake Ethernet" that have
274 * Ethernet link-layer headers but that *shouldn't offer
275 * DLT_DOCSIS as a Cisco CMTS won't put traffic onto it
276 * or get traffic bridged onto it? "el" is for ATM LANE
277 * Ethernet devices, so that might be the case for them;
278 * the same applies for "qaa" classical IP devices. If
279 * "fa" devices are for FORE SPANS, that'd apply to them
280 * as well; what are "cip" devices - some other ATM
281 * Classical IP devices?
283 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
285 * If that fails, just leave the list empty.
287 if (p
->dlt_list
!= NULL
) {
288 p
->dlt_list
[0] = DLT_EN10MB
;
289 p
->dlt_list
[1] = DLT_DOCSIS
;
292 } else if (strncmp("ipg", p
->opt
.source
, 3) == 0 ||
293 strncmp("rns", p
->opt
.source
, 3) == 0 || /* O2/200/2000 FDDI */
294 strncmp("xpi", p
->opt
.source
, 3) == 0) {
295 p
->linktype
= DLT_FDDI
;
296 p
->offset
= 3; /* XXX yeah? */
298 } else if (strncmp("ppp", p
->opt
.source
, 3) == 0) {
299 p
->linktype
= DLT_RAW
;
300 ll_hdrlen
= 0; /* DLT_RAW meaning "no PPP header, just the IP packet"? */
301 } else if (strncmp("qfa", p
->opt
.source
, 3) == 0) {
302 p
->linktype
= DLT_IP_OVER_FC
;
304 } else if (strncmp("pl", p
->opt
.source
, 2) == 0) {
305 p
->linktype
= DLT_RAW
;
306 ll_hdrlen
= 0; /* Cray UNICOS/mp pseudo link */
307 } else if (strncmp("lo", p
->opt
.source
, 2) == 0) {
308 p
->linktype
= DLT_NULL
;
311 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
312 "snoop: unknown physical layer type");
318 * No monitor mode on Irix (no Wi-Fi devices on
319 * hardware supported by Irix).
321 return (PCAP_ERROR_RFMON_NOTSUP
);
326 * XXX - IRIX appears to give you an error if you try to set the
327 * capture length to be greater than the MTU, so let's try to get
328 * the MTU first and, if that succeeds, trim the snap length
329 * to be no greater than the MTU.
331 (void)strncpy(ifr
.ifr_name
, p
->opt
.source
, sizeof(ifr
.ifr_name
));
332 if (ioctl(fd
, SIOCGIFMTU
, (char *)&ifr
) < 0) {
333 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMTU: %s",
334 pcap_strerror(errno
));
340 * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an
341 * "ifru_metric" member of the "ifr_ifru" union in an "ifreq"
342 * structure, others don't.
344 * I've no idea what's going on, so, if "ifr_mtu" isn't defined,
345 * we define it as "ifr_metric", as using that field appears to
346 * work on the versions that lack "ifr_mtu" (and, on those that
347 * don't lack it, "ifru_metric" and "ifru_mtu" are both "int"
348 * members of the "ifr_ifru" union, which suggests that they
349 * may be interchangeable in this case).
352 #define ifr_mtu ifr_metric
354 if (p
->snapshot
> ifr
.ifr_mtu
+ ll_hdrlen
)
355 p
->snapshot
= ifr
.ifr_mtu
+ ll_hdrlen
;
359 * The argument to SIOCSNOOPLEN is the number of link-layer
360 * payload bytes to capture - it doesn't count link-layer
363 snooplen
= p
->snapshot
- ll_hdrlen
;
366 if (ioctl(fd
, SIOCSNOOPLEN
, &snooplen
) < 0) {
367 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCSNOOPLEN: %s",
368 pcap_strerror(errno
));
372 if (ioctl(fd
, SIOCSNOOPING
, &v
) < 0) {
373 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCSNOOPING: %s",
374 pcap_strerror(errno
));
378 p
->bufsize
= 4096; /* XXX */
379 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
380 if (p
->buffer
== NULL
) {
381 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
382 pcap_strerror(errno
));
387 * "p->fd" is a socket, so "select()" should work on it.
389 p
->selectable_fd
= p
->fd
;
391 p
->read_op
= pcap_read_snoop
;
392 p
->inject_op
= pcap_inject_snoop
;
393 p
->setfilter_op
= install_bpf_program
; /* no kernel filtering */
394 p
->setdirection_op
= NULL
; /* Not implemented. */
395 p
->set_datalink_op
= NULL
; /* can't change data link type */
396 p
->getnonblock_op
= pcap_getnonblock_fd
;
397 p
->setnonblock_op
= pcap_setnonblock_fd
;
398 p
->stats_op
= pcap_stats_snoop
;
402 pcap_cleanup_live_common(p
);
407 pcap_create_interface(const char *device
, char *ebuf
)
411 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_snoop
));
415 p
->activate_op
= pcap_activate_snoop
;
420 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)