pcap-pf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29. #include <sys/timeb.h>
  30. #include <sys/socket.h>
  31. #include <sys/file.h>
  32. #include <sys/ioctl.h>
  33. #include <net/pfilt.h>
  34. struct mbuf;
  35. struct rtentry;
  36. #include <net/if.h>
  37. #include <netinet/in.h>
  38. #include <netinet/in_systm.h>
  39. #include <netinet/ip.h>
  40. #include <netinet/if_ether.h>
  41. #include <netinet/ip_var.h>
  42. #include <netinet/udp.h>
  43. #include <netinet/udp_var.h>
  44. #include <netinet/tcp.h>
  45. #include <netinet/tcpip.h>
  46. #include <ctype.h>
  47. #include <errno.h>
  48. #include <netdb.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. /*
  54. * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
  55. * native OS version, as we need various BPF ioctls from it.
  56. */
  57. #define PCAP_DONT_INCLUDE_PCAP_BPF_H
  58. #include <net/bpf.h>
  59. #include "pcap-int.h"
  60. #ifdef HAVE_OS_PROTO_H
  61. #include "os-proto.h"
  62. #endif
  63. /*
  64. * FDDI packets are padded to make everything line up on a nice boundary.
  65. */
  66. #define PCAP_FDDIPAD 3
  67. /*
  68. * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
  69. * Tru64 UNIX packetfilter devices.
  70. */
  71. struct pcap_pf {
  72. int filtering_in_kernel; /* using kernel filter */
  73. u_long TotPkts; /* can't oflow for 79 hrs on ether */
  74. u_long TotAccepted; /* count accepted by filter */
  75. u_long TotDrops; /* count of dropped packets */
  76. long TotMissed; /* missed by i/f during this run */
  77. long OrigMissed; /* missed by i/f before this run */
  78. };
  79. static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
  80. /*
  81. * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
  82. * applications aren't going to need more than 200 bytes of packet header
  83. * and the read shouldn't return more packets than packetfilter's internal
  84. * queue limit (bounded at 256).
  85. */
  86. #define BUFSPACE (200 * 256)
  87. static int
  88. pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
  89. {
  90. struct pcap_pf *pf = pc->priv;
  91. register u_char *p, *bp;
  92. register int cc, n, buflen, inc;
  93. register struct enstamp *sp;
  94. #ifdef LBL_ALIGN
  95. struct enstamp stamp;
  96. #endif
  97. register u_int pad;
  98. again:
  99. cc = pc->cc;
  100. if (cc == 0) {
  101. cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
  102. if (cc < 0) {
  103. if (errno == EWOULDBLOCK)
  104. return (0);
  105. if (errno == EINVAL &&
  106. lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
  107. /*
  108. * Due to a kernel bug, after 2^31 bytes,
  109. * the kernel file offset overflows and
  110. * read fails with EINVAL. The lseek()
  111. * to 0 will fix things.
  112. */
  113. (void)lseek(pc->fd, 0L, SEEK_SET);
  114. goto again;
  115. }
  116. pcap_fmt_errmsg_for_errno(pc->errbuf,
  117. sizeof(pc->errbuf), errno, "pf read");
  118. return (-1);
  119. }
  120. bp = (u_char *)pc->buffer + pc->offset;
  121. } else
  122. bp = pc->bp;
  123. /*
  124. * Loop through each packet.
  125. */
  126. n = 0;
  127. pad = pc->fddipad;
  128. while (cc > 0) {
  129. /*
  130. * Has "pcap_breakloop()" been called?
  131. * If so, return immediately - if we haven't read any
  132. * packets, clear the flag and return -2 to indicate
  133. * that we were told to break out of the loop, otherwise
  134. * leave the flag set, so that the *next* call will break
  135. * out of the loop without having read any packets, and
  136. * return the number of packets we've processed so far.
  137. */
  138. if (pc->break_loop) {
  139. if (n == 0) {
  140. pc->break_loop = 0;
  141. return (-2);
  142. } else {
  143. pc->cc = cc;
  144. pc->bp = bp;
  145. return (n);
  146. }
  147. }
  148. if (cc < sizeof(*sp)) {
  149. pcap_snprintf(pc->errbuf, sizeof(pc->errbuf),
  150. "pf short read (%d)", cc);
  151. return (-1);
  152. }
  153. #ifdef LBL_ALIGN
  154. if ((long)bp & 3) {
  155. sp = &stamp;
  156. memcpy((char *)sp, (char *)bp, sizeof(*sp));
  157. } else
  158. #endif
  159. sp = (struct enstamp *)bp;
  160. if (sp->ens_stamplen != sizeof(*sp)) {
  161. pcap_snprintf(pc->errbuf, sizeof(pc->errbuf),
  162. "pf short stamplen (%d)",
  163. sp->ens_stamplen);
  164. return (-1);
  165. }
  166. p = bp + sp->ens_stamplen;
  167. buflen = sp->ens_count;
  168. if (buflen > pc->snapshot)
  169. buflen = pc->snapshot;
  170. /* Calculate inc before possible pad update */
  171. inc = ENALIGN(buflen + sp->ens_stamplen);
  172. cc -= inc;
  173. bp += inc;
  174. pf->TotPkts++;
  175. pf->TotDrops += sp->ens_dropped;
  176. pf->TotMissed = sp->ens_ifoverflows;
  177. if (pf->OrigMissed < 0)
  178. pf->OrigMissed = pf->TotMissed;
  179. /*
  180. * Short-circuit evaluation: if using BPF filter
  181. * in kernel, no need to do it now - we already know
  182. * the packet passed the filter.
  183. *
  184. * Note: the filter code was generated assuming
  185. * that pc->fddipad was the amount of padding
  186. * before the header, as that's what's required
  187. * in the kernel, so we run the filter before
  188. * skipping that padding.
  189. */
  190. if (pf->filtering_in_kernel ||
  191. bpf_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
  192. struct pcap_pkthdr h;
  193. pf->TotAccepted++;
  194. h.ts = sp->ens_tstamp;
  195. h.len = sp->ens_count - pad;
  196. p += pad;
  197. buflen -= pad;
  198. h.caplen = buflen;
  199. (*callback)(user, &h, p);
  200. if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
  201. pc->cc = cc;
  202. pc->bp = bp;
  203. return (n);
  204. }
  205. }
  206. }
  207. pc->cc = 0;
  208. return (n);
  209. }
  210. static int
  211. pcap_inject_pf(pcap_t *p, const void *buf, size_t size)
  212. {
  213. int ret;
  214. ret = write(p->fd, buf, size);
  215. if (ret == -1) {
  216. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  217. errno, "send");
  218. return (-1);
  219. }
  220. return (ret);
  221. }
  222. static int
  223. pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
  224. {
  225. struct pcap_pf *pf = p->priv;
  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 = pf->TotAccepted;
  263. ps->ps_drop = pf->TotDrops;
  264. ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
  265. return (0);
  266. }
  267. /*
  268. * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
  269. * don't get DLT_DOCSIS defined.
  270. */
  271. #ifndef DLT_DOCSIS
  272. #define DLT_DOCSIS 143
  273. #endif
  274. static int
  275. pcap_activate_pf(pcap_t *p)
  276. {
  277. struct pcap_pf *pf = p->priv;
  278. short enmode;
  279. int backlog = -1; /* request the most */
  280. struct enfilter Filter;
  281. struct endevp devparams;
  282. int err;
  283. /*
  284. * Initially try a read/write open (to allow the inject
  285. * method to work). If that fails due to permission
  286. * issues, fall back to read-only. This allows a
  287. * non-root user to be granted specific access to pcap
  288. * capabilities via file permissions.
  289. *
  290. * XXX - we should have an API that has a flag that
  291. * controls whether to open read-only or read-write,
  292. * so that denial of permission to send (or inability
  293. * to send, if sending packets isn't supported on
  294. * the device in question) can be indicated at open
  295. * time.
  296. *
  297. * XXX - we assume here that "pfopen()" does not, in fact, modify
  298. * its argument, even though it takes a "char *" rather than a
  299. * "const char *" as its first argument. That appears to be
  300. * the case, at least on Digital UNIX 4.0.
  301. *
  302. * XXX - is there an error that means "no such device"? Is
  303. * there one that means "that device doesn't support pf"?
  304. */
  305. p->fd = pfopen(p->opt.device, O_RDWR);
  306. if (p->fd == -1 && errno == EACCES)
  307. p->fd = pfopen(p->opt.device, O_RDONLY);
  308. if (p->fd < 0) {
  309. if (errno == EACCES) {
  310. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  311. "pf open: %s: Permission denied\n"
  312. "your system may not be properly configured; see the packetfilter(4) man page",
  313. p->opt.device);
  314. err = PCAP_ERROR_PERM_DENIED;
  315. } else {
  316. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  317. errno, "pf open: %s", p->opt.device);
  318. err = PCAP_ERROR;
  319. }
  320. goto bad;
  321. }
  322. /*
  323. * Turn a negative snapshot value (invalid), a snapshot value of
  324. * 0 (unspecified), or a value bigger than the normal maximum
  325. * value, into the maximum allowed value.
  326. *
  327. * If some application really *needs* a bigger snapshot
  328. * length, we should just increase MAXIMUM_SNAPLEN.
  329. */
  330. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  331. p->snapshot = MAXIMUM_SNAPLEN;
  332. pf->OrigMissed = -1;
  333. enmode = ENTSTAMP|ENNONEXCL;
  334. if (!p->opt.immediate)
  335. enmode |= ENBATCH;
  336. if (p->opt.promisc)
  337. enmode |= ENPROMISC;
  338. if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
  339. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  340. errno, "EIOCMBIS");
  341. err = PCAP_ERROR;
  342. goto bad;
  343. }
  344. #ifdef ENCOPYALL
  345. /* Try to set COPYALL mode so that we see packets to ourself */
  346. enmode = ENCOPYALL;
  347. (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
  348. #endif
  349. /* set the backlog */
  350. if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
  351. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  352. errno, "EIOCSETW");
  353. err = PCAP_ERROR;
  354. goto bad;
  355. }
  356. /* discover interface type */
  357. if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
  358. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  359. errno, "EIOCDEVP");
  360. err = PCAP_ERROR;
  361. goto bad;
  362. }
  363. /* HACK: to compile prior to Ultrix 4.2 */
  364. #ifndef ENDT_FDDI
  365. #define ENDT_FDDI 4
  366. #endif
  367. switch (devparams.end_dev_type) {
  368. case ENDT_10MB:
  369. p->linktype = DLT_EN10MB;
  370. p->offset = 2;
  371. /*
  372. * This is (presumably) a real Ethernet capture; give it a
  373. * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
  374. * that an application can let you choose it, in case you're
  375. * capturing DOCSIS traffic that a Cisco Cable Modem
  376. * Termination System is putting out onto an Ethernet (it
  377. * doesn't put an Ethernet header onto the wire, it puts raw
  378. * DOCSIS frames out on the wire inside the low-level
  379. * Ethernet framing).
  380. */
  381. p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
  382. /*
  383. * If that fails, just leave the list empty.
  384. */
  385. if (p->dlt_list != NULL) {
  386. p->dlt_list[0] = DLT_EN10MB;
  387. p->dlt_list[1] = DLT_DOCSIS;
  388. p->dlt_count = 2;
  389. }
  390. break;
  391. case ENDT_FDDI:
  392. p->linktype = DLT_FDDI;
  393. break;
  394. #ifdef ENDT_SLIP
  395. case ENDT_SLIP:
  396. p->linktype = DLT_SLIP;
  397. break;
  398. #endif
  399. #ifdef ENDT_PPP
  400. case ENDT_PPP:
  401. p->linktype = DLT_PPP;
  402. break;
  403. #endif
  404. #ifdef ENDT_LOOPBACK
  405. case ENDT_LOOPBACK:
  406. /*
  407. * It appears to use Ethernet framing, at least on
  408. * Digital UNIX 4.0.
  409. */
  410. p->linktype = DLT_EN10MB;
  411. p->offset = 2;
  412. break;
  413. #endif
  414. #ifdef ENDT_TRN
  415. case ENDT_TRN:
  416. p->linktype = DLT_IEEE802;
  417. break;
  418. #endif
  419. default:
  420. /*
  421. * XXX - what about ENDT_IEEE802? The pfilt.h header
  422. * file calls this "IEEE 802 networks (non-Ethernet)",
  423. * but that doesn't specify a specific link layer type;
  424. * it could be 802.4, or 802.5 (except that 802.5 is
  425. * ENDT_TRN), or 802.6, or 802.11, or.... That's why
  426. * DLT_IEEE802 was hijacked to mean Token Ring in various
  427. * BSDs, and why we went along with that hijacking.
  428. *
  429. * XXX - what about ENDT_HDLC and ENDT_NULL?
  430. * Presumably, as ENDT_OTHER is just "Miscellaneous
  431. * framing", there's not much we can do, as that
  432. * doesn't specify a particular type of header.
  433. */
  434. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  435. "unknown data-link type %u", devparams.end_dev_type);
  436. err = PCAP_ERROR;
  437. goto bad;
  438. }
  439. /* set truncation */
  440. if (p->linktype == DLT_FDDI) {
  441. p->fddipad = PCAP_FDDIPAD;
  442. /* packetfilter includes the padding in the snapshot */
  443. p->snapshot += PCAP_FDDIPAD;
  444. } else
  445. p->fddipad = 0;
  446. if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
  447. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  448. errno, "EIOCTRUNCATE");
  449. err = PCAP_ERROR;
  450. goto bad;
  451. }
  452. /* accept all packets */
  453. memset(&Filter, 0, sizeof(Filter));
  454. Filter.enf_Priority = 37; /* anything > 2 */
  455. Filter.enf_FilterLen = 0; /* means "always true" */
  456. if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
  457. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  458. errno, "EIOCSETF");
  459. err = PCAP_ERROR;
  460. goto bad;
  461. }
  462. if (p->opt.timeout != 0) {
  463. struct timeval timeout;
  464. timeout.tv_sec = p->opt.timeout / 1000;
  465. timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
  466. if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
  467. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  468. errno, "EIOCSRTIMEOUT");
  469. err = PCAP_ERROR;
  470. goto bad;
  471. }
  472. }
  473. p->bufsize = BUFSPACE;
  474. p->buffer = malloc(p->bufsize + p->offset);
  475. if (p->buffer == NULL) {
  476. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  477. errno, "malloc");
  478. err = PCAP_ERROR;
  479. goto bad;
  480. }
  481. /*
  482. * "select()" and "poll()" work on packetfilter devices.
  483. */
  484. p->selectable_fd = p->fd;
  485. p->read_op = pcap_read_pf;
  486. p->inject_op = pcap_inject_pf;
  487. p->setfilter_op = pcap_setfilter_pf;
  488. p->setdirection_op = NULL; /* Not implemented. */
  489. p->set_datalink_op = NULL; /* can't change data link type */
  490. p->getnonblock_op = pcap_getnonblock_fd;
  491. p->setnonblock_op = pcap_setnonblock_fd;
  492. p->stats_op = pcap_stats_pf;
  493. return (0);
  494. bad:
  495. pcap_cleanup_live_common(p);
  496. return (err);
  497. }
  498. pcap_t *
  499. pcap_create_interface(const char *device _U_, char *ebuf)
  500. {
  501. pcap_t *p;
  502. p = pcap_create_common(ebuf, sizeof (struct pcap_pf));
  503. if (p == NULL)
  504. return (NULL);
  505. p->activate_op = pcap_activate_pf;
  506. return (p);
  507. }
  508. /*
  509. * XXX - is there an error from pfopen() that means "no such device"?
  510. * Is there one that means "that device doesn't support pf"?
  511. */
  512. static int
  513. can_be_bound(const char *name _U_)
  514. {
  515. return (1);
  516. }
  517. static int
  518. get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
  519. {
  520. /*
  521. * Nothing we can do other than mark loopback devices as "the
  522. * connected/disconnected status doesn't apply".
  523. *
  524. * XXX - is there a way to find out whether an adapter has
  525. * something plugged into it?
  526. */
  527. if (*flags & PCAP_IF_LOOPBACK) {
  528. /*
  529. * Loopback devices aren't wireless, and "connected"/
  530. * "disconnected" doesn't apply to them.
  531. */
  532. *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  533. return (0);
  534. }
  535. return (0);
  536. }
  537. int
  538. pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
  539. {
  540. return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
  541. get_if_flags));
  542. }
  543. static int
  544. pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
  545. {
  546. struct pcap_pf *pf = p->priv;
  547. struct bpf_version bv;
  548. /*
  549. * See if BIOCVERSION works. If not, we assume the kernel doesn't
  550. * support BPF-style filters (it's not documented in the bpf(7)
  551. * or packetfiler(7) man pages, but the code used to fail if
  552. * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
  553. * kernel filtering in DU 4.0, so presumably BIOCVERSION works
  554. * there, at least).
  555. */
  556. if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
  557. /*
  558. * OK, we have the version of the BPF interpreter;
  559. * is it the same major version as us, and the same
  560. * or better minor version?
  561. */
  562. if (bv.bv_major == BPF_MAJOR_VERSION &&
  563. bv.bv_minor >= BPF_MINOR_VERSION) {
  564. /*
  565. * Yes. Try to install the filter.
  566. */
  567. if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
  568. pcap_fmt_errmsg_for_errno(p->errbuf,
  569. sizeof(p->errbuf), errno, "BIOCSETF");
  570. return (-1);
  571. }
  572. /*
  573. * OK, that succeeded. We're doing filtering in
  574. * the kernel. (We assume we don't have a
  575. * userland filter installed - that'd require
  576. * a previous version check to have failed but
  577. * this one to succeed.)
  578. *
  579. * XXX - this message should be supplied to the
  580. * application as a warning of some sort,
  581. * except that if it's a GUI application, it's
  582. * not clear that it should be displayed in
  583. * a window to annoy the user.
  584. */
  585. fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
  586. pf->filtering_in_kernel = 1;
  587. /*
  588. * Discard any previously-received packets,
  589. * as they might have passed whatever filter
  590. * was formerly in effect, but might not pass
  591. * this filter (BIOCSETF discards packets buffered
  592. * in the kernel, so you can lose packets in any
  593. * case).
  594. */
  595. p->cc = 0;
  596. return (0);
  597. }
  598. /*
  599. * We can't use the kernel's BPF interpreter; don't give
  600. * up, just log a message and be inefficient.
  601. *
  602. * XXX - this should really be supplied to the application
  603. * as a warning of some sort.
  604. */
  605. fprintf(stderr,
  606. "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
  607. BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
  608. bv.bv_major, bv.bv_minor);
  609. }
  610. /*
  611. * We couldn't do filtering in the kernel; do it in userland.
  612. */
  613. if (install_bpf_program(p, fp) < 0)
  614. return (-1);
  615. /*
  616. * XXX - this message should be supplied by the application as
  617. * a warning of some sort.
  618. */
  619. fprintf(stderr, "tcpdump: Filtering in user process\n");
  620. pf->filtering_in_kernel = 0;
  621. return (0);
  622. }
  623. /*
  624. * Libpcap version string.
  625. */
  626. const char *
  627. pcap_lib_version(void)
  628. {
  629. return (PCAP_VERSION_STRING);
  630. }