add initial support for the crisarchitecture used on foxboards to openwrt
[openwrt/staging/dedeckeh.git] / target / linux / etrax-2.6 / image / e100boot / src / libpcap-0.4 / pcap-linux.c
1 /*
2 * Copyright (c) 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21 #ifndef lint
22 static const char rcsid[] =
23 "@(#) $Header: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/pcap-linux.c,v 1.1 1999/08/26 10:05:24 johana Exp $ (LBL)";
24 #endif
25
26 #include <sys/param.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30
31 #include <net/if.h>
32 #ifdef HAVE_NET_IF_ARP_H
33 #include <net/if_arp.h>
34 #else
35 #include <linux/if_arp.h>
36 #endif
37 #include <linux/if_ether.h>
38
39 #include <netinet/in.h>
40
41 #include <errno.h>
42 #include <malloc.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 static struct ifreq saved_ifr;
49 static int read_timout_ms = 0;
50
51 #include "pcap-int.h"
52
53 #include "gnuc.h"
54 #ifdef HAVE_OS_PROTO_H
55 #include "os-proto.h"
56 #endif
57
58 void linux_restore_ifr(void);
59
60 int
61 pcap_stats(pcap_t *p, struct pcap_stat *ps)
62 {
63
64 *ps = p->md.stat;
65 return (0);
66 }
67
68 int
69 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
70 {
71 register int cc;
72 register int bufsize;
73 register int caplen;
74 register u_char *bp;
75 struct sockaddr from;
76 int fromlen;
77
78 fd_set mask;
79 struct timeval tv;
80
81 if (read_timout_ms == 0) {
82 tv.tv_sec = 0;
83 tv.tv_usec = 0;
84 }
85 else {
86 tv.tv_sec = read_timout_ms/1000;
87 tv.tv_usec = read_timout_ms%1000;
88 }
89 FD_ZERO(&mask);
90 FD_SET(p->fd, &mask);
91
92 bp = p->buffer + p->offset;
93 bufsize = p->bufsize;
94 if (p->md.pad > 0) {
95 memset(bp, 0, p->md.pad);
96 bp += p->md.pad;
97 bufsize -= p->md.pad;
98 }
99
100 again:
101 do {
102 fromlen = sizeof(from);
103 select(FD_SETSIZE, &mask, NULL, NULL, &tv);
104 if (FD_ISSET(p->fd, &mask) == 0) {
105 return (0);
106 }
107
108 cc = recvfrom(p->fd, bp, bufsize, 0, &from, &fromlen);
109 if (cc < 0) {
110 /* Don't choke when we get ptraced */
111 switch (errno) {
112
113 case EINTR:
114 goto again;
115
116 case EWOULDBLOCK:
117 return (0); /* XXX */
118 }
119 sprintf(p->errbuf, "read: %s", pcap_strerror(errno));
120 return (-1);
121 }
122 } while (strcmp(p->md.device, from.sa_data));
123
124 /* If we need have leading zero bytes, adjust count */
125 cc += p->md.pad;
126 bp = p->buffer + p->offset;
127
128 /* If we need to step over leading junk, adjust count and pointer */
129 cc -= p->md.skip;
130 bp += p->md.skip;
131
132 /* Captured length can't exceed our read buffer size */
133 caplen = cc;
134 if (caplen > bufsize)
135 caplen = bufsize;
136
137 /* Captured length can't exceed the snapshot length */
138 if (caplen > p->snapshot)
139 caplen = p->snapshot;
140
141 if (p->fcode.bf_insns == NULL ||
142 bpf_filter(p->fcode.bf_insns, bp, cc, caplen)) {
143 struct pcap_pkthdr h;
144
145 ++p->md.stat.ps_recv;
146 /* Get timestamp */
147 if (ioctl(p->fd, SIOCGSTAMP, &h.ts) < 0) {
148 sprintf(p->errbuf, "SIOCGSTAMP: %s",
149 pcap_strerror(errno));
150 return (-1);
151 }
152 h.len = cc;
153 h.caplen = caplen;
154 (*callback)(user, &h, bp);
155 return (1);
156 }
157 return (0);
158 }
159
160 pcap_t *
161 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
162 {
163 register int fd, broadcast;
164 register pcap_t *p;
165 struct ifreq ifr;
166 struct sockaddr sa;
167
168 read_timout_ms = to_ms;
169 p = (pcap_t *)malloc(sizeof(*p));
170 if (p == NULL) {
171 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
172 return (NULL);
173 }
174 memset(p, 0, sizeof(*p));
175 fd = -1;
176
177 fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
178 if (fd < 0) {
179 sprintf(ebuf, "socket: %s", pcap_strerror(errno));
180 goto bad;
181 }
182 p->fd = fd;
183
184 /* Bind to the interface name */
185 memset(&sa, 0, sizeof(sa));
186 sa.sa_family = AF_INET;
187 (void)strncpy(sa.sa_data, device, sizeof(sa.sa_data));
188 if (bind(p->fd, &sa, sizeof(sa))) {
189 sprintf(ebuf, "bind: %s: %s", device, pcap_strerror(errno));
190 goto bad;
191 }
192
193 memset(&ifr, 0, sizeof(ifr));
194 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
195 if (ioctl(p->fd, SIOCGIFHWADDR, &ifr) < 0 ) {
196 sprintf(ebuf, "SIOCGIFHWADDR: %s", pcap_strerror(errno));
197 goto bad;
198 }
199 broadcast = 0;
200 switch (ifr.ifr_hwaddr.sa_family) {
201
202 case ARPHRD_ETHER:
203 case ARPHRD_METRICOM:
204 p->linktype = DLT_EN10MB;
205 p->offset = 2;
206 ++broadcast;
207 break;
208
209 case ARPHRD_EETHER:
210 p->linktype = DLT_EN3MB;
211 ++broadcast;
212 break;
213
214 case ARPHRD_AX25:
215 p->linktype = DLT_AX25;
216 ++broadcast;
217 break;
218
219 case ARPHRD_PRONET:
220 p->linktype = DLT_PRONET;
221 break;
222
223 case ARPHRD_CHAOS:
224 p->linktype = DLT_CHAOS;
225 break;
226
227 case ARPHRD_IEEE802:
228 p->linktype = DLT_IEEE802;
229 ++broadcast;
230 break;
231
232 case ARPHRD_ARCNET:
233 p->linktype = DLT_ARCNET;
234 ++broadcast;
235 break;
236
237 case ARPHRD_SLIP:
238 case ARPHRD_CSLIP:
239 case ARPHRD_SLIP6:
240 case ARPHRD_CSLIP6:
241 case ARPHRD_PPP:
242 p->linktype = DLT_RAW;
243 break;
244
245 case ARPHRD_LOOPBACK:
246 p->linktype = DLT_NULL;
247 p->md.pad = 2;
248 p->md.skip = 12;
249 break;
250
251 #ifdef ARPHRD_FDDI
252 /* Not all versions of the kernel has this define */
253 case ARPHRD_FDDI:
254 p->linktype = DLT_FDDI;
255 ++broadcast;
256 break;
257 #endif
258
259 #ifdef notdef
260 case ARPHRD_LOCALTLK:
261 case ARPHRD_NETROM:
262 case ARPHRD_APPLETLK:
263 case ARPHRD_DLCI:
264 case ARPHRD_RSRVD:
265 case ARPHRD_ADAPT:
266 case ARPHRD_TUNNEL:
267 case ARPHRD_TUNNEL6:
268 case ARPHRD_FRAD:
269 case ARPHRD_SKIP:
270 /* XXX currently do not know what to do with these... */
271 abort();
272 #endif
273
274 default:
275 sprintf(ebuf, "unknown physical layer type 0x%x",
276 ifr.ifr_hwaddr.sa_family);
277 goto bad;
278 }
279
280 /* Base the buffer size on the interface MTU */
281 memset(&ifr, 0, sizeof(ifr));
282 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
283 if (ioctl(p->fd, SIOCGIFMTU, &ifr) < 0 ) {
284 sprintf(ebuf, "SIOCGIFMTU: %s", pcap_strerror(errno));
285 goto bad;
286 }
287
288 /* Leave room for link header (which is never large under linux...) */
289 p->bufsize = ifr.ifr_mtu + 64;
290
291 p->buffer = (u_char *)malloc(p->bufsize + p->offset);
292 if (p->buffer == NULL) {
293 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
294 goto bad;
295 }
296
297 /* XXX */
298 if (promisc && broadcast) {
299 memset(&ifr, 0, sizeof(ifr));
300 strcpy(ifr.ifr_name, device);
301 if (ioctl(p->fd, SIOCGIFFLAGS, &ifr) < 0 ) {
302 sprintf(ebuf, "SIOCGIFFLAGS: %s", pcap_strerror(errno));
303 goto bad;
304 }
305 saved_ifr = ifr;
306 ifr.ifr_flags |= IFF_PROMISC;
307 if (ioctl(p->fd, SIOCSIFFLAGS, &ifr) < 0 ) {
308 sprintf(ebuf, "SIOCSIFFLAGS: %s", pcap_strerror(errno));
309 goto bad;
310 }
311 ifr.ifr_flags &= ~IFF_PROMISC;
312 atexit(linux_restore_ifr);
313 }
314
315 p->md.device = strdup(device);
316 if (p->md.device == NULL) {
317 sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
318 goto bad;
319 }
320 p->snapshot = snaplen;
321
322 return (p);
323 bad:
324 if (fd >= 0)
325 (void)close(fd);
326 if (p->buffer != NULL)
327 free(p->buffer);
328 if (p->md.device != NULL)
329 free(p->md.device);
330 free(p);
331 return (NULL);
332 }
333
334 int
335 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
336 {
337
338 p->fcode = *fp;
339 return (0);
340 }
341
342 void
343 linux_restore_ifr(void)
344 {
345 register int fd;
346
347 fd = socket(PF_INET, SOCK_PACKET, htons(0x0003));
348 if (fd < 0)
349 fprintf(stderr, "linux socket: %s", pcap_strerror(errno));
350 else if (ioctl(fd, SIOCSIFFLAGS, &saved_ifr) < 0)
351 fprintf(stderr, "linux SIOCSIFFLAGS: %s", pcap_strerror(errno));
352 }