]> The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
pcap_read: Check the error code of select against EINTR. Reported by
[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.33 2000-10-18 23:47:41 torsten Exp $ (LBL)";
30 #endif
31
32 /*
33 * Known bugs:
34 * - setting promiscuous on loopback gives every packet twice
35 */
36
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include "pcap-int.h"
43
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <string.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <linux/if_ether.h>
54 #include <netinet/if_ether.h>
55
56 #ifdef HAVE_NETPACKET_PACKET_H
57 #include <netpacket/packet.h>
58 #endif
59 #ifdef SO_ATTACH_FILTER
60 #include <linux/types.h>
61 #include <linux/filter.h>
62 #endif
63
64 #ifndef __GLIBC__
65 typedef int socklen_t;
66 #endif
67
68 #ifndef MSG_TRUNC
69 #define MSG_TRUNC 0
70 #endif
71
72 #define MAX_LINKHEADER_SIZE 256
73
74 /*
75 * When capturing on all interfaces we use this as the buffer size.
76 * Should be bigger then all MTUs that occur in real life.
77 * 64kB should be enough for now.
78 */
79 #define BIGGER_THAN_ALL_MTUS (64*1024)
80
81 /*
82 * Prototypes for internal functions
83 */
84 static int map_arphrd_to_dlt(int arptype );
85 static int live_open_old(pcap_t *, char *, int, int, char *);
86 static int live_open_new(pcap_t *, char *, int, int, char *);
87 static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
88
89 /*
90 * Wrap some ioctl calls
91 */
92 static int iface_get_id(int fd, const char *device, char *ebuf);
93 static int iface_get_mtu(int fd, const char *device, char *ebuf);
94 static int iface_get_arptype(int fd, const char *device, char *ebuf);
95 static int iface_bind(int fd, int ifindex, char *ebuf);
96 static int iface_bind_old(int fd, const char *device, char *ebuf);
97
98 /*
99 * Get a handle for a live capture from the given device. You can
100 * pass NULL as device to get all packages (without link level
101 * information of course). If you pass 1 as promisc the interface
102 * will be set to promiscous mode (XXX: I think this usage should
103 * be deprecated and functions be added to select that later allow
104 * modification of that values -- Torsten).
105 *
106 * See also pcap(3).
107 */
108 pcap_t *
109 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
110 {
111 /* Allocate a handle for this session. */
112
113 pcap_t *handle = malloc(sizeof(*handle));
114 if (handle == NULL) {
115 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
116 pcap_strerror(errno));
117 return NULL;
118 }
119
120 /* Initialize some components of the pcap structure. */
121
122 memset(handle, 0, sizeof(*handle));
123 handle->snapshot = snaplen;
124 handle->md.timeout = to_ms;
125 handle->md.promisc = promisc;
126
127 /*
128 * NULL and "any" are special devices which give us the hint to
129 * monitor all devices.
130 */
131 if (!device || strcmp(device, "any") == 0) {
132 device = NULL;
133 handle->md.device = strdup("any");
134 } else
135 handle->md.device = strdup(device);
136
137 if (handle->md.device == NULL) {
138 snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s",
139 pcap_strerror(errno) );
140 free(handle);
141 return NULL;
142 }
143
144 /*
145 * Current Linux kernels use the protocol family PF_PACKET to
146 * allow direct access to all packets on the network while
147 * older kernels had a special socket type SOCK_PACKET to
148 * implement this feature.
149 * While this old implementation is kind of obsolete we need
150 * to be compatible with older kernels for a while so we are
151 * trying both methods with the newer method preferred.
152 */
153
154 if (! (live_open_new(handle, device, promisc, to_ms, ebuf) ||
155 live_open_old(handle, device, promisc, to_ms, ebuf)) )
156 {
157 /*
158 * Both methods to open the packet socket failed. Tidy
159 * up and report our failure (ebuf is expected to be
160 * set by the functions above).
161 */
162
163 free(handle->md.device);
164 free(handle);
165 return NULL;
166 }
167
168 /*
169 * Okay, now we have a packet stream open. Maybe we need to handle
170 * a timeout? In that case we set the filehandle to nonblocking
171 * so pcap_read can try reading the fd and call select if no data
172 * is available at first.
173 */
174
175 if (to_ms > 0) {
176 int flags = fcntl(handle->fd, F_GETFL);
177 if (flags != -1) {
178 flags |= O_NONBLOCK;
179 flags = fcntl(handle->fd, F_SETFL, flags);
180 }
181 if (flags == -1) {
182 snprintf(ebuf, PCAP_ERRBUF_SIZE, "fcntl: %s",
183 pcap_strerror(errno));
184 pcap_close(handle);
185 return NULL;
186 }
187 }
188
189 return handle;
190 }
191
192 /*
193 * Read at most max_packets from the capture stream and call the callback
194 * for each of them. Returns the number of packets handled or -1 if an
195 * error occured.
196 *
197 * XXX: Can I rely on the Linux-specified behaviour of select (returning
198 * the time left in the timeval structure)? I really don't want to query
199 * the system time before each select call...
200 *
201 * pcap_read currently gets not only a packet from the kernel but also
202 * the sockaddr_ll returned as source of the packet. This way we can at
203 * some time extend tcpdump and libpcap to sniff on all devices at a time
204 * and find the right printing routine by using the information in the
205 * sockaddr_ll structure.
206 */
207 int
208 pcap_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
209 {
210 int status, packets;
211 fd_set read_fds;
212 struct timeval tv;
213
214 /*
215 * Fill in a timeval structure for select if we need to obeye a
216 * timeout.
217 */
218 if (handle->md.timeout > 0) {
219 tv.tv_usec = (handle->md.timeout % 1000) * 1000;
220 tv.tv_sec = (handle->md.timeout / 1000);
221 }
222
223 /*
224 * Read packets until the packet limit has been reached or
225 * an error occured while reading. Call the user function
226 * for each received packet.
227 */
228 for (packets = 0; max_packets == -1 || packets < max_packets;)
229 {
230 status = pcap_read_packet(handle, callback, user);
231
232 if (status > 0) {
233 packets += status;
234 continue;
235 } else if (status == -1)
236 return -1;
237
238 /*
239 * If no packet is available we go to sleep. FIXME: This
240 * might be better implemented using poll(?)
241 */
242 FD_ZERO(&read_fds);
243 FD_SET(handle->fd, &read_fds);
244 do {
245 status = select(handle->fd + 1,
246 &read_fds, NULL, NULL, &tv);
247 } while (status == -1 && errno == EINTR);
248
249 if (status == -1) {
250 snprintf(handle->errbuf, sizeof(handle->errbuf),
251 "select: %s", pcap_strerror(errno));
252 return -1;
253 } else if (status == 0 ||
254 (tv.tv_usec == 0 && tv.tv_sec == 0))
255 return packets;
256 }
257
258 return packets;
259 }
260
261 /*
262 * Read a packet from the socket calling the handler provided by
263 * the user. Returns the number of packets received or -1 if an
264 * error occured.
265 */
266 static int
267 pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
268 {
269 struct sockaddr from;
270 socklen_t fromlen;
271 int packet_len, caplen;
272 struct pcap_pkthdr pcap_header;
273
274 /*
275 * We don't currently use the from return value of recvfrom but
276 * this will probably be implemented in the future.
277 */
278
279 /* Receive a single packet from the kernel */
280
281 do {
282 fromlen = sizeof(from);
283 packet_len = recvfrom(
284 handle->fd, handle->buffer + handle->offset,
285 handle->snapshot, MSG_TRUNC,
286 (struct sockaddr *) &from, &fromlen);
287 } while (packet_len == -1 && errno == EINTR);
288
289 /* Check if an error occured */
290
291 if (packet_len == -1) {
292 if (errno == EAGAIN)
293 return 0; /* no packet there */
294 else {
295 snprintf(handle->errbuf, sizeof(handle->errbuf),
296 "recvfrom: %s", pcap_strerror(errno));
297 return -1;
298 }
299 }
300
301 /*
302 * XXX: According to the kernel source we should get the real
303 * packet len if calling recvfrom with MSG_TRUNC set. It does
304 * not seem to work here :(, but it is supported by this code
305 * anyway.
306 * To be honest the code RELIES on that feature so this is really
307 * broken with 2.2.x kernels.
308 * I spend a day to figure out what's going on and I found out
309 * that the following is happening:
310 *
311 * The packet comes from a random interface and the packet_rcv
312 * hook is called with a clone of the packet. That code inserts
313 * the packet into the receive queue of the packet socket.
314 * If a filter is attached to that socket that filter is run
315 * first - and there lies the problem. The default filter always
316 * cuts the packet at the snaplen:
317 *
318 * # tcpdump -d
319 * (000) ret #68
320 *
321 * So the packet filter cuts down the packet. The recvfrom call
322 * says "hey, it's only 68 bytes, it fits into the buffer" with
323 * the result that we don't get the real packet length. This
324 * is valid at least until kernel 2.2.17pre6.
325 *
326 * tcpdump is currently fixed by changing the BPF code generator
327 * to not truncate the received packet.
328 */
329
330 caplen = packet_len;
331 if (caplen > handle->snapshot)
332 caplen = handle->snapshot;
333
334 /* Run the packet filter if not using kernel filter */
335 if (!handle->md.use_bpf && handle->fcode.bf_insns) {
336 if (bpf_filter(handle->fcode.bf_insns, handle->buffer,
337 packet_len, caplen) == 0)
338 {
339 /* rejected by filter */
340 return 0;
341 }
342 }
343
344 /* Fill in our own header data */
345
346 if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
347 snprintf(handle->errbuf, sizeof(handle->errbuf),
348 "ioctl: %s", pcap_strerror(errno));
349 return -1;
350 }
351 pcap_header.caplen = caplen;
352 pcap_header.len = packet_len;
353
354 /* Call the user supplied callback function */
355 handle->md.stat.ps_recv++;
356 callback(userdata, &pcap_header, handle->buffer + handle->offset);
357
358 return 1;
359 }
360
361 /*
362 * Get the statistics for the given packet capture handle.
363 * FIXME: Currently does not report the number of dropped packets.
364 */
365 int
366 pcap_stats(pcap_t *handle, struct pcap_stat *stats)
367 {
368 *stats = handle->md.stat;
369 return 0;
370 }
371
372 /*
373 * Attach the given BPF code to the packet capture device.
374 */
375 int
376 pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
377 {
378 #ifdef SO_ATTACH_FILTER
379 struct sock_fprog fcode;
380 #endif
381
382 if (!handle)
383 return -1;
384 if (!filter) {
385 strncpy(handle->errbuf, "setfilter: No filter specified",
386 sizeof(handle->errbuf));
387 return -1;
388 }
389
390 /* Free old filter code if existing */
391
392 handle->fcode.bf_len = 0;
393 if (handle->fcode.bf_insns) {
394 free(handle->fcode.bf_insns);
395 handle->fcode.bf_insns = NULL;
396 }
397
398
399 /* Make our private copy of the filter */
400
401 handle->fcode.bf_len = filter->bf_len;
402 handle->fcode.bf_insns =
403 malloc(filter->bf_len * sizeof(*filter->bf_insns));
404 if (handle->fcode.bf_insns == NULL) {
405 snprintf(handle->errbuf, sizeof(handle->errbuf),
406 "malloc: %s", pcap_strerror(errno));
407 return -1;
408 }
409 memcpy(handle->fcode.bf_insns, filter->bf_insns,
410 filter->bf_len * sizeof(*filter->bf_insns));
411
412 /*
413 * Run user level packet filter by default. Will be overriden if
414 * installing a kernel filter succeeds.
415 */
416 handle->md.use_bpf = 0;
417
418 /* Install kernel level filter if possible */
419
420 #ifdef SO_ATTACH_FILTER
421 /*
422 * Oh joy, the Linux kernel uses struct sock_fprog instead of
423 * struct bpf_program and of course the length field is of
424 * different size. Pointed out by Sebastian
425 */
426
427 fcode.filter = (struct sock_filter *) handle->fcode.bf_insns;
428 fcode.len = filter->bf_len;
429 #ifdef USHRT_MAX
430 if (filter->bf_len > USHRT_MAX) {
431 /*
432 * fcode.len is an unsigned short for current kernel.
433 * I have yet to see BPF-Code with that much instructions
434 * but still it is possible. So for the sake of
435 * correctness I added this check.
436 */
437 fprintf(stderr, "Warning: Filter to complex for kernel\n");
438 }
439 else
440 #endif
441 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
442 &fcode, sizeof(fcode)) == 0)
443 {
444 /* Installation succeded - using kernel filter. */
445 handle->md.use_bpf = 1;
446 }
447 else
448 {
449 /*
450 * Print a warning if kernel filter available but a problem
451 * occured using it.
452 */
453 if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) {
454 fprintf(stderr, "Warning: Kernel filter failed: %s\n",
455 pcap_strerror(errno));
456 }
457 }
458 #endif
459
460 return 0;
461 }
462
463 /*
464 * Linux uses the ARP hardware type to identify the type of an
465 * interface. pcap uses the DLT_xxx constants for this. This
466 * function maps the ARPHRD_xxx constant to an appropriate
467 * DLT_xxx constant.
468 *
469 * Returns -1 if unable to map the type.
470 */
471 static int map_arphrd_to_dlt(int arptype)
472 {
473 switch (arptype) {
474 case ARPHRD_ETHER:
475 case ARPHRD_METRICOM:
476 case ARPHRD_LOOPBACK: return DLT_EN10MB;
477 case ARPHRD_EETHER: return DLT_EN3MB;
478 case ARPHRD_AX25: return DLT_AX25;
479 case ARPHRD_PRONET: return DLT_PRONET;
480 case ARPHRD_CHAOS: return DLT_CHAOS;
481 case ARPHRD_IEEE802: return DLT_IEEE802;
482 case ARPHRD_ARCNET: return DLT_ARCNET;
483 case ARPHRD_FDDI: return DLT_FDDI;
484
485 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
486 #define ARPHRD_ATM 19
487 #endif
488 case ARPHRD_ATM: return DLT_ATM_CLIP;
489
490 case ARPHRD_PPP:
491 case ARPHRD_CSLIP:
492 case ARPHRD_SLIP6:
493 case ARPHRD_CSLIP6:
494 case ARPHRD_SLIP: return DLT_RAW;
495 }
496
497 return -1;
498 }
499
500 /* ===== Functions to interface to the newer kernels ================== */
501
502 /*
503 * Try to open a packet socket using the new kernel interface.
504 * Returns 0 on failure.
505 * FIXME: 0 uses to mean success (Sebastian)
506 */
507 static int
508 live_open_new(pcap_t *handle, char *device, int promisc,
509 int to_ms, char *ebuf)
510 {
511 #ifdef HAVE_NETPACKET_PACKET_H
512 int sock_fd = -1, device_id, mtu, arptype;
513 struct packet_mreq mr;
514
515 /* One shot loop used for error handling - bail out with break */
516
517 do {
518 /*
519 * Open a socket with protocol family packet. If a device is
520 * given we try to open it in raw mode otherwise we use
521 * the cooked interface.
522 */
523 sock_fd = device ?
524 socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
525 : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
526
527 if (sock_fd == -1) {
528 snprintf(ebuf, PCAP_ERRBUF_SIZE, "socket: %s",
529 pcap_strerror(errno) );
530 break;
531 }
532
533 /* It seems the kernel supports the new interface. */
534 handle->md.sock_packet = 0;
535
536 /*
537 * What kind of frames do we have to deal with? Fall back
538 * to cooked mode if we have an unknown interface type.
539 */
540
541 if (device) {
542 arptype = iface_get_arptype(sock_fd, device, ebuf);
543 if (arptype == -1)
544 break;
545 handle->linktype = map_arphrd_to_dlt(arptype);
546 } else
547 handle->linktype = DLT_RAW;
548
549 if (handle->linktype == -1) {
550 /* Unknown interface type - reopen in cooked mode */
551
552 if (close(sock_fd) == -1) {
553 snprintf(ebuf, PCAP_ERRBUF_SIZE,
554 "close: %s", pcap_strerror(errno));
555 break;
556 }
557 sock_fd = socket(PF_PACKET, SOCK_DGRAM,
558 htons(ETH_P_ALL));
559 if (sock_fd == -1) {
560 snprintf(ebuf, PCAP_ERRBUF_SIZE,
561 "socket: %s", pcap_strerror(errno));
562 break;
563 }
564
565 fprintf(stderr,
566 "Warning: Falling back to cooked socket\n");
567 handle->linktype = DLT_RAW;
568 }
569
570
571 if (device) {
572 device_id = iface_get_id(sock_fd, device, ebuf);
573 if (device_id == -1)
574 break;
575
576 if (iface_bind(sock_fd, device_id, ebuf) == -1)
577 break;
578 }
579
580 /* Select promiscous mode on/off */
581
582 #ifdef SOL_PACKET
583 /*
584 * Hmm, how can we set promiscuous mode on all interfaces?
585 * I am not sure if that is possible at all.
586 */
587
588 if (device) {
589 memset(&mr, 0, sizeof(mr));
590 mr.mr_ifindex = device_id;
591 mr.mr_type = promisc ?
592 PACKET_MR_PROMISC : PACKET_MR_ALLMULTI;
593 if (setsockopt(sock_fd, SOL_PACKET,
594 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
595 {
596 snprintf(ebuf, PCAP_ERRBUF_SIZE,
597 "setsockopt: %s", pcap_strerror(errno));
598 break;
599 }
600 }
601 #endif
602
603 /* Compute the buffersize */
604
605 mtu = iface_get_mtu(sock_fd, device, ebuf);
606 if (mtu == -1)
607 break;
608 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
609
610 /* Fill in the pcap structure */
611
612 handle->fd = sock_fd;
613 handle->offset = 0;
614
615 handle->buffer = malloc(handle->bufsize);
616 if (!handle->buffer) {
617 snprintf(ebuf, PCAP_ERRBUF_SIZE,
618 "malloc: %s", pcap_strerror(errno));
619 break;
620 }
621
622 return 1;
623
624 } while(0);
625
626 if (sock_fd != -1)
627 close(sock_fd);
628 return 0;
629 #else
630 strncpy(ebuf,
631 "New packet capturing interface not supported by build "
632 "environment", PCAP_ERRBUF_SIZE);
633 return 0;
634 #endif
635 }
636
637 #ifdef HAVE_NETPACKET_PACKET_H
638 /*
639 * Return the index of the given device name. Fill ebuf and return
640 * -1 on failure.
641 */
642 static int
643 iface_get_id(int fd, const char *device, char *ebuf)
644 {
645 struct ifreq ifr;
646
647 memset(&ifr, 0, sizeof(ifr));
648 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
649
650 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
651 snprintf(ebuf, PCAP_ERRBUF_SIZE,
652 "ioctl: %s", pcap_strerror(errno));
653 return -1;
654 }
655
656 return ifr.ifr_ifindex;
657 }
658
659 /*
660 * Bind the socket associated with FD to the given device.
661 */
662 static int
663 iface_bind(int fd, int ifindex, char *ebuf)
664 {
665 struct sockaddr_ll sll;
666
667 memset(&sll, 0, sizeof(sll));
668 sll.sll_family = AF_PACKET;
669 sll.sll_ifindex = ifindex;
670 sll.sll_protocol = htons(ETH_P_ALL);
671
672 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
673 snprintf(ebuf, PCAP_ERRBUF_SIZE,
674 "bind: %s", pcap_strerror(errno));
675 return -1;
676 }
677
678 return 0;
679 }
680
681 #endif
682
683
684 /* ===== Functions to interface to the older kernels ================== */
685
686 /*
687 * With older kernels promiscuous mode is kind of interesting because we
688 * have to reset the interface before exiting. The problem can't really
689 * be solved without some daemon taking care of managing usage counts.
690 * We save the promiscuous state of the device when opening the capture
691 * stream and arrange for it to be reset on process exit.
692 *
693 * XXX: This solution is still not correct even for this case. The
694 * devices stay in promiscuous mode until the process exits. I need to
695 * modify pcap_close to solve this.
696 */
697
698 /*
699 * The device name and the interface flags to be restored at exit
700 */
701 struct ifreq restore_ifr;
702
703 static void restore_interface( void )
704 {
705 int status = socket(PF_INET, SOCK_PACKET, 0);
706
707 if (status != -1)
708 status = ioctl(status, SIOCSIFFLAGS, &restore_ifr);
709
710 if (status == -1) {
711 fprintf(stderr,
712 "Can't restore interface flags. Please adjust manually. \n"
713 "Hint: This can't happen with Linux >= 2.2.0.\n");
714 }
715 }
716
717 /*
718 * Try to open a packet socket using the old kernel interface.
719 * Returns 0 on failure.
720 * FIXME: 0 uses to mean success (Sebastian)
721 */
722 static int
723 live_open_old(pcap_t *handle, char *device, int promisc,
724 int to_ms, char *ebuf)
725 {
726 int sock_fd = -1, mtu, arptype;
727 struct ifreq ifr;
728
729 do {
730 /* Open the socket */
731
732 sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
733 if (sock_fd == -1) {
734 snprintf(ebuf, PCAP_ERRBUF_SIZE,
735 "socket: %s", pcap_strerror(errno));
736 break;
737 }
738
739 /* It worked - we are using the old interface */
740 handle->md.sock_packet = 1;
741
742 /* Bind to the given device */
743
744 if (!device) {
745 strncpy(ebuf, "pcap_open_live: No interface given",
746 PCAP_ERRBUF_SIZE);
747 break;
748 }
749 if (iface_bind_old(sock_fd, device, ebuf) == -1)
750 break;
751
752 /* Go to promisc mode */
753 if (promisc) {
754 memset(&ifr, 0, sizeof(ifr));
755 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
756 if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
757 snprintf(ebuf, PCAP_ERRBUF_SIZE,
758 "ioctl: %s", pcap_strerror(errno));
759 break;
760 }
761 if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
762 restore_ifr = ifr;
763 ifr.ifr_flags |= IFF_PROMISC;
764 if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
765 snprintf(ebuf, PCAP_ERRBUF_SIZE,
766 "ioctl: %s",
767 pcap_strerror(errno));
768 break;
769 }
770 if (atexit(restore_interface) == -1) {
771 restore_interface();
772 strncpy(ebuf, "atexit failed",
773 PCAP_ERRBUF_SIZE);
774 break;
775 }
776 }
777 }
778
779
780 /* Compute the buffersize */
781
782 mtu = iface_get_mtu(sock_fd, device, ebuf);
783 if (mtu == -1)
784 break;
785 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
786
787 /* All done - fill in the pcap handle */
788
789 arptype = iface_get_arptype(sock_fd, device, ebuf);
790 if (arptype == -1)
791 break;
792
793 handle->fd = sock_fd;
794 handle->offset = 0;
795 handle->linktype = map_arphrd_to_dlt(arptype);
796 if (handle->linktype == -1) {
797 snprintf(ebuf, PCAP_ERRBUF_SIZE,
798 "interface type of %s not supported", device);
799 break;
800 }
801 handle->buffer = malloc(handle->bufsize);
802 if (!handle->buffer) {
803 snprintf(ebuf, PCAP_ERRBUF_SIZE,
804 "malloc: %s", pcap_strerror(errno));
805 break;
806 }
807
808 return 1;
809
810 } while (0);
811
812 if (sock_fd != -1)
813 close(sock_fd);
814 return 0;
815 }
816
817 /*
818 * Bind the socket associated with FD to the given device using the
819 * interface of the old kernels.
820 */
821 static int
822 iface_bind_old(int fd, const char *device, char *ebuf)
823 {
824 struct sockaddr saddr;
825
826 memset(&saddr, 0, sizeof(saddr));
827 strncpy(saddr.sa_data, device, sizeof(saddr.sa_data));
828 if (bind(fd, &saddr, sizeof(saddr)) == -1) {
829 snprintf(ebuf, PCAP_ERRBUF_SIZE,
830 "bind: %s", pcap_strerror(errno));
831 return -1;
832 }
833
834 return 0;
835 }
836
837
838 /* ===== System calls available on all supported kernels ============== */
839
840 /*
841 * Query the kernel for the MTU of the given interface.
842 */
843 static int
844 iface_get_mtu(int fd, const char *device, char *ebuf)
845 {
846 struct ifreq ifr;
847
848 if (!device)
849 return BIGGER_THAN_ALL_MTUS;
850
851 memset(&ifr, 0, sizeof(ifr));
852 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
853
854 if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
855 snprintf(ebuf, PCAP_ERRBUF_SIZE,
856 "ioctl: %s", pcap_strerror(errno));
857 return -1;
858 }
859
860 return ifr.ifr_mtu;
861 }
862
863 /*
864 * Get the hardware type of the given interface as ARPHRD_xxx constant.
865 */
866 static int
867 iface_get_arptype(int fd, const char *device, char *ebuf)
868 {
869 struct ifreq ifr;
870
871 memset(&ifr, 0, sizeof(ifr));
872 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
873
874 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
875 snprintf(ebuf, PCAP_ERRBUF_SIZE,
876 "ioctl: %s", pcap_strerror(errno));
877 return -1;
878 }
879
880 return ifr.ifr_hwaddr.sa_family;
881 }