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