2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
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[] =
23 "@(#) $Header: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/pcap-snoop.c,v 1.1 1999/08/26 10:05:26 johana Exp $ (LBL)";
26 #include <sys/param.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/ip_var.h>
40 #include <netinet/udp.h>
41 #include <netinet/udp_var.h>
42 #include <netinet/tcp.h>
43 #include <netinet/tcpip.h>
54 #ifdef HAVE_OS_PROTO_H
59 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
62 register struct snoopheader *sh;
68 cc = read(p->fd, (char *)p->buffer, p->bufsize);
70 /* Don't choke when we get ptraced */
79 sprintf(p->errbuf, "read: %s", pcap_strerror(errno));
82 sh = (struct snoopheader *)p->buffer;
83 datalen = sh->snoop_packetlen;
84 caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
85 cp = (u_char *)(sh + 1) + p->offset; /* XXX */
87 if (p->fcode.bf_insns == NULL ||
88 bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
91 h.ts = sh->snoop_timestamp;
94 (*callback)(user, &h, cp);
101 pcap_stats(pcap_t *p, struct pcap_stat *ps)
103 register struct rawstats *rs;
104 struct rawstats rawstats;
107 bzero((char *)rs, sizeof(*rs));
108 if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
109 sprintf(p->errbuf, "SIOCRAWSTATS: %s", pcap_strerror(errno));
114 rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
115 rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
121 /* XXX can't disable promiscuous */
123 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
126 struct sockaddr_raw sr;
127 struct snoopfilter sf;
131 p = (pcap_t *)malloc(sizeof(*p));
133 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
136 bzero((char *)p, sizeof(*p));
137 fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
139 sprintf(ebuf, "snoop socket: %s", pcap_strerror(errno));
143 bzero((char *)&sr, sizeof(sr));
144 sr.sr_family = AF_RAW;
145 (void)strncpy(sr.sr_ifname, device, sizeof(sr.sr_ifname));
146 if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
147 sprintf(ebuf, "snoop bind: %s", pcap_strerror(errno));
150 bzero((char *)&sf, sizeof(sf));
151 if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
152 sprintf(ebuf, "SIOCADDSNOOP: %s", pcap_strerror(errno));
156 (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
157 if (ioctl(fd, SIOCSNOOPLEN, &snaplen) < 0) {
158 sprintf(ebuf, "SIOCSNOOPLEN: %s", pcap_strerror(errno));
161 p->snapshot = snaplen;
163 if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
164 sprintf(ebuf, "SIOCSNOOPING: %s", pcap_strerror(errno));
168 * XXX hack - map device name to link layer type
170 if (strncmp("et", device, 2) == 0 || /* Challenge 10 Mbit */
171 strncmp("ec", device, 2) == 0 || /* Indigo/Indy 10 Mbit,
173 strncmp("ef", device, 2) == 0 || /* O200/2000 10/100 Mbit */
174 strncmp("gfe", device, 3) == 0 || /* GIO 100 Mbit */
175 strncmp("fxp", device, 3) == 0 || /* Challenge VME Enet */
176 strncmp("ep", device, 2) == 0 || /* Challenge 8x10 Mbit EPLEX */
177 strncmp("vfe", device, 3) == 0 || /* Challenge VME 100Mbit */
178 strncmp("fa", device, 2) == 0 ||
179 strncmp("qaa", device, 3) == 0) {
180 p->linktype = DLT_EN10MB;
181 p->offset = RAW_HDRPAD(sizeof(struct ether_header));
182 } else if (strncmp("ipg", device, 3) == 0 ||
183 strncmp("rns", device, 3) == 0 || /* O2/200/2000 FDDI */
184 strncmp("xpi", device, 3) == 0) {
185 p->linktype = DLT_FDDI;
186 p->offset = 3; /* XXX yeah? */
187 } else if (strncmp("ppp", device, 3) == 0) {
188 p->linktype = DLT_RAW;
189 } else if (strncmp("lo", device, 2) == 0) {
190 p->linktype = DLT_NULL;
192 sprintf(ebuf, "snoop: unknown physical layer type");
196 p->bufsize = 4096; /* XXX */
197 p->buffer = (u_char *)malloc(p->bufsize);
198 if (p->buffer == NULL) {
199 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
211 pcap_setfilter(pcap_t *p, struct bpf_program *fp)