pcap-linux.c revision 146768
1123992Ssobomax/* 2103026Ssobomax * pcap-linux.c: Packet capture interface to the Linux kernel 3103026Ssobomax * 4103026Ssobomax * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org> 5103026Ssobomax * Sebastian Krahmer <krahmer@cs.uni-potsdam.de> 6103026Ssobomax * 7103026Ssobomax * License: BSD 8103026Ssobomax * 9103026Ssobomax * Redistribution and use in source and binary forms, with or without 10103026Ssobomax * modification, are permitted provided that the following conditions 11103026Ssobomax * are met: 12103026Ssobomax * 13103026Ssobomax * 1. Redistributions of source code must retain the above copyright 14103026Ssobomax * notice, this list of conditions and the following disclaimer. 15103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright 16103026Ssobomax * notice, this list of conditions and the following disclaimer in 17103026Ssobomax * the documentation and/or other materials provided with the 18103026Ssobomax * distribution. 19103026Ssobomax * 3. The names of the authors may not be used to endorse or promote 20103026Ssobomax * products derived from this software without specific prior 21103026Ssobomax * written permission. 22103026Ssobomax * 23103026Ssobomax * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 24103026Ssobomax * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 25103026Ssobomax * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 26103026Ssobomax */ 27103026Ssobomax 28103026Ssobomax#ifndef lint 29103026Ssobomaxstatic const char rcsid[] _U_ = 30103026Ssobomax "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.110 2004/10/19 07:06:12 guy Exp $ (LBL)"; 31103026Ssobomax#endif 32103026Ssobomax 33103026Ssobomax/* 34103026Ssobomax * Known problems with 2.0[.x] kernels: 35103026Ssobomax * 36103026Ssobomax * - The loopback device gives every packet twice; on 2.2[.x] kernels, 37103026Ssobomax * if we use PF_PACKET, we can filter out the transmitted version 38103026Ssobomax * of the packet by using data in the "sockaddr_ll" returned by 39103026Ssobomax * "recvfrom()", but, on 2.0[.x] kernels, we have to use 40103026Ssobomax * PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a 41103026Ssobomax * "sockaddr_pkt" which doesn't give us enough information to let 42103026Ssobomax * us do that. 43103026Ssobomax * 44103026Ssobomax * - We have to set the interface's IFF_PROMISC flag ourselves, if 45103026Ssobomax * we're to run in promiscuous mode, which means we have to turn 46103026Ssobomax * it off ourselves when we're done; the kernel doesn't keep track 47103026Ssobomax * of how many sockets are listening promiscuously, which means 48103026Ssobomax * it won't get turned off automatically when no sockets are 49103394Sbde * listening promiscuously. We catch "pcap_close()" and, for 50103026Ssobomax * interfaces we put into promiscuous mode, take them out of 51122699Sbms * promiscuous mode - which isn't necessarily the right thing to 52103026Ssobomax * do, if another socket also requested promiscuous mode between 53103026Ssobomax * the time when we opened the socket and the time when we close 54103026Ssobomax * the socket. 55103026Ssobomax * 56103026Ssobomax * - MSG_TRUNC isn't supported, so you can't specify that "recvfrom()" 57103026Ssobomax * return the amount of data that you could have read, rather than 58103026Ssobomax * the amount that was returned, so we can't just allocate a buffer 59103026Ssobomax * whose size is the snapshot length and pass the snapshot length 60103026Ssobomax * as the byte count, and also pass MSG_TRUNC, so that the return 61103344Sbde * value tells us how long the packet was on the wire. 62103026Ssobomax * 63103026Ssobomax * This means that, if we want to get the actual size of the packet, 64103026Ssobomax * so we can return it in the "len" field of the packet header, 65103026Ssobomax * we have to read the entire packet, not just the part that fits 66103026Ssobomax * within the snapshot length, and thus waste CPU time copying data 67103026Ssobomax * from the kernel that our caller won't see. 68103026Ssobomax * 69103026Ssobomax * We have to get the actual size, and supply it in "len", because 70103026Ssobomax * otherwise, the IP dissector in tcpdump, for example, will complain 71103026Ssobomax * about "truncated-ip", as the packet will appear to have been 72103026Ssobomax * shorter, on the wire, than the IP header said it should have been. 73103026Ssobomax */ 74103026Ssobomax 75103026Ssobomax 76103026Ssobomax#ifdef HAVE_CONFIG_H 77103026Ssobomax#include "config.h" 78103026Ssobomax#endif 79103026Ssobomax 80103026Ssobomax#include "pcap-int.h" 81103026Ssobomax#include "sll.h" 82103026Ssobomax 83103026Ssobomax#ifdef HAVE_DAG_API 84103026Ssobomax#include "pcap-dag.h" 85103026Ssobomax#endif /* HAVE_DAG_API */ 86103026Ssobomax 87103026Ssobomax#include <errno.h> 88103026Ssobomax#include <stdlib.h> 89103026Ssobomax#include <unistd.h> 90103026Ssobomax#include <fcntl.h> 91103026Ssobomax#include <string.h> 92103026Ssobomax#include <sys/socket.h> 93103026Ssobomax#include <sys/ioctl.h> 94103026Ssobomax#include <sys/utsname.h> 95103026Ssobomax#include <net/if.h> 96103026Ssobomax#include <netinet/in.h> 97103026Ssobomax#include <linux/if_ether.h> 98105300Salfred#include <net/if_arp.h> 99105300Salfred 100103032Ssobomax/* 101103032Ssobomax * If PF_PACKET is defined, we can use {SOCK_RAW,SOCK_DGRAM}/PF_PACKET 102103032Ssobomax * sockets rather than SOCK_PACKET sockets. 103103026Ssobomax * 104103032Ssobomax * To use them, we include <linux/if_packet.h> rather than 105103026Ssobomax * <netpacket/packet.h>; we do so because 106103026Ssobomax * 107103032Ssobomax * some Linux distributions (e.g., Slackware 4.0) have 2.2 or 108103026Ssobomax * later kernels and libc5, and don't provide a <netpacket/packet.h> 109105300Salfred * file; 110103026Ssobomax * 111103026Ssobomax * not all versions of glibc2 have a <netpacket/packet.h> file 112103026Ssobomax * that defines stuff needed for some of the 2.4-or-later-kernel 113103026Ssobomax * features, so if the system has a 2.4 or later kernel, we 114103026Ssobomax * still can't use those features. 115103026Ssobomax * 116103026Ssobomax * We're already including a number of other <linux/XXX.h> headers, and 117103026Ssobomax * this code is Linux-specific (no other OS has PF_PACKET sockets as 118103026Ssobomax * a raw packet capture mechanism), so it's not as if you gain any 119103026Ssobomax * useful portability by using <netpacket/packet.h> 120103026Ssobomax * 121103026Ssobomax * XXX - should we just include <linux/if_packet.h> even if PF_PACKET 122103026Ssobomax * isn't defined? It only defines one data structure in 2.0.x, so 123103026Ssobomax * it shouldn't cause any problems. 124103026Ssobomax */ 125103026Ssobomax#ifdef PF_PACKET 126103026Ssobomax# include <linux/if_packet.h> 127103026Ssobomax 128103026Ssobomax /* 129103026Ssobomax * On at least some Linux distributions (for example, Red Hat 5.2), 130123338Sbms * there's no <netpacket/packet.h> file, but PF_PACKET is defined if 131103026Ssobomax * you include <sys/socket.h>, but <linux/if_packet.h> doesn't define 132103026Ssobomax * any of the PF_PACKET stuff such as "struct sockaddr_ll" or any of 133103026Ssobomax * the PACKET_xxx stuff. 134103026Ssobomax * 135103026Ssobomax * So we check whether PACKET_HOST is defined, and assume that we have 136103026Ssobomax * PF_PACKET sockets only if it is defined. 137103026Ssobomax */ 138103026Ssobomax# ifdef PACKET_HOST 139103026Ssobomax# define HAVE_PF_PACKET_SOCKETS 140103026Ssobomax# endif /* PACKET_HOST */ 141103026Ssobomax#endif /* PF_PACKET */ 142103026Ssobomax 143103026Ssobomax#ifdef SO_ATTACH_FILTER 144103026Ssobomax#include <linux/types.h> 145103026Ssobomax#include <linux/filter.h> 146103026Ssobomax#endif 147103026Ssobomax 148103032Ssobomax#ifndef __GLIBC__ 149103026Ssobomaxtypedef int socklen_t; 150103026Ssobomax#endif 151103026Ssobomax 152103026Ssobomax#ifndef MSG_TRUNC 153103026Ssobomax/* 154103026Ssobomax * This is being compiled on a system that lacks MSG_TRUNC; define it 155103026Ssobomax * with the value it has in the 2.2 and later kernels, so that, on 156103032Ssobomax * those kernels, when we pass it in the flags argument to "recvfrom()" 157103026Ssobomax * we're passing the right value and thus get the MSG_TRUNC behavior 158103026Ssobomax * we want. (We don't get that behavior on 2.0[.x] kernels, because 159103026Ssobomax * they didn't support MSG_TRUNC.) 160103026Ssobomax */ 161103026Ssobomax#define MSG_TRUNC 0x20 162103026Ssobomax#endif 163111119Simp 164103026Ssobomax#ifndef SOL_PACKET 165103026Ssobomax/* 166121816Sbrooks * This is being compiled on a system that lacks SOL_PACKET; define it 167103026Ssobomax * with the value it has in the 2.2 and later kernels, so that we can 168103026Ssobomax * set promiscuous mode in the good modern way rather than the old 169123338Sbms * 2.0-kernel crappy way. 170103026Ssobomax */ 171103026Ssobomax#define SOL_PACKET 263 172103026Ssobomax#endif 173103026Ssobomax 174103026Ssobomax#define MAX_LINKHEADER_SIZE 256 175103026Ssobomax 176103026Ssobomax/* 177103026Ssobomax * When capturing on all interfaces we use this as the buffer size. 178103026Ssobomax * Should be bigger then all MTUs that occur in real life. 179103026Ssobomax * 64kB should be enough for now. 180103026Ssobomax */ 181103026Ssobomax#define BIGGER_THAN_ALL_MTUS (64*1024) 182103026Ssobomax 183103026Ssobomax/* 184103026Ssobomax * Prototypes for internal functions 185103026Ssobomax */ 186103026Ssobomaxstatic void map_arphrd_to_dlt(pcap_t *, int, int); 187103032Ssobomaxstatic int live_open_old(pcap_t *, const char *, int, int, char *); 188103026Ssobomaxstatic int live_open_new(pcap_t *, const char *, int, int, char *); 189103026Ssobomaxstatic int pcap_read_linux(pcap_t *, int, pcap_handler, u_char *); 190103026Ssobomaxstatic int pcap_read_packet(pcap_t *, pcap_handler, u_char *); 191103026Ssobomaxstatic int pcap_inject_linux(pcap_t *, const void *, size_t); 192103026Ssobomaxstatic int pcap_stats_linux(pcap_t *, struct pcap_stat *); 193103026Ssobomaxstatic int pcap_setfilter_linux(pcap_t *, struct bpf_program *); 194103026Ssobomaxstatic void pcap_close_linux(pcap_t *); 195103026Ssobomax 196103026Ssobomax/* 197103026Ssobomax * Wrap some ioctl calls 198103026Ssobomax */ 199103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 200103026Ssobomaxstatic int iface_get_id(int fd, const char *device, char *ebuf); 201103026Ssobomax#endif 202103026Ssobomaxstatic int iface_get_mtu(int fd, const char *device, char *ebuf); 203103026Ssobomaxstatic int iface_get_arptype(int fd, const char *device, char *ebuf); 204103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 205103026Ssobomaxstatic int iface_bind(int fd, int ifindex, char *ebuf); 206103026Ssobomax#endif 207103032Ssobomaxstatic int iface_bind_old(int fd, const char *device, char *ebuf); 208103026Ssobomax 209103026Ssobomax#ifdef SO_ATTACH_FILTER 210103026Ssobomaxstatic int fix_program(pcap_t *handle, struct sock_fprog *fcode); 211103026Ssobomaxstatic int fix_offset(struct bpf_insn *p); 212103026Ssobomaxstatic int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode); 213103026Ssobomaxstatic int reset_kernel_filter(pcap_t *handle); 214103026Ssobomax 215123992Ssobomaxstatic struct sock_filter total_insn 216103026Ssobomax = BPF_STMT(BPF_RET | BPF_K, 0); 217103026Ssobomaxstatic struct sock_fprog total_fcode 218103026Ssobomax = { 1, &total_insn }; 219103026Ssobomax#endif 220103026Ssobomax 221103026Ssobomax/* 222103026Ssobomax * Get a handle for a live capture from the given device. You can 223103026Ssobomax * pass NULL as device to get all packages (without link level 224103026Ssobomax * information of course). If you pass 1 as promisc the interface 225103026Ssobomax * will be set to promiscous mode (XXX: I think this usage should 226103026Ssobomax * be deprecated and functions be added to select that later allow 227103026Ssobomax * modification of that values -- Torsten). 228103026Ssobomax * 229103026Ssobomax * See also pcap(3). 230103026Ssobomax */ 231103026Ssobomaxpcap_t * 232103026Ssobomaxpcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 233103026Ssobomax char *ebuf) 234103026Ssobomax{ 235103026Ssobomax pcap_t *handle; 236103026Ssobomax int mtu; 237103026Ssobomax int err; 238103026Ssobomax int live_open_ok = 0; 239103026Ssobomax struct utsname utsname; 240103026Ssobomax 241103026Ssobomax#ifdef HAVE_DAG_API 242123922Ssam if (strstr(device, "dag")) { 243103026Ssobomax return dag_open_live(device, snaplen, promisc, to_ms, ebuf); 244103026Ssobomax } 245103026Ssobomax#endif /* HAVE_DAG_API */ 246103026Ssobomax 247103026Ssobomax /* Allocate a handle for this session. */ 248103026Ssobomax 249103026Ssobomax handle = malloc(sizeof(*handle)); 250103026Ssobomax if (handle == NULL) { 251103026Ssobomax snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 252103026Ssobomax pcap_strerror(errno)); 253103026Ssobomax return NULL; 254103026Ssobomax } 255103026Ssobomax 256103026Ssobomax /* Initialize some components of the pcap structure. */ 257103026Ssobomax 258103026Ssobomax memset(handle, 0, sizeof(*handle)); 259103026Ssobomax handle->snapshot = snaplen; 260103026Ssobomax handle->md.timeout = to_ms; 261103026Ssobomax 262103026Ssobomax /* 263103026Ssobomax * NULL and "any" are special devices which give us the hint to 264103026Ssobomax * monitor all devices. 265103026Ssobomax */ 266103026Ssobomax if (!device || strcmp(device, "any") == 0) { 267103026Ssobomax device = NULL; 268103026Ssobomax handle->md.device = strdup("any"); 269103026Ssobomax if (promisc) { 270103026Ssobomax promisc = 0; 271103026Ssobomax /* Just a warning. */ 272103026Ssobomax snprintf(ebuf, PCAP_ERRBUF_SIZE, 273103026Ssobomax "Promiscuous mode not supported on the \"any\" device"); 274103026Ssobomax } 275103026Ssobomax 276103026Ssobomax } else 277103026Ssobomax handle->md.device = strdup(device); 278103026Ssobomax 279103026Ssobomax if (handle->md.device == NULL) { 280103026Ssobomax snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s", 281103026Ssobomax pcap_strerror(errno) ); 282103026Ssobomax free(handle); 283123992Ssobomax return NULL; 284103026Ssobomax } 285103026Ssobomax 286103026Ssobomax /* 287111119Simp * Current Linux kernels use the protocol family PF_PACKET to 288103026Ssobomax * allow direct access to all packets on the network while 289103026Ssobomax * older kernels had a special socket type SOCK_PACKET to 290103026Ssobomax * implement this feature. 291103026Ssobomax * While this old implementation is kind of obsolete we need 292103026Ssobomax * to be compatible with older kernels for a while so we are 293103026Ssobomax * trying both methods with the newer method preferred. 294103026Ssobomax */ 295103026Ssobomax 296103026Ssobomax if ((err = live_open_new(handle, device, promisc, to_ms, ebuf)) == 1) 297103026Ssobomax live_open_ok = 1; 298103026Ssobomax else if (err == 0) { 299103026Ssobomax /* Non-fatal error; try old way */ 300103026Ssobomax if (live_open_old(handle, device, promisc, to_ms, ebuf)) 301103026Ssobomax live_open_ok = 1; 302103026Ssobomax } 303103026Ssobomax if (!live_open_ok) { 304103026Ssobomax /* 305103026Ssobomax * Both methods to open the packet socket failed. Tidy 306103026Ssobomax * up and report our failure (ebuf is expected to be 307103026Ssobomax * set by the functions above). 308103026Ssobomax */ 309103026Ssobomax 310103026Ssobomax if (handle->md.device != NULL) 311103026Ssobomax free(handle->md.device); 312103026Ssobomax free(handle); 313103026Ssobomax return NULL; 314103026Ssobomax } 315103026Ssobomax 316103026Ssobomax /* 317103026Ssobomax * Compute the buffer size. 318103026Ssobomax * 319103026Ssobomax * If we're using SOCK_PACKET, this might be a 2.0[.x] kernel, 320103026Ssobomax * and might require special handling - check. 321103026Ssobomax */ 322103026Ssobomax if (handle->md.sock_packet && (uname(&utsname) < 0 || 323103026Ssobomax strncmp(utsname.release, "2.0", 3) == 0)) { 324103026Ssobomax /* 325103026Ssobomax * We're using a SOCK_PACKET structure, and either 326103026Ssobomax * we couldn't find out what kernel release this is, 327103026Ssobomax * or it's a 2.0[.x] kernel. 328103026Ssobomax * 329103026Ssobomax * In the 2.0[.x] kernel, a "recvfrom()" on 330103026Ssobomax * a SOCK_PACKET socket, with MSG_TRUNC set, will 331103026Ssobomax * return the number of bytes read, so if we pass 332103026Ssobomax * a length based on the snapshot length, it'll 333103026Ssobomax * return the number of bytes from the packet 334103026Ssobomax * copied to userland, not the actual length 335103026Ssobomax * of the packet. 336111119Simp * 337103026Ssobomax * This means that, for example, the IP dissector 338103026Ssobomax * in tcpdump will get handed a packet length less 339103026Ssobomax * than the length in the IP header, and will 340103026Ssobomax * complain about "truncated-ip". 341103026Ssobomax * 342103026Ssobomax * So we don't bother trying to copy from the 343103026Ssobomax * kernel only the bytes in which we're interested, 344103026Ssobomax * but instead copy them all, just as the older 345103026Ssobomax * versions of libpcap for Linux did. 346103026Ssobomax * 347103026Ssobomax * The buffer therefore needs to be big enough to 348103026Ssobomax * hold the largest packet we can get from this 349103026Ssobomax * device. Unfortunately, we can't get the MRU 350103026Ssobomax * of the network; we can only get the MTU. The 351103026Ssobomax * MTU may be too small, in which case a packet larger 352103026Ssobomax * than the buffer size will be truncated *and* we 353103026Ssobomax * won't get the actual packet size. 354103026Ssobomax * 355103026Ssobomax * However, if the snapshot length is larger than 356103026Ssobomax * the buffer size based on the MTU, we use the 357103026Ssobomax * snapshot length as the buffer size, instead; 358103026Ssobomax * this means that with a sufficiently large snapshot 359103026Ssobomax * length we won't artificially truncate packets 360103026Ssobomax * to the MTU-based size. 361103026Ssobomax * 362103026Ssobomax * This mess just one of many problems with packet 363103032Ssobomax * capture on 2.0[.x] kernels; you really want a 364103026Ssobomax * 2.2[.x] or later kernel if you want packet capture 365103026Ssobomax * to work well. 366123992Ssobomax */ 367103026Ssobomax mtu = iface_get_mtu(handle->fd, device, ebuf); 368103026Ssobomax if (mtu == -1) { 369103026Ssobomax pcap_close_linux(handle); 370103026Ssobomax free(handle); 371103026Ssobomax return NULL; 372123992Ssobomax } 373123992Ssobomax handle->bufsize = MAX_LINKHEADER_SIZE + mtu; 374103026Ssobomax if (handle->bufsize < handle->snapshot) 375103026Ssobomax handle->bufsize = handle->snapshot; 376103026Ssobomax } else { 377103026Ssobomax /* 378103026Ssobomax * This is a 2.2[.x] or later kernel (we know that 379103026Ssobomax * either because we're not using a SOCK_PACKET 380103026Ssobomax * socket - PF_PACKET is supported only in 2.2 381103032Ssobomax * and later kernels - or because we checked the 382103026Ssobomax * kernel version). 383103026Ssobomax * 384103026Ssobomax * We can safely pass "recvfrom()" a byte count 385103026Ssobomax * based on the snapshot length. 386103026Ssobomax */ 387103026Ssobomax handle->bufsize = handle->snapshot; 388103026Ssobomax } 389103026Ssobomax 390103026Ssobomax /* Allocate the buffer */ 391103026Ssobomax 392103026Ssobomax handle->buffer = malloc(handle->bufsize + handle->offset); 393103026Ssobomax if (!handle->buffer) { 394103026Ssobomax snprintf(ebuf, PCAP_ERRBUF_SIZE, 395103026Ssobomax "malloc: %s", pcap_strerror(errno)); 396103026Ssobomax pcap_close_linux(handle); 397103026Ssobomax free(handle); 398103026Ssobomax return NULL; 399103026Ssobomax } 400103026Ssobomax 401125020Ssobomax /* 402103026Ssobomax * "handle->fd" is a socket, so "select()" and "poll()" 403103026Ssobomax * should work on it. 404103026Ssobomax */ 405103026Ssobomax handle->selectable_fd = handle->fd; 406103026Ssobomax 407103026Ssobomax handle->read_op = pcap_read_linux; 408103026Ssobomax handle->inject_op = pcap_inject_linux; 409103026Ssobomax handle->setfilter_op = pcap_setfilter_linux; 410103026Ssobomax handle->set_datalink_op = NULL; /* can't change data link type */ 411103026Ssobomax handle->getnonblock_op = pcap_getnonblock_fd; 412103026Ssobomax handle->setnonblock_op = pcap_setnonblock_fd; 413103026Ssobomax handle->stats_op = pcap_stats_linux; 414103026Ssobomax handle->close_op = pcap_close_linux; 415103026Ssobomax 416103026Ssobomax return handle; 417103026Ssobomax} 418103026Ssobomax 419103026Ssobomax/* 420103026Ssobomax * Read at most max_packets from the capture stream and call the callback 421103026Ssobomax * for each of them. Returns the number of packets handled or -1 if an 422103026Ssobomax * error occured. 423103026Ssobomax */ 424103026Ssobomaxstatic int 425103026Ssobomaxpcap_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) 426103026Ssobomax{ 427103026Ssobomax /* 428103026Ssobomax * Currently, on Linux only one packet is delivered per read, 429103026Ssobomax * so we don't loop. 430103026Ssobomax */ 431103026Ssobomax return pcap_read_packet(handle, callback, user); 432103026Ssobomax} 433103026Ssobomax 434103026Ssobomax/* 435103026Ssobomax * Read a packet from the socket calling the handler provided by 436103026Ssobomax * the user. Returns the number of packets received or -1 if an 437103026Ssobomax * error occured. 438103026Ssobomax */ 439103026Ssobomaxstatic int 440103026Ssobomaxpcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) 441103026Ssobomax{ 442103026Ssobomax u_char *bp; 443103026Ssobomax int offset; 444103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 445103026Ssobomax struct sockaddr_ll from; 446103026Ssobomax struct sll_header *hdrp; 447103026Ssobomax#else 448103026Ssobomax struct sockaddr from; 449103026Ssobomax#endif 450103026Ssobomax socklen_t fromlen; 451103026Ssobomax int packet_len, caplen; 452103026Ssobomax struct pcap_pkthdr pcap_header; 453103026Ssobomax 454103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 455103026Ssobomax /* 456103026Ssobomax * If this is a cooked device, leave extra room for a 457103026Ssobomax * fake packet header. 458103026Ssobomax */ 459103026Ssobomax if (handle->md.cooked) 460103026Ssobomax offset = SLL_HDR_LEN; 461103026Ssobomax else 462103026Ssobomax offset = 0; 463103026Ssobomax#else 464103026Ssobomax /* 465103026Ssobomax * This system doesn't have PF_PACKET sockets, so it doesn't 466103026Ssobomax * support cooked devices. 467103026Ssobomax */ 468103026Ssobomax offset = 0; 469103026Ssobomax#endif 470103026Ssobomax 471103026Ssobomax /* Receive a single packet from the kernel */ 472103026Ssobomax 473103026Ssobomax bp = handle->buffer + handle->offset; 474103026Ssobomax do { 475103026Ssobomax /* 476103026Ssobomax * Has "pcap_breakloop()" been called? 477103026Ssobomax */ 478103026Ssobomax if (handle->break_loop) { 479103026Ssobomax /* 480103026Ssobomax * Yes - clear the flag that indicates that it 481103026Ssobomax * has, and return -2 as an indication that we 482103026Ssobomax * were told to break out of the loop. 483103026Ssobomax */ 484103026Ssobomax handle->break_loop = 0; 485103026Ssobomax return -2; 486103026Ssobomax } 487103026Ssobomax fromlen = sizeof(from); 488103026Ssobomax packet_len = recvfrom( 489103026Ssobomax handle->fd, bp + offset, 490103026Ssobomax handle->bufsize - offset, MSG_TRUNC, 491103026Ssobomax (struct sockaddr *) &from, &fromlen); 492125020Ssobomax } while (packet_len == -1 && errno == EINTR); 493103026Ssobomax 494103026Ssobomax /* Check if an error occured */ 495103026Ssobomax 496103026Ssobomax if (packet_len == -1) { 497103026Ssobomax if (errno == EAGAIN) 498103026Ssobomax return 0; /* no packet there */ 499103026Ssobomax else { 500103026Ssobomax snprintf(handle->errbuf, sizeof(handle->errbuf), 501103026Ssobomax "recvfrom: %s", pcap_strerror(errno)); 502103026Ssobomax return -1; 503103026Ssobomax } 504103026Ssobomax } 505103026Ssobomax 506103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 507103026Ssobomax /* 508103026Ssobomax * If this is from the loopback device, reject outgoing packets; 509103026Ssobomax * we'll see the packet as an incoming packet as well, and 510103026Ssobomax * we don't want to see it twice. 511103026Ssobomax * 512103026Ssobomax * We can only do this if we're using PF_PACKET; the address 513103026Ssobomax * returned for SOCK_PACKET is a "sockaddr_pkt" which lacks 514103026Ssobomax * the relevant packet type information. 515103026Ssobomax */ 516103026Ssobomax if (!handle->md.sock_packet && 517103026Ssobomax from.sll_ifindex == handle->md.lo_ifindex && 518103026Ssobomax from.sll_pkttype == PACKET_OUTGOING) 519103026Ssobomax return 0; 520103026Ssobomax#endif 521103026Ssobomax 522103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 523103026Ssobomax /* 524103026Ssobomax * If this is a cooked device, fill in the fake packet header. 525103026Ssobomax */ 526103026Ssobomax if (handle->md.cooked) { 527103026Ssobomax /* 528103026Ssobomax * Add the length of the fake header to the length 529103026Ssobomax * of packet data we read. 530103026Ssobomax */ 531103026Ssobomax packet_len += SLL_HDR_LEN; 532103026Ssobomax 533103026Ssobomax hdrp = (struct sll_header *)bp; 534103026Ssobomax 535103026Ssobomax /* 536103026Ssobomax * Map the PACKET_ value to a LINUX_SLL_ value; we 537103026Ssobomax * want the same numerical value to be used in 538103026Ssobomax * the link-layer header even if the numerical values 539103026Ssobomax * for the PACKET_ #defines change, so that programs 540103026Ssobomax * that look at the packet type field will always be 541103026Ssobomax * able to handle DLT_LINUX_SLL captures. 542103026Ssobomax */ 543103026Ssobomax switch (from.sll_pkttype) { 544103026Ssobomax 545103026Ssobomax case PACKET_HOST: 546103026Ssobomax hdrp->sll_pkttype = htons(LINUX_SLL_HOST); 547103026Ssobomax break; 548103026Ssobomax 549103026Ssobomax case PACKET_BROADCAST: 550103026Ssobomax hdrp->sll_pkttype = htons(LINUX_SLL_BROADCAST); 551103026Ssobomax break; 552103026Ssobomax 553103026Ssobomax case PACKET_MULTICAST: 554103026Ssobomax hdrp->sll_pkttype = htons(LINUX_SLL_MULTICAST); 555103026Ssobomax break; 556103026Ssobomax 557103026Ssobomax case PACKET_OTHERHOST: 558103026Ssobomax hdrp->sll_pkttype = htons(LINUX_SLL_OTHERHOST); 559103026Ssobomax break; 560103026Ssobomax 561103026Ssobomax case PACKET_OUTGOING: 562103026Ssobomax hdrp->sll_pkttype = htons(LINUX_SLL_OUTGOING); 563103026Ssobomax break; 564103026Ssobomax 565103026Ssobomax default: 566103026Ssobomax hdrp->sll_pkttype = -1; 567103026Ssobomax break; 568103026Ssobomax } 569103026Ssobomax 570103026Ssobomax hdrp->sll_hatype = htons(from.sll_hatype); 571103026Ssobomax hdrp->sll_halen = htons(from.sll_halen); 572103026Ssobomax memcpy(hdrp->sll_addr, from.sll_addr, 573103026Ssobomax (from.sll_halen > SLL_ADDRLEN) ? 574103026Ssobomax SLL_ADDRLEN : 575103026Ssobomax from.sll_halen); 576103026Ssobomax hdrp->sll_protocol = from.sll_protocol; 577103026Ssobomax } 578103026Ssobomax#endif 579103026Ssobomax 580103026Ssobomax /* 581122699Sbms * XXX: According to the kernel source we should get the real 582122699Sbms * packet len if calling recvfrom with MSG_TRUNC set. It does 583122699Sbms * not seem to work here :(, but it is supported by this code 584103026Ssobomax * anyway. 585103026Ssobomax * To be honest the code RELIES on that feature so this is really 586103026Ssobomax * broken with 2.2.x kernels. 587103026Ssobomax * I spend a day to figure out what's going on and I found out 588103026Ssobomax * that the following is happening: 589103026Ssobomax * 590103026Ssobomax * The packet comes from a random interface and the packet_rcv 591103026Ssobomax * hook is called with a clone of the packet. That code inserts 592103026Ssobomax * the packet into the receive queue of the packet socket. 593103026Ssobomax * If a filter is attached to that socket that filter is run 594103026Ssobomax * first - and there lies the problem. The default filter always 595122699Sbms * cuts the packet at the snaplen: 596122699Sbms * 597122699Sbms * # tcpdump -d 598103026Ssobomax * (000) ret #68 599103026Ssobomax * 600103026Ssobomax * So the packet filter cuts down the packet. The recvfrom call 601103026Ssobomax * says "hey, it's only 68 bytes, it fits into the buffer" with 602103026Ssobomax * the result that we don't get the real packet length. This 603103026Ssobomax * is valid at least until kernel 2.2.17pre6. 604103026Ssobomax * 605103026Ssobomax * We currently handle this by making a copy of the filter 606103026Ssobomax * program, fixing all "ret" instructions with non-zero 607103026Ssobomax * operands to have an operand of 65535 so that the filter 608103026Ssobomax * doesn't truncate the packet, and supplying that modified 609103026Ssobomax * filter to the kernel. 610103026Ssobomax */ 611103026Ssobomax 612103026Ssobomax caplen = packet_len; 613103026Ssobomax if (caplen > handle->snapshot) 614103026Ssobomax caplen = handle->snapshot; 615103026Ssobomax 616103026Ssobomax /* Run the packet filter if not using kernel filter */ 617103026Ssobomax if (!handle->md.use_bpf && handle->fcode.bf_insns) { 618103026Ssobomax if (bpf_filter(handle->fcode.bf_insns, bp, 619103026Ssobomax packet_len, caplen) == 0) 620103026Ssobomax { 621103026Ssobomax /* rejected by filter */ 622123992Ssobomax return 0; 623103026Ssobomax } 624103026Ssobomax } 625103026Ssobomax 626103026Ssobomax /* Fill in our own header data */ 627103026Ssobomax 628103032Ssobomax if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) { 629103026Ssobomax snprintf(handle->errbuf, sizeof(handle->errbuf), 630103026Ssobomax "ioctl: %s", pcap_strerror(errno)); 631103026Ssobomax return -1; 632103026Ssobomax } 633103026Ssobomax pcap_header.caplen = caplen; 634103026Ssobomax pcap_header.len = packet_len; 635103026Ssobomax 636103026Ssobomax /* 637103026Ssobomax * Count the packet. 638103026Ssobomax * 639103026Ssobomax * Arguably, we should count them before we check the filter, 640103026Ssobomax * as on many other platforms "ps_recv" counts packets 641103026Ssobomax * handed to the filter rather than packets that passed 642103026Ssobomax * the filter, but if filtering is done in the kernel, we 643103026Ssobomax * can't get a count of packets that passed the filter, 644103026Ssobomax * and that would mean the meaning of "ps_recv" wouldn't 645103026Ssobomax * be the same on all Linux systems. 646103026Ssobomax * 647103026Ssobomax * XXX - it's not the same on all systems in any case; 648103026Ssobomax * ideally, we should have a "get the statistics" call 649103026Ssobomax * that supplies more counts and indicates which of them 650103026Ssobomax * it supplies, so that we supply a count of packets 651103026Ssobomax * handed to the filter only on platforms where that 652103026Ssobomax * information is available. 653103026Ssobomax * 654103026Ssobomax * We count them here even if we can get the packet count 655103026Ssobomax * from the kernel, as we can only determine at run time 656103026Ssobomax * whether we'll be able to get it from the kernel (if 657103026Ssobomax * HAVE_TPACKET_STATS isn't defined, we can't get it from 658123992Ssobomax * the kernel, but if it is defined, the library might 659103026Ssobomax * have been built with a 2.4 or later kernel, but we 660103026Ssobomax * might be running on a 2.2[.x] kernel without Alexey 661103026Ssobomax * Kuznetzov's turbopacket patches, and thus the kernel 662103026Ssobomax * might not be able to supply those statistics). We 663103026Ssobomax * could, I guess, try, when opening the socket, to get 664103026Ssobomax * the statistics, and if we can not increment the count 665103026Ssobomax * here, but it's not clear that always incrementing 666103026Ssobomax * the count is more expensive than always testing a flag 667103026Ssobomax * in memory. 668103026Ssobomax */ 669103026Ssobomax handle->md.stat.ps_recv++; 670103026Ssobomax 671103026Ssobomax /* Call the user supplied callback function */ 672103026Ssobomax callback(userdata, &pcap_header, bp); 673103026Ssobomax 674103026Ssobomax return 1; 675103026Ssobomax} 676103026Ssobomax 677103026Ssobomaxstatic int 678103026Ssobomaxpcap_inject_linux(pcap_t *handle, const void *buf, size_t size) 679103026Ssobomax{ 680103026Ssobomax int ret; 681103026Ssobomax 682103026Ssobomax#ifdef HAVE_PF_PACKET_SOCKETS 683103026Ssobomax if (!handle->md.sock_packet) { 684103026Ssobomax /* PF_PACKET socket */ 685103026Ssobomax if (handle->md.ifindex == -1) { 686103026Ssobomax /* 687103026Ssobomax * We don't support sending on the "any" device. 688103026Ssobomax */ 689103026Ssobomax strlcpy(handle->errbuf, 690103026Ssobomax "Sending packets isn't supported on the \"any\" device", 691103026Ssobomax PCAP_ERRBUF_SIZE); 692103026Ssobomax return (-1); 693103026Ssobomax } 694103026Ssobomax 695103026Ssobomax if (handle->md.cooked) { 696103026Ssobomax /* 697103026Ssobomax * We don't support sending on the "any" device. 698123992Ssobomax * 699123992Ssobomax * XXX - how do you send on a bound cooked-mode 700103026Ssobomax * socket? 701123992Ssobomax * Is a "sendto()" required there? 702103026Ssobomax */ 703103026Ssobomax strlcpy(handle->errbuf, 704103026Ssobomax "Sending packets isn't supported in cooked mode", 705103026Ssobomax PCAP_ERRBUF_SIZE); 706103026Ssobomax return (-1); 707103026Ssobomax } 708103026Ssobomax } 709103026Ssobomax#endif 710103026Ssobomax 711103026Ssobomax ret = send(handle->fd, buf, size, 0); 712103026Ssobomax if (ret == -1) { 713103026Ssobomax snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 714103026Ssobomax pcap_strerror(errno)); 715103026Ssobomax return (-1); 716103026Ssobomax } 717103026Ssobomax return (ret); 718103026Ssobomax} 719103026Ssobomax 720103026Ssobomax/* 721103026Ssobomax * Get the statistics for the given packet capture handle. 722103026Ssobomax * Reports the number of dropped packets iff the kernel supports 723103026Ssobomax * the PACKET_STATISTICS "getsockopt()" argument (2.4 and later 724103026Ssobomax * kernels, and 2.2[.x] kernels with Alexey Kuznetzov's turbopacket 725103026Ssobomax * patches); otherwise, that information isn't available, and we lie 726103026Ssobomax * and report 0 as the count of dropped packets. 727103026Ssobomax */ 728103026Ssobomaxstatic int 729103026Ssobomaxpcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) 730103026Ssobomax{ 731103026Ssobomax#ifdef HAVE_TPACKET_STATS 732103026Ssobomax struct tpacket_stats kstats; 733103026Ssobomax socklen_t len = sizeof (struct tpacket_stats); 734103026Ssobomax#endif 735103026Ssobomax 736103026Ssobomax#ifdef HAVE_TPACKET_STATS 737103026Ssobomax /* 738103026Ssobomax * Try to get the packet counts from the kernel. 739103026Ssobomax */ 740103026Ssobomax if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, 741103026Ssobomax &kstats, &len) > -1) { 742103026Ssobomax /* 743103026Ssobomax * In "linux/net/packet/af_packet.c", at least in the 744103026Ssobomax * 2.4.9 kernel, "tp_packets" is incremented for every 745103026Ssobomax * packet that passes the packet filter *and* is 746103026Ssobomax * successfully queued on the socket; "tp_drops" is 747103026Ssobomax * incremented for every packet dropped because there's 748103026Ssobomax * not enough free space in the socket buffer. 749 * 750 * When the statistics are returned for a PACKET_STATISTICS 751 * "getsockopt()" call, "tp_drops" is added to "tp_packets", 752 * so that "tp_packets" counts all packets handed to 753 * the PF_PACKET socket, including packets dropped because 754 * there wasn't room on the socket buffer - but not 755 * including packets that didn't pass the filter. 756 * 757 * In the BSD BPF, the count of received packets is 758 * incremented for every packet handed to BPF, regardless 759 * of whether it passed the filter. 760 * 761 * We can't make "pcap_stats()" work the same on both 762 * platforms, but the best approximation is to return 763 * "tp_packets" as the count of packets and "tp_drops" 764 * as the count of drops. 765 * 766 * Keep a running total because each call to 767 * getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, .... 768 * resets the counters to zero. 769 */ 770 handle->md.stat.ps_recv += kstats.tp_packets; 771 handle->md.stat.ps_drop += kstats.tp_drops; 772 } 773 else 774 { 775 /* 776 * If the error was EOPNOTSUPP, fall through, so that 777 * if you build the library on a system with 778 * "struct tpacket_stats" and run it on a system 779 * that doesn't, it works as it does if the library 780 * is built on a system without "struct tpacket_stats". 781 */ 782 if (errno != EOPNOTSUPP) { 783 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, 784 "pcap_stats: %s", pcap_strerror(errno)); 785 return -1; 786 } 787 } 788#endif 789 /* 790 * On systems where the PACKET_STATISTICS "getsockopt()" argument 791 * is supported on PF_PACKET sockets: 792 * 793 * "ps_recv" counts only packets that *passed* the filter, 794 * not packets that didn't pass the filter. This includes 795 * packets later dropped because we ran out of buffer space. 796 * 797 * "ps_drop" counts packets dropped because we ran out of 798 * buffer space. It doesn't count packets dropped by the 799 * interface driver. It counts only packets that passed 800 * the filter. 801 * 802 * Both statistics include packets not yet read from the 803 * kernel by libpcap, and thus not yet seen by the application. 804 * 805 * On systems where the PACKET_STATISTICS "getsockopt()" argument 806 * is not supported on PF_PACKET sockets: 807 * 808 * "ps_recv" counts only packets that *passed* the filter, 809 * not packets that didn't pass the filter. It does not 810 * count packets dropped because we ran out of buffer 811 * space. 812 * 813 * "ps_drop" is not supported. 814 * 815 * "ps_recv" doesn't include packets not yet read from 816 * the kernel by libpcap. 817 */ 818 *stats = handle->md.stat; 819 return 0; 820} 821 822/* 823 * Description string for the "any" device. 824 */ 825static const char any_descr[] = "Pseudo-device that captures on all interfaces"; 826 827int 828pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 829{ 830 if (pcap_add_if(alldevsp, "any", 0, any_descr, errbuf) < 0) 831 return (-1); 832 833#ifdef HAVE_DAG_API 834 if (dag_platform_finddevs(alldevsp, errbuf) < 0) 835 return (-1); 836#endif /* HAVE_DAG_API */ 837 838 return (0); 839} 840 841/* 842 * Attach the given BPF code to the packet capture device. 843 */ 844static int 845pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter) 846{ 847#ifdef SO_ATTACH_FILTER 848 struct sock_fprog fcode; 849 int can_filter_in_kernel; 850 int err = 0; 851#endif 852 853 if (!handle) 854 return -1; 855 if (!filter) { 856 strncpy(handle->errbuf, "setfilter: No filter specified", 857 sizeof(handle->errbuf)); 858 return -1; 859 } 860 861 /* Make our private copy of the filter */ 862 863 if (install_bpf_program(handle, filter) < 0) 864 /* install_bpf_program() filled in errbuf */ 865 return -1; 866 867 /* 868 * Run user level packet filter by default. Will be overriden if 869 * installing a kernel filter succeeds. 870 */ 871 handle->md.use_bpf = 0; 872 873 /* Install kernel level filter if possible */ 874 875#ifdef SO_ATTACH_FILTER 876#ifdef USHRT_MAX 877 if (handle->fcode.bf_len > USHRT_MAX) { 878 /* 879 * fcode.len is an unsigned short for current kernel. 880 * I have yet to see BPF-Code with that much 881 * instructions but still it is possible. So for the 882 * sake of correctness I added this check. 883 */ 884 fprintf(stderr, "Warning: Filter too complex for kernel\n"); 885 fcode.filter = NULL; 886 can_filter_in_kernel = 0; 887 } else 888#endif /* USHRT_MAX */ 889 { 890 /* 891 * Oh joy, the Linux kernel uses struct sock_fprog instead 892 * of struct bpf_program and of course the length field is 893 * of different size. Pointed out by Sebastian 894 * 895 * Oh, and we also need to fix it up so that all "ret" 896 * instructions with non-zero operands have 65535 as the 897 * operand, and so that, if we're in cooked mode, all 898 * memory-reference instructions use special magic offsets 899 * in references to the link-layer header and assume that 900 * the link-layer payload begins at 0; "fix_program()" 901 * will do that. 902 */ 903 switch (fix_program(handle, &fcode)) { 904 905 case -1: 906 default: 907 /* 908 * Fatal error; just quit. 909 * (The "default" case shouldn't happen; we 910 * return -1 for that reason.) 911 */ 912 return -1; 913 914 case 0: 915 /* 916 * The program performed checks that we can't make 917 * work in the kernel. 918 */ 919 can_filter_in_kernel = 0; 920 break; 921 922 case 1: 923 /* 924 * We have a filter that'll work in the kernel. 925 */ 926 can_filter_in_kernel = 1; 927 break; 928 } 929 } 930 931 if (can_filter_in_kernel) { 932 if ((err = set_kernel_filter(handle, &fcode)) == 0) 933 { 934 /* Installation succeded - using kernel filter. */ 935 handle->md.use_bpf = 1; 936 } 937 else if (err == -1) /* Non-fatal error */ 938 { 939 /* 940 * Print a warning if we weren't able to install 941 * the filter for a reason other than "this kernel 942 * isn't configured to support socket filters. 943 */ 944 if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) { 945 fprintf(stderr, 946 "Warning: Kernel filter failed: %s\n", 947 pcap_strerror(errno)); 948 } 949 } 950 } 951 952 /* 953 * If we're not using the kernel filter, get rid of any kernel 954 * filter that might've been there before, e.g. because the 955 * previous filter could work in the kernel, or because some other 956 * code attached a filter to the socket by some means other than 957 * calling "pcap_setfilter()". Otherwise, the kernel filter may 958 * filter out packets that would pass the new userland filter. 959 */ 960 if (!handle->md.use_bpf) 961 reset_kernel_filter(handle); 962 963 /* 964 * Free up the copy of the filter that was made by "fix_program()". 965 */ 966 if (fcode.filter != NULL) 967 free(fcode.filter); 968 969 if (err == -2) 970 /* Fatal error */ 971 return -1; 972#endif /* SO_ATTACH_FILTER */ 973 974 return 0; 975} 976 977/* 978 * Linux uses the ARP hardware type to identify the type of an 979 * interface. pcap uses the DLT_xxx constants for this. This 980 * function takes a pointer to a "pcap_t", and an ARPHRD_xxx 981 * constant, as arguments, and sets "handle->linktype" to the 982 * appropriate DLT_XXX constant and sets "handle->offset" to 983 * the appropriate value (to make "handle->offset" plus link-layer 984 * header length be a multiple of 4, so that the link-layer payload 985 * will be aligned on a 4-byte boundary when capturing packets). 986 * (If the offset isn't set here, it'll be 0; add code as appropriate 987 * for cases where it shouldn't be 0.) 988 * 989 * If "cooked_ok" is non-zero, we can use DLT_LINUX_SLL and capture 990 * in cooked mode; otherwise, we can't use cooked mode, so we have 991 * to pick some type that works in raw mode, or fail. 992 * 993 * Sets the link type to -1 if unable to map the type. 994 */ 995static void map_arphrd_to_dlt(pcap_t *handle, int arptype, int cooked_ok) 996{ 997 switch (arptype) { 998 999 case ARPHRD_ETHER: 1000 /* 1001 * This is (presumably) a real Ethernet capture; give it a 1002 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 1003 * that an application can let you choose it, in case you're 1004 * capturing DOCSIS traffic that a Cisco Cable Modem 1005 * Termination System is putting out onto an Ethernet (it 1006 * doesn't put an Ethernet header onto the wire, it puts raw 1007 * DOCSIS frames out on the wire inside the low-level 1008 * Ethernet framing). 1009 * 1010 * XXX - are there any sorts of "fake Ethernet" that have 1011 * ARPHRD_ETHER but that *shouldn't offer DLT_DOCSIS as 1012 * a Cisco CMTS won't put traffic onto it or get traffic 1013 * bridged onto it? ISDN is handled in "live_open_new()", 1014 * as we fall back on cooked mode there; are there any 1015 * others? 1016 */ 1017 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 1018 /* 1019 * If that fails, just leave the list empty. 1020 */ 1021 if (handle->dlt_list != NULL) { 1022 handle->dlt_list[0] = DLT_EN10MB; 1023 handle->dlt_list[1] = DLT_DOCSIS; 1024 handle->dlt_count = 2; 1025 } 1026 /* FALLTHROUGH */ 1027 1028 case ARPHRD_METRICOM: 1029 case ARPHRD_LOOPBACK: 1030 handle->linktype = DLT_EN10MB; 1031 handle->offset = 2; 1032 break; 1033 1034 case ARPHRD_EETHER: 1035 handle->linktype = DLT_EN3MB; 1036 break; 1037 1038 case ARPHRD_AX25: 1039 handle->linktype = DLT_AX25; 1040 break; 1041 1042 case ARPHRD_PRONET: 1043 handle->linktype = DLT_PRONET; 1044 break; 1045 1046 case ARPHRD_CHAOS: 1047 handle->linktype = DLT_CHAOS; 1048 break; 1049 1050#ifndef ARPHRD_IEEE802_TR 1051#define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */ 1052#endif 1053 case ARPHRD_IEEE802_TR: 1054 case ARPHRD_IEEE802: 1055 handle->linktype = DLT_IEEE802; 1056 handle->offset = 2; 1057 break; 1058 1059 case ARPHRD_ARCNET: 1060 handle->linktype = DLT_ARCNET_LINUX; 1061 break; 1062 1063#ifndef ARPHRD_FDDI /* From Linux 2.2.13 */ 1064#define ARPHRD_FDDI 774 1065#endif 1066 case ARPHRD_FDDI: 1067 handle->linktype = DLT_FDDI; 1068 handle->offset = 3; 1069 break; 1070 1071#ifndef ARPHRD_ATM /* FIXME: How to #include this? */ 1072#define ARPHRD_ATM 19 1073#endif 1074 case ARPHRD_ATM: 1075 /* 1076 * The Classical IP implementation in ATM for Linux 1077 * supports both what RFC 1483 calls "LLC Encapsulation", 1078 * in which each packet has an LLC header, possibly 1079 * with a SNAP header as well, prepended to it, and 1080 * what RFC 1483 calls "VC Based Multiplexing", in which 1081 * different virtual circuits carry different network 1082 * layer protocols, and no header is prepended to packets. 1083 * 1084 * They both have an ARPHRD_ type of ARPHRD_ATM, so 1085 * you can't use the ARPHRD_ type to find out whether 1086 * captured packets will have an LLC header, and, 1087 * while there's a socket ioctl to *set* the encapsulation 1088 * type, there's no ioctl to *get* the encapsulation type. 1089 * 1090 * This means that 1091 * 1092 * programs that dissect Linux Classical IP frames 1093 * would have to check for an LLC header and, 1094 * depending on whether they see one or not, dissect 1095 * the frame as LLC-encapsulated or as raw IP (I 1096 * don't know whether there's any traffic other than 1097 * IP that would show up on the socket, or whether 1098 * there's any support for IPv6 in the Linux 1099 * Classical IP code); 1100 * 1101 * filter expressions would have to compile into 1102 * code that checks for an LLC header and does 1103 * the right thing. 1104 * 1105 * Both of those are a nuisance - and, at least on systems 1106 * that support PF_PACKET sockets, we don't have to put 1107 * up with those nuisances; instead, we can just capture 1108 * in cooked mode. That's what we'll do, if we can. 1109 * Otherwise, we'll just fail. 1110 */ 1111 if (cooked_ok) 1112 handle->linktype = DLT_LINUX_SLL; 1113 else 1114 handle->linktype = -1; 1115 break; 1116 1117#ifndef ARPHRD_IEEE80211 /* From Linux 2.4.6 */ 1118#define ARPHRD_IEEE80211 801 1119#endif 1120 case ARPHRD_IEEE80211: 1121 handle->linktype = DLT_IEEE802_11; 1122 break; 1123 1124#ifndef ARPHRD_IEEE80211_PRISM /* From Linux 2.4.18 */ 1125#define ARPHRD_IEEE80211_PRISM 802 1126#endif 1127 case ARPHRD_IEEE80211_PRISM: 1128 handle->linktype = DLT_PRISM_HEADER; 1129 break; 1130 1131 case ARPHRD_PPP: 1132 /* 1133 * Some PPP code in the kernel supplies no link-layer 1134 * header whatsoever to PF_PACKET sockets; other PPP 1135 * code supplies PPP link-layer headers ("syncppp.c"); 1136 * some PPP code might supply random link-layer 1137 * headers (PPP over ISDN - there's code in Ethereal, 1138 * for example, to cope with PPP-over-ISDN captures 1139 * with which the Ethereal developers have had to cope, 1140 * heuristically trying to determine which of the 1141 * oddball link-layer headers particular packets have). 1142 * 1143 * As such, we just punt, and run all PPP interfaces 1144 * in cooked mode, if we can; otherwise, we just treat 1145 * it as DLT_RAW, for now - if somebody needs to capture, 1146 * on a 2.0[.x] kernel, on PPP devices that supply a 1147 * link-layer header, they'll have to add code here to 1148 * map to the appropriate DLT_ type (possibly adding a 1149 * new DLT_ type, if necessary). 1150 */ 1151 if (cooked_ok) 1152 handle->linktype = DLT_LINUX_SLL; 1153 else { 1154 /* 1155 * XXX - handle ISDN types here? We can't fall 1156 * back on cooked sockets, so we'd have to 1157 * figure out from the device name what type of 1158 * link-layer encapsulation it's using, and map 1159 * that to an appropriate DLT_ value, meaning 1160 * we'd map "isdnN" devices to DLT_RAW (they 1161 * supply raw IP packets with no link-layer 1162 * header) and "isdY" devices to a new DLT_I4L_IP 1163 * type that has only an Ethernet packet type as 1164 * a link-layer header. 1165 * 1166 * But sometimes we seem to get random crap 1167 * in the link-layer header when capturing on 1168 * ISDN devices.... 1169 */ 1170 handle->linktype = DLT_RAW; 1171 } 1172 break; 1173 1174#ifndef ARPHRD_CISCO 1175#define ARPHRD_CISCO 513 /* previously ARPHRD_HDLC */ 1176#endif 1177 case ARPHRD_CISCO: 1178 handle->linktype = DLT_C_HDLC; 1179 break; 1180 1181 /* Not sure if this is correct for all tunnels, but it 1182 * works for CIPE */ 1183 case ARPHRD_TUNNEL: 1184#ifndef ARPHRD_SIT 1185#define ARPHRD_SIT 776 /* From Linux 2.2.13 */ 1186#endif 1187 case ARPHRD_SIT: 1188 case ARPHRD_CSLIP: 1189 case ARPHRD_SLIP6: 1190 case ARPHRD_CSLIP6: 1191 case ARPHRD_ADAPT: 1192 case ARPHRD_SLIP: 1193#ifndef ARPHRD_RAWHDLC 1194#define ARPHRD_RAWHDLC 518 1195#endif 1196 case ARPHRD_RAWHDLC: 1197#ifndef ARPHRD_DLCI 1198#define ARPHRD_DLCI 15 1199#endif 1200 case ARPHRD_DLCI: 1201 /* 1202 * XXX - should some of those be mapped to DLT_LINUX_SLL 1203 * instead? Should we just map all of them to DLT_LINUX_SLL? 1204 */ 1205 handle->linktype = DLT_RAW; 1206 break; 1207 1208#ifndef ARPHRD_FRAD 1209#define ARPHRD_FRAD 770 1210#endif 1211 case ARPHRD_FRAD: 1212 handle->linktype = DLT_FRELAY; 1213 break; 1214 1215 case ARPHRD_LOCALTLK: 1216 handle->linktype = DLT_LTALK; 1217 break; 1218 1219#ifndef ARPHRD_FCPP 1220#define ARPHRD_FCPP 784 1221#endif 1222 case ARPHRD_FCPP: 1223#ifndef ARPHRD_FCAL 1224#define ARPHRD_FCAL 785 1225#endif 1226 case ARPHRD_FCAL: 1227#ifndef ARPHRD_FCPL 1228#define ARPHRD_FCPL 786 1229#endif 1230 case ARPHRD_FCPL: 1231#ifndef ARPHRD_FCFABRIC 1232#define ARPHRD_FCFABRIC 787 1233#endif 1234 case ARPHRD_FCFABRIC: 1235 /* 1236 * We assume that those all mean RFC 2625 IP-over- 1237 * Fibre Channel, with the RFC 2625 header at 1238 * the beginning of the packet. 1239 */ 1240 handle->linktype = DLT_IP_OVER_FC; 1241 break; 1242 1243#ifndef ARPHRD_IRDA 1244#define ARPHRD_IRDA 783 1245#endif 1246 case ARPHRD_IRDA: 1247 /* Don't expect IP packet out of this interfaces... */ 1248 handle->linktype = DLT_LINUX_IRDA; 1249 /* We need to save packet direction for IrDA decoding, 1250 * so let's use "Linux-cooked" mode. Jean II */ 1251 //handle->md.cooked = 1; 1252 break; 1253 1254 default: 1255 handle->linktype = -1; 1256 break; 1257 } 1258} 1259 1260/* ===== Functions to interface to the newer kernels ================== */ 1261 1262/* 1263 * Try to open a packet socket using the new kernel interface. 1264 * Returns 0 on failure. 1265 * FIXME: 0 uses to mean success (Sebastian) 1266 */ 1267static int 1268live_open_new(pcap_t *handle, const char *device, int promisc, 1269 int to_ms, char *ebuf) 1270{ 1271#ifdef HAVE_PF_PACKET_SOCKETS 1272 int sock_fd = -1, arptype; 1273 int err; 1274 int fatal_err = 0; 1275 struct packet_mreq mr; 1276 1277 /* One shot loop used for error handling - bail out with break */ 1278 1279 do { 1280 /* 1281 * Open a socket with protocol family packet. If a device is 1282 * given we try to open it in raw mode otherwise we use 1283 * the cooked interface. 1284 */ 1285 sock_fd = device ? 1286 socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) 1287 : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)); 1288 1289 if (sock_fd == -1) { 1290 snprintf(ebuf, PCAP_ERRBUF_SIZE, "socket: %s", 1291 pcap_strerror(errno) ); 1292 break; 1293 } 1294 1295 /* It seems the kernel supports the new interface. */ 1296 handle->md.sock_packet = 0; 1297 1298 /* 1299 * Get the interface index of the loopback device. 1300 * If the attempt fails, don't fail, just set the 1301 * "md.lo_ifindex" to -1. 1302 * 1303 * XXX - can there be more than one device that loops 1304 * packets back, i.e. devices other than "lo"? If so, 1305 * we'd need to find them all, and have an array of 1306 * indices for them, and check all of them in 1307 * "pcap_read_packet()". 1308 */ 1309 handle->md.lo_ifindex = iface_get_id(sock_fd, "lo", ebuf); 1310 1311 /* 1312 * Default value for offset to align link-layer payload 1313 * on a 4-byte boundary. 1314 */ 1315 handle->offset = 0; 1316 1317 /* 1318 * What kind of frames do we have to deal with? Fall back 1319 * to cooked mode if we have an unknown interface type. 1320 */ 1321 1322 if (device) { 1323 /* Assume for now we don't need cooked mode. */ 1324 handle->md.cooked = 0; 1325 1326 arptype = iface_get_arptype(sock_fd, device, ebuf); 1327 if (arptype == -1) { 1328 fatal_err = 1; 1329 break; 1330 } 1331 map_arphrd_to_dlt(handle, arptype, 1); 1332 if (handle->linktype == -1 || 1333 handle->linktype == DLT_LINUX_SLL || 1334 handle->linktype == DLT_LINUX_IRDA || 1335 (handle->linktype == DLT_EN10MB && 1336 (strncmp("isdn", device, 4) == 0 || 1337 strncmp("isdY", device, 4) == 0))) { 1338 /* 1339 * Unknown interface type (-1), or a 1340 * device we explicitly chose to run 1341 * in cooked mode (e.g., PPP devices), 1342 * or an ISDN device (whose link-layer 1343 * type we can only determine by using 1344 * APIs that may be different on different 1345 * kernels) - reopen in cooked mode. 1346 */ 1347 if (close(sock_fd) == -1) { 1348 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1349 "close: %s", pcap_strerror(errno)); 1350 break; 1351 } 1352 sock_fd = socket(PF_PACKET, SOCK_DGRAM, 1353 htons(ETH_P_ALL)); 1354 if (sock_fd == -1) { 1355 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1356 "socket: %s", pcap_strerror(errno)); 1357 break; 1358 } 1359 handle->md.cooked = 1; 1360 1361 /* 1362 * Get rid of any link-layer type list 1363 * we allocated - this only supports cooked 1364 * capture. 1365 */ 1366 if (handle->dlt_list != NULL) { 1367 free(handle->dlt_list); 1368 handle->dlt_list = NULL; 1369 handle->dlt_count = 0; 1370 } 1371 1372 if (handle->linktype == -1) { 1373 /* 1374 * Warn that we're falling back on 1375 * cooked mode; we may want to 1376 * update "map_arphrd_to_dlt()" 1377 * to handle the new type. 1378 */ 1379 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1380 "arptype %d not " 1381 "supported by libpcap - " 1382 "falling back to cooked " 1383 "socket", 1384 arptype); 1385 } 1386 /* IrDA capture is not a real "cooked" capture, 1387 * it's IrLAP frames, not IP packets. */ 1388 if (handle->linktype != DLT_LINUX_IRDA) 1389 handle->linktype = DLT_LINUX_SLL; 1390 } 1391 1392 handle->md.ifindex = iface_get_id(sock_fd, device, ebuf); 1393 if (handle->md.ifindex == -1) 1394 break; 1395 1396 if ((err = iface_bind(sock_fd, handle->md.ifindex, 1397 ebuf)) < 0) { 1398 if (err == -2) 1399 fatal_err = 1; 1400 break; 1401 } 1402 } else { 1403 /* 1404 * This is cooked mode. 1405 */ 1406 handle->md.cooked = 1; 1407 handle->linktype = DLT_LINUX_SLL; 1408 1409 /* 1410 * We're not bound to a device. 1411 * XXX - true? Or true only if we're using 1412 * the "any" device? 1413 * For now, we're using this as an indication 1414 * that we can't transmit; stop doing that only 1415 * if we figure out how to transmit in cooked 1416 * mode. 1417 */ 1418 handle->md.ifindex = -1; 1419 } 1420 1421 /* 1422 * Select promiscuous mode on if "promisc" is set. 1423 * 1424 * Do not turn allmulti mode on if we don't select 1425 * promiscuous mode - on some devices (e.g., Orinoco 1426 * wireless interfaces), allmulti mode isn't supported 1427 * and the driver implements it by turning promiscuous 1428 * mode on, and that screws up the operation of the 1429 * card as a normal networking interface, and on no 1430 * other platform I know of does starting a non- 1431 * promiscuous capture affect which multicast packets 1432 * are received by the interface. 1433 */ 1434 1435 /* 1436 * Hmm, how can we set promiscuous mode on all interfaces? 1437 * I am not sure if that is possible at all. 1438 */ 1439 1440 if (device && promisc) { 1441 memset(&mr, 0, sizeof(mr)); 1442 mr.mr_ifindex = handle->md.ifindex; 1443 mr.mr_type = PACKET_MR_PROMISC; 1444 if (setsockopt(sock_fd, SOL_PACKET, 1445 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1) 1446 { 1447 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1448 "setsockopt: %s", pcap_strerror(errno)); 1449 break; 1450 } 1451 } 1452 1453 /* Save the socket FD in the pcap structure */ 1454 1455 handle->fd = sock_fd; 1456 1457 return 1; 1458 1459 } while(0); 1460 1461 if (sock_fd != -1) 1462 close(sock_fd); 1463 1464 if (fatal_err) { 1465 /* 1466 * Get rid of any link-layer type list we allocated. 1467 */ 1468 if (handle->dlt_list != NULL) 1469 free(handle->dlt_list); 1470 return -2; 1471 } else 1472 return 0; 1473#else 1474 strncpy(ebuf, 1475 "New packet capturing interface not supported by build " 1476 "environment", PCAP_ERRBUF_SIZE); 1477 return 0; 1478#endif 1479} 1480 1481#ifdef HAVE_PF_PACKET_SOCKETS 1482/* 1483 * Return the index of the given device name. Fill ebuf and return 1484 * -1 on failure. 1485 */ 1486static int 1487iface_get_id(int fd, const char *device, char *ebuf) 1488{ 1489 struct ifreq ifr; 1490 1491 memset(&ifr, 0, sizeof(ifr)); 1492 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 1493 1494 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) { 1495 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1496 "ioctl: %s", pcap_strerror(errno)); 1497 return -1; 1498 } 1499 1500 return ifr.ifr_ifindex; 1501} 1502 1503/* 1504 * Bind the socket associated with FD to the given device. 1505 */ 1506static int 1507iface_bind(int fd, int ifindex, char *ebuf) 1508{ 1509 struct sockaddr_ll sll; 1510 int err; 1511 socklen_t errlen = sizeof(err); 1512 1513 memset(&sll, 0, sizeof(sll)); 1514 sll.sll_family = AF_PACKET; 1515 sll.sll_ifindex = ifindex; 1516 sll.sll_protocol = htons(ETH_P_ALL); 1517 1518 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) { 1519 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1520 "bind: %s", pcap_strerror(errno)); 1521 return -1; 1522 } 1523 1524 /* Any pending errors, e.g., network is down? */ 1525 1526 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { 1527 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1528 "getsockopt: %s", pcap_strerror(errno)); 1529 return -2; 1530 } 1531 1532 if (err > 0) { 1533 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1534 "bind: %s", pcap_strerror(err)); 1535 return -2; 1536 } 1537 1538 return 0; 1539} 1540 1541#endif 1542 1543 1544/* ===== Functions to interface to the older kernels ================== */ 1545 1546/* 1547 * With older kernels promiscuous mode is kind of interesting because we 1548 * have to reset the interface before exiting. The problem can't really 1549 * be solved without some daemon taking care of managing usage counts. 1550 * If we put the interface into promiscuous mode, we set a flag indicating 1551 * that we must take it out of that mode when the interface is closed, 1552 * and, when closing the interface, if that flag is set we take it out 1553 * of promiscuous mode. 1554 */ 1555 1556/* 1557 * List of pcaps for which we turned promiscuous mode on by hand. 1558 * If there are any such pcaps, we arrange to call "pcap_close_all()" 1559 * when we exit, and have it close all of them to turn promiscuous mode 1560 * off. 1561 */ 1562static struct pcap *pcaps_to_close; 1563 1564/* 1565 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to 1566 * be called on exit. 1567 */ 1568static int did_atexit; 1569 1570static void pcap_close_all(void) 1571{ 1572 struct pcap *handle; 1573 1574 while ((handle = pcaps_to_close) != NULL) 1575 pcap_close(handle); 1576} 1577 1578static void pcap_close_linux( pcap_t *handle ) 1579{ 1580 struct pcap *p, *prevp; 1581 struct ifreq ifr; 1582 1583 if (handle->md.clear_promisc) { 1584 /* 1585 * We put the interface into promiscuous mode; take 1586 * it out of promiscuous mode. 1587 * 1588 * XXX - if somebody else wants it in promiscuous mode, 1589 * this code cannot know that, so it'll take it out 1590 * of promiscuous mode. That's not fixable in 2.0[.x] 1591 * kernels. 1592 */ 1593 memset(&ifr, 0, sizeof(ifr)); 1594 strncpy(ifr.ifr_name, handle->md.device, sizeof(ifr.ifr_name)); 1595 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) { 1596 fprintf(stderr, 1597 "Can't restore interface flags (SIOCGIFFLAGS failed: %s).\n" 1598 "Please adjust manually.\n" 1599 "Hint: This can't happen with Linux >= 2.2.0.\n", 1600 strerror(errno)); 1601 } else { 1602 if (ifr.ifr_flags & IFF_PROMISC) { 1603 /* 1604 * Promiscuous mode is currently on; turn it 1605 * off. 1606 */ 1607 ifr.ifr_flags &= ~IFF_PROMISC; 1608 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) { 1609 fprintf(stderr, 1610 "Can't restore interface flags (SIOCSIFFLAGS failed: %s).\n" 1611 "Please adjust manually.\n" 1612 "Hint: This can't happen with Linux >= 2.2.0.\n", 1613 strerror(errno)); 1614 } 1615 } 1616 } 1617 1618 /* 1619 * Take this pcap out of the list of pcaps for which we 1620 * have to take the interface out of promiscuous mode. 1621 */ 1622 for (p = pcaps_to_close, prevp = NULL; p != NULL; 1623 prevp = p, p = p->md.next) { 1624 if (p == handle) { 1625 /* 1626 * Found it. Remove it from the list. 1627 */ 1628 if (prevp == NULL) { 1629 /* 1630 * It was at the head of the list. 1631 */ 1632 pcaps_to_close = p->md.next; 1633 } else { 1634 /* 1635 * It was in the middle of the list. 1636 */ 1637 prevp->md.next = p->md.next; 1638 } 1639 break; 1640 } 1641 } 1642 } 1643 1644 if (handle->md.device != NULL) 1645 free(handle->md.device); 1646 handle->md.device = NULL; 1647 pcap_close_common(handle); 1648} 1649 1650/* 1651 * Try to open a packet socket using the old kernel interface. 1652 * Returns 0 on failure. 1653 * FIXME: 0 uses to mean success (Sebastian) 1654 */ 1655static int 1656live_open_old(pcap_t *handle, const char *device, int promisc, 1657 int to_ms, char *ebuf) 1658{ 1659 int arptype; 1660 struct ifreq ifr; 1661 1662 do { 1663 /* Open the socket */ 1664 1665 handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL)); 1666 if (handle->fd == -1) { 1667 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1668 "socket: %s", pcap_strerror(errno)); 1669 break; 1670 } 1671 1672 /* It worked - we are using the old interface */ 1673 handle->md.sock_packet = 1; 1674 1675 /* ...which means we get the link-layer header. */ 1676 handle->md.cooked = 0; 1677 1678 /* Bind to the given device */ 1679 1680 if (!device) { 1681 strncpy(ebuf, "pcap_open_live: The \"any\" device isn't supported on 2.0[.x]-kernel systems", 1682 PCAP_ERRBUF_SIZE); 1683 break; 1684 } 1685 if (iface_bind_old(handle->fd, device, ebuf) == -1) 1686 break; 1687 1688 /* 1689 * Try to get the link-layer type. 1690 */ 1691 arptype = iface_get_arptype(handle->fd, device, ebuf); 1692 if (arptype == -1) 1693 break; 1694 1695 /* 1696 * Try to find the DLT_ type corresponding to that 1697 * link-layer type. 1698 */ 1699 map_arphrd_to_dlt(handle, arptype, 0); 1700 if (handle->linktype == -1) { 1701 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1702 "unknown arptype %d", arptype); 1703 break; 1704 } 1705 1706 /* Go to promisc mode if requested */ 1707 1708 if (promisc) { 1709 memset(&ifr, 0, sizeof(ifr)); 1710 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 1711 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) { 1712 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1713 "ioctl: %s", pcap_strerror(errno)); 1714 break; 1715 } 1716 if ((ifr.ifr_flags & IFF_PROMISC) == 0) { 1717 /* 1718 * Promiscuous mode isn't currently on, 1719 * so turn it on, and remember that 1720 * we should turn it off when the 1721 * pcap_t is closed. 1722 */ 1723 1724 /* 1725 * If we haven't already done so, arrange 1726 * to have "pcap_close_all()" called when 1727 * we exit. 1728 */ 1729 if (!did_atexit) { 1730 if (atexit(pcap_close_all) == -1) { 1731 /* 1732 * "atexit()" failed; don't 1733 * put the interface in 1734 * promiscuous mode, just 1735 * give up. 1736 */ 1737 strncpy(ebuf, "atexit failed", 1738 PCAP_ERRBUF_SIZE); 1739 break; 1740 } 1741 did_atexit = 1; 1742 } 1743 1744 ifr.ifr_flags |= IFF_PROMISC; 1745 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) { 1746 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1747 "ioctl: %s", 1748 pcap_strerror(errno)); 1749 break; 1750 } 1751 handle->md.clear_promisc = 1; 1752 1753 /* 1754 * Add this to the list of pcaps 1755 * to close when we exit. 1756 */ 1757 handle->md.next = pcaps_to_close; 1758 pcaps_to_close = handle; 1759 } 1760 } 1761 1762 /* 1763 * Default value for offset to align link-layer payload 1764 * on a 4-byte boundary. 1765 */ 1766 handle->offset = 0; 1767 1768 return 1; 1769 1770 } while (0); 1771 1772 pcap_close_linux(handle); 1773 return 0; 1774} 1775 1776/* 1777 * Bind the socket associated with FD to the given device using the 1778 * interface of the old kernels. 1779 */ 1780static int 1781iface_bind_old(int fd, const char *device, char *ebuf) 1782{ 1783 struct sockaddr saddr; 1784 int err; 1785 socklen_t errlen = sizeof(err); 1786 1787 memset(&saddr, 0, sizeof(saddr)); 1788 strncpy(saddr.sa_data, device, sizeof(saddr.sa_data)); 1789 if (bind(fd, &saddr, sizeof(saddr)) == -1) { 1790 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1791 "bind: %s", pcap_strerror(errno)); 1792 return -1; 1793 } 1794 1795 /* Any pending errors, e.g., network is down? */ 1796 1797 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { 1798 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1799 "getsockopt: %s", pcap_strerror(errno)); 1800 return -1; 1801 } 1802 1803 if (err > 0) { 1804 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1805 "bind: %s", pcap_strerror(err)); 1806 return -1; 1807 } 1808 1809 return 0; 1810} 1811 1812 1813/* ===== System calls available on all supported kernels ============== */ 1814 1815/* 1816 * Query the kernel for the MTU of the given interface. 1817 */ 1818static int 1819iface_get_mtu(int fd, const char *device, char *ebuf) 1820{ 1821 struct ifreq ifr; 1822 1823 if (!device) 1824 return BIGGER_THAN_ALL_MTUS; 1825 1826 memset(&ifr, 0, sizeof(ifr)); 1827 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 1828 1829 if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) { 1830 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1831 "ioctl: %s", pcap_strerror(errno)); 1832 return -1; 1833 } 1834 1835 return ifr.ifr_mtu; 1836} 1837 1838/* 1839 * Get the hardware type of the given interface as ARPHRD_xxx constant. 1840 */ 1841static int 1842iface_get_arptype(int fd, const char *device, char *ebuf) 1843{ 1844 struct ifreq ifr; 1845 1846 memset(&ifr, 0, sizeof(ifr)); 1847 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); 1848 1849 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { 1850 snprintf(ebuf, PCAP_ERRBUF_SIZE, 1851 "ioctl: %s", pcap_strerror(errno)); 1852 return -1; 1853 } 1854 1855 return ifr.ifr_hwaddr.sa_family; 1856} 1857 1858#ifdef SO_ATTACH_FILTER 1859static int 1860fix_program(pcap_t *handle, struct sock_fprog *fcode) 1861{ 1862 size_t prog_size; 1863 register int i; 1864 register struct bpf_insn *p; 1865 struct bpf_insn *f; 1866 int len; 1867 1868 /* 1869 * Make a copy of the filter, and modify that copy if 1870 * necessary. 1871 */ 1872 prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len; 1873 len = handle->fcode.bf_len; 1874 f = (struct bpf_insn *)malloc(prog_size); 1875 if (f == NULL) { 1876 snprintf(handle->errbuf, sizeof(handle->errbuf), 1877 "malloc: %s", pcap_strerror(errno)); 1878 return -1; 1879 } 1880 memcpy(f, handle->fcode.bf_insns, prog_size); 1881 fcode->len = len; 1882 fcode->filter = (struct sock_filter *) f; 1883 1884 for (i = 0; i < len; ++i) { 1885 p = &f[i]; 1886 /* 1887 * What type of instruction is this? 1888 */ 1889 switch (BPF_CLASS(p->code)) { 1890 1891 case BPF_RET: 1892 /* 1893 * It's a return instruction; is the snapshot 1894 * length a constant, rather than the contents 1895 * of the accumulator? 1896 */ 1897 if (BPF_MODE(p->code) == BPF_K) { 1898 /* 1899 * Yes - if the value to be returned, 1900 * i.e. the snapshot length, is anything 1901 * other than 0, make it 65535, so that 1902 * the packet is truncated by "recvfrom()", 1903 * not by the filter. 1904 * 1905 * XXX - there's nothing we can easily do 1906 * if it's getting the value from the 1907 * accumulator; we'd have to insert 1908 * code to force non-zero values to be 1909 * 65535. 1910 */ 1911 if (p->k != 0) 1912 p->k = 65535; 1913 } 1914 break; 1915 1916 case BPF_LD: 1917 case BPF_LDX: 1918 /* 1919 * It's a load instruction; is it loading 1920 * from the packet? 1921 */ 1922 switch (BPF_MODE(p->code)) { 1923 1924 case BPF_ABS: 1925 case BPF_IND: 1926 case BPF_MSH: 1927 /* 1928 * Yes; are we in cooked mode? 1929 */ 1930 if (handle->md.cooked) { 1931 /* 1932 * Yes, so we need to fix this 1933 * instruction. 1934 */ 1935 if (fix_offset(p) < 0) { 1936 /* 1937 * We failed to do so. 1938 * Return 0, so our caller 1939 * knows to punt to userland. 1940 */ 1941 return 0; 1942 } 1943 } 1944 break; 1945 } 1946 break; 1947 } 1948 } 1949 return 1; /* we succeeded */ 1950} 1951 1952static int 1953fix_offset(struct bpf_insn *p) 1954{ 1955 /* 1956 * What's the offset? 1957 */ 1958 if (p->k >= SLL_HDR_LEN) { 1959 /* 1960 * It's within the link-layer payload; that starts at an 1961 * offset of 0, as far as the kernel packet filter is 1962 * concerned, so subtract the length of the link-layer 1963 * header. 1964 */ 1965 p->k -= SLL_HDR_LEN; 1966 } else if (p->k == 14) { 1967 /* 1968 * It's the protocol field; map it to the special magic 1969 * kernel offset for that field. 1970 */ 1971 p->k = SKF_AD_OFF + SKF_AD_PROTOCOL; 1972 } else { 1973 /* 1974 * It's within the header, but it's not one of those 1975 * fields; we can't do that in the kernel, so punt 1976 * to userland. 1977 */ 1978 return -1; 1979 } 1980 return 0; 1981} 1982 1983static int 1984set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode) 1985{ 1986 int total_filter_on = 0; 1987 int save_mode; 1988 int ret; 1989 int save_errno; 1990 1991 /* 1992 * The socket filter code doesn't discard all packets queued 1993 * up on the socket when the filter is changed; this means 1994 * that packets that don't match the new filter may show up 1995 * after the new filter is put onto the socket, if those 1996 * packets haven't yet been read. 1997 * 1998 * This means, for example, that if you do a tcpdump capture 1999 * with a filter, the first few packets in the capture might 2000 * be packets that wouldn't have passed the filter. 2001 * 2002 * We therefore discard all packets queued up on the socket 2003 * when setting a kernel filter. (This isn't an issue for 2004 * userland filters, as the userland filtering is done after 2005 * packets are queued up.) 2006 * 2007 * To flush those packets, we put the socket in read-only mode, 2008 * and read packets from the socket until there are no more to 2009 * read. 2010 * 2011 * In order to keep that from being an infinite loop - i.e., 2012 * to keep more packets from arriving while we're draining 2013 * the queue - we put the "total filter", which is a filter 2014 * that rejects all packets, onto the socket before draining 2015 * the queue. 2016 * 2017 * This code deliberately ignores any errors, so that you may 2018 * get bogus packets if an error occurs, rather than having 2019 * the filtering done in userland even if it could have been 2020 * done in the kernel. 2021 */ 2022 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, 2023 &total_fcode, sizeof(total_fcode)) == 0) { 2024 char drain[1]; 2025 2026 /* 2027 * Note that we've put the total filter onto the socket. 2028 */ 2029 total_filter_on = 1; 2030 2031 /* 2032 * Save the socket's current mode, and put it in 2033 * non-blocking mode; we drain it by reading packets 2034 * until we get an error (which is normally a 2035 * "nothing more to be read" error). 2036 */ 2037 save_mode = fcntl(handle->fd, F_GETFL, 0); 2038 if (save_mode != -1 && 2039 fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) >= 0) { 2040 while (recv(handle->fd, &drain, sizeof drain, 2041 MSG_TRUNC) >= 0) 2042 ; 2043 save_errno = errno; 2044 fcntl(handle->fd, F_SETFL, save_mode); 2045 if (save_errno != EAGAIN) { 2046 /* Fatal error */ 2047 reset_kernel_filter(handle); 2048 snprintf(handle->errbuf, sizeof(handle->errbuf), 2049 "recv: %s", pcap_strerror(save_errno)); 2050 return -2; 2051 } 2052 } 2053 } 2054 2055 /* 2056 * Now attach the new filter. 2057 */ 2058 ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER, 2059 fcode, sizeof(*fcode)); 2060 if (ret == -1 && total_filter_on) { 2061 /* 2062 * Well, we couldn't set that filter on the socket, 2063 * but we could set the total filter on the socket. 2064 * 2065 * This could, for example, mean that the filter was 2066 * too big to put into the kernel, so we'll have to 2067 * filter in userland; in any case, we'll be doing 2068 * filtering in userland, so we need to remove the 2069 * total filter so we see packets. 2070 */ 2071 save_errno = errno; 2072 2073 /* 2074 * XXX - if this fails, we're really screwed; 2075 * we have the total filter on the socket, 2076 * and it won't come off. What do we do then? 2077 */ 2078 reset_kernel_filter(handle); 2079 2080 errno = save_errno; 2081 } 2082 return ret; 2083} 2084 2085static int 2086reset_kernel_filter(pcap_t *handle) 2087{ 2088 /* setsockopt() barfs unless it get a dummy parameter */ 2089 int dummy; 2090 2091 return setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER, 2092 &dummy, sizeof(dummy)); 2093} 2094#endif 2095