]> The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
a214e15f20f697a12b666c92ed39c6863481c168
[libpcap] / pcap-linux.c
1 /*
2 * pcap-linux.c: Packet capture interface to the Linux kernel
3 *
4 * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
5 * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
6 *
7 * License: BSD
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 */
27 #ifndef lint
28 static const char rcsid[] =
29 "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.48 2000-12-22 12:30:04 guy Exp $ (LBL)";
30 #endif
31
32 /*
33 * Known problems with 2.0[.x] kernels:
34 *
35 * - The loopback device gives every packet twice; on 2.2[.x] kernels,
36 * if we use PF_PACKET, we can filter out the transmitted version
37 * of the packet by using data in the "sockaddr_ll" returned by
38 * "recvfrom()", but, on 2.0[.x] kernels, we have to use
39 * PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
40 * "sockaddr_pkt" which doesn't give us enough information to let
41 * us do that.
42 *
43 * - We have to set the interface's IFF_PROMISC flag ourselves, if
44 * we're to run in promiscuous mode, which means we have to turn
45 * it off ourselves when we're done; the kernel doesn't keep track
46 * of how many sockets are listening promiscuously, which means
47 * it won't get turned off automatically when no sockets are
48 * listening promiscuously. We catch "pcap_close()" and, for
49 * interfaces we put into promiscuous mode, take them out of
50 * promiscuous mode - which isn't necessarily the right thing to
51 * do, if another socket also requested promiscuous mode between
52 * the time when we opened the socket and the time when we close
53 * the socket.
54 */
55
56
57 #ifdef HAVE_CONFIG_H
58 #include "config.h"
59 #endif
60
61 #include "pcap-int.h"
62 #include "sll.h"
63
64 #include <errno.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <string.h>
69 #include <sys/socket.h>
70 #include <sys/ioctl.h>
71 #include <sys/utsname.h>
72 #include <net/if.h>
73 #include <netinet/in.h>
74 #include <linux/if_ether.h>
75 #include <netinet/if_ether.h>
76
77 #ifdef HAVE_NETPACKET_PACKET_H
78 #include <netpacket/packet.h>
79 #endif
80 #ifdef SO_ATTACH_FILTER
81 #include <linux/types.h>
82 #include <linux/filter.h>
83 #endif
84
85 #ifndef __GLIBC__
86 typedef int socklen_t;
87 #endif
88
89 #ifndef MSG_TRUNC
90 #define MSG_TRUNC 0
91 #endif
92
93 #define MAX_LINKHEADER_SIZE 256
94
95 /*
96 * When capturing on all interfaces we use this as the buffer size.
97 * Should be bigger then all MTUs that occur in real life.
98 * 64kB should be enough for now.
99 */
100 #define BIGGER_THAN_ALL_MTUS (64*1024)
101
102 /*
103 * Prototypes for internal functions
104 */
105 static int map_arphrd_to_dlt(int arptype );
106 static int live_open_old(pcap_t *, char *, int, int, char *);
107 static int live_open_new(pcap_t *, char *, int, int, char *);
108 static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
109
110 /*
111 * Wrap some ioctl calls
112 */
113 #ifdef HAVE_NETPACKET_PACKET_H
114 static int iface_get_id(int fd, const char *device, char *ebuf);
115 #endif
116 static int iface_get_mtu(int fd, const char *device, char *ebuf);
117 static int iface_get_arptype(int fd, const char *device, char *ebuf);
118 #ifdef HAVE_NETPACKET_PACKET_H
119 static int iface_bind(int fd, int ifindex, char *ebuf);
120 #endif
121 static int iface_bind_old(int fd, const char *device, char *ebuf);
122
123 #ifdef SO_ATTACH_FILTER
124 static int fix_program(pcap_t *handle, struct sock_fprog *fcode);
125 static int fix_offset(struct bpf_insn *p);
126 #endif
127
128 /*
129 * Get a handle for a live capture from the given device. You can
130 * pass NULL as device to get all packages (without link level
131 * information of course). If you pass 1 as promisc the interface
132 * will be set to promiscous mode (XXX: I think this usage should
133 * be deprecated and functions be added to select that later allow
134 * modification of that values -- Torsten).
135 *
136 * See also pcap(3).
137 */
138 pcap_t *
139 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
140 {
141 /* Allocate a handle for this session. */
142
143 pcap_t *handle = malloc(sizeof(*handle));
144 if (handle == NULL) {
145 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
146 pcap_strerror(errno));
147 return NULL;
148 }
149
150 /* Initialize some components of the pcap structure. */
151
152 memset(handle, 0, sizeof(*handle));
153 handle->snapshot = snaplen;
154 handle->md.timeout = to_ms;
155
156 /*
157 * NULL and "any" are special devices which give us the hint to
158 * monitor all devices.
159 */
160 if (!device || strcmp(device, "any") == 0) {
161 device = NULL;
162 handle->md.device = strdup("any");
163 } else
164 handle->md.device = strdup(device);
165
166 if (handle->md.device == NULL) {
167 snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s",
168 pcap_strerror(errno) );
169 free(handle);
170 return NULL;
171 }
172
173 /*
174 * Current Linux kernels use the protocol family PF_PACKET to
175 * allow direct access to all packets on the network while
176 * older kernels had a special socket type SOCK_PACKET to
177 * implement this feature.
178 * While this old implementation is kind of obsolete we need
179 * to be compatible with older kernels for a while so we are
180 * trying both methods with the newer method preferred.
181 */
182
183 if (! (live_open_new(handle, device, promisc, to_ms, ebuf) ||
184 live_open_old(handle, device, promisc, to_ms, ebuf)) )
185 {
186 /*
187 * Both methods to open the packet socket failed. Tidy
188 * up and report our failure (ebuf is expected to be
189 * set by the functions above).
190 */
191
192 free(handle->md.device);
193 free(handle);
194 return NULL;
195 }
196
197 /*
198 * Okay, now we have a packet stream open. Maybe we need to handle
199 * a timeout? In that case we set the filehandle to nonblocking
200 * so pcap_read can try reading the fd and call select if no data
201 * is available at first.
202 */
203
204 if (to_ms > 0) {
205 int flags = fcntl(handle->fd, F_GETFL);
206 if (flags != -1) {
207 flags |= O_NONBLOCK;
208 flags = fcntl(handle->fd, F_SETFL, flags);
209 }
210 if (flags == -1) {
211 snprintf(ebuf, PCAP_ERRBUF_SIZE, "fcntl: %s",
212 pcap_strerror(errno));
213 pcap_close(handle);
214 return NULL;
215 }
216 }
217
218 return handle;
219 }
220
221 /*
222 * Read at most max_packets from the capture stream and call the callback
223 * for each of them. Returns the number of packets handled or -1 if an
224 * error occured.
225 *
226 * XXX: Can I rely on the Linux-specified behaviour of select (returning
227 * the time left in the timeval structure)? I really don't want to query
228 * the system time before each select call...
229 *
230 * pcap_read currently gets not only a packet from the kernel but also
231 * the sockaddr_ll returned as source of the packet. This way we can at
232 * some time extend tcpdump and libpcap to sniff on all devices at a time
233 * and find the right printing routine by using the information in the
234 * sockaddr_ll structure.
235 */
236 int
237 pcap_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
238 {
239 int status, packets;
240 fd_set read_fds;
241 struct timeval tv;
242
243 /*
244 * Fill in a timeval structure for select if we need to obeye a
245 * timeout.
246 */
247 if (handle->md.timeout > 0) {
248 tv.tv_usec = (handle->md.timeout % 1000) * 1000;
249 tv.tv_sec = (handle->md.timeout / 1000);
250 }
251
252 /*
253 * Read packets until the packet limit has been reached or
254 * an error occured while reading. Call the user function
255 * for each received packet.
256 */
257 for (packets = 0; max_packets == -1 || packets < max_packets;)
258 {
259 status = pcap_read_packet(handle, callback, user);
260
261 if (status > 0) {
262 packets += status;
263 continue;
264 } else if (status == -1)
265 return -1;
266
267 /*
268 * If no packet is available we go to sleep. FIXME: This
269 * might be better implemented using poll(?)
270 */
271 FD_ZERO(&read_fds);
272 FD_SET(handle->fd, &read_fds);
273 status = select(handle->fd + 1,
274 &read_fds, NULL, NULL, &tv);
275
276 if (status == -1) {
277 if (errno == EINTR)
278 return packets;
279 snprintf(handle->errbuf, sizeof(handle->errbuf),
280 "select: %s", pcap_strerror(errno));
281 return -1;
282 }
283 else if (status == 0 ||
284 (tv.tv_usec == 0 && tv.tv_sec == 0))
285 return packets;
286 }
287
288 return packets;
289 }
290
291 /*
292 * Read a packet from the socket calling the handler provided by
293 * the user. Returns the number of packets received or -1 if an
294 * error occured.
295 */
296 static int
297 pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
298 {
299 int offset;
300 #ifdef HAVE_NETPACKET_PACKET_H
301 struct sockaddr_ll from;
302 struct sll_header *hdrp;
303 #else
304 struct sockaddr from;
305 #endif
306 socklen_t fromlen;
307 int packet_len, caplen;
308 struct pcap_pkthdr pcap_header;
309
310 #ifdef HAVE_NETPACKET_PACKET_H
311 /*
312 * If this is a cooked device, leave extra room for a
313 * fake packet header.
314 */
315 if (handle->md.cooked)
316 offset = SLL_HDR_LEN;
317 else
318 offset = 0;
319 #else
320 /*
321 * This system doesn't have PF_PACKET sockets, so it doesn't
322 * support cooked devices.
323 */
324 offset = 0;
325 #endif
326
327 /* Receive a single packet from the kernel */
328
329 do {
330 fromlen = sizeof(from);
331 packet_len = recvfrom(
332 handle->fd, handle->buffer + offset + handle->offset,
333 handle->md.readlen - offset, MSG_TRUNC,
334 (struct sockaddr *) &from, &fromlen);
335 } while (packet_len == -1 && errno == EINTR);
336
337 /* Check if an error occured */
338
339 if (packet_len == -1) {
340 if (errno == EAGAIN)
341 return 0; /* no packet there */
342 else {
343 snprintf(handle->errbuf, sizeof(handle->errbuf),
344 "recvfrom: %s", pcap_strerror(errno));
345 return -1;
346 }
347 }
348
349 #ifdef HAVE_NETPACKET_PACKET_H
350 /*
351 * If this is from the loopback device, reject outgoing packets;
352 * we'll see the packet as an incoming packet as well, and
353 * we don't want to see it twice.
354 *
355 * We can only do this if we're using PF_PACKET; the address
356 * returned for SOCK_PACKET is a "sockaddr_pkt" which lacks
357 * the relevant packet type information.
358 */
359 if (!handle->md.sock_packet &&
360 from.sll_ifindex == handle->md.lo_ifindex &&
361 from.sll_pkttype == PACKET_OUTGOING)
362 return 0;
363 #endif
364
365 #ifdef HAVE_NETPACKET_PACKET_H
366 /*
367 * If this is a cooked device, fill in the fake packet header.
368 */
369 if (handle->md.cooked) {
370 /*
371 * Add the length of the fake header to the length
372 * of packet data we read.
373 */
374 packet_len += SLL_HDR_LEN;
375
376 hdrp = (struct sll_header *)handle->buffer;
377
378 /*
379 * Map the PACKET_ value to a LINUX_SLL_ value; we
380 * want the same numerical value to be used in
381 * the link-layer header even if the numerical values
382 * for the PACKET_ #defines change, so that programs
383 * that look at the packet type field will always be
384 * able to handle DLT_LINUX_SLL captures.
385 */
386 switch (from.sll_pktttype) {
387
388 case PACKET_HOST:
389 hdrp->sll_pkttype = htons(LINUX_SLL_HOST);
390 break;
391
392 case PACKET_BROADCAST:
393 hdrp->sll_pkttype = htons(LINUX_SLL_BROADCAST);
394 break;
395
396 case PACKET_MULTICAST:
397 hdrp->sll_pkttype = htons(LINUX_SLL_MULTICAST);
398 break;
399
400 case PACKET_OTHERHOST:
401 hdrp->sll_pkttype = htons(LINUX_SLL_OTHERHOST);
402 break;
403
404 case PACKET_OUTGOING:
405 hdrp->sll_pkttype = htons(LINUX_SLL_OUTGOING);
406 break;
407
408 default:
409 hdrp->sll_pkttype = -1;
410 break;
411 }
412
413 hdrp->sll_protocol = from.sll_protocol;
414 hdrp->sll_hatype = htons(from.sll_hatype);
415 hdrp->sll_halen = htons(from.sll_halen);
416 memcpy(hdrp->sll_addr, from.sll_addr,
417 (from.sll_halen > SLL_ADDRLEN) ?
418 SLL_ADDRLEN :
419 from.sll_halen);
420 }
421 #endif
422
423 /*
424 * XXX: According to the kernel source we should get the real
425 * packet len if calling recvfrom with MSG_TRUNC set. It does
426 * not seem to work here :(, but it is supported by this code
427 * anyway.
428 * To be honest the code RELIES on that feature so this is really
429 * broken with 2.2.x kernels.
430 * I spend a day to figure out what's going on and I found out
431 * that the following is happening:
432 *
433 * The packet comes from a random interface and the packet_rcv
434 * hook is called with a clone of the packet. That code inserts
435 * the packet into the receive queue of the packet socket.
436 * If a filter is attached to that socket that filter is run
437 * first - and there lies the problem. The default filter always
438 * cuts the packet at the snaplen:
439 *
440 * # tcpdump -d
441 * (000) ret #68
442 *
443 * So the packet filter cuts down the packet. The recvfrom call
444 * says "hey, it's only 68 bytes, it fits into the buffer" with
445 * the result that we don't get the real packet length. This
446 * is valid at least until kernel 2.2.17pre6.
447 *
448 * We currently handle this by making a copy of the filter
449 * program, fixing all "ret" instructions with non-zero
450 * operands to have an operand of 65535 so that the filter
451 * doesn't truncate the packet, and supplying that modified
452 * filter to the kernel.
453 */
454
455 caplen = packet_len;
456 if (caplen > handle->snapshot)
457 caplen = handle->snapshot;
458
459 /* Run the packet filter if not using kernel filter */
460 if (!handle->md.use_bpf && handle->fcode.bf_insns) {
461 if (bpf_filter(handle->fcode.bf_insns, handle->buffer,
462 packet_len, caplen) == 0)
463 {
464 /* rejected by filter */
465 return 0;
466 }
467 }
468
469 /* Fill in our own header data */
470
471 if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
472 snprintf(handle->errbuf, sizeof(handle->errbuf),
473 "ioctl: %s", pcap_strerror(errno));
474 return -1;
475 }
476 pcap_header.caplen = caplen;
477 pcap_header.len = packet_len;
478
479 /* Call the user supplied callback function */
480 handle->md.stat.ps_recv++;
481 callback(userdata, &pcap_header, handle->buffer + handle->offset);
482
483 return 1;
484 }
485
486 /*
487 * Get the statistics for the given packet capture handle.
488 * FIXME: Currently does not report the number of dropped packets.
489 */
490 int
491 pcap_stats(pcap_t *handle, struct pcap_stat *stats)
492 {
493 *stats = handle->md.stat;
494 return 0;
495 }
496
497 /*
498 * Attach the given BPF code to the packet capture device.
499 */
500 int
501 pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
502 {
503 #ifdef SO_ATTACH_FILTER
504 struct sock_fprog fcode;
505 int can_filter_in_kernel;
506 #endif
507
508 if (!handle)
509 return -1;
510 if (!filter) {
511 strncpy(handle->errbuf, "setfilter: No filter specified",
512 sizeof(handle->errbuf));
513 return -1;
514 }
515
516 /* Make our private copy of the filter */
517
518 if (install_bpf_program(handle, filter) < 0) {
519 snprintf(handle->errbuf, sizeof(handle->errbuf),
520 "malloc: %s", pcap_strerror(errno));
521 return -1;
522 }
523
524 /*
525 * Run user level packet filter by default. Will be overriden if
526 * installing a kernel filter succeeds.
527 */
528 handle->md.use_bpf = 0;
529
530 /*
531 * If we're reading from a savefile, don't try to install
532 * a kernel filter.
533 */
534 if (handle->sf.rfile != NULL)
535 return 0;
536
537 /* Install kernel level filter if possible */
538
539 #ifdef SO_ATTACH_FILTER
540 #ifdef USHRT_MAX
541 if (handle->fcode.bf_len > USHRT_MAX) {
542 /*
543 * fcode.len is an unsigned short for current kernel.
544 * I have yet to see BPF-Code with that much
545 * instructions but still it is possible. So for the
546 * sake of correctness I added this check.
547 */
548 fprintf(stderr, "Warning: Filter too complex for kernel\n");
549 fcode.filter = NULL;
550 can_filter_in_kernel = 0;
551 } else
552 #endif /* USHRT_MAX */
553 {
554 /*
555 * Oh joy, the Linux kernel uses struct sock_fprog instead
556 * of struct bpf_program and of course the length field is
557 * of different size. Pointed out by Sebastian
558 *
559 * Oh, and we also need to fix it up so that all "ret"
560 * instructions with non-zero operands have 65535 as the
561 * operand, and so that, if we're in cooked mode, all
562 * memory-reference instructions use special magic offsets
563 * in references to the link-layer header and assume that
564 * the link-layer payload begins at 0; "fix_program()"
565 * will do that.
566 */
567 switch (fix_program(handle, &fcode)) {
568
569 case -1:
570 default:
571 /*
572 * Fatal error; just quit.
573 * (The "default" case shouldn't happen; we
574 * return -1 for that reason.)
575 */
576 return -1;
577
578 case 0:
579 /*
580 * The program performed checks that we can't make
581 * work in the kernel.
582 */
583 can_filter_in_kernel = 0;
584 break;
585
586 case 1:
587 /*
588 * We have a filter that'll work in the kernel.
589 */
590 can_filter_in_kernel = 1;
591 break;
592 }
593 }
594
595 if (can_filter_in_kernel) {
596 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
597 &fcode, sizeof(fcode)) == 0)
598 {
599 /* Installation succeded - using kernel filter. */
600 handle->md.use_bpf = 1;
601 }
602 else
603 {
604 /*
605 * Print a warning if we weren't able to install
606 * the filter for a reason other than "this kernel
607 * isn't configured to support socket filters.
608 */
609 if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) {
610 fprintf(stderr,
611 "Warning: Kernel filter failed: %s\n",
612 pcap_strerror(errno));
613 }
614 }
615 }
616
617 /*
618 * Free up the copy of the filter that was made by "fix_program()".
619 */
620 if (fcode.filter != NULL)
621 free(fcode.filter);
622 #endif /* SO_ATTACH_FILTER */
623
624 return 0;
625 }
626
627 /*
628 * Linux uses the ARP hardware type to identify the type of an
629 * interface. pcap uses the DLT_xxx constants for this. This
630 * function maps the ARPHRD_xxx constant to an appropriate
631 * DLT_xxx constant.
632 *
633 * Returns -1 if unable to map the type; we print a message and,
634 * if we're using PF_PACKET/SOCK_RAW rather than PF_INET/SOCK_PACKET,
635 * we fall back on using PF_PACKET/SOCK_DGRAM.
636 */
637 static int map_arphrd_to_dlt(int arptype)
638 {
639 switch (arptype) {
640 case ARPHRD_ETHER:
641 case ARPHRD_METRICOM:
642 case ARPHRD_LOOPBACK: return DLT_EN10MB;
643 case ARPHRD_EETHER: return DLT_EN3MB;
644 case ARPHRD_AX25: return DLT_AX25;
645 case ARPHRD_PRONET: return DLT_PRONET;
646 case ARPHRD_CHAOS: return DLT_CHAOS;
647 #ifndef ARPHRD_IEEE802_TR
648 #define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */
649 #endif
650 case ARPHRD_IEEE802_TR:
651 case ARPHRD_IEEE802: return DLT_IEEE802;
652 case ARPHRD_ARCNET: return DLT_ARCNET;
653 case ARPHRD_FDDI: return DLT_FDDI;
654
655 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
656 #define ARPHRD_ATM 19
657 #endif
658 case ARPHRD_ATM: return DLT_ATM_CLIP;
659
660 case ARPHRD_PPP:
661 /* Not sure if this is correct for all tunnels, but it
662 * works for CIPE */
663 case ARPHRD_TUNNEL:
664 case ARPHRD_SIT:
665 case ARPHRD_CSLIP:
666 case ARPHRD_SLIP6:
667 case ARPHRD_CSLIP6:
668 case ARPHRD_SLIP: return DLT_RAW;
669 }
670
671 return -1;
672 }
673
674 /* ===== Functions to interface to the newer kernels ================== */
675
676 /*
677 * Try to open a packet socket using the new kernel interface.
678 * Returns 0 on failure.
679 * FIXME: 0 uses to mean success (Sebastian)
680 */
681 static int
682 live_open_new(pcap_t *handle, char *device, int promisc,
683 int to_ms, char *ebuf)
684 {
685 #ifdef HAVE_NETPACKET_PACKET_H
686 int sock_fd = -1, device_id, mtu, arptype;
687 struct packet_mreq mr;
688
689 /* One shot loop used for error handling - bail out with break */
690
691 do {
692 /*
693 * Open a socket with protocol family packet. If a device is
694 * given we try to open it in raw mode otherwise we use
695 * the cooked interface.
696 */
697 sock_fd = device ?
698 socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
699 : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
700
701 if (sock_fd == -1) {
702 snprintf(ebuf, PCAP_ERRBUF_SIZE, "socket: %s",
703 pcap_strerror(errno) );
704 break;
705 }
706
707 /* It seems the kernel supports the new interface. */
708 handle->md.sock_packet = 0;
709
710 /*
711 * Get the interface index of the loopback device.
712 * If the attempt fails, don't fail, just set the
713 * "md.lo_ifindex" to -1.
714 *
715 * XXX - can there be more than one device that loops
716 * packets back, i.e. devices other than "lo"? If so,
717 * we'd need to find them all, and have an array of
718 * indices for them, and check all of them in
719 * "pcap_read_packet()".
720 */
721 handle->md.lo_ifindex = iface_get_id(sock_fd, "lo", ebuf);
722
723 /*
724 * What kind of frames do we have to deal with? Fall back
725 * to cooked mode if we have an unknown interface type.
726 */
727
728 if (device) {
729 /* Assume for now we don't need cooked mode. */
730 handle->md.cooked = 0;
731
732 arptype = iface_get_arptype(sock_fd, device, ebuf);
733 if (arptype == -1)
734 break;
735 handle->linktype = map_arphrd_to_dlt(arptype);
736 if (handle->linktype == -1 ||
737 (handle->linktype == DLT_EN10MB &&
738 (strncmp("isdn", device, 4) == 0 ||
739 strncmp("isdY", device, 4) == 0)) ||
740 (handle->linktype == DLT_RAW &&
741 (strncmp("ippp", device, 4) == 0))) {
742 /*
743 * Unknown interface type (-1), or an ISDN
744 * device (whose link-layer type we
745 * can only determine by using APIs
746 * that may be different on different
747 * kernels) - reopen in cooked mode.
748 *
749 * XXX - do that with DLT_RAW as well?
750 */
751 if (close(sock_fd) == -1) {
752 snprintf(ebuf, PCAP_ERRBUF_SIZE,
753 "close: %s", pcap_strerror(errno));
754 break;
755 }
756 sock_fd = socket(PF_PACKET, SOCK_DGRAM,
757 htons(ETH_P_ALL));
758 if (sock_fd == -1) {
759 snprintf(ebuf, PCAP_ERRBUF_SIZE,
760 "socket: %s", pcap_strerror(errno));
761 break;
762 }
763 handle->md.cooked = 1;
764
765 if (handle->linktype == -1) {
766 /*
767 * Warn that we're falling back on
768 * cooked mode; we may want to
769 * update "map_arphrd_to_dlt()"
770 * to handle the new type.
771 */
772 fprintf(stderr,
773 "Warning: arptype %d not "
774 "supported by libpcap - "
775 "falling back to cooked "
776 "socket\n",
777 arptype);
778 }
779 handle->linktype = DLT_LINUX_SLL;
780 }
781
782 device_id = iface_get_id(sock_fd, device, ebuf);
783 if (device_id == -1)
784 break;
785
786 if (iface_bind(sock_fd, device_id, ebuf) == -1)
787 break;
788 } else {
789 /*
790 * This is cooked mode.
791 */
792 handle->md.cooked = 1;
793 handle->linktype = DLT_LINUX_SLL;
794
795 /*
796 * XXX - squelch GCC complaints about
797 * uninitialized variables; if we can't
798 * select promiscuous mode on all interfaces,
799 * we should move the code below into the
800 * "if (device)" branch of the "if" and
801 * get rid of the next statement.
802 */
803 device_id = -1;
804 }
805
806 /* Select promiscuous mode on/off */
807
808 #ifdef SOL_PACKET
809 /*
810 * Hmm, how can we set promiscuous mode on all interfaces?
811 * I am not sure if that is possible at all.
812 */
813
814 if (device) {
815 memset(&mr, 0, sizeof(mr));
816 mr.mr_ifindex = device_id;
817 mr.mr_type = promisc ?
818 PACKET_MR_PROMISC : PACKET_MR_ALLMULTI;
819 if (setsockopt(sock_fd, SOL_PACKET,
820 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
821 {
822 snprintf(ebuf, PCAP_ERRBUF_SIZE,
823 "setsockopt: %s", pcap_strerror(errno));
824 break;
825 }
826 }
827 #endif
828
829 /* Compute the buffersize */
830
831 mtu = iface_get_mtu(sock_fd, device, ebuf);
832 if (mtu == -1)
833 break;
834 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
835
836 /* Fill in the pcap structure */
837
838 handle->fd = sock_fd;
839 handle->offset = 0;
840
841 handle->buffer = malloc(handle->bufsize);
842 if (!handle->buffer) {
843 snprintf(ebuf, PCAP_ERRBUF_SIZE,
844 "malloc: %s", pcap_strerror(errno));
845 break;
846 }
847
848 /*
849 * This is a 2.2 or later kernel, as it has PF_PACKET;
850 * "recvfrom()", when passed the MSG_TRUNC flag, will
851 * return the actual length of the packet, not the
852 * number of bytes from the packet copied to userland,
853 * so we can safely pass it a byte count based on the
854 * snapshot length.
855 */
856 handle->md.readlen = handle->snapshot;
857 return 1;
858
859 } while(0);
860
861 if (sock_fd != -1)
862 close(sock_fd);
863 return 0;
864 #else
865 strncpy(ebuf,
866 "New packet capturing interface not supported by build "
867 "environment", PCAP_ERRBUF_SIZE);
868 return 0;
869 #endif
870 }
871
872 #ifdef HAVE_NETPACKET_PACKET_H
873 /*
874 * Return the index of the given device name. Fill ebuf and return
875 * -1 on failure.
876 */
877 static int
878 iface_get_id(int fd, const char *device, char *ebuf)
879 {
880 struct ifreq ifr;
881
882 memset(&ifr, 0, sizeof(ifr));
883 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
884
885 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
886 snprintf(ebuf, PCAP_ERRBUF_SIZE,
887 "ioctl: %s", pcap_strerror(errno));
888 return -1;
889 }
890
891 return ifr.ifr_ifindex;
892 }
893
894 /*
895 * Bind the socket associated with FD to the given device.
896 */
897 static int
898 iface_bind(int fd, int ifindex, char *ebuf)
899 {
900 struct sockaddr_ll sll;
901
902 memset(&sll, 0, sizeof(sll));
903 sll.sll_family = AF_PACKET;
904 sll.sll_ifindex = ifindex;
905 sll.sll_protocol = htons(ETH_P_ALL);
906
907 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
908 snprintf(ebuf, PCAP_ERRBUF_SIZE,
909 "bind: %s", pcap_strerror(errno));
910 return -1;
911 }
912
913 return 0;
914 }
915
916 #endif
917
918
919 /* ===== Functions to interface to the older kernels ================== */
920
921 /*
922 * With older kernels promiscuous mode is kind of interesting because we
923 * have to reset the interface before exiting. The problem can't really
924 * be solved without some daemon taking care of managing usage counts.
925 * If we put the interface into promiscuous mode, we set a flag indicating
926 * that we must take it out of that mode when the interface is closed,
927 * and, when closing the interface, if that flag is set we take it out
928 * of promiscuous mode.
929 */
930
931 /*
932 * List of pcaps for which we turned promiscuous mode on by hand.
933 * If there are any such pcaps, we arrange to call "pcap_close_all()"
934 * when we exit, and have it close all of them to turn promiscuous mode
935 * off.
936 */
937 static struct pcap *pcaps_to_close;
938
939 /*
940 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
941 * be called on exit.
942 */
943 static int did_atexit;
944
945 static void pcap_close_all(void)
946 {
947 struct pcap *handle;
948
949 while ((handle = pcaps_to_close) != NULL)
950 pcap_close(handle);
951 }
952
953 void pcap_close_linux( pcap_t *handle )
954 {
955 struct pcap *p, *prevp;
956 struct ifreq ifr;
957
958 if (handle->md.clear_promisc) {
959 /*
960 * We put the interface into promiscuous mode; take
961 * it out of promiscuous mode.
962 *
963 * XXX - if somebody else wants it in promiscuous mode,
964 * this code cannot know that, so it'll take it out
965 * of promiscuous mode. That's not fixable in 2.0[.x]
966 * kernels.
967 */
968 memset(&ifr, 0, sizeof(ifr));
969 strncpy(ifr.ifr_name, handle->md.device, sizeof(ifr.ifr_name));
970 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
971 fprintf(stderr,
972 "Can't restore interface flags (SIOCGIFFLAGS failed: %s).\n"
973 "Please adjust manually.\n"
974 "Hint: This can't happen with Linux >= 2.2.0.\n",
975 strerror(errno));
976 } else {
977 if (ifr.ifr_flags & IFF_PROMISC) {
978 /*
979 * Promiscuous mode is currently on; turn it
980 * off.
981 */
982 ifr.ifr_flags &= ~IFF_PROMISC;
983 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
984 fprintf(stderr,
985 "Can't restore interface flags (SIOCSIFFLAGS failed: %s).\n"
986 "Please adjust manually.\n"
987 "Hint: This can't happen with Linux >= 2.2.0.\n",
988 strerror(errno));
989 }
990 }
991 }
992
993 /*
994 * Take this pcap out of the list of pcaps for which we
995 * have to take the interface out of promiscuous mode.
996 */
997 for (p = pcaps_to_close, prevp = NULL; p != NULL;
998 prevp = p, p = p->md.next) {
999 if (p == handle) {
1000 /*
1001 * Found it. Remove it from the list.
1002 */
1003 if (prevp == NULL) {
1004 /*
1005 * It was at the head of the list.
1006 */
1007 pcaps_to_close = p->md.next;
1008 } else {
1009 /*
1010 * It was in the middle of the list.
1011 */
1012 prevp->md.next = p->md.next;
1013 }
1014 break;
1015 }
1016 }
1017 }
1018 if (handle->md.device != NULL)
1019 free(handle->md.device);
1020 }
1021
1022 /*
1023 * Try to open a packet socket using the old kernel interface.
1024 * Returns 0 on failure.
1025 * FIXME: 0 uses to mean success (Sebastian)
1026 */
1027 static int
1028 live_open_old(pcap_t *handle, char *device, int promisc,
1029 int to_ms, char *ebuf)
1030 {
1031 int sock_fd = -1, mtu, arptype;
1032 struct utsname utsname;
1033 struct ifreq ifr;
1034
1035 do {
1036 /* Open the socket */
1037
1038 sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1039 if (sock_fd == -1) {
1040 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1041 "socket: %s", pcap_strerror(errno));
1042 break;
1043 }
1044
1045 /* It worked - we are using the old interface */
1046 handle->md.sock_packet = 1;
1047
1048 /* ...which means we get the link-layer header. */
1049 handle->md.cooked = 0;
1050
1051 /* Bind to the given device */
1052
1053 if (!device) {
1054 strncpy(ebuf, "pcap_open_live: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
1055 PCAP_ERRBUF_SIZE);
1056 break;
1057 }
1058 if (iface_bind_old(sock_fd, device, ebuf) == -1)
1059 break;
1060
1061 /* Go to promisc mode */
1062 if (promisc) {
1063 memset(&ifr, 0, sizeof(ifr));
1064 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1065 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
1066 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1067 "ioctl: %s", pcap_strerror(errno));
1068 break;
1069 }
1070 if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
1071 /*
1072 * Promiscuous mode isn't currently on,
1073 * so turn it on, and remember that
1074 * we should turn it off when the
1075 * pcap_t is closed.
1076 */
1077
1078 /*
1079 * If we haven't already done so, arrange
1080 * to have "pcap_close_all()" called when
1081 * we exit.
1082 */
1083 if (!did_atexit) {
1084 if (atexit(pcap_close_all) == -1) {
1085 /*
1086 * "atexit()" failed; don't
1087 * put the interface in
1088 * promiscuous mode, just
1089 * give up.
1090 */
1091 strncpy(ebuf, "atexit failed",
1092 PCAP_ERRBUF_SIZE);
1093 break;
1094 }
1095 }
1096
1097 ifr.ifr_flags |= IFF_PROMISC;
1098 if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
1099 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1100 "ioctl: %s",
1101 pcap_strerror(errno));
1102 break;
1103 }
1104 handle->md.clear_promisc = 1;
1105
1106 /*
1107 * Add this to the list of pcaps
1108 * to close when we exit.
1109 */
1110 handle->md.next = pcaps_to_close;
1111 pcaps_to_close = handle;
1112 }
1113 }
1114
1115 /* Compute the buffersize */
1116
1117 mtu = iface_get_mtu(sock_fd, device, ebuf);
1118 if (mtu == -1)
1119 break;
1120 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
1121 if (handle->bufsize < handle->snapshot)
1122 handle->bufsize = handle->snapshot;
1123
1124 /* All done - fill in the pcap handle */
1125
1126 arptype = iface_get_arptype(sock_fd, device, ebuf);
1127 if (arptype == -1)
1128 break;
1129
1130 handle->fd = sock_fd;
1131 handle->offset = 0;
1132 handle->linktype = map_arphrd_to_dlt(arptype);
1133 /*
1134 * XXX - handle ISDN types here? We can't fall back on
1135 * cooked sockets, so we'd have to figure out from the
1136 * device name what type of link-layer encapsulation
1137 * it's using, and map that to an appropriate DLT_
1138 * value, meaning we'd map "isdnN" devices to DLT_RAW
1139 * (they supply raw IP packets with no link-layer
1140 * header) and "isdY" devices to a new DLT_I4L_IP
1141 * type that has only an Ethernet packet type as
1142 * a link-layer header.
1143 */
1144 if (handle->linktype == -1) {
1145 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1146 "interface type of %s not supported", device);
1147 break;
1148 }
1149 handle->buffer = malloc(handle->bufsize);
1150 if (!handle->buffer) {
1151 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1152 "malloc: %s", pcap_strerror(errno));
1153 break;
1154 }
1155
1156 /*
1157 * This might be a 2.0[.x] kernel - check.
1158 */
1159 if (uname(&utsname) < 0 ||
1160 strncmp(utsname.release, "2.0", 3) == 0) {
1161 /*
1162 * Either we couldn't find out what kernel release
1163 * this is, or it's a 2.0[.x] kernel.
1164 *
1165 * In the 2.0[.x] kernel, a "recvfrom()" on
1166 * a SOCK_PACKET socket, with MSG_TRUNC set, will
1167 * return the number of bytes read, so if we pass
1168 * a length based on the snapshot length, it'll
1169 * return the number of bytes from the packet
1170 * copied to userland, not the actual length
1171 * of the packet.
1172 *
1173 * This means that, for example, the IP dissector
1174 * in tcpdump will get handed a packet length less
1175 * than the length in the IP header, and will
1176 * complain about "truncated-ip".
1177 *
1178 * So we don't bother trying to copy from the
1179 * kernel only the bytes in which we're interested,
1180 * but instead copy them all, just as the older
1181 * versions of libpcap for Linux did.
1182 *
1183 * Just one of many problems with packet capture
1184 * on 2.0[.x] kernels; you really want a 2.2[.x]
1185 * or later kernel if you want packet capture to
1186 * work well.
1187 */
1188 handle->md.readlen = handle->bufsize;
1189 } else {
1190 /*
1191 * This is a 2.2[.x] or later kernel (although
1192 * why we're using SOCK_PACKET on such a system
1193 * is unknown to me).
1194 *
1195 * We can safely pass "recvfrom()" a byte count
1196 * based on the snapshot length.
1197 */
1198 handle->md.readlen = handle->snapshot;
1199 }
1200 return 1;
1201
1202 } while (0);
1203
1204 if (sock_fd != -1)
1205 close(sock_fd);
1206 return 0;
1207 }
1208
1209 /*
1210 * Bind the socket associated with FD to the given device using the
1211 * interface of the old kernels.
1212 */
1213 static int
1214 iface_bind_old(int fd, const char *device, char *ebuf)
1215 {
1216 struct sockaddr saddr;
1217
1218 memset(&saddr, 0, sizeof(saddr));
1219 strncpy(saddr.sa_data, device, sizeof(saddr.sa_data));
1220 if (bind(fd, &saddr, sizeof(saddr)) == -1) {
1221 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1222 "bind: %s", pcap_strerror(errno));
1223 return -1;
1224 }
1225
1226 return 0;
1227 }
1228
1229
1230 /* ===== System calls available on all supported kernels ============== */
1231
1232 /*
1233 * Query the kernel for the MTU of the given interface.
1234 */
1235 static int
1236 iface_get_mtu(int fd, const char *device, char *ebuf)
1237 {
1238 struct ifreq ifr;
1239
1240 if (!device)
1241 return BIGGER_THAN_ALL_MTUS;
1242
1243 memset(&ifr, 0, sizeof(ifr));
1244 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1245
1246 if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
1247 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1248 "ioctl: %s", pcap_strerror(errno));
1249 return -1;
1250 }
1251
1252 return ifr.ifr_mtu;
1253 }
1254
1255 /*
1256 * Get the hardware type of the given interface as ARPHRD_xxx constant.
1257 */
1258 static int
1259 iface_get_arptype(int fd, const char *device, char *ebuf)
1260 {
1261 struct ifreq ifr;
1262
1263 memset(&ifr, 0, sizeof(ifr));
1264 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1265
1266 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1267 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1268 "ioctl: %s", pcap_strerror(errno));
1269 return -1;
1270 }
1271
1272 return ifr.ifr_hwaddr.sa_family;
1273 }
1274
1275 #ifdef HAVE_NETPACKET_PACKET_H
1276 static int
1277 fix_program(pcap_t *handle, struct sock_fprog *fcode)
1278 {
1279 size_t prog_size;
1280 register int i;
1281 register struct bpf_insn *p;
1282 struct bpf_insn *f;
1283 int len;
1284
1285 /*
1286 * Make a copy of the filter, and modify that copy if
1287 * necessary.
1288 */
1289 prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len;
1290 len = handle->fcode.bf_len;
1291 f = (struct bpf_insn *)malloc(prog_size);
1292 if (f == NULL) {
1293 snprintf(handle->errbuf, sizeof(handle->errbuf),
1294 "malloc: %s", pcap_strerror(errno));
1295 return -1;
1296 }
1297 memcpy(f, handle->fcode.bf_insns, prog_size);
1298 fcode->len = len;
1299 fcode->filter = (struct sock_filter *) f;
1300
1301 for (i = 0; i < len; ++i) {
1302 p = &f[i];
1303 /*
1304 * What type of instruction is this?
1305 */
1306 switch (BPF_CLASS(p->code)) {
1307
1308 case BPF_RET:
1309 /*
1310 * It's a return instruction; is the snapshot
1311 * length a constant, rather than the contents
1312 * of the accumulator?
1313 */
1314 if (BPF_MODE(p->code) == BPF_K) {
1315 /*
1316 * Yes - if the value to be returned,
1317 * i.e. the snapshot length, is anything
1318 * other than 0, make it 65535, so that
1319 * the packet is truncated by "recvfrom()",
1320 * not by the filter.
1321 *
1322 * XXX - there's nothing we can easily do
1323 * if it's getting the value from the
1324 * accumulator; we'd have to insert
1325 * code to force non-zero values to be
1326 * 65535.
1327 */
1328 if (p->k != 0)
1329 p->k = 65535;
1330 }
1331 break;
1332
1333 case BPF_LD:
1334 case BPF_LDX:
1335 /*
1336 * It's a load instruction; is it loading
1337 * from the packet?
1338 */
1339 switch (BPF_MODE(p->code)) {
1340
1341 case BPF_ABS:
1342 case BPF_IND:
1343 case BPF_MSH:
1344 /*
1345 * Yes; are we in cooked mode?
1346 */
1347 if (handle->md.cooked) {
1348 /*
1349 * Yes, so we need to fix this
1350 * instruction.
1351 */
1352 if (fix_offset(p) < 0) {
1353 /*
1354 * We failed to do so.
1355 * Return 0, so our caller
1356 * knows to punt to userland.
1357 */
1358 return 0;
1359 }
1360 }
1361 break;
1362 }
1363 break;
1364 }
1365 }
1366 return 1; /* we succeeded */
1367 }
1368
1369 static int
1370 fix_offset(struct bpf_insn *p)
1371 {
1372 /*
1373 * What's the offset?
1374 */
1375 if (p->k >= SLL_HDR_LEN) {
1376 /*
1377 * It's within the link-layer payload; that starts at an
1378 * offset of 0, as far as the kernel packet filter is
1379 * concerned, so subtract the length of the link-layer
1380 * header.
1381 */
1382 p->k -= SLL_HDR_LEN;
1383 } else if (p->k == 2) {
1384 /*
1385 * It's the protocol field; map it to the special magic
1386 * kernel offset for that field.
1387 */
1388 p->k = SKF_AD_OFF + SKF_AD_PROTOCOL;
1389 } else {
1390 /*
1391 * It's within the header, but it's not one of those
1392 * fields; we can't do that in the kernel, so punt
1393 * to userland.
1394 */
1395 return -1;
1396 }
1397 return 0;
1398 }
1399 #endif