]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
2 * pcap-linux.c: Packet capture interface to the Linux kernel
4 * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
5 * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior
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.
28 static const char rcsid
[] =
29 "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.40 2000-11-04 07:19:23 guy Exp $ (LBL)";
33 * Known problems with 2.0[.x] kernels:
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
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'd have to catch "pcap_close()"
49 * and restore the value the promiscuous flag had when we opened
50 * the device - which may not be the value it should have, if
51 * another socket also requested promiscuous mode between the time
52 * when we opened the socket and the time when we close the socket.
53 * We currently just punt, printing a warning and hinting that the
54 * user should upgrade to a 2.2 or later kernel.
69 #include <sys/socket.h>
70 #include <sys/ioctl.h>
72 #include <netinet/in.h>
73 #include <linux/if_ether.h>
74 #include <netinet/if_ether.h>
76 #ifdef HAVE_NETPACKET_PACKET_H
77 #include <netpacket/packet.h>
79 #ifdef SO_ATTACH_FILTER
80 #include <linux/types.h>
81 #include <linux/filter.h>
85 typedef int socklen_t
;
92 #define MAX_LINKHEADER_SIZE 256
95 * When capturing on all interfaces we use this as the buffer size.
96 * Should be bigger then all MTUs that occur in real life.
97 * 64kB should be enough for now.
99 #define BIGGER_THAN_ALL_MTUS (64*1024)
102 * Prototypes for internal functions
104 static int map_arphrd_to_dlt(int arptype
);
105 static int live_open_old(pcap_t
*, char *, int, int, char *);
106 static int live_open_new(pcap_t
*, char *, int, int, char *);
107 static int pcap_read_packet(pcap_t
*, pcap_handler
, u_char
*);
110 * Wrap some ioctl calls
112 #ifdef HAVE_NETPACKET_PACKET_H
113 static int iface_get_id(int fd
, const char *device
, char *ebuf
);
115 static int iface_get_mtu(int fd
, const char *device
, char *ebuf
);
116 static int iface_get_arptype(int fd
, const char *device
, char *ebuf
);
117 #ifdef HAVE_NETPACKET_PACKET_H
118 static int iface_bind(int fd
, int ifindex
, char *ebuf
);
120 static int iface_bind_old(int fd
, const char *device
, char *ebuf
);
123 * Get a handle for a live capture from the given device. You can
124 * pass NULL as device to get all packages (without link level
125 * information of course). If you pass 1 as promisc the interface
126 * will be set to promiscous mode (XXX: I think this usage should
127 * be deprecated and functions be added to select that later allow
128 * modification of that values -- Torsten).
133 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
135 /* Allocate a handle for this session. */
137 pcap_t
*handle
= malloc(sizeof(*handle
));
138 if (handle
== NULL
) {
139 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
140 pcap_strerror(errno
));
144 /* Initialize some components of the pcap structure. */
146 memset(handle
, 0, sizeof(*handle
));
147 handle
->snapshot
= snaplen
;
148 handle
->md
.timeout
= to_ms
;
149 handle
->md
.promisc
= promisc
;
152 * NULL and "any" are special devices which give us the hint to
153 * monitor all devices.
155 if (!device
|| strcmp(device
, "any") == 0) {
157 handle
->md
.device
= strdup("any");
159 handle
->md
.device
= strdup(device
);
161 if (handle
->md
.device
== NULL
) {
162 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
163 pcap_strerror(errno
) );
169 * Current Linux kernels use the protocol family PF_PACKET to
170 * allow direct access to all packets on the network while
171 * older kernels had a special socket type SOCK_PACKET to
172 * implement this feature.
173 * While this old implementation is kind of obsolete we need
174 * to be compatible with older kernels for a while so we are
175 * trying both methods with the newer method preferred.
178 if (! (live_open_new(handle
, device
, promisc
, to_ms
, ebuf
) ||
179 live_open_old(handle
, device
, promisc
, to_ms
, ebuf
)) )
182 * Both methods to open the packet socket failed. Tidy
183 * up and report our failure (ebuf is expected to be
184 * set by the functions above).
187 free(handle
->md
.device
);
193 * Okay, now we have a packet stream open. Maybe we need to handle
194 * a timeout? In that case we set the filehandle to nonblocking
195 * so pcap_read can try reading the fd and call select if no data
196 * is available at first.
200 int flags
= fcntl(handle
->fd
, F_GETFL
);
203 flags
= fcntl(handle
->fd
, F_SETFL
, flags
);
206 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "fcntl: %s",
207 pcap_strerror(errno
));
217 * Read at most max_packets from the capture stream and call the callback
218 * for each of them. Returns the number of packets handled or -1 if an
221 * XXX: Can I rely on the Linux-specified behaviour of select (returning
222 * the time left in the timeval structure)? I really don't want to query
223 * the system time before each select call...
225 * pcap_read currently gets not only a packet from the kernel but also
226 * the sockaddr_ll returned as source of the packet. This way we can at
227 * some time extend tcpdump and libpcap to sniff on all devices at a time
228 * and find the right printing routine by using the information in the
229 * sockaddr_ll structure.
232 pcap_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
239 * Fill in a timeval structure for select if we need to obeye a
242 if (handle
->md
.timeout
> 0) {
243 tv
.tv_usec
= (handle
->md
.timeout
% 1000) * 1000;
244 tv
.tv_sec
= (handle
->md
.timeout
/ 1000);
248 * Read packets until the packet limit has been reached or
249 * an error occured while reading. Call the user function
250 * for each received packet.
252 for (packets
= 0; max_packets
== -1 || packets
< max_packets
;)
254 status
= pcap_read_packet(handle
, callback
, user
);
259 } else if (status
== -1)
263 * If no packet is available we go to sleep. FIXME: This
264 * might be better implemented using poll(?)
267 FD_SET(handle
->fd
, &read_fds
);
268 status
= select(handle
->fd
+ 1,
269 &read_fds
, NULL
, NULL
, &tv
);
274 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
275 "select: %s", pcap_strerror(errno
));
278 else if (status
== 0 ||
279 (tv
.tv_usec
== 0 && tv
.tv_sec
== 0))
287 * Read a packet from the socket calling the handler provided by
288 * the user. Returns the number of packets received or -1 if an
292 pcap_read_packet(pcap_t
*handle
, pcap_handler callback
, u_char
*userdata
)
294 #ifdef HAVE_NETPACKET_PACKET_H
295 struct sockaddr_ll from
;
297 struct sockaddr from
;
300 int packet_len
, caplen
;
301 struct pcap_pkthdr pcap_header
;
304 * We don't currently use the from return value of recvfrom but
305 * this will probably be implemented in the future.
308 /* Receive a single packet from the kernel */
311 fromlen
= sizeof(from
);
312 packet_len
= recvfrom(
313 handle
->fd
, handle
->buffer
+ handle
->offset
,
314 handle
->snapshot
, MSG_TRUNC
,
315 (struct sockaddr
*) &from
, &fromlen
);
316 } while (packet_len
== -1 && errno
== EINTR
);
318 /* Check if an error occured */
320 if (packet_len
== -1) {
322 return 0; /* no packet there */
324 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
325 "recvfrom: %s", pcap_strerror(errno
));
330 #ifdef HAVE_NETPACKET_PACKET_H
332 * If this is from the loopback device, reject outgoing packets;
333 * we'll see the packet as an incoming packet as well, and
334 * we don't want to see it twice.
336 * We can only do this if we're using PF_PACKET; the address
337 * returned for SOCK_PACKET is a "sockaddr_pkt" which lacks
338 * the relevant packet type information.
340 if (!handle
->md
.sock_packet
&&
341 from
.sll_ifindex
== handle
->md
.lo_ifindex
&&
342 from
.sll_pkttype
== PACKET_OUTGOING
)
347 * XXX: According to the kernel source we should get the real
348 * packet len if calling recvfrom with MSG_TRUNC set. It does
349 * not seem to work here :(, but it is supported by this code
351 * To be honest the code RELIES on that feature so this is really
352 * broken with 2.2.x kernels.
353 * I spend a day to figure out what's going on and I found out
354 * that the following is happening:
356 * The packet comes from a random interface and the packet_rcv
357 * hook is called with a clone of the packet. That code inserts
358 * the packet into the receive queue of the packet socket.
359 * If a filter is attached to that socket that filter is run
360 * first - and there lies the problem. The default filter always
361 * cuts the packet at the snaplen:
366 * So the packet filter cuts down the packet. The recvfrom call
367 * says "hey, it's only 68 bytes, it fits into the buffer" with
368 * the result that we don't get the real packet length. This
369 * is valid at least until kernel 2.2.17pre6.
371 * tcpdump is currently fixed by changing the BPF code generator
372 * to not truncate the received packet.
376 if (caplen
> handle
->snapshot
)
377 caplen
= handle
->snapshot
;
379 /* Run the packet filter if not using kernel filter */
380 if (!handle
->md
.use_bpf
&& handle
->fcode
.bf_insns
) {
381 if (bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
382 packet_len
, caplen
) == 0)
384 /* rejected by filter */
389 /* Fill in our own header data */
391 if (ioctl(handle
->fd
, SIOCGSTAMP
, &pcap_header
.ts
) == -1) {
392 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
393 "ioctl: %s", pcap_strerror(errno
));
396 pcap_header
.caplen
= caplen
;
397 pcap_header
.len
= packet_len
;
399 /* Call the user supplied callback function */
400 handle
->md
.stat
.ps_recv
++;
401 callback(userdata
, &pcap_header
, handle
->buffer
+ handle
->offset
);
407 * Get the statistics for the given packet capture handle.
408 * FIXME: Currently does not report the number of dropped packets.
411 pcap_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
413 *stats
= handle
->md
.stat
;
418 * Attach the given BPF code to the packet capture device.
421 pcap_setfilter(pcap_t
*handle
, struct bpf_program
*filter
)
423 #ifdef SO_ATTACH_FILTER
424 struct sock_fprog fcode
;
430 strncpy(handle
->errbuf
, "setfilter: No filter specified",
431 sizeof(handle
->errbuf
));
435 /* Make our private copy of the filter */
437 if (install_bpf_program(handle
, filter
) < 0) {
438 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
439 "malloc: %s", pcap_strerror(errno
));
444 * Run user level packet filter by default. Will be overriden if
445 * installing a kernel filter succeeds.
447 handle
->md
.use_bpf
= 0;
450 * If we're reading from a savefile, don't try to install
453 if (handle
->sf
.rfile
!= NULL
)
456 /* Install kernel level filter if possible */
458 #ifdef SO_ATTACH_FILTER
460 * Oh joy, the Linux kernel uses struct sock_fprog instead of
461 * struct bpf_program and of course the length field is of
462 * different size. Pointed out by Sebastian
465 fcode
.filter
= (struct sock_filter
*) handle
->fcode
.bf_insns
;
466 fcode
.len
= filter
->bf_len
;
468 if (filter
->bf_len
> USHRT_MAX
) {
470 * fcode.len is an unsigned short for current kernel.
471 * I have yet to see BPF-Code with that much instructions
472 * but still it is possible. So for the sake of
473 * correctness I added this check.
475 fprintf(stderr
, "Warning: Filter to complex for kernel\n");
479 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_ATTACH_FILTER
,
480 &fcode
, sizeof(fcode
)) == 0)
482 /* Installation succeded - using kernel filter. */
483 handle
->md
.use_bpf
= 1;
488 * Print a warning if kernel filter available but a problem
491 if (errno
!= ENOPROTOOPT
&& errno
!= EOPNOTSUPP
) {
492 fprintf(stderr
, "Warning: Kernel filter failed: %s\n",
493 pcap_strerror(errno
));
502 * Linux uses the ARP hardware type to identify the type of an
503 * interface. pcap uses the DLT_xxx constants for this. This
504 * function maps the ARPHRD_xxx constant to an appropriate
507 * Returns -1 if unable to map the type.
509 static int map_arphrd_to_dlt(int arptype
)
513 case ARPHRD_METRICOM
:
514 case ARPHRD_LOOPBACK
: return DLT_EN10MB
;
515 case ARPHRD_EETHER
: return DLT_EN3MB
;
516 case ARPHRD_AX25
: return DLT_AX25
;
517 case ARPHRD_PRONET
: return DLT_PRONET
;
518 case ARPHRD_CHAOS
: return DLT_CHAOS
;
519 case ARPHRD_IEEE802
: return DLT_IEEE802
;
520 case ARPHRD_ARCNET
: return DLT_ARCNET
;
521 case ARPHRD_FDDI
: return DLT_FDDI
;
523 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
524 #define ARPHRD_ATM 19
526 case ARPHRD_ATM
: return DLT_ATM_CLIP
;
533 case ARPHRD_SLIP
: return DLT_RAW
;
539 /* ===== Functions to interface to the newer kernels ================== */
542 * Try to open a packet socket using the new kernel interface.
543 * Returns 0 on failure.
544 * FIXME: 0 uses to mean success (Sebastian)
547 live_open_new(pcap_t
*handle
, char *device
, int promisc
,
548 int to_ms
, char *ebuf
)
550 #ifdef HAVE_NETPACKET_PACKET_H
551 int sock_fd
= -1, device_id
, mtu
, arptype
;
552 struct packet_mreq mr
;
554 /* One shot loop used for error handling - bail out with break */
558 * Open a socket with protocol family packet. If a device is
559 * given we try to open it in raw mode otherwise we use
560 * the cooked interface.
563 socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
))
564 : socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_ALL
));
567 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
568 pcap_strerror(errno
) );
572 /* It seems the kernel supports the new interface. */
573 handle
->md
.sock_packet
= 0;
576 * Get the interface index of the loopback device.
577 * If the attempt fails, don't fail, just set the
578 * "md.lo_ifindex" to -1.
580 * XXX - can there be more than one device that loops
581 * packets back, i.e. devices other than "lo"? If so,
582 * we'd need to find them all, and have an array of
583 * indices for them, and check all of them in
584 * "pcap_read_packet()".
586 handle
->md
.lo_ifindex
= iface_get_id(sock_fd
, "lo", ebuf
);
589 * What kind of frames do we have to deal with? Fall back
590 * to cooked mode if we have an unknown interface type.
594 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
597 handle
->linktype
= map_arphrd_to_dlt(arptype
);
598 if (handle
->linktype
== -1) {
600 * Unknown interface type - reopen in cooked
603 if (close(sock_fd
) == -1) {
604 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
605 "close: %s", pcap_strerror(errno
));
608 sock_fd
= socket(PF_PACKET
, SOCK_DGRAM
,
611 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
612 "socket: %s", pcap_strerror(errno
));
617 "Warning: arptype %d not supported by "
618 "libpcap - falling back to cooked "
621 handle
->linktype
= DLT_RAW
;
624 device_id
= iface_get_id(sock_fd
, device
, ebuf
);
628 if (iface_bind(sock_fd
, device_id
, ebuf
) == -1)
631 handle
->linktype
= DLT_RAW
;
634 * XXX - squelch GCC complaints about
635 * uninitialized variables; if we can't
636 * select promiscuous mode on all interfaces,
637 * we should move the code below into the
638 * "if (device)" branch of the "if" and
639 * get rid of the next statement.
644 /* Select promiscuous mode on/off */
648 * Hmm, how can we set promiscuous mode on all interfaces?
649 * I am not sure if that is possible at all.
653 memset(&mr
, 0, sizeof(mr
));
654 mr
.mr_ifindex
= device_id
;
655 mr
.mr_type
= promisc
?
656 PACKET_MR_PROMISC
: PACKET_MR_ALLMULTI
;
657 if (setsockopt(sock_fd
, SOL_PACKET
,
658 PACKET_ADD_MEMBERSHIP
, &mr
, sizeof(mr
)) == -1)
660 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
661 "setsockopt: %s", pcap_strerror(errno
));
667 /* Compute the buffersize */
669 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
672 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
674 /* Fill in the pcap structure */
676 handle
->fd
= sock_fd
;
679 handle
->buffer
= malloc(handle
->bufsize
);
680 if (!handle
->buffer
) {
681 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
682 "malloc: %s", pcap_strerror(errno
));
695 "New packet capturing interface not supported by build "
696 "environment", PCAP_ERRBUF_SIZE
);
701 #ifdef HAVE_NETPACKET_PACKET_H
703 * Return the index of the given device name. Fill ebuf and return
707 iface_get_id(int fd
, const char *device
, char *ebuf
)
711 memset(&ifr
, 0, sizeof(ifr
));
712 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
714 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) == -1) {
715 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
716 "ioctl: %s", pcap_strerror(errno
));
720 return ifr
.ifr_ifindex
;
724 * Bind the socket associated with FD to the given device.
727 iface_bind(int fd
, int ifindex
, char *ebuf
)
729 struct sockaddr_ll sll
;
731 memset(&sll
, 0, sizeof(sll
));
732 sll
.sll_family
= AF_PACKET
;
733 sll
.sll_ifindex
= ifindex
;
734 sll
.sll_protocol
= htons(ETH_P_ALL
);
736 if (bind(fd
, (struct sockaddr
*) &sll
, sizeof(sll
)) == -1) {
737 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
738 "bind: %s", pcap_strerror(errno
));
748 /* ===== Functions to interface to the older kernels ================== */
751 * With older kernels promiscuous mode is kind of interesting because we
752 * have to reset the interface before exiting. The problem can't really
753 * be solved without some daemon taking care of managing usage counts.
754 * We save the promiscuous state of the device when opening the capture
755 * stream and arrange for it to be reset on process exit.
757 * XXX: This solution is still not correct even for this case. The
758 * devices stay in promiscuous mode until the process exits. I need to
759 * modify pcap_close to solve this.
763 * The device name and the interface flags to be restored at exit
765 struct ifreq restore_ifr
;
767 static void restore_interface( void )
769 int status
= socket(PF_INET
, SOCK_PACKET
, 0);
772 status
= ioctl(status
, SIOCSIFFLAGS
, &restore_ifr
);
776 "Can't restore interface flags. Please adjust manually. \n"
777 "Hint: This can't happen with Linux >= 2.2.0.\n");
782 * Try to open a packet socket using the old kernel interface.
783 * Returns 0 on failure.
784 * FIXME: 0 uses to mean success (Sebastian)
787 live_open_old(pcap_t
*handle
, char *device
, int promisc
,
788 int to_ms
, char *ebuf
)
790 int sock_fd
= -1, mtu
, arptype
;
794 /* Open the socket */
796 sock_fd
= socket(PF_INET
, SOCK_PACKET
, htons(ETH_P_ALL
));
798 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
799 "socket: %s", pcap_strerror(errno
));
803 /* It worked - we are using the old interface */
804 handle
->md
.sock_packet
= 1;
806 /* Bind to the given device */
809 strncpy(ebuf
, "pcap_open_live: No interface given",
813 if (iface_bind_old(sock_fd
, device
, ebuf
) == -1)
816 /* Go to promisc mode */
818 memset(&ifr
, 0, sizeof(ifr
));
819 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
820 if (ioctl(sock_fd
, SIOCGIFFLAGS
, &ifr
) == -1) {
821 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
822 "ioctl: %s", pcap_strerror(errno
));
825 if ((ifr
.ifr_flags
& IFF_PROMISC
) == 0) {
827 ifr
.ifr_flags
|= IFF_PROMISC
;
828 if (ioctl(sock_fd
, SIOCSIFFLAGS
, &ifr
) == -1) {
829 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
831 pcap_strerror(errno
));
834 if (atexit(restore_interface
) == -1) {
836 strncpy(ebuf
, "atexit failed",
843 /* Compute the buffersize */
845 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
848 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
849 if (handle
->bufsize
< handle
->snapshot
)
850 handle
->bufsize
= handle
->snapshot
;
852 /* All done - fill in the pcap handle */
854 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
858 handle
->fd
= sock_fd
;
860 handle
->linktype
= map_arphrd_to_dlt(arptype
);
861 if (handle
->linktype
== -1) {
862 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
863 "interface type of %s not supported", device
);
866 handle
->buffer
= malloc(handle
->bufsize
);
867 if (!handle
->buffer
) {
868 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
869 "malloc: %s", pcap_strerror(errno
));
883 * Bind the socket associated with FD to the given device using the
884 * interface of the old kernels.
887 iface_bind_old(int fd
, const char *device
, char *ebuf
)
889 struct sockaddr saddr
;
891 memset(&saddr
, 0, sizeof(saddr
));
892 strncpy(saddr
.sa_data
, device
, sizeof(saddr
.sa_data
));
893 if (bind(fd
, &saddr
, sizeof(saddr
)) == -1) {
894 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
895 "bind: %s", pcap_strerror(errno
));
903 /* ===== System calls available on all supported kernels ============== */
906 * Query the kernel for the MTU of the given interface.
909 iface_get_mtu(int fd
, const char *device
, char *ebuf
)
914 return BIGGER_THAN_ALL_MTUS
;
916 memset(&ifr
, 0, sizeof(ifr
));
917 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
919 if (ioctl(fd
, SIOCGIFMTU
, &ifr
) == -1) {
920 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
921 "ioctl: %s", pcap_strerror(errno
));
929 * Get the hardware type of the given interface as ARPHRD_xxx constant.
932 iface_get_arptype(int fd
, const char *device
, char *ebuf
)
936 memset(&ifr
, 0, sizeof(ifr
));
937 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
939 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) == -1) {
940 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
941 "ioctl: %s", pcap_strerror(errno
));
945 return ifr
.ifr_hwaddr
.sa_family
;