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 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 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_ =
| 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 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 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.79.2.5 2003/11/22 00:32:55 guy Exp $ (LBL)";
| 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> 36#include <sys/timeb.h> 37#include <sys/socket.h> 38#include <sys/file.h> 39#include <sys/ioctl.h> 40#include <net/pfilt.h> 41 42struct mbuf; 43struct rtentry; 44#include <net/if.h> 45 46#include <netinet/in.h> 47#include <netinet/in_systm.h> 48#include <netinet/ip.h> 49#include <netinet/if_ether.h> 50#include <netinet/ip_var.h> 51#include <netinet/udp.h> 52#include <netinet/udp_var.h> 53#include <netinet/tcp.h> 54#include <netinet/tcpip.h> 55 56#include <ctype.h> 57#include <errno.h> 58#include <netdb.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <unistd.h> 63 64/* 65 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the 66 * native OS version, as we need various BPF ioctls from it. 67 */ 68#define PCAP_DONT_INCLUDE_PCAP_BPF_H 69#include <net/bpf.h> 70 71#include "pcap-int.h" 72 73#ifdef HAVE_OS_PROTO_H 74#include "os-proto.h" 75#endif 76 77static int pcap_setfilter_pf(pcap_t *, struct bpf_program *); 78 79/* 80 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump 81 * applications aren't going to need more than 200 bytes of packet header 82 * and the read shouldn't return more packets than packetfilter's internal 83 * queue limit (bounded at 256). 84 */ 85#define BUFSPACE (200 * 256) 86 87static int 88pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) 89{ 90 register u_char *p, *bp; 91 struct bpf_insn *fcode; 92 register int cc, n, buflen, inc; 93 register struct enstamp *sp; 94#ifdef LBL_ALIGN 95 struct enstamp stamp; 96#endif 97#ifdef PCAP_FDDIPAD 98 register int pad; 99#endif 100 101 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns; 102 again: 103 cc = pc->cc; 104 if (cc == 0) { 105 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize); 106 if (cc < 0) { 107 if (errno == EWOULDBLOCK) 108 return (0); 109 if (errno == EINVAL && 110 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) { 111 /* 112 * Due to a kernel bug, after 2^31 bytes, 113 * the kernel file offset overflows and 114 * read fails with EINVAL. The lseek() 115 * to 0 will fix things. 116 */ 117 (void)lseek(pc->fd, 0L, SEEK_SET); 118 goto again; 119 } 120 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s", 121 pcap_strerror(errno)); 122 return (-1); 123 } 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
| 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> 36#include <sys/timeb.h> 37#include <sys/socket.h> 38#include <sys/file.h> 39#include <sys/ioctl.h> 40#include <net/pfilt.h> 41 42struct mbuf; 43struct rtentry; 44#include <net/if.h> 45 46#include <netinet/in.h> 47#include <netinet/in_systm.h> 48#include <netinet/ip.h> 49#include <netinet/if_ether.h> 50#include <netinet/ip_var.h> 51#include <netinet/udp.h> 52#include <netinet/udp_var.h> 53#include <netinet/tcp.h> 54#include <netinet/tcpip.h> 55 56#include <ctype.h> 57#include <errno.h> 58#include <netdb.h> 59#include <stdio.h> 60#include <stdlib.h> 61#include <string.h> 62#include <unistd.h> 63 64/* 65 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the 66 * native OS version, as we need various BPF ioctls from it. 67 */ 68#define PCAP_DONT_INCLUDE_PCAP_BPF_H 69#include <net/bpf.h> 70 71#include "pcap-int.h" 72 73#ifdef HAVE_OS_PROTO_H 74#include "os-proto.h" 75#endif 76 77static int pcap_setfilter_pf(pcap_t *, struct bpf_program *); 78 79/* 80 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump 81 * applications aren't going to need more than 200 bytes of packet header 82 * and the read shouldn't return more packets than packetfilter's internal 83 * queue limit (bounded at 256). 84 */ 85#define BUFSPACE (200 * 256) 86 87static int 88pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) 89{ 90 register u_char *p, *bp; 91 struct bpf_insn *fcode; 92 register int cc, n, buflen, inc; 93 register struct enstamp *sp; 94#ifdef LBL_ALIGN 95 struct enstamp stamp; 96#endif 97#ifdef PCAP_FDDIPAD 98 register int pad; 99#endif 100 101 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns; 102 again: 103 cc = pc->cc; 104 if (cc == 0) { 105 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize); 106 if (cc < 0) { 107 if (errno == EWOULDBLOCK) 108 return (0); 109 if (errno == EINVAL && 110 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) { 111 /* 112 * Due to a kernel bug, after 2^31 bytes, 113 * the kernel file offset overflows and 114 * read fails with EINVAL. The lseek() 115 * to 0 will fix things. 116 */ 117 (void)lseek(pc->fd, 0L, SEEK_SET); 118 goto again; 119 } 120 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s", 121 pcap_strerror(errno)); 122 return (-1); 123 } 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 if (pc->linktype == DLT_FDDI) 133 pad = pcap_fddipad; 134 else 135 pad = 0;
| 132 pad = p->fddipad;
|
136#endif 137 while (cc > 0) { 138 /* 139 * Has "pcap_breakloop()" been called? 140 * If so, return immediately - if we haven't read any 141 * packets, clear the flag and return -2 to indicate 142 * that we were told to break out of the loop, otherwise 143 * leave the flag set, so that the *next* call will break 144 * out of the loop without having read any packets, and 145 * return the number of packets we've processed so far. 146 */ 147 if (pc->break_loop) { 148 if (n == 0) { 149 pc->break_loop = 0; 150 return (-2); 151 } else { 152 pc->cc = cc; 153 pc->bp = bp; 154 return (n); 155 } 156 } 157 if (cc < sizeof(*sp)) { 158 snprintf(pc->errbuf, sizeof(pc->errbuf), 159 "pf short read (%d)", cc); 160 return (-1); 161 } 162#ifdef LBL_ALIGN 163 if ((long)bp & 3) { 164 sp = &stamp; 165 memcpy((char *)sp, (char *)bp, sizeof(*sp)); 166 } else 167#endif 168 sp = (struct enstamp *)bp; 169 if (sp->ens_stamplen != sizeof(*sp)) { 170 snprintf(pc->errbuf, sizeof(pc->errbuf), 171 "pf short stamplen (%d)", 172 sp->ens_stamplen); 173 return (-1); 174 } 175 176 p = bp + sp->ens_stamplen; 177 buflen = sp->ens_count; 178 if (buflen > pc->snapshot) 179 buflen = pc->snapshot; 180 181 /* Calculate inc before possible pad update */ 182 inc = ENALIGN(buflen + sp->ens_stamplen); 183 cc -= inc; 184 bp += inc;
| 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 141 * out of the loop without having read any packets, and 142 * return the number of packets we've processed so far. 143 */ 144 if (pc->break_loop) { 145 if (n == 0) { 146 pc->break_loop = 0; 147 return (-2); 148 } else { 149 pc->cc = cc; 150 pc->bp = bp; 151 return (n); 152 } 153 } 154 if (cc < sizeof(*sp)) { 155 snprintf(pc->errbuf, sizeof(pc->errbuf), 156 "pf short read (%d)", cc); 157 return (-1); 158 } 159#ifdef LBL_ALIGN 160 if ((long)bp & 3) { 161 sp = &stamp; 162 memcpy((char *)sp, (char *)bp, sizeof(*sp)); 163 } else 164#endif 165 sp = (struct enstamp *)bp; 166 if (sp->ens_stamplen != sizeof(*sp)) { 167 snprintf(pc->errbuf, sizeof(pc->errbuf), 168 "pf short stamplen (%d)", 169 sp->ens_stamplen); 170 return (-1); 171 } 172 173 p = bp + sp->ens_stamplen; 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;
|
185#ifdef PCAP_FDDIPAD 186 p += pad; 187 buflen -= pad; 188#endif
| |
189 pc->md.TotPkts++; 190 pc->md.TotDrops += sp->ens_dropped; 191 pc->md.TotMissed = sp->ens_ifoverflows; 192 if (pc->md.OrigMissed < 0) 193 pc->md.OrigMissed = pc->md.TotMissed; 194 195 /* 196 * Short-circuit evaluation: if using BPF filter 197 * in kernel, no need to do it now.
| 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
|
198 */ 199 if (fcode == NULL || 200 bpf_filter(fcode, p, sp->ens_count, buflen)) { 201 struct pcap_pkthdr h; 202 pc->md.TotAccepted++; 203 h.ts = sp->ens_tstamp; 204#ifdef PCAP_FDDIPAD 205 h.len = sp->ens_count - pad; 206#else 207 h.len = sp->ens_count; 208#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
|
209 h.caplen = buflen; 210 (*callback)(user, &h, p); 211 if (++n >= cnt && cnt > 0) { 212 pc->cc = cc; 213 pc->bp = bp; 214 return (n); 215 } 216 } 217 } 218 pc->cc = 0; 219 return (n); 220} 221 222static int
| 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
|
223pcap_stats_pf(pcap_t *p, struct pcap_stat *ps) 224{ 225 226 /* 227 * If packet filtering is being done in the kernel: 228 * 229 * "ps_recv" counts only packets that passed the filter. 230 * This does not include packets dropped because we 231 * ran out of buffer space. (XXX - perhaps it should, 232 * by adding "ps_drop" to "ps_recv", for compatibility 233 * with some other platforms. On the other hand, on 234 * some platforms "ps_recv" counts only packets that 235 * passed the filter, and on others it counts packets 236 * that didn't pass the filter....) 237 * 238 * "ps_drop" counts packets that passed the kernel filter 239 * (if any) but were dropped because the input queue was 240 * full. 241 * 242 * "ps_ifdrop" counts packets dropped by the network 243 * inteface (regardless of whether they would have passed 244 * the input filter, of course). 245 * 246 * If packet filtering is not being done in the kernel: 247 * 248 * "ps_recv" counts only packets that passed the filter. 249 * 250 * "ps_drop" counts packets that were dropped because the 251 * input queue was full, regardless of whether they passed 252 * the userland filter. 253 * 254 * "ps_ifdrop" counts packets dropped by the network 255 * inteface (regardless of whether they would have passed 256 * the input filter, of course). 257 * 258 * These statistics don't include packets not yet read from 259 * the kernel by libpcap, but they may include packets not 260 * yet read from libpcap by the application. 261 */ 262 ps->ps_recv = p->md.TotAccepted; 263 ps->ps_drop = p->md.TotDrops; 264 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed; 265 return (0); 266} 267
| 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 250 * ran out of buffer space. (XXX - perhaps it should, 251 * by adding "ps_drop" to "ps_recv", for compatibility 252 * with some other platforms. On the other hand, on 253 * some platforms "ps_recv" counts only packets that 254 * passed the filter, and on others it counts packets 255 * that didn't pass the filter....) 256 * 257 * "ps_drop" counts packets that passed the kernel filter 258 * (if any) but were dropped because the input queue was 259 * full. 260 * 261 * "ps_ifdrop" counts packets dropped by the network 262 * inteface (regardless of whether they would have passed 263 * the input filter, of course). 264 * 265 * If packet filtering is not being done in the kernel: 266 * 267 * "ps_recv" counts only packets that passed the filter. 268 * 269 * "ps_drop" counts packets that were dropped because the 270 * input queue was full, regardless of whether they passed 271 * the userland filter. 272 * 273 * "ps_ifdrop" counts packets dropped by the network 274 * inteface (regardless of whether they would have passed 275 * the input filter, of course). 276 * 277 * These statistics don't include packets not yet read from 278 * the kernel by libpcap, but they may include packets not 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
|
268static void 269pcap_close_pf(pcap_t *p) 270{ 271 if (p->buffer != NULL) 272 free(p->buffer); 273 if (p->fd >= 0) 274 close(p->fd); 275}
| 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
|
276 277pcap_t * 278pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 279 char *ebuf) 280{ 281 pcap_t *p; 282 short enmode; 283 int backlog = -1; /* request the most */ 284 struct enfilter Filter; 285 struct endevp devparams; 286 287 p = (pcap_t *)malloc(sizeof(*p)); 288 if (p == NULL) { 289 snprintf(ebuf, PCAP_ERRBUF_SIZE, 290 "pcap_open_live: %s", pcap_strerror(errno)); 291 return (0); 292 } 293 memset(p, 0, sizeof(*p));
| 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));
|
294
| |
295 /*
| 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 *
|
296 * XXX - we assume here that "pfopen()" does not, in fact, modify 297 * its argument, even though it takes a "char *" rather than a 298 * "const char *" as its first argument. That appears to be 299 * the case, at least on Digital UNIX 4.0. 300 */
| 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 */
|
301 p->fd = pfopen(device, O_RDONLY);
| 331 p->fd = pfopen(device, O_RDWR); 332 if (p->fd == -1 && errno == EACCES) 333 p->fd = pfopen(device, O_RDONLY);
|
302 if (p->fd < 0) { 303 snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\ 304your system may not be properly configured; see the packetfilter(4) man page\n", 305 device, pcap_strerror(errno)); 306 goto bad; 307 } 308 p->md.OrigMissed = -1; 309 enmode = ENTSTAMP|ENBATCH|ENNONEXCL; 310 if (promisc) 311 enmode |= ENPROMISC; 312 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) { 313 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s", 314 pcap_strerror(errno)); 315 goto bad; 316 } 317#ifdef ENCOPYALL 318 /* Try to set COPYALL mode so that we see packets to ourself */ 319 enmode = ENCOPYALL; 320 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */ 321#endif 322 /* set the backlog */ 323 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) { 324 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s", 325 pcap_strerror(errno)); 326 goto bad; 327 } 328 /* discover interface type */ 329 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) { 330 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s", 331 pcap_strerror(errno)); 332 goto bad; 333 } 334 /* HACK: to compile prior to Ultrix 4.2 */ 335#ifndef ENDT_FDDI 336#define ENDT_FDDI 4 337#endif 338 switch (devparams.end_dev_type) { 339 340 case ENDT_10MB: 341 p->linktype = DLT_EN10MB; 342 p->offset = 2;
| 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; 342 if (promisc) 343 enmode |= ENPROMISC; 344 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) { 345 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s", 346 pcap_strerror(errno)); 347 goto bad; 348 } 349#ifdef ENCOPYALL 350 /* Try to set COPYALL mode so that we see packets to ourself */ 351 enmode = ENCOPYALL; 352 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */ 353#endif 354 /* set the backlog */ 355 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) { 356 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s", 357 pcap_strerror(errno)); 358 goto bad; 359 } 360 /* discover interface type */ 361 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) { 362 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s", 363 pcap_strerror(errno)); 364 goto bad; 365 } 366 /* HACK: to compile prior to Ultrix 4.2 */ 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 }
|
343 break; 344 345 case ENDT_FDDI: 346 p->linktype = DLT_FDDI; 347 break; 348 349#ifdef ENDT_SLIP 350 case ENDT_SLIP: 351 p->linktype = DLT_SLIP; 352 break; 353#endif 354 355#ifdef ENDT_PPP 356 case ENDT_PPP: 357 p->linktype = DLT_PPP; 358 break; 359#endif 360 361#ifdef ENDT_LOOPBACK 362 case ENDT_LOOPBACK: 363 /* 364 * It appears to use Ethernet framing, at least on 365 * Digital UNIX 4.0. 366 */ 367 p->linktype = DLT_EN10MB; 368 p->offset = 2; 369 break; 370#endif 371 372#ifdef ENDT_TRN 373 case ENDT_TRN: 374 p->linktype = DLT_IEEE802; 375 break; 376#endif 377 378 default: 379 /* 380 * XXX - what about ENDT_IEEE802? The pfilt.h header 381 * file calls this "IEEE 802 networks (non-Ethernet)", 382 * but that doesn't specify a specific link layer type; 383 * it could be 802.4, or 802.5 (except that 802.5 is 384 * ENDT_TRN), or 802.6, or 802.11, or.... That's why 385 * DLT_IEEE802 was hijacked to mean Token Ring in various 386 * BSDs, and why we went along with that hijacking. 387 * 388 * XXX - what about ENDT_HDLC and ENDT_NULL? 389 * Presumably, as ENDT_OTHER is just "Miscellaneous 390 * framing", there's not much we can do, as that 391 * doesn't specify a particular type of header. 392 */ 393 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u", 394 devparams.end_dev_type); 395 goto bad; 396 } 397 /* set truncation */ 398#ifdef PCAP_FDDIPAD
| 394 break; 395 396 case ENDT_FDDI: 397 p->linktype = DLT_FDDI; 398 break; 399 400#ifdef ENDT_SLIP 401 case ENDT_SLIP: 402 p->linktype = DLT_SLIP; 403 break; 404#endif 405 406#ifdef ENDT_PPP 407 case ENDT_PPP: 408 p->linktype = DLT_PPP; 409 break; 410#endif 411 412#ifdef ENDT_LOOPBACK 413 case ENDT_LOOPBACK: 414 /* 415 * It appears to use Ethernet framing, at least on 416 * Digital UNIX 4.0. 417 */ 418 p->linktype = DLT_EN10MB; 419 p->offset = 2; 420 break; 421#endif 422 423#ifdef ENDT_TRN 424 case ENDT_TRN: 425 p->linktype = DLT_IEEE802; 426 break; 427#endif 428 429 default: 430 /* 431 * XXX - what about ENDT_IEEE802? The pfilt.h header 432 * file calls this "IEEE 802 networks (non-Ethernet)", 433 * but that doesn't specify a specific link layer type; 434 * it could be 802.4, or 802.5 (except that 802.5 is 435 * ENDT_TRN), or 802.6, or 802.11, or.... That's why 436 * DLT_IEEE802 was hijacked to mean Token Ring in various 437 * BSDs, and why we went along with that hijacking. 438 * 439 * XXX - what about ENDT_HDLC and ENDT_NULL? 440 * Presumably, as ENDT_OTHER is just "Miscellaneous 441 * framing", there's not much we can do, as that 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
|
399 if (p->linktype == DLT_FDDI)
| 450 if (p->linktype == DLT_FDDI) { 451 p->fddipad = PCAP_FDDIPAD: 452
|
400 /* packetfilter includes the padding in the snapshot */
| 453 /* packetfilter includes the padding in the snapshot */
|
401 snaplen += pcap_fddipad;
| 454 snaplen += PCAP_FDDIPAD; 455 } else 456 p->fddipad = 0;
|
402#endif 403 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) { 404 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s", 405 pcap_strerror(errno)); 406 goto bad; 407 } 408 p->snapshot = snaplen; 409 /* accept all packets */ 410 memset(&Filter, 0, sizeof(Filter)); 411 Filter.enf_Priority = 37; /* anything > 2 */ 412 Filter.enf_FilterLen = 0; /* means "always true" */ 413 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) { 414 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s", 415 pcap_strerror(errno)); 416 goto bad; 417 } 418 419 if (to_ms != 0) { 420 struct timeval timeout; 421 timeout.tv_sec = to_ms / 1000; 422 timeout.tv_usec = (to_ms * 1000) % 1000000; 423 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) { 424 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s", 425 pcap_strerror(errno)); 426 goto bad; 427 } 428 } 429 430 p->bufsize = BUFSPACE; 431 p->buffer = (u_char*)malloc(p->bufsize + p->offset); 432 if (p->buffer == NULL) { 433 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 434 goto bad; 435 } 436 437 /* 438 * "select()" and "poll()" work on packetfilter devices. 439 */ 440 p->selectable_fd = p->fd; 441 442 p->read_op = pcap_read_pf;
| 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 */ 465 memset(&Filter, 0, sizeof(Filter)); 466 Filter.enf_Priority = 37; /* anything > 2 */ 467 Filter.enf_FilterLen = 0; /* means "always true" */ 468 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) { 469 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s", 470 pcap_strerror(errno)); 471 goto bad; 472 } 473 474 if (to_ms != 0) { 475 struct timeval timeout; 476 timeout.tv_sec = to_ms / 1000; 477 timeout.tv_usec = (to_ms * 1000) % 1000000; 478 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) { 479 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s", 480 pcap_strerror(errno)); 481 goto bad; 482 } 483 } 484 485 p->bufsize = BUFSPACE; 486 p->buffer = (u_char*)malloc(p->bufsize + p->offset); 487 if (p->buffer == NULL) { 488 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 489 goto bad; 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;
|
443 p->setfilter_op = pcap_setfilter_pf; 444 p->set_datalink_op = NULL; /* can't change data link type */ 445 p->getnonblock_op = pcap_getnonblock_fd; 446 p->setnonblock_op = pcap_setnonblock_fd; 447 p->stats_op = pcap_stats_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;
|
448 p->close_op = pcap_close_pf;
| 504 p->close_op = pcap_close_common;
|
449 450 return (p); 451 bad: 452 if (p->fd >= 0) 453 close(p->fd);
| 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);
|
454 free(p); 455 return (NULL); 456} 457 458int 459pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 460{ 461 return (0); 462} 463 464static int 465pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp) 466{ 467 struct bpf_version bv; 468 469 /* 470 * See if BIOCVERSION works. If not, we assume the kernel doesn't 471 * support BPF-style filters (it's not documented in the bpf(7) 472 * or packetfiler(7) man pages, but the code used to fail if 473 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do 474 * kernel filtering in DU 4.0, so presumably BIOCVERSION works 475 * there, at least). 476 */ 477 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) { 478 /* 479 * OK, we have the version of the BPF interpreter; 480 * is it the same major version as us, and the same 481 * or better minor version? 482 */ 483 if (bv.bv_major == BPF_MAJOR_VERSION && 484 bv.bv_minor >= BPF_MINOR_VERSION) { 485 /* 486 * Yes. Try to install the filter. 487 */ 488 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) { 489 snprintf(p->errbuf, sizeof(p->errbuf), 490 "BIOCSETF: %s", pcap_strerror(errno)); 491 return (-1); 492 } 493 494 /* 495 * OK, that succeeded. We're doing filtering in 496 * the kernel. (We assume we don't have a 497 * userland filter installed - that'd require 498 * a previous version check to have failed but 499 * this one to succeed.) 500 * 501 * XXX - this message should be supplied to the 502 * application as a warning of some sort, 503 * except that if it's a GUI application, it's 504 * not clear that it should be displayed in 505 * a window to annoy the user. 506 */ 507 fprintf(stderr, "tcpdump: Using kernel BPF filter\n"); 508 p->md.use_bpf = 1;
| 515 free(p); 516 return (NULL); 517} 518 519int 520pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 521{ 522 return (0); 523} 524 525static int 526pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp) 527{ 528 struct bpf_version bv; 529 530 /* 531 * See if BIOCVERSION works. If not, we assume the kernel doesn't 532 * support BPF-style filters (it's not documented in the bpf(7) 533 * or packetfiler(7) man pages, but the code used to fail if 534 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do 535 * kernel filtering in DU 4.0, so presumably BIOCVERSION works 536 * there, at least). 537 */ 538 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) { 539 /* 540 * OK, we have the version of the BPF interpreter; 541 * is it the same major version as us, and the same 542 * or better minor version? 543 */ 544 if (bv.bv_major == BPF_MAJOR_VERSION && 545 bv.bv_minor >= BPF_MINOR_VERSION) { 546 /* 547 * Yes. Try to install the filter. 548 */ 549 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) { 550 snprintf(p->errbuf, sizeof(p->errbuf), 551 "BIOCSETF: %s", pcap_strerror(errno)); 552 return (-1); 553 } 554 555 /* 556 * OK, that succeeded. We're doing filtering in 557 * the kernel. (We assume we don't have a 558 * userland filter installed - that'd require 559 * a previous version check to have failed but 560 * this one to succeed.) 561 * 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;
|
509 return (0); 510 } 511 512 /* 513 * We can't use the kernel's BPF interpreter; don't give 514 * up, just log a message and be inefficient. 515 * 516 * XXX - this should really be supplied to the application 517 * as a warning of some sort. 518 */ 519 fprintf(stderr, 520 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n", 521 BPF_MAJOR_VERSION, BPF_MINOR_VERSION, 522 bv.bv_major, bv.bv_minor); 523 } 524 525 /* 526 * We couldn't do filtering in the kernel; do it in userland. 527 */ 528 if (install_bpf_program(p, fp) < 0) 529 return (-1); 530 531 /* 532 * XXX - this message should be supplied by the application as 533 * a warning of some sort. 534 */ 535 fprintf(stderr, "tcpdump: Filtering in user process\n"); 536 p->md.use_bpf = 0; 537 return (0); 538}
| 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 588 * as a warning of some sort. 589 */ 590 fprintf(stderr, 591 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n", 592 BPF_MAJOR_VERSION, BPF_MINOR_VERSION, 593 bv.bv_major, bv.bv_minor); 594 } 595 596 /* 597 * We couldn't do filtering in the kernel; do it in userland. 598 */ 599 if (install_bpf_program(p, fp) < 0) 600 return (-1); 601 602 /* 603 * XXX - this message should be supplied by the application as 604 * a warning of some sort. 605 */ 606 fprintf(stderr, "tcpdump: Filtering in user process\n"); 607 p->md.use_bpf = 0; 608 return (0); 609}
|