dlpisubs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * This code is derived from code formerly in pcap-dlpi.c, originally
  3. * contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), University College
  4. * London, and subsequently modified by Guy Harris (guy@alum.mit.edu),
  5. * Mark Pizzolato <List-tcpdump-workers@subscriptions.pizzolato.net>,
  6. * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.COM>.
  7. */
  8. /*
  9. * This file contains dlpi/libdlpi related common functions used
  10. * by pcap-[dlpi,libdlpi].c.
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #ifndef DL_IPATM
  16. #define DL_IPATM 0x12 /* ATM Classical IP interface */
  17. #endif
  18. #ifdef HAVE_SYS_BUFMOD_H
  19. /*
  20. * Size of a bufmod chunk to pass upstream; that appears to be the
  21. * biggest value to which you can set it, and setting it to that value
  22. * (which is bigger than what appears to be the Solaris default of 8192)
  23. * reduces the number of packet drops.
  24. */
  25. #define CHUNKSIZE 65536
  26. /*
  27. * Size of the buffer to allocate for packet data we read; it must be
  28. * large enough to hold a chunk.
  29. */
  30. #define PKTBUFSIZE CHUNKSIZE
  31. #else /* HAVE_SYS_BUFMOD_H */
  32. /*
  33. * Size of the buffer to allocate for packet data we read; this is
  34. * what the value used to be - there's no particular reason why it
  35. * should be tied to MAXDLBUF, but we'll leave it as this for now.
  36. */
  37. #define MAXDLBUF 8192
  38. #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32))
  39. #endif
  40. #include <sys/types.h>
  41. #include <sys/time.h>
  42. #ifdef HAVE_SYS_BUFMOD_H
  43. #include <sys/bufmod.h>
  44. #endif
  45. #include <sys/dlpi.h>
  46. #include <sys/stream.h>
  47. #include <errno.h>
  48. #include <memory.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <stropts.h>
  53. #include <unistd.h>
  54. #ifdef HAVE_LIBDLPI
  55. #include <libdlpi.h>
  56. #endif
  57. #include "pcap-int.h"
  58. #include "dlpisubs.h"
  59. #ifdef HAVE_SYS_BUFMOD_H
  60. static void pcap_stream_err(const char *, int, char *);
  61. #endif
  62. /*
  63. * Get the packet statistics.
  64. */
  65. int
  66. pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps)
  67. {
  68. struct pcap_dlpi *pd = p->priv;
  69. /*
  70. * "ps_recv" counts packets handed to the filter, not packets
  71. * that passed the filter. As filtering is done in userland,
  72. * this would not include packets dropped because we ran out
  73. * of buffer space; in order to make this more like other
  74. * platforms (Linux 2.4 and later, BSDs with BPF), where the
  75. * "packets received" count includes packets received but dropped
  76. * due to running out of buffer space, and to keep from confusing
  77. * applications that, for example, compute packet drop percentages,
  78. * we also make it count packets dropped by "bufmod" (otherwise we
  79. * might run the risk of the packet drop count being bigger than
  80. * the received-packet count).
  81. *
  82. * "ps_drop" counts packets dropped by "bufmod" because of
  83. * flow control requirements or resource exhaustion; it doesn't
  84. * count packets dropped by the interface driver, or packets
  85. * dropped upstream. As filtering is done in userland, it counts
  86. * packets regardless of whether they would've passed the filter.
  87. *
  88. * These statistics don't include packets not yet read from
  89. * the kernel by libpcap, but they may include packets not
  90. * yet read from libpcap by the application.
  91. */
  92. *ps = pd->stat;
  93. /*
  94. * Add in the drop count, as per the above comment.
  95. */
  96. ps->ps_recv += ps->ps_drop;
  97. return (0);
  98. }
  99. /*
  100. * Loop through the packets and call the callback for each packet.
  101. * Return the number of packets read.
  102. */
  103. int
  104. pcap_process_pkts(pcap_t *p, pcap_handler callback, u_char *user,
  105. int count, u_char *bufp, int len)
  106. {
  107. struct pcap_dlpi *pd = p->priv;
  108. int n, caplen, origlen;
  109. u_char *ep, *pk;
  110. struct pcap_pkthdr pkthdr;
  111. #ifdef HAVE_SYS_BUFMOD_H
  112. struct sb_hdr *sbp;
  113. #ifdef LBL_ALIGN
  114. struct sb_hdr sbhdr;
  115. #endif
  116. #endif
  117. /* Loop through packets */
  118. ep = bufp + len;
  119. n = 0;
  120. #ifdef HAVE_SYS_BUFMOD_H
  121. while (bufp < ep) {
  122. /*
  123. * Has "pcap_breakloop()" been called?
  124. * If so, return immediately - if we haven't read any
  125. * packets, clear the flag and return -2 to indicate
  126. * that we were told to break out of the loop, otherwise
  127. * leave the flag set, so that the *next* call will break
  128. * out of the loop without having read any packets, and
  129. * return the number of packets we've processed so far.
  130. */
  131. if (p->break_loop) {
  132. if (n == 0) {
  133. p->break_loop = 0;
  134. return (-2);
  135. } else {
  136. p->bp = bufp;
  137. p->cc = ep - bufp;
  138. return (n);
  139. }
  140. }
  141. #ifdef LBL_ALIGN
  142. if ((long)bufp & 3) {
  143. sbp = &sbhdr;
  144. memcpy(sbp, bufp, sizeof(*sbp));
  145. } else
  146. #endif
  147. sbp = (struct sb_hdr *)bufp;
  148. pd->stat.ps_drop = sbp->sbh_drops;
  149. pk = bufp + sizeof(*sbp);
  150. bufp += sbp->sbh_totlen;
  151. origlen = sbp->sbh_origlen;
  152. caplen = sbp->sbh_msglen;
  153. #else
  154. origlen = len;
  155. caplen = min(p->snapshot, len);
  156. pk = bufp;
  157. bufp += caplen;
  158. #endif
  159. ++pd->stat.ps_recv;
  160. if (bpf_filter(p->fcode.bf_insns, pk, origlen, caplen)) {
  161. #ifdef HAVE_SYS_BUFMOD_H
  162. pkthdr.ts.tv_sec = sbp->sbh_timestamp.tv_sec;
  163. pkthdr.ts.tv_usec = sbp->sbh_timestamp.tv_usec;
  164. #else
  165. (void) gettimeofday(&pkthdr.ts, NULL);
  166. #endif
  167. pkthdr.len = origlen;
  168. pkthdr.caplen = caplen;
  169. /* Insure caplen does not exceed snapshot */
  170. if (pkthdr.caplen > (bpf_u_int32)p->snapshot)
  171. pkthdr.caplen = (bpf_u_int32)p->snapshot;
  172. (*callback)(user, &pkthdr, pk);
  173. if (++n >= count && !PACKET_COUNT_IS_UNLIMITED(count)) {
  174. p->cc = ep - bufp;
  175. p->bp = bufp;
  176. return (n);
  177. }
  178. }
  179. #ifdef HAVE_SYS_BUFMOD_H
  180. }
  181. #endif
  182. p->cc = 0;
  183. return (n);
  184. }
  185. /*
  186. * Process the mac type. Returns -1 if no matching mac type found, otherwise 0.
  187. */
  188. int
  189. pcap_process_mactype(pcap_t *p, u_int mactype)
  190. {
  191. int retv = 0;
  192. switch (mactype) {
  193. case DL_CSMACD:
  194. case DL_ETHER:
  195. p->linktype = DLT_EN10MB;
  196. p->offset = 2;
  197. /*
  198. * This is (presumably) a real Ethernet capture; give it a
  199. * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
  200. * that an application can let you choose it, in case you're
  201. * capturing DOCSIS traffic that a Cisco Cable Modem
  202. * Termination System is putting out onto an Ethernet (it
  203. * doesn't put an Ethernet header onto the wire, it puts raw
  204. * DOCSIS frames out on the wire inside the low-level
  205. * Ethernet framing).
  206. */
  207. p->dlt_list = (u_int *)malloc(sizeof(u_int) * 2);
  208. /*
  209. * If that fails, just leave the list empty.
  210. */
  211. if (p->dlt_list != NULL) {
  212. p->dlt_list[0] = DLT_EN10MB;
  213. p->dlt_list[1] = DLT_DOCSIS;
  214. p->dlt_count = 2;
  215. }
  216. break;
  217. case DL_FDDI:
  218. p->linktype = DLT_FDDI;
  219. p->offset = 3;
  220. break;
  221. case DL_TPR:
  222. /* XXX - what about DL_TPB? Is that Token Bus? */
  223. p->linktype = DLT_IEEE802;
  224. p->offset = 2;
  225. break;
  226. #ifdef HAVE_SOLARIS
  227. case DL_IPATM:
  228. p->linktype = DLT_SUNATM;
  229. p->offset = 0; /* works for LANE and LLC encapsulation */
  230. break;
  231. #endif
  232. #ifdef DL_IPV4
  233. case DL_IPV4:
  234. p->linktype = DLT_IPV4;
  235. p->offset = 0;
  236. break;
  237. #endif
  238. #ifdef DL_IPV6
  239. case DL_IPV6:
  240. p->linktype = DLT_IPV6;
  241. p->offset = 0;
  242. break;
  243. #endif
  244. #ifdef DL_IPNET
  245. case DL_IPNET:
  246. /*
  247. * XXX - DL_IPNET devices default to "raw IP" rather than
  248. * "IPNET header"; see
  249. *
  250. * http://seclists.org/tcpdump/2009/q1/202
  251. *
  252. * We'd have to do DL_IOC_IPNET_INFO to enable getting
  253. * the IPNET header.
  254. */
  255. p->linktype = DLT_RAW;
  256. p->offset = 0;
  257. break;
  258. #endif
  259. default:
  260. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown mactype 0x%x",
  261. mactype);
  262. retv = -1;
  263. }
  264. return (retv);
  265. }
  266. #ifdef HAVE_SYS_BUFMOD_H
  267. /*
  268. * Push and configure the buffer module. Returns -1 for error, otherwise 0.
  269. */
  270. int
  271. pcap_conf_bufmod(pcap_t *p, int snaplen)
  272. {
  273. struct timeval to;
  274. bpf_u_int32 ss, chunksize;
  275. /* Non-standard call to get the data nicely buffered. */
  276. if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
  277. pcap_stream_err("I_PUSH bufmod", errno, p->errbuf);
  278. return (-1);
  279. }
  280. ss = snaplen;
  281. if (ss > 0 &&
  282. strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
  283. pcap_stream_err("SBIOCSSNAP", errno, p->errbuf);
  284. return (-1);
  285. }
  286. if (p->opt.immediate) {
  287. /* Set the timeout to zero, for immediate delivery. */
  288. to.tv_sec = 0;
  289. to.tv_usec = 0;
  290. if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
  291. pcap_stream_err("SBIOCSTIME", errno, p->errbuf);
  292. return (-1);
  293. }
  294. } else {
  295. /* Set up the bufmod timeout. */
  296. if (p->opt.timeout != 0) {
  297. to.tv_sec = p->opt.timeout / 1000;
  298. to.tv_usec = (p->opt.timeout * 1000) % 1000000;
  299. if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
  300. pcap_stream_err("SBIOCSTIME", errno, p->errbuf);
  301. return (-1);
  302. }
  303. }
  304. /* Set the chunk length. */
  305. chunksize = CHUNKSIZE;
  306. if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize)
  307. != 0) {
  308. pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf);
  309. return (-1);
  310. }
  311. }
  312. return (0);
  313. }
  314. #endif /* HAVE_SYS_BUFMOD_H */
  315. /*
  316. * Allocate data buffer. Returns -1 if memory allocation fails, else 0.
  317. */
  318. int
  319. pcap_alloc_databuf(pcap_t *p)
  320. {
  321. p->bufsize = PKTBUFSIZE;
  322. p->buffer = malloc(p->bufsize + p->offset);
  323. if (p->buffer == NULL) {
  324. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  325. errno, "malloc");
  326. return (-1);
  327. }
  328. return (0);
  329. }
  330. /*
  331. * Issue a STREAMS I_STR ioctl. Returns -1 on error, otherwise
  332. * length of returned data on success.
  333. */
  334. int
  335. strioctl(int fd, int cmd, int len, char *dp)
  336. {
  337. struct strioctl str;
  338. int retv;
  339. str.ic_cmd = cmd;
  340. str.ic_timout = -1;
  341. str.ic_len = len;
  342. str.ic_dp = dp;
  343. if ((retv = ioctl(fd, I_STR, &str)) < 0)
  344. return (retv);
  345. return (str.ic_len);
  346. }
  347. #ifdef HAVE_SYS_BUFMOD_H
  348. /*
  349. * Write stream error message to errbuf.
  350. */
  351. static void
  352. pcap_stream_err(const char *func, int err, char *errbuf)
  353. {
  354. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, err, "%s", func);
  355. }
  356. #endif