PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/uClinux-dist/user/nessus/nessus-libraries/libpcap-nessus/pcap-bpf.c

https://bitbucket.org/__wp__/mb-linux-msli
C | 263 lines | 187 code | 28 blank | 48 comment | 34 complexity | f075742c24c9428ea17371e23b8a303e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.0, MPL-2.0, ISC, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, 0BSD, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0, AGPL-1.0, Unlicense
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1998
  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. #ifndef lint
  22. static const char rcsid[] =
  23. "@(#) $Header: /usr/local/cvs/nessus-libraries/libpcap-nessus/pcap-bpf.c,v 1.3 2003/02/06 20:28:08 renaud Exp $ (LBL)";
  24. #endif
  25. #include <sys/param.h> /* optionally get BSD define */
  26. #include <sys/time.h>
  27. #include <sys/timeb.h>
  28. #include <sys/socket.h>
  29. #include <sys/file.h>
  30. #include <sys/ioctl.h>
  31. #include <net/if.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <netdb.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "pcap-int.h"
  40. #include "gnuc.h"
  41. #ifdef HAVE_OS_PROTO_H
  42. #include "os-proto.h"
  43. #endif
  44. int
  45. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  46. {
  47. struct bpf_stat s;
  48. if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
  49. sprintf(p->errbuf, "BIOCGSTATS: %s", pcap_strerror(errno));
  50. return (-1);
  51. }
  52. ps->ps_recv = s.bs_recv;
  53. ps->ps_drop = s.bs_drop;
  54. return (0);
  55. }
  56. int
  57. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  58. {
  59. int cc;
  60. int n = 0;
  61. register u_char *bp, *ep;
  62. again:
  63. cc = p->cc;
  64. if (p->cc == 0) {
  65. cc = read(p->fd, (char *)p->buffer, p->bufsize);
  66. if (cc < 0) {
  67. /* Don't choke when we get ptraced */
  68. switch (errno) {
  69. case EINTR:
  70. goto again;
  71. case EWOULDBLOCK:
  72. return (0);
  73. #if defined(sun) && !defined(BSD)
  74. /*
  75. * Due to a SunOS bug, after 2^31 bytes, the kernel
  76. * file offset overflows and read fails with EINVAL.
  77. * The lseek() to 0 will fix things.
  78. */
  79. case EINVAL:
  80. if (lseek(p->fd, 0L, SEEK_CUR) +
  81. p->bufsize < 0) {
  82. (void)lseek(p->fd, 0L, SEEK_SET);
  83. goto again;
  84. }
  85. /* fall through */
  86. #endif
  87. }
  88. sprintf(p->errbuf, "read: %s", pcap_strerror(errno));
  89. return (-1);
  90. }
  91. bp = p->buffer;
  92. } else
  93. bp = p->bp;
  94. /*
  95. * Loop through each packet.
  96. */
  97. #define bhp ((struct bpf_hdr *)bp)
  98. ep = bp + cc;
  99. while (bp < ep) {
  100. register int caplen, hdrlen;
  101. caplen = bhp->bh_caplen;
  102. hdrlen = bhp->bh_hdrlen;
  103. /*
  104. * XXX A bpf_hdr matches a pcap_pkthdr.
  105. */
  106. (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen);
  107. bp += BPF_WORDALIGN(caplen + hdrlen);
  108. if (++n >= cnt && cnt > 0) {
  109. p->bp = bp;
  110. p->cc = ep - bp;
  111. return (n);
  112. }
  113. }
  114. #undef bhp
  115. p->cc = 0;
  116. return (n);
  117. }
  118. static inline int
  119. bpf_open(pcap_t *p, char *errbuf)
  120. {
  121. int fd;
  122. int n = 0;
  123. char device[sizeof "/dev/bpf000"];
  124. /*
  125. * Go through all the minors and find one that isn't in use.
  126. */
  127. do {
  128. (void)sprintf(device, "/dev/bpf%d", n++);
  129. fd = open(device, O_RDONLY);
  130. } while (fd < 0 && errno == EBUSY);
  131. /*
  132. * XXX better message for all minors used
  133. */
  134. if (fd < 0)
  135. sprintf(errbuf, "%s: %s", device, pcap_strerror(errno));
  136. return (fd);
  137. }
  138. pcap_t *
  139. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  140. {
  141. int fd;
  142. struct ifreq ifr;
  143. struct bpf_version bv;
  144. u_int v;
  145. pcap_t *p;
  146. p = (pcap_t *)malloc(sizeof(*p));
  147. if (p == NULL) {
  148. sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
  149. return (NULL);
  150. }
  151. bzero(p, sizeof(*p));
  152. fd = bpf_open(p, ebuf);
  153. if (fd < 0)
  154. goto bad;
  155. p->fd = fd;
  156. p->snapshot = snaplen;
  157. if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
  158. sprintf(ebuf, "BIOCVERSION: %s", pcap_strerror(errno));
  159. goto bad;
  160. }
  161. if (bv.bv_major != BPF_MAJOR_VERSION ||
  162. bv.bv_minor < BPF_MINOR_VERSION) {
  163. sprintf(ebuf, "kernel bpf filter out of date");
  164. goto bad;
  165. }
  166. v = 32768; /* XXX this should be a user-accessible hook */
  167. /* Ignore the return value - this is because the call fails on
  168. * BPF systems that don't have kernel malloc. And if the call
  169. * fails, it's no big deal, we just continue to use the standard
  170. * buffer size.
  171. */
  172. (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
  173. (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  174. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
  175. sprintf(ebuf, "%s: %s", device, pcap_strerror(errno));
  176. goto bad;
  177. }
  178. /* Get the data link layer type. */
  179. if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
  180. sprintf(ebuf, "BIOCGDLT: %s", pcap_strerror(errno));
  181. goto bad;
  182. }
  183. #if _BSDI_VERSION - 0 >= 199510
  184. /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
  185. switch (v) {
  186. case DLT_SLIP:
  187. v = DLT_SLIP_BSDOS;
  188. break;
  189. case DLT_PPP:
  190. v = DLT_PPP_BSDOS;
  191. break;
  192. }
  193. #endif
  194. p->linktype = v;
  195. /* set timeout */
  196. if (to_ms != 0) {
  197. struct timeval to;
  198. to.tv_sec = to_ms / 1000;
  199. to.tv_usec = (to_ms * 1000) % 1000000;
  200. if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
  201. sprintf(ebuf, "BIOCSRTIMEOUT: %s",
  202. pcap_strerror(errno));
  203. goto bad;
  204. }
  205. }
  206. if (promisc)
  207. /* set promiscuous mode, okay if it fails */
  208. (void)ioctl(p->fd, BIOCPROMISC, NULL);
  209. if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
  210. sprintf(ebuf, "BIOCGBLEN: %s", pcap_strerror(errno));
  211. goto bad;
  212. }
  213. p->bufsize = v;
  214. p->buffer = (u_char *)malloc(p->bufsize);
  215. if (p->buffer == NULL) {
  216. sprintf(ebuf, "malloc: %s", pcap_strerror(errno));
  217. goto bad;
  218. }
  219. return (p);
  220. bad:
  221. (void)close(fd);
  222. free(p);
  223. return (NULL);
  224. }
  225. int
  226. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  227. {
  228. if (p->sf.rfile != NULL)
  229. p->fcode = *fp;
  230. else if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
  231. sprintf(p->errbuf, "BIOCSETF: %s", pcap_strerror(errno));
  232. return (-1);
  233. }
  234. return (0);
  235. }