1/* 2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and --- 10 unchanged lines hidden (view full) --- 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * packet filter subroutines for tcpdump 22 * Extraction/creation by Jeffrey Mogul, DECWRL 23 */ 24 25#ifndef lint 26static const char rcsid[] _U_ = |
27 "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.91 2005/02/26 21:58:06 guy Exp $ (LBL)"; |
28#endif 29 30#ifdef HAVE_CONFIG_H 31#include "config.h" 32#endif 33 34#include <sys/types.h> 35#include <sys/time.h> --- 88 unchanged lines hidden (view full) --- 124 bp = pc->buffer + pc->offset; 125 } else 126 bp = pc->bp; 127 /* 128 * Loop through each packet. 129 */ 130 n = 0; 131#ifdef PCAP_FDDIPAD |
132 pad = p->fddipad; |
133#endif 134 while (cc > 0) { 135 /* 136 * Has "pcap_breakloop()" been called? 137 * If so, return immediately - if we haven't read any 138 * packets, clear the flag and return -2 to indicate 139 * that we were told to break out of the loop, otherwise 140 * leave the flag set, so that the *next* call will break --- 33 unchanged lines hidden (view full) --- 174 buflen = sp->ens_count; 175 if (buflen > pc->snapshot) 176 buflen = pc->snapshot; 177 178 /* Calculate inc before possible pad update */ 179 inc = ENALIGN(buflen + sp->ens_stamplen); 180 cc -= inc; 181 bp += inc; |
182 pc->md.TotPkts++; 183 pc->md.TotDrops += sp->ens_dropped; 184 pc->md.TotMissed = sp->ens_ifoverflows; 185 if (pc->md.OrigMissed < 0) 186 pc->md.OrigMissed = pc->md.TotMissed; 187 188 /* 189 * Short-circuit evaluation: if using BPF filter 190 * in kernel, no need to do it now. |
191 * 192#ifdef PCAP_FDDIPAD 193 * Note: the filter code was generated assuming 194 * that p->fddipad was the amount of padding 195 * before the header, as that's what's required 196 * in the kernel, so we run the filter before 197 * skipping that padding. 198#endif |
199 */ 200 if (fcode == NULL || 201 bpf_filter(fcode, p, sp->ens_count, buflen)) { 202 struct pcap_pkthdr h; 203 pc->md.TotAccepted++; 204 h.ts = sp->ens_tstamp; 205#ifdef PCAP_FDDIPAD 206 h.len = sp->ens_count - pad; 207#else 208 h.len = sp->ens_count; 209#endif |
210#ifdef PCAP_FDDIPAD 211 p += pad; 212 buflen -= pad; 213#endif |
214 h.caplen = buflen; 215 (*callback)(user, &h, p); 216 if (++n >= cnt && cnt > 0) { 217 pc->cc = cc; 218 pc->bp = bp; 219 return (n); 220 } 221 } 222 } 223 pc->cc = 0; 224 return (n); 225} 226 227static int |
228pcap_inject_pf(pcap_t *p, const void *buf, size_t size) 229{ 230 int ret; 231 232 ret = write(p->fd, buf, size); 233 if (ret == -1) { 234 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 235 pcap_strerror(errno)); 236 return (-1); 237 } 238 return (ret); 239} 240 241static int |
242pcap_stats_pf(pcap_t *p, struct pcap_stat *ps) 243{ 244 245 /* 246 * If packet filtering is being done in the kernel: 247 * 248 * "ps_recv" counts only packets that passed the filter. 249 * This does not include packets dropped because we --- 29 unchanged lines hidden (view full) --- 279 * yet read from libpcap by the application. 280 */ 281 ps->ps_recv = p->md.TotAccepted; 282 ps->ps_drop = p->md.TotDrops; 283 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed; 284 return (0); 285} 286 |
287/* 288 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably 289 * don't get DLT_DOCSIS defined. 290 */ 291#ifndef DLT_DOCSIS 292#define DLT_DOCSIS 143 293#endif |
294 295pcap_t * 296pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 297 char *ebuf) 298{ 299 pcap_t *p; 300 short enmode; 301 int backlog = -1; /* request the most */ 302 struct enfilter Filter; 303 struct endevp devparams; 304 305 p = (pcap_t *)malloc(sizeof(*p)); 306 if (p == NULL) { 307 snprintf(ebuf, PCAP_ERRBUF_SIZE, 308 "pcap_open_live: %s", pcap_strerror(errno)); 309 return (0); 310 } 311 memset(p, 0, sizeof(*p)); |
312 /* |
313 * Initially try a read/write open (to allow the inject 314 * method to work). If that fails due to permission 315 * issues, fall back to read-only. This allows a 316 * non-root user to be granted specific access to pcap 317 * capabilities via file permissions. 318 * 319 * XXX - we should have an API that has a flag that 320 * controls whether to open read-only or read-write, 321 * so that denial of permission to send (or inability 322 * to send, if sending packets isn't supported on 323 * the device in question) can be indicated at open 324 * time. 325 * |
326 * XXX - we assume here that "pfopen()" does not, in fact, modify 327 * its argument, even though it takes a "char *" rather than a 328 * "const char *" as its first argument. That appears to be 329 * the case, at least on Digital UNIX 4.0. 330 */ |
331 p->fd = pfopen(device, O_RDWR); 332 if (p->fd == -1 && errno == EACCES) 333 p->fd = pfopen(device, O_RDONLY); |
334 if (p->fd < 0) { 335 snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\ 336your system may not be properly configured; see the packetfilter(4) man page\n", 337 device, pcap_strerror(errno)); 338 goto bad; 339 } 340 p->md.OrigMissed = -1; 341 enmode = ENTSTAMP|ENBATCH|ENNONEXCL; --- 25 unchanged lines hidden (view full) --- 367#ifndef ENDT_FDDI 368#define ENDT_FDDI 4 369#endif 370 switch (devparams.end_dev_type) { 371 372 case ENDT_10MB: 373 p->linktype = DLT_EN10MB; 374 p->offset = 2; |
375 /* 376 * This is (presumably) a real Ethernet capture; give it a 377 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 378 * that an application can let you choose it, in case you're 379 * capturing DOCSIS traffic that a Cisco Cable Modem 380 * Termination System is putting out onto an Ethernet (it 381 * doesn't put an Ethernet header onto the wire, it puts raw 382 * DOCSIS frames out on the wire inside the low-level 383 * Ethernet framing). 384 */ 385 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 386 /* 387 * If that fails, just leave the list empty. 388 */ 389 if (p->dlt_list != NULL) { 390 p->dlt_list[0] = DLT_EN10MB; 391 p->dlt_list[1] = DLT_DOCSIS; 392 p->dlt_count = 2; 393 } |
394 break; 395 396 case ENDT_FDDI: 397 p->linktype = DLT_FDDI; 398 break; 399 400#ifdef ENDT_SLIP 401 case ENDT_SLIP: --- 40 unchanged lines hidden (view full) --- 442 * doesn't specify a particular type of header. 443 */ 444 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u", 445 devparams.end_dev_type); 446 goto bad; 447 } 448 /* set truncation */ 449#ifdef PCAP_FDDIPAD |
450 if (p->linktype == DLT_FDDI) { 451 p->fddipad = PCAP_FDDIPAD: 452 |
453 /* packetfilter includes the padding in the snapshot */ |
454 snaplen += PCAP_FDDIPAD; 455 } else 456 p->fddipad = 0; |
457#endif 458 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) { 459 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s", 460 pcap_strerror(errno)); 461 goto bad; 462 } 463 p->snapshot = snaplen; 464 /* accept all packets */ --- 25 unchanged lines hidden (view full) --- 490 } 491 492 /* 493 * "select()" and "poll()" work on packetfilter devices. 494 */ 495 p->selectable_fd = p->fd; 496 497 p->read_op = pcap_read_pf; |
498 p->inject_op = pcap_inject_pf; |
499 p->setfilter_op = pcap_setfilter_pf; 500 p->set_datalink_op = NULL; /* can't change data link type */ 501 p->getnonblock_op = pcap_getnonblock_fd; 502 p->setnonblock_op = pcap_setnonblock_fd; 503 p->stats_op = pcap_stats_pf; |
504 p->close_op = pcap_close_common; |
505 506 return (p); 507 bad: 508 if (p->fd >= 0) 509 close(p->fd); |
510 /* 511 * Get rid of any link-layer type list we allocated. 512 */ 513 if (p->dlt_list != NULL) 514 free(p->dlt_list); |
515 free(p); 516 return (NULL); 517} 518 519int 520pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 521{ 522 return (0); --- 39 unchanged lines hidden (view full) --- 562 * XXX - this message should be supplied to the 563 * application as a warning of some sort, 564 * except that if it's a GUI application, it's 565 * not clear that it should be displayed in 566 * a window to annoy the user. 567 */ 568 fprintf(stderr, "tcpdump: Using kernel BPF filter\n"); 569 p->md.use_bpf = 1; |
570 571 /* 572 * Discard any previously-received packets, 573 * as they might have passed whatever filter 574 * was formerly in effect, but might not pass 575 * this filter (BIOCSETF discards packets buffered 576 * in the kernel, so you can lose packets in any 577 * case). 578 */ 579 p->cc = 0; |
580 return (0); 581 } 582 583 /* 584 * We can't use the kernel's BPF interpreter; don't give 585 * up, just log a message and be inefficient. 586 * 587 * XXX - this should really be supplied to the application --- 22 unchanged lines hidden --- |